1.1.8.4.10 DRV_SPI_TransferEventHandlerSet Function

C

void DRV_SPI_TransferEventHandlerSet
(
    const DRV_HANDLE handle,
    const DRV_SPI_TRANSFER_EVENT_HANDLER eventHandler,
    const uintptr_t context
)

Summary

Allows a client to set a transfer event handling function for the driver to call back when queued transfer has finished.

Description

This function allows a client to register a transfer event handling function with the driver to call back when queued transfer has finished. When a client calls either the DRV_SPI_ReadTransferAdd or DRV_SPI_WriteTransferAdd or DRV_SPI_WriteReadTransferAdd function, it is provided with a handle identifying the transfer request that was added to the driver's queue. The driver will pass this handle back to the client by calling "eventHandler" function when the transfer has completed.

The event handler should be set before the client performs any "transfer add" operations that could generate events. The event handler once set, persists until the client closes the driver or sets another event handler (which could be a "NULL" pointer to indicate no callback).

Precondition

DRV_SPI_Open must have been called to obtain a valid open instance handle.

Parameters

ParamDescription
handleA valid open-instance handle, returned from the driver's open routine.
eventHandlerPointer to the event handler function.
contextThe value of parameter will be passed back to the client unchanged, when the eventHandler function is called. It can be used to identify any client specific data object that identifies the instance of the client module (for example, it may be a pointer to the client module's state structure).

Returns

None.

Example

// myAppObj is an application specific state data object.
MY_APP_OBJ myAppObj;

uint8_t myTxBuffer[MY_TX_BUFFER_SIZE];
uint8_t myRxBuffer[MY_RX_BUFFER_SIZE];
DRV_SPI_TRANSFER_HANDLE transferHandle;

// Event is received when the transfer is completed.

void APP_SPITransferEventHandler(DRV_SPI_TRANSFER_EVENT event,
DRV_SPI_TRANSFER_HANDLE handle, uintptr_t context)
{
    // The context handle was set to an application specific
    // object. It is now retrievable easily in the event handler.
    MY_APP_OBJ myAppObj = (MY_APP_OBJ *) context;
    
    switch(event)
    {
        case DRV_SPI_TRANSFER_EVENT_COMPLETE:
        {
            // This means the data was transferred.
            break;
        }
        
        case DRV_SPI_TRANSFER_EVENT_ERROR:
        {
            // Error handling here.
            break;
        }
        
        default:
        {
            break;
        }
    }
}

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

// Client registers an event handler with driver. This is done once

DRV_SPI_TransferEventHandlerSet( mySPIHandle, APP_SPITransferEventHandler,
(uintptr_t)&myAppObj );

DRV_SPI_WriteReadTransferAdd(mySPIhandle, myTxBuffer,
MY_TX_BUFFER_SIZE, myRxBuffer,
MY_RX_BUFFER_SIZE, &transferHandle);

if(transferHandle == DRV_SPI_TRANSFER_HANDLE_INVALID)
{
    // Error handling here
}

Remarks

If the client does not want to be notified when the queued transfer request has completed, it does not need to register a callback.