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



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

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

AT88CK490 Evaluation Kit

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

USER GUIDE EDBG. Description

AVR106: C Functions for Reading and Writing to Flash Memory. Introduction. Features. AVR 8-bit Microcontrollers APPLICATION NOTE

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

APPLICATION NOTE. AT07175: SAM-BA Bootloader for SAM D21. Atmel SAM D21. Introduction. Features

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

AT15007: Differences between ATmega328/P and ATmega328PB. Introduction. Features. Atmel AVR 8-bit Microcontrollers APPLICATION NOTE

AVR127: Understanding ADC Parameters. Introduction. Features. Atmel 8-bit and 32-bit Microcontrollers APPLICATION NOTE

CryptoAuth Xplained Pro

APPLICATION NOTE. Secure Personalization with Transport Key Authentication. ATSHA204A, ATECC108A, and ATECC508A. Introduction.

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

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

APPLICATION NOTE. Authentication Counting. Atmel CryptoAuthentication. Features. Introduction

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

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

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

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

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

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

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

Atmel AVR4921: ASF - USB Device Stack Differences between ASF V1 and V2. 8-bit Atmel Microcontrollers. 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. AT16268: JD Smart Cloud Based Smart Plug Getting. Started Guide ATSAMW25. Introduction. Features

APPLICATION NOTE. AT12405: Low Power Sensor Design with PTC. Atmel MCU Integrated Touch. Introduction

AVR126: ADC of megaavr in Single Ended Mode. Introduction. Features. AVR 8-bit Microcontrollers APPLICATION NOTE

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

AT11805: Capacitive Touch Long Slider Design with PTC. Introduction. Features. Touch Solutions APPLICATION NOTE

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

AVR305: Half Duplex Compact Software UART. 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.

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

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

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

The I2C Bus. NXP Semiconductors: UM10204 I2C-bus specification and user manual HAW - Arduino 1

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

USER GUIDE. ZigBit USB Stick User Guide. Introduction

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

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

APPLICATION NOTE. RF System Architecture Considerations ATAN0014. Description

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

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

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

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

QT1 Xplained Pro. Preface. Atmel QTouch USER GUIDE

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

AN2824 Application note

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

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

APPLICATION NOTE. Atmel AT01095: Joystick Game Controller Reference Design. 8-/16-bit Atmel Microcontrollers. Features.

AN2680 Application note

Atmel AVR1017: XMEGA - USB Hardware Design Recommendations. 8-bit Atmel Microcontrollers. Application Note. Features.

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

SAMA5D2. Scope. Reference Documents. Atmel SMART ARM-based MPU ERRATA

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

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

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

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

General Porting Considerations. Memory EEPROM XRAM

AN3332 Application note

APPLICATION NOTE. AT05558: Wireless Manufacturing Test Kit. Atmel ATmega256RFR2. Description. Features

Definitions and Documents

NB3H5150 I2C Programming Guide. I2C/SMBus Custom Configuration Application Note

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

AN3265 Application note

Old Company Name in Catalogs and Other Documents

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

USER GUIDE. ATWINC1500B Hardware Design Guidelines - IEEE b/g/n IoT Module. Atmel SmartConnect. Introduction

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

AN11008 Flash based non-volatile storage

AN111: Using 8-Bit MCUs in 5 Volt Systems

AT09333: USB Host Interface (UHI) for Communication Class Device (CDC) Introduction. Atmel Microcontrollers APPLICATION NOTE

APPLICATION NOTE. Atmel LF-RFID Kits Overview. Atmel LF-RFID Kit. LF-RFID Kit Introduction

AN601 I2C 2.8 Communication Protocol. SM130 SM130 - Mini APPLICATION NOTE

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

AVR134: Real Time Clock (RTC) using the Asynchronous Timer. 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.

AT12181: ATWINC1500 Wi-Fi Network Controller - AP Provision Mode. Introduction. Features. Atmel SmartConnect APPLICATION NOTE

DS1621 Digital Thermometer and Thermostat

DS Wire Digital Thermometer and Thermostat

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

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

USB2.0 <=> I2C V4.4. Konverter Kabel und Box mit Galvanischetrennung

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

AND8336. Design Examples of On Board Dual Supply Voltage Logic Translators. Prepared by: Jim Lepkowski ON Semiconductor.

DS1307ZN. 64 x 8 Serial Real-Time Clock

