SlideShare a Scribd company logo
2
Most read
4
Most read
12
Most read
Serial Peripheral Interface (SPI)
SPI = Simple, 3 wire, full duplex, synchronous serial data transfer
Interfaces to many devices, even many non-SPI peripherals
Can be a master or slave interface
4 interface pins:
-MOSI master out slave in
-MISO master in slave out
-SCK serial clock
-SS_n slave select
3 registers:
-SPCR control register
-SPSR status register
-SPDR data register
Serial Peripheral Interface (SPI)
Full duplex, synchronous serial data transfer
Data is shifted out of the master's (mega128) MOSI pin and in it's MISO pin
Data transfer is initiated by simply writing data to the SPI data register.
All data movement is coordinated by SCK.
Slave select may or may not be used depending on interfacing device.
To get input data only you send “junk” data to SPDR to start the clock.
slave SPI devicemaster SPI device
Serial Peripheral Interface (SPI)
Slave Select... use it carefully!
In master mode:
-SPI interface has no control of SS_n
-User software has full control of SS_n (Port B, bit 0)
-If configured as output, it’s a general purpose output
-If configured as input, it must be held high, else you will enter slave mode
We will use SPI in master mode, full duplex
Serial Peripheral Interface (SPI)
SPI Control Register (SPCR)
data order: if set, LSB is
transmitted first
interrupt enable: if set, interrupt
occurs when SPI interrupt flag
and global interrupt enable are set
spi enable: if set, SPI interface
is enabled
master/slave select: if set,
SPI in master mode
clock polarity:
'0' SCK low in idle
'1' SCK high in idle
clock phase:
'0' leading edge sample, trailing edge setup
'1' leading edge setup, trailing edge sample
clock rate
SPI2X SPR1 SPR0 SCLK
0 0 0 fosc/4
0 0 1 fosc/16
0 1 0 fosc/64
0 1 1 fosc/128
1 0 0 fosc/2
1 0 1 fosc/8
1 1 0 fosc/32
1 1 1 fosc/64
(in SPSR)
Serial Peripheral Interface (SPI)
SPI Status Register (SPSR)
interrupt flag: set when serial
transfer is complete
write collision: set if SPDR is
written during a receive transfer
2x clock rate: if set, doubles
clock rate in master mode
reserved bits
SPI Data Register (SPDR)
SPDR is a read/write register used for data transfer. Writing SPDR sends data
out MOSI. Reading SPDR gets the data that was clocked into MISO.
Serial Peripheral Interface (SPI)
Mega128 LCD interface
SCK, PB1
LCD strobe, PF3
MOSI, PB2
enable pulse generator
9-bit shift register
Serial Peripheral Interface (SPI)
SPI Application - Code
/*********************************************************************/
// spi_init
//Initializes the SPI port on the mega128. Does not do any further
//external device specific initializations.
/*********************************************************************/
void spi_init(void){
DDRB = 0x07; //Turn on SS, MOSI, SCLK (SS is output)
SPCR = (1<<SPE) | (1<<MSTR); //SPI enabled, master, low polarity, MSB 1st
SPSR = (1<<SPI2X); //run at i/o clock/2
}//spi_init
/***********************************************************************/
// digi_pot_send
//Sends command and data to the digital pot. SPI device chip select is
//active low and is connected to port F bit 2. Total of 16 bits are sent.
//One byte for control and one byte as data passed in.
/***********************************************************************/
void digi_pot_send(uint8_t data){
PORTF &= 0xFB; //port F bit 2, assert active low
SPDR = 0x13; //send command byte (fixed value)
while (bit_is_clear(SPSR,SPIF)) {} //wait till data is sent out
SPDR = data; //send data byte
while (bit_is_clear(SPSR,SPIF)) {} //wait till data is sent out
PORTF |= 0x04; //port F bit 2, deassert to logic high
} //digi_pot_send
Serial Peripheral Interface (SPI)
Typical SPI IC (MCP42010)
Serial Peripheral Interface (SPI)
74HC595 – A perfectly fine SPI peripheral
Serial Peripheral Interface (SPI)
What if you want only to read the SPI port?
To get the SPI clock to run, a "dummy" write is made to the SPI
SPDR register. This starts the clock running so the data on MISO
is brought into the uC.
If no peripherals are selected, the outgoing data will be ignored. If you
are clever, you can send data out and bring data in at the same time.
/*********************************************************************/
// spi_read
//Reads the SPI port.
/*********************************************************************/
uint8_t spi_read(void){
SPDR = 0x00; //"dummy" write to SPDR
while (bit_is_clear(SPSR,SPIF)){} //wait till 8 clock cycles are done
return(SPDR); //return incoming data from SPDR
}//read_spi
Serial Peripheral Interface (SPI)
74HC165 – Another fine SPI peripheral
Serial Peripheral Interface (SPI)
SPI “Gotchas”
“Now my board won’t program.”
SPI shares SCK with programming interface. If it won’t program anymore,
you likely messed up SCK.
“SPI acts totally wierd.”
Often a symptom of SS_n being configured as an input and being left to float
or allowed to go high. SPI goes in and out between slave and master modes.
“I never get data to the SPI device.”
Is clock correctly oriented ? Did you assert the device chip select?
(hint: put SPI write inside a “tight” loop and check with scope. Watch
SCK, data, and chip select)
"SPI device interactions:" When programming, the programmer first does a chip
reset. When the mega128 resets, all pins are set to input with high impedance
(floating). If a SPI device is on the SPI bus, its chip-select may
float low and enable the device, and SPI data will crash the programming data.
Adding a pull-up resistor to chip selects will solve this problem.

