SPI HAL API

Module: HAL (AM13E230X) API

More…

Structures

Name

struct

Spi_Hal_InitParamsType
SPI HW initialization parameters.

struct

Spi_Hal_ReConfigParamsType
SPI HW reconfiguration parameters.

struct

Spi_Hal_InstanceType
SPI HW instance structure. Should not be modified by application directly.

struct

Spi_Hal_TransferStatusType
SPI Transfer status.

Types

Name

enum

Spi_Hal_ModeType { SPI_HAL_MODE_CONTROLLER, SPI_HAL_MODE_PERIPHERAL}
SPI Mode.

enum

Spi_Hal_DataWidthType { SPI_HAL_DATA_WIDTH_4 = 4U, SPI_HAL_DATA_WIDTH_5 = 5U, SPI_HAL_DATA_WIDTH_6 = 6U, SPI_HAL_DATA_WIDTH_7 = 7U, SPI_HAL_DATA_WIDTH_8 = 8U, SPI_HAL_DATA_WIDTH_9 = 9U, SPI_HAL_DATA_WIDTH_10 = 10U, SPI_HAL_DATA_WIDTH_11 = 11U, SPI_HAL_DATA_WIDTH_12 = 12U, SPI_HAL_DATA_WIDTH_13 = 13U, SPI_HAL_DATA_WIDTH_14 = 14U, SPI_HAL_DATA_WIDTH_15 = 15U, SPI_HAL_DATA_WIDTH_16 = 16U}
SPI Data Size.

enum

Spi_Hal_FrameFormatType { SPI_HAL_FRAMEFORMAT_MOTO_4WIRE_POL0_PHA0, SPI_HAL_FRAMEFORMAT_MOTO_4WIRE_POL0_PHA1, SPI_HAL_FRAMEFORMAT_MOTO_4WIRE_POL1_PHA0, SPI_HAL_FRAMEFORMAT_MOTO_4WIRE_POL1_PHA1, SPI_HAL_FRAMEFORMAT_MOTO_3WIRE_POL0_PHA0, SPI_HAL_FRAMEFORMAT_MOTO_3WIRE_POL0_PHA1, SPI_HAL_FRAMEFORMAT_MOTO_3WIRE_POL1_PHA0, SPI_HAL_FRAMEFORMAT_MOTO_3WIRE_POL1_PHA1, SPI_HAL_FRAMEFORMAT_TI}
SPI Frame Format.

enum

Spi_Hal_BitOrderType { SPI_HAL_BITORDER_MSB_FIRST, SPI_HAL_BITORDER_LSB_FIRST}
SPI Bit Order.

enum

Spi_Hal_ParityType { SPI_HAL_PARITY_NONE, SPI_HAL_PARITY_ODD, SPI_HAL_PARITY_EVEN}
SPI Parity.

enum

Spi_Hal_StateType { SPI_HAL_STATE_UNCONFIGURED, SPI_HAL_STATE_READY, SPI_HAL_STATE_TRANSFER}
SPI State.

Functions

Name

sint32

Spi_Hal_InitParamsSetDefault(Spi_Hal_InitParamsType * paramsPtr)
Initialize params.

sint32

Spi_Hal_Init(Spi_Hal_InstanceType * instancePtr, const Spi_Hal_InitParamsType * paramsPtr)
Initialize a SPI HW instance.

sint32

Spi_Hal_Reconfigure(Spi_Hal_InstanceType * instancePtr, const Spi_Hal_ReConfigParamsType * paramsPtr)
Reconfigure a SPI HW instance to interface with a peripheral device with different configuration.

sint32

Spi_Hal_DeInit(Spi_Hal_InstanceType * instancePtr)
De-initialize a SPI HW instance.

sint32

Spi_Hal_TransferPolling(Spi_Hal_InstanceType * instancePtr, void * txBufferPtr, void * rxBufferPtr, uint32 size)
Transfers data in Polling mode.

sint32

Spi_Hal_TransferContinuePolling(Spi_Hal_InstanceType * instancePtr, Spi_Hal_TransferStatusType * statusPtr)
Continues the transfer of remaining bytes and gets transfer status in polling mode. Can be called only when interrupt mode is disabled.

sint32

Spi_Hal_TransferInterrupt(Spi_Hal_InstanceType * instancePtr, void * txBufferPtr, void * rxBufferPtr, uint32 size)
Transfers data in Interrupt Mode.

sint32

Spi_Hal_TransferAbort(Spi_Hal_InstanceType * instancePtr)
Abort Transfer operation. Only valid in polling and interrupt mode.

