AVR319: Using the USI module for SPI communication. 8-bit Microcontrollers. Application Note. Features. Introduction



Similar documents
AVR317: Using the Master SPI Mode of the USART module. 8-bit Microcontrollers. Application Note. Features. Introduction

General Porting Considerations. Memory EEPROM XRAM

AVR305: Half Duplex Compact Software UART. 8-bit Microcontrollers. Application Note. Features. 1 Introduction

AVR134: Real Time Clock (RTC) using the Asynchronous Timer. 8-bit Microcontrollers. Application Note. Features. 1 Introduction

AVR106: C functions for reading and writing to Flash memory. 8-bit Microcontrollers. Application Note. Features. Introduction

8-bit Microcontroller. Application Note. AVR400: Low Cost A/D Converter

AT89C5131A Starter Kit... Software User Guide

8-bit Microcontroller. Application Note. AVR222: 8-point Moving Average Filter

AVR1309: Using the XMEGA SPI. 8-bit Microcontrollers. Application Note. Features. 1 Introduction SCK MOSI MISO SS

8-bit Microcontroller. Application Note. AVR105: Power Efficient High Endurance Parameter Storage in Flash Memory

AVR245: Code Lock with 4x4 Keypad and I2C LCD. 8-bit Microcontrollers. Application Note. Features. 1 Introduction

Application Note. C51 Bootloaders. C51 General Information about Bootloader and In System Programming. Overview. Abreviations

8-bit Microcontroller. Application Note. AVR415: RC5 IR Remote Control Transmitter. Features. Introduction. Figure 1.

8-bit RISC Microcontroller. Application Note. AVR182: Zero Cross Detector

AVR030: Getting Started with IAR Embedded Workbench for Atmel AVR. 8-bit Microcontrollers. Application Note. Features.

8-bit RISC Microcontroller. Application Note. AVR236: CRC Check of Program Memory

AVR32110: Using the AVR32 Timer/Counter. 32-bit Microcontrollers. Application Note. Features. 1 Introduction

AT91 ARM Thumb Microcontrollers. Application Note. Interfacing a PC Card to an AT91RM9200-DK. Introduction. Hardware Interface

How to Calculate the Capacitor of the Reset Input of a C51 Microcontroller 80C51. Application Note. Microcontrollers. Introduction

AVR32701: AVR32AP7 USB Performance. 32-bit Microcontrollers. Application Note. Features. 1 Introduction

8-bit Microcontroller. Application Note. AVR314: DTMF Generator

AVR151: Setup and Use of the SPI. Introduction. Features. Atmel AVR 8-bit Microcontroller APPLICATION NOTE

AT91 ARM Thumb Microcontrollers. AT91SAM CAN Bootloader. AT91SAM CAN Bootloader User Notes. 1. Description. 2. Key Features

AVR32100: Using the AVR32 USART. 32-bit Microcontrollers. Application Note. Features. 1 Introduction

AVR1318: Using the XMEGA built-in AES accelerator. 8-bit Microcontrollers. Application Note. Features. 1 Introduction

Quick Start Guide. CAN Microcontrollers. ATADAPCAN01 - STK501 CAN Extension. Requirements

Using CryptoMemory in Full I 2 C Compliant Mode. Using CryptoMemory in Full I 2 C Compliant Mode AT88SC0104CA AT88SC0204CA AT88SC0404CA AT88SC0808CA

Application Note. 8-bit Microcontrollers. AVR307: Half Duplex UART Using the USI Module

8-bit Microcontroller. Application Note. AVR201: Using the AVR Hardware Multiplier

8-bit RISC Microcontroller. Application Note. AVR155: Accessing an I 2 C LCD Display using the AVR 2-wire Serial Interface

APPLICATION NOTE. Atmel AVR134: Real Time Clock (RTC) Using the Asynchronous Timer. Atmel AVR 8-bit Microcontroller. Introduction.

8-bit Microcontroller. Application. Note. AVR204: BCD Arithmetics. Features. Introduction. 16-bit Binary to 5-digit BCD Conversion bin2bcd16