AN1991. Audio decibel level detector with meter driver

Application Note. Atmel CryptoAuthentication Product Uses. Atmel ATSHA204. Abstract. Overview

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

GTS-4E Hardware User Manual. Version: V1.1.0 Date:

AT91SAM ARM-based Flash MCU. Application Note

DS1621 Digital Thermometer and Thermostat

Capacitive Touch Technology Opens the Door to a New Generation of Automotive User Interfaces

AN3998 Application note

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

AVR1003: Using the XMEGA Clock System. 8-bit Microcontrollers. Application Note. Features. 1 Introduction

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

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

Transcription:

AVR 8-bit Microcontrollers AVR315: Using the TWI Module as I2C Master APPLICATION NOTE Introduction The Two-wire Serial Interface (TWI) is compatible with Philips I 2 C protocol. The bus allows simple, robust, and cost effective communication between integrated circuits in electronics. The strengths of the TWI bus are its capability to address up to 128 devices using the same bus and arbitration, and the possibility to have multiple masters on the bus. A hardware TWI module is included in most of the Atmel AVR devices. This application note describes a TWI master implementation, in the form of a full-featured driver and contains an example of usage for this driver. The driver handles transmission based on the both Standard mode (<100kbps) and Fast mode (<400kbps). Features C-code driver for TWI master Compatible with Philips I 2 C protocol Uses the hardware TWI module Interrupt driven transmission Supports both Standard mode and Fast mode

Table of Contents Introduction...1 Features... 1 1. Overview...3 1.1. Two-wire Serial Interface...3 1.2. The AVR TWI Module...4 2. Implementation... 8 2.1. Functions...8 3. Summary... 12 4. Revision History...13 2

1. Overview This section provides a short description of the TWI interface in general and the TWI module on the Atmel megaavr 8-bit Microcontroller Family. For more information, refer to the specific device datasheet. 1.1. Two-wire Serial Interface The Two-wire Serial Interface (TWI) is ideally suited for microcontroller applications. The TWI protocol allows the systems designer to interconnect up to 128 individually addressable devices using only two bidirectional bus lines; one for clock (SCL) and one for data (SDA). The only external hardware required to implement the bus is a single pull-up resistor for each of the TWI bus lines. All devices connected to the bus have individual addresses, and mechanisms for resolving bus contention are inherent in the TWI protocol. Figure 1-1. TWI Bus Interconnection The TWI bus is a multi-master bus where one or more devices are capable of taking control of the bus, can be connected. Only Master devices can drive both the SCL and SDA lines, while a Slave device is only allowed to issue data on the SDA line. Data transfer is always initiated by a Bus Master device. A high to low transition on the SDA line, while SCL is high, is defined to be a START condition or a repeated start condition. Figure 1-2. TWI Address and Data Packet Format A START condition is always followed by the (unique) 7-bit slave address and then by a Data Direction bit. The Slave device addressed now acknowledges to the Master by holding SDA low for one clock cycle. If the Master does not receive any acknowledge the transfer is terminated. Depending of the Data Direction bit, the Master or Slave now transmits 8-bit of data on the SDA line. The receiving device then acknowledges the data. Multiple bytes can be transferred in one direction before a repeated START or 3

STOP condition is issued by the Master. The transfer is terminated when the Master issues a STOP condition. A STOP condition is defined by a low to high transition on the SDA line while the SCL is high. If a Slave device cannot handle incoming data until it has performed some other function, it can hold SCL low to force the Master into a wait-state. All data packets transmitted on the TWI bus are 9 bits, consisting of one data byte and an acknowledge bit. During a data transfer, the master generates the clock and the START and STOP conditions, while the receiver is responsible for acknowledging the reception. An Acknowledge (ACK) is signaled by the receiver pulling the SDA line low during the ninth SCL cycle. If the receiver leaves the SDA line high, a NACK is signaled. 1.2. The AVR TWI Module The TWI module consists of several sub-modules, as shown in following figure. All registers drawn in a thick line are accessible through the AVR data bus. Figure 1-3. Overview of the TWI Module in the AVR Devices SCL SD A Sle w-rate Control Spik e Filter Sle w-rate Control Spik e Filter Bus Interf ace Unit Bit Rate Gener ator START / ST OP Control Spik e Suppression Prescaler Arbitration detection Address/Data Shift Register (TWDR) Ack Bit Rate Register (TWBR) Address Match Unit Control Unit Address Register (TWAR) Address Comparator Status Register (TWSR) State Machine and Status control Control Register (TWCR) TWI Unit 1.2.1. Control Unit The AVR TWI module can operate in both Master and Slave mode. The mode of operation is distinguished by the TWI status codes in the TWI Status Register (TWSR) and by the use of certain bits in the TWI Control Register (TWCR). A set of predefined status codes covers the various states the TWI can be present in when a TWI event occurs. The status codes are divided in Master and Slave codes and further in receive and transmit related codes. Status codes for Bus Error and Idle also exist. 4

