CSE190 Winter 2020: SPI and I2C
CSE190 Winter 2020: SPI and I2C
Lecture 7
SPI and I2C
2
SPI clocking: there is no “standard way”
• Four clocking “modes”
– Two phases
– Two polarities
• Master and selected slave must be in the same mode
• During transfers with slaves A and B, Master must
– Configure clock to Slave A’s clock mode
– Select Slave A
– Do transfer
– Deselect Slave A
– Configure clock to Slave B’s clock mode
– Select Slave B
– Do transfer
– Deselect Slave B
• Master reconfigures clock mode on-the-fly! 3
SPI timing diagram
4
SPI Pros and Cons
• Pros:
– Fast and easy
• Fast for point-to-point connections
• Easily allows streaming/Constant data inflow
• No addressing/Simple to implement
– Everyone supports it
• Cons:
– SS makes multiple slaves very complicated
– No acknowledgement ability
– No inherent arbitration
– No flow control
5
I2C bus (in our projects)
• Communication with the accelerometer
– Read acceleration values and configure interrupts
• Pros
– Two wires bus that can connect multiple
peripherals with the MCU
• Cons
– Overhead is significantly higher, and bus is slower
I2C Details
• Two lines
– Serial data line (SDA)
– Serial clock line (SCL)
SDA
SCL
Data line stable; Change
Data valid of data
allowed
10 of 40
Start and Stop Conditions
• A transition of the data line while the clock line is high is
defined as either a start or a stop condition.
• Both start and stop conditions are generated by the bus
master
• The bus is considered busy after a start condition, until a
stop condition occurs
SDA SDA
SCL SCL
Start Stop
Condition Condition
11 of 40
I C Addressing
2
12 of 40
I2C-Connected System
• In both cases, the master (microcontroller A) generates the timing and terminates the
transfer
Exercise: How fast can I2C run?
• How fast can you run it?
• Assumptions
– 0’s are driven
– 1’s are “pulled up”
• Some working figures
– Rp = 10 kΩ
– Ccap = 100 pF
– VDD = 5 V
– Vin_high = 3.5 V
• Recall for RC circuit
– Vcap(t) = VDD(1-e-t/τ)
– Where τ = RC
15
Exercise: Bus bit rate vs Useful data rate
• An I2C “transactions” involves the following bits
– <S><A6:A0><R/W><A><D7:D0><A><F>
• Which of these actually carries useful data?
– <S><A6:A0><R/W><A><D7:D0><A><F>
• So, if a bus runs at 400 kHz
– What is the clock period?
– What is the data throughput (i.e. data-bits/second)?
– What is the bus “efficiency”?
16
How to operate the accelerometer?
Accel
I2C register 1
MCU I2C register 2
….
Springs
https://www.youtube.com/watch?v=eqZgxR6eRjo