1.41.5.46 FLEXCOMx_SPI_CallbackRegister Function

C

/* x = FLEXCOM instance number */

/* SPI master (non-blocking mode) mode */

void FLEXCOMx_SPI_CallbackRegister(FLEXCOM_SPI_CALLBACK callback, uintptr_t context )

/* SPI slave mode */

void FLEXCOMx_SPI_CallbackRegister(FLEXCOM_SPI_SLAVE_CALLBACK callBack, uintptr_t context )

Summary

This function allows application to register a callback function for the PLIB to call back when the requested data transfer has completed.

Description

This function sets the pointer to a client/application function to be called "back" when the given FLEXCOM SPI's transfer events occur. It also passes a context value (usually a pointer to a context structure) that is passed into the function when it is called. The specified callback function will be called from the peripheral interrupt context. The callback should be registered before a transfer operation is requested.

Precondition

The FLEXCOMx_SPI_Initialize function must have been called. This function is only available if the library is configured for interrupt (non-blocking) mode.

Parameters

Param Description
callback A pointer to a function with a calling signature defined by the FLEXCOM_SPI_CALLBACK or FLEXCOM_SPI_SLAVE_CALLBACK data type. Setting this to NULL disables the callback feature.
context A value (usually a pointer) which is passed (unused) into the function identified by the callback parameter

Returns

None.

Example

SPI master (non-blocking mode) mode

uint8_t txBuffer[4];
uint8_t rxBuffer[10];
size_t txSize = 4;
size_t rxSize = 10;
bool reqAccepted;

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

FLEXCOM0_SPI_Initialize();
FLEXCOM0_SPI_CallbackRegister(&APP_SPITransferHandler, (uintptr_t)NULL);
if(FLEXCOM0_SPI_WriteRead(&txBuffer, txSize, &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];
size_t nBytesAvailable;
size_t nBytesRead;

void SPIEventHandler(uintptr_t context )
{
    if (FLEXCOM0_SPI_ErrorGet() == FLEXCOM_SPI_SLAVE_ERROR_NONE)
    {
        nBytesAvailable = FLEXCOM0_SPI_ReadCountGet();

        nBytesRead = FLEXCOM0_SPI_Read(APP_RxData, nBytesAvailable);
    }
    else
    {
        // Handle error
    }

}

FLEXCOM0_SPI_CallbackRegister(SPIEventHandler, (uintptr_t) 0);

Remarks

None