3.13.1 SPI - Serial Peripheral Interface
3.13.1.1 Introduction
The Serial Peripheral Interface (SPI) module is a serial interface useful for communicating with other peripheral or microcontroller devices. These peripheral devices may be serial EEPROMs, shift registers, display drivers, Analog-to-Digital (A/D) converters, etc.
3.13.1.2 Supported Device Families
AVR® Dx | AVR® Ex | ATtiny | ATmega |
3.13.1.3 Required header files:
#include "mcc_generated_files/spi/spi[X].h"
3.13.1.4 How to use the SPI Driver
The SPI driver generates a portable API interface. For an example of how to use the interface to write and read data, refer to this examples linked below. Click the links to view the code snippets associated with each
Instructions common for all code snippets: SPI Example Snippet Instructions
- SPI Example 1: SPI Host Loopback When in Host mode, the SPI generates the SCK clock. Data is driven out on the MOSI I/O pin according to the configured phase and polarity.
- SPI Example 2: SPI ClientHere the Host sends bytes to the Micrcontroller running this code, acting as a client. When in Client mode, the SPI receives the SCK clock from the Host and uses this to sample data on the MOSI I/O pin. The phase and polarity must be configured according to the values used by the Host.
3.13.1.5 Module Documentation
3.13.1.5.1 SPI0
This header file provides API prototypes for the SPI0 driver.
Module description
This header file provides API prototypes for the SPI0 driver.
Data structures
struct SPI_INTERFACE
SPI driver interface object.
Enumerations
enum SPI0_configuration_name_t { SPI0_DEFAULT, SPI0_DEFAULT }
Enumeration for the different configurations supported by the driver. A configuration is specified as parameter to SPI0_Open() and is used by the function to set SPI parameters as specified by the configuration.
Functions
void SPI0_Initialize (void)
Initializes the SPI0 module.
void SPI0_Deinitialize (void)
Deinitializes the SPI0 module.
bool SPI0_Open (uint8_t spiConfigIndex)
Enables the SPI0 module with the configurations passed as the parameter. See spi0_configuration_name_t for the available configurations.
void SPI0_Close (void)
Closes the active configuration of the SPI0 module.
uint8_t SPI0_ByteExchange (uint8_t byteData)
Exchanges one byte using the SPI protocol. This function is blocking.
void SPI0_BufferExchange (void *bufferData, size_t bufferSize)
Exchanges the buffer using the SPI protocol. This function is blocking in Polling mode.
void SPI0_BufferWrite (void *bufferData, size_t bufferSize)
Writes a buffer using the SPI protocol. This function is blocking in Polling mode.
void SPI0_BufferRead (void *bufferData, size_t bufferSize)
Reads a buffer using the SPI protocol. This function is blocking in Polling mode.
void SPI0_ByteWrite (uint8_t byteData)
Writes one byte using the SPI protocol. This function is blocking in Polling mode.
uint8_t SPI0_ByteRead (void)
Receives one byte using SPI protocol. This function is blocking.
bool SPI0_IsRxReady (void)
Checks if the SPI0 module is ready to read data.
bool SPI0_IsTxReady (void)
Checks if the SPI0 module is ready to write data.
void SPI0_RxCompleteCallbackRegister (void(*callbackHandler)(void))
Sets the callback function to be executed at the completion of data transfer in Interrupt mode.
Function Documentation
SPI0_BufferExchange()
void SPI0_BufferExchange (void * bufferData, size_t bufferSize)
Exchanges the buffer using the SPI protocol. This function is blocking in Polling mode.
in,out | *bufferData |
Buffer address of the data to be exchanged |
in | bufferSize |
Size of the data in bytes |
None. |
SPI0_BufferRead()
void SPI0_BufferRead (void * bufferData, size_t bufferSize)
Reads a buffer using the SPI protocol. This function is blocking in Polling mode.
out | *bufferData |
Buffer address of the data to be read |
in | bufferSize |
Size of the data in bytes |
None. |
SPI0_BufferWrite()
void SPI0_BufferWrite (void * bufferData, size_t bufferSize)
Writes a buffer using the SPI protocol. This function is blocking in Polling mode.
in | *bufferData |
Buffer address of the data to be written |
in | bufferSize |
Size of the data in bytes |
None. |
SPI0_ByteExchange()
uint8_t SPI0_ByteExchange (uint8_t byteData)
Exchanges one byte using the SPI protocol. This function is blocking.
byteData |
The byte to be written |
Received data byte |
SPI0_ByteRead()
uint8_t SPI0_ByteRead (void )
Receives one byte using SPI protocol. This function is blocking.
None. |
Received data byte |
SPI0_ByteWrite()
void SPI0_ByteWrite (uint8_t byteData)
Writes one byte using the SPI protocol. This function is blocking in Polling mode.
byteData |
The byte to be written |
None. |
SPI0_Close()
void SPI0_Close (void )
Closes the active configuration of the SPI0 module.
None. |
None. |
SPI0_Deinitialize()
void SPI0_Deinitialize (void )
Deinitializes the SPI0 module.
None. |
None. |
SPI0_Initialize()
void SPI0_Initialize (void )
Initializes the SPI0 module.
None. |
None. |
SPI0_IsRxReady()
bool SPI0_IsRxReady (void )
Checks if the SPI0 module is ready to read data.
None. |
True |
SPI0 module has data ready in buffer |
False |
SPI0 module is not ready to read data |
SPI0_IsTxReady()
bool SPI0_IsTxReady (void )
Checks if the SPI0 module is ready to write data.
None. |
True |
SPI0 module is ready to write data |
False |
SPI0 module is not ready to write data |
SPI0_Open()
bool SPI0_Open (uint8_t spiConfigIndex)
Enables the SPI0 module with the configurations passed as the parameter. See spi0_configuration_name_t for the available configurations.
spiConfigIndex |
The Configuration index |
True |
SPI0 module is enabled successfully with the chosen configuration |
False |
SPI0 module is already open with another configuration |
SPI0_RxCompleteCallbackRegister()
void SPI0_RxCompleteCallbackRegister (void(*)(void) callbackHandler)
Sets the callback function to be executed at the completion of data transfer in Interrupt mode.
Pointer |
to the function to be executed |
None. |
Enumeration Type Documentation
SPI0_configuration_name_t
enum enum SPI0_configuration_name_t
Enumeration for the different configurations supported by the driver. A configuration is specified as parameter to SPI0_Open() and is used by the function to set SPI parameters as specified by the configuration.
SPI0_DEFAULT | |
SPI0_DEFAULT |
3.13.1.5.2 SPI
This header file provides API prototypes for the SPI module in Polling and Interrupt mode.
Module description
This header file provides API prototypes for the SPI module in Polling and Interrupt mode.
This header file provides type definitions for the SPI module operation in Polling mode.
This header file provides type definitions for the SPI module operation in Interrupt mode.
Data structures
struct SPI_INTERFACE
SPI driver interface object.
Enumerations
enum spi_status_t { SPI_RESET = 0x00U, SPI_IDLE = 0x81U, SPI_BUSY = 0x42U }
Enumeration for the different states of the SPI module.
enum spi_transfer_type_t { SPI_READ = 0x00U, SPI_WRITE = 0x81U, SPI_EXCHANGE = 0X42U }
Enumeration for the different transfer types of the SPI module.
Enumeration Type Documentation
spi_status_t
enum enum spi_status_t
Enumeration for the different states of the SPI module.
The enum values are chosen with a specific bit pattern to make sure no error happens in case a single bit changes.
SPI_RESET |
SPI module is in Reset state |
SPI_IDLE |
SPI module is ready to use |
SPI_BUSY |
SPI module is in use |
spi_transfer_type_t
enum enum spi_transfer_type_t
Enumeration for the different transfer types of the SPI module.
The enum values are chosen with a specific bit pattern to make sure no error happens in case a single bit changes.
SPI_READ |
SPI Module is performing a read operation |
SPI_WRITE |
SPI Module is performing a write operation |
SPI_EXCHANGE |
SPI Module is performing a exchange operation |
3.13.1.5.3 SPI_example
SPI Example Snippet Instructions
Check MISO/MOSI/SCK pin configurations in Pin Manager (LED pin where applicable)
Add SPIxPLIB to the project
SPIxPLIB as described
Generate the code and program the board
Add the code snippet to the application code (adding required headers and call the example snippet)
The SPI must be opened by calling <component_name>_open() before it can be used. After use, the SPI is closed by calling <component_name>_close(). A call to <component_name>_open() for a SPI which is already open will return a false value, indicating that the SPI is already busy on behalf of another process.
If several SPI hardware instances are available on the device, the user can select which SPI instance the driver shall use.
SPI Example 1: SPI Host Loopback
When in Host mode, the SPI generates the SCK clock. Data is driven out on the MOSI I/O pin according to the configured phase and polarity.
SPI Example 2: SPI Client
Here the Host sends bytes to the Micrcontroller running this code, acting as a client. When in Client mode, the SPI receives the SCK clock from the Host and uses this to sample data on the MOSI I/O pin. The phase and polarity must be configured according to the values used by the Host.
/* Required header files for example */ #include "/mcc_generated_files/spi/spi0.h"
typedef struct SPIdatas { uint8_t buffer[10]; uint32_t count; } SPIdata; SPIdata SPI; volatile uint8_t SPIFlag; void SPITransferComplete(void) { IO_INTERRUPT_SetHigh(); SPIFlag = 1; IO_INTERRUPT_SetLow(); } int main(void) { SYSTEM_Initialize(); SPI_Open(0); // Initialize SPI with configuration 0 SPI_RegisterTransferDoneCallback(SPITransferComplete); SPI_BufferRead(SPI.buffer, 1); // set up transfer for new SPI block // CPUINT_Initialize(); ENABLE_INTERRUPTS(); while (1) { if (SPIFlag == 1) { // SPI.done = 1; // Deal with newly read SPI data IO_INTERRUPT_SetHigh(); SPI_BufferRead(SPI.buffer, 1); // set up transfer for new SPI block SPIFlag = 0; // Deal with newly read SPI data SPI.count++; IO_INTERRUPT_SetLow(); } else { } } }
3.13.1.6 Class Documentation
3.13.1.6.1 spi_configuration_t Struct Reference
#include <spi_interrupt_types.h>
Public Attributes
Member Data Documentation
The documentation for this struct was generated from the following files:
source/
source/
ctrla
uint8_t ctrla
ctrlb
uint8_t ctrlb
3.13.1.6.2 spi_descriptor_t Struct Reference
#include <spi_interrupt_types.h>
Public Attributes
uint8_t * buffer
size_t bufferLength
Member Data Documentation
The documentation for this struct was generated from the following file:
source/
buffer
uint8_t* buffer
bufferLength
size_t bufferLength
status
spi_status_t status
transferType
spi_transfer_type_t transferType
3.13.1.6.3 SPI_INTERFACE Struct Reference
SPI driver interface object.
Detailed Description
SPI driver interface object.
SPI Driver prototypes struct.
#include <spi_interface_interrupt.h>
Public Attributes
void(* Initialize )(void)
void(* Deinitialize )(void)
bool(* Open )(uint8_t spiConfigIndex)
void(* Close )(void)
void(* BufferExchange )(void *bufferData, size_t bufferSize)
void(* BufferRead )(void *bufferData, size_t bufferSize)
void(* BufferWrite )(void *bufferData, size_t bufferSize)
uint8_t(* ByteExchange )(uint8_t byteData)
uint8_t(* ByteRead )(void)
void(* ByteWrite )(uint8_t byteData)
bool(* IsRxReady )(void)
bool(* IsTxReady )(void)
void(* RxCompleteCallbackRegister )(void(*callbackHandler)(void))
void(* TxCompleteCallbackRegister )(void(*callbackHandler)(void))
Member Data Documentation
The documentation for this struct was generated from the following files:
source/
source/
BufferExchange
void(* BufferExchange
BufferRead
void(* BufferRead
BufferWrite
void(* BufferWrite
ByteExchange
uint8_t(* ByteExchange
ByteRead
uint8_t(* ByteRead
ByteWrite
void(* ByteWrite
Close
void(* Close
Deinitialize
void(* Deinitialize
Initialize
void(* Initialize
IsRxReady
bool(* IsRxReady
IsTxReady
bool(* IsTxReady
Open
bool(* Open
RxCompleteCallbackRegister
void(* RxCompleteCallbackRegister
TxCompleteCallbackRegister
void(* TxCompleteCallbackRegister
3.13.1.7 File Documentation
3.13.1.7.1 source/spi0_interrupt.c File Reference
#include "../spi0.h" #include "../spi_interrupt_types.h"
Functions
void SPI0_ISR (void)
void SPI0_Initialize (void)
Initializes the SPI0 module.
void SPI0_Deinitialize (void)
Deinitializes the SPI0 module.
bool SPI0_Open (uint8_t spiConfigIndex)
Enables the SPI0 module with the configurations passed as the parameter. See spi0_configuration_name_t for the available configurations.
void SPI0_Close (void)
Closes the active configuration of the SPI0 module.
void SPI0_RxCompleteCallbackRegister (void(*CallbackHandler)(void))
Sets the callback function to be executed at the completion of data transfer in Interrupt mode.
uint8_t SPI0_ByteExchange (uint8_t byteData)
Exchanges one byte using the SPI protocol. This function is blocking.
void SPI0_BufferExchange (void *bufferData, size_t bufferSize)
Exchanges the buffer using the SPI protocol. This function is blocking in Polling mode.
void SPI0_BufferWrite (void *bufferData, size_t bufferSize)
Writes a buffer using the SPI protocol. This function is blocking in Polling mode.
void SPI0_BufferRead (void *bufferData, size_t bufferSize)
Reads a buffer using the SPI protocol. This function is blocking in Polling mode.
void SPI0_ByteWrite (uint8_t byteData)
Writes one byte using the SPI protocol. This function is blocking in Polling mode.
uint8_t SPI0_ByteRead (void)
Receives one byte using SPI protocol. This function is blocking.
bool SPI0_IsTxReady (void)
Checks if the SPI0 module is ready to write data.
bool SPI0_IsRxReady (void)
Checks if the SPI0 module is ready to read data.
ISR (SPI0_INT_vect)
Variables
const struct SPI_INTERFACE SPI0_s
static void(* SPI0_RxCompleteCallback )(void)
static spi_descriptor_t spi0_descriptor
static const spi_configuration_t spi0_configuration []
Function Documentation
ISR()
ISR (SPI0_INT_vect )
SPI0_ISR()
void SPI0_ISR (void )
Variable Documentation
spi0_configuration
const spi_configuration_t spi0_configuration[][static]
Initial value:
= { { 0x33, 0x80 } }
spi0_descriptor
spi_descriptor_t spi0_descriptor[static]
Initial value:
= { .buffer = NULL, .bufferLength = 0, .status = SPI_RESET }
SPI0_RxCompleteCallback
void(* SPI0_RxCompleteCallback) (void)[static]
SPI0_s
const struct SPI_INTERFACE SPI0_s
Initial value:
= { .Initialize = SPI0_Initialize, .Deinitialize = SPI0_Deinitialize, .Open = SPI0_Open, .Close = SPI0_Close, .BufferExchange = SPI0_BufferExchange, .BufferWrite = SPI0_BufferWrite, .BufferRead = SPI0_BufferRead, .ByteExchange = SPI0_ByteExchange, .ByteWrite = SPI0_ByteWrite, .ByteRead = SPI0_ByteRead, .IsTxReady = SPI0_IsTxReady, .IsRxReady = SPI0_IsRxReady, .RxCompleteCallbackRegister = SPI0_RxCompleteCallbackRegister, .TxCompleteCallbackRegister = NULL }
3.13.1.7.2 source/spi0_interrupt.h File Reference
#include <stdbool.h> #include "../system/utils/compiler.h" #include "spi_interface.h"
Functions
void SPI0_Initialize (void)
Initializes the SPI0 module.
void SPI0_Deinitialize (void)
Deinitializes the SPI0 module.
bool SPI0_Open (uint8_t spiConfigIndex)
Enables the SPI0 module with the configurations passed as the parameter. See spi0_configuration_name_t for the available configurations.
void SPI0_Close (void)
Closes the active configuration of the SPI0 module.
uint8_t SPI0_ByteExchange (uint8_t byteData)
Exchanges one byte using the SPI protocol. This function is blocking.
void SPI0_BufferExchange (void *bufferData, size_t bufferSize)
Exchanges the buffer using the SPI protocol. This function is blocking in Polling mode.
void SPI0_BufferWrite (void *bufferData, size_t bufferSize)
Writes a buffer using the SPI protocol. This function is blocking in Polling mode.
void SPI0_BufferRead (void *bufferData, size_t bufferSize)
Reads a buffer using the SPI protocol. This function is blocking in Polling mode.
void SPI0_ByteWrite (uint8_t byteData)
Writes one byte using the SPI protocol. This function is blocking in Polling mode.
uint8_t SPI0_ByteRead (void)
Receives one byte using SPI protocol. This function is blocking.
bool SPI0_IsRxReady (void)
Checks if the SPI0 module is ready to read data.
bool SPI0_IsTxReady (void)
Checks if the SPI0 module is ready to write data.
void SPI0_RxCompleteCallbackRegister (void(*callbackHandler)(void))
Sets the callback function to be executed at the completion of data transfer in Interrupt mode.
Enumerations
enum SPI0_configuration_name_t { SPI0_DEFAULT, SPI0_DEFAULT }
Enumeration for the different configurations supported by the driver. A configuration is specified as parameter to SPI0_Open() and is used by the function to set SPI parameters as specified by the configuration.
Variables
const struct SPI_INTERFACE SPI0_s
Variable Documentation
SPI0_s
const struct SPI_INTERFACE SPI0_s
3.13.1.7.3 source/spi0_polling.c File Reference
#include "../spi0.h" #include "../spi_polling_types.h"
Functions
void SPI0_Initialize (void)
Initializes the SPI0 module.
void SPI0_Deinitialize (void)
Deinitializes the SPI0 module.
bool SPI0_Open (uint8_t spiConfigIndex)
Enables the SPI0 module with the configurations passed as the parameter. See spi0_configuration_name_t for the available configurations.
void SPI0_Close (void)
Closes the active configuration of the SPI0 module.
uint8_t SPI0_ByteExchange (uint8_t byteData)
Exchanges one byte using the SPI protocol. This function is blocking.
bool SPI0_IsTxReady (void)
Checks if the SPI0 module is ready to write data.
bool SPI0_IsRxReady (void)
Checks if the SPI0 module is ready to read data.
void SPI0_BufferExchange (void *bufferData, size_t bufferSize)
Exchanges the buffer using the SPI protocol. This function is blocking in Polling mode.
void SPI0_BufferWrite (void *bufferData, size_t bufferSize)
Writes a buffer using the SPI protocol. This function is blocking in Polling mode.
void SPI0_BufferRead (void *bufferData, size_t bufferSize)
Reads a buffer using the SPI protocol. This function is blocking in Polling mode.
void SPI0_ByteWrite (uint8_t byteData)
Writes one byte using the SPI protocol. This function is blocking in Polling mode.
uint8_t SPI0_ByteRead (void)
Receives one byte using SPI protocol. This function is blocking.
Variables
const struct SPI_INTERFACE SPI0_s
static const spi_configuration_t spi0_configuration []
Variable Documentation
spi0_configuration
const spi_configuration_t spi0_configuration[][static]
Initial value:
= { { 0x33, 0xC0 } }
SPI0_s
const struct SPI_INTERFACE SPI0_s
Initial value:
= { .Initialize = SPI0_Initialize, .Deinitialize = SPI0_Deinitialize, .Open = SPI0_Open, .Close = SPI0_Close, .BufferExchange = SPI0_BufferExchange, .BufferWrite = SPI0_BufferWrite, .BufferRead = SPI0_BufferRead, .ByteExchange = SPI0_ByteExchange, .ByteWrite = SPI0_ByteWrite, .ByteRead = SPI0_ByteRead, .IsTxReady = SPI0_IsTxReady, .IsRxReady = SPI0_IsRxReady, .RxCompleteCallbackRegister = NULL, .TxCompleteCallbackRegister = NULL }
3.13.1.7.4 source/spi0_polling.h File Reference
#include <stdbool.h> #include "../system/utils/compiler.h" #include "spi_interface.h"
Functions
void SPI0_Initialize (void)
Initializes the SPI0 module.
void SPI0_Deinitialize (void)
Deinitializes the SPI0 module.
bool SPI0_Open (uint8_t spiConfigIndex)
Enables the SPI0 module with the configurations passed as the parameter. See spi0_configuration_name_t for the available configurations.
void SPI0_Close (void)
Closes the active configuration of the SPI0 module.
uint8_t SPI0_ByteExchange (uint8_t byteData)
Exchanges one byte using the SPI protocol. This function is blocking.
void SPI0_BufferExchange (void *bufferData, size_t bufferSize)
Exchanges the buffer using the SPI protocol. This function is blocking in Polling mode.
void SPI0_BufferWrite (void *bufferData, size_t bufferSize)
Writes a buffer using the SPI protocol. This function is blocking in Polling mode.
void SPI0_BufferRead (void *bufferData, size_t bufferSize)
Reads a buffer using the SPI protocol. This function is blocking in Polling mode.
void SPI0_ByteWrite (uint8_t byteData)
Writes one byte using the SPI protocol. This function is blocking in Polling mode.
uint8_t SPI0_ByteRead (void)
Receives one byte using SPI protocol. This function is blocking.
bool SPI0_IsRxReady (void)
Checks if the SPI0 module is ready to read data.
bool SPI0_IsTxReady (void)
Checks if the SPI0 module is ready to write data.
Enumerations
Variables
const struct SPI_INTERFACE SPI0_s
Enumeration Type Documentation
SPI0_configuration_name_t
enum SPI0_configuration_name_t
SPI0_DEFAULT | |
SPI0_DEFAULT |
Variable Documentation
SPI0_s
const struct SPI_INTERFACE SPI0_s
3.13.1.7.5 source/spi_interface_interrupt.h File Reference
#include <stdint.h> #include <stdbool.h> #include <stddef.h>
Data structures
struct SPI_INTERFACE
SPI driver interface object.
3.13.1.7.6 source/spi_interface_polling.h File Reference
#include <stdint.h> #include <stdbool.h> #include <stddef.h>
Data structures
struct SPI_INTERFACE
SPI driver interface object.
3.13.1.7.7 source/spi_interrupt_types.h File Reference
Data structures
struct spi_descriptor_t
struct spi_configuration_t
Enumerations
enum spi_status_t { SPI_RESET = 0x00U, SPI_IDLE = 0x81U, SPI_BUSY = 0x42U }
Enumeration for the different states of the SPI module.
enum spi_transfer_type_t { SPI_READ = 0x00U, SPI_WRITE = 0x81U, SPI_EXCHANGE = 0X42U }
Enumeration for the different transfer types of the SPI module.
Detailed Description
SPI Type Definitions Header File
3.13.1.7.8 source/spi_polling_types.h File Reference
Data structures
struct spi_configuration_t
Detailed Description
SPI Type Definitions Header File