SlideShare a Scribd company logo
1 of 12
Download to read offline
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)

Slow peripheral interfaces (i2 c spi uart)
Slow peripheral interfaces (i2 c  spi uart)Slow peripheral interfaces (i2 c  spi uart)
Slow peripheral interfaces (i2 c spi uart)
 
I2C Protocol
I2C ProtocolI2C Protocol
I2C Protocol
 
I2C Protocol
I2C ProtocolI2C Protocol
I2C Protocol
 
I2c protocol - Inter–Integrated Circuit Communication Protocol
I2c protocol - Inter–Integrated Circuit Communication ProtocolI2c protocol - Inter–Integrated Circuit Communication Protocol
I2c protocol - Inter–Integrated Circuit Communication Protocol
 
I2C
I2CI2C
I2C
 
I2C
I2CI2C
I2C
 
I2C introduction
I2C introductionI2C introduction
I2C introduction
 
Spi master core verification
Spi master core verificationSpi master core verification
Spi master core verification
 
I2 c protocol
I2 c protocolI2 c protocol
I2 c protocol
 
Serial peripheral Interface - Embedded System Protocol
Serial peripheral Interface - Embedded System ProtocolSerial peripheral Interface - Embedded System Protocol
Serial peripheral Interface - Embedded System Protocol
 
Uart
UartUart
Uart
 
I2C BUS PROTOCOL
I2C BUS PROTOCOLI2C BUS PROTOCOL
I2C BUS PROTOCOL
 
Pcie basic
Pcie basicPcie basic
Pcie basic
 
PCIe
PCIePCIe
PCIe
 
SPI Protocol in LPC2148
SPI  Protocol in LPC2148SPI  Protocol in LPC2148
SPI Protocol in LPC2148
 
dual-port RAM (DPRAM)
dual-port RAM (DPRAM)dual-port RAM (DPRAM)
dual-port RAM (DPRAM)
 
Pc ie tl_layer (3)
Pc ie tl_layer (3)Pc ie tl_layer (3)
Pc ie tl_layer (3)
 
PCI express
PCI expressPCI express
PCI express
 
UART
UARTUART
UART
 
Slideshare - PCIe
Slideshare - PCIeSlideshare - PCIe
Slideshare - PCIe
 

Similar to SPI Protocol

Spi in arm7(lpc2148)
Spi in arm7(lpc2148)Spi in arm7(lpc2148)
Spi in arm7(lpc2148)Aarav Soni
 
UNI T 6- SPI_I2C_Lecture8.pptx
UNI                    T 6- SPI_I2C_Lecture8.pptxUNI                    T 6- SPI_I2C_Lecture8.pptx
UNI T 6- SPI_I2C_Lecture8.pptxnaveen088888
 
D-7-17 Interface an 8-bit serial device using SPI- Thecontrol pin is i.docx
D-7-17 Interface an 8-bit serial device using SPI- Thecontrol pin is i.docxD-7-17 Interface an 8-bit serial device using SPI- Thecontrol pin is i.docx
D-7-17 Interface an 8-bit serial device using SPI- Thecontrol pin is i.docxearleanp
 
Datasheet 89S8253.pdf
Datasheet 89S8253.pdfDatasheet 89S8253.pdf
Datasheet 89S8253.pdfEFran9
 
communication interfaces-Embedded real time systems
communication interfaces-Embedded real time systemscommunication interfaces-Embedded real time systems
communication interfaces-Embedded real time systemsRaghunath reddy
 
What's going on with SPI
What's going on with SPI What's going on with SPI
What's going on with SPI Mark Brown
 
Designing of fifo and serial peripheral interface protocol using Verilog HDL
Designing of fifo and serial peripheral interface protocol using Verilog HDLDesigning of fifo and serial peripheral interface protocol using Verilog HDL
Designing of fifo and serial peripheral interface protocol using Verilog HDLJay Baxi
 
Welcome to International Journal of Engineering Research and Development (IJERD)
Welcome to International Journal of Engineering Research and Development (IJERD)Welcome to International Journal of Engineering Research and Development (IJERD)
Welcome to International Journal of Engineering Research and Development (IJERD)IJERD Editor
 
spi-180501092933-converted.pptx
spi-180501092933-converted.pptxspi-180501092933-converted.pptx
spi-180501092933-converted.pptxsauryakumar3
 
Part-2: Mastering microcontroller with embedded driver development
Part-2: Mastering microcontroller with embedded driver developmentPart-2: Mastering microcontroller with embedded driver development
Part-2: Mastering microcontroller with embedded driver developmentFastBit Embedded Brain Academy
 
Cisco router basic
Cisco router basicCisco router basic
Cisco router basicTapan Khilar
 
Chapter 2-8085 Microprocessor Architecture and Microcomputer Systems
Chapter 2-8085 Microprocessor Architecture and Microcomputer SystemsChapter 2-8085 Microprocessor Architecture and Microcomputer Systems
Chapter 2-8085 Microprocessor Architecture and Microcomputer Systemscmkandemir
 
Introduction to 8085 Microprocessor
Introduction to 8085 MicroprocessorIntroduction to 8085 Microprocessor
Introduction to 8085 MicroprocessorRavi Anand
 
SOC Peripheral Components & SOC Tools
SOC Peripheral Components & SOC ToolsSOC Peripheral Components & SOC Tools
SOC Peripheral Components & SOC ToolsA B Shinde
 
