Fig. 1: I2C Bus Topology

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

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

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

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

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

DS1621 Digital Thermometer and Thermostat

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

Definitions and Documents

DS1621 Digital Thermometer and Thermostat

MODULE BOUSSOLE ÉLECTRONIQUE CMPS03 Référence :

Allows the user to protect against inadvertent write operations. Device select and address bytes are Acknowledged Data Bytes are not Acknowledged

Elettronica dei Sistemi Digitali Costantino Giaconia SERIAL I/O COMMON PROTOCOLS

DS Wire Digital Thermometer and Thermostat

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

Application Note 120 Communicating Through the 1-Wire Master

SPI I2C LIN Ethernet. u Today: Wired embedded networks. u Next lecture: CAN bus u Then: wireless embedded network

AN736. An I 2 C TM Network Protocol for Environmental Monitoring THE I 2 C BUS SPECIFICATION INTRODUCTION

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

Software User Guide UG-461

Data Acquisition Module with I2C interface «I2C-FLEXEL» User s Guide

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

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

PACKAGE OUTLINE DALLAS DS2434 DS2434 GND. PR 35 PACKAGE See Mech. Drawings Section

DS1307ZN. 64 x 8 Serial Real-Time Clock

DS18B20 Programmable Resolution 1-Wire Digital Thermometer

DS1821 Programmable Digital Thermostat and Thermometer

UM I 2 C-bus specification and user manual. Document information

Serial port interface for microcontroller embedded into integrated power meter

TDA CHANNEL VOLUME CONTROLLER 1 FEATURES 2 DESCRIPTION. Figure 1. Package

RETRIEVING DATA FROM THE DDC112

How to design and implement firmware for embedded systems

Introduction the Serial Communications Huang Sections 9.2, 10.2 SCI Block User Guide SPI Block User Guide

Serial Communications

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

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

USER GUIDE EDBG. Description

Welcome to the Introduction to Controller Area Network web seminar My name is William Stuart, and I am a Applications Engineer for the Automotive

HANDLING SUSPEND MODE ON A USB MOUSE

HDMM01 V1.0. Dual-axis Magnetic Sensor Module With I 2 C Interface FEATURES. Signal Path X

Designing VM2 Application Boards

Pmod peripheral modules are powered by the host via the interface s power and ground pins.

Microcomputer Protocol Implementation at Local Interconnect Network Georgi Krastev

Arbitration and Switching Between Bus Masters

TI ARM Lab 7 Accelerometers

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

Embedded Systems Design Course Applying the mbed microcontroller

1. Computer System Structure and Components

Old Company Name in Catalogs and Other Documents

DS2401 Silicon Serial Number

Interfacing 3V and 5V applications

MicroMag3 3-Axis Magnetic Sensor Module

Web Site: Forums: forums.parallax.com Sales: Technical:

Bi-directional level shifter for I²C-bus and other systems.

Integration of Arduino as a slave system to LonWorks based System using I2C Interface

Raspberry Pi. Hans- Petter Halvorsen, M.Sc.

Data Cables. Schmitt TTL LABORATORY ELECTRONICS II

A+ Guide to Managing and Maintaining Your PC, 7e. Chapter 1 Introducing Hardware

Accessing I2C devices with Digi Embedded Linux 5.2 example on Digi Connect ME 9210

CHAPTER 11: Flip Flops

FUNCTIONAL BLOCK DIAGRAM SDA SCL SMBALERT. SMBus SERIAL BUS INTERFACE ADDRESS SELECTION PWM CONFIG AUTOMATIC FAN SPEED CONTROL REGISTERS

Design and Verification of Nine port Network Router

Fiber Optic Monitor and Control Evaluation Kit

Below is a diagram explaining the data packet and the timing related to the mouse clock while receiving a byte from the PS-2 mouse:

HP03 BAROMETER MODULE Version: 1.1

RS-485 Protocol Manual

8-Bit Flash Microcontroller for Smart Cards. AT89SCXXXXA Summary. Features. Description. Complete datasheet available under NDA

The Programming Interface

ZL30136 GbE and Telecom Rate Network Interface Synchronizer

TDA7318D DIGITAL CONTROLLED STEREO AUDIO PROCESSOR

Chapter 13. PIC Family Microcontroller

Serial Communications

The I 2 C-bus and how to use it (including specifications)

TIMING DIAGRAM O 8085

Computer Performance. Topic 3. Contents. Prerequisite knowledge Before studying this topic you should be able to:

Microcontroller Based Low Cost Portable PC Mouse and Keyboard Tester

AN2680 Application note

MMC314xMR. Ultra Small 3-axis Magnetic Sensor, With I 2 C Interface. Signal Path X. Signal Path Y. Signal Path Z FEATURES

Section 14. Compare/Capture/PWM (CCP)

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

AN LPC1700 timer triggered memory to GPIO data transfer. Document information. LPC1700, GPIO, DMA, Timer0, Sleep Mode

Using the HT46R46 I/O Ports to Implement Half-Duplex SPI Communication

FM75 Low-Voltage Two-Wire Digital Temperature Sensor with Thermal Alarm