AVR034: Mixing C and Assembly Code with IAR Embedded Workbench for AVR. 8-bit Microcontroller. Application Note. Features.

8-bit Microcontroller. Application Note. AVR134: Real-Time Clock (RTC) using the Asynchronous Timer. Features. Theory of Operation.

AVR442: PC Fan Control using ATtiny13. 8-bit Microcontrollers. Application Note. Features. 1 Introduction

AVR1900: Getting started with ATxmega128A1 on STK bit Microcontrollers. Application Note. 1 Introduction

Tag Tuning/RFID. Application Note. Tag Tuning. Introduction. Antenna Equivalent Circuit

AVR1301: Using the XMEGA DAC. 8-bit Microcontrollers. Application Note. Features. 1 Introduction

AVR1600: Using the XMEGA Quadrature Decoder. 8-bit Microcontrollers. Application Note. Features. 1 Introduction. Sensors

Atmel AVR4921: ASF - USB Device Stack Differences between ASF V1 and V2. 8-bit Atmel Microcontrollers. Application Note. Features.

8-bit Microcontroller. Application Note. AVR461: Quick Start Guide for the Embedded Internet Toolkit. Introduction. System Requirements

AVR241: Direct driving of LCD display using general IO. 8-bit Microcontrollers. Application Note. Features. Introduction AVR

Application Note. 8-bit Microcontrollers. AVR270: USB Mouse Demonstration

AVR033: Getting Started with the CodeVisionAVR C Compiler. 8-bit Microcontrollers. Application Note. Features. 1 Introduction

AVR1510: Xplain training - XMEGA USART. 8-bit Microcontrollers. Application Note. Prerequisites. 1 Introduction

3-output Laser Driver for HD-DVD/ Blu-ray/DVD/ CD-ROM ATR0885. Preliminary. Summary

8-bit. Application Note. Microcontrollers. AVR282: USB Firmware Upgrade for AT90USB

Application Note. 8-bit Microcontrollers. AVR280: USB Host CDC Demonstration. 1. Introduction

USB Test Environment ATUSBTEST- SS7400. Summary

USER GUIDE EDBG. Description

AVR120: Characterization and Calibration of the ADC on an AVR. 8-bit Microcontrollers. Application Note. Features. Introduction

AVR353: Voltage Reference Calibration and Voltage ADC Usage. 8-bit Microcontrollers. Application Note. Features. 1 Introduction

Application Note. USB Mass Storage Device Implementation. USB Microcontrollers. References. Abbreviations. Supported Controllers

AVR115: Data Logging with Atmel File System on ATmega32U4. Microcontrollers. Application Note. 1 Introduction. Atmel

AVR1922: Xplain Board Controller Firmware. 8-bit Microcontrollers. Application Note. Features. 1 Introduction

AVR32138: How to optimize the ADC usage on AT32UC3A0/1, AT32UC3A3 and AT32UC3B0/1 series. 32-bit Microcontrollers. Application Note.

AVR32788: AVR 32 How to use the SSC in I2S mode. 32-bit Microcontrollers. Application Note. Features. 1 Introduction

Atmel AVR4903: ASF - USB Device HID Mouse Application. Atmel Microcontrollers. Application Note. Features. 1 Introduction

USB 2.0 Full-Speed Host/Function Processor AT43USB370. Summary. Features. Overview

32-bit AVR UC3 Microcontrollers. 32-bit AtmelAVR Application Note. AVR32769: How to Compile the standalone AVR32 Software Framework in AVR32 Studio V2

AVR2006: Design and characterization of the Radio Controller Board's 2.4GHz PCB Antenna. Application Note. Features.

How To Use An Atmel Atmel Avr32848 Demo For Android (32Bit) With A Microcontroller (32B) And An Android Accessory (32D) On A Microcontroller (32Gb) On An Android Phone Or

Application Note. Migrating from RS-232 to USB Bridge Specification USB Microcontrollers. Doc Control. References. Abbreviations

