Xmega Application Note


spi_polled_example.c File Reference

XMEGA SPI polled driver example source. More...

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

Include dependency graph for spi_polled_example.c:

Go to the source code of this file.

Defines

#define NUM_BYTES   4
 The number of test data bytes.

Functions

int main (void)
 Test function.

Variables

SPI_DataPacket_t dataPacket
 SPI Data packet.
uint8_t masterReceivedData [NUM_BYTES]
 Data received from slave.
uint8_t masterSendData [NUM_BYTES] = {0x11, 0x22, 0x33, 0x44}
 Test data to send from master.
SPI_Master_t spiMasterC
 SPI master module on PORT C.
SPI_Slave_t spiSlaveD
 SPI slave module on PORT D.
bool success = true
 Result of the test.


Detailed Description

XMEGA SPI polled driver example source.

This file contains an example application that demonstrates the polled SPI drivers.

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
771
Date
2007-11-06 14:54:00 +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_polled_example.c.


Define Documentation

#define NUM_BYTES   4

The number of test data bytes.

Definition at line 55 of file spi_polled_example.c.


Function Documentation

int main ( void   ) 

Test function.

This function tests the SPI master and slave drivers in polled 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 drivers are tested in two phases:

1: Data is transmitted on byte at a time 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.

2: Data is transmitted 4 bytes at a time to the slave. As the master sends a byte to the slave, the preceding byte is sent back to the master. When all bytes have been sent, it is verified that the last 3 bytes received at the master, equal the first 3 bytes sent.

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

Definition at line 105 of file spi_polled_example.c.

References masterReceivedData, masterSendData, NUM_BYTES, SPI_MasterCreateDataPacket(), SPI_MasterInit(), SPI_MasterSSHigh, SPI_MasterSSLow, SPI_MasterTransceiveByte(), SPI_MasterTransceivePacket(), SPI_SlaveDataAvailable, SPI_SlaveInit(), SPI_SlaveReadByte, SPI_SlaveWriteByte, and success.

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 }

Here is the call graph for this function:


Variable Documentation

SPI Data packet.

Definition at line 66 of file spi_polled_example.c.

uint8_t masterReceivedData[NUM_BYTES]

Data received from slave.

Definition at line 72 of file spi_polled_example.c.

Referenced by main().

uint8_t masterSendData[NUM_BYTES] = {0x11, 0x22, 0x33, 0x44}

Test data to send from master.

Definition at line 69 of file spi_polled_example.c.

Referenced by main().

SPI master module on PORT C.

Definition at line 60 of file spi_polled_example.c.

SPI slave module on PORT D.

Definition at line 63 of file spi_polled_example.c.

bool success = true

Result of the test.

Definition at line 75 of file spi_polled_example.c.

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