MCUXpresso SDK API Reference Manual  Rev. 0
NXP Semiconductors
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
SPI_Adapter

Overview

Data Structures

struct  hal_spi_master_config_t
 spi master user configure structure. More...
 
struct  hal_spi_slave_config_t
 spi slave user configure structure. More...
 
struct  hal_spi_transfer_t
 spi transfer structure More...
 

Macros

#define HAL_SPI_MASTER_HANDLE_SIZE   (68U)
 spi master handle size. More...
 
#define HAL_SPI_SLAVE_HANDLE_SIZE   (68U)
 spi slave handle size. More...
 
#define HAL_SPI_MASTER_HANDLE_DEFINE(name)   uint32_t name[(HAL_SPI_MASTER_HANDLE_SIZE + sizeof(uint32_t) - 1U) / sizeof(uint32_t)]
 Defines the SPI master handle. More...
 
#define HAL_SPI_SLAVE_HANDLE_DEFINE(name)   uint32_t name[(HAL_SPI_SLAVE_HANDLE_SIZE + sizeof(uint32_t) - 1U) / sizeof(uint32_t)]
 Defines the SPI slave handle. More...
 

Typedefs

typedef void * hal_spi_master_handle_t
 spi master handle. More...
 
typedef void * hal_spi_slave_handle_t
 spi slave handle. More...
 
typedef void(* hal_spi_master_transfer_callback_t )(hal_spi_master_handle_t handle, hal_spi_status_t status, void *callbackParam)
 spi master callback for finished transmit
 
typedef void(* hal_spi_slave_transfer_callback_t )(hal_spi_slave_handle_t handle, hal_spi_status_t status, void *callbackParam)
 spi slave callback for finished transmit
 

Enumerations

enum  hal_spi_status_t {
  kStatus_HAL_SpiSuccess = kStatus_Success,
  kStatus_HAL_SpiError = MAKE_STATUS(kStatusGroup_HAL_SPI, 1),
  kStatus_HAL_SpiBusy = MAKE_STATUS(kStatusGroup_HAL_SPI, 2),
  kStatus_HAL_SpiIdle = MAKE_STATUS(kStatusGroup_HAL_SPI, 3)
}
 Return status for the spi driver. More...
 
enum  hal_spi_clock_polarity_t {
  kHAL_SpiClockPolarityActiveHigh = 0x0U,
  kHAL_SpiClockPolarityActiveLow
}
 spi clock polarity configuration. More...
 
enum  hal_spi_clock_phase_t {
  kHAL_SpiClockPhaseFirstEdge = 0x0U,
  kHAL_SpiClockPhaseSecondEdge
}
 spi clock phase configuration. More...
 
enum  hal_spi_shift_direction_t {
  kHAL_SpiMsbFirst = 0x0U,
  kHAL_SpiLsbFirst
}
 spi data shifter direction options. More...
 

Initialization and de-initialization

hal_spi_status_t HAL_SpiMasterInit (hal_spi_master_handle_t handle, const hal_spi_master_config_t *config)
 Initializes the spi with master configuration. More...
 
hal_spi_status_t HAL_SpiSlaveInit (hal_spi_slave_handle_t handle, const hal_spi_slave_config_t *config)
 Initializes the spi with slave configuration. More...
 
hal_spi_status_t HAL_SpiMasterDeinit (hal_spi_master_handle_t handle)
 De-initializes the spi master. More...
 
hal_spi_status_t HAL_SpiSlaveDeinit (hal_spi_slave_handle_t handle)
 De-initializes the spi slave. More...
 

Transactional

hal_spi_status_t HAL_SpiMasterTransferInstallCallback (hal_spi_master_handle_t handle, hal_spi_master_transfer_callback_t callback, void *callbackParam)
 Installs a callback and callback parameter. More...
 
hal_spi_status_t HAL_SpiMasterTransferBlocking (hal_spi_master_handle_t handle, hal_spi_transfer_t *xfer)
 Transfers a block of data using a polling method. More...
 
hal_spi_status_t HAL_SpiMasterTransferNonBlocking (hal_spi_master_handle_t handle, hal_spi_transfer_t *xfer)
 Performs a non-blocking spi interrupt transfer. More...
 