The TWI module operates as a state machine and is event driven. If a START CONDITION is followed by a TWI address that matches the address in the Slave s TWI Address Register (TWAR) the TWINT flag is set. This results in the execution of the corresponding interrupt (if Global Interrupt and TWI interrupts are enabled). The firmware of the Slave responds by reading the status code in TWSR and will respond accordingly. All TWI events will set the TWINT flag, and the firmware must respond based on the status in TWSR. As long as the TWINT Flag is set, the SCL line is held low. This allows the application software to complete its tasks before allowing the TWI transmission to continue. The TWINT Flag is set : After the TWI has transmitted a START/REPEATED START condition After the TWI has transmitted SLA+R/W After the TWI has transmitted an address byte After the TWI has lost arbitration After the TWI has been addressed by own Slave address or general call After the TWI has received a data byte After a STOP or REPEATED START has been received while still addressed as a Slave When a bus error has occurred due to an illegal START or STOP condition 1.2.2. Bit Rate Generator The Bite Rate Generator unit controls the period of SCL when operating in a Master mode. The SCL period is controlled by settings in the TWI Bit Rate Register (TWBR) and the Prescaler bits in the TWI Status Register (TWSR). Slave operation does not depend on Bit Rate or Prescaler settings, but the CPU clock frequency in the Slave must be at least 16 times higher than the SCL frequency. Note that Slaves may prolong the SCL low period, thereby reducing the average TWI bus clock frequency. The SCL frequency is generated according to the following equation. = 16 + 2 4 TWBR = Value of the TWI Bit Rate Register TWPS = Value of the prescaler bits in the TWI Status Register TWPS is located in TWISR which is the same register as the TWI Status bits. TWPS should therefore always be set to 0 to simplify the handling of the TWI Status bits. Use TWBR to achieve the required speed on the SCL. The following table shows a selection of pre calculated TWBR values based on CPU and SCL frequencies. Table 1-1. CPU and SCL Frequencies versus Bit Rate Generator Register Settings CPU clock frequency [MHz] TWBR TWPS SCL frequency [khz] 16 12 0 400 16 72 0 100 14.4 10 0 400 14.4 64 0 100 12 7 0 400 5

CPU clock frequency [MHz] TWBR TWPS SCL frequency [khz] 12 52 0 100 8 2 0 400 8 32 0 100 4 12 0 100 3.6 10 0 100 2 2 0 100 2 12 0 50 1 2 0 50 1.2.3. SCL and SDA Pins Both TWI lines (SDA and SCL) are bi-directional, therefore outputs connected to the TWI bus must be of an open-drain or an open-collector type. Each line must be connected to the supply voltage via a pull-up resistor. A line is then logic high when none of the connected devices drives the line, and logic low if one or more drives the line low. The output drivers contain a slew-rate limiter. The input stages contain a spike suppression unit removing spikes shorter than 50ns. Note that the internal pull-ups in the AVR pads can be enabled by setting the PORT bits corresponding to the SCL and SDA pins, as explained in the I/O Port section. In some systems, the internal pull-ups can eliminate the need for external resistors. The following figure shows how to connect the TWI units to the TWI bus. The value of Rp depends on VCC and the bus capacitance, typically 4.7k. Figure 1-4. TWI Connection 1.2.4. Address Match Unit The Address Match unit is only used in slave mode, and checks if the received address bytes match the 7-bit address in the TWI Address Register (TWAR). Upon an address match, the Control Unit is informed, allowing correct action to be taken. The TWI may or may not acknowledge its address, depending on settings in the TWCR. Although the clock system to the TWI is turned off in all sleep modes, the interface can still acknowledge its own Slave address or the general call address by using the TWI Bus clock as a clock source. The part 6

