嵌入式系统代写代做代考 Embedded Systems Reconfigurable computing

Reconfigurable computing

Small Embedded Systems

Unit 4.5
Synchronous Serial Communication: I2C

Introduction
Inter-Integrated Circuit (I2C)
I2C Transactions
An I2C Lab Example using Arduino functions
Comparison of I2C and SPI

Inter Integrated Circuit (I2C)
Invented in the 1980’s by Phillips (now NXP semiconductors) to support low-speed communication between several ICs. Standardized in the 1990s.
I2C requires only two wires
Serial Data Line (SDA)
Serial Clock Line (SCL)
Prior to 2006, I2C was a trademark of NXP:
companies had to pay a license fee to use the name
so companies normally used the name TWI (two wire interface), which required no license fee payments

Signalling on Two Wires
The wires have pull-up resistors connected to power (VDD)
The connected devices (master, slaves) can
Drive a zero onto the wire
Tri-state their outputs to disconnect their outputs from the wire
If no device is driving a 0 then the wire goes high
If any device is driving a 0 then the wire goes low

I2C Master / Slave
All communications initiated by Master (to avoid garbled messaging).
When a master wants to write to a slave, it transmits a slave’s unique address to initiate communication.
When a master wants to receive data, the master must send a read request with the slave’s address and then the slave will transmit
Overhead: for every 8 bits of data, “ACK” bit must be returned.

Device address
R/W
7 bits
1 bit

Master sends Start signal to bus
SDA transitions high→low while SCL is high
Master sends 7-bit slave address plus read (1) or write (0) to device it will contact.
For address and data bits SDA is constant whilst SCL pulses high then low
Slave responds with ACK bit
SDA pulled low

An I2C transaction
SDA
SCL
R/W
A4
A3
A2
A1
A0
A5
A6
Start
Slave pulls line low to acknowledge
A high value here would indicate failure to communicate

Address of slave device
Read/write

If write:
Master sends 1-byte message.
Slave responds with ACK bit.
If read:
Slave sends 1-byte message and Master sends ACK.
When communication is complete, Master sends Stop signal
SDA transitions low→high while SCL is high

An I2C transaction (continued)
SDA
SCL

Data
Stop

D5
D4
D3
D2
D1
D0
D6
D7
Ack

I2C Byte format and messages
A bus transaction is a series of one-byte (8-bit) strings
A device address is 7 bits in standard I2C
10 bits in extended I2C
I2C messages:
START (SDA high→low , SCL high)
ADDRESS + READ/WRITE (8 bits then 1 bit ACK)
DATA (8 bits; 1 bit ACK)
STOP (SDA low→high , SCL high)

Example using Arduino Library
TC74A0 ‘Tiny Digital Thermal Sensor’
I2C device returns temperature in °C
Device address is 0x48
It contains two registers locations
0x00 the data register
holds temperature in °C: 8-bit integer
0x01 the configuration register:
puts device into standby mode
1-bit flags packed into 8-bit byte
Microcontroller will send address 0x48 and request return of 1-byte of data starting from location 0x00

The Arduino I2C Library
The I2C library is called “Wire”

#include
int sensor_address = 0x48

void setup()
{
Serial.begin(9600);
Wire.begin();
}

Enable I2C

The Arduino I2C Library
The I2C library is called “Wire”

void loop()
{
Wire.beginTransmission(sensor_address);
Wire.write(0x00);
Wire.endTransmission();

//Request 1 Byte from the specified address
Wire.requestFrom(sensor_address, 1);
//wait for response
while(Wire.available() == 0);
// Get the temp and read it into a variable
int temp = Wire.read();
delay(500);
}

Address of sensor
Register in sensor
How many bytes are we waiting for?
Wait for completion

What if we read multiple sensors?
Each slave must have different address
Each different type of device is manufactured with a different address
If our slaves are different sensors then there is no problem: all have different addresses

What if we had a weather station where slaves #1, #2, #3 are all TC74 temperature sensors?

What if we read multiple sensors?
The TC74 is sold in 8 different model numbers, which are identical in every respect except their address:
TC74A0 – address 0x48
TC74A1 – address 0x49
TC74A2 – address 0x4A
and so on …

We would build our weather station out of devices with different addresses
TC74A0
TC74A1
TC74A2

Multi-master I2C Systems
Some I2C implementations permit multiple masters within one system
The necessitates one more feature of the protocol:
arbitration

Arbitration Between Two Masters

Arbitration to decide which master has control of bus
Masters monitor the value on the wire
If a master sees that it tried to transmit a 1, but the wire took a value of 0 then there must be some other master sending
The master that lost the resolution quits sending
Master 1 sending…
Master 2 sending…
Value on the wire

Comparison of SPI and I2C
SPI advantages
Active drive of 1s and 0s, so high speed
No overhead required for sending of addresses or acknowledgements
Full duplex – transmit and receive at same time
I2C
Pull-up resistors mean lower speed and higher power drain
Half duplex: only send or receive at one time
Acknowledgments mean that I2C master knows whether or not data has been received and can tell if slaves go offline

Comparison of SPI and I2C
I2C advantages
Acknowledgments mean that I2C master knows whether or not data has been received and can tell if slaves go offline
Only two wires needed
Multi-master not too difficult to implement
SPI
Master can’t tell whether slaves are online and have received data
At least 4 wires needed
Multi-master is very awkward

Comparison of SPI and I2C
Name Duplex Speed (kbps) Pin Count
I2C Half Slow: <100 Fast: 400 High-speed: 3,400 2 SPI Full 10,000+ 4+ Maximum usable distance depends on speed required Typically ~1 metre, but variants exist that have longer range Summary Microcontrollers usually use serial communications in preference to parallel Main synchronous interfaces are SPI and I2C Both are short range SPI has higher speed but higher pin count I2C is slower, but more economical with pins /docProps/thumbnail.jpeg