00001
00050 #include "avr_compiler.h"
00051 #include "spi_driver.h"
00052
00054 #define NUM_BYTES 2
00055
00056
00057
00059 SPI_Master_t spiMasterC;
00060
00062 SPI_Slave_t spiSlaveD = {NULL, NULL};
00063
00065 SPI_DataPacket_t dataPacket;
00066
00068 const uint8_t sendData[NUM_BYTES + 1] = { 0x55, 0xaa, 0x00 };
00069
00071 uint8_t receivedData[NUM_BYTES + 1];
00072
00074 bool success;
00075
00076
00077
00104 int main( void )
00105 {
00106
00107 PORTC.DIRSET = PIN4_bm;
00108 PORTC.PIN4CTRL = PORT_OPC_WIREDANDPULL_gc;
00109
00110
00111 PORTC.OUTSET = PIN4_bm;
00112
00113
00114 SPI_MasterInit(&spiMasterC,
00115 &SPIC,
00116 &PORTC,
00117 false,
00118 SPI_MODE_0_gc,
00119 SPI_INTLVL_LO_gc,
00120 false,
00121 SPI_PRESCALER_DIV4_gc);
00122
00123
00124 SPI_SlaveInit(&spiSlaveD,
00125 &SPID,
00126 &PORTD,
00127 false,
00128 SPI_MODE_0_gc,
00129 SPI_INTLVL_MED_gc);
00130
00131
00132 PMIC.CTRL |= PMIC_MEDLVLEN_bm | PMIC_LOLVLEN_bm;
00133 sei();
00134
00135
00136 SPI_MasterCreateDataPacket(&dataPacket,
00137 sendData,
00138 receivedData,
00139 NUM_BYTES + 1,
00140 &PORTC,
00141 PIN4_bm);
00142
00143
00144 uint8_t status;
00145 do {
00146 status = SPI_MasterInterruptTransceivePacket(&spiMasterC, &dataPacket);
00147 } while (status != SPI_OK);
00148
00149
00150 while (dataPacket.complete == false) {
00151
00152 }
00153
00154
00155 success = true;
00156 for (uint8_t i = 0; i < NUM_BYTES; i++) {
00157 if (receivedData[i + 1] != (uint8_t)(sendData[i] + 1)) {
00158 success = false;
00159 }
00160 }
00161 while(true) {
00162 nop();
00163 }
00164 }
00165
00166
00175 ISR(SPIC_INT_vect)
00176 {
00177 SPI_MasterInterruptHandler(&spiMasterC);
00178 }
00179
00180
00181
00192 ISR(SPID_INT_vect)
00193 {
00194
00195 uint8_t data = SPI_SlaveReadByte(&spiSlaveD);
00196
00197
00198 data++;
00199
00200
00201 SPI_SlaveWriteByte(&spiSlaveD, data);
00202 }