hal_spi_status_t HAL_SpiMasterTransferGetCount (hal_spi_master_handle_t handle, size_t *spiCount)
 Gets the bytes of the spi interrupt transferred. More...
 
hal_spi_status_t HAL_SpiMasterTransferAbort (hal_spi_master_handle_t handle)
 Aborts an spi transfer using interrupt. More...
 
hal_spi_status_t HAL_SpiSlaveTransferInstallCallback (hal_spi_slave_handle_t handle, hal_spi_slave_transfer_callback_t callback, void *callbackParam)
 Installs a callback and callback parameter. More...
 
hal_spi_status_t HAL_SpiSlaveTransferNonBlocking (hal_spi_slave_handle_t handle, hal_spi_transfer_t *xfer)
 Performs a non-blocking spi slave interrupt transfer. More...
 
hal_spi_status_t HAL_SpiSlaveTransferGetCount (hal_spi_slave_handle_t handle, size_t *spiCount)
 Gets the bytes of the spi interrupt transferred. More...
 
hal_spi_status_t HAL_SpiSlaveTransferAbort (hal_spi_slave_handle_t handle)
 Aborts an spi slave transfer using interrupt. More...
 

Data Structure Documentation

struct hal_spi_master_config_t

Data Fields

uint32_t srcClock_Hz
 Clock source for spi in Hz.
 
uint32_t baudRate_Bps
 Baud Rate for spi in Hz.
 
hal_spi_clock_polarity_t polarity
 Clock polarity.
 
hal_spi_clock_phase_t phase
 Clock phase.
 
hal_spi_shift_direction_t direction
 MSB or LSB.
 
uint8_t instance
 Instance of the spi.
 
bool enableMaster
 Enable spi at initialization time.
 
struct hal_spi_slave_config_t

Data Fields

hal_spi_clock_polarity_t polarity
 Clock polarity.
 
hal_spi_clock_phase_t phase
 Clock phase.
 
hal_spi_shift_direction_t direction
 MSB or LSB.
 
uint8_t instance
 Instance of the spi.
 
bool enableSlave
 Enable spi at initialization time.
 
struct hal_spi_transfer_t

Data Fields

uint8_t * txData
 Send buffer.
 
uint8_t * rxData
 Receive buffer.
 
size_t dataSize
 Transfer bytes.
 
uint32_t flags
 spi control flag. More...
 

Field Documentation

uint32_t hal_spi_transfer_t::flags

Macro Definition Documentation

#define HAL_SPI_MASTER_HANDLE_SIZE   (68U)
#define HAL_SPI_SLAVE_HANDLE_SIZE   (68U)
#define HAL_SPI_MASTER_HANDLE_DEFINE (   name)    uint32_t name[(HAL_SPI_MASTER_HANDLE_SIZE + sizeof(uint32_t) - 1U) / sizeof(uint32_t)]

This macro is used to define the SPI master handle. Then use "(hal_spi_master_handle_t)name" to get the SPI master handle.

The macro should be global and could be optional. You could also define SPI master handle by yourself.

This is an example,

* HAL_SPI_MASTER_HANDLE_DEFINE(spiMasterHandle);
*
Parameters
nameThe name string of the SPI master handle.
#define HAL_SPI_SLAVE_HANDLE_DEFINE (   name)    uint32_t name[(HAL_SPI_SLAVE_HANDLE_SIZE + sizeof(uint32_t) - 1U) / sizeof(uint32_t)]

This macro is used to define the SPI slave handle. Then use "(hal_spi_slave_handle_t)name" to get the SPI slave handle.

The macro should be global and could be optional. You could also define SPI slave handle by yourself.

This is an example,

* HAL_SPI_SLAVE_HANDLE_DEFINE(spiSlaveHandle);
*
Parameters
nameThe name string of the SPI slave handle.

Typedef Documentation

typedef void* hal_spi_master_handle_t
typedef void* hal_spi_slave_handle_t

Enumeration Type Documentation

Enumerator
kStatus_HAL_SpiSuccess 

Successfully.

kStatus_HAL_SpiError 