More Related Content

What's hot (20)

PDF
Serial Peripheral Interface
Chirag Parikh
 
PPT
Serial Peripheral Interface(SPI)
Dhaval Kaneria
 
PPT
SPI Bus Protocol
Sudhanshu Janwadkar
 
PDF
Pcie basic
Saifuddin Kaijar
 
PDF
Verification Strategy for PCI-Express
DVClub
 
PPTX
Serial peripheral Interface - Embedded System Protocol
Aditya Porwal
 
ODP
APB protocol v1.0
Azad Mishra
 
PDF
The I2C Interface
Corrado Santoro
 
PDF
USB protocol
Mostafa El-koumy
 
PPTX
Axi protocol
Rohit Kumar Pathak
 
PPT
I2C Protocol
Anurag Tomar
 
PPT
Axi protocol
Azad Mishra
 
PPTX
PCIe
ChiaYang Tsai
 
PPTX
I2c protocol - Inter–Integrated Circuit Communication Protocol
Ankur Soni
 
PPTX
PCI express
sarangaprabod
 
PPTX
Uart
Aditee Apurvaa
 
PPT
Wishbone interface and bus cycles
dennis gookyi
 
PDF
Jtag presentation
klinetik
 
PPTX
I2C Protocol
Sudhanshu Janwadkar
 
Serial Peripheral Interface
Chirag Parikh
 
Serial Peripheral Interface(SPI)
Dhaval Kaneria
 
SPI Bus Protocol
Sudhanshu Janwadkar
 
Pcie basic
Saifuddin Kaijar
 
Verification Strategy for PCI-Express
DVClub
 
Serial peripheral Interface - Embedded System Protocol
Aditya Porwal
 
APB protocol v1.0
Azad Mishra
 
The I2C Interface
Corrado Santoro
 
USB protocol
Mostafa El-koumy
 
Axi protocol
Rohit Kumar Pathak
 
I2C Protocol
Anurag Tomar
 
Axi protocol
Azad Mishra
 
I2c protocol - Inter–Integrated Circuit Communication Protocol
Ankur Soni
 
PCI express
sarangaprabod
 
Wishbone interface and bus cycles
dennis gookyi
 
Jtag presentation
klinetik
 
I2C Protocol
Sudhanshu Janwadkar
 

Similar to SPI Protocol (20)

PPTX
LPC2148 SPI (Serial Peripheral Interface).pptx
dhanashribiradar2
 
PPTX
Spi in arm7(lpc2148)
Aarav Soni
 
PPT
Master synchronous serial port (mssp)
babak danyal
 
PPTX
Designing of fifo and serial peripheral interface protocol using Verilog HDL
Jay Baxi
 
PPTX
Deepu Kumar Shah.pptx
DeepuShah
 
PPTX
UNI T 6- SPI_I2C_Lecture8.pptx
naveen088888
 
PDF
SPI Protocol in LPC2148
Dnyanesh P. Joshi
 
DOCX
D-7-17 Interface an 8-bit serial device using SPI- Thecontrol pin is i.docx
earleanp
 
PDF
Serial Peripheral Interface Design for Advanced Microcontroller Bus Architect...
IJMTST Journal
 
PPTX
spi-180501092933-converted.pptx
sauryakumar3
 
PPTX
Serial Communication - Serial Peripheral Interface
arksramesh
 
PPTX
Serial peripheral interface
Abhijeet kapse
 
PPTX
Serial Peripheral Interface (SPI).pptx
PraisePedzai
 
PDF
AREA OPTIMIZATION OF SPI MODULE USING VERILOG HDL
IAEME Publication
 
PDF
SPI Design Report used in serial communication
sakethvarma239
 
PDF
Cd36479483
IJERA Editor
 
PDF
Microcontroller part 8_v1
Keroles karam khalil
 
PDF
What's going on with SPI
Mark Brown
 
PPTX
Serial connectors, Protocols , USB (universal serial bus)
A. Shamel
 