will then wake up from sleep and the TWI will hold the SCL clock low during the wake up and until the TWINT Flag is cleared. 1.2.5. Bus Interface Unit This unit contains the Data and Address Shift Register (TWDR), a START/STOP Controller, and Arbitration detection hardware. The TWDR contains the address or data bytes to be transmitted, or the address or data bytes received. In addition it also contains a register containing the (N)ACK bit to be transmitted or received. The START/STOP Controller is responsible for generation and detection of START, REPEATED START, and STOP conditions. The START/STOP controller is able to detect START and STOP conditions even when the AVR MCU is in one of the sleep modes, enabling the MCU to wake up if addressed by a Master. If the TWI has initiated a transmission as Master, the Arbitration Detection hardware continuously monitors the transmission trying to determine if arbitration is in process. If the TWI has lost an arbitration, the Control Unit is informed. Correct action can then be taken and appropriate status codes generated. 7

2. Implementation The implemented code in this application note is a pure master driver. The TWI modules also support slave operation. See AVR311: Using the TWI module as I 2 C slave application note for a sample of a slave driver. The master and slave drivers could be merged to one combined master and slave driver, but this is not in the scope of this application note. The master driver C-code consists of three files. 1. TWI_Master.c 2. TWI_Master.h 3. Main.c There is an example on how to use the driver in the main.c file. The TWI_Master.h file must be included in the main application and contains all function declarations, a define of the Bit Rate Register (TWBR), and defines for all TWI status codes. The TWBR must be set according to the description earlier in this application note. The TWI status code defines can be used to evaluate error messages and to take appropriate actions. The TWI_Master.c file contains all the driver functions. 2.1. Functions The driver consists of the TWI Interrupt Service Routine and six functions. All functions are available for use outside the driver file scope. Some of them are however also used internally by the driver itself. All functions in the driver are listed in the following table. Table 2-1. Description of functions in the TWI Master driver Function name void TWI_Master_Initialise( ) Description Call this function to set up the TWI master to its initial standby state. Remember to enable interrupts from the main application after initializing the TWI. void TWI_Start_Transceiver_with_Data ( unsigned char *msg, unsigned char msgsize) void TWI_Start_Transceiver( ) unsigned char TWI_Transceiver_Busy( ) Call this function to send a prepared message. The first byte must contain the slave address and the read/write bit. Consecutive bytes contain the data to be sent, or empty locations for data to be read from the slave. Also include how many bytes that should be sent/read including the address byte. The function will hold execution (loop) until the TWI_ISR has completed with the previous operation, then initialize the next operation and return. Call this function to resend the last message. The driver will reuse the data previously put in the transceiver buffers. The function will hold execution (loop) until the TWI_ISR has completed with the previous operation, then initialize the next operation and return. Call this function to test if the TWI_ISR is busy transmitting. 8

Function name unsigned char TWI_Get_State_Info( ) unsigned char TWI_Get_Data_From_Transceiver ( unsigned char *message, unsigned char messagesize) ISR(TWI_vect)(For GCC)/ interrupt void TWI_ISR(void) (For IAR) Description Call this function to fetch the state information of the previous operation. The function will hold execution (loop) until the TWI_ISR has completed with the previous operation. If there was an error, then the function will return the TWIState code. Call this function to read out the requested data from the TWI transceiver buffer. I.e. first call TWI_Start_Transceiver to send a request for data to the slave. Then Run this function to collect the data when they have arrived. Include a pointer to where to place the data and the number of bytes requested (including the address field) in the function call. The function will hold execution (loop) until the TWI_ISR has completed with the previous operation, before reading out the data and returning. If there was an error in the previous transmission the function will return the TWI error code. This function is the Interrupt Service Routine (ISR), and automatically called when the TWI interrupt is triggered; that is whenever a TWI event has occurred. This function should not be called directly from the main application. The following table consists the description of the driver register byte containing status information from the last transceiver operation. Available as bit fields within a byte. Table 2-2. Description of the Driver Status Register Byte TWI_statusReg TWI_statusReg.lastTransOK Description Set to 1 when an operation has completed successfully. The following figure shows a flowchart of the process of sending and requesting data over the TWI interface through the drivers. Data is passed through parameters to the functions while the status of an operation is available trough a global status variable. 9

