API Reference

Normative

The SPI driver API contract. A conformant driver MUST provide these types and functions with the behavior described. Function and type signatures are extracted from the driver headers with Doxygen/Breathe; only the interrupt handler is hand-framed, because its name is vendor-specific. Keywords per Requirement Keywords; behavioral rules in Spi — Requirements; error codes in Errors.

Data types at a glance

Type

Kind

Purpose

Spi_ConfigType

struct

Root configuration structure (build-time; Spi_Init takes NULL_PTR).

Spi_BufferDataType

typedef

Element type of an application data buffer.

Spi_BufferType

struct

One chunk of a TX/RX buffer (pointer + length).

Spi_BufferSetType

struct

A set of buffer chunks transferred in one chip-select sequence.

Spi_ChannelStateType

enum

Channel state (UNINIT / IDLE / READY / BUSY).

API at a glance

Function

Group

Purpose

Spi_Init

Initialization and shutdown

Initialize the driver and every channel.

Spi_DeInit

Initialization and shutdown

Abort transfers and de-initialize the driver.

Spi_StartTransaction

Transactions

Start (controller) or stage (target) a buffer-set transfer.

Spi_CancelTransaction

Transactions

Cancel a staged (not-yet-started) target transaction.

Spi_OnCs

Transactions

Target CS notification when the hardware cannot detect it.

Spi_GetChannelState

Status and scheduling

Read a channel’s current state.

Spi_PollFunction

Status and scheduling

Service events for polled channels.

Spi_Interrupt

Status and scheduling

Driver interrupt service routine.

Callouts and expected interfaces at a glance

The functions the integrator MUST provide (CHI-SPI-MUST-21); see the Callouts group below. SpiDrv has no upper-layer interface stack, so it reports its events through callouts that the integrator implements in the generated Spi_Callout_Stubs.c (see The Callout Stub File).

Function

Kind

Purpose

Spi_Callout_OnTransactionEnd

Callout

A transaction completed; reports the transferred length.

Spi_Callout_CsControl

Callout

Requests GPIO chip-select assert/release from the integration code.

LogM_Report

Common

Reports development / runtime errors with a log level.

<Mip>_EnterCriticalSection / <Mip>_ExitCriticalSection

Common (optional)

Bracket sections requiring exclusive access — see critical sections.

Data types
Spi_ConfigType
struct Spi_ConfigType

Configuration structure type.

Configuration variants are selected at build time; Spi_Init currently takes NULL_PTR. Available via: Spi.h

Spi_BufferDataType
typedef uint8 Spi_BufferDataType

Type of application data buffer elements.

The driver casts elements to a wider type when the configured data_width exceeds 8 bits (uint16 for 9–16 bits, uint32 for 17–32 bits). Available via: Spi_DriverTypes.h

Spi_BufferType
struct Spi_BufferType

This type represents a chunk of TX/RX buffer.

Public Members

Spi_BufferDataType *bufferDataPtr

Pointer to data buffer. Can be null pointer and the behavior varies to TX/RX.

TX: If bufferDataPtr is null pointer, sends default data for length

RX: If bufferDataPtr is null pointer, ignore received data for length

uint16 length

Length of the buffer element. Must be ≥ 1.

Available via: Spi_DriverTypes.h

Spi_BufferSetType
struct Spi_BufferSetType

This type represents total data to be transferred in one CS sequence.

Public Members

Spi_BufferType *bufferPtr

Pointer to buffer type array. Must not be null pointer.

uint8 numOfBuffer

Number of buffers in the array. Must be ≥ 1.

Available via: Spi_DriverTypes.h

Spi_ChannelStateType
enum Spi_ChannelStateType

Specifies the SpiDrv internal state.

Values:

enumerator SPI_CH_STATE_UNINIT = 0x00

Not yet initialized

enumerator SPI_CH_STATE_IDLE = 0x01

Not in transaction

enumerator SPI_CH_STATE_READY = 0x02

Only for Target Mode. Buffer has been set for next transaction by Spi_StartTransaction.

enumerator SPI_CH_STATE_BUSY = 0x03

In transaction

Also note SPI_CS_UNSPECIFIED — the csId a target passes to Spi_StartTransaction, and the csId for a controller-level error not tied to a chip select. Available via: Spi_DriverTypes.h

