1.39.18.34 SERCOM_USART_RING_BUFFER_CALLBACK Typedef

C

/* Ring buffer mode */

typedef void (* SERCOM_USART_RING_BUFFER_CALLBACK)(SERCOM_USART_EVENT event, uintptr_t context )

Summary

Defines the data type and function signature for the SERCOM_USART peripheral callback function in the ring buffer mode

Description

This data type defines the function signature for the SERCOM_USART peripheral callback function in the ring buffer mode. The SERCOM_USART peripheral will call back the client's function with this signature when the SERCOM_USART buffer event has occurred.

Precondition

SERCOMx_USART_Initialize must have been called for the given SERCOM_USART peripheral instance. Callback must have been registered using SERCOMx_USART_WriteCallbackRegister or SERCOMx_USART_ReadCallbackRegister routines and event notifications are enabled using the SERCOMx_USART_WriteNotificationEnable or SERCOMx_USART_ReadNotificationEnable routines.

Parameters

Param Description
event Indicates the event for which the callback is called
context Allows the caller to provide a context value (usually a pointerto the callers context for multiple clients).

Returns

None.

Example

uint8_t txBuffer[50];
uint8_t rxBuffer[10];

volatile bool txThresholdEventReceived = false;
volatile bool rxThresholdEventReceived = false;
volatile uint32_t nBytesRead = 0;

void usartWriteEventHandler(SERCOM_USART_EVENT event, uintptr_t context )
{
    txThresholdEventReceived = true;
}

void usartReadEventHandler(SERCOM_USART_EVENT event, uintptr_t context )
{
    uint32_t nBytesAvailable = 0;
    
    if (event == SERCOM_USART_EVENT_READ_THRESHOLD_REACHED)
    {
        nBytesAvailable = SERCOM0_USART_ReadCountGet();
        
        nBytesRead += SERCOM0_USART_Read((uint8_t*)&rxBuffer[nBytesRead], nBytesAvailable);
    }
}

//----------------------------------------------------------//

// Register a callback for write events
SERCOM0_USART_WriteCallbackRegister(usartWriteEventHandler, (uintptr_t) NULL);

// Set TX threshold - TX buffer is empty
SERCOM0_USART_WriteThresholdSet(SERCOM0_USART_WriteBufferSizeGet());

// Enable notifications. Enables notification when threshold condition is reached
SERCOM0_USART_WriteNotificationEnable(true, false);

// Register a callback for read events
SERCOM0_USART_ReadCallbackRegister(usartReadEventHandler, (uintptr_t) NULL);

// Set RX threshold - when 5 characters are available in the receive buffer
SERCOM0_USART_ReadThresholdSet(5);

// Enable RX event notifications
SERCOM0_USART_ReadNotificationEnable(true, false);

Remarks

None.