Serial Peripheral Interface - Peripheral Abstraction Layer.
The SPI PAL driver allows communication on an SPI bus. It was designed to be portable across all platforms and IPs which support SPI communication.
How to integrate DSPI in your application
Unlike the other drivers, SPI PAL modules need to include a configuration file named spi_pal_cfg.h, which allows the user to specify which IPSs are used and how many resources are allocated for each of them (state structures). The following code example shows how to configure one instance for each available SPI IPs.
#ifndef SPI_PAL_cfg_H
#define SPI_PAL_cfg_H
#define SPI_OVER_LPSPI
#define SPI_OVER_FLEXIO
#define SPI_OVER_DSPI
#define NO_OF_LPSPI_INSTS_FOR_SPI 1U
#define NO_OF_FLEXIO_INSTS_FOR_SPI 1U
#define NO_OF_DSPI_INSTS_FOR_SPI 1U
#endif
The following table contains the matching between platforms and available IPs
IP/MCU | S32K142 | S32K144 | S32K148 | MPC5748G | MPC5746C |
LPSPI | YES | YES | YES | NO | NO |
LPSPI | YES | YES | YES | NO | NO |
DSPI | NO | NO | NO | YES | YES |
In order to use the SPI driver it must be first initialized in either master or slave mode, using functions SPI_MasterInit() or SPI_SlaveInit(). Once initialized, it cannot be initialized again for the same SPI module instance until it is de-initialized, using SPI_SlaveDeinit() or SPI_MasterDeinit. Different SPI module instances can work independently of each other.
In each mode (master/slave) are available two types of transfers: blocking and non-blocking. The functions which initiate blocking transfers will configure the time out for transmission. If time expires SPI_MasterTransferBlocking() or SPI_SlaveTransferBlocking() will return error and the transmission will be aborted.
The configuration structure includes a special field named extension. It will be used only for SPI transfers over FLEXIO and should contain a pointer to extension_flexio_for_spi_t structure. The purpose of this structure is to configure which FLEXIO pins are used by the applications and their functionality (MISO, MOSI, SCK, SS).
Important Notes
- The driver enables the interrupts for the corresponding module, but any interrupt priority setting must be done by the application.
Example code
{
.frameSize = 8,
.rxDMAChannel = 255,
.txDMAChannel = 255,
.callback = NULL,
.callbackParam = NULL,
.ssPin = 0,
.extension = NULL
};
spi0_MasterConfig0.extension = &extension;
uint8_t tx[5] = {1,2,3,4,5};
uint8_t rx[5];
|
status_t | SPI_MasterInit (spi_instance_t instance, spi_master_t *config) |
| Initializes the SPI module in master mode. More...
|
|
status_t | SPI_SlaveInit (spi_instance_t instance, spi_slave_t *config) |
| Initializes the SPI module in slave mode. More...
|
|
status_t | SPI_SetSS (spi_instance_t, uint8_t ss) |
| Update the SS. More...
|
|
status_t | SPI_MasterTransfer (spi_instance_t instance, void *txBuffer, void *rxBuffer, uint16_t numberOfFrames) |
| Initializes a non-blocking master transfer. More...
|
|
status_t | SPI_MasterTransferBlocking (spi_instance_t instance, void *txBuffer, void *rxBuffer, uint16_t numberOfFrames, uint16_t timeout) |
| Initializes a blocking master transfer. More...
|
|
status_t | SPI_SlaveTransfer (spi_instance_t instance, void *txBuffer, void *rxBuffer, uint16_t numberOfFrames) |
| Initializes a non-blocking slave transfer. More...
|
|
status_t | SPI_SlaveTransferBlocking (spi_instance_t instance, void *txBuffer, void *rxBuffer, uint16_t numberOfFrames, uint16_t timeout) |
| Initializes a blocking slave transfer. More...
|
|
status_t | SPI_GetStatus (spi_instance_t instance) |
| Gets the status of the last transfer. More...
|
|
status_t | SPI_GetDefaultMasterConfig (spi_master_t *config) |
| Gets the default configuration structure for master. More...
|
|
status_t | SPI_GetDefaultSlaveConfig (spi_slave_t *config) |
| Gets the default configuration structure for slave. More...
|
|
status_t | SPI_MasterDeinit (spi_instance_t instance) |
| De-initializes the spi master module. More...
|
|
status_t | SPI_SlaveDeinit (spi_instance_t instance) |
| De-initializes the spi slave module. More...
|
|
Defines the edges used for sampling and shifting.
Enumerator |
---|
READ_ON_ODD_EDGE |
The SPI signal is read on odd edges of SCK and counting starts after SS activation
|
READ_ON_EVEN_EDGE |
The SPI signal is read on even edges of SCK and counting starts after SS activation
|
Definition at line 60 of file spi_pal.h.
Defines the polarity of signals.
Enumerator |
---|
SPI_ACTIVE_HIGH |
The signal is active high
|
SPI_ACTIVE_LOW |
The signal is active low
|
Definition at line 51 of file spi_pal.h.
Defines the bit order.
Enumerator |
---|
SPI_TRANSFER_MSB_FIRST |
Transmit data starting with most significant bit
|
SPI_TRANSFER_LSB_FIRST |
Transmit data starting with least significant bit
|
Definition at line 69 of file spi_pal.h.
Defines the mechanism to update the rx or tx buffers.
Enumerator |
---|
SPI_USING_DMA |
The driver will use DMA to perform SPI transfer
|
SPI_USING_INTERRUPTS |
The driver will use interrupts to perform SPI transfer
|
Definition at line 42 of file spi_pal.h.
Gets the default configuration structure for master.
The default configuration structure is:
- Parameters
-
[out] | config | Pointer to configuration structure |
- Returns
- Error or success status returned by API
Definition at line 453 of file spi_pal.c.
Gets the default configuration structure for slave.
The default configuration structure is:
- Parameters
-
[out] | config | Pointer to configuration structure |
- Returns
- Error or success status returned by API
Definition at line 477 of file spi_pal.c.
Gets the status of the last transfer.
This function return the status of the last transfer. Using this function the user can check if the transfer is still in progress or if time-out event occurred.
- Parameters
-
[in] | instance | The name of the instance |
[in] | txBuffer | Pointer to tx buffer. |
[in] | rxBuffer | Pointer to rx buffer. |
[in] | numberOfFrames | Number of frames sent/received |
[in] | timeout | Transfer time-out in ms |
- Returns
- Error or success status returned by API
Definition at line 612 of file spi_pal.c.
De-initializes the spi master module.
This function de-initialized the spi master module.
- Parameters
-
[in] | instance | The name of the instance |
- Returns
- Error or success status returned by API
Definition at line 499 of file spi_pal.c.
Initializes the SPI module in master mode.
This function initializes and enables the requested SPI module in master mode, configuring the bus parameters.
- Parameters
-
[in] | instance | The name of the instance |
[in] | config | The configuration structure |
- Returns
- Error or success status returned by API
Definition at line 137 of file spi_pal.c.
status_t SPI_MasterTransfer |
( |
spi_instance_t |
instance, |
|
|
void * |
txBuffer, |
|
|
void * |
rxBuffer, |
|
|
uint16_t |
numberOfFrames |
|
) |
| |
Initializes a non-blocking master transfer.
This function initializes a non-blocking master transfer.
- Parameters
-
[in] | instance | The name of the instance |
[in] | txBuffer | Pointer to tx buffer. |
[in] | rxBuffer | Pointer to rx buffer. |
[in] | numberOfFrames | Number of frames sent/received |
- Returns
- Error or success status returned by API
Definition at line 233 of file spi_pal.c.
status_t SPI_MasterTransferBlocking |
( |
spi_instance_t |
instance, |
|
|
void * |
txBuffer, |
|
|
void * |
rxBuffer, |
|
|
uint16_t |
numberOfFrames, |
|
|
uint16_t |
timeout |
|
) |
| |
Initializes a blocking master transfer.
This function initializes a blocking master transfer.
- Parameters
-
[in] | instance | The name of the instance |
[in] | txBuffer | Pointer to tx buffer. |
[in] | rxBuffer | Pointer to rx buffer. |
[in] | numberOfFrames | Number of frames sent/received |
[in] | timeout | Transfer time-out in ms |
- Returns
- Error or success status returned by API
Definition at line 266 of file spi_pal.c.
Update the SS.
This function changes the SS, if this feature is available.
- Parameters
-
[in] | instance | The name of the instance |
[in] | ss | The number of SS |
- Returns
- Error or success status returned by API
Definition at line 577 of file spi_pal.c.
De-initializes the spi slave module.
This function de-initialized the spi slave module.
- Parameters
-
[in] | instance | The name of the instance |
- Returns
- Error or success status returned by API
Definition at line 544 of file spi_pal.c.
Initializes the SPI module in slave mode.
This function initializes and enables the requested SPI module in slave mode, configuring the bus parameters.
- Parameters
-
[in] | instance | The name of the instance |
[in] | config | The configuration structure |
- Returns
- Error or success status returned by API
Definition at line 299 of file spi_pal.c.
status_t SPI_SlaveTransfer |
( |
spi_instance_t |
instance, |
|
|
void * |
txBuffer, |
|
|
void * |
rxBuffer, |
|
|
uint16_t |
numberOfFrames |
|
) |
| |
Initializes a non-blocking slave transfer.
This function initializes a non-blocking slave transfer.
- Parameters
-
[in] | instance | The name of the instance |
[in] | txBuffer | Pointer to tx buffer. |
[in] | rxBuffer | Pointer to rx buffer. |
[in] | numberOfFrames | Number of frames sent/received |
- Returns
- Error or success status returned by API
Definition at line 387 of file spi_pal.c.
status_t SPI_SlaveTransferBlocking |
( |
spi_instance_t |
instance, |
|
|
void * |
txBuffer, |
|
|
void * |
rxBuffer, |
|
|
uint16_t |
numberOfFrames, |
|
|
uint16_t |
timeout |
|
) |
| |
Initializes a blocking slave transfer.
This function initializes a blocking slave transfer.
- Parameters
-
[in] | instance | The name of the instance |
[in] | txBuffer | Pointer to tx buffer. |
[in] | rxBuffer | Pointer to rx buffer. |
[in] | numberOfFrames | Number of frames sent/received |
[in] | timeout | Transfer time-out in ms |
- Returns
- Error or success status returned by API
Definition at line 420 of file spi_pal.c.