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