1.1.8.4.14 DRV_SPI_WriteReadTransfer Function

C

void DRV_SPI_WriteReadTransfer
(
    const DRV_HANDLE handle,
    void* pTransmitData,
    size_t txSize,
    void* pReceiveData,
    size_t rxSize
);

Summary

This is a blocking function that transmits and receives data over SPI.

Description

This function does a blocking write-read operation. The function blocks till the data receive is complete. Function will return true if the receive is successful or false in case of an error. The failure will occur for the following reasons:

  • if the handle is invalid

  • if the transmit size is non-zero and pointer to the transmit buffer is NULL

  • if the receive size is non-zero and pointer to the receive buffer is NULL

Precondition

  • DRV_SPI_Open must have been called to obtain a valid opened device handle.

  • DRV_SPI_TransferSetup must have been called if GPIO pin has to be used for

chip select or any of the setup parameters has to be changed dynamically.

Parameters

ParamDescription
handleHandle of the communication channel as returned by the DRV_SPI_Open function.
*pTransmitDataPointer to the data which has to be transmitted. If it is NULL, that means only data receiving is expected.
txSizeNumber of bytes to be transmitted. The size must be specified in terms of the SPI data width. For example, if the data width is 8-bits, and if 10 bytes are being transmitted, then the txSize must be set to 10. If the data width is 16-bits then transmitting 10 bytes requires specifying the txSize as 10 (meaning 10 16-bit words).
*pReceiveDataPointer to the buffer where the data is to be received. If it is NULL, that means only data transmission is expected.
rxSizeNumber of bytes to be received. The size must be specified in terms of the SPI data width. For example, if the data width is 8-bits, and if 10 bytes are being received, then the rxSize must be set to 10. If the data width is 16-bits then receiving 10 bytes requires specifying the rxSize as 10 (meaning 10 16-bit words). If "n" number of bytes has to be received AFTER transmitting "m" number of bytes, then "txSize" should be set as "m" and "rxSize" should be set as "m+n".

Returns

true - write-read is successful

false - error has occurred

Example

MY_APP_OBJ myAppObj;
uint8_t myTxBuffer[MY_TX_BUFFER_SIZE];
uint8_t myRxBuffer[MY_RX_BUFFER_SIZE];

// mySPIHandle is the handle returned by the DRV_SPI_Open function.

if (DRV_SPI_WriteReadTransfer(mySPIhandle, myTxBuffer, MY_TX_BUFFER_SIZE,
myRxBuffer, MY_RX_BUFFER_SIZE) == false)
{
    // Handle error here
}

Remarks

  • This function is thread safe in a RTOS application.

  • This function should not be called from an interrupt context.