Go to the documentation of this file.00001
00053 #include "avr_compiler.h"
00054 #include "spi_driver.h"
00055
00056
00058 #define NUM_BYTES 4
00059
00060
00061
00063 SPI_Master_t spiMasterC;
00064
00066 SPI_Slave_t spiSlaveD;
00067
00069 SPI_DataPacket_t dataPacket;
00070
00072 uint8_t masterSendData[NUM_BYTES] = {0x11, 0x22, 0x33, 0x44};
00073
00075 uint8_t masterReceivedData[NUM_BYTES];
00076
00078 bool success = true;
00079
00080
00081
00108 int main(void)
00109 {
00110
00111 PORTC.DIRSET = PIN4_bm;
00112 PORTC.PIN4CTRL = PORT_OPC_WIREDANDPULL_gc;
00113
00114
00115 PORTC.OUTSET = PIN4_bm;
00116
00117
00118 PORT_t *ssPort = &PORTC;
00119
00120
00121 SPI_MasterInit(&spiMasterC,
00122 &SPIC,
00123 &PORTC,
00124 false,
00125 SPI_MODE_0_gc,
00126 SPI_INTLVL_OFF_gc,
00127 false,
00128 SPI_PRESCALER_DIV4_gc);
00129
00130
00131 SPI_SlaveInit(&spiSlaveD,
00132 &SPID,
00133 &PORTD,
00134 false,
00135 SPI_MODE_0_gc,
00136 SPI_INTLVL_OFF_gc);
00137
00138
00139
00140
00141
00142 SPI_MasterSSLow(ssPort, PIN4_bm);
00143
00144 for(uint8_t i = 0; i < NUM_BYTES; i++) {
00145
00146 SPI_MasterTransceiveByte(&spiMasterC, masterSendData[i]);
00147
00148
00149 while (SPI_SlaveDataAvailable(&spiSlaveD) == false) {
00150
00151 }
00152
00153
00154 uint8_t slaveByte = SPI_SlaveReadByte(&spiSlaveD);
00155
00156
00157 slaveByte++;
00158 SPI_SlaveWriteByte(&spiSlaveD, slaveByte);
00159
00160
00161 uint8_t masterReceivedByte = SPI_MasterTransceiveByte(&spiMasterC, 0x00);
00162
00163
00164 if (masterReceivedByte != (masterSendData[i] + 1) ) {
00165 success = false;
00166 }
00167 }
00168
00169
00170 SPI_MasterSSHigh(ssPort, PIN4_bm);
00171
00172
00173
00174
00175 SPI_MasterCreateDataPacket(&dataPacket,
00176 masterSendData,
00177 masterReceivedData,
00178 NUM_BYTES,
00179 &PORTC,
00180 PIN4_bm);
00181
00182
00183 SPI_MasterTransceivePacket(&spiMasterC, &dataPacket);
00184
00185
00186 for (uint8_t i = 0; i < NUM_BYTES - 1; i++) {
00187 if (masterReceivedData[i + 1] != masterSendData[i]) {
00188 success = false;
00189 }
00190 }
00191
00192 while(true) {
00193 nop();
00194 }
00195 }