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