1.37.18.48 SERCOMx_SPI_CallbackRegister Function
C
/* x = SERCOM instance number */ /* SPI master (non-blocking mode) mode */ void SERCOMx_SPI_CallbackRegister(SERCOM_SPI_CALLBACK callback, uintptr_t context ) /* SPI slave mode */ void SERCOMx_SPI_CallbackRegister(SERCOM_SPI_SLAVE_CALLBACK callBack, uintptr_t context )
Summary
Allows application to register a callback with the PLIB
Description
This function sets the pointer to a client/application function to be called "back" when the given SERCOM 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 SERCOMx_SPI_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 SERCOM_SPI_CALLBACK or SERCOM_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. } SERCOM0_SPI_Initialize(); SERCOM0_SPI_CallbackRegister(&APP_SPITransferHandler, (uintptr_t)NULL); if(SERCOM0_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 (SERCOM0_SPI_ErrorGet() == SPI_SLAVE_ERROR_NONE) { nBytesAvailable = SERCOM0_SPI_ReadCountGet(); nBytesRead = SERCOM0_SPI_Read(APP_RxData, nBytesAvailable); } else { // Handle error } } SERCOM0_SPI_CallbackRegister(SPIEventHandler, (uintptr_t) 0);
Remarks
None