Figure 2-1. Calling the TWI Driver from the Application Send data Request data Prepare transmission buffer with slave address, R/W bit and data to send Prepare transmission buffer with slave address and R/W bit. Call Transeiver with pointer to buffer and number of bytes to send Call Transeiver with pointer to buffer and number of bytes to request Do something else while waiting for TWI transmission to complete Do something else while waiting for TWI reception to complete When complete, check statusreg to se if transmission went ok or if special actions must be taken Call GetData to check if reception went ok, and read out received data, or if special actions must be taken Return Return The following figure shows a flowchart which contains the description of TWI Driver itself. The Transceiver function copies the complete message into the transmission buffer. Then it enables the TWI interrupt to initiate the transmission. The Interrupt then takes care of the complete transmission and disables itself when the transmission is completed, or if an error state occurs. This way the driver can poll the interrupt enable bit to check if a transmission is ongoing. The main application is only allowed to access the global transceiver variables while the TWI transceiver is not busy. The interrupt stores eventual error states in a variable that is available for the main application through a function call. Figure 2-2. TWI Driver Functions 10

In the following flowchart shows a more detailed description of the actions for each event/state in the TWI Interrupt Service Routine. The left column contains the different states/events the TWI state machine can be in when entering the Interrupt. A case switch executes the different actions dependent on the cause of the interrupt call. Figure 2-3. TWI Interrupt Service Routine TWI Interrupt START has been transmitted Repeated START has been transmitted Reset bufferpointer SLA+W has been tramsmitted and ACK received Data byte has been tramsmitted and ACK received buffer pointer at the end? Yes Data byte has been received and ACK tramsmitted SLA+R has been tramsmitted and ACK received Data byte has been received and NACK tramsmitted Arbitration lost Copy from data register to current buffer position. Copy from data register to current buffer position. Post incr pointer. No Copy data from current buffer position to data register. Post incr pointer. Set Success bit in StatusReg Disable TWI interrupt, initalise a STOP condition and clear interrupt flag Error States SLA+W/R has been tramsmitted and NACK received Data byte has been tramsmitted and NACK received Bus error due to an illegal START or STOP condition Initalise a RESTART condition and clear TWI interrupt flag Store TWI state information Disable TWI interrupt Set Success bit in StatusReg Disable TWI interrupt, initalise a STOP condition and clear interrupt flag Yes Intialise a NACK after reception and clear TWI interrupt flag buffer pointer position at byte before the end? No Intialise a ACK after reception and clear TWI interrupt flag Clear TWI interrupt flag Return 11

3. Summary This application note describes the steps to configure the TWI module as a Master and provides an example driver software to implement the module. The firmware for Master Driver is available as a download along with this application note. 12

4. Revision History Doc Rev. Date Comments 2564D 03/2016 The firmware is ported to Atmel Studio 7 2564C 01/2010 Updated table for bit rates 2564B 09/2007 Removed text about TWBR needs to be higher than 10 2564A 04/2006 Initial document release. 13

Atmel Corporation 1600 Technology Drive, San Jose, CA 95110 USA T: (+1)(408) 441.0311 F: (+1)(408) 436.4200 www.atmel.com 2016 Atmel Corporation. / Rev.: Atmel, Atmel logo and combinations thereof, Enabling Unlimited Possibilities, AVR, megaavr, and others are registered trademarks or trademarks of Atmel Corporation in U.S. and other countries. Other terms and product names may be trademarks of others. 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 THE ATMEL TERMS AND CONDITIONS OF SALES LOCATED ON THE ATMEL WEBSITE, 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 AND PROFITS, BUSINESS INTERRUPTION, OR LOSS OF INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT, 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 products descriptions at any time without notice. Atmel does not make any commitment to update the information contained herein. Unless specifically provided otherwise, Atmel products are not suitable for, and shall not be used in, automotive applications. Atmel products are not intended, authorized, or warranted for use as components in applications intended to support or sustain life. SAFETY-CRITICAL, MILITARY, AND AUTOMOTIVE APPLICATIONS DISCLAIMER: Atmel products are not designed for and will not be used in connection with any applications where the failure of such products would reasonably be expected to result in significant personal injury or death ( Safety-Critical Applications ) without an Atmel officer's specific written consent. Safety-Critical Applications include, without limitation, life support devices and systems, equipment or systems for the operation of nuclear facilities and weapons systems. Atmel products are not designed nor intended for use in military or aerospace applications or environments unless specifically designated by Atmel as military-grade. Atmel products are not designed nor intended for use in automotive applications unless specifically designated by Atmel as automotive-grade.