Xmega Application Note


spi_polled_example.c
Go to the documentation of this file.
00001 /* This file has been prepared for Doxygen automatic documentation generation.*/
00053 #include "avr_compiler.h"
00054 #include "spi_driver.h"
00055 
00056 
00058 #define NUM_BYTES     4
00059 
00060 /* Global variables */
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         /* Init SS pin as output with wired AND and pull-up. */
00111         PORTC.DIRSET = PIN4_bm;
00112         PORTC.PIN4CTRL = PORT_OPC_WIREDANDPULL_gc;
00113 
00114         /* Set SS output to high. (No slave addressed). */
00115         PORTC.OUTSET = PIN4_bm;
00116 
00117         /* Instantiate pointer to ssPort. */
00118         PORT_t *ssPort = &PORTC;
00119 
00120         /* Initialize SPI master on port C. */
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         /* Initialize SPI slave on port D. */
00131         SPI_SlaveInit(&spiSlaveD,
00132                       &SPID,
00133                       &PORTD,
00134                       false,
00135                                   SPI_MODE_0_gc,
00136                                   SPI_INTLVL_OFF_gc);
00137 
00138         /* PHASE 1: Transceive individual bytes. */
00139 
00140         /* MASTER: Pull SS line low. This has to be done since
00141          *         SPI_MasterTransceiveByte() does not control the SS line(s). */
00142         SPI_MasterSSLow(ssPort, PIN4_bm);
00143 
00144         for(uint8_t i = 0; i < NUM_BYTES; i++) {
00145                 /* MASTER: Transmit data from master to slave. */
00146                 SPI_MasterTransceiveByte(&spiMasterC, masterSendData[i]);
00147 
00148                 /* SLAVE: Wait for data to be available. */
00149                 while (SPI_SlaveDataAvailable(&spiSlaveD) == false) {
00150 
00151                 }
00152 
00153                 /* SLAVE: Get the byte received. */
00154                 uint8_t slaveByte = SPI_SlaveReadByte(&spiSlaveD);
00155 
00156                 /* SLAVE: Increment received byte and send back. */
00157                 slaveByte++;
00158                 SPI_SlaveWriteByte(&spiSlaveD, slaveByte);
00159 
00160                 /* MASTER: Transmit dummy data to shift data from slave to master. */
00161                 uint8_t masterReceivedByte = SPI_MasterTransceiveByte(&spiMasterC, 0x00);
00162 
00163                 /* MASTER: Check if the correct value was received. */
00164                 if (masterReceivedByte != (masterSendData[i] + 1) ) {
00165                         success = false;
00166                 }
00167         }
00168 
00169         /* MASTER: Release SS to slave. */
00170         SPI_MasterSSHigh(ssPort, PIN4_bm);
00171 
00172         /* PHASE 2: Transceive data packet. */
00173 
00174         /* Create data packet (SS to slave by PC4). */
00175         SPI_MasterCreateDataPacket(&dataPacket,
00176                                    masterSendData,
00177                                    masterReceivedData,
00178                                    NUM_BYTES,
00179                                    &PORTC,
00180                                    PIN4_bm);
00181 
00182         /* Transceive packet. */
00183         SPI_MasterTransceivePacket(&spiMasterC, &dataPacket);
00184 
00185         /* Check that correct data was received. Assume success at first. */
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 }