sint32

Spi_Hal_TransferDma(Spi_Hal_InstanceType * instancePtr, void * txBufferPtr, void * rxBufferPtr, uint32 size)
Transfers data in DMA mode.

sint32

Spi_Hal_TransferAbortDma(Spi_Hal_InstanceType * instancePtr)
Abort Transfer operation. Only valid in DMA mode.

sint32

Spi_Hal_DiscardRxFifoContents(Spi_Hal_InstanceType * instancePtr, uint32_t timeoutCpuCycles)
Discard all RX FIFO contents.

sint32

Spi_Hal_DiscardTxFifoContents(Spi_Hal_InstanceType * instancePtr, uint32_t timeoutCpuCycles)
Discard all TX FIFO contents.

void

Spi_Hal_TransferCompleteCallback(Spi_Hal_InstanceType * instancePtr, void * userArgsPtr)
Callback that is called when Transfer completes. Only valid in interrupt or DMA mode.

void

Spi_Hal_ErrorCallback(Spi_Hal_InstanceType * instancePtr, uint32 errorCodeMask, void * userArgsPtr)
Callback that is called in case of any Transfer error. Only valid in interrupt or DMA mode.

void

Spi_Hal_InterruptHandler(Spi_Hal_InstanceType * instancePtr)
Interrupt handler. Needs to be called from the ISR registered in the device’s interrupt controller.

void

Spi_Hal_DmaInterruptHandler(Spi_Hal_InstanceType * instancePtr)
DMA interrupt handler. Needs to be called from the ISR registered in the device’s interrupt controller.

Defines

Name

SPI_HAL_ERROR_NONE
No error.

SPI_HAL_ERROR_INVALID_INSTANCE
Invalid instance pointer.

SPI_HAL_ERROR_INVALID_PARAMS
Invalid parameters.

SPI_HAL_ERROR_UNSUPPORTED_FEATURE
Unsupported feature.

SPI_HAL_ERROR_HW_BUSY
Transfer fail since underlying HW was busy.

SPI_HAL_ERROR_TIMEOUT
Timeout while doing operation.

SPI_HAL_DMA_ADDR_ERROR_MASK
DMA address error mask.

Detailed Description

Note: All transfer APIs are non-blocking unless specified otherwise, i.e they don’t wait for the RX/TX operation to complete.

SPI Transfer APIs can be used in below operation modes

  • Polling mode

    • APIs to use for data transfer Spi_Hal_TransferPolling, Spi_Hal_TransferContinuePolling.

    • APIs to use to cancel or abort a data transfer Spi_Hal_TransferAbort

    • The Abort function essentially stops TX interrupt. Because of this, the TX and RX FIFOs may still have some bytes left. It is recommended to discard the contents of the RX FIFO (see Spi_Hal_DiscardRxFifoContents) before starting the next transfer, as this is more effective than discarding the FIFO after the abort. The TX FIFO can be discarded right after the transfer abort function runs.

    • Spi_Hal_InterruptHandler MUST not be registered to CPU interrupt line

    • Spi_Hal_TransferCompleteCallback, is not called in this mode

  • Interrupt mode

  • DMA mode

    • APIs to use for data transfer Spi_Hal_TransferDma

    • APIs to use to cancel or abort a data transfer Spi_Hal_TransferAbortDma

    • Callback functions to be implemented by application,

    • Spi_Hal_InterruptHandler MUST be registered to CPU interrupt line. In addition DMA interrupt handler MUST also be registered (TODO: review this statement. Should the SPI interrupt be registered?) Parameter size is not in bytes, but multiples of datawidth configured. Application must make sure the input buffers are of type uint8 in case datawidth <= 8 and uint16 in case datawidth > 8.

Types Documentation

enum Spi_Hal_ModeType

Enumerator

Value

Description

SPI_HAL_MODE_CONTROLLER

Controller mode.

SPI_HAL_MODE_PERIPHERAL

Peripheral mode.

SPI Mode.

enum Spi_Hal_DataWidthType

Enumerator

Value

Description

SPI_HAL_DATA_WIDTH_4

4U

4-bit data width

SPI_HAL_DATA_WIDTH_5

5U

5-bit data width

SPI_HAL_DATA_WIDTH_6

6U

6-bit data width

SPI_HAL_DATA_WIDTH_7

7U

7-bit data width

SPI_HAL_DATA_WIDTH_8

8U

8-bit data width

SPI_HAL_DATA_WIDTH_9

