Serial Peripheral Interface (SPI)

## Data Structures

struct | spi_master_config_t
| SPI master user configure structure. More...
struct | spi_slave_config_t
| SPI slave user configure structure. More...
struct | spi_transfer_t
| SPI transfer structure. More...
struct | spi_master_handle_t
| SPI transfer handle structure. More...

## Typedefs

typedef spi_master_handle_t | spi_slave_handle_t
| Slave handle is the same with master handle.
typedef void(* | spi_master_callback_t) (SPI_Type *base, spi_master_handle_t *handle, status_t status, void *userData)
| SPI master callback for finished transmit.
typedef void(* | spi_slave_callback_t) (SPI_Type *base, spi_slave_handle_t *handle, status_t status, void *userData)
| SPI master callback for finished transmit.

## Enumerations

enum | _spi_status { kStatus_SPI_Busy = MAKE_STATUS(kStatusGroup_SPI, 0), kStatus_SPI_Idle = MAKE_STATUS(kStatusGroup_SPI, 1), kStatus_SPI_Error = MAKE_STATUS(kStatusGroup_SPI, 2) }
| Return status for the SPI driver. More...
enum | spi_clock_polarity_t { kSPI_ClockPolarityActiveHigh = 0x0U, kSPI_ClockPolarityActiveLow }
| SPI clock polarity configuration. More...
enum | spi_clock_phase_t { kSPI_ClockPhaseFirstEdge = 0x0U, kSPI_ClockPhaseSecondEdge }
| SPI clock phase configuration. More...
enum | spi_shift_direction_t { kSPI_MsbFirst = 0x0U, kSPI_LsbFirst }
| SPI data shifter direction options. More...
enum | spi_ss_output_mode_t { kSPI_SlaveSelectAsGpio = 0x0U, kSPI_SlaveSelectFaultInput = 0x2U, kSPI_SlaveSelectAutomaticOutput = 0x3U }
| SPI slave select output mode options. More...
enum | spi_pin_mode_t { kSPI_PinModeNormal = 0x0U, kSPI_PinModeInput = 0x1U, kSPI_PinModeOutput = 0x3U }
| SPI pin mode options. More...
enum | spi_data_bitcount_mode_t { kSPI_8BitMode = 0x0U, kSPI_16BitMode }
| SPI data length mode options. More...
enum | _spi_interrupt_enable { kSPI_RxFullAndModfInterruptEnable = 0x1U, kSPI_TxEmptyInterruptEnable = 0x2U, kSPI_MatchInterruptEnable = 0x4U, kSPI_RxFifoNearFullInterruptEnable = 0x8U, kSPI_TxFifoNearEmptyInterruptEnable = 0x10U }
| SPI interrupt sources. More...

enum

_spi_flags {
kSPI_RxBufferFullFlag = SPI_S_SPRF_MASK,
kSPI_MatchFlag = SPI_S_SPMF_MASK,
kSPI_TxBufferEmptyFlag = SPI_S_SPTEF_MASK,
kSPI_ModeFaultFlag = SPI_S_MODF_MASK,
kSPI_RxFifoNearFullFlag = SPI_S_RNFULLF_MASK,
kSPI_TxFifoNearEmptyFlag = SPI_S_TNEAREF_MASK,
kSPI_RxFifoFullFlag = SPI_S_TXFULLF_MASK,
kSPI_TxFifoEmptyFlag = SPI_S_RFIFOEF_MASK,
kSPI_TxFifoError = SPI_CI_TXFERR_MASK << 8U,
kSPI_RxFifoError = SPI_CI_RXFERR_MASK << 8U,
kSPI_TxOverflow = SPI_CI_TXFOF_MASK << 8U,
kSPI_RxOverflow
}