UniPi technical documentation REV 1.1

AN2824 Application note

THE I 2 C-BUS SPECIFICATION VERSION 2.1 JANUARY document order number:

AN460 Using the P82B96 for bus interface

Using Altera MAX Series as Microcontroller I/O Expanders

PCAN-MicroMod Universal I/O Module with CAN Interface. User Manual. Document version ( )

MPR121 Serial Communication

Sample Project List. Software Reverse Engineering

Chapter 2 Logic Gates and Introduction to Computer Architecture

AN3265 Application note

Embedded Systems on ARM Cortex-M3 (4weeks/45hrs)

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

Project 4: Pseudo USB Simulation Introduction to UNIVERSAL SERIAL BUS (USB) STANDARD

Operating Systems 4 th Class

Web Site: Forums: forums.parallax.com Sales: Technical:

INPUT/OUTPUT ORGANIZATION

Lizy Kurian John Electrical and Computer Engineering Department, The University of Texas as Austin

Transcription:

Firmware Emulation of an I2C Slave Device by Steve Kolokowsky, Cypress Semiconductor Many embedded microprocessors contain a two-wire serial interface, which looks remarkably similar to Philips I²C (I2C) interface. In many designs these two pins are an unused access point into the microprocessor. This article will provide a detailed look at how to implement the slave side of the I2C interface using bit banging. Soon you will be able to use two GPIOs on your own processor for inter-processor communication, which will save a lot of pins over an 8-bit parallel bus. The I2C standard is covered by patents owned by the Philips Semiconductor spin-off, NXP, created in 2006. In October 2000, Philips was aggressively enforcing these patents, filing a lawsuit against six semiconductor companies. However, according to NXP s website, "I²C licenses under the remaining patents in the program will be free of any royalties, for any use of the patents after October 1, 2006, without any prejudice to any claims for past use whatsoever. Also the fee for obtaining an I²C slave address allocation will not be affected and will continue to apply until further notice. I2C Overview The I2C interface is a shared two-wire serial bus. The bus allows communication between a single master and a single slave at any given time, but multiple masters and slaves can share the same bus. CPU devices are typically masters and peripheral devices are usually slaves. The master device initiates the transaction and provides the clock. The slave devices are identified by a 7-bit address, theoretically allowing up to 127 slave devices on any I2C bus. Fig. 1 shows the I2C bus topology. VCC SCL SDA Device 1 Device 2 Device 3 Fig. 1: I2C Bus Topology

Each bus transaction is initiated by the master device. The first byte of the transaction is a 7-bit address field and a single read/write (r/w) bit. If a slave device matches the address, it responds to the address byte with an ACK bit by pulling SDA low during the 9th bit time. If the r/w bit is 1 (read), the slave device sends one or more data bytes to the host which the host ACKs in the same fashion. The slave stops sending data when it receives a STOP bus condition from the host. If the r/w bit is 0 (write) the roles are reversed with the master sending data and the slave sending the ACK. I2C defines two special bus conditions called and STOP see Fig. 2). occurs when there is a falling edge on SDA with SCL held high. After the condition, the master drives SCL low, and then sends the address byte. A STOP condition is the opposite of, a rising edge on SDA with SCL high. During normal DATA and ACK transmission, the data bus is stable when SCL is high, transitioning when SCL is low. The bus is idle when both lines are 1 (VCC). SCL and SDA are both wired-or signals implemented with open-collector/open-drain I/O cells. Open collector signals are driven low, but not driven high. The bus is pulled high by a resistor attached to VCCIO. This allows multiple devices to share the bus at the same time and minimizes concerns about transitions between transmission and reception. The use of wired-or signals even allows multiple master devices to collide on the bus without damaging each other s drivers. STOP SDA SCL Clock pulse: Data stable Data changes I2C Emulation In Firmware Fig. 2: I2C And STOP Conditions Emulating I2C via GPIOs requires attention to four protocol layers: Hardware interface: simulating open drain i/o cells Bit-level protocol:, STOP, data Byte-level protocol: address, data, ACK Application-level: EEPROM, temp sensor, tuner, etc

To understand the sample code below, the following definitions must be understood: SCL, SDA: bit-wide variables mapped to the I/O ports controlling SCL, SDA pins. The same address is used for input and output values OEA: output-enable register containing both SCL and SDA bits bmscl, bmsda: bit masks for SCL, SDA bits within OEA The hardware-interface layer is an easy firmware task. To truly emulate an open-collector I/O cell, only enable the driver when the signal is low. The primitives defined in Fig. 3 will be used by the higher-layer code below. Init() OEA &= ~bmsda; OEA &= ~bmscl; // float the i/os SDA = 0; SCL = 0; // Drive 0 when on Drive1() OEA &= ~bmsda; // Allow pullup to work Drive0() OEA = bmsda; // Drive to 0 Fig. 3: Bit-Level Functions On a slave device, the bit-level protocol is the hardest part of I2C to implement because of the strict timing requirements in the spec. In a 100-kHz device the master can begin sending SCL pulses 4 µs after. In a 400-kHz device this limit is just 600 ns! Another option is to engineer your ISR so that it can operate with a starting point in the acceptable region shown below. This adds one SCL low time to your latency for a total of 8.7 µs at 100 khz. Once you have seen the, you can either stay in the ISR to process the data, or trigger another ISR on the rising edge of SCL. This ISR will also have to be very low latency, since the data are only guaranteed to be stable when SCL is high. This thigh parameter is 4 µs at 100 khz, but only 600 ns at 400 khz. In this example implementation, we remain in a loop watching SCL and SDA until STOP. SDA Best ISR latency 100kHz = 4 usec 400kHz = 600nSec Acceptable 100kHz = 4+4.7 usec 400kHz =.6+1.3 usec SCL Fig. 4: Timing If your device cannot always meet these requirements and you can change the master device, there are two options in the spec to allow for longer timing. One is for the host to transmit a dummy address (0000 0001) called a start byte to allow the ISR to start. This

start byte is not used as an address by any device. This increases the allowable latency to a full +BYTE+ACK time, almost 10 SCL clocks. Another option is to build retransmission capability into the master driver. If the slave device fails to ACK its address the first time, simply issue another and transmit the address again. void ISR_INT1_SDA(void) interrupt WORD retval; Bit readmode; // make sure this is an I2C // (SDA goes low while SCL is high) and set the flag if (SCL) disable_sda_interrupt(); retval = I2C DETECTED; else // False trigger, exit the ISR. return; // Loop for one or several /address conditions before // a STOP. while(retval == I2C DETECTED) retval = readi2cslavebyte(1); // read address, 1=addr match if ((retval & 0xfffe)!= I2C_SLAVE_ADDR) break; // Other addr or STOP received, return. // LSBit of the returned address is the read/!write bit // Enter data transfer phase until STOP or received if (retval & 1) retval = senddata(); else retval = receivedata(); enable_sda_interrupt(); return; Fig. 5: SDA ISR Detects, Then Reads Or Sends Data As Needed Once a has been detected, the next task is to read the address sent by the master and compare it to our slave address. This is done by a generic read routine that can read addresses or data. The only difference between reading an address and reading data is that the slave device should only ACK its own address, while it should ACK all data it

receives. This routine returns a WORD rather than a BYTE to allow special conditions (STOP, ) to be returned. The readi2cslavebyte() routine shown in Fig. 6 waits for the clock to become high, signaling that the data line is stable and can be read. The data bit is stored, and the routine waits for another low to high transition on SCL until 8 bits have been read. While SCL is high, SDA is monitored for or STOP conditions. WORD readi2cslavebyte(bit addrmatch) // Read 8 bits from I2C (MSB-->LSB), respond with ACK // SCL could be high or low coming in here, depending on CPU speed BYTE i; BYTE retval = 0; for (i = 0; i < 8; i++) while (!SCL) ; retval = (retval << 1) SDA; while (SCL) // If SDA changes while SCL is high, it's a // stop (low to high) or start (high to low) condition. if ((retval & 1)!= SDA) return((sda)? (I2C_STOP_DETECTED) : (I2C DETECTED)); // Send ACK -- SCL is low now // 100Khz hold time spec is 0-3.45usec, high speed is 0-.9usec if (!addrmatch ((retval & 0xfe) == I2C_SLAVE_ADDR)) Drive0(); while (!SCL) ; while (SCL); Drive1(); else return(i2c_addr_mismatch); return retval; Fig. 6: readi2cslavebyte Reads Addresses Or Data

Fig. 7 shows the high-level slave state machine. It may look daunting with all of the potential state transitions, but many of them are the same. Any condition puts the device in receive address mode. Any STOP condition triggers wait for. All of the other paths deal with address interpretation: An address mismatch triggers wait for, since every address byte is preceded by a. An address match with the READ bit set moves the state machine to send data, while the WRITE bit causes receive data. The ISR shown in Fig. 5 is triggered by a potential and contains states 1, 2, and 3. 0 WAIT FOR STOP or Addr mismatch 1 RECEIVE ADDRESS STOP Addr match, rw bit = 0 Addr match, rw bit = 1 3 SEND DATA STOP 2 RECEIVE DATA SCL pulses SCL pulses Fig. 7: I2C Slave State Machine Although it is difficult to meet the timing requirements of an I2C slave device, it is possible with the low interrupt latencies that can be achieved without OS overhead. If both sides of the link are under your control, many options exist that can make implementation easier. Once you have an I2C slave on your device, you can free up your big 16-bit bus for more important tasks. References The I2C Bus Specification, version 2.1, January 2000: http://www.nxp.com

About The Author Steve Kolokowsky is a Member of the Technical Staff in Cypress Semiconductor's Consumer and Computation Division (CCD). Steve's focus is high-speed USB peripheral products. He has participated in the design of many of today's highest selling MP3 players. Steve earned a BS in Systems Engineering from Rensselaer Polytechnic Institute in Troy, NY. He is based in sunny San Diego, California, USA. He can be reached at syk@cypress.com