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:
5407
Date:
2011-10-12 14:53:14 +0200 (on, 12 okt 2011)


Copyright (c) 2009 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.

4. This software may only be redistributed and used in connection with an Atmel AVR product.

THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT 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 58 of file spi_polled_example.c.

Referenced by main().


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 108 of file spi_polled_example.c.

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

{
        /* Init SS pin as output with wired AND and pull-up. */
        PORTC.DIRSET = PIN4_bm;
        PORTC.PIN4CTRL = PORT_OPC_WIREDANDPULL_gc;

        /* Set SS output to high. (No slave addressed). */
        PORTC.OUTSET = PIN4_bm;

        /* Instantiate pointer to ssPort. */
        PORT_t *ssPort = &PORTC;

        /* Initialize SPI master on port C. */
        SPI_MasterInit(&spiMasterC,
                       &SPIC,
                       &PORTC,
                       false,
                       SPI_MODE_0_gc,
                       SPI_INTLVL_OFF_gc,
                       false,
                       SPI_PRESCALER_DIV4_gc);

        /* Initialize SPI slave on port D. */
        SPI_SlaveInit(&spiSlaveD,
                      &SPID,
                      &PORTD,
                      false,
                                  SPI_MODE_0_gc,
                                  SPI_INTLVL_OFF_gc);

        /* PHASE 1: Transceive individual bytes. */

        /* MASTER: Pull SS line low. This has to be done since
         *         SPI_MasterTransceiveByte() does not control the SS line(s). */
        SPI_MasterSSLow(ssPort, PIN4_bm);

        for(uint8_t i = 0; i < NUM_BYTES; i++) {
                /* MASTER: Transmit data from master to slave. */
                SPI_MasterTransceiveByte(&spiMasterC, masterSendData[i]);

                /* SLAVE: Wait for data to be available. */
                while (SPI_SlaveDataAvailable(&spiSlaveD) == false) {

                }

                /* SLAVE: Get the byte received. */
                uint8_t slaveByte = SPI_SlaveReadByte(&spiSlaveD);

                /* SLAVE: Increment received byte and send back. */
                slaveByte++;
                SPI_SlaveWriteByte(&spiSlaveD, slaveByte);

                /* MASTER: Transmit dummy data to shift data from slave to master. */
                uint8_t masterReceivedByte = SPI_MasterTransceiveByte(&spiMasterC, 0x00);

                /* MASTER: Check if the correct value was received. */
                if (masterReceivedByte != (masterSendData[i] + 1) ) {
                        success = false;
                }
        }

        /* MASTER: Release SS to slave. */
        SPI_MasterSSHigh(ssPort, PIN4_bm);

        /* PHASE 2: Transceive data packet. */

        /* Create data packet (SS to slave by PC4). */
        SPI_MasterCreateDataPacket(&dataPacket,
                                   masterSendData,
                                   masterReceivedData,
                                   NUM_BYTES,
                                   &PORTC,
                                   PIN4_bm);

        /* Transceive packet. */
        SPI_MasterTransceivePacket(&spiMasterC, &dataPacket);

        /* Check that correct data was received. Assume success at first. */
        for (uint8_t i = 0; i < NUM_BYTES - 1; i++) {
                if (masterReceivedData[i + 1] != masterSendData[i]) {
                        success = false;
                }
        }

        while(true) {
                nop();
        }
}

Here is the call graph for this function:


Variable Documentation

SPI Data packet.

Definition at line 69 of file spi_polled_example.c.

uint8_t masterReceivedData[NUM_BYTES]

Data received from slave.

Definition at line 75 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 72 of file spi_polled_example.c.

Referenced by main().

SPI master module on PORT C.

Definition at line 63 of file spi_polled_example.c.

SPI slave module on PORT D.

Definition at line 66 of file spi_polled_example.c.

bool success = true

Result of the test.

Definition at line 78 of file spi_polled_example.c.