| SPI status flags. More...
enum | spi_w1c_interrupt_t { kSPI_RxFifoFullClearInterrupt = SPI_CI_SPRFCI_MASK, kSPI_TxFifoEmptyClearInterrupt = SPI_CI_SPTEFCI_MASK, kSPI_RxNearFullClearInterrupt = SPI_CI_RNFULLFCI_MASK, kSPI_TxNearEmptyClearInterrupt = SPI_CI_TNEAREFCI_MASK }
| SPI FIFO write-1-to-clear interrupt flags. More...
enum | spi_txfifo_watermark_t { kSPI_TxFifoOneFourthEmpty = 0, kSPI_TxFifoOneHalfEmpty = 1 }
| SPI TX FIFO watermark settings. More...
enum | spi_rxfifo_watermark_t { kSPI_RxFifoThreeFourthsFull = 0, kSPI_RxFifoOneHalfFull = 1 }
| SPI RX FIFO watermark settings. More...
enum | _spi_dma_enable_t { kSPI_TxDmaEnable = SPI_C2_TXDMAE_MASK, kSPI_RxDmaEnable = SPI_C2_RXDMAE_MASK, kSPI_DmaAllEnable = (SPI_C2_TXDMAE_MASK | SPI_C2_RXDMAE_MASK) }
| SPI dma source. More...

## Initialization and deinitialization

void | SPI_MasterGetDefaultConfig (spi_master_config_t *config)
| Set the SPI master configuration structure to default values. More...
void | SPI_MasterInit (SPI_Type *base, const spi_master_config_t *config, uint32_t srcClock_Hz)
| Initialize SPI with master configuration. More...
void | SPI_SlaveGetDefaultConfig (spi_slave_config_t *config)
| Set the SPI slave configuration structure to default values. More...
void | SPI_SlaveInit (SPI_Type *base, const spi_slave_config_t *config)
| Initialize SPI with slave configuration. More...
void | SPI_Deinit (SPI_Type *base)
| De-initialize SPI. More...
static void | SPI_Enable (I2C_Type *base, bool enable)
| Enable or disables the SPI. More...

## Status

uint32_t | SPI_GetStatusFlags (SPI_Type *base)
| Get the status flag. More...
static void | SPI_ClearInterrupt (SPI_Type *base, uint32_t mask)
| Clear the interrupt if enable INCTLR. More...

## Interrupts

void | SPI_EnableInterrupts (SPI_Type *base, uint32_t mask)
| Enable interrupt for SPI. More...
void | SPI_DisableInterrupts (SPI_Type *base, uint32_t mask)
| Disable interrupt for SPI. More...

## DMA Control

static void | SPI_EnableDMA (SPI_Type *base, uint32_t mask, bool enable)
| Enable dma source for SPI. More...
static uint32_t | SPI_GetDataRegisterAddress (SPI_Type *base)
| Get SPI tx/rx data register address. More...

## Bus Operations

void | SPI_MasterSetBaudRate (SPI_Type *base, uint32_t baudRate_Bps, uint32_t srcClock_Hz)
| Set Baud rate for SPI transfer, this only used in master. More...
static void | SPI_SetMatchData (SPI_Type *base, uint32_t matchData)
| Set match data for SPI. More...
void | SPI_EnableFIFO (SPI_Type *base, bool enable)
| Enable or Disable FIFO if there is FIFO. More...
void | SPI_WriteBlocking (SPI_Type *base, uint8_t *buffer, size_t size)
| Sends a buffer of data bytes in blocking way. More...
void | SPI_WriteData (SPI_Type *base, uint16_t data)
| Write a data into SPI data register. More...
uint16_t | SPI_ReadData (SPI_Type *base)
| Get a data from SPI data register. More...

## Transactional

void | SPI_MasterTransferCreateHandle (SPI_Type *base, spi_master_handle_t *handle, spi_master_callback_t callback, void *userData)
| Initialize the SPI master handle. More...
status_t | SPI_MasterTransferBlocking (SPI_Type *base, spi_transfer_t *xfer)
| Transfer a block of data in polling way. More...
status_t | SPI_MasterTransferNonBlocking (SPI_Type *base, spi_master_handle_t *handle, spi_transfer_t *xfer)
| Performs a non-blocking SPI interrupt transfer. More...
status_t | SPI_MasterTransferGetCount (SPI_Type *base, spi_master_handle_t *handle, size_t *count)
| Get the bytes of SPI Interrupt transferred. More...
void | SPI_MasterTransferAbort (SPI_Type *base, spi_master_handle_t *handle)
| Abort a SPI transfer using interrupt. More...
void | SPI_MasterTransferHandleIRQ (SPI_Type *base, spi_master_handle_t *handle)
| Interrupt handler for SPI. More...
static void | SPI_SlaveTransferCreateHandle (SPI_Type *base, spi_slave_handle_t *handle, spi_slave_callback_t callback, void *userData)
| Initialize the SPI slave handle. More...
static status_t | SPI_SlaveTransferNonBlocking (SPI_Type *base, spi_slave_handle_t *handle, spi_transfer_t *xfer)
| Performs a non-blocking SPI slave interrupt transfer. More...
static status_t | SPI_SlaveTransferGetCount (SPI_Type *base, spi_slave_handle_t *handle, size_t *count)
| Get the bytes of SPI Interrupt transferred. More...
static void | SPI_SlaveTransferAbort (SPI_Type *base, spi_slave_handle_t *handle)
| Abort a SPI slave transfer using interrupt. More...
static void | SPI_SlaveTransferHandleIRQ (SPI_Type *base, spi_slave_handle_t *handle)
| Interrupt handler for SPI slave. More...

