Xmega Application Note


spi_interrupt_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 
00057 #define NUM_BYTES   2
00058 
00059 /* Global variables. */
00060 
00062 SPI_Master_t spiMasterC;
00063 
00065 SPI_Slave_t spiSlaveD = {NULL, NULL};
00066 
00068 SPI_DataPacket_t dataPacket;
00069 
00071 const uint8_t sendData[NUM_BYTES + 1] = { 0x55, 0xaa, 0x00 };
00072 
00074 uint8_t receivedData[NUM_BYTES + 1];
00075 
00077 bool success;
00078 
00079 
00080 
00107 int main( void )
00108 {
00109         /* Init SS pin as output with wired AND and pull-up. */
00110         PORTC.DIRSET = PIN4_bm;
00111         PORTC.PIN4CTRL = PORT_OPC_WIREDANDPULL_gc;
00112 
00113         /* Set SS output to high. (No slave addressed). */
00114         PORTC.OUTSET = PIN4_bm;
00115 
00116         /* Initialize SPI master on port C. */
00117         SPI_MasterInit(&spiMasterC,
00118                        &SPIC,
00119                        &PORTC,
00120                                    false,
00121                        SPI_MODE_0_gc,
00122                        SPI_INTLVL_LO_gc,
00123                        false,
00124                        SPI_PRESCALER_DIV4_gc);
00125 
00126         /* Initialize SPI slave on port D. */
00127         SPI_SlaveInit(&spiSlaveD,
00128                       &SPID,
00129                       &PORTD,
00130                       false,
00131                       SPI_MODE_0_gc,
00132                       SPI_INTLVL_MED_gc);
00133 
00134         /* Enable low and medium level interrupts in the interrupt controller. */
00135         PMIC.CTRL |= PMIC_MEDLVLEN_bm | PMIC_LOLVLEN_bm;
00136         sei();
00137 
00138         /* Create data packet (SS to slave by PC4) */
00139         SPI_MasterCreateDataPacket(&dataPacket,
00140                                    sendData,
00141                                    receivedData,
00142                                    NUM_BYTES + 1,
00143                                    &PORTC,
00144                                    PIN4_bm);
00145 
00146         /* Transmit and receive first data byte. */
00147         uint8_t status;
00148         do {
00149                 status = SPI_MasterInterruptTransceivePacket(&spiMasterC, &dataPacket);
00150         } while (status != SPI_OK);
00151 
00152         /* Wait for transmission to complete. */
00153         while (dataPacket.complete == false) {
00154 
00155         }
00156 
00157         /* Check that correct data was received. Assume success at first. */
00158         success = true;
00159         for (uint8_t i = 0; i < NUM_BYTES; i++) {
00160                 if (receivedData[i + 1] != (uint8_t)(sendData[i] + 1)) {
00161                         success = false;
00162                 }
00163         }
00164         while(true) {
00165                 nop();
00166         }
00167 }
00168 
00169 
00178 ISR(SPIC_INT_vect)
00179 {
00180         SPI_MasterInterruptHandler(&spiMasterC);
00181 }
00182 
00183 
00184 
00195 ISR(SPID_INT_vect)
00196 {
00197         /* Get received data. */
00198         uint8_t data = SPI_SlaveReadByte(&spiSlaveD);
00199 
00200         /* Increment data. */
00201         data++;
00202 
00203         /* Send back incremented value. */
00204         SPI_SlaveWriteByte(&spiSlaveD, data);
00205 }