spi error

kStatus_HAL_SpiBusy 

spi bus is busy

kStatus_HAL_SpiIdle 

spi is idle

Enumerator
kHAL_SpiClockPolarityActiveHigh 

Active-high spi clock (idles low).

kHAL_SpiClockPolarityActiveLow 

Active-low spi clock (idles high).

Enumerator
kHAL_SpiClockPhaseFirstEdge 

First edge on SPSCK occurs at the middle of the first cycle of a data transfer.

kHAL_SpiClockPhaseSecondEdge 

First edge on SPSCK occurs at the start of the first cycle of a data transfer.

Enumerator
kHAL_SpiMsbFirst 

Data transfers start with most significant bit.

kHAL_SpiLsbFirst 

Data transfers start with least significant bit.

Function Documentation

hal_spi_status_t HAL_SpiMasterInit ( hal_spi_master_handle_t  handle,
const hal_spi_master_config_t config 
)

This function configures the spi master with user-defined settings. The user can configure the configuration structure. The parameter handle is a pointer to point to a memory space of size HAL_SPI_MASTER_HANDLE_SIZE allocated by the caller.

Example below shows how to use this API to configure the SPI master.

* userConfig.direction = kHAL_SpiMsbFirst;
* userConfig.baudRate_Bps = 500000U;
* userConfig.enableMaster = true;
* userConfig.srcClock_Hz = 48000000;
* userConfig.instance = 0;
* HAL_SpiMasterInit((hal_spi_master_handle_t)masterHandle, &userConfig);
*
Parameters
handlePointer to point to a memory space of size HAL_SPI_MASTER_HANDLE_SIZE allocated by the caller. The handle should be 4 byte aligned, because unaligned access doesn't be supported on some devices. You can define the handle in the following two ways: HAL_SPI_MASTER_HANDLE_DEFINE(handle); or uint32_t handle[((HAL_SPI_MASTER_HANDLE_SIZE + sizeof(uint32_t) - 1U) / sizeof(uint32_t))];
configpointer to master configuration structure
Return values
kStatus_HAL_SpiErrorAn error occurred.
kStatus_HAL_SpiSuccessspi master initialization succeed
hal_spi_status_t HAL_SpiSlaveInit ( hal_spi_slave_handle_t  handle,
const hal_spi_slave_config_t config 
)

This function configures the spi slave with user-defined settings. The user can configure the configuration structure. The parameter handle is a pointer to point to a memory space of size HAL_SPI_SLAVE_HANDLE_SIZE allocated by the caller. After calling this API, the slave is ready to transfer.

Example below shows how to use this API to configure the SPI slave.

* userConfig.direction = kHAL_SpiMsbFirst;
* userConfig.instance = 0;
* userConfig.enableSlave = true;
* HAL_SpiSlaveInit((hal_spi_slave_handle_t)slaveHandle, &userConfig);
*
Parameters
handlePointer to point to a memory space of size HAL_SPI_SLAVE_HANDLE_SIZE allocated by the caller. The handle should be 4 byte aligned, because unaligned access doesn't be supported on some devices. You can define the handle in the following two ways: HAL_SPI_MASTER_HANDLE_DEFINE(handle); or uint32_t handle[((HAL_SPI_SLAVE_HANDLE_SIZE + sizeof(uint32_t) - 1U) / sizeof(uint32_t))];
configpointer to slave configuration structure
Return values
kStatus_HAL_SpiErrorAn error occurred.
kStatus_HAL_SpiSuccessspi slave initialization succeed
hal_spi_status_t HAL_SpiMasterDeinit ( hal_spi_master_handle_t  handle)

De-initializes the spi master. The spi master can't work unless calling the HAL_SpiMasterInit to initialize module.

Parameters
handlespi master handle pointer, this should be a static variable.
Return values
kStatus_HAL_SpiSuccessspi master de-initialization succeed
hal_spi_status_t HAL_SpiSlaveDeinit ( hal_spi_slave_handle_t  handle)

De-initializes the spi slave. The spi slave can't work unless calling the HAL_SpiSlaveInit to initialize module.