9U

9-bit data width

SPI_HAL_DATA_WIDTH_10

10U

10-bit data width

SPI_HAL_DATA_WIDTH_11

11U

11-bit data width

SPI_HAL_DATA_WIDTH_12

12U

12-bit data width

SPI_HAL_DATA_WIDTH_13

13U

13-bit data width

SPI_HAL_DATA_WIDTH_14

14U

14-bit data width

SPI_HAL_DATA_WIDTH_15

15U

15-bit data width

SPI_HAL_DATA_WIDTH_16

16U

16-bit data width

SPI Data Size.

enum Spi_Hal_FrameFormatType

Enumerator

Value

Description

SPI_HAL_FRAMEFORMAT_MOTO_4WIRE_POL0_PHA0

Motorola 4-wire frame format, clock polarity 0, clock phase 0.

SPI_HAL_FRAMEFORMAT_MOTO_4WIRE_POL0_PHA1

Motorola 4-wire frame format, clock polarity 0, clock phase 1.

SPI_HAL_FRAMEFORMAT_MOTO_4WIRE_POL1_PHA0

Motorola 4-wire frame format, clock polarity 1, clock phase 0.

SPI_HAL_FRAMEFORMAT_MOTO_4WIRE_POL1_PHA1

Motorola 4-wire frame format, clock polarity 1, clock phase 1.

SPI_HAL_FRAMEFORMAT_MOTO_3WIRE_POL0_PHA0

Motorola 3-wire (no CS pin) frame format, clock polarity 0, clock phase 0.

SPI_HAL_FRAMEFORMAT_MOTO_3WIRE_POL0_PHA1

Motorola 3-wire (no CS pin) frame format, clock polarity 0, clock phase 1.

SPI_HAL_FRAMEFORMAT_MOTO_3WIRE_POL1_PHA0

Motorola 3-wire (no CS pin) frame format, clock polarity 1, clock phase 0.

SPI_HAL_FRAMEFORMAT_MOTO_3WIRE_POL1_PHA1

Motorola 3-wire (no CS pin) frame format, clock polarity 1, clock phase 1.

SPI_HAL_FRAMEFORMAT_TI

TI synchronous serial frame format.

SPI Frame Format.

enum Spi_Hal_BitOrderType

Enumerator

Value

Description

SPI_HAL_BITORDER_MSB_FIRST

MSB transferred first.

SPI_HAL_BITORDER_LSB_FIRST

LSB transferred first.

SPI Bit Order.

enum Spi_Hal_ParityType

Enumerator

Value

Description

SPI_HAL_PARITY_NONE

No parity.

SPI_HAL_PARITY_ODD

Odd parity.

SPI_HAL_PARITY_EVEN

Even parity.

SPI Parity.

enum Spi_Hal_StateType

Enumerator

Value

Description

SPI_HAL_STATE_UNCONFIGURED

SPI instance has not been initialized/configured.

SPI_HAL_STATE_READY

SPI instance is initialized and ready for a transfer.

SPI_HAL_STATE_TRANSFER

SPI instance has an ongoing transfer.

SPI State.

Functions Documentation

function Spi_Hal_InitParamsSetDefault

sint32 Spi_Hal_InitParamsSetDefault(
    Spi_Hal_InitParamsType * paramsPtr
)

Initialize params.

Parameters:

  • paramsPtr Pointer to SPI HW initialization parameters

Return: SPI_HAL_ERROR_NONE on success, otherwise error code [Spi_Hal_ErrorType]

function Spi_Hal_Init

sint32 Spi_Hal_Init(
    Spi_Hal_InstanceType * instancePtr,
    const Spi_Hal_InitParamsType * paramsPtr
)

Initialize a SPI HW instance.

Parameters:

  • instancePtr Pointer to SPI HW instance

  • paramsPtr Pointer to SPI HW initialization parameters

Return: SPI_HAL_ERROR_NONE on success, otherwise error code [Spi_Hal_ErrorType]

Initializes the SPI hardware instance with the provided configuration parameters. The function configures timing parameters, operating parameters and prepares hardware for transmission.

function Spi_Hal_Reconfigure

sint32 Spi_Hal_Reconfigure(
    Spi_Hal_InstanceType * instancePtr,
    const Spi_Hal_ReConfigParamsType * paramsPtr
)

Reconfigure a SPI HW instance to interface with a peripheral device with different configuration.

function Spi_Hal_DeInit

sint32 Spi_Hal_DeInit(
    Spi_Hal_InstanceType * instancePtr
)