LPC2148 SPI (Serial Peripheral Interface).pptx
dhanashribiradar2
 
Spi in arm7(lpc2148)
Aarav Soni
 
Master synchronous serial port (mssp)
babak danyal
 
Designing of fifo and serial peripheral interface protocol using Verilog HDL
Jay Baxi
 
Deepu Kumar Shah.pptx
DeepuShah
 
UNI T 6- SPI_I2C_Lecture8.pptx
naveen088888
 
SPI Protocol in LPC2148
Dnyanesh P. Joshi
 
D-7-17 Interface an 8-bit serial device using SPI- Thecontrol pin is i.docx
earleanp
 
Serial Peripheral Interface Design for Advanced Microcontroller Bus Architect...
IJMTST Journal
 
spi-180501092933-converted.pptx
sauryakumar3
 
Serial Communication - Serial Peripheral Interface
arksramesh
 
Serial peripheral interface
Abhijeet kapse
 
Serial Peripheral Interface (SPI).pptx
PraisePedzai
 
AREA OPTIMIZATION OF SPI MODULE USING VERILOG HDL
IAEME Publication
 
SPI Design Report used in serial communication
sakethvarma239
 
Cd36479483
IJERA Editor
 
Microcontroller part 8_v1
Keroles karam khalil
 
What's going on with SPI
Mark Brown
 
Serial connectors, Protocols , USB (universal serial bus)
A. Shamel
 
Ad

Recently uploaded (20)

PPTX
Farrell_Programming Logic and Design slides_10e_ch02_PowerPoint.pptx
bashnahara11
 
PPTX
The Future of AI & Machine Learning.pptx
pritsen4700
 
PPTX
Simple and concise overview about Quantum computing..pptx
mughal641
 
PPTX
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
PDF
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
PPTX
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
PPTX
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
 
PPTX
AI Code Generation Risks (Ramkumar Dilli, CIO, Myridius)
Priyanka Aash
 
PDF
TrustArc Webinar - Navigating Data Privacy in LATAM: Laws, Trends, and Compli...
TrustArc
 
PDF
Build with AI and GDG Cloud Bydgoszcz- ADK .pdf
jaroslawgajewski1
 
PDF
Market Insight : ETH Dominance Returns
CIFDAQ
 
PPTX
Agile Chennai 18-19 July 2025 | Workshop - Enhancing Agile Collaboration with...
AgileNetwork
 
PPTX
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
PDF
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
PDF
The Future of Mobile Is Context-Aware—Are You Ready?
iProgrammer Solutions Private Limited
 
PDF
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
PDF
Brief History of Internet - Early Days of Internet
sutharharshit158
 
PDF
Generative AI vs Predictive AI-The Ultimate Comparison Guide
Lily Clark
 
PDF
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
PDF
State-Dependent Conformal Perception Bounds for Neuro-Symbolic Verification
Ivan Ruchkin
 
Farrell_Programming Logic and Design slides_10e_ch02_PowerPoint.pptx
bashnahara11
 
The Future of AI & Machine Learning.pptx
pritsen4700
 
Simple and concise overview about Quantum computing..pptx
mughal641
 
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
 
AI Code Generation Risks (Ramkumar Dilli, CIO, Myridius)
Priyanka Aash
 
TrustArc Webinar - Navigating Data Privacy in LATAM: Laws, Trends, and Compli...
TrustArc
 
Build with AI and GDG Cloud Bydgoszcz- ADK .pdf
jaroslawgajewski1
 
Market Insight : ETH Dominance Returns
CIFDAQ
 
Agile Chennai 18-19 July 2025 | Workshop - Enhancing Agile Collaboration with...
AgileNetwork
 
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
The Future of Mobile Is Context-Aware—Are You Ready?
iProgrammer Solutions Private Limited
 
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
Brief History of Internet - Early Days of Internet
sutharharshit158
 
Generative AI vs Predictive AI-The Ultimate Comparison Guide
Lily Clark
 
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
State-Dependent Conformal Perception Bounds for Neuro-Symbolic Verification
Ivan Ruchkin
 
Ad