Application Note. 8-bit Microcontrollers. AVR091: Replacing AT90S2313 by ATtiny2313. Features. Introduction

AVR287: USB Host HID and Mass Storage Demonstration. 8-bit Microcontrollers. Application Note. Features. 1 Introduction

How To Prevent Power Supply Corruption On An 8Bit Microcontroller From Overheating

AT86RF230 (2450 MHz band) Radio Transceiver... User Guide

Atmel AVR4920: ASF - USB Device Stack - Compliance and Performance Figures. Atmel Microcontrollers. Application Note. Features.

DIP Top View VCC A16 A15 A12 A7 A6 A5 A4 A3 A2 A1 A0 I/O0 I/O1 I/O2 GND A17 A14 A13 A8 A9 A11 A10 I/O7 I/O6 I/O5 I/O4 I/O3. PLCC Top View VCC A17

AVR318: Dallas 1-Wire master. 8-bit Microcontrollers. Application Note. Features. Introduction

Implementing SPI Master and Slave Functionality Using the Z8 Encore! F083A

Table of Contents. Section 1 Introduction Section 2 Getting Started Section 3 Hardware Description

Application Note. 8-bit Microcontrollers. AVR272: USB CDC Demonstration UART to USB Bridge

Two-wire Automotive Serial EEPROM AT24C01A AT24C02 AT24C04 AT24C08 (1) AT24C16 (2)

AVR1321: Using the Atmel AVR XMEGA 32-bit Real Time Counter and Battery Backup System. 8-bit Microcontrollers. Application Note.

8-bit RISC Microcontroller. Application Note. AVR910: In-System Programming

AVR068: STK500 Communication Protocol. 8-bit Microcontrollers. Application Note. Features. 1 Introduction

8-bit Microcontroller. Application Note. AVR410: RC5 IR Remote Control Receiver

AVR055: Using a 32kHz XTAL for run-time calibration of the internal RC. 8-bit Microcontrollers. Application Note. Features.

ATF15xx Product Family Conversion. Application Note. ATF15xx Product Family Conversion. Introduction

8051 Flash Microcontroller. Application Note. A Digital Thermometer Using the Atmel AT89LP2052 Microcontroller

AVR125: ADC of tinyavr in Single Ended Mode. 8-bit Microcontrollers. Application Note. Features. 1 Introduction

APPLICATION NOTE. Atmel AVR443: Sensor-based Control of Three Phase Brushless DC Motor. Atmel AVR 8-bit Microcontrollers. Features.

APPLICATION NOTE. Atmel AT04389: Connecting SAMD20E to the AT86RF233 Transceiver. Atmel SAMD20. Description. Features

AVR444: Sensorless control of 3-phase brushless DC motors. 8-bit Microcontrollers. Application Note. Features. 1 Introduction

Application Note. Atmel ATSHA204 Authentication Modes. Prerequisites. Overview. Introduction

3-output Laser Driver for HD-DVD/ Blu-ray/DVD/ CD-ROM ATR0885. Preliminary. Summary. Features. Applications. 1. Description

AVR131: Using the AVR s High-speed PWM. Introduction. Features. AVR 8-bit Microcontrollers APPLICATION NOTE

STK 500, AVRISP, AVRISP

AT91SAM ARM-based Flash MCU. Application Note

SMARTCARD XPRO. Preface. SMART ARM-based Microcontrollers USER GUIDE

APPLICATION NOTE Atmel AT02509: In House Unit with Bluetooth Low Energy Module Hardware User Guide 8-bit Atmel Microcontroller Features Description

Two-wire Serial EEPROM AT24C1024 (1)

AVR2004: LC-Balun for AT86RF230. Application Note. Features. 1 Introduction

Step Motor Controller. Application Note. AVR360: Step Motor Controller. Theory of Operation. Features. Introduction

2-wire Serial EEPROM AT24C1024. Advance Information

Implementing SPI Communication Between MSP430 G2452 and LTC ADC

Application Note. 8-bit Microcontrollers. AVR293: USB Composite Device