Initialization and shutdown
Spi_Init
void Spi_Init(const Spi_ConfigType *configPtr)

Initialize driver and HW.

Sync/Async:

Synchronous

Reentrancy:

Non reentrant

Service ID [hex]:

0x00

Development error(s):

Parameters:

configPtr[in] Pointer to the configuration structure. This parameter is intended for future extensions to support runtime-selectable configuration variants, which allow the selection and initialization of specific variants during runtime. As this functionality is not currently supported, a NULL_PTR SHALL be provided.

Returns:

None.

When to call: once, before any other SPI function, with the driver in SPI_DRV_STATE_UNINIT. configPtr MUST be NULL_PTR.

Effect: initialises the driver and hardware; leaves every channel in IDLE (a target channel begins detecting chip-select activation); enables interrupts for interrupt channels. Errors: see Errors. Available via: Spi.h

Spi_DeInit
void Spi_DeInit(void)

Abort on-going transaction and de-initialize SPI hardware. Typically used when micro controller will be switched to other state (e.g. low power mode) or recovery from SPI communication erroneous state.

Sync/Async:

Synchronous

Reentrancy:

Non reentrant

Service ID [hex]:

0x01

Parameters:

None

Returns:

None.

Effect: immediately aborts any ongoing transfer and de-initialises the driver, returning it to SPI_DRV_STATE_UNINIT. Prefer calling it with the channel IDLE. Available via: Spi.h

Transactions
Spi_StartTransaction
Std_ReturnType Spi_StartTransaction(uint8 channelId, uint8 csId, const Spi_BufferSetType *txBuffSetPtr, Spi_BufferSetType *rxBuffSetPtr)

This service starts transaction. If the channel is in controller mode, this function initiates the transaction. This function is non-blocking and returns immediately after initiating the transfer. If the channel is in Target Mode, this function will provide TX/RX buffers to the HW that are used for the next transaction.

Sync/Async:

Asynchronous

Reentrancy:

Conditionally reentrant (reentrant when called with different channelId)

Service ID [hex]:

0x02

Development error(s):

Parameters:
  • channelId[in] ID of the SPI channel to be addressed.

  • csId[in] chip select ID in the channel. SPI_CS_UNSPECIFIED shall be set when the channel is Target Mode. Note: If the channel is Target Node, SpiDrv must not check the csId parameter. Even if the csId is not SPI_CS_UNSPECIFIED, SpiDrv must not log the development error

  • txBuffSetPtr[in] Pointer to a buffer set to be transferred.

  • rxBuffSetPtr[out] Pointer to a buffer set to fill with received data.

Return values:
  • E_OK – Start transaction has been accepted

  • E_NOT_OK – Development error occurred

Returns:

Std_ReturnType.

When to call: on an IDLE controller channel to start a transfer (with the target’s csId), or on a target channel to stage the next transfer (with SPI_CS_UNSPECIFIED). Returns: E_OK if accepted, else E_NOT_OK (development error). The caller MUST keep the buffer sets valid until Spi_Callout_OnTransactionEnd. A TX chunk with a null bufferDataPtr sends default_data; a null RX pointer discards the received data. Errors: see Errors. Available via: Spi.h

Spi_CancelTransaction
Std_ReturnType Spi_CancelTransaction(uint8 channelId)

This interface is Target Mode only. Cancel transaction set by Spi_StartTransaction. Cancellation does not mean canceling on-going transaction. This interface just cancel the transaction setup not yet transferred to the bus.

Sync/Async:

Synchronous

Reentrancy:

Conditionally reentrant (reentrant when called with different channelId)

Service ID [hex]:

0x08

Development error(s):

Parameters:

channelId[in] ID of the SPI channel to be addressed.

Return values:
  • E_OK – Request has been accepted, including no transaction setup to be cancelled.

  • E_NOT_OK – Development error occurred or request has not been accepted due to the transaction is already started.

When to call: on a target channel to cancel a staged transaction that has not started on the bus. Returns: E_OK if accepted (including when nothing was staged); E_NOT_OK if the transaction has already started. Errors: see Errors. Available via: Spi.h

Spi_OnCs
void Spi_OnCs(uint8 channelId, uint8 csId, boolean status)