Parameters
handlespi slave handle pointer, this should be a static variable.
Return values
kStatus_HAL_SpiSuccessspi slave de-initialization succeed
hal_spi_status_t HAL_SpiMasterTransferInstallCallback ( hal_spi_master_handle_t  handle,
hal_spi_master_transfer_callback_t  callback,
void *  callbackParam 
)

This function is used to install the callback and callback parameter for spi master module. When any status of the spi master changed, the driver will notify the upper layer by the installed callback function. And the status is also passed as status parameter when the callback is called.

Parameters
handlespi master handle pointer, this should be a static variable.
callbackCallback function.
callbackParamThe parameter of the callback function.
Return values
kStatus_HAL_SpiSuccessspi master handle created
hal_spi_status_t HAL_SpiMasterTransferBlocking ( hal_spi_master_handle_t  handle,
hal_spi_transfer_t xfer 
)
Parameters
handlespi master handle pointer, this should be a static variable.
xferpointer to hal_spi_transfer_t structure
Return values
kStatus_HAL_SpiErrorAn error occurred.
kStatus_HAL_SpiSuccessSuccessfully sent all data.
hal_spi_status_t HAL_SpiMasterTransferNonBlocking ( hal_spi_master_handle_t  handle,
hal_spi_transfer_t xfer 
)
Note
If spi transfer data frame size is 16 bits, the transfer size cannot be an odd number.
Parameters
handlespi master handle pointer, this should be a static variable.
xferpointer to hal_spi_transfer_t structure
Return values
kStatus_HAL_SpiErrorAn error occurred.
kStatus_HAL_SpiBusyspi is not idle, is running another transfer.
kStatus_HAL_SpiSuccessSuccessfully start the data transmission.
hal_spi_status_t HAL_SpiMasterTransferGetCount ( hal_spi_master_handle_t  handle,
size_t *  spiCount 
)
Parameters
handlespi master handle pointer, this should be a static variable.
countTransferred bytes of spi master.
Return values
kStatus_HAL_SpiErrorAn error occurred.
kStatus_HAL_SpiSuccessSucceed get the transfer count.
hal_spi_status_t HAL_SpiMasterTransferAbort ( hal_spi_master_handle_t  handle)
Parameters
handlespi master handle pointer, this should be a static variable.
Return values
kStatus_HAL_SpiSuccessSucceed abort the transfer.
hal_spi_status_t HAL_SpiSlaveTransferInstallCallback ( hal_spi_slave_handle_t  handle,
hal_spi_slave_transfer_callback_t  callback,
void *  callbackParam 
)

This function is used to install the callback and callback parameter for spi slave module. When any status of the spi slave changed, the driver will notify the upper layer by the installed callback function. And the status is also passed as status parameter when the callback is called.

Parameters
handlespi slave handle pointer, this should be a static variable.
callbackCallback function.
callbackParamThe parameter of the callback function.
Return values
kStatus_HAL_SpiSuccessspi slave handle created
hal_spi_status_t HAL_SpiSlaveTransferNonBlocking ( hal_spi_slave_handle_t  handle,
hal_spi_transfer_t xfer 
)
Note
If spi transfer data frame size is 16 bits, the transfer size cannot be an odd number.
Parameters
handlespi slave handle pointer, this should be a static variable.
xferpointer to hal_spi_xfer_config_t structure
Return values
kStatus_HAL_SpiSuccessSuccessfully start a transfer.
kStatus_HAL_SpiErrorAn error occurred.
kStatus_HAL_SpiBusyspi is not idle, is running another transfer.
hal_spi_status_t HAL_SpiSlaveTransferGetCount ( hal_spi_slave_handle_t  handle,
size_t *  spiCount 
)
Parameters
handlespi slave handle pointer, this should be a static variable.
countTransferred bytes of spi slave.
Return values
kStatus_HAL_SpiSuccessSucceed get the transfer count.
kStatus_HAL_SpiErrorAn error occurred.
hal_spi_status_t HAL_SpiSlaveTransferAbort ( hal_spi_slave_handle_t  handle)
Parameters
handlespi slave handle pointer, this should be a static variable.
Return values
kStatus_HAL_SpiSuccessSucceed abort the transfer.