1.2.21.44 1.3.25.44 1.4.21.44 1.5.25.44 1.6.22.44 1.7.23.44 1.9.19.44 1.29.23.44 1.30.18.44 1.31.18.44 1.32.26.44 1.33.16.44 1.37.18.44 1.38.21.44 1.39.18.44 1.40.20.44 SERCOMx_SPI_Read Function

C

/* x = SERCOM instance number */

/* SPI master mode */

bool SERCOMx_SPI_Read(void* pRdBuffer, size_t rxSize)	

/* SPI slave mode */

size_t SERCOMx_SPI_Read(void* pRdBuffer, size_t rxSize)	

Summary

SPI master mode

Reads data on the SERCOM 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 SERCOM 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 SERCOMx_SPI_Read() function. The application can use the SERCOMx_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 SERCOMx_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 SERCOMx_SPI_Initialize function must have been called.

SPI master mode

Callback can be registered using SERCOMx_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 SERCOMx_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. For 9 bit mode, data should be right aligned in the 16 bit memory location. In "Interrupt Mode", this buffer should not be modified after calling the function and before the callback function has been called or the SERCOMx_SPI_IsBusy() function returns false.
rxSize Number of bytes to be received. This value is the byte size of the buffer. For 9 bit data length, this is an even number.

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. For 9-bit mode, the rxSize must be specified in terms of 16-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 (or 16-bit words if PLIB is in 9-bit mode) actually copied into the pRdBuffer

Example

SPI master mode

uint8_t rxBuffer[10];
size_t rxSize = 10;

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

SERCOM0_SPI_Initialize();
SERCOM0_SPI_CallbackRegister(&APP_SPITransferHandler, (uintptr_t)NULL);
if(SERCOM0_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 (SERCOM0_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 = SERCOM0_SPI_Read(APP_RxData, SERCOM0_SPI_ReadCountGet());
    }
    else
    {
        // Handle error
    }

}

SERCOM0_SPI_CallbackRegister(SPIEventHandler, (uintptr_t) 0);

Remarks

None