1.25.6.68 FLEXCOMx_TWI_CallbackRegister Function

C

/* x = FLEXCOM instance number */

/* TWI master mode */

void FLEXCOMx_TWI_CallbackRegister(FLEXCOM_TWI_CALLBACK callback, uintptr_t context)			

/* TWI slave with interrupt enabled */

void FLEXCOMx_TWI_CallbackRegister(FLEXCOM_TWI_SLAVE_CALLBACK callback, uintptr_t context)	

Summary

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

Description

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

FLEXCOMx_TWI_Initialize must have been called for the associated FLEXCOM TWI instance.

Parameters

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

TWI master mode

void FLEXCOM0_TWI_Callback(uintptr_t context)
{
    if(FLEXCOM0_TWI_ErrorGet() == FLEXCOM_TWI_ERROR_NONE)
    {
        //Transfer is completed successfully
    }
    else
    {
        //Error occurred during transfer.
    }
}

// Register Callback function which is defined above
FLEXCOM0_TWI_CallbackRegister(FLEXCOM0_TWI_Callback, (uintptr_t)0);

TWI slave in non-blocking/interrupt mode

bool FLEXCOM0_TWI_Callback ( FLEXCOM_TWI_SLAVE_TRANSFER_EVENT event, uintptr_t context )
{
	uint8_t rxData;
	uint8_t txData;
	
    switch(event)
    {
        case FLEXCOM_TWI_SLAVE_TRANSFER_EVENT_ADDR_MATCH:

        // Handle address match event

        break;

        case FLEXCOM_TWI_SLAVE_TRANSFER_EVENT_RX_READY:

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

        rxData = FLEXCOM0_TWI_ReadByte();

        break;
        case FLEXCOM_TWI_SLAVE_TRANSFER_EVENT_TX_READY:

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

        FLEXCOM0_TWI_WriteByte(txData);

        break;        
    }
}

// Register Callback function which is defined above
FLEXCOM0_TWI_CallbackRegister(FLEXCOM0_TWI_Callback, (uintptr_t)0);

Remarks

When TWI is configured in master mode, the callback function prototype is FLEXCOM_TWI_CALLBACK.

When TWI is configured in slave mode, the callback function prototype is FLEXCOM_TWI_SLAVE_CALLBACK.