Serial Peripheral Interface Bus SPI
Serial Peripheral Interface Bus SPI
SPI
SPI Bus
Existing peripheral busses (UART, I2C) offered inadequate bandwidth and required
overly complex control
Lots of IC’s use SPI: ADC, DAC, EEPROM, Flash, QSPI, FRAM, SD, sensors of all kinds,
RTCs, etc. A quick survey showed well more than 1000 low-cost devices
SPI
SPI uses unidirectional, synchronous signals: SDO, SDI, SCLK, and SS.
Every slave device requires it’s own Slave Select (SS) signal
SS SS
SPI SCLK SCLK SPI
Master MOSI MOSI Slave
MISO MISO
SPI
SPI uses unidirectional, synchronous signals: SDO, SDI, SCLK, and SS.
Every slave device requires it’s own Slave Select (SS) signal
SCLK
MOSI
Tri-State outputs
SPI
SCLK
MOSI
SPI with multiple slaves
Every slave device requires it’s own Slave Select (SS) signal
SS
SCLK SPI
MOSI Slave2
MISO
SPI data protocol…
… there is none.
Every SPI device can have a unique data protocol. Very flexible and powerful, but…
… there is none.
SPI devices must implement their own checks for overflow, timeouts, etc.
Synchronous Communications
SS (almost always active low) starts/defines a data frame. Eight or more clocks per
data frame move data through continuous shift register.
SS
SCLK
SCLK
MOSI
Synchronous Communications
The SPI spec allows for different clock frequencies and different clock polarities.
Imagine a stream of data. Which edge should the slave use to drive the bus? Which
edge should the master use to read the bus?
SS
CPOL = 0 SCLK
CPOL = 1 SCLK
SCLK
MOSI
SPI clock phase
Leading edge vs. next edge
“Inside” current transaction vs. “outside”
SS
CPOL = 0 SCLK
CPOL = 1 SCLK
SCLK
MOSI
SPI “Modes”
• Transmitter and receiver use same clock; a precision clock is not needed.
Period, phase and duty cycle can vary widely, provided the minimum
clock period requirement is met
• No address needed since SPI is point-to-point
• Signals are unidirectional so three state buffers and bus turn-around
circuits are not needed
• Any data size can be used (not just 8 bits)
• No arbitration or clock extraction is needed since the clock is included in
the bus
• No special transceivers are needed – SPI signals are simple logic-level
signals
SPI relative disadvantages
• Since the bus is point-to-point, more pins are required to interface with
more devices
• The SPI protocol does not include flow control, data acknowledge, or
error checking, so the master has no good way to know whether data was
accurately sent and received
• Only one master is defined, so the bus cannot be shared
QSPI: 4 data pins (everything else is the same) in 8-pin package (don’t
forget Vdd and GND!)
SS
SCLK
MISO (D0)
MOSI (D1)
WP (D2)
Reset (D3)
SD cards
Secure Digital cards: very small, power efficient, low-cost, popular
SD/SDIO physical pins are more or less QSPI, but protocol is unique
Default speed: 25MHz; High speed 50MHz
Spec is free!
UHS-1 and USH-2
Same pinout and form-factor as SD
Single-ended I/O at up to 104MHz
UHS-2
Adds more pins
Adds differential signaling at lower voltages (1.8V)
Adds DDR option
SPI on ZYNQ
Two independent SPI
controllers
FPGA
Memory-mapped peripheral using AXI bus
TXD and RXD path have 64-byte FIFOs From ARM To ARM
SPI FIFOs
0x0000 001C 0x0000 0020
128 Bytes
Transmit Receive
FIFO FIFO
Shift Registers
MOSI MISO
ZYNQ’s SPI Status Register
SCK = 166MHz / 2 n + 1
CPU_1xClk Divisor
SCK
(166 MHz) (Bits 5:3 in config. register)
Configuring the SPI controller
After unlocking, SPI system can be reset, and then it can be configured
Configuring the SPI controller
void SPI_reset() {
uint32_t register_value;
*((uint32_t *) MOD_RESET_BASEADDR+0x8/4) = 0x0000DF0D; // unlock the SLCRs
*((uint32_t *) MOD_RESET_BASEADDR + 0x0000021C/4) = 0xF; // Reset SPI0
*((uint32_t *) MOD_RESET_BASEADDR + 0x0000021C/4) = 0; // Release the reset
return;
}
Configuring ZYNQ’s SPI Controller
The LSM9DS1 uses two SS signals: one for ACC/Gyro, one for Mag (must drive
SS signals independently)
SCLK
MOSI
SS A/G
A/G SPI
MISO A/G
SS M
M SPI
MISO M
LSM9DS1
ZYNQ’s Inertial Module
The LSM9DS1 SPI interface can use an SCLK up to 10MHz
All transfers are 16-bits