Lecture4-Serial Busses
Lecture4-Serial Busses
Lecture 4
Serial Busses
Internal
&
GPIO/INT Timers USART DAC/ADC
External
Memory
Internal
External
C
Ca pare
e
Ou put
C
te t
pt
C
UA I
C
RT
EM
ur
In tpu
SP
DA
AD
I2
rru
pt
In
m
Co
3
Parallel Bus VS Serial Bus
Simplistic View of Serial Port Operation
Transmitter Receiver
n 0 1 2 3 4 5 6 7 n
n+1 0 1 2 3 4 5 6 n+1 7
n+2 0 1 2 3 4 5 n+2 6 7
n+3 0 1 2 3 4 n+3 5 6 7
n+4 0 1 2 3 n+4 4 5 6 7
n+5 0 1 2 n+5 3 4 5 6 7
n+6 0 1 n+6 2 3 4 5 6 7
n+7 0 n+7 1 2 3 4 5 6 7
n+8 n+8 0 1 2 3 4 5 6 7
7
Serial Bus Examples
S/A Type Duplex #Device Speed Distance Wires
s (kbps) (ft)
RS232 A Peer Full 2 20 30 2+
11
Protocol
• Each character is sent as
– a logic low start bit
– a configurable number of data bits (usually 7 or
8, sometimes 5)
– an optional parity bit
– one or more logic high stop bits
– with a particular bit timing (“baud”)
12
UART Example
• Send the ASCII letter ‘W’ (1010111)
UART Hardware Connection
UART Character Reception
22
Serial Peripheral Interconnect (SPI)
• Another kind of serial protocol in embedded systems (proposed by
Motorola)
• Four-wire protocol
– SCLK — Serial Clock
– MOSI/SIMO — Master Output, Slave Input
– MISO/SOMI — Master Input, Slave Output
– SS — Slave Select
• Single master device and with one or more slave devices
• Higher throughput than I2C and can do “stream transfers”
• No arbitration required
• But
– Requires more pins
– Has no hardware flow control
– No slave acknowledgment (master could be talking to thin air and not even know it)
What is SPI?
• Synchronized
25
SPI Capabilities
• Always Full Duplex
– Communicating in two directions at the same time
– Transmission need not be meaningful
• Multiple slaves
– Daisy-chaining possible
26
SPI Protocol
• Wires:
– Master Out Slave In (MOSI)
– Master In Slave Out (MISO)
– System Clock (SCLK)
– Slave Select 1…N
Master shifts out data to Slave, and shifts in data from Slave
http://upload.wikimedia.org/wikipedia/commons/thumb/b/bb/SPI_8-bit_circular_transfer.svg/400px-SPI_8-bit_circular_transfer.svg.png
29
SPI Communication
30
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
31
• Master reconfigures clock mode on-the-fly!
SPI timing diagram
32
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
33
I2C bus in our projects
• Communication with the accelerometer
– Read from the accelerometer
• Pros
– Simple wire connection
– Two wires bus that can connect multiple
peripherals with the MCU
• Cons
– Complexity is significantly higher
How to operate the camera?
Accel
I2C register 1
MCU I2C register 2
….
Springs
https://www.youtube.com/watch?v=eqZgxR6eRjo
I2C Details
• Two lines
– Serial data line (SDA)
– Serial clock line (SCL)
SDA
SCL
Data line stable; Change
Data valid of data
allowed
38 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
39 of 40
I C Addressing
2
• 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
43
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”?
44