Session 13 - SPI
Session 13 - SPI
SPI
Serial Peripheral Interface:
Specs of SPI:
The Serial Peripheral Interface (SPI) allows high-speed synchronous data transfer
between the ATmega32 and peripheral devices or between several AVR devices.
The ATmega32 SPI includes the following features:
Wired & Serial.
Throughput = 100 %.
TTL.
Serial Peripheral Interface:
Hardware Connection of SPI:
There are four pins for SPI connection,
every pin has a specific function, like:
- MOSI:
The pin is configured as output,
if the mode of Node is a Master,
the master uses it to write and send its data to Slave,
if the mode of Node is Slave, it must be configured as
input and the slave uses it to read the data which
master sends.
Serial Peripheral Interface:
Hardware Connection of SPI:
There are four pins for SPI connection,
every pin has a specific function, like:
- MISO:
The pin is configured as input,
if the mode of Node is a Master,
the master uses it to read the data which slave sends,
if the mode of Node is Slave, it must be configured as
output and the slave uses to write and
send its data to Master.
Serial Peripheral Interface:
Hardware Connection of SPI:
There are four pins for SPI connection,
every pin has a specific function, like:
- SCLK:
The pin is configured as output,
if the mode of Node is a Master, and it must be configured
as input, if the mode of Node is Slave.
the master slave uses it to provide its clock for
all nodes exist into the network to make all nodes work
with the same speed of master, so SPI protocol is
a synchronies protocol.
Serial Peripheral Interface:
Hardware Connection of SPI:
There are four pins for SPI connection,
every pin has a specific function, like:
- SS:
This pin is useless if the node is
configured as a master, and take care not to be
configured as an input and driven to low, because the
low signal will convert the master to slave “it will be
declared later”.
In slave mode, this pin must be driven to low by
master to indicate the salve must listen to master now,
so it is used to select which slave the master will communicate.
Serial Peripheral Interface:
Data Frame of SPI:
The transferring into SPI depends on exchanging data between
Master and slave, it means when the master generates its clock, the
master and slave at the same time will write their data, master will its
bit on MOSI, and the slave will its bit on MISO and so on until the data
of slave becomes into the master and the data of master becomes
into the slave, as the following figure, and if it does not work, use the
link below:
https://electrosome.com/wp-content/uploads/2017/04/SPI-Working-
Data-Transfer.gif
Serial Peripheral Interface:
Data Frame and Throughput of SPI:
CPOL = 1, CPHA = 0:
the leading edge is Falling edge and SPI will sample at it,
the trailing edge is Rising edge and SPI will setup at it.
Look at the figure exists in the next page:
Serial Peripheral Interface:
Register Description of SPI:
SPI Control Register “SPCR”:
Serial Peripheral Interface:
Register Description of SPI:
SPI Control Register “SPCR”:
According to the last two bits configuration, the leading edge
varies according to CPOL bit configuration, and the sampling at
first or setup at first varies according to the CPHA configuration,
so if:
CPOL = 0, CPHA = 1:
the leading edge is Rising edge and SPI will setup at it,
the trailing edge is Falling edge and SPI will sample at it.
CPOL = 1, CPHA = 1:
the leading edge is Falling edge and SPI will setup at it,
the trailing edge is Rising edge and SPI will sample at it.
Look at the figure exists in the next page:
Serial Peripheral Interface:
Register Description of SPI:
SPI Control Register “SPCR”:
Serial Peripheral Interface:
Register Description of SPI:
SPI Control Register “SPCR”:
Bits 1, 0 – SPR1, SPR0: SPI Clock Rate Select 1 and 0:
These two bits control the SCK rate of the
device configured as a Master.
SPR1 and SPR0 have no effect on the Slave.
SPI can be run at a double speed mode,
if the SPI2X bit exists into SPSR register is
set.
The relationship between SCK and the
Oscillator Clock frequency F_CPU is shown
in the following table:
Serial Peripheral Interface:
Register Description of SPI:
SPI Status Register “SPSR”:
Bit 7 – SPIF: SPI Interrupt Flag:
When a serial transfer is complete, the SPIF Flag is set.
An interrupt is generated if
SPIE in SPCR is set and global
interrupts are enabled.
If SS is an input and is driven low when the SPI is in Master
mode, this will also set the SPIF Flag.
SPIF is cleared by hardware when executing the corresponding
interrupt handling vector.
Alternatively, the SPIF bit is cleared by first reading the SPI
Status Register with SPIF set, then accessing the SPI Data
Register (SPDR).
Serial Peripheral Interface:
Register Description of SPI:
SPI Status Register “SPSR”:
Bit 6 – WCOL: Write Collision Flag:
The WCOL bit is set if the SPI Data Register (SPDR) is written
during a data transfer.
The WCOL bit (and the SPIF bit) are cleared by first reading the
SPI Status Register with WCOL set, and then accessing the SPI
Data Register.
At the previous example, master sends two bytes to inform the intended
slaves with data, so the throughput will be 50%.
If the master wants to send a byte to the sixth slave as example, master
must send this byte first then sends five dummy bytes,
so the throughput in this case will be “1/6” 16.67 % and so on.
Time To
Code
THANK YOU!