Reconfigurable computing
Small Embedded Systems
Unit 4.4
Synchronous Serial Communication: SPI
Introduction
Communication highways
Requirements for busses
Serial Peripheral Interface (SPI)
Communicating using the Arduino library functions
Suppose we have multiple devices to connect
We could connect all to all:
Requires a lot of wiring complexity and bulk
Requires a lot of pins on our devices
Alternatively there can be one shared communication highway: a bus
Busses
Device 1
Device 2
Device 3
Device 4
Device 1
Device 2
Device 3
Device 4
Challenges for Communication Busses
A bus connects multiple devices
Challenges:
How to maintain exact synchronization between devices?
Even a slight mismatch between the transmitter clock and receiver clock will result in messages being garbled
1
1
0
1
0
1
0
1
1
0
1
1
0
Transmitted message
1
0
1
0
1
0
1
1
1
1
1
0
Received message
Challenges for Communication Busses
A bus connects multiple devices
Challenges:
How to maintain exact synchronization between devices?
How tell difference between a valid bit (1 or 0) and a line idle signal when there is no data?
How does a device know if it is the intended recipient of the message?
Can multiple devices try to transmit at same time? The resulting messages would be completely garbled.
How do we detect message collisions?
How do we establish which sender should get priority?
Simple Synchronous Serial Comms
A simple solution starts with a shift register on each device
Parallel load/serial out
Data line connects devices’ shift registers
One device (the “master”) controls the clock and sends the clock signal to the other device (the “slave”) so both devices can synchronize
Transmitting device
Receiving device
Clock
1
0
1
1
0
1
1
1
Data
Simple Synchronous Serial Comms
On each clock cycle, one bit is sent
Transmitting device
Receiving device
1
0
1
1
0
1
1
1
Data
Clock
Simple Synchronous Serial Comms
On each clock cycle, one bit is sent
Transmitting device
Receiving device
1
0
1
1
0
1
1
Data
1
Clock
1
0
1
1
0
1
1
1
Simple Synchronous Serial Comms
Eventually…
Transmitting device
Receiving device
Data
Clock
Serial Peripheral Interface (SPI)
Shift registers form circular buffer: data can be transferred in either direction using two pins:
MISO – master out slave in
MOSI – master in slave out
SS – slave select – enables/disables the slave device
Master device
Slave device
SCK
MOSI
MISO
SS
Serial Peripheral Interface (SPI)
Only the master can initiate transfers
If master wants to pull data from slave, it sends a request (normally the ID of the slave register that it wants to read) and then activates the SPI clock SCK for enough cycles for slave device to complete transfer
Master device
Slave device
SCK
MOSI
MISO
SS
SPI with multiple peripherals
There can be only one SPI master (normally the microcontroller), but there can be multiple slaves
SS (slave select, active low) shows which slave should respond to transaction
This means master needs one extra pin for each peripheral
Diagram by Cburnett https://commons.wikimedia.org/w/index.php?curid=1476503
SPI with multiple peripherals
Alternatively, some types of peripheral can be daisy-chained
There is only one slave-select line, shared by all
All peripherals participate in all transactions
Data is scanned through all peripherals
Only a minority of peripherals support this mode
Diagram by Cburnett https://commons.wikimedia.org/w/index.php?curid=1476503
SPI Example using Arduino Library
Microcontroller sends a data byte called “value” to the address “address” on the peripheral device
#include
const int sensorPin = 10;
void setup() {
pinMode(sensorPin, OUTPUT);
SPI.begin();
}
void loop() {
digitalWrite(sensorPin, LOW);
delay(100);
SPI.transfer(address);
SPI.transfer(value);
delay(100);
digitalWrite(sensorPin, HIGH);
}
Send the address that we are writing
The pin used to select the sensor
Enable SPI
Select the device
Send the value that we are writing
Deselect the device
Why SPI might not always be a good idea…
SPI is good for high data, full duplex connections at up to 10MHz
SPI typically uses simple shift register at either end of the transmission
Nice and simple
But…
SPI bus requires
4 pins for one peripheral,
3+n pins for n peripherals,
Can we use fewer pins?
SPI allows one Master on the bus
In the next unit, we’ll look at I2C which achieves a multi-master bus and requires only 2 pins
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
/docProps/thumbnail.jpeg