Xmega Application Note


spi_interrupt_example.c File Reference

XMEGA SPI interrupt-driven driver example source. More...

#include "avr_compiler.h"
#include "spi_driver.h"

Include dependency graph for spi_interrupt_example.c:

Go to the source code of this file.

Defines

#define NUM_BYTES   2
 Number of test data bytes.

Functions

 ISR (SPID_INT_vect)
 SPI slave interrupt service routine.
 ISR (SPIC_INT_vect)
 SPI master interrupt service routine.
int main (void)
 Test function.

Variables

SPI_DataPacket_t dataPacket
 Data packet.
uint8_t receivedData [NUM_BYTES+1]
 Buffer for test data reception.
const uint8_t sendData [NUM_BYTES+1] = { 0x55, 0xaa, 0x00 }
 Test data to send.
SPI_Master_t spiMasterC
 SPI master on PORT C.
SPI_Slave_t spiSlaveD = {NULL, NULL}
 SPI slave on PORT D.
bool success
 Result of the example test.


Detailed Description

XMEGA SPI interrupt-driven driver example source.

This file contains an example application that demonstrates the SPI interrupt-driven driver.

Application note:
AVR1309: Using the XMEGA SPI
Documentation
For comprehensive code documentation, supported compilers, compiler settings and supported devices see readme.html
Author:
Atmel Corporation: http://www.atmel.com
Support email: avr@atmel.com
Revision
763
Date
2007-11-06 14:51:56 +0100 (ti, 06 nov 2007)

Copyright (c) 2008, Atmel Corporation All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

3. The name of ATMEL may not be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Definition in file spi_interrupt_example.c.


Define Documentation

#define NUM_BYTES   2

Number of test data bytes.

Definition at line 54 of file spi_interrupt_example.c.

Referenced by main().


Function Documentation

ISR ( SPID_INT_vect   ) 

SPI slave interrupt service routine.

This ISR increments the received data and stores it in the data register, read to be shifted back by the master.

If more extensive computation is needed, it is recommended to do this in a function, which is then called by the ISR.

Similar ISRs must be added if other SPI modules are to be used.

Definition at line 192 of file spi_interrupt_example.c.

References SPI_SlaveReadByte, and SPI_SlaveWriteByte.

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 }

ISR ( SPIC_INT_vect   ) 

SPI master interrupt service routine.

The interrupt service routines calls one common function, SPI_MasterInterruptHandler(SPI_Master_t *spi), passing information about what module to handle.

Similar ISRs must be added if other SPI modules are to be used.

Definition at line 175 of file spi_interrupt_example.c.

References SPI_MasterInterruptHandler().

00176 {
00177         SPI_MasterInterruptHandler(&spiMasterC);
00178 }

Here is the call graph for this function:

int main ( void   ) 

Test function.

This function tests the SPI master and slave drivers in interrupt-driven operation, with a master (on port C) communicating with a slave (on port D).

Hardware setup:

  • Connect PC4 to PD4 (SS)
  • Connect PC5 to PD5 (MOSI)
  • Connect PC6 to PD6 (MISO)
  • Connect PC7 to PD7 (SCK)

The driver is tested by transmitting data from the master to the slave. The slave increments the received data and sends it back. The master reads the data from the slave and verifies that it equals the data sent + 1.

The first data transaction is initiated by the main routine. When a transaction has finished, an interrupt will be triggered which will start new transactions until all bytes have been transceived.

The variable, 'success', will be non-zero when the function reaches the infinite for-loop if the test was successful.

Note:
This example uses multilevel interrupts. For more information on how to use the interrupt controller, refer to application note AVR1305.

Definition at line 104 of file spi_interrupt_example.c.

References SPI_DataPacket::complete, NUM_BYTES, receivedData, sendData, SPI_MasterCreateDataPacket(), SPI_MasterInit(), SPI_MasterInterruptTransceivePacket(), SPI_OK, SPI_SlaveInit(), and success.

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 }

Here is the call graph for this function:


Variable Documentation

Data packet.

Definition at line 65 of file spi_interrupt_example.c.

Referenced by SPI_MasterInterruptHandler().

uint8_t receivedData[NUM_BYTES+1]

Buffer for test data reception.

Definition at line 71 of file spi_interrupt_example.c.

Referenced by main().

const uint8_t sendData[NUM_BYTES+1] = { 0x55, 0xaa, 0x00 }

Test data to send.

Definition at line 68 of file spi_interrupt_example.c.

Referenced by main().

SPI master on PORT C.

Definition at line 59 of file spi_interrupt_example.c.

SPI_Slave_t spiSlaveD = {NULL, NULL}

SPI slave on PORT D.

Definition at line 62 of file spi_interrupt_example.c.

bool success

Result of the example test.

Definition at line 74 of file spi_interrupt_example.c.

Referenced by main().

@DOC_TITLE@
Generated on Mon Nov 2 13:52:28 2009 for AVR1309 Using the XMEGA SPI by doxygen 1.5.9