3.22.1 SPI - Serial Peripheral Interface
3.22.1.1 Introduction
The Serial Peripheral Interface (SPI) module is a synchronous serial data communication bus that operates in Full Duplex mode.
3.22.1.2 Supported Device Families
PIC18F-Q20 | PIC18F-Q40 | PIC18F-Q41 |
PIC18F-K83 | PIC18F-Q71 | PIC18F-Q24 |
PIC18F-K42 | PIC18F-Q43 | PIC18F-Q83 |
PIC18F-Q84 |
3.22.1.3 Required header files:
#include "mcc_generated_files/spi/spi[X].h"
3.22.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 Use Case Code Snippet Instructions
SPI Host Mode (Tx/Rx/Exchange - of Bytes and Blocks):
- Use Case: Host Mode Transmit a Byte of Data via SPI
- Use Case: Host Mode Receive a Byte of Data via SPI
- Use Case: Host Mode Exchange a Byte of Data via SPI
- Use Case: Host Mode Transmit a Block of Data via SPI
- Use Case: Host Mode Receive a Block of Data via SPI
- Use Case: Host Mode Exchange a Block of Data via SPI
SPI Client Mode (Tx/Rx/Exchange - of Bytes and Blocks):
- Use Case: Client Mode Transmit a Byte of Data via SPI
- Use Case: Client Mode Receive a Byte of Data via SPI
- Use Case: Client Mode Exchange a Byte of Data via SPI
- Use Case: Client Mode Transmit a Block of Data via SPI
- Use Case: Client Mode Receive a Block of Data via SPI
- Use Case: Client Mode Exchange a Block of Data via SPI
3.22.1.5 Module Documentation
3.22.1.5.1 SPI1
This header file provides API prototypes for the SPI1 driver.
Module description
This header file provides API prototypes for the SPI1 driver.
Data structures
struct SPI_INTERFACE
Defines an object for SPI_INTERFACE functions.
Enumerations
enum spi1_configuration_name_t { SPI1_DEFAULT }
Enumeration of the different configurations supported by the driver. A configuration is specified as parameter to Open() and is used by the function to set SPI parameters as specified by the configuration.
Functions
void SPI1_Initialize (void)
Initializes the SPI1 module.
void SPI1_Deinitialize (void)
Deinitializes the SPI1 module.
bool SPI1_Open (uint8_t spiConfigIndex)
Enables the SPI1 module with the configurations passed as the parameter. See spi1_config_names_t for the available configurations.
void SPI1_Close (void)
Closes the active configuration of the SPI1 module.
void SPI1_BufferExchange (void *bufferData, size_t bufferSize)
Exchanges the buffer using the SPI protocol. This function is blocking in Polling mode.
void SPI1_BufferWrite (void *bufferData, size_t bufferSize)
Writes a buffer using the SPI protocol. This function is blocking in Polling mode.
void SPI1_BufferRead (void *bufferData, size_t bufferSize)
Reads a buffer using the SPI protocol. This function is blocking in Polling mode.
uint8_t SPI1_ByteExchange (uint8_t byteData)
Exchanges one byte using the SPI protocol. This function is blocking.
void SPI1_ByteWrite (uint8_t byteData)
Writes one byte using the SPI protocol.
uint8_t SPI1_ByteRead (void)
Receives one byte using the SPI protocol.
bool SPI1_IsRxReady (void)
Checks if the SPI1 module is ready to read data.
bool SPI1_IsTxReady (void)
Checks if the SPI1 is ready to write data.
Function Documentation
SPI1_BufferExchange()
void SPI1_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. |
SPI1_BufferRead()
void SPI1_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. |
SPI1_BufferWrite()
void SPI1_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. |
SPI1_ByteExchange()
uint8_t SPI1_ByteExchange (uint8_t byteData)
Exchanges one byte using the SPI protocol. This function is blocking.
byteData |
Byte to be written |
Received data byte |
SPI1_ByteRead()
uint8_t SPI1_ByteRead (void )
Receives one byte using the SPI protocol.
None. |
Received data byte. |
SPI1_ByteWrite()
void SPI1_ByteWrite (uint8_t byteData)
Writes one byte using the SPI protocol.
byteData |
Byte to be written |
None. |
SPI1_Close()
void SPI1_Close (void )
Closes the active configuration of the SPI1 module.
None. |
None. |
SPI1_Deinitialize()
void SPI1_Deinitialize (void )
Deinitializes the SPI1 module.
None. |
None. |
SPI1_Initialize()
void SPI1_Initialize (void )
Initializes the SPI1 module.
None. |
None. |
SPI1_IsRxReady()
bool SPI1_IsRxReady (void )
Checks if the SPI1 module is ready to read data.
None. |
True |
SPI1 module is ready to read data |
False |
SPI1 module is not ready to read data |
SPI1_IsTxReady()
bool SPI1_IsTxReady (void )
Checks if the SPI1 is ready to write data.
None. |
True |
SPI1 module is ready to write data |
False |
SPI1 module is not ready to write data |
SPI1_Open()
bool SPI1_Open (uint8_t spiConfigIndex)
Enables the SPI1 module with the configurations passed as the parameter. See spi1_config_names_t for the available configurations.
spiConfigIndex |
Configuration index |
True |
SPI1 module is enabled successfully with the chosen configuration |
False |
SPI1 module is already open with another configuration |
Enumeration Type Documentation
spi1_configuration_name_t
enum enum spi1_configuration_name_t
Enumeration of the different configurations supported by the driver. A configuration is specified as parameter to Open() and is used by the function to set SPI parameters as specified by the configuration.
SPI1_DEFAULT |
3.22.1.5.2 SPI2
This header file provides API prototypes for the SPI2 driver.
Module description
This header file provides API prototypes for the SPI2 driver.
Data structures
struct SPI_INTERFACE
Defines an object for SPI_INTERFACE functions.
Enumerations
enum spi2_configuration_name_t { SPI2_DEFAULT }
Enumeration of the different configurations supported by the driver. A configuration is specified as parameter to Open() and is used by the function to set SPI parameters as specified by the configuration.
Functions
void SPI2_Initialize (void)
Initializes the SPI2 module.
void SPI2_Deinitialize (void)
Deinitializes the SPI2 module.
bool SPI2_Open (uint8_t spiConfigIndex)
Enables the SPI2 module with the configurations passed as the parameter. See spi2_config_names_t for the available configurations.
void SPI2_Close (void)
Closes the active configuration of the SPI2 module.
void SPI2_BufferExchange (void *bufferData, size_t bufferSize)
Exchanges the buffer using the SPI protocol. This function is blocking in Polling mode.
void SPI2_BufferWrite (void *bufferData, size_t bufferSize)
Writes a buffer using the SPI protocol. This function is blocking in Polling mode.
void SPI2_BufferRead (void *bufferData, size_t bufferSize)
Reads a buffer using the SPI protocol. This function is blocking in Polling mode.
uint8_t SPI2_ByteExchange (uint8_t byteData)
Exchanges one byte using the SPI protocol. This function is blocking.
void SPI2_ByteWrite (uint8_t byteData)
Writes one byte using the SPI protocol.
uint8_t SPI2_ByteRead (void)
Receives one byte using the SPI protocol.
bool SPI2_IsRxReady (void)
Checks if the SPI2 module is ready to read data.
bool SPI2_IsTxReady (void)
Checks if the SPI2 is ready to write data.
void SPI2_RxCompleteCallbackRegister (void(*rxCompleteCallbackHandler)(void))
Sets the callback function to be executed at the completion of data transfer receive in Interrupt mode.
void SPI2_TxCompleteCallbackRegister (void(*txCompleteCallbackHandler)(void))
Sets the callback function to be executed at the completion of data transfer transmit in Interrupt mode.
void SPI2_Receive_ISR (void)
Interrupt service routine (ISR) with receive interrupt RXIF for the SPI2 module.
void SPI2_Transmit_ISR (void)
ISR with transmit interrupt TXIF for the SPI2 module.
void SPI2_ISR (void)
ISR for the SPI2 module.
Function Documentation
SPI2_BufferExchange()
void SPI2_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. |
SPI2_BufferRead()
void SPI2_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. |
SPI2_BufferWrite()
void SPI2_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. |
SPI2_ByteExchange()
uint8_t SPI2_ByteExchange (uint8_t byteData)
Exchanges one byte using the SPI protocol. This function is blocking.
byteData |
Byte to be written |
Received data byte |
SPI2_ByteRead()
uint8_t SPI2_ByteRead (void )
Receives one byte using the SPI protocol.
None. |
Received data byte. |
SPI2_ByteWrite()
void SPI2_ByteWrite (uint8_t byteData)
Writes one byte using the SPI protocol.
byteData |
Byte to be written |
None. |
SPI2_Close()
void SPI2_Close (void )
Closes the active configuration of the SPI2 module.
None. |
None. |
SPI2_Deinitialize()
void SPI2_Deinitialize (void )
Deinitializes the SPI2 module.
None. |
None. |
SPI2_Initialize()
void SPI2_Initialize (void )
Initializes the SPI2 module.
None. |
None. |
SPI2_ISR()
void SPI2_ISR (void )
ISR for the SPI2 module.
None. |
None. |
SPI2_IsRxReady()
bool SPI2_IsRxReady (void )
Checks if the SPI2 module is ready to read data.
None. |
True |
SPI2 module is ready to read data |
False |
SPI2 module is not ready to read data |
SPI2_IsTxReady()
bool SPI2_IsTxReady (void )
Checks if the SPI2 is ready to write data.
None. |
True |
SPI2 module is ready to write data |
False |
SPI2 module is not ready to write data |
SPI2_Open()
bool SPI2_Open (uint8_t spiConfigIndex)
Enables the SPI2 module with the configurations passed as the parameter. See spi2_config_names_t for the available configurations.
spiConfigIndex |
Configuration index |
True |
SPI2 module is enabled successfully with the chosen configuration |
False |
SPI2 module is already open with another configuration |
SPI2_Receive_ISR()
void SPI2_Receive_ISR (void )
Interrupt service routine (ISR) with receive interrupt RXIF for the SPI2 module.
None. |
None. |
SPI2_RxCompleteCallbackRegister()
void SPI2_RxCompleteCallbackRegister (void(*)(void) rxCompleteCallbackHandler)
Sets the callback function to be executed at the completion of data transfer receive in Interrupt mode.
Pointer |
to the function to be executed |
None. |
SPI2_Transmit_ISR()
void SPI2_Transmit_ISR (void )
ISR with transmit interrupt TXIF for the SPI2 module.
None. |
None. |
SPI2_TxCompleteCallbackRegister()
void SPI2_TxCompleteCallbackRegister (void(*)(void) txCompleteCallbackHandler)
Sets the callback function to be executed at the completion of data transfer transmit in Interrupt mode.
Pointer |
to the function to be executed |
None. |
Enumeration Type Documentation
spi2_configuration_name_t
enum enum spi2_configuration_name_t
Enumeration of the different configurations supported by the driver. A configuration is specified as parameter to Open() and is used by the function to set SPI parameters as specified by the configuration.
SPI2_DEFAULT |
3.22.1.5.3 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
Defines an object for SPI_INTERFACE functions.
Enumerations
enum spi_status_t { SPI_RESET = 0x00U, SPI_IDLE = 0x81U, SPI_BUSY = 0x42U }
Enumeration for the different states 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 |
3.22.1.6 Data Structure Documentation
3.22.1.6.1 spi_configuration_t Struct Reference
#include <spi_interrupt_types.h>
Data Fields
Field Documentation
The documentation for this struct was generated from the following files:
source/source-files/
source/source-files/
baud
uint8_t baud
clksel
uint8_t clksel
con0
uint8_t con0
con1
uint8_t con1
con2
uint8_t con2
3.22.1.6.2 spi_descriptor_t Struct Reference
#include <spi_interrupt_types.h>
Data Fields
uint8_t * transmitBuffer
uint8_t * receiveBuffer
size_t bytesToTransmit
size_t bytesToReceive
Field Documentation
The documentation for this struct was generated from the following file:
source/source-files/
bytesToReceive
size_t bytesToReceive
bytesToTransmit
size_t bytesToTransmit
receiveBuffer
uint8_t* receiveBuffer
status
spi_status_t status
transmitBuffer
uint8_t* transmitBuffer
3.22.1.6.3 SPI_INTERFACE Struct Reference
Defines an object for SPI_INTERFACE functions.
Detailed Description
Defines an object for SPI_INTERFACE functions.
SPI Driver prototypes struct.
SPI driver interface object.
Section: Included Files
#include <spi_interface_interrupt.h>
Data Fields
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))
Field Documentation
The documentation for this struct was generated from the following files:
source/source-files/
source/source-files/
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.22.1.7 File Documentation
3.22.1.7.1 source/source-files/spi1_polling.c File Reference
#include <xc.h> #include "../spi_interface.h" #include "../spi1.h" #include "../spi_polling_types.h"
Functions
void SPI1_Initialize (void)
Initializes the SPI1 module.
void SPI1_Deinitialize (void)
Deinitializes the SPI1 module.
bool SPI1_Open (uint8_t spiConfigIndex)
Enables the SPI1 module with the configurations passed as the parameter. See spi1_config_names_t for the available configurations.
void SPI1_Close (void)
Closes the active configuration of the SPI1 module.
void SPI1_BufferExchange (void *bufferData, size_t bufferSize)
Exchanges the buffer using the SPI protocol. This function is blocking in Polling mode.
void SPI1_BufferWrite (void *bufferData, size_t bufferSize)
Writes a buffer using the SPI protocol. This function is blocking in Polling mode.
void SPI1_BufferRead (void *bufferData, size_t bufferSize)
Reads a buffer using the SPI protocol. This function is blocking in Polling mode.
uint8_t SPI1_ByteExchange (uint8_t byteData)
Exchanges one byte using the SPI protocol. This function is blocking.
void SPI1_ByteWrite (uint8_t byteData)
Writes one byte using the SPI protocol.
uint8_t SPI1_ByteRead (void)
Receives one byte using the SPI protocol.
bool SPI1_IsTxReady (void)
Checks if the SPI1 is ready to write data.
bool SPI1_IsRxReady (void)
Checks if the SPI1 module is ready to read data.
Variables
const struct SPI_INTERFACE SPI1
static const spi_configuration_t spi1_configuration []
Variable Documentation
SPI1
const struct SPI_INTERFACE SPI1
Initial value:
= { .Initialize = SPI1_Initialize, .Deinitialize = SPI1_Deinitialize, .Open = SPI1_Open, .Close = SPI1_Close, .BufferExchange = SPI1_BufferExchange, .BufferWrite = SPI1_BufferWrite, .BufferRead = SPI1_BufferRead, .ByteExchange = SPI1_ByteExchange, .ByteWrite = SPI1_ByteWrite, .ByteRead = SPI1_ByteRead, .IsTxReady = SPI1_IsTxReady, .IsRxReady = SPI1_IsRxReady, .RxCompleteCallbackRegister = NULL, .TxCompleteCallbackRegister = NULL }
spi1_configuration
const spi_configuration_t spi1_configuration[][static]
Initial value:
= { { 0x2, 0x40, 0x0, 0x0, 0x0 } }
3.22.1.7.2 source/source-files/spi1_polling.h File Reference
#include <stdint.h> #include <stdbool.h> #include "spi_interface.h"
Functions
void SPI1_Initialize (void)
Initializes the SPI1 module.
void SPI1_Deinitialize (void)
Deinitializes the SPI1 module.
bool SPI1_Open (uint8_t spiConfigIndex)
Enables the SPI1 module with the configurations passed as the parameter. See spi1_config_names_t for the available configurations.
void SPI1_Close (void)
Closes the active configuration of the SPI1 module.
void SPI1_BufferExchange (void *bufferData, size_t bufferSize)
Exchanges the buffer using the SPI protocol. This function is blocking in Polling mode.
void SPI1_BufferWrite (void *bufferData, size_t bufferSize)
Writes a buffer using the SPI protocol. This function is blocking in Polling mode.
void SPI1_BufferRead (void *bufferData, size_t bufferSize)
Reads a buffer using the SPI protocol. This function is blocking in Polling mode.
uint8_t SPI1_ByteExchange (uint8_t byteData)
Exchanges one byte using the SPI protocol. This function is blocking.
void SPI1_ByteWrite (uint8_t byteData)
Writes one byte using the SPI protocol.
uint8_t SPI1_ByteRead (void)
Receives one byte using the SPI protocol.
bool SPI1_IsRxReady (void)
Checks if the SPI1 module is ready to read data.
bool SPI1_IsTxReady (void)
Checks if the SPI1 is ready to write data.
Enumerations
enum spi1_configuration_name_t { SPI1_DEFAULT }
Enumeration of the different configurations supported by the driver. A configuration is specified as parameter to Open() and is used by the function to set SPI parameters as specified by the configuration.
Variables
const struct SPI_INTERFACE SPI1
Variable Documentation
SPI1
const struct SPI_INTERFACE SPI1
3.22.1.7.3 source/source-files/spi2_interrupt.c File Reference
#include <xc.h> #include "../spi_interface.h" #include "../spi2.h" #include "../spi_interrupt_types.h"
Functions
void SPI2_Initialize (void)
Initializes the SPI2 module.
void SPI2_Deinitialize (void)
Deinitializes the SPI2 module.
bool SPI2_Open (uint8_t spiConfigIndex)
Enables the SPI2 module with the configurations passed as the parameter. See spi2_config_names_t for the available configurations.
void SPI2_Close (void)
Closes the active configuration of the SPI2 module.
void SPI2_BufferExchange (void *bufferData, size_t bufferSize)
Exchanges the buffer using the SPI protocol. This function is blocking in Polling mode.
void SPI2_BufferWrite (void *bufferData, size_t bufferSize)
Writes a buffer using the SPI protocol. This function is blocking in Polling mode.
void SPI2_BufferRead (void *bufferData, size_t bufferSize)
Reads a buffer using the SPI protocol. This function is blocking in Polling mode.
uint8_t SPI2_ByteExchange (uint8_t byteData)
Exchanges one byte using the SPI protocol. This function is blocking.
void SPI2_ByteWrite (uint8_t byteData)
Writes one byte using the SPI protocol.
uint8_t SPI2_ByteRead (void)
Receives one byte using the SPI protocol.
bool SPI2_IsTxReady (void)
Checks if the SPI2 is ready to write data.
bool SPI2_IsRxReady (void)
Checks if the SPI2 module is ready to read data.
void SPI2_RxCompleteCallbackRegister (void(*rxCompleteCallbackHandler)(void))
Sets the callback function to be executed at the completion of data transfer receive in Interrupt mode.
void SPI2_TxCompleteCallbackRegister (void(*txCompleteCallbackHandler)(void))
Sets the callback function to be executed at the completion of data transfer transmit in Interrupt mode.
void SPI2_Receive_ISR (void)
Interrupt service routine (ISR) with receive interrupt RXIF for the SPI2 module.
void SPI2_Transmit_ISR (void)
ISR with transmit interrupt TXIF for the SPI2 module.
void SPI2_ISR (void)
ISR for the SPI2 module.
Variables
const struct SPI_INTERFACE SPI2
static void(* SPI2_RxCompleteCallback )(void)
static void(* SPI2_TxCompleteCallback )(void)
static spi_descriptor_t spi2_descriptor
static const spi_configuration_t spi2_configuration []
Variable Documentation
SPI2
const struct SPI_INTERFACE SPI2
Initial value:
= { .Initialize = SPI2_Initialize, .Deinitialize = SPI2_Deinitialize, .Open = SPI2_Open, .Close = SPI2_Close, .BufferExchange = SPI2_BufferExchange, .BufferWrite = SPI2_BufferWrite, .BufferRead = SPI2_BufferRead, .ByteExchange = SPI2_ByteExchange, .ByteWrite = SPI2_ByteWrite, .ByteRead = SPI2_ByteRead, .IsTxReady = SPI2_IsTxReady, .IsRxReady = SPI2_IsRxReady, .RxCompleteCallbackRegister = SPI2_RxCompleteCallbackRegister, .TxCompleteCallbackRegister = SPI2_TxCompleteCallbackRegister }
spi2_configuration
const spi_configuration_t spi2_configuration[][static]
Initial value:
= { { 0x00, 0x40, 0x04, 0x00, 0x00 } }
spi2_descriptor
spi_descriptor_t spi2_descriptor[static]
Initial value:
= { .transmitBuffer = NULL, .receiveBuffer = NULL, .bytesToTransmit = 0, .bytesToReceive = 0, .status = SPI_RESET }
SPI2_RxCompleteCallback
void(* SPI2_RxCompleteCallback) (void)[static]
SPI2_TxCompleteCallback
void(* SPI2_TxCompleteCallback) (void)[static]
3.22.1.7.4 source/source-files/spi2_interrupt.h File Reference
#include <stdint.h> #include <stdbool.h> #include "spi_interface.h"
Functions
void SPI2_Initialize (void)
Initializes the SPI2 module.
void SPI2_Deinitialize (void)
Deinitializes the SPI2 module.
bool SPI2_Open (uint8_t spiConfigIndex)
Enables the SPI2 module with the configurations passed as the parameter. See spi2_config_names_t for the available configurations.
void SPI2_Close (void)
Closes the active configuration of the SPI2 module.
void SPI2_BufferExchange (void *bufferData, size_t bufferSize)
Exchanges the buffer using the SPI protocol. This function is blocking in Polling mode.
void SPI2_BufferWrite (void *bufferData, size_t bufferSize)
Writes a buffer using the SPI protocol. This function is blocking in Polling mode.
void SPI2_BufferRead (void *bufferData, size_t bufferSize)
Reads a buffer using the SPI protocol. This function is blocking in Polling mode.
uint8_t SPI2_ByteExchange (uint8_t byteData)
Exchanges one byte using the SPI protocol. This function is blocking.
void SPI2_ByteWrite (uint8_t byteData)
Writes one byte using the SPI protocol.
uint8_t SPI2_ByteRead (void)
Receives one byte using the SPI protocol.
bool SPI2_IsRxReady (void)
Checks if the SPI2 module is ready to read data.
bool SPI2_IsTxReady (void)
Checks if the SPI2 is ready to write data.
void SPI2_RxCompleteCallbackRegister (void(*rxCompleteCallbackHandler)(void))
Sets the callback function to be executed at the completion of data transfer receive in Interrupt mode.
void SPI2_TxCompleteCallbackRegister (void(*txCompleteCallbackHandler)(void))
Sets the callback function to be executed at the completion of data transfer transmit in Interrupt mode.
void SPI2_Receive_ISR (void)
Interrupt service routine (ISR) with receive interrupt RXIF for the SPI2 module.
void SPI2_Transmit_ISR (void)
ISR with transmit interrupt TXIF for the SPI2 module.
void SPI2_ISR (void)
ISR for the SPI2 module.
Enumerations
enum spi2_configuration_name_t { SPI2_DEFAULT }
Enumeration of the different configurations supported by the driver. A configuration is specified as parameter to Open() and is used by the function to set SPI parameters as specified by the configuration.
Variables
const struct SPI_INTERFACE SPI2
Variable Documentation
SPI2
const struct SPI_INTERFACE SPI2
3.22.1.7.5 source/source-files/spi_interface_interrupt.h File Reference
#include <stdint.h> #include <stdbool.h> #include <stddef.h>
Data structures
struct SPI_INTERFACE
Defines an object for SPI_INTERFACE functions.
3.22.1.7.6 source/source-files/spi_interface_polling.h File Reference
#include <stdint.h> #include <stdbool.h> #include <stddef.h>
Data structures
struct SPI_INTERFACE
Defines an object for SPI_INTERFACE functions.
3.22.1.7.7 source/source-files/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.
Detailed Description
SPI Type Definitions Header File
3.22.1.7.8 source/source-files/spi_polling_types.h File Reference
Data structures
struct spi_configuration_t
Detailed Description
SPI Type Definitions Header File
3.22.1.8 Module Documentation
3.22.1.8.1 SPI Use Cases
SPI Use Case Code Snippet Instructions
The use cases show example uses of the SPI PLIB Driver, within a MCC Melody project:
-
Add SPIx to the project
-
Configure:
-
SPIx component as described in the example.
-
Any other components or pins needed for the use case.
-
-
Generate the code
-
Add the code snippet(s) to the application code
-
Program the board
Use Case: Host Mode Transmit a Byte of Data via SPI
This example shows how to Setup Host Mode and to transmit a byte of data via the SPI peripheral.
-
Add SPIx
-
Software Settings:
-
Custome Name: SPI1
-
-
Hardware Settings:
-
Mode: Host
-
SPI Mode: Mode 0
-
Input data sampled at: Middle
-
Requested Clock Frequency (Hz): 500000
-
-
Interrupt Settings:
-
Enable SPI Interrupt: Disabled
-
-
Advanced Settings:
-
Clock Source Selection: HFINTOSC
-
-
Dependency Selection:
-
SPI Dependency Selection: SPI1
-
-
Pin Grid View:
-
Select PINS for SCKx, SDIx and SDOx
-
-
PINS:
-
Name pins SCKx, SDIx and SDOx as SCK, SDI and SDO.
-
#include "mcc_generated_files/system/system.h"
int main(void) { SYSTEM_Initialize(); SPI1_Close(); uint8_t data = 0xA5; while(1) { //Send Byte via SPI SPI1_Open(SPI1_DEFAULT); SPI1TCNT = 1; SPI1_ByteWrite(data); while(!PIR3bits.SPI1RXIF); SPI1_Close(); } }
Use Case: Host Mode Receive a Byte of Data via SPI
This example shows how to Setup Host Mode and to Receive a byte of data via the SPI peripheral.
-
Add SPIx
-
Software Settings:
-
Custome Name: SPI1
-
-
Hardware Settings:
-
Mode: Host
-
SPI Mode: Select Mode 0
-
Input data sampled at: Middle
-
Requested Clock Frequency (Hz): 500000
-
-
Interrupt Settings:
-
Enable SPI Interrupt: Disabled
-
-
Advanced Settings:
-
Clock Source Selection: HFINTOSC
-
-
Dependency Selection:
-
SPI Dependency Selection: SPI1
-
-
Pin Grid View:
-
Select PINS for SCKx, SDIx and SDOx
-
-
PINS:
-
Name pins SCKx, SDIx and SDOx as SCK, SDI and SDO.
-
After configuring the components as described above, click 'Generate' to generate code. Then add the following code snippets to your application:
#include "mcc_generated_files/system/system.h"
int main(void) { SYSTEM_Initialize(); SPI1_Close(); while(1) { uint8_t receivedData; SPI1_Open(SPI1_DEFAULT); receivedData = SPI1_ByteRead(); __delay_us(80); SPI1_Close(); } }
Use Case: Host Mode Exchange a Byte of Data via SPI
This example shows how to Setup Host Mode and to Exchange a byte of data via the SPI peripheral.
-
Add SPIx
-
Software Settings:
-
Custome Name: SPI1
-
-
Hardware Settings:
-
Mode: Host
-
SPI Mode: Select Mode 0
-
Input data sampled at: Middle
-
Requested Clock Frequency (Hz): 500000
-
-
Interrupt Settings:
-
Enable SPI Interrupt: Disabled
-
-
Advanced Settings:
-
Clock Source Selection: HFINTOSC
-
-
Dependency Selection:
-
SPI Dependency Selection: SPI1
-
-
Pin Grid View:
-
Select PINS for SCKx, SDIx and SDOx
-
-
PINS:
-
Name pins SCKx, SDIx and SDOx as SCK, SDI and SDO.
-
After configuring the components as described above, click 'Generate' to generate code. Then add the following code snippets to your application:
#include "mcc_generated_files/system/system.h"
int main(void) { SYSTEM_Initialize(); SPI1_Close(); while(1) { //Exchange Byte via SPI uint8_t data = 0xA5; uint8_t receivedData; SPI1_Open(SPI1_DEFAULT); receivedData = SPI1_ByteExchange(data); SPI1_Close(); } }
Use Case: Host Mode Transmit a Block of Data via SPI
This example shows how to Setup Host Mode and to transmit a block of data via the SPI peripheral.
-
Add SPIx
-
Software Settings:
-
Custome Name: SPI1
-
-
Hardware Settings:
-
Mode: Host
-
SPI Mode: Select Mode 0
-
Input data sampled at: Middle
-
Requested Clock Frequency (Hz): 500000
-
-
Interrupt Settings:
-
Enable SPI Interrupt: Disabled
-
-
Advanced Settings:
-
Clock Source Selection: HFINTOSC
-
-
Dependency Selection:
-
SPI Dependency Selection: SPI1
-
-
Pin Grid View:
-
Select PINS for SCKx, SDIx and SDOx
-
-
PINS:
-
Name pins SCKx, SDIx and SDOx as SCK, SDI and SDO.
-
After configuring the components as described above, click 'Generate' to generate code. Then add the following code snippets to your application:
#include "mcc_generated_files/system/system.h"
uint32_t bufferSize = 64; uint8_t dataBufPtr[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F, 0x40 }; int main(void) { SYSTEM_Initialize(); SPI1_Close(); while(1) { SPI1_Open(SPI1_DEFAULT); SPI1_BufferWrite(dataBufPtr, bufferSize); SPI1_Close(); } }
Use Case: Host Mode Receive a Block of Data via SPI
This example shows how to Setup Host Mode and to Receive a block of data via the SPI peripheral.
-
Add SPIx
-
Software Settings:
-
Custome Name: SPI1
-
-
Hardware Settings:
-
Mode: Host
-
SPI Mode: Select Mode 0
-
Input data sampled at: Middle
-
Requested Clock Frequency (Hz): 500000
-
-
Interrupt Settings:
-
Enable SPI Interrupt: Disabled
-
-
Advanced Settings:
-
Clock Source Selection: HFINTOSC
-
-
Dependency Selection:
-
SPI Dependency Selection: SPI1
-
-
Pin Grid View:
-
Select PINS for SCKx, SDIx and SDOx
-
-
PINS:
-
Name pins SCKx, SDIx and SDOx as SCK, SDI and SDO.
-
After configuring the components as described above, click 'Generate' to generate code. Then add the following code snippets to your application:
#include "mcc_generated_files/system/system.h"
int main(void) { SYSTEM_Initialize(); SPI1_Close(); while(1) { #define bufferSize 64 volatile uint8_t readDataBuffer[bufferSize]; SPI1_Open(SPI1_DEFAULT); SS1_SetLow(); SPI1_BufferRead(readDataBuffer, bufferSize); SS1_SetHigh(); SPI1_Close(); } }
Use Case: Host Mode Exchange a Block of Data via SPI
This example shows how to setup Host Mode and to Exchange a block of data via the SPI peripheral.
-
Add SPIx
-
Software Settings:
-
Custome Name: SPI1
-
-
Hardware Settings:
-
Mode: Host
-
SPI Mode: Select Mode 0
-
Input data sampled at: Middle
-
Requested Clock Frequency (Hz): 500000
-
-
Interrupt Settings:
-
Enable SPI Interrupt: Disabled
-
-
Advanced Settings:
-
Clock Source Selection: HFINTOSC
-
-
Dependency Selection:
-
SPI Dependency Selection: SPI1
-
-
Pin Grid View:
-
Select PINS for SCKx, SDIx and SDOx
-
-
PINS:
-
Name pins SCKx, SDIx and SDOx as SCK, SDI and SDO.
-
After configuring the components as described above, click 'Generate' to generate code. Then add the following code snippets to your application:
#include "mcc_generated_files/system/system.h"
uint32_t bufferSize = 64; volatile uint8_t dataBufPtr[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F, 0x40, }; int main(void) { SYSTEM_Initialize(); SPI1_Close(); SPI1_Open(SPI1_DEFAULT); SS1_SetLow(); SPI1_BufferWrite(dataBufPtr, bufferSize); SS1_SetHigh(); SPI1_Close(); while(1) { } }
Use Case: Client Mode Transmit a Byte of Data via SPI
This example shows how to setup Client Mode to transmit a byte of data via the SPI peripheral.
-
Add SPIx
-
Software Settings:
-
Custome Name: SPI1
-
-
Hardware Settings:
-
Mode: Client
-
SPI Mode: Mode 0
-
-
Interrupt Settings:
-
Enable SPI Interrupt: Disabled
-
-
Dependency Selection:
-
SPI Dependency Selection: SPI1
-
-
Pin Grid View:
-
Select PINS for SCKx, SDIx and SDOx
-
-
PINS:
-
Name pins SCKx, SDIx and SDOx as SCK, SDI and SDO.
-
#include "mcc_generated_files/system/system.h"
int main(void) { SYSTEM_Initialize(); SPI1_Close(); uint8_t data = 0xA5; while(1) { SPI1_Open(SPI1_DEFAULT); SPI1_ByteWrite(data); SPI1_Close(); } }
Use Case: Client Mode Receive a Byte of Data via SPI
This example shows how to setup Client Mode to Receive a byte of data via the SPI peripheral.
-
Add SPIx
-
Software Settings:
-
Custome Name: SPI1
-
-
Hardware Settings:
-
Mode: Client
-
SPI Mode: Mode 0
-
-
Interrupt Settings:
-
Enable SPI Interrupt: Disabled
-
-
Dependency Selection:
-
SPI Dependency Selection: SPI1
-
-
Pin Grid View:
-
Select PINS for SCKx, SDIx and SDOx
-
-
PINS:
-
Name pins SCKx, SDIx and SDOx as SCK, SDI and SDO.
-
#include "mcc_generated_files/system/system.h"
int main(void) { SYSTEM_Initialize(); SPI1_Close(); while(1) { uint8_t receivedData; SPI1_Open(SPI1_DEFAULT); receivedData = SPI1_ByteRead(); SPI1_Close(); } }
Use Case: Client Mode Exchange a Byte of Data via SPI
This example shows how to setup Client Mode to Exchange a byte of data via the SPI peripheral.
-
Add SPIx
-
Software Settings:
-
Custome Name: SPI1
-
-
Hardware Settings:
-
Mode: Client
-
SPI Mode: Mode 0
-
-
Interrupt Settings:
-
Enable SPI Interrupt: Disabled
-
-
Dependency Selection:
-
SPI Dependency Selection: SPI1
-
-
Pin Grid View:
-
Select PINS for SCKx, SDIx and SDOx
-
-
PINS:
-
Name pins SCKx, SDIx and SDOx as SCK, SDI and SDO.
-
#include "mcc_generated_files/system/system.h"
int main(void) { SYSTEM_Initialize(); SPI1_Close(); while(1) { //Exchange Byte via SPI uint8_t data = 0xA5; uint8_t receivedData; SPI1_Open(SPI1_DEFAULT); receivedData = SPI1_ByteExchange(data); SPI1_Close(); } }
Use Case: Client Mode Transmit a Block of Data via SPI
This example shows how to setup Client Mode to Transmit a block of data via the SPI peripheral.
-
Add SPIx
-
Software Settings:
-
Custome Name: SPI1
-
-
Hardware Settings:
-
Mode: Client
-
SPI Mode: Mode 0
-
-
Interrupt Settings:
-
Enable SPI Interrupt: Disabled
-
-
Dependency Selection:
-
SPI Dependency Selection: SPI1
-
-
Pin Grid View:
-
Select PINS for SCKx, SDIx and SDOx
-
-
PINS:
-
Name pins SCKx, SDIx and SDOx as SCK, SDI and SDO.
-
uint32_t bufferSize = 64; volatile uint8_t dataBufPtr[] = { 0x40, 0x3F, 0x3E, 0x3D, 0x3C, 0x3B, 0x3A, 0x39, 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x31, 0x30, 0x2F, 0x2E, 0x2D, 0x2C, 0x2B, 0x2A, 0x29, 0x28, 0x27, 0x26, 0x25, 0x24, 0x23, 0x22, 0x21, 0x20, 0x1F, 0x1E, 0x1D, 0x1C, 0x1B, 0x1A, 0x19, 0x18, 0x17, 0x16, 0x15, 0x14, 0x13, 0x12, 0x11, 0x10, 0x0F, 0x0E, 0x0D, 0x0C, 0x0B, 0x0A, 0x09, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01 }; int main(void) { SYSTEM_Initialize(); SPI1_Close(); SPI1_Open(SPI1_DEFAULT); SPI1_BufferExchange(dataBufPtr, bufferSize); SPI1_Close(); while(1) { } }
Use Case: Client Mode Receive a Block of Data via SPI
This example shows how to Setup Client Mode and to Receive a block of data via the SPI peripheral.
-
Add SPIx
-
Software Settings:
-
Custome Name: SPI1
-
-
Hardware Settings:
-
Mode: Client
-
SPI Mode: Mode 0
-
-
Interrupt Settings:
-
Enable SPI Interrupt: Disabled
-
-
Dependency Selection:
-
SPI Dependency Selection: SPI1
-
-
Pin Grid View:
-
Select PINS for SCKx, SDIx and SDOx
-
-
PINS:
-
Name pins SCKx, SDIx and SDOx as SCK, SDI and SDO.
-
After configuring the components as described above, click 'Generate' to generate code. Then add the following code snippets to your application:
#include "mcc_generated_files/system/system.h"
int main(void) { SYSTEM_Initialize(); SPI1_Close(); while(1) { uint8_t receivedData; SPI1_Open(SPI1_DEFAULT); receivedData = SPI1_ByteRead(); SPI1_Close(); } }
Use Case: Client Mode Exchange a Block of Data via SPI
This example shows how to setup Client Mode and to Exchange a block of data via the SPI peripheral.
-
Add SPIx
-
Software Settings:
-
Custome Name: SPI1
-
-
Hardware Settings:
-
Mode: Client
-
SPI Mode: Mode 0
-
-
Interrupt Settings:
-
Enable SPI Interrupt: Disabled
-
-
Dependency Selection:
-
SPI Dependency Selection: SPI1
-
-
Pin Grid View:
-
Select PINS for SCKx, SDIx and SDOx
-
-
PINS:
-
Name pins SCKx, SDIx and SDOx as SCK, SDI and SDO..
-
After configuring the components as described above, click 'Generate' to generate code. Then add the following code snippets to your application:
#include "mcc_generated_files/system/system.h"
uint32_t bufferSize = 64; volatile uint8_t dataBufPtr[] = { 0x40, 0x3F, 0x3E, 0x3D, 0x3C, 0x3B, 0x3A, 0x39, 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x31, 0x30, 0x2F, 0x2E, 0x2D, 0x2C, 0x2B, 0x2A, 0x29, 0x28, 0x27, 0x26, 0x25, 0x24, 0x23, 0x22, 0x21, 0x20, 0x1F, 0x1E, 0x1D, 0x1C, 0x1B, 0x1A, 0x19, 0x18, 0x17, 0x16, 0x15, 0x14, 0x13, 0x12, 0x11, 0x10, 0x0F, 0x0E, 0x0D, 0x0C, 0x0B, 0x0A, 0x09, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01 }; int main(void) { SYSTEM_Initialize(); SPI1_Close(); SPI1_Open(SPI1_DEFAULT); SPI1_BufferExchange(dataBufPtr, bufferSize); SPI1_Close(); while(1) { } }