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



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

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

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

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

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

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

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

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

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

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

General Porting Considerations. Memory EEPROM XRAM

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

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

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

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

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

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

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

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

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

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

AT89C5131A Starter Kit... Software User Guide

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 RISC Microcontroller. Application Note. AVR155: Accessing an I 2 C LCD Display using the AVR 2-wire Serial Interface

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

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

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

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

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

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

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

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

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

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

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

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

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

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

USB Test Environment ATUSBTEST- SS7400. Summary

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2-Wire Serial EEPROM AT24C32 AT24C64. 2-Wire, 32K Serial E 2 PROM. Features. Description. Pin Configurations. 32K (4096 x 8) 64K (8192 x 8)

USER GUIDE EDBG. Description

2-wire Serial EEPROM AT24C1024. Advance Information

STK 500, AVRISP, AVRISP

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

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

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

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

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

Two-wire Serial EEPROM AT24C1024 (1)

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

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

AVR441: Intelligent BLDC Fan Controller with Temperature Sensor and Serial Interface. 8-bit Microcontrollers. Application Note.

Serial Communications

AVR311: Using the TWI Module as I2C Slave. Introduction. Features. AVR 8-bit Microcontrollers APPLICATION NOTE

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

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

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

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

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

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

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

Atmel AVR4903: ASF - USB Device HID Mouse Application. Atmel Microcontrollers. Application Note. Features. 1 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

Develop a Dallas 1-Wire Master Using the Z8F1680 Series of MCUs

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

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

AVR315: Using the TWI Module as I2C Master. Introduction. Features. AVR 8-bit Microcontrollers APPLICATION NOTE

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

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

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

APPLICATION NOTE. Atmel AT02985: User s Guide for USB-CAN Demo on SAM4E-EK. Atmel AVR 32-bit Microcontroller. Features. Description.

2-wire Serial EEPROM AT24C512

ARM Thumb Microcontrollers. Application Note. Software ISO 7816 I/O Line Implementation. Features. Introduction

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

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

AN141 SMBUS COMMUNICATION FOR SMALL FORM FACTOR DEVICE FAMILIES. 1. Introduction. 2. Overview of the SMBus Specification. 2.1.

JTAG ICE.... User Guide

Transcription:

AVR307: Half Duplex UART Using the USI Module Features Half Duplex UART Communication Communication Speed Up To 230.4 kbps at 14.75MHz Interrupt Controlled Communication Eight Bit Data, One Stop-bit, No Parity Data Buffer Handling Introduction The Universal Serial Interface (USI) present in AVR devices like the ATtiny26, ATtiny2313, and ATmega169, is a communication module designed for TWI and SPI communication. The USI is however not restricted to these two serial communication standards. It can be used for UART communication as well. By using the USI for UART communication one can avoid some of the inconveniences associated with software implemented UART communication. By using the USI, interrupts control the communication and the bits are automatically shifted out. In this way, less processing time is occupied by the communication, freeing processing time for other parts of the application while communicating. This document describes how to use the USI of the ATtiny26 for UART communication. Source code for communication drivers for transmission and reception is provided. The drivers can be adapted to the ATtiny2313 or ATmega169 USI modules by minor changes to the code. The code is complete with both data buffer handling and combined transmitter and receiver. These features can be split or removed to optimize the use of resources. 8-bit Microcontrollers Application Note Rev.

