Target transactions
Informative
How a target-mode channel responds to a controller on the bus. Binding rules: CHI-SPI-MUST-02, CHI-SPI-MUST-08, CHI-SPI-MUST-15, CHI-SPI-MUST-17, and CHI-SPI-SHOULD-01 in Spi — Requirements.
Stage a response ahead of time
A target cannot know when the controller will select it, so you stage the next transaction in
advance with Spi_StartTransaction — passing SPI_CS_UNSPECIFIED as the csId (the target’s
chip select is fixed by configuration):
static uint8 reply[4] = { 0xDEu, 0xADu, 0xBEu, 0xEFu };
static Spi_BufferType txChunks[1] = { { reply, 4u } };
static Spi_BufferSetType txSet = { txChunks, 1u };
Spi_StartTransaction(1u, SPI_CS_UNSPECIFIED, &txSet, NULL_PTR); /* channel -> READY */
When the controller selects the target, the driver transfers the staged data. On completion it
calls Spi_Callout_OnTransactionEnd, where you can stage the next response.
void Spi_Callout_OnTransactionEnd(uint8 channelId, uint8 csId, uint16 length)
{
/* controller finished a transaction; stage the next one if you have data */
Spi_StartTransaction(channelId, SPI_CS_UNSPECIFIED, &txSet, &rxSet);
}
If no buffer is staged when the controller selects the target, the target sends default_data and
ignores what it receives — but Spi_Callout_OnTransactionEnd still fires (with length = 0).
Cancel a staged transaction
If the staged data has gone stale (no controller request arrived in time), cancel it and stage fresh data. Cancellation only affects a transaction that has not yet started on the bus:
Std_ReturnType r = Spi_CancelTransaction(1u);
/* E_OK: nothing was staged, or the staged (not-yet-started) transaction was cancelled
E_NOT_OK: the transaction had already started -> cannot cancel */
CS detection without hardware support
If the SPI hardware cannot detect chip-select deactivation, the integration code tells the driver
the transaction has ended by calling Spi_OnCs (typically from a GPIO edge-detection interrupt):
/* on the CS edge interrupt for this target */
Spi_OnCs(1u, csId, FALSE); /* FALSE = CS deactivated -> driver calls Spi_Callout_OnTransactionEnd */
FALSE (deactivation) is mandatory when Spi_OnCs is used; TRUE (activation) is optional.
Note
Data consistency. Buffer sets you hand to Spi_StartTransaction must stay valid and
unmodified until Spi_Callout_OnTransactionEnd fires (CHI-SPI-MUST-13).