Jump to main content

##

# 2.129.8 TWIHSx_CallbackRegister Function

## C

    /* x = TWIHS instance number */

    /* TWIHS master mode */

    void TWIHSx_CallbackRegister(TWIHS_CALLBACK callback, uintptr_t context)

    /* TWIHS slave with interrupt enabled */

    void TWIHSx_CallbackRegister(TWIHS_SLAVE_CALLBACK callback, uintptr_t context)

## Summary

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

## Description

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

TWIHSx_Initialize must have been called for the associated TWIHS instance.

## Parameters

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

TWIHS master mode

    void TWIHS1_Callback(uintptr_t context)
    {
        if(TWIHS1_ErrorGet() == TWIHS_ERROR_NONE)
        {
            //Transfer is completed successfully
        }
        else
        {
            //Error occurred during transfer.
        }
    }

    // Register Callback function which is defined above
    TWIHS1_CallbackRegister(TWIHS1_Callback, (uintptr_t)0);

TWIHS slave in non-blocking/interrupt mode

    bool TWIHS1_Callback ( TWIHS_SLAVE_TRANSFER_EVENT event, uintptr_t context )
    {
    	uint8_t rxData;
    	uint8_t txData;

        switch(event)
        {
            case TWIHS_SLAVE_TRANSFER_EVENT_ADDR_MATCH:

            // Handle address match event

            break;

            case TWIHS_SLAVE_TRANSFER_EVENT_RX_READY:

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

            rxData = TWIHS1_ReadByte();

            break;
            case TWIHS_SLAVE_TRANSFER_EVENT_TX_READY:

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

            TWIHS1_WriteByte(txData);

            break;
        }
    }

    // Register Callback function which is defined above
    TWIHS1_CallbackRegister(TWIHS1_Callback, (uintptr_t)0);

## Remarks

When TWIHS is configured in master mode, the callback function prototype is TWIHS_CALLBACK.

When TWIHS is configured in slave mode, the callback function prototype is TWIHS_SLAVE_CALLBACK.