Jump to main content

##

# 1.1.2.4.1 DRV_I2C_Initialize Function

## C

    SYS_MODULE_OBJ DRV_I2C_Initialize
    (
        const SYS_MODULE_INDEX drvIndex,
        const SYS_MODULE_INIT * const init
    )

## Summary

Initializes the I2C instance for the specified driver index.

## Description

This routine initializes the I2C driver instance for the specified driver index, making it ready for clients to open and use it. The initialization data is specified by the init parameter. The initialization may fail if the number of driver objects allocated are insufficient or if the specified driver instance is already initialized. The driver instance index is independent of the I2C peripheral instance it is associated with. For example, driver instance 0 can be assigned to I2C peripheral instance 2.

## Precondition

None.

## Parameters

Param | Description
--- | ---
drvIndex | Identifier for the instance to be initialized
init | Pointer to the init data structure containing any data necessary to initialize the driver.

## Returns

If successful, returns a valid handle to a driver instance object. Otherwise, returns SYS_MODULE_OBJ_INVALID.

## Example

    // The following code snippet shows an example I2C driver initialization.

    SYS_MODULE_OBJ objectHandle;

    DRV_I2C_CLIENT_OBJ drvI2C0ClientObjPool[DRV_I2C_CLIENTS_NUMBER_IDX0] = {0};
    DRV_I2C_TRANSFER_OBJ drvI2C0TransferObjPool[DRV_I2C_QUEUE_SIZE_IDX0] = {0};

        DRV_I2C_PLIB_INTERFACE drvI2C0PLibAPI = {
            .read = (DRV_I2C_PLIB_READ)TWIHS0_Read,
            .write = (DRV_I2C_PLIB_WRITE)TWIHS0_Write,
            .writeRead = (DRV_I2C_PLIB_WRITE_READ)TWIHS0_WriteRead,
            .errorGet = (DRV_I2C_PLIB_ERROR_GET)TWIHS0_ErrorGet,
            .callbackRegister = (DRV_I2C_PLIB_CALLBACK_REGISTER)TWIHS0_CallbackRegister,
        };

        DRV_I2C_INIT drvI2C0InitData = {

            .i2cPlib = &drvI2C0PLibAPI,
            .numClients = DRV_I2C_CLIENTS_NUMBER_IDX0
            .clientObjPool = (uintptr_t)&drvI2C0ClientObjPool[0],
            .interruptI2C = DRV_I2C_INT_SRC_IDX0,
            .queueSize = DRV_I2C_QUEUE_SIZE_IDX0,
            .transferObj = (uintptr_t)&drvI2C0TransferObj[0],
            .clockSpeed = DRV_I2C_CLOCK_SPEED_IDX0,
        };

        objectHandle = DRV_I2C_Initialize(DRV_I2C_INDEX_0, (SYS_MODULE_INIT*)&drvI2C0InitData);
        if (objectHandle == SYS_MODULE_OBJ_INVALID)
        {
            // Handle error
        }

## Remarks

This routine must be called before any other I2C routine is called. This routine should only be called once during system initialization.