Theory of Operation The UART protocol is an asynchronous serial communication standard. The frame format is specified as a start bit followed by 5 to 9 data bits, one parity bit (optional) and one or two stop bits. The data is sent with LSB first. The frame format for UART communication is illustrated in Figure 1. Figure 1. Frame Format for UART Communication Figure 2. Universal Communication Interface of the ATtiny26 The USI is basically an eight-bit shift register connected to input and output pins. When used for UART communication, a general purpose timer is control the in/out clocking of data bits, and a dedicated USI counter generates interrupts when a given number of bits have been shifted in/out. The USI block diagram can be seen in Figure 2. D Q LE PB1 PB0 DO (Output only) DI/SDA (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 OVF 0 1 Two-wire Clock Control Unit CLOCK HOLD PB2 SCK/SCL (Input/Open Drain) USICR Since a UART frame format is specified to be from 7 to 13 bits in total, with a typical frame size of 10 bits, the USI shift register is a couple of bits short of a typical frame. However, using the features of the USI module one can circumvent this limitation. UART Reception 2 AVR307 The suggested solution for reception is to detect a start-bit and then sample the eight data bits in their appropriate time slots (while ignoring the stop bit). The first sampling of the data is done after a half-bit duration, which is the time from the falling edge of the start-bit to the middle of the start-bit. The successive samplings are done with fixed intervals of one bit duration determined by the baud rate. Since the shift register can

AVR307 only hold 8 bits the start-bit is not kept in the shift register, but falls out when the last data bit is shifted in. This method is preferred next to performing the first sampling on the first data bit, since it simplifies the use of Timer/Counter0, controlling the delays and thereby the bit rates used. More details about bit rate is provided in the section Bit Rates on page 4. One should be aware that one difference between the AVR hardware UART and UART communication implemented in software or by the use of the USI module is how the input data is sampled. In the hardware UART a majority vote of three samples is used to determine the value of the sampled bit. The majority vote eliminates the likelihood of noise causing data corruption. In most full or partial software implementations only one sampling of each data bit is done. This is also the case for the USI implementation of UART communication. The possibility that noise can cause corruption of the incoming data is therefore increased. This fact could be seen as an incitement to use parity check for reception. Parity check is not added in the receive driver described in this document. An alternative solution is to use CRC checking of the complete message as described in the application note AVR350 available on the Atmel web site. UART Transmission UART transmission is slightly more complicated; the frame has to be divided in two. The MSB of the USI shift register is directly connected to the USI Data Output pin. To ensure correct timing the MSB must therefore hold a high state until the timer starts shifting the data correctly. The USI Shift register is loaded with a high value, the start-bit, and the first 6 LSB of the data. The transmission is then initiated. When the first 5 bits in the shift register have been shifted out the shift register is reloaded, and in this way the remainder of the frame is added to the contents of the shift register. The procedure for loading/reloading the USI data register (USIDR) for transmission is illustrated in Figure 3. Figure 3. Loading the USIDR Buffer for Transmission TxData = Bit_Reverse( TxData ); USIDR = HIGH & START_BIT & ( TxData >> 2 ); //START_BIT = 0xBF USIDR: 1 START D0 D1 D2 D3 D4 D5 Shifting out the first 5 data bits (left shift) (First "half frame") USIDR: D3 D4 D5 X X X X X USIDR = ( TxData << 3 ) (STOP_BIT); //STOP_BIT = 0x07 USIDR: D3 D4 D5 D6 D7 Stop (Stop) (Stop) (Second "half frame") 3

USI Modes Different communication modes, or Wire Modes, can be selected for the USI, but since the USI module is designed for TWI and SPI communication, there are no dedicated wire modes for UART communication. The wire modes that can be selected are Threewire (SPI) or Two-wire (TWI). The mode that should be used for UART communication is the SPI mode, since this is the mode that has separate data in and out lines. In both wire modes the USI module can generate a so-called Start Condition Interrupt. The Start Condition interrupt is in SPI mode triggered by a level change on the SCK pin, but since it is desired to wake up on a change on the DI (Data Input) pin, it is better to use a pin change interrupt. A pin change interrupt can wake up the AVR from all sleep modes. The pin change interrupt can thus be used to detect the start-bit of a UART frame. The start bit in the UART reception will in this way generate a pin change interrupt and the interrupt routines can thereafter handle the reception. The SCK pin is therefore not required and can be used for other purposes. Bit Rates The eight-bit Timer/Counter0 is used to specify the bit rate for the serial communication. In ATtiny26 the overflow of the timer can be used to trigger the USI shift register. In ATmega169 a compare match is used to trigger the shift register. The method for generating the bit rate of the serial communication is thus to run the timer and make the timer overflow/compare match events trigger the bit shifts. In this document the USI of the ATtiny26 and therefore the Timer/Counter0 overflow is used to trigger the bit shifting. The timer seed matching the duration of one bit can be determined from Equation 1. Figure 4. Equation 1 System Clock Cycles per bit = Baudrate Timer prescaler Timer0 seed = 256 (Cycles per bit) If the timer seed approaches the top value of the timer one must be aware that the rounding/ truncation of the timer seed can introduce a significant timing error. The rounding error relative to the number of cycles per bit is recommended as low as possible. As a rule of thumb the total timing error should be less than 2% to ensure error-less communication the total error is the sum of both the accuracy of the system clock and the UART bit timing. Further, if the timer seed is close to the top value of the timer, which will occur when using high-speed communication, there will be rather few cycles between each timer interrupt. This can potentially cause problems in serving the interrupts fast/often enough. It is recommended to carefully consider the number of cycles required to serve the timer interrupt, especially if other interrupts can occur and thereby block the timer overflow interrupt. In addition one should also consider the interrupt latency as an important factor and correct the timer seed according to this. The interrupt latency should be added to the timer seed value together with the number of cycles it takes to replant the timer seed. This is described in the implementation section. A list of timer seeds is found in Table 1. The timer seeds are calculated using Equation 1 and are not corrected with regard to interrupt latency. 4 AVR307

AVR307 Table 1. Timer0 Seed for Different System Clocks and Communication Speeds System Clock (MHz) Communication Speeds Possible 9600 14.4k 19.2k 28.8k 57.6k 115.2k 230.4k 1 152 187 (3) Na Na Na Na Na 1.84 64 128 160 192 (3) Na Na Na 2 48 117 152 187 (3) Na Na Na 3.69 208 (2) (1) 64 128 192 (3) Na Na 4 204 (2) 221 (2) 48 117 186 (3) Na Na 7.37 160 (2) 192 (2) 207 (2) (1) 128 192 (3) Na 8 152 (2) 187 (2) 204 (2) 221 (2) 117 187 (3) Na 11.06 112 (2) 160 (2) 184 (2) 208 (2) 64 160 Na 14.75 64 (2) 128 (2) 160 (2) 192 (2) (1) 128 192 (3) 16 48 (2) 117 (2) 152 (2) 187 (2) 221 (2) 117 187 (3) Notes: 1. Does not require timer0 timer seed. 2. Timer prescaler is CK/8. 3. Indicates critical timing. At some communication speeds the timer seed is 0. In these cases it is not required to replant the timer seed, since the seed is replanting itself by wrapping once the overflow occurs. The Timer/Counter overflow interrupt can thus be disabled in these cases. Note that the initial timer seed for reception is different from the timer seeds specified in Table 1; the initial timer seed used for the reception should generate a delay of a ½ or 1½ bit duration, depending on whether your first sample is going to be the start bit or the first data bit. They can be calculated as shown by Equation 2 and Equation 3. Figure 5. Equation 2 Timer0 seed = 256 ( Cycles per bit 1 ) 2 Figure 6. Equation 3 Timer0 seed = 256 ( Cycles per bit 3 ) Each strategy has its limitations that you must be aware of. If you have a very low Cycles per bit value, dividing it by 2 may give you an initial timer seed that is shorter then the time it takes to set up the USI for reception. On the other hand, if the Cycles per bit value is very high, then multiplying it by 1.5 may give an initial timer seed value that is higher then 256. A recommended approach could be to first check if ((Cycles per bit * 3/2) > 256) then use (Cycles per bit * 1/2). 2 5

For the ATmega169 the considerations related to the reloading of the timer seed are minimized since the compare match is used; the compare match is equivalent to autoloading of the timer seed, similar to the situation mentioned above where the timer seed is 0. USI Counter Reception The Timer/Counter0 determines the bit rate, whereas the USI counter is handling the transmission of the data on byte/frame level. The USI counter is a four-bit counter used to count the number of bits received/transmitted from USIDR. The counter top value is 16. The clock for the USI counter is depending on the USI Clock Source Select bits (USICS1:0) and the USI Clock Strobe bit (USICLK). When the USI is used for UART communication these bit should be USICS1:0 = 01 and USICLK = X (don t care). With these settings the USI counter and the USIDR shift register are clocked by the Timer/Counter0 overflow. The USI counter thus counts the number of bits transmitted/received. The USI timer/counter can generate an overflow interrupt, which can be used to indicate that a number of bits have been received. To accomplish this the USI counter is preloaded with the top value minus the number of data bits (typically eight bit). See Figure 7 and Figure 8. Figure 7. Equation 4 USI Counter seed = Counter top value (Start bit + Data bits) Figure 8. Equation 5 USI Counter seed = Counter top value (Data bits) Counter top value is 16, Start bit is 1 and Data bits is typically 8 (size of one byte). The choice is dependent on whether your initial timer seed will make you sample the start bit or not. See initial timer seed discussion in the Bit Rates on page 4 for more information. Transmission In a transmission the USI counter overflow is used to determine that the USIDR needs reloading (to be able to send an entire 10-bit frame). The USI counter is in this case preloaded with the Counter top value minus the Half-frame size, which is the number of bits that should be transmitted before reloading the counter (see Figure 3). The initial USI Counter seed for transmission is determined as shown by Figure 9. Figure 9. Equation 6 USI Counter seed = Counter top value (Half frame size) After the second half-frame is loaded into USIDR, the USI counter is preloaded again to generate an interrupt once the entire frame has been transmitted. The secondary USI Counter seed for transmission is determined by Equation 7. 6 AVR307

AVR307 Figure 10. Equation 7 USI Counter seed = Counter top value ((Full frame size) - (Half frame size)) Summary The Timer/Counter0 is used to control the bit rate and the four-bit USI counter overflow is used for the frame handling. Since both reception and transmission require Timer/Counter0 and the USI counter, it is only possible to use the USI for half duplex communication. The pin change interrupt on the USI Data Input pin is in the reception used for detecting the frame start. In transmission, the USI Data Register must be loaded twice for each transmitted frame. 7

Implementation Communication Buffers USI UART Receiver The code described this application note is written as drivers for UART communication. The code includes the control elements allowing interlaced receptions and transmissions giving true half duplex communication. To simplify the use of the drivers, communication buffers have been implemented. This enables your application to execute in parallel with the UART communication. If there is no need for both transmission and reception, or communication buffers, then this code could be removed to minimize the drivers in size and optimize the performance. The code has been written and compiled using the IAR EWAVR 2.28A compiler with all optimizations enabled. Testing and debugging has been done using AVR Studio 4.07 and ICE50. The receive and transmit routines use modulo 2n addressing of circular buffers for buffering incoming and outgoing data. The buffer sizes must be defined before using the routines. Set the UART_RX_BUFFER_SIZE and UART_TX_BUFFER_SIZE variables to the buffer size in bytes. Note that these variables must be a power of 2. If not, a compiler error message will be flagged. By inspecting the buffer head and tail pointers it can be determined if new data is available; that is, if the write pointer is different from the read pointer. It is recommended to increase the receive buffer size if high-speed communication with critical timing is required. Otherwise, data may be lost since the receive driver does not verify that data have been read from the receive buffer. An extra function is added to the example code. The DataInReceiveBuffer returns zero if the receive buffer does not contain any data. This function does not, in contrast to the ReceiveByte function, wait for incoming data, but returns immediately the status of the buffer. Note: This routine does not return the number of bytes in the buffer. The driver consists of four parts: USI UART initialization Pin Change interrupt service routine Timer/Counter0 overflow interrupt service routine USI Counter overflow interrupt service routine 8 AVR307

AVR307 Figure 11. Execution Sequence of the Receive Driver Enable USI_UART Receiver Initialization routine Pin Change Interrupt Timer0 Overflow Interrupt USI Overflow Interrupt Each of these parts are described below by flow-charts and comments to these flow charts. USI UART Initialization - Receiver This function prepares the device for a reception. The USI has a dedicated USI Start Condition interrupt, but this is associated with the SCK line. For UART communication a separate clock line is not used, a reception is detected as activity on the Data Input (DI) line. The initial activity is always a high-to-low transition on the bus, due to the start-bit of the UART frame. A Pin Change interrupt is used to detect the initiated transmission. Therefore, the USI_UART initialization routine sets up the Pin Change interrupt to wait for the incoming data. The USI functionality will override the Pin Change feature; the USI module is therefore disabled. Then both the DI and DO pins are defined as inputs. DI because data is expected on this pin, and DO to ensure that no data is shifted out unintentionally. The idle level for a UART communication pin is high, as it is an open collector bus, so the pull up on the DO pin is enabled. Finally the Pin Change interrupt is enabled. Note that global interrupts are not enabled in the initialization function. This should therefore be done in the main routine. The flow chart is shown in Figure 12. 9

Figure 12. Initialization of the Receive Driver USI UART Rx Initialization Disable USI Enable pull up on Data Output pin Set Data Input pin & Data Output pin as input Enable Pin Change interrupt Return Pin Change Interrupt Receiver The purpose of the Pin Change interrupt is to start Timer/Counter0 once the falling edge of the start bit is detected. Once the start bit is detected the Pin Change interrupt should be disabled, since only the falling edge of the start bit should generate a Pin Change interrupt. This is done automatically by enabling the USI, since it will overrun the pin change feature on the same pins. The pin change interrupt is enabled again in the USI Counter Overflow interrupt service routine once a complete frame has been received. A flow chart of the Pin Change interrupt service routine is presented in Pin Change Interrupt Service Routine. To use the USI for reception the wire-mode is set to three-wire (SPI). The USI clock source is selected to be the Timer0 overflow. The USI counter is initialized so that it will generate overflow when the required number of bits have been received. Timer/Counter0 is initialized by loading it with the timer seed required to generate the initial delay; the delay from the falling edge of the start-bit to the middle of the sampled bit. See Figure 1 regarding the frame format. For exact timing one could also include the delay from when the DI pin changes value to when the timer0 starts running. This value depends on how fast you are able to preload and start the timer0 in the interrupt service routine. Place this operation first in the ISR and simulate execution of the final code to calculate the delay. Finally all the required interrupts are enabled. If the Timer/Counter0 timer seed is 0, the Timer/Counter0 overflow does not need to be enabled. See comments on this in the section Timer/Counter0 Overflow Interrupt - Transmitter on page 15. 10 AVR307

AVR307 Figure 13. Pin Change Interrupt Service Routine Pin Change Interrupt Plant initial Timer0 Seed Plant USI counter seed Start Timer0 Prescaler = CK Select USI 3-wire mode Enable Timer0 overflow interrupt Set USI clock source to timer0 overflow RETI Timer/Counter0 Overflow Interrupt - Receiver The purpose of the Timer/Counter0 Overflow interrupt service routine is to replant the timer seed that controls the sampling of the incoming data bits. The interrupt routine is described by the flow chart in Figure 14. The Timer/Counter0 overflow interrupt service routine is similar for both the receive driver and the transmit driver. If the timer seed is 0 (see Table 1) the overflow interrupt service routine is not required since there is no timer seed to replant. If the timer overflow interrupt can be avoided the timing of the UART communication will be less influenced by the presence of other interrupts sources. Further, less processing resources are used on the UART reception (and transmission). Figure 14. Timer/Counter0 Overflow Interrupt Service Routine Timer/Counter0 overflow Plant timer0 seed plus the current timer value (1 bit duration) RETI USI Counter Overflow Interrupt - Receiver In the receive driver, the USI Counter overflow is triggered when an entire byte has been received. It thereby indicates the completion of a reception and handles the moving of the received data to the receive buffer and the re-initialization of the USI to make it ready for receiving a new frame. The interrupt routine is visualized in Figure 15. 11

First, the Timer/Counter0 is stopped. This ensures that the no further in-shifting of data is made. The received data is located in USIDR and needs to be moved before new data can be received. The data is moved to a circular data buffer. The data needs to be bit reversed before used. This could either be done here or when it is read out from the receive buffer. The re-initialization of the USI as receiver involves disabling the USI and enabling the pin change interrupt. Figure 15. USI Counter Overflow Interrupt Service Routine USI counter overflow interrupt (Receive mode) Read USIDR Reverse bit order of received byte Disable USI Overflow Interrupt Load data to Receive buffer Clear Pin Change Interrupt flag Stop Timer0 Enable Pin Change interrupt RETI Used Resources The receiver uses Timer0, USI module/pins, and the pin change interrupt. The memory requirements for the receive driver are displayed in Memory Requirements of the Receive Driver. The majority of the code memory, which is 136 bytes of the total 236, is required by the interrupts. Reversing the bit order of the data is among the routines that demand much memory. Note that many of the sub sections of the code are equal with and therefore shared with the transmitter, making the total code size smaller then the sum. 12 AVR307

AVR307 Table 2. Memory Requirements of the Receive Driver Receiver Code [bytes] Initialisation and data/buffer handling USI_UART_Initialise_Receiver() 30 USI_UART_Receive_Byte() 28 Bit_Reverse() 42 Sum 100 Sampling & storing data IO_Pin_Change_ISR() 68 USI_Counter_Overflow_ISR() 68 Sum 136 USI UART Transmitter The transmit driver is implemented as a function call, where the byte to transmit is passed as an argument in the function call. The transmit driver consists of four parts: Transmit function USI UART initialization Timer/Counter0 overflow interrupt service routine USI Counter Overflow interrupt service routine 13

Figure 16. Execution Sequence of the Transmission Driver Enable USI UART Transmitter Yes Ongoing reception? No Initialization function Repetition determined by half-byte flag Timer0 Overflow Interrupt Repetitions determined by bit counter USI Overflow Interrupt Enable USI UART Receiver Enable USI UART Transmitter Transmit Function To initiate a transmission the transmit function is called. The function is the part of the driver that the application interfaces. The transmit function is declared as: void USI_UART_TransmitByte( unsigned char ); The data is prepared for transmission by reversing the bit order of the byte that is about to be transmitted (TxByte). Then it is stored in the transmit buffer. If a transmission is already in operation then no other actions are needed since the interrupt routines will pick up all data from the buffer automatically. If there are no transmissions ongoing then a test will be done of whether there is an ongoing reception before initializing the USI for transmission. The USI is configured for operation, but initial seeds are set to minimize the time until the interrupt routines are entered. It is in the interrupt routines that the data registers and seeds are loaded with correct content. 14 AVR307

AVR307 Figure 17. USI UART Transmit and Initialization Functions USI UART Tx Initialization Load USIDR with 0xFF Select USI clock source Configure Pin 1 on Port B as output Enable USI Counter overflow interrupt Select USI 3-wire mode Enable Timer0 overflow interrupt Return USI UART Initialization Transmitter Timer/Counter0 Overflow Interrupt - Transmitter USI Counter Overflow Interrupt Transmitter When three-wire mode is selected, the direction of pin 1 on port B is set to output, which in Three-wire mode is an open-drain output. An external pull-up is therefore required. The interrupts required for the transmission are enabled. Timer/counter0 overflow interrupt service routine is similar to the one used by the receive driver. Refer to the section Timer/Counter0 Overflow Interrupt - Receiver on page 11 for details on this routine. Note that the USI counter overflow interrupt is used for both transmit and receive. The first action taken is therefore to determine the current mode. The data needs to be reversed before transmission. This is already prepared in the TransmitByte function. This is to transmit the data with LSB first according to the UART frame format (see Figure 1). The TxByte is then merged with a high value and a start bit. The merged data is copied to the USIDR (see Figure 3). A status bit is set to indicate that the USIDR has been loaded with the first half-frame, the start bit and the 6 LSB of the TxByte. This status bit is used in the USI Counter interrupt service routine. The timer seeds for the Timer/Counter0 and the USI Counter are planted and the Timer/Counter0 is started. The Timer/Counter overflow will generate the clock for shifting out the bits in USIDR. The next overflow interrupts from the USI Counter are generated when the first 5 bits of a frame (a half frame ) have been transmitted and when a frame transmission has been completed. The USI Counter Overflow interrupt service routine is illustrated in Figure 15. Note that the figure also shows the receiver since it too uses the same USI Overflow interrupt. If the interrupt is due to the transmission of the first half frame, the rest of the data and a stop-bit are merged into USIDR (see Figure 3). The status bit that indicates that USIDR is loaded with the first half frame is then cleared. 15

Figure 18. USI Counter Overflow Interrupt Service Routine If the interrupt is triggered by the transmission of a complete frame, the Timer/Counter0 is stopped and the TxOngoing flag is cleared. Finally the USI Counter Overflow flag is cleared (all flags are cleared), since this is not done in hardware by triggering the interrupt vector. The overflow interrupt routine will work through the complete transmit buffer before it disables the USI and reenters the USI UART Receive mode. USI Overflow Interrupt No In transmitter mode? Yes Store data in receive buffer Disable USI Overflow Interrupt Ready to send 2nd half of package? No Enable Pin Change Interrupt Yes Place 2nd half of package into USI Data register Data in transmit buffer? No Yes RETI RETI Get data from first location of transmit buffer Disable USI Overflow Interrupt Place 1st half of package into USI Data register Enable Pin Change Interrupt RETI RETI 16 AVR307

AVR307 Used Resources The receiver uses Timer0 and USI module/pins. The memory requirements for the receive driver are displayed in Table 3. The majority of the code memory, which is 116 bytes of the total 252, is required by the USI counter overflow interrupt. Reversing the bit order of the data is among the routines that demand much memory. Note that many of the sub sections of the codes are equal with and therefore shared with the receiver, making the total code size smaller then the sum. The total code size is ~450 bytes. Table 3. Memory Requirements of the Transmit Driver Transmitter Code [bytes] Initialisation and data/buffer handling USI_UART_Initialise_Transmitter() 46 USI_UART_Transmit_Byte() 48 Bit_Reverse() 42 Sum 136 Sampling & storing data USI_Counter_Overflow_ISR() 116 17

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: Atmel Corporation makes no warranty for the use of its products, other than those expressly contained in the Company s standard warranty which is detailed in Atmel s Terms and Conditions located on the Company s web site. The Company assumes no responsibility for any errors which may appear in this document, reserves the right to change devices or specifications detailed herein at any time without notice, and does not make any commitment to update the information contained herein. No licenses to patents or other intellectual property of Atmel are granted by the Company in connection with the sale of Atmel products, expressly or by implication. Atmel s products are not authorized for use as critical components in life support devices or systems. Atmel Corporation 2003. All rights reserved. Atmel and combinations thereof AVR and megaavr are the registered trademarks of Atmel Corporation or its subsidiaries. Other terms and product names may be the trademarks of others. Printed on recycled paper.