## Usage Information

The KSDK provides a periphera driver for the Serial Peripheral Interface (SPI) module of Kinetis devices.

# Overview

SPI driver includes functional APIs and transactional APIs.

Functional APIs are feature/property target low level APIs. Functional APIs can be used for SPI initialization/configuration/operation for optimization/customization purpose. Using the functional API requires the knowledge of the SPI peripheral and how to organize functional APIs to meet the application requirements. All functional API use the peripheral base address as the first parameter. SPI functional operation groups provide the functional API set.

Transactional APIs are transaction target high level APIs. Transactional APIs can be used to enable the peripheral and in the application if the code size and performance of transactional APIs satisfy the requirements. If the code size and performance are a critical requirement, see the transactional API implementation and write a custom code. All transactional APIs use the spi_handle_t as the first parameter. Initialize the handle by calling the SPI_MasterTransferCreateHandle() or SPI_SlaveTransferCreateHandle() API.

Transactional APIs support asynchronous transfer. This means that the functions SPI_MasterTransferNonBlocking() and SPI_SlaveTransferNonBlocking() set up the interrupt for data transfer. When the transfer completes, the upper layer is notified through a callback function with the kStatus_SPI_Idle status.

# Function groups

## Initialization and deinitialization

The SPI_MasterInit() and SPI_SlaveInit() functions initialize SPI to be a master or slave with specific configurations. The SPI_MasterGetDefaultConfig() and SPI_SlaveGetDefaultConfig() functions get the default configurations.

## Status

Provides functions to get and clear the SPI status.

## Interrupt

Provides functions to enable/disable SPI interrupts.

## DMA Control

Provides functions to enable/disable SPI to generate the DMA request if the chip support SPI DMA feature.

## Bus Operations

Provides functions to set baud rate, enable/disable transfer, to write/read data.

## Transactional

Provides functions to transfer data using an interrupt or a DMA method.

# Typical use case

## SPI master transfer using an interrupt method

#define BUFFER_LEN (64)
spi_master_handle_t spiHandle;
spi_master_config_t masterConfig;
spi_transfer_t xfer;
volatile bool isFinished = false;
const uint8_t sendData[BUFFER_LEN] = [......];
uint8_t receiveBuff[BUFFER_LEN];
void SPI_UserCallback(SPI_Type *base, spi_master_handle_t *handle, status_t status, void *userData)
{
isFinished = true;
}
void main(void)
{
//...
SPI_MasterGetDefaultConfig(&masterConfig);
SPI_MasterInit(SPI0, &masterConfig);
SPI_MasterTransferCreateHandle(SPI0, &spiHandle, SPI_UserCallback, NULL);
// Prepare to send.
xfer.txData = sendData;
xfer.rxData = receiveBuff;
xfer.dataSize = BUFFER_LEN;
// Send out.
SPI_MasterTransferNonBlocking(SPI0, &spiHandle, &xfer);
// Wait send finished.
while (!isFinished)
{
}
// ...
}

## SPI Send/receive using a DMA method