De-initialize a SPI HW instance.

Parameters:

  • instancePtr Pointer to SPI HW instance

Return: SPI_HAL_ERROR_NONE on success, otherwise error code [Spi_Hal_ErrorType]

Disable the HW and any enabled interrupts at the SPI HW. Interrupts at CPU should be disabled by the application before calling this API.

function Spi_Hal_TransferPolling

sint32 Spi_Hal_TransferPolling(
    Spi_Hal_InstanceType * instancePtr,
    void * txBufferPtr,
    void * rxBufferPtr,
    uint32 size
)

Transfers data in Polling mode.

Parameters:

  • instancePtr Pointer to SPI HW instance

  • txBufferPtr Pointer to transfer buffer. Can be NULL if only receiving data. When NULL, dummy data will be transmitted.

  • rxBufferPtr Pointer to receive buffer. Can be NULL if only transmitting data. When NULL, received data will be discarded.

  • size Number of SPI frames to be transmitted

Return: SPI_HAL_ERROR_NONE on success, otherwise error code [Spi_Hal_ErrorType]

Note: Either txBufferPtr or rxBufferPtr must be non-NULL. Both cannot be NULL simultaneously.

Can transfer data in both Controller and Peripheral mode

Depending on the transfer options, it can perform:

  • Transmitting data

  • Receiving data

  • Both transmitting and receiving data

The function:

  • Performs data transfer (TX and/or RX)

function Spi_Hal_TransferContinuePolling

sint32 Spi_Hal_TransferContinuePolling(
    Spi_Hal_InstanceType * instancePtr,
    Spi_Hal_TransferStatusType * statusPtr
)

Continues the transfer of remaining bytes and gets transfer status in polling mode. Can be called only when interrupt mode is disabled.

Parameters:

  • instancePtr Pointer to SPI HW instance

  • statusPtr Pointer that stores the status of current transfer

Return: SPI_HAL_ERROR_NONE on success, otherwise error code [Spi_Hal_ErrorType]

function Spi_Hal_TransferInterrupt

sint32 Spi_Hal_TransferInterrupt(
    Spi_Hal_InstanceType * instancePtr,
    void * txBufferPtr,
    void * rxBufferPtr,
    uint32 size
)

Transfers data in Interrupt Mode.

Parameters:

  • instancePtr Pointer to SPI HW instance

  • txBufferPtr Pointer to transfer buffer. Can be NULL if only receiving data. When NULL, dummy data will be transmitted.

  • rxBufferPtr Pointer to receive buffer. Can be NULL if only transmitting data. When NULL, received data will be discarded.

  • size Number of SPI frames to be transmitted

Return: SPI_HAL_ERROR_NONE on success, otherwise error code [Spi_Hal_ErrorType]

Note: Either txBufferPtr or rxBufferPtr must be non-NULL. Both cannot be NULL simultaneously.

Can transfer data in both Controller and Peripheral mode

Depending on the transfer options, it can perform:

  • Transmitting data

  • Receiving data

  • Both transmitting and receiving data

The function:

  • Performs data transfer (TX and/or RX)

The function returns immediately, and the transfer is handled asynchronously through interrupts. When the transfer is complete, the configured callback function will be invoked.

See also Spi_Hal_TransferCompleteCallback

function Spi_Hal_TransferAbort

sint32 Spi_Hal_TransferAbort(
    Spi_Hal_InstanceType * instancePtr
)

Abort Transfer operation. Only valid in polling and interrupt mode.

function Spi_Hal_TransferDma

sint32 Spi_Hal_TransferDma(
    Spi_Hal_InstanceType * instancePtr,
    void * txBufferPtr,
    void * rxBufferPtr,
    uint32 size
)

Transfers data in DMA mode.

Parameters:

  • instancePtr Pointer to SPI HW instance

  • txBufferPtr Pointer to transfer buffer

  • rxBufferPtr Pointer to receive buffer

  • size Number of SPI frames to be transmitted

Return: SPI_HAL_ERROR_NONE on success, otherwise error code [Spi_Hal_ErrorType]

Can transfer data in both Controller and Peripheral mode

Depending on the transfer options, it can perform:

  • Transmitting data

  • Receiving data

  • Both transmitting and receiving data

The function:

  • Performs data transfer (TX and/or RX)

See also Spi_Hal_TransferCompleteCallback

function Spi_Hal_TransferAbortDma

sint32 Spi_Hal_TransferAbortDma(
    Spi_Hal_InstanceType * instancePtr
)