8-bit RISC Microcontroller. Application Note. AVR335: Digital Sound Recorder with AVR and DataFlash

Transcription:

AVR319: Using the USI module for SPI communication Features C-code driver for SPI master and slave Uses the USI module Supports SPI Mode 0 and 1 Introduction The Serial Peripheral Interface (SPI) allows high-speed synchronous data transfer between an AVR device and peripheral devices or between several AVR devices. The strength of the SPI bus includes ease of use, high communication speed and a vast amount of peripheral devices supporting it. The Universal Serial Interface (USI) module on devices like ATmega169, ATtiny26 and ATtiny2313 has a dedicated Three-wire mode. The USI provides the basic hardware resources needed for synchronous serial communication. Combined with a minimum of control software, the USI allows higher transfer rates, less CPU load and in general uses less code space than solutions based on software only. This application note describes a SPI interface implementation, in form of a fullfeatured driver and an example of usage for this driver. The driver handles transmission according to SPI Modes 0 and 1. 8-bit Microcontrollers Application Note Figure 1. Example application setup MOSI DI MISO USI module in ATmega169 USCK SCK SPI module in ATmega128 SW controlled ~SS Rev.

Theory Serial Peripheral Interface This section gives a short description of the SPI interface and the USI module. For more detailed information refer to the datasheets. The Serial Peripheral Interface allows high-speed synchronous data transfer between an AVR device and peripheral devices or between several AVR devices. The interconnection between Master and Slave devices with SPI is shown in Figure 2. The system consists of two shift registers, and a master clock generator. The SPI Master initiates the communication cycle when pulling low the Slave Select (SS) pin of the desired Slave. Master and Slave prepare the data to be sent in their respective shift registers, and the Master generates the required clock pulses on the SCK line to interchange data. Figure 2. SPI Master-slave interconnection SHIFT ENABLE SPI Data Modes The four combinations of SCK phase and polarity with respect to serial data are determined by clock phase (CPHA) and clock polarity (CPOL) settings. The SPI data transfer formats are shown in Figure 3 and Figure 4 below. Data bits are shifted out and latched in on opposite edges of the SCK signal, ensuring sufficient time for data signals to stabilize. 2 AVR319

AVR319 Figure 3. SPI Transfer Format with CPHA = 0 SCK (CPOL = 0) mode 0 SCK (CPOL = 1) mode 2 SAMPLE I MOSI/MISO CHANGE 0 MOSI PIN CHANGE 0 MISO PIN SS MSB first (RD = 0) LSB first (RD = 1) MSB LSB Bit 6 Bit 1 Bit 5 Bit 2 Bit 4 Bit 3 Bit 3 Bit 4 Bit 2 Bit 5 Bit 1 Bit 6 LSB MSB Figure 4. SPI Transfer Format with CPHA = 1 SCK (CPOL = 0) mode 1 SCK (CPOL = 1) mode 3 SAMPLE I MOSI/MISO CHANGE 0 MOSI PIN CHANGE 0 MISO PIN SS MSB first (RD = 0) LSB first (RD = 1) MSB LSB Bit 6 Bit 1 Bit 5 Bit 2 Bit 4 Bit 3 Bit 3 Bit 4 Bit 2 Bit 5 Bit 1 Bit 6 LSB MSB Universal Serial Interface The Universal Serial Interface provides the basic hardware resources needed for synchronous serial communication. The main features of the USI are: Two-wire Synchronous Data Transfer Three-wire Synchronous Data Transfer Data Received Interrupt Wakeup from Idle Mode The USI Three-wire mode is compliant with the Serial Peripheral Interface (SPI) mode 0 and 1, but does not have the slave select (SS) pin functionality. However, this feature can be implemented in software if necessary. Figure 5 below shows the USI module block diagram, and Figure 6 shows the module in Three-wire mode. 3

