1.26.5.42 FLEXCOMx_SPI_Read Function

C

/* x = FLEXCOM instance number */

/* SPI master mode */

bool FLEXCOMx_SPI_Read(void* pRdBuffer, size_t rxSize)	

/* SPI slave mode */

size_t FLEXCOMx_SPI_Read(void* pRdBuffer, size_t rxSize)	

Summary

SPI master mode

Reads data on the FLEXCOM SPI peripheral.

SPI slave mode

Reads data from the PLIB's internal buffer to the application buffer

Description

SPI master mode

This function reads "rxSize" number of bytes on FLEXCOM SPI module. The received data is stored in the buffer pointed by pRdBuffer. When the interrupt is disabled in the PLIB, this function will be blocking in nature. In this mode, the function will not return until all the requested data is transferred. The function returns true after receiving "rxSize" number of bytes. This indicates that the operation has been completed. When interrupt is enabled, the function will be non-blocking in nature. The function returns immediately. The data transfer process continues from the peripheral interrupt. The application specified receive buffer is owned by the PLIB until the data transfer is complete and should not be modified by the application till the transfer is complete. Only one transfer is allowed at any time. The application can use a callback function or a polling function to check for completion of the transfer. If a callback is required then it should be registered prior to calling the FLEXCOMx_SPI_Read() function. The application can use the FLEXCOMx_SPI_IsBusy() to poll for completion of transfer.

SPI slave mode

This function reads "rxSize" number of bytes received from SPI master. The function copies the data that is already received by the PLIB in its internal buffer to the application's buffer specified by pRdBuffer. The return value indicates the actual number of bytes copied to the application buffer. The FLEXCOMx_SPI_ReadCountGet API can be used to know the available bytes in the PLIB. Application must call this API from the callback function itself.

Precondition

The FLEXCOMx_SPI_Initialize function must have been called.

SPI master mode

Callback can be registered using FLEXCOMx_SPI_CallbackRegister API if the PLIB is configured in Interrupt mode and transfer completion status needs to be communicated back to application via callback.

SPI slave mode

Callback must have been registered using FLEXCOMx_SPI_CallbackRegister API to get notified when the transfer is complete.

Parameters

SPI master mode

Param Description
pRdBuffer Pointer to the buffer where the received data will be stored. In "Interrupt Mode", this buffer should not be modified after calling the function and before the callback function has been called or the FLEXCOMx_SPI_IsBusy() function returns false.
rxSize Number of bytes to be received. This value is the byte size of the buffer irrespective of 8/16/32 bit transfer mode.

SPI slave mode

Param Description
pRdBuffer Pointer to the buffer where the data from the PLIB's internal buffer must be copied
rxSize Number of bytes to copy. If 16/32 bit modes are supported, the rxSize must be specified in terms of 16/32 bit words

Returns

SPI master mode

true - If configured for non-interrupt mode, the function has received the requested number of bytes. If configured for Interrupt mode, the request was accepted successfully and will be processed in the interrupt.

false - If pRdBuffer is NULL or rxSize is 0. In Interrupt mode, the function will additionally return false if there is an on-going data transfer at the time of calling the function.

SPI slave mode

Returns the number of bytes actually copied into the pRdBuffer. If 16/32 bit modes are supported, the return value is specified in terms of 16/32 bit words.

Example

SPI master mode

uint8_t rxBuffer[10];
size_t rxSize = 10;

void SPIEventHandler(uintptr_t context)
{
    //Transfer was completed without error, do something else now.
}

FLEXCOM0_SPI_Initialize();
FLEXCOM0_SPI_CallbackRegister(SPIEventHandler, (uintptr_t)0);
if(FLEXCOM0_SPI_Read(&rxBuffer, rxSize))
{
    // request got accepted
}
else
{
    // request didn't get accepted, try again later with correct arguments
}

SPI slave mode

uint8_t APP_RxData[10];

void SPIEventHandler(uintptr_t context )
{
    if (FLEXCOM0_SPI_ErrorGet() == SPI_SLAVE_ERROR_NONE)
    {
        // Read out the received data. This could be meaningful data if SPI master is
        // writing to slave or it could be dummy data if SPI master is reading from slave.
        // However, irrespective of whether slave is expecting meaningful data or dummy
        // data from master, SPI slave must always read out the data to clear the PLIB's
        // internal receive buffer.

        appData.nBytesRead = FLEXCOM0_SPI_Read(APP_RxData, FLEXCOM0_SPI_ReadCountGet());
    }
    else
    {
        // Handle error
    }

}

FLEXCOM0_SPI_CallbackRegister(SPIEventHandler, (uintptr_t) 0);

Remarks

None