#define BUFFER_LEN (64)
spi_dma_handle_t spiHandle;
dma_handle_t g_spiTxDmaHandle;
dma_handle_t g_spiRxDmaHandle;
spi_config_t masterConfig;
spi_transfer_t xfer;
volatile bool isFinished;
uint8_t sendData[BUFFER_LEN] = ...;
uint8_t receiveBuff[BUFFER_LEN];
void SPI_UserCallback(SPI_Type *base, spi_dma_handle_t *handle, status_t status, void *userData)
{
isFinished = true;
}
void main(void)
{
//...
SPI_MasterGetDefaultConfig(&masterConfig);
SPI_MasterInit(SPI0, &masterConfig);
// Sets up the DMA.
DMAMUX_Init(DMAMUX0);
DMAMUX_SetSource(DMAMUX0, SPI_TX_DMA_CHANNEL, SPI_TX_DMA_REQUEST);
DMAMUX_EnableChannel(DMAMUX0, SPI_TX_DMA_CHANNEL);
DMAMUX_SetSource(DMAMUX0, SPI_RX_DMA_CHANNEL, SPI_RX_DMA_REQUEST);
DMAMUX_EnableChannel(DMAMUX0, SPI_RX_DMA_CHANNEL);
DMA_Init(DMA0);
/* Creates the DMA handle. */
DMA_CreateHandle(&g_spiTxDmaHandle, DMA0, SPI_TX_DMA_CHANNEL);
DMA_CreateHandle(&g_spiRxDmaHandle, DMA0, SPI_RX_DMA_CHANNEL);
SPI_MasterTransferCreateHandleDMA(SPI0, spiHandle, &g_spiTxDmaHandle, &g_spiRxDmaHandle, SPI_UserCallback, NULL);
// Prepares to send.
xfer.txData = sendData;
xfer.rxData = receiveBuff;
xfer.dataSize = BUFFER_LEN;
// Sends out.
SPI_MasterTransferDMA(SPI0, &spiHandle, &xfer);
// Waits for send to complete.
while (!isFinished)
{
}
// ...
}

## Data Structure Documentation

struct spi_master_config_t

SPI master user configure structure.

Data Fields
uint32_t | baudRate_Bps | Baud Rate for SPI in Hz
spi_data_bitcount_mode_t | dataMode | 8bit or 16bit mode
spi_shift_direction_t | direction | MSB or LSB
bool | enableFifoInterruptClear | Enable interrupt clear mechanism
bool | enableMaster | Enable SPI at init time
bool | enableStopInWaitMode | SPI stop in wait mode
spi_ss_output_mode_t | outputMode | SS pin setting
spi_clock_phase_t | phase | Clock phase
spi_pin_mode_t | pinMode | SPI pin mode select
spi_clock_polarity_t | polarity | Clock polarity
spi_rxfifo_watermark_t | rxWatermark | Rx watermark settings
spi_txfifo_watermark_t | txWatermark | Tx watermark settings

struct spi_slave_config_t

SPI slave user configure structure.

Data Fields
spi_data_bitcount_mode_t | dataMode | 8bit or 16bit mode
spi_shift_direction_t | direction | MSB or LSB
bool | enableFifoInterruptClear | Enable interrupt clear mechanism
bool | enableSlave | Enable SPI at init time
bool | enableStopInWaitMode | SPI stop in wait mode
spi_clock_phase_t | phase | Clock phase
spi_clock_polarity_t | polarity | Clock polarity
spi_rxfifo_watermark_t | rxWatermark | Rx watermark settings
spi_txfifo_watermark_t | txWatermark | Tx watermark settings

struct spi_transfer_t

SPI transfer structure.

Data Fields
size_t | dataSize | Transfer bytes
uint32_t | flags | SPI control flag, useless to SPI.
uint8_t * | rxData | Receive buffer
uint8_t * | txData | Send buffer

struct _spi_master_handle

SPI transfer handle structure.

Data Fields
uint8_t | bytePerFrame | SPI mode, 2bytes or 1byte in a frame
spi_master_callback_t | callback | SPI callback
uint8_t *volatile | rxData | Receive buffer
volatile size_t | rxRemainingBytes | Receive data remaining in bytes
volatile uint32_t | state | SPI interanl state
size_t | transferSize | Bytes to be transferred
uint8_t *volatile | txData | Transfer buffer
volatile size_t | txRemainingBytes | Send data remaining in bytes
void * | userData | Cllback parameter
uint8_t | watermark | Watermark value for SPI transfer

## Enumeration Type Documentation

enum _spi_dma_enable_t

SPI dma source.