Figure 5. Universal Serial Interface, Block Diagram D Q LE DI/SDA (Output only) (Input/Open Drain) DATA BUS USISIE USISIF Bit7 USIOIF USIPF USIDC USIDR USISR 2 Bit0 4-bit Counter USIOIE USIWM1 USIWM0 USICS1 USICS0 USICLK USITC 3 2 1 0 3 2 1 0 [1] TIM0 COMP 0 1 Two-wire Clock Control Unit CLOCK HOLD USCK/SCL (Input/Open Drain) USICR Figure 6. Three-wire Mode Operation, Simplified Diagram Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0 DI USCK SLAVE Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0 DI USCK PORTxn MASTER 4 AVR319 The USI Data Register (USIDR) is an 8-bit Shift Register that contains the incoming and outgoing data. The register has no buffering so the data must be read as quickly as possible to ensure that no data is lost. The USI Status Register (USISR) contains a 4-bit counter. Both the shift register and the counter are clocked simultaneously by the same clock source. This allows the counter to count the number of bits received or transmitted and sets a flag alternatively generates an interrupt when the transfer is complete. The clock can be selected to use three different sources: The USCK pin, Timer/Counter0 Compare Match or from software.

AVR319 Figure 6 shows two USI units operating in Three-wire mode, one as Master and one as Slave. The two shift registers are interconnected in such way that after eight USCK clocks, the data in each register are interchanged. The same clock also increments the USI s 4-bit counter. The Counter Overflow Interrupt Flag, or USIOIF, can therefore be used to determine when a transfer is completed. The clock is generated by the Master device software by toggling the USCK pin via the PORT Register or by writing a one to the USITC bit in USICR. It is the master device s responsibility to give the slave device time to prepare its next byte before starting a new transfer. Implementation This application note describes the implementation of a SPI driver for both master and slave communication. An example setup with the USI module as a SPI master is shown in Figure 7 below. The driver is written as a standalone driver that easily can be included into the main application. Use the code as an example, or customize it for own use. All relevant functions and global variables are prefixed with spix_, so a quick search-and-replace is enough to rename the driver interface in case of naming conflicts. Figure 7. USI module setup as SPI master MOSI Master device implementing SPI communication via its USI module DI USCK SW controlled MISO SCK ~SS Slave SPI device The driver uses the USI module and the USI counter overflow interrupt. Therefore, interrupts must be enabled to be able to use the driver. In master mode the driver also uses Timer/Counter0. The T/C0 compare match interrupt is used to generate the master clock signal. The driver interface consists of these functions: spix_initmaster which initializes the driver in master mode. spix_initslave which initializes the driver in slave mode. spix_put which starts a transfer in master mode, or prepares a byte in slave mode. spix_get which returns the last incoming byte. spix_wait which waits for a transfer to finish. Note that the Slave Select (SS) line of the SPI bus must be controlled manually in software if required by the slave device. When using the USI as a SPI slave, you also need to watch the incoming SS line with for instance an interrupt line if required. The driver in this application note does not use the SS line. The following global variables are also available: spix_status which contains the driver status flags. storedusidr which contains the last incoming byte. This should be accessed with the spix_get() function only. 5

The functions are documented in the source code. In addition, the simplified flowchart for a typical byte transfer session is given in Figure 8 below. The example source code accompanying this application note shows an implementation of such a session. Note that the part of the flowchart below grouped under the spix_wait() function is performed by interrupt handlers and not the function itself. The function just waits for the transfer complete flag to be set. The example code is written for the IAR Embedded Workbench AVR C compiler version 3.2. Literature References ATmega169 Datasheet Application Note AVR310: Using the USI module as a TWI master 6 AVR319

AVR319 Figure 8. One byte transfer in master and slave mode MASTER SLAVE Master application calls spix_put(...) Slave application calls spix_put(...) Yes Write Collision Ongoing transfer? No Yes Write Collision Ongoing transfer? No spix_put(...) Prepare flags and USI data register and start timer Prepare flags and USI data register and wait for clock signal from master No Compare match interrupt handler toggles clock line which in turn clocks the USI module No Clock signal from master device clocks the USI module All 8 bits transferred? All 8 bits transferred? spix_wait() Yes Yes USI overflow interrupt handler stops timer and stores incoming byte USI overflow interrupt handler stores incoming byte Master application calls spix_get() to get stored byte Slave application calls spix_get() to get stored byte spix_get() Ready for next transfer Ready for next transfer 7

