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