Enumerator
kSPI_TxDmaEnable | Tx dma request source
kSPI_RxDmaEnable | Rx dma request source
kSPI_DmaAllEnable | All dma request source

enum _spi_flags

SPI status flags.

Enumerator
kSPI_RxBufferFullFlag | Read buffer full flag
kSPI_MatchFlag | Match flag
kSPI_TxBufferEmptyFlag | Transmit buffer empty flag
kSPI_ModeFaultFlag | Mode fault flag
kSPI_RxFifoNearFullFlag | Rx FIFO near full
kSPI_TxFifoNearEmptyFlag | Tx FIFO near empty
kSPI_RxFifoFullFlag | Rx fifo full
kSPI_TxFifoEmptyFlag | Tx fifo empty
kSPI_TxFifoError | Tx FIFO error
kSPI_RxFifoError | Rx FIFO Overflow
kSPI_TxOverflow | Tx FIFO Overflow
kSPI_RxOverflow | Rx FIFO Overflow

enum _spi_interrupt_enable

SPI interrupt sources.

Enumerator
kSPI_RxFullAndModfInterruptEnable | Receive buffer full (SPRF) and mode fault (MODF) interrupt
kSPI_TxEmptyInterruptEnable | Transmit buffer empty interrupt
kSPI_MatchInterruptEnable | Match interrupt
kSPI_RxFifoNearFullInterruptEnable | Receive FIFO nearly full interrupt
kSPI_TxFifoNearEmptyInterruptEnable | Transmit FIFO nearly empty interrupt

enum _spi_status

Return status for the SPI driver.

Enumerator
kStatus_SPI_Busy | SPI bus is busy
kStatus_SPI_Idle | SPI is idle
kStatus_SPI_Error | SPI error

enum spi_clock_phase_t

SPI clock phase configuration.

Enumerator
kSPI_ClockPhaseFirstEdge | First edge on SPSCK occurs at the middle of the first cycle of a data transfer.
kSPI_ClockPhaseSecondEdge | First edge on SPSCK occurs at the start of the first cycle of a data transfer.

enum spi_clock_polarity_t

SPI clock polarity configuration.

Enumerator
kSPI_ClockPolarityActiveHigh | Active-high SPI clock (idles low).
kSPI_ClockPolarityActiveLow | Active-low SPI clock (idles high).

enum spi_data_bitcount_mode_t

SPI data length mode options.

Enumerator
kSPI_8BitMode | 8-bit data transmission mode
kSPI_16BitMode | 16-bit data transmission mode

enum spi_pin_mode_t

SPI pin mode options.

Enumerator
kSPI_PinModeNormal | Pins operate in normal, single-direction mode.
kSPI_PinModeInput | Bidirectional mode. Master: MOSI pin is input; Slave: MISO pin is input.
kSPI_PinModeOutput | Bidirectional mode. Master: MOSI pin is output; Slave: MISO pin is output.

enum spi_rxfifo_watermark_t

SPI RX FIFO watermark settings.

Enumerator
kSPI_RxFifoThreeFourthsFull | SPI rx watermark at 3/4 FIFO size
kSPI_RxFifoOneHalfFull | SPI rx watermark at 1/2 FIFO size

enum spi_shift_direction_t

SPI data shifter direction options.

Enumerator
kSPI_MsbFirst | Data transfers start with most significant bit.
kSPI_LsbFirst | Data transfers start with least significant bit.

enum spi_ss_output_mode_t

SPI slave select output mode options.

Enumerator
kSPI_SlaveSelectAsGpio | Slave select pin configured as GPIO.
kSPI_SlaveSelectFaultInput | Slave select pin configured for fault detection.
kSPI_SlaveSelectAutomaticOutput | Slave select pin configured for automatic SPI output.

enum spi_txfifo_watermark_t

SPI TX FIFO watermark settings.

Enumerator
kSPI_TxFifoOneFourthEmpty | SPI tx watermark at 1/4 FIFO size
kSPI_TxFifoOneHalfEmpty | SPI tx watermark at 1/2 FIFO size

enum spi_w1c_interrupt_t

SPI FIFO write-1-to-clear interrupt flags.