SPI Protocol

  • 1. Serial Peripheral Interface (SPI) SPI = Simple, 3 wire, full duplex, synchronous serial data transfer Interfaces to many devices, even many non-SPI peripherals Can be a master or slave interface 4 interface pins: -MOSI master out slave in -MISO master in slave out -SCK serial clock -SS_n slave select 3 registers: -SPCR control register -SPSR status register -SPDR data register
  • 2. Serial Peripheral Interface (SPI) Full duplex, synchronous serial data transfer Data is shifted out of the master's (mega128) MOSI pin and in it's MISO pin Data transfer is initiated by simply writing data to the SPI data register. All data movement is coordinated by SCK. Slave select may or may not be used depending on interfacing device. To get input data only you send “junk” data to SPDR to start the clock. slave SPI devicemaster SPI device
  • 3. Serial Peripheral Interface (SPI) Slave Select... use it carefully! In master mode: -SPI interface has no control of SS_n -User software has full control of SS_n (Port B, bit 0) -If configured as output, it’s a general purpose output -If configured as input, it must be held high, else you will enter slave mode We will use SPI in master mode, full duplex
  • 4. Serial Peripheral Interface (SPI) SPI Control Register (SPCR) data order: if set, LSB is transmitted first interrupt enable: if set, interrupt occurs when SPI interrupt flag and global interrupt enable are set spi enable: if set, SPI interface is enabled master/slave select: if set, SPI in master mode clock polarity: '0' SCK low in idle '1' SCK high in idle clock phase: '0' leading edge sample, trailing edge setup '1' leading edge setup, trailing edge sample clock rate SPI2X SPR1 SPR0 SCLK 0 0 0 fosc/4 0 0 1 fosc/16 0 1 0 fosc/64 0 1 1 fosc/128 1 0 0 fosc/2 1 0 1 fosc/8 1 1 0 fosc/32 1 1 1 fosc/64 (in SPSR)
  • 5. Serial Peripheral Interface (SPI) SPI Status Register (SPSR) interrupt flag: set when serial transfer is complete write collision: set if SPDR is written during a receive transfer 2x clock rate: if set, doubles clock rate in master mode reserved bits SPI Data Register (SPDR) SPDR is a read/write register used for data transfer. Writing SPDR sends data out MOSI. Reading SPDR gets the data that was clocked into MISO.
  • 6. Serial Peripheral Interface (SPI) Mega128 LCD interface SCK, PB1 LCD strobe, PF3 MOSI, PB2 enable pulse generator 9-bit shift register
  • 7. Serial Peripheral Interface (SPI) SPI Application - Code /*********************************************************************/ // spi_init //Initializes the SPI port on the mega128. Does not do any further //external device specific initializations. /*********************************************************************/ void spi_init(void){ DDRB = 0x07; //Turn on SS, MOSI, SCLK (SS is output) SPCR = (1<<SPE) | (1<<MSTR); //SPI enabled, master, low polarity, MSB 1st SPSR = (1<<SPI2X); //run at i/o clock/2 }//spi_init /***********************************************************************/ // digi_pot_send //Sends command and data to the digital pot. SPI device chip select is //active low and is connected to port F bit 2. Total of 16 bits are sent. //One byte for control and one byte as data passed in. /***********************************************************************/ void digi_pot_send(uint8_t data){ PORTF &= 0xFB; //port F bit 2, assert active low SPDR = 0x13; //send command byte (fixed value) while (bit_is_clear(SPSR,SPIF)) {} //wait till data is sent out SPDR = data; //send data byte while (bit_is_clear(SPSR,SPIF)) {} //wait till data is sent out PORTF |= 0x04; //port F bit 2, deassert to logic high } //digi_pot_send
  • 8. Serial Peripheral Interface (SPI) Typical SPI IC (MCP42010)
  • 9. Serial Peripheral Interface (SPI) 74HC595 – A perfectly fine SPI peripheral
  • 10. Serial Peripheral Interface (SPI) What if you want only to read the SPI port? To get the SPI clock to run, a "dummy" write is made to the SPI SPDR register. This starts the clock running so the data on MISO is brought into the uC. If no peripherals are selected, the outgoing data will be ignored. If you are clever, you can send data out and bring data in at the same time. /*********************************************************************/ // spi_read //Reads the SPI port. /*********************************************************************/ uint8_t spi_read(void){ SPDR = 0x00; //"dummy" write to SPDR while (bit_is_clear(SPSR,SPIF)){} //wait till 8 clock cycles are done return(SPDR); //return incoming data from SPDR }//read_spi
  • 11. Serial Peripheral Interface (SPI) 74HC165 – Another fine SPI peripheral
  • 12. Serial Peripheral Interface (SPI) SPI “Gotchas” “Now my board won’t program.” SPI shares SCK with programming interface. If it won’t program anymore, you likely messed up SCK. “SPI acts totally wierd.” Often a symptom of SS_n being configured as an input and being left to float or allowed to go high. SPI goes in and out between slave and master modes. “I never get data to the SPI device.” Is clock correctly oriented ? Did you assert the device chip select? (hint: put SPI write inside a “tight” loop and check with scope. Watch SCK, data, and chip select) "SPI device interactions:" When programming, the programmer first does a chip reset. When the mega128 resets, all pins are set to input with high impedance (floating). If a SPI device is on the SPI bus, its chip-select may float low and enable the device, and SPI data will crash the programming data. Adding a pull-up resistor to chip selects will solve this problem.