Target Mode only and optional interface if the SPI HW cannot detect CS control. This service is a callback notification that is triggered by CS edge detection in the integration code. FALSE (deactivation) is mandatory if this interface is used. TRUE (activation) is optional if required.

Sync/Async:

Synchronous

Reentrancy:

Conditionally reentrant (reentrant when called with different channelId)

Service ID [hex]:

0x05

Development error(s):

Parameters:
  • channelId[in] ID of the SPI channel to be addressed.

  • csId[in] CS ID in the channel.

  • status[in] status of the chip select, TRUE: CS activated, FALSE: CS deactivated

Returns:

None.

When to call: target mode only, from the integration code (typically a GPIO edge interrupt) when the SPI hardware cannot detect chip-select control. FALSE (deactivation) is mandatory; TRUE (activation) is optional. On deactivation the driver completes the transaction and calls Spi_Callout_OnTransactionEnd. Errors: see Errors. Available via: Spi.h

Status and scheduling
Spi_GetChannelState
Spi_ChannelStateType Spi_GetChannelState(uint8 channelId)

Returns the channel state.

Sync/Async:

Synchronous

Reentrancy:

Reentrant

Service ID [hex]:

0x04

Development error(s):

Parameters:

channelId[in] ID of the SPI channel to be addressed.

Returns:

Informs the channel internal status

Effect: returns the current state of the channel. Errors: see Errors. Available via: Spi.h

Spi_PollFunction
void Spi_PollFunction(void)

Handle interrupt event with polling method. This interface is optional. Typically SPI is handled by interrupt especially Target Node. If polling method is applicable, e.g. call cycle of poll function is fast enough, handle transaction by DMA, SpiDrv provides this interface.

Sync/Async:

Synchronous

Reentrancy:

Non reentrant

Service ID [hex]:

0x07

Parameters:

None

Returns:

None.

When to call: cyclically, for channels configured for polling. Handles the same events as Spi_Interrupt. This service is optional — provided only where polling is applicable. Available via: Spi.h

Spi_Interrupt
void Spi_Interrupt<vectorNr>[<VendorSpecificSuffix>](void)

Effect: the driver interrupt service routine; pumps the buffer-set transfer and fires the chip-select and completion callouts. Define it with the ISR() macro (see the general reference). Available via: Spi.h

Callouts the integrator provides

The driver calls these callouts; the integrator MUST provide them (CHI-SPI-MUST-21) by filling in the matching user code block in Spi_Callout_Stubs.c (see The Callout Stub File). The prototypes are declared in Spi.h. LogM_Report and the critical-section services are documented in the general reference.

Spi_Callout_OnTransactionEnd
void Spi_Callout_OnTransactionEnd(uint8 channelId, uint8 csId, uint16 length)

Informs transaction end.

Sync/Async:

Synchronous

Reentrancy:

Conditionally reentrant (reentrant when called with different channelId)

Parameters:
  • channelId[in] ID of the SPI channel to be addressed.

  • csId[in] Informs Target Node CS ID

  • length[in] Total transfer length in the transaction

Returns:

None.

Informs the integration code that a transaction ended; length elements were transferred (0 if a target transferred nothing). Available via: Spi.h; implement it in Spi_Callout_Stubs.c

Spi_Callout_CsControl
void Spi_Callout_CsControl(uint8 channelId, uint8 csId, boolean status)

Delegates CS control by GPIO to the integration code. This service is called only if the CS control is configured as GPIO control. If the CS control is configured as GPIO, then the integration code must implement this service by overwriting the default implementation.

Sync/Async:

Synchronous

Reentrancy:

Conditionally reentrant (when called with different channelIds)

Note

The SpiDrv shall provide a definition for this service following to callout definition rule described in [1].

Parameters:
  • channelId[in] ID of the SPI channel to be addressed.

  • csId[in] Informs Target Node CS ID.

  • status[in] TRUE: CS activation request, FALSE: CS deactivation request.

Returns:

None.

Delegates GPIO chip-select control to the integration code — called only when the chip select is configured as ViaGPIO. status == TRUE requests assert, FALSE requests release. The integration code overrides the driver’s default definition. Available via: Spi.h; implement it in Spi_Callout_Stubs.c