Enumerator
kSPI_RxFifoFullClearInterrupt | Receive FIFO full interrupt
kSPI_TxFifoEmptyClearInterrupt | Transmit FIFO empty interrupt
kSPI_RxNearFullClearInterrupt | Receive FIFO nearly full interrupt
kSPI_TxNearEmptyClearInterrupt | Transmit FIFO nearly empty interrupt

## Function Documentation

static void SPI_ClearInterrupt | ( | SPI_Type * | base,
|  | uint32_t | mask
| ) |  |

inlinestatic

Clear the interrupt if enable INCTLR.

Parameters

  base | SPI base pointer

  interrupt

  Interrupt need to be cleared The parameter could be any combination of the following values:
  - kSPIRxFifoFullClearInt
  - kSPITxFifoEmptyClearInt
  - kSPIRxNearFullClearInt
  - kSPITxNearEmptyClearInt

void SPI_Deinit | ( | SPI_Type * | base | ) |

De-initialize SPI.

Call this API will reset the SPI module, gate the spi clock , so the SPI module can not work unless call SPI_MasterInit/SPI_SlaveInit to initialize module.

Parameters

  base | SPI base pointer

void SPI_DisableInterrupts | ( | SPI_Type * | base,
|  | uint32_t | mask
| ) |  |

Disable interrupt for SPI.

Parameters

  base | SPI base pointer

  mask

  SPI interrupt source. The parameter could be any combination of the following values:
  - kSPI_RxFullAndModfInterruptEnable
  - kSPI_TxEmptyInterruptEnable
  - kSPI_MatchInterruptEnable
  - kSPI_RxFifoNearFullInterruptEnable
  - kSPI_TxFifoNearEmptyInterruptEnable

static void SPI_Enable | ( | I2C_Type * | base,
|  | bool | enable
| ) |  |

inlinestatic

Enable or disables the SPI.

Parameters

  base | SPI base pointer
  enable | pass true to enable module, false to disable module

static void SPI_EnableDMA | ( | SPI_Type * | base,
|  | uint32_t | mask,
|  | bool | enable
| ) |  |

inlinestatic

Enable dma source for SPI.

Parameters

  base | SPI base pointer
  source | SPI dma source.
  enable | True means enable DMA, false means disable DMA

void SPI_EnableFIFO | ( | SPI_Type * | base,
|  | bool | enable
| ) |  |

Enable or Disable FIFO if there is FIFO.

Parameters

  base | SPI base pointer
  enable | True means enable FIFO, false means disable FIFO.

void SPI_EnableInterrupts | ( | SPI_Type * | base,
|  | uint32_t | mask
| ) |  |

Enable interrupt for SPI.

Parameters

  base | SPI base pointer

  mask

  SPI interrupt source. The parameter could be any combination of the following values:
  - kSPI_RxFullAndModfInterruptEnable
  - kSPI_TxEmptyInterruptEnable
  - kSPI_MatchInterruptEnable
  - kSPI_RxFifoNearFullInterruptEnable
  - kSPI_TxFifoNearEmptyInterruptEnable

static uint32_t SPI_GetDataRegisterAddress | ( | SPI_Type * | base | ) |

inlinestatic

Get SPI tx/rx data register address.

This API is used to provide transfer address for SPI DMA transfer configuration.

Parameters

  base | SPI base pointer

Returns
  data register address

uint32_t SPI_GetStatusFlags | ( | SPI_Type * | base | ) |

Get the status flag.

Parameters

  base | SPI base pointer

Returns
  SPI Status, use status flag to AND _spi_flags could get the related status.

void SPI_MasterGetDefaultConfig | ( | spi_master_config_t * | config | ) |

Set the SPI master configuration structure to default values.

The purpose of this API is to get the config structure initialized for use in SPI_MasterInit(). User may use the initialized structure unchanged in SPI_MasterInit(), or modify some fields of the structure before calling SPI_MasterInit(). After calling this API, The master is ready to transfer. Example:

spi_master_config_t config;
SPI_MasterGetDefaultConfig(&config);

Parameters

  config | pointer to master config structure

void SPI_MasterInit | ( | SPI_Type * | base,
|  | const spi_master_config_t * | config,
|  | uint32_t | srcClock_Hz
| ) |  |

Initialize SPI with master configuration.

