1.37.18.73 SERCOMx_I2C_CallbackRegister Function

C

/* x = SERCOM instance number */

/* I2C master mode */

void SERCOMx_I2C_CallbackRegister(SERCOM_I2C_CALLBACK callback, uintptr_t context)			

/* I2C slave in interrupt mode */

void SERCOMx_I2C_CallbackRegister(SERCOM_I2C_SLAVE_CALLBACK callback, uintptr_t context)	

Summary

Sets the pointer to the function (and it's context) to be called when the given SERCOM I2C's transfer events occur.

Description

This function sets the pointer to a client/application function to be called "back" when the given SERCOM I2C'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.

Precondition

SERCOMx_I2C_Initialize must have been called for the associated SERCOM I2C instance.

Parameters

Param Description
callback A pointer to a function with a calling signature defined by the SERCOM_I2C_CALLBACK (in I2C master mode) or SERCOM_I2C_SLAVE_CALLBACK (in I2C slave mode) 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

I2C master mode

void SERCOM0_I2C_Callback(uintptr_t context)
{
    if(SERCOM0_I2C_ErrorGet() == SERCOM_I2C_ERROR_NONE)
    {
        //Transfer is completed successfully
    }
    else
    {
        //Error occurred during transfer.
    }
}

// Register Callback function which is defined above
SERCOM0_I2C_CallbackRegister(SERCOM0_I2C_Callback, (uintptr_t)NULL);

I2C slave in non-blocking/interrupt mode

bool SERCOM0_I2C_Callback ( SERCOM_I2C_SLAVE_TRANSFER_EVENT event, uintptr_t context )
{
	uint8_t rxData;
	uint8_t txData;
	
    switch(event)
    {
        case SERCOM_I2C_SLAVE_TRANSFER_EVENT_ADDR_MATCH:

        // Handle address match event

        break;

        case SERCOM_I2C_SLAVE_TRANSFER_EVENT_RX_READY:

        // I2C master is writing data to I2C slave.
        // Read the received data byte.

        rxData = SERCOM0_I2C_ReadByte();

        break;
        case SERCOM_I2C_SLAVE_TRANSFER_EVENT_TX_READY:

        // I2C master is reading data from I2C slave.
        // Provide data to I2C master.

        SERCOM0_I2C_WriteByte(txData);

        break;

        case SERCOM_I2C_SLAVE_TRANSFER_EVENT_STOP_BIT_RECEIVED:

        // Handle stop bit received event

        break;
    }
}

// Register Callback function which is defined above
SERCOM0_I2C_CallbackRegister(SERCOM0_I2C_Callback, 0);

Remarks

When I2C is configured in master mode, the callback function prototype is SERCOM_I2C_CALLBACK.

When I2C is configured in slave mode, the callback function prototype is SERCOM_I2C_SLAVE_CALLBACK.