Disclaimer Atmel Corporation 2325 Orchard Parkway San Jose, CA 95131, USA Tel: 1(408) 441-0311 Fax: 1(408) 487-2600 Regional Headquarters Europe Atmel Sarl Route des Arsenaux 41 Case Postale 80 CH-1705 Fribourg Switzerland Tel: (41) 26-426-5555 Fax: (41) 26-426-5500 Asia Room 1219 Chinachem Golden Plaza 77 Mody Road Tsimshatsui East Kowloon Hong Kong Tel: (852) 2721-9778 Fax: (852) 2722-1369 Japan 9F, Tonetsu Shinkawa Bldg. 1-24-8 Shinkawa Chuo-ku, Tokyo 104-0033 Japan Tel: (81) 3-3523-3551 Fax: (81) 3-3523-7581 Atmel Operations Memory 2325 Orchard Parkway San Jose, CA 95131, USA Tel: 1(408) 441-0311 Fax: 1(408) 436-4314 Microcontrollers 2325 Orchard Parkway San Jose, CA 95131, USA Tel: 1(408) 441-0311 Fax: 1(408) 436-4314 La Chantrerie BP 70602 44306 Nantes Cedex 3, France Tel: (33) 2-40-18-18-18 Fax: (33) 2-40-18-19-60 ASIC/ASSP/Smart Cards Zone Industrielle 13106 Rousset Cedex, France Tel: (33) 4-42-53-60-00 Fax: (33) 4-42-53-60-01 1150 East Cheyenne Mtn. Blvd. Colorado Springs, CO 80906, USA Tel: 1(719) 576-3300 Fax: 1(719) 540-1759 Scottish Enterprise Technology Park Maxwell Building East Kilbride G75 0QR, Scotland Tel: (44) 1355-803-000 Fax: (44) 1355-242-743 RF/Automotive Theresienstrasse 2 Postfach 3535 74025 Heilbronn, Germany Tel: (49) 71-31-67-0 Fax: (49) 71-31-67-2340 1150 East Cheyenne Mtn. Blvd. Colorado Springs, CO 80906, USA Tel: 1(719) 576-3300 Fax: 1(719) 540-1759 Biometrics/Imaging/Hi-Rel MPU/ High Speed Converters/RF Datacom Avenue de Rochepleine BP 123 38521 Saint-Egreve Cedex, France Tel: (33) 4-76-58-30-00 Fax: (33) 4-76-58-34-80 Literature Requests www.atmel.com/literature Disclaimer: The information in this document is provided in connection with Atmel products. No license, express or implied, by estoppel or otherwise, to any intellectual property right is granted by this document or in connection with the sale of Atmel products. EXCEPT AS SET FORTH IN ATMEL S TERMS AND CONDITIONS OF SALE LOCATED ON ATMEL S WEB SITE, ATMEL ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS, IMPLIED OR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON- INFRINGEMENT. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, CONSEQUENTIAL, PUNITIVE, SPECIAL OR INCIDENTAL DAMAGES (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF PROFITS, BUSINESS INTERRUPTION, OR LOSS OF INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS CUMENT, EVEN IF ATMEL HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. Atmel makes no representations or warranties with respect to the accuracy or completeness of the contents of this document and reserves the right to make changes to specifications and product descriptions at any time without notice. Atmel does not make any commitment to update the information contained herein. Atmel s products are not intended, authorized, or warranted for use as components in applications intended to support or sustain life. Atmel Corporation 2004. All rights reserved. Atmel, logo and combinations thereof, AVR, and AVR Studio are registered trademarks, and Everywhere You Are are the trademarks of Atmel Corporation or its subsidiaries. Other terms and product names may be trademarks of others.