The configuration structure can be filled by user from scratch, or be set with default values by SPI_MasterGetDefaultConfig(). After calling this API, the slave is ready to transfer. Example

spi_master_config_t config = {
.baudRate_Bps = 400000,
...
};
SPI_MasterInit(SPI0, &config);

Parameters

  base | SPI base pointer
  config | pointer to master config structure
  srcClock_Hz | Source clock frequency.

void SPI_MasterSetBaudRate | ( | SPI_Type * | base,
|  | uint32_t | baudRate_Bps,
|  | uint32_t | srcClock_Hz
| ) |  |

Set Baud rate for SPI transfer, this only used in master.

Parameters

  base | SPI base pointer
  baudRate_Bps | Buad Rate needed in Hz.
  srcClock_Hz | SPI source clock frequency in Hz.

void SPI_MasterTransferAbort | ( | SPI_Type * | base,
|  | spi_master_handle_t * | handle
| ) |  |

Abort a SPI transfer using interrupt.

Parameters

  base | SPI peripheral base address.
  handle | Pointer to SPI transfer handle, this should be a static variable.

status_t SPI_MasterTransferBlocking | ( | SPI_Type * | base,
|  | spi_transfer_t * | xfer
| ) |  |

Transfer a block of data in polling way.

Parameters

  base | SPI base pointer
  xfer | pointer to spi_xfer_config_t structure

Return values

  kStatus_Success | Successfully start a transfer.
  kStatus_InvalidArgument | Input argument is invalid.

void SPI_MasterTransferCreateHandle | ( | SPI_Type * | base,
|  | spi_master_handle_t * | handle,
|  | spi_master_callback_t | callback,
|  | void * | userData
| ) |  |

Initialize the SPI master handle.

This function initializes the SPI master handle which can be used for other SPI master transactional APIs. Usually, for a specified SPI instance, user need only call this API once to get the initialized handle.

Parameters

  base | SPI peripheral base address.
  handle | SPI handle pointer.
  callback | Callback function.
  userData | User data.

status_t SPI_MasterTransferGetCount | ( | SPI_Type * | base,
|  | spi_master_handle_t * | handle,
|  | size_t * | count
| ) |  |

Get the bytes of SPI Interrupt transferred.

Parameters

  base | SPI peripheral base address.
  handle | Pointer to SPI transfer handle, this should be a static variable.
  count | Transferred bytes of SPI master.

Return values

  kStatus_SPI_Success | Succeed get the transfer count.
  kStatus_NoTransferInProgress | There is not a non-blocking transaction currently in progress.

void SPI_MasterTransferHandleIRQ | ( | SPI_Type * | base,
|  | spi_master_handle_t * | handle
| ) |  |

Interrupt handler for SPI.

Parameters

  base | SPI peripheral base address.
  handle | pointer to spi_master_handle_t structure which stores the transfer state.

status_t SPI_MasterTransferNonBlocking | ( | SPI_Type * | base,
|  | spi_master_handle_t * | handle,
|  | spi_transfer_t * | xfer
| ) |  |

Performs a non-blocking SPI interrupt transfer.

Note
  Calling the API will immediately return after transfer intialize finished. Users need to call SPI_GetStatusIRQ() to get the transfer status

Parameters

  base | SPI peripheral base address.
  handle | pointer to spi_master_handle_t structure which stores the transfer state
  xfer | pointer to spi_xfer_config_t structure

Return values

  kStatus_Success | Successfully start a transfer.
  kStatus_InvalidArgument | Input argument is invalid.
  kStatus_SPI_Busy | SPI is not idle, is running another transfer.

uint16_t SPI_ReadData | ( | SPI_Type * | base | ) |

Get a data from SPI data register.

Parameters

  base | SPI base pointer

Returns
  Data in the register.

static void SPI_SetMatchData | ( | SPI_Type * | base,
|  | uint32_t | matchData
| ) |  |

inlinestatic

Set match data for SPI.

Match Data is a hardware compare value, when the value received in the SPI receive data buffer equals this hardware compare value, the SPI Match Flag in the S register (S[SPMF]) sets, this can also generate a interrupt if the enable bit sets.

Parameters

  base | SPI base pointer
  matchData | Match data.

void SPI_SlaveGetDefaultConfig | ( | spi_slave_config_t * | config | ) |

Set the SPI slave configuration structure to default values.

