Jump to main content

##

# 2.110.14 SERCOMx_USART_ReadCallbackRegister Function

## C

    /* x = SERCOM instance number */

    /* Non-blocking mode */

    void SERCOMx_USART_ReadCallbackRegister( SERCOM_USART_CALLBACK callback, uintptr_t context )

    /* Ring buffer mode */

    void SERCOMx_USART_ReadCallbackRegister( SERCOM_USART_RING_BUFFER_CALLBACK callback, uintptr_t context)

## Summary

Sets the pointer to the function (and it's context) to be called when the given USART's read events occur.

## Description

This function sets the pointer to a client function to be called "back" when the given USART's read 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.

## Precondition

SERCOMx_USART_Initialize must have been called for the associated USART instance.

## Parameters

Param | Description
--- | ---
callback | Pointer to the function that will be called when a read request has completed. Setting this to NULL will disable the callback feature.
context | A value (usually a pointer) passed (unused) into the function identified by the callback parameter.

## Returns

None.

## Example

Non-blocking mode

    // Data tracking what each of my instances is doing.
    char myData[2];

    void usartReadEventHandler ( uintptr_t context )
    {
        if(USART_ERROR_NONE != SERCOM0_USART_ErrorGet())
        {
            //Handle error case
        }
        else
        {
            //Transfer completed successfully
        }
    }

    SERCOM0_USART_ReadCallbackRegister(usartReadEventHandler, (uintptr_t) NULL);

Ring buffer mode

    uint8_t rxBuffer[50];
    uint32_t nBytes;

    void usartReadEventHandler(SERCOM_USART_EVENT event, uintptr_t context )
    {
        uint32_t nBytesAvailable = 0;

        if (event == SERCOM_USART_EVENT_READ_THRESHOLD_REACHED)
        {
            // Receiver should have the thershold number of bytes in the receive buffer
            nBytesAvailable = SERCOM0_USART_ReadCountGet();

            nBytesRead += SERCOM0_USART_Read((uint8_t*)&rxBuffer[nBytesRead], nBytesAvailable);
        }
    }

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

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

    // Set a threshold value to receive a callback after every 10 characters are received
    SERCOM0_USART_ReadThresholdSet(10);

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

## Remarks

None