Abort Transfer operation. Only valid in DMA mode.

function Spi_Hal_DiscardRxFifoContents

sint32 Spi_Hal_DiscardRxFifoContents(
    Spi_Hal_InstanceType * instancePtr,
    uint32_t timeoutCpuCycles
)

Discard all RX FIFO contents.

Parameters:

  • instancePtr Pointer to SPI HW instance

  • timeoutCpuCycles Timeout in CPU cycles

function Spi_Hal_DiscardTxFifoContents

sint32 Spi_Hal_DiscardTxFifoContents(
    Spi_Hal_InstanceType * instancePtr,
    uint32_t timeoutCpuCycles
)

Discard all TX FIFO contents.

Parameters:

  • instancePtr Pointer to SPI HW instance

  • timeoutCpuCycles Timeout in CPU cycles

function Spi_Hal_TransferCompleteCallback

void Spi_Hal_TransferCompleteCallback(
    Spi_Hal_InstanceType * instancePtr,
    void * userArgsPtr
)

Callback that is called when Transfer completes. Only valid in interrupt or DMA mode.

Parameters:

  • instancePtr Pointer to SPI HW instance

  • userArgsPtr Pointer to user arguments passed during initialization

function Spi_Hal_ErrorCallback

void Spi_Hal_ErrorCallback(
    Spi_Hal_InstanceType * instancePtr,
    uint32 errorCodeMask,
    void * userArgsPtr
)

Callback that is called in case of any Transfer error. Only valid in interrupt or DMA mode.

Parameters:

  • instancePtr Pointer to SPI HW instance

  • errorCodeMask Error code mask indicating the type of error

  • userArgsPtr Pointer to user arguments passed during initialization

function Spi_Hal_InterruptHandler

void Spi_Hal_InterruptHandler(
    Spi_Hal_InstanceType * instancePtr
)

Interrupt handler. Needs to be called from the ISR registered in the device’s interrupt controller.

Parameters:

  • instancePtr Pointer to SPI HW instance

Application MUST register an ISR against the appropriate CPU interrupt number after Spi_Hal_Init. This function must be called as part of the ISR with the right parameters. When polling is enabled as part of Spi_Hal_Init, the interrupt handler should not be registered.

The interrupt handler will call one of below callbacks, depending on the conditions detected within the interrupt handler. The interrupt handler will also clear status and interrupt bits as needed for correct operation.

In each of the callbacks, user arguments passed during initialization are passed back to the user.

The callbacks need to be implemented by the application. This driver provides weak definitions for these.

The supported callbacks are,

function Spi_Hal_DmaInterruptHandler

void Spi_Hal_DmaInterruptHandler(
    Spi_Hal_InstanceType * instancePtr
)

DMA interrupt handler. Needs to be called from the ISR registered in the device’s interrupt controller.

Parameters:

  • instancePtr Pointer to SPI HW instance

Application MUST register an ISR against the appropriate DMA interrupt number after Spi_Hal_Init. This function must be called as part of the ISR with the right parameters.

The DMA interrupt handler will call one of below callbacks, depending on the conditions detected within the interrupt handler. The DMA interrupt handler will also clear status and interrupt bits as needed for correct operation.

In each of the callbacks, user arguments passed during initialization are passed back to the user.

The callbacks need to be implemented by the application. This driver provides weak definitions for these.

The supported callbacks are,

Macros Documentation

define SPI_HAL_ERROR_NONE

#define SPI_HAL_ERROR_NONE (0U)

No error.

define SPI_HAL_ERROR_INVALID_INSTANCE

#define SPI_HAL_ERROR_INVALID_INSTANCE (1U)

Invalid instance pointer.

define SPI_HAL_ERROR_INVALID_PARAMS

#define SPI_HAL_ERROR_INVALID_PARAMS (2U)

Invalid parameters.

define SPI_HAL_ERROR_UNSUPPORTED_FEATURE

#define SPI_HAL_ERROR_UNSUPPORTED_FEATURE (3U)

Unsupported feature.

define SPI_HAL_ERROR_HW_BUSY

#define SPI_HAL_ERROR_HW_BUSY (4U)

Transfer fail since underlying HW was busy.

define SPI_HAL_ERROR_TIMEOUT

#define SPI_HAL_ERROR_TIMEOUT (5U)

Timeout while doing operation.

define SPI_HAL_DMA_ADDR_ERROR_MASK

#define SPI_HAL_DMA_ADDR_ERROR_MASK (1U << 31U)

DMA address error mask.