At 89s52
At 89s52At 89s52
At 89s52Mr Giap
 

Similar to SPI Protocol (20)

Spi in arm7(lpc2148)
Spi in arm7(lpc2148)Spi in arm7(lpc2148)
Spi in arm7(lpc2148)
 
UNI T 6- SPI_I2C_Lecture8.pptx
UNI                    T 6- SPI_I2C_Lecture8.pptxUNI                    T 6- SPI_I2C_Lecture8.pptx
UNI T 6- SPI_I2C_Lecture8.pptx
 
D-7-17 Interface an 8-bit serial device using SPI- Thecontrol pin is i.docx
D-7-17 Interface an 8-bit serial device using SPI- Thecontrol pin is i.docxD-7-17 Interface an 8-bit serial device using SPI- Thecontrol pin is i.docx
D-7-17 Interface an 8-bit serial device using SPI- Thecontrol pin is i.docx
 
Datasheet 89S8253.pdf
Datasheet 89S8253.pdfDatasheet 89S8253.pdf
Datasheet 89S8253.pdf
 
communication interfaces-Embedded real time systems
communication interfaces-Embedded real time systemscommunication interfaces-Embedded real time systems
communication interfaces-Embedded real time systems
 
What's going on with SPI
What's going on with SPI What's going on with SPI
What's going on with SPI
 
Assembler4
Assembler4Assembler4
Assembler4
 
Designing of fifo and serial peripheral interface protocol using Verilog HDL
Designing of fifo and serial peripheral interface protocol using Verilog HDLDesigning of fifo and serial peripheral interface protocol using Verilog HDL
Designing of fifo and serial peripheral interface protocol using Verilog HDL
 
10_2Starting with serial_II.ppt
10_2Starting with serial_II.ppt10_2Starting with serial_II.ppt
10_2Starting with serial_II.ppt
 
Welcome to International Journal of Engineering Research and Development (IJERD)
Welcome to International Journal of Engineering Research and Development (IJERD)Welcome to International Journal of Engineering Research and Development (IJERD)
Welcome to International Journal of Engineering Research and Development (IJERD)
 
At89 s8252
At89 s8252At89 s8252
At89 s8252
 
Introduction to embedded systems
Introduction to embedded systemsIntroduction to embedded systems
Introduction to embedded systems
 
spi-180501092933-converted.pptx
spi-180501092933-converted.pptxspi-180501092933-converted.pptx
spi-180501092933-converted.pptx
 
Part-2: Mastering microcontroller with embedded driver development
Part-2: Mastering microcontroller with embedded driver developmentPart-2: Mastering microcontroller with embedded driver development
Part-2: Mastering microcontroller with embedded driver development
 
Cisco router basic
Cisco router basicCisco router basic
Cisco router basic
 
89s52 2
89s52 289s52 2
89s52 2
 
Chapter 2-8085 Microprocessor Architecture and Microcomputer Systems
Chapter 2-8085 Microprocessor Architecture and Microcomputer SystemsChapter 2-8085 Microprocessor Architecture and Microcomputer Systems
Chapter 2-8085 Microprocessor Architecture and Microcomputer Systems
 
Introduction to 8085 Microprocessor
Introduction to 8085 MicroprocessorIntroduction to 8085 Microprocessor
Introduction to 8085 Microprocessor
 
SOC Peripheral Components & SOC Tools
SOC Peripheral Components & SOC ToolsSOC Peripheral Components & SOC Tools
SOC Peripheral Components & SOC Tools
 
At 89s52
At 89s52At 89s52
At 89s52
 

Recently uploaded

Simplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptxSimplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptxMarkSteadman7
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
API Governance and Monetization - The evolution of API governance
API Governance and Monetization -  The evolution of API governanceAPI Governance and Monetization -  The evolution of API governance
API Governance and Monetization - The evolution of API governanceWSO2
 
JohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard37
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
Navigating Identity and Access Management in the Modern Enterprise
Navigating Identity and Access Management in the Modern EnterpriseNavigating Identity and Access Management in the Modern Enterprise
Navigating Identity and Access Management in the Modern EnterpriseWSO2
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamUiPathCommunity
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdfSandro Moreira
 
Quantum Leap in Next-Generation Computing
Quantum Leap in Next-Generation ComputingQuantum Leap in Next-Generation Computing
Quantum Leap in Next-Generation ComputingWSO2
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Orbitshub
 
Modernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using BallerinaModernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using BallerinaWSO2
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxRemote DBA Services
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistandanishmna97
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 

Recently uploaded (20)

Simplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptxSimplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptx
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
API Governance and Monetization - The evolution of API governance
API Governance and Monetization -  The evolution of API governanceAPI Governance and Monetization -  The evolution of API governance
API Governance and Monetization - The evolution of API governance
 
JohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptx
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Navigating Identity and Access Management in the Modern Enterprise
Navigating Identity and Access Management in the Modern EnterpriseNavigating Identity and Access Management in the Modern Enterprise
Navigating Identity and Access Management in the Modern Enterprise
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Quantum Leap in Next-Generation Computing
Quantum Leap in Next-Generation ComputingQuantum Leap in Next-Generation Computing
Quantum Leap in Next-Generation Computing
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Modernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using BallerinaModernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using Ballerina
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 

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.