topic 18-2 i2c

Upload: anu-abraham

Post on 06-Apr-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/3/2019 Topic 18-2 I2C

    1/53

    ELET 3232Topic 18b: The I 2C Bus

    Microcontroller Systems

  • 8/3/2019 Topic 18-2 I2C

    2/53

    ObjectivesTo understand the basics of the I 2C busTo understand the format of a serialtransmission between I 2C devicesTo understand how AVR devicesimplement the I 2C bus

    TWI: Two Wire Interface

    1/5/2011 2

  • 8/3/2019 Topic 18-2 I2C

    3/53

    I2C IntroductionLow end serial communication optionDeveloped by Philips

    Short for Inter-IC (integrated circuit) busOften used to communicate acrosscircuit-board distances

    1/5/2011 3

  • 8/3/2019 Topic 18-2 I2C

    4/53

    I2C IntroductionI2C provides support for communication with:

    Slow, on-board peripheral devices that are accessedintermittently

    It is a simple, low-bandwidth, short-distanceprotocol.Most I 2C devices operate at speeds up to400Kbps

    Some operate in the low megahertz rangeI2C has a built-in addressing schemeUsed to link multiple devices together

    1/5/2011 4

  • 8/3/2019 Topic 18-2 I2C

    5/53

    I2C IntroductionPhilips originally developed I 2C forcommunication between devices inside ofa TV setExamples of I 2C-compatible devicesinclude:

    EEPROMsThermal sensorsReal-time clocks

    1/5/2011 5

  • 8/3/2019 Topic 18-2 I2C

    6/53

    I2C IntroductionI2C is also used as a control interface to signalprocessing devicesFor instance, it is commonly used in multimedia

    applications including:RF tunersVideo decoders and encodersAudio processors

    Philips, National Semiconductor, Xicor,Siemens, and other manufacturers offerhundreds of I 2C-compatible devices.

    1/5/2011 6

  • 8/3/2019 Topic 18-2 I2C

    7/53

    I2C IntroductionI2C is appropriate for interfacing to devices on asingle board

    Can be stretched across multiple boards inside a closedsystem, but not much further.

    An example:A host CPU on a main embedded board using I 2C tocommunicate with user interface devices located on aseparate front panel board.

    A second example:SDRAM DIMMs: can feature an I 2C EEPROM containingparameters needed to correctly configure a memorycontroller for that module

    1/5/2011 7

  • 8/3/2019 Topic 18-2 I2C

    8/53

    SDA and SCLI2C is a two-wire serial bus

    The two I 2C signals are serial data (SDA)and serial clock (SCL)

    1/5/2011 8

    Central

    Microcontroller

    Peripheral Device #1

    Peripheral Device #2

    Peripheral Device #3

    Peripheral Device #4

    SDA

    SCL

  • 8/3/2019 Topic 18-2 I2C

    9/53

    SDA and SCLSDA and SCL support serial transmission of8-bit

    bytes of data-7-bit device addresses pluscontrol bits-over the two-wire serial bus.

    1/5/2011 9

    Central

    Microcontroller

    Peripheral Device #1

    Peripheral Device #2

    Peripheral Device #3

    Peripheral Device #4

    SDA

    SCL

  • 8/3/2019 Topic 18-2 I2C

    10/53

    SDA and SCLThe device that initiates communication isthe master

    The master normally controls the clock signal.A device being addressed by the master is called aslave

    1/5/2011 10

    Central

    Microcontroller

    Peripheral Device #1

    Peripheral Device #2

    Peripheral Device #3

    Peripheral Device #4

    SDA

    SCL

  • 8/3/2019 Topic 18-2 I2C

    11/53

    Masters and SlavesAn I2C slave can hold off the master during atransaction

    Uses what's called clock stretching (the slave keepsSCL pulled low until it's ready to continue)Most I 2C slave devices don't use this feature, but everymaster should support it.

    The I 2C protocol supports multiple mastersMost system designs include only one

    There may be one or more slaves on the busBoth masters and slaves can receive and transmitdata bytes

    1/5/2011 11

  • 8/3/2019 Topic 18-2 I2C

    12/53

    Masters and SlavesEach I 2C-compatible slave device comeswith a predefined device address

    The lower bits of which may be configurable atthe board levelThis limits the number of identical slave deviceson an I 2C bus without contention

    The limit set by the number of user-configurableaddress bits (typically two bits, allowing up tofour identical devices)

    1/5/2011 12

  • 8/3/2019 Topic 18-2 I2C

    13/53

    Masters and SlavesThe master transmits the device address of theintended slave at the beginning of everytransaction

    Each slave is responsible for monitoring the bus andresponding only to its own address

    There are four potential modes of operation for agiven bus device, although most devices only usea single role and its two modes:

    master transmit master node is sending data to a slavemaster receive master node is receiving data from a slaveslave transmit slave node is sending data to a masterslave receive slave node is receiving data from the master

    1/5/2011 13

  • 8/3/2019 Topic 18-2 I2C

    14/53

    Masters and Slaves

    The master begins the communication by issuing thestart condition (S)

    initially in master transmit mode

    The master then sends a unique 7-bit slave deviceaddress, with the most significant bit (MSB) first

    1/5/2011 14

  • 8/3/2019 Topic 18-2 I2C

    15/53

    Masters and Slaves

    The eighth bit after the start, (read/not-write),specifies whether the slave is now to receive (0) orto transmit (1) dataThis is followed by an ACK bit issued by thereceiver, acknowledging receipt of the previous byte.

    1/5/2011 15

  • 8/3/2019 Topic 18-2 I2C

    16/53

    Masters and Slaves

    Then the transmitter (slave or master, as indicated bythe R/W bit) transmits a byte of data starting with theMSB.

    At the end of the byte, the receiver (whether master orslave) issues a new ACK bit.This 9-bit pattern is repeated if more bytes need to betransmitted

    1/5/2011 16

  • 8/3/2019 Topic 18-2 I2C

    17/53

    Masters and Slaves

    When the master is done transmitting all of the databytes it wants to send in a write transaction (slavereceiving), it monitors the last ACK and then issuesthe stop condition (P)

    1/5/2011 17

  • 8/3/2019 Topic 18-2 I2C

    18/53

    Masters and Slaves

    In a read transaction (slave transmitting)The master acknowledges all bytes it receives (sends andACK bit) except the last one

    It does not sends and ACK bit for the final byte it receives,it issues the stop conditionThis tells the slave that its transmission is done.

    1/5/2011 18

  • 8/3/2019 Topic 18-2 I2C

    19/53

    How does the AVR implement

    I2

    C protocols?

    1/5/2011 19

  • 8/3/2019 Topic 18-2 I2C

    20/53

    TWI: Two Wire InterfaceThe TWI protocol allows the systems designer tointerconnect up to 128 different devices using onlytwo bi-directional bus lines:

    One for clock (SCL) and one for data (SDA).The only external hardware needed to implementthe bus is a single pull-up resistor for each of theTWI bus lines.

    1/5/2011 20

    The bus drivers of all TWI-compliantdevices are open-drain or open-collector.

    This implements a wired-AND functionwhich is essential to the operation of theinterface.

  • 8/3/2019 Topic 18-2 I2C

    21/53

    TWI: Two Wire InterfaceEach data bit transferred on the TWI bus isaccompanied by a pulse on the clock line.The level of the data line must be stable when the

    clock line is high.The only exception to this rule is for generating startand stop conditions.

    1/5/2011 21

  • 8/3/2019 Topic 18-2 I2C

    22/53

    TWI: Two Wire InterfaceThe master initiates and terminates a datatransmissionThe transmission is initiated when the master issues

    a START condition on the busIt is terminated when the master issues a STOPcondition.Between a START and a STOP condition, the bus isconsidered busy, and no other master should try toseize control of the bus.

    1/5/2011 22

  • 8/3/2019 Topic 18-2 I2C

    23/53

    Start/Stop/Repeated StartA special case occurs when a new START condition is issuedbetween a START and STOP condition.

    Referred to as a REPEATED START conditionUsed when the master wishes to initiate a new transfer without

    relinquishing control of the bus.After a REPEATED START, the bus is considered busy untilthe next STOP.START (and REPEATED START) and STOP conditions are

    signaled by changing the level of the SDA line when the SCLline is high

    1/5/2011 23

  • 8/3/2019 Topic 18-2 I2C

    24/53

    AddressesAll address packets are 9 bits long:

    7 address bitsOne Read/Write control bit

    If Read/Write = 1, a read operation will be performedIf Read/Write = 0, a write operation will be performed

    One acknowledge bitWhen a slave recognizes that it is being addressed, it shouldacknowledge by pulling SDA low in the ninth SCL (ACK) cycle

    1/5/2011 24

  • 8/3/2019 Topic 18-2 I2C

    25/53

    Typical Data TransmissionA transmission consists of a START condition, aSLA+R/W, one or more data packets, and a STOPcondition

    SLA = Slave Address

    An empty message, consisting of a START followedby a STOP condition is illegal.

    1/5/2011 25

  • 8/3/2019 Topic 18-2 I2C

    26/53

    Using the TWI

    1/5/2011 26

  • 8/3/2019 Topic 18-2 I2C

    27/53

    Interrupt BasedThe AVR TWI:

    Byte-orientedInterrupt basedInterrupts are issued after all bus events:

    Ex: reception of a byte or transmission of a START condition.

    Two Control bits enable interrupts:TWI Interrupt Enable (TWIE) bit in TWCRGlobal Interrupt Enable bit in SREG

    If both are enabled: assertion of the TWINT flag will generate an interruptrequestIf the TWIE bit is cleared (disabled), the application must poll the TWINTflag in order to detect actions on the TWI bus

    1/5/2011 27

  • 8/3/2019 Topic 18-2 I2C

    28/53

    Interrupt BasedWhen the TWINT flag is asserted (high), the TWI has finishedan operation and awaits application response

    The TWI Status Register (TWSR) contains a value indicating thecurrent state of the TWI bus

    The TWINT flag is set in the following situations:After the TWI has transmitted a START/REPEATED START conditionAfter the TWI has transmitted SLA+R/WAfter the TWI has transmitted an address byteAfter the TWI has lost arbitrationAfter the TWI has been addressed by own slave address or general callAfter a STOP or REPEATED START has been received while still addressed as aslaveAfter the TWI has received a data byteWhen a bus error has occurred due to an illegal START or STOP condition

    1/5/2011 28

  • 8/3/2019 Topic 18-2 I2C

    29/53

    TWI Bit Rate Register

    TWBR selects the division factor for the bit rate generatorThe bit rate generator is a frequency divider which generates the SCL clock frequency in theMaster modes

    1/5/2011 29

  • 8/3/2019 Topic 18-2 I2C

    30/53

    TWI Control Register

    The TWCR is used to control the operation of the TWIEnables the TWIInitiates a master access by applying a START condition to the busGenerates a receiver acknowledgeGenerates a stop conditionControls halting of the bus while the data to be written to the bus are

    written to the TWDRIndicates a write collision if a data write is attempted to the TWDRwhile the register is inaccessible

    1/5/2011 30

  • 8/3/2019 Topic 18-2 I2C

    31/53

    TWI Control Register

    TWINT:TWI Interrupt FlagThis bit is set by hardware when the TWI has finished its current joband expects application software responseIf the I-bit in SREG and TWIE in TWCR are set, the MCU will jump tothe TWI interrupt vector (the ISR)

    While the TWINT flag is set, the SCL low period is stretched

    1/5/2011 31

  • 8/3/2019 Topic 18-2 I2C

    32/53

    TWI Control Register

    TWINT:TWI Interrupt FlagThe TWINT flag must be cleared through software by writing a logic one to it .Note that this flag is not automatically cleared by hardware whenexecuting the interrupt routine

    Also note that clearing this flag starts the operation of the TWI, so allaccesses to the TWI Address Register (TWAR), TWI Status Register(TWSR), and TWI Data Register (TWDR) must be complete beforeclearing this flag.

    1/5/2011 32

  • 8/3/2019 Topic 18-2 I2C

    33/53

    TWI Control Register

    TWEA:TWI Enable Acknowledge BitThe TWEA bit controls the generation of the acknowledge pulse

    If a one is written to the TWEA bit, the ACK pulse is generated on the TWI bus ifthe following conditions are met:

    The devices own slave address has been receivedA general call has been received, while the TWGCE bit in the TWAR is setA data byte has been received in Master Receiver or Slave Receiver mode

    By writing a 0 to the TWEA bit, the device can be virtually disconnected from the Two-wire Serial Bus (temporarily)

    Address recognition can then be resumed by writing the TWEA bit to one again

    1/5/2011 33

  • 8/3/2019 Topic 18-2 I2C

    34/53

    TWI Control Register

    TWSTA:TWI START Condition BitThe application writes a 1 to the TWSTA bit when it desires tobecome a master on the Two-wire Serial BusThe TWI hardware checks if the bus is available, and generates aSTART condition on the bus if it is free

    If the bus is not free, the TWI waits until a STOP condition is detected, and thengenerates a new START condition to claim the bus Master status

    TWSTA must be cleared by software when the START condition hasbeen transmitted

    1/5/2011 34

  • 8/3/2019 Topic 18-2 I2C

    35/53

  • 8/3/2019 Topic 18-2 I2C

    36/53

    TWI Control Register

    TWWC:TWI Write Collision FlagThe TWWC bit is set when attempting to write to the TWI DataRegister (TWDR) when TWINT is lowThis flag is cleared by writing data to the TWDR Register whenTWINT is high

    1/5/2011 36

  • 8/3/2019 Topic 18-2 I2C

    37/53

    TWI Control Register

    TWEN:TWI Enable BitThe TWEN bit enables TWI operation and activates the TWI interface.When a one is written to TWEN, the TWI takes control over the I/Opins connected to the SCL and SDA pinsIf this a 0 is written tot his bit, the TWI is switched off and all TWI

    transmissions are terminated, regardless of any ongoing operation

    1/5/2011 37

  • 8/3/2019 Topic 18-2 I2C

    38/53

    TWI Control Register

    TWIE:TWI Interrupt EnableWhen a one is written to this bit, and the I-bit in SREG (GlobalInterrupt enable) is set, interrupts are enabled for the TWI

    Whenever the TWINT flag is high, a TWI interrupt request is generated

    1/5/2011 38

  • 8/3/2019 Topic 18-2 I2C

    39/53

    TWI Status Register

    TWSR:TWS (7-3): TWI StatusThese 5 bits reflect the status of the TWI logic and the Two-WireSerial BusThe TWSR contains both the 5-bit status value and the 2-bit prescalervalue

    The application designer should mask the prescaler bits to zero when checking theStatus bitsThis makes status checking independent of prescaler setting

    1/5/2011 39

  • 8/3/2019 Topic 18-2 I2C

    40/53

    TWI Status Register

    TWS:TWPS (1-0): TWI Prescaler BitsThese bits can be read and writtenThey control the bit rate prescaler (see slide 29)

    1/5/2011 40

  • 8/3/2019 Topic 18-2 I2C

    41/53

    TWI Data Register

    TWDR:In Transmit mode, TWDR contains the next byte to be transmittedIn receive mode, the TWDR contains the last byte receivedIt is not writable when the TWI interrupt flag (TWINT) is setThis occurs while the TWI is in the process of shifting a byte

    1/5/2011 41

  • 8/3/2019 Topic 18-2 I2C

    42/53

    TWI Address Register (slave)

    TWAR:The TWAR should be loaded with the 7-bit slave address (in the sevenmost significant bits of TWAR) to which the TWI will respond whenprogrammed as a slave transmitter or receiver , and not needed in themaster modesThe LSB of TWAR is used to enable recognition of the general calladdress ($00)

    There is an associated address comparator that looks for the slave address (or generalcall address if enabled) in the received serial addressIf a match is found, an interrupt request is generated.

    1/5/2011 42

  • 8/3/2019 Topic 18-2 I2C

    43/53

    Using the TWI

    1/5/2011 43

    ldi r16,(1

  • 8/3/2019 Topic 18-2 I2C

    44/53

    Using the TWI

    1/5/2011 44

    wait1:in r16,TWCRsbrs r16,TWINTrjmp wait1

    while (!(TWCR & (1

  • 8/3/2019 Topic 18-2 I2C

    45/53

    Using the TWI

    1/5/2011 45

    in r16,TWSRandi r16, 0xF8cpi r16, STARTbrne ERROR

    if ((TWSR & 0xF8) != START) ERROR();Step 3a:

    Checks the value of TWI Status Register and masks theprescaler bits. If status is different from the START conditiongo to ERROR

  • 8/3/2019 Topic 18-2 I2C

    46/53

    Using the TWI

    1/5/2011 46

    ldi r16, SLA_Wout TWDR, r16ldi r16, (1

  • 8/3/2019 Topic 18-2 I2C

    47/53

    Using the TWI

    1/5/2011 47

    wait2:in r16,TWCRsbrs r16,TWINTrjmp wai t2

    while (!(TWCR & (1

  • 8/3/2019 Topic 18-2 I2C

    48/53

    Using the TWI

    1/5/2011 48

    in r16,TWSRandi r16, 0xF8cpi r16, MT_SLA_ACKbrne ERROR

    if ((TWSR & 0xF8) != MT_SLA_ACK)ERROR();

    Step 5a:

    Checks the value of TWI Status Register and masks theprescaler bits. If the status is different from MT_SLA_ACK goto ERROR

  • 8/3/2019 Topic 18-2 I2C

    49/53

    Using the TWI

    1/5/2011 49

    ldi r16, DATAout TWDR, r16ldi r16, (1

  • 8/3/2019 Topic 18-2 I2C

    50/53

    Using the TWI

    1/5/2011 50

    wait3:in r16,TWCRsbrs r16,TWINTrjmp wai t3

    while (!(TWCR & (1

  • 8/3/2019 Topic 18-2 I2C

    51/53

    Using the TWI

    1/5/2011 51

    in r16,TWSRandi r16, 0xF8cpi r16, MT_DATA_ACKbrne ERROR

    if ((TWSR & 0xF8) != MT_DATA_ACK)ERROR();Step 7a:

    Checks the value of TWI Status Register and masks theprescaler bits. If the status is different from MT_DATA_ACKgo to ERROR

  • 8/3/2019 Topic 18-2 I2C

    52/53

    Using the TWI

    1/5/2011 52

    ldi r16,(1

  • 8/3/2019 Topic 18-2 I2C

    53/53

    SummaryWe discussed:

    The basics of the I 2C busThe format of a serial transmission betweenI2C devicesHow AVR devices implement the I 2C bus

    We looked in detail atTWI: Two Wire Interface on the ATmega128