1.8.10.6 MCSPIx_CallbackRegister Function

C

/* x = MCSPI instance number */

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

void MCSPIx_CallbackRegister(MCSPI_CALLBACK callback, uintptr_t context )

/* MCSPI slave mode */

void MCSPIx_CallbackRegister(MCSPI_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 operation has completed.

Description

This function sets the pointer to a client/application function to be called "back" when the given MCSPI'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 MCSPIx_Initialize function must have been called. This function is only available if the library is configured for interrupt operation mode.

Parameters

Param Description
callback A pointer to a function with a calling signature defined by the MCSPI_CALLBACK or MCSPI_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

MCSPI 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_MCSPITransferHandler(uintptr_t context)
{
    //Transfer was completed without error, do something else now.
}

MCSPI1_Initialize();
MCSPI1_CallbackRegister(&APP_MCSPITransferHandler, (uintptr_t)NULL);
if(MCSPI1_WriteRead(&txBuffer, txSize, &rxBuffer, rxSize))
{
    // request got accepted
}
else
{
    // request didn't get accepted, try again later with correct arguments
}

MCSPI slave mode

uint8_t APP_RxData[10];
size_t nBytesAvailable;
size_t nBytesRead;

void MCSPIEventHandler(uintptr_t context )
{
    if (MCSPI1_ErrorGet() == MCSPI_SLAVE_ERROR_NONE)
    {
        nBytesAvailable = MCSPI1_ReadCountGet();

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

}

MCSPI1_CallbackRegister(MCSPIEventHandler, (uintptr_t) 0);

Remarks

None