The purpose of this API is to get the config structure initialized for use in SPI_SlaveInit(). User need to modify some fields of the structure before calling SPI_SlaveInit(). Example:

spi_slave_config_t config;
SPI_SlaveGetDefaultConfig(&config);

Parameters

  config | pointer to slave config structure

void SPI_SlaveInit | ( | SPI_Type * | base,
|  | const spi_slave_config_t * | config
| ) |  |

Initialize SPI with slave configuration.

The configuration structure can be filled by user from scratch, or be set with default values by SPI_SlaveGetDefaultConfig(). After calling this API, the slave is ready to transfer. Example

spi_slave_config_t config = {
.polarity = kSPIClockPolarity_ActiveHigh;
.phase = kSPIClockPhase_FirstEdge;
.direction = kSPIMsbFirst;
...
};
SPI_MasterInit(SPI0, &config);

Parameters

  base | SPI base pointer
  config | pointer to master config structure

static void SPI_SlaveTransferAbort | ( | SPI_Type * | base,
|  | spi_slave_handle_t * | handle
| ) |  |

inlinestatic

Abort a SPI slave transfer using interrupt.

Parameters

  base | SPI peripheral base address.
  handle | Pointer to SPI transfer handle, this should be a static variable.

static void SPI_SlaveTransferCreateHandle | ( | SPI_Type * | base,
|  | spi_slave_handle_t * | handle,
|  | spi_slave_callback_t | callback,
|  | void * | userData
| ) |  |

inlinestatic

Initialize the SPI slave handle.

This function initializes the SPI slave handle which can be used for other SPI slave transactional APIs. Usually, for a specified SPI instance, user need only call this API once to get the initialized handle.

Parameters

  base | SPI peripheral base address.
  handle | SPI handle pointer.
  callback | Callback function.
  userData | User data.

static status_t SPI_SlaveTransferGetCount | ( | SPI_Type * | base,
|  | spi_slave_handle_t * | handle,
|  | size_t * | count
| ) |  |

inlinestatic

Get the bytes of SPI Interrupt transferred.

Parameters

  base | SPI peripheral base address.
  handle | Pointer to SPI transfer handle, this should be a static variable.
  count | Transferred bytes of SPI slave.

Return values

  kStatus_SPI_Success | Succeed get the transfer count.
  kStatus_NoTransferInProgress | There is not a non-blocking transaction currently in progress.

static void SPI_SlaveTransferHandleIRQ | ( | SPI_Type * | base,
|  | spi_slave_handle_t * | handle
| ) |  |

inlinestatic

Interrupt handler for SPI slave.

Parameters

  base | SPI peripheral base address.
  handle | pointer to spi_slave_handle_t structure which stores the transfer state

static status_t SPI_SlaveTransferNonBlocking | ( | SPI_Type * | base,
|  | spi_slave_handle_t * | handle,
|  | spi_transfer_t * | xfer
| ) |  |

inlinestatic

Performs a non-blocking SPI slave interrupt transfer.

Note
  Calling the API will immediately return after transfer intialize finished. Users need to call SPI_GetStatusIRQ() to get the transfer status.
  If use SPI with FIFO to do interrupt transfer, the transfer size shall be the integer times of watermark. Or the last data may lost as it cannot generate interrupt request. Users can also call functional API to get the last several receive data.

Parameters

  base | SPI peripheral base address.
  handle | pointer to spi_master_handle_t structure which stores the transfer state
  xfer | pointer to spi_xfer_config_t structure

Return values

  kStatus_Success | Successfully start a transfer.
  kStatus_InvalidArgument | Input argument is invalid.
  kStatus_SPI_Busy | SPI is not idle, is running another transfer.

void SPI_WriteBlocking | ( | SPI_Type * | base,
|  | uint8_t * | buffer,
|  | size_t | size
| ) |  |

Sends a buffer of data bytes in blocking way.

Note
  This function blocks via polling until all bytes have been sent.

Parameters

  base | SPI base pointer
  buffer | The data bytes to send
  size | The number of data bytes to send

void SPI_WriteData | ( | SPI_Type * | base,
|  | uint16_t | data
| ) |  |

Write a data into SPI data register.

Parameters

  base | SPI base pointer
  data | needs to be write.