components/esp-cryptoauthlib/cryptoauthlib/lib/hal/hal_pic32mz2048efm_i2c.c
ATCA_STATUS hal_i2c_init(void *hal, ATCAIfaceCfg *cfg)
{
    DRV_I2C_Object *tempI2cObj;

    if (cfg->atcai2c.bus >= MAX_I2C_BUSES)
    {
        return ATCA_COMM_FAIL;
    }
    ATCAI2CMaster_t* data = &i2c_hal_data[cfg->atcai2c.bus];

    if (data->ref_ct <= 0)
    {
        // Bus isn't being used, enable it
        tempI2cObj = (DRV_I2C_Object*)(cfg->cfg_data);
        drvI2CMasterHandle = DRV_I2C_Open(tempI2cObj->i2cDriverInstanceIndex, DRV_IO_INTENT_NONBLOCKING | DRV_IO_INTENT_READWRITE);
        if (drvI2CMasterHandle == DRV_HANDLE_INVALID)
        {
            return ATCA_COMM_FAIL;
        }

        // store this for use during the release phase
        data->bus_index = cfg->atcai2c.bus;
        // buses are shared, this is the first instance
        data->ref_ct = 1;
    }
    else
    {
        // Bus is already is use, increment reference counter
        data->ref_ct++;
    }

    ((ATCAHAL_t*)hal)->hal_data = data;

    return ATCA_SUCCESS;
}
/** \brief initialize an I2C interface using given config
 * \param[in] hal  opaque ptr to HAL data
 * \param[in] cfg  interface configuration
 * \return ATCA_SUCCESS on success, otherwise an error code.
 */
\brief initialize an I2C interface using given config
\param[in] hal  opaque ptr to HAL data
\param[in] cfg  interface configuration
\return ATCA_SUCCESS on success, otherwise an error code.
\ brief initialize an I2C interface using given config \ param [ in ] hal opaque ptr to HAL data \ param [ in ] cfg interface configuration \ return ATCA_SUCCESS on success otherwise an error code .
ATCA_STATUS hal_i2c_init(void *hal, ATCAIfaceCfg *cfg)
{
    DRV_I2C_Object *tempI2cObj;
    if (cfg->atcai2c.bus >= MAX_I2C_BUSES)
    {
        return ATCA_COMM_FAIL;
    }
    ATCAI2CMaster_t* data = &i2c_hal_data[cfg->atcai2c.bus];
    if (data->ref_ct <= 0)
    {
        tempI2cObj = (DRV_I2C_Object*)(cfg->cfg_data);
        drvI2CMasterHandle = DRV_I2C_Open(tempI2cObj->i2cDriverInstanceIndex, DRV_IO_INTENT_NONBLOCKING | DRV_IO_INTENT_READWRITE);
        if (drvI2CMasterHandle == DRV_HANDLE_INVALID)
        {
            return ATCA_COMM_FAIL;
        }
        data->bus_index = cfg->atcai2c.bus;
        data->ref_ct = 1;
    }
    else
    {
        data->ref_ct++;
    }
    ((ATCAHAL_t*)hal)->hal_data = data;
    return ATCA_SUCCESS;
}
ATCA_STATUS hal_i2c_init ( void * hal , ATCAIfaceCfg * cfg ) { DRV_I2C_Object * tempI2cObj ; if ( cfg -> atcai2c . bus >= MAX_I2C_BUSES ) { return ATCA_COMM_FAIL ; } ATCAI2CMaster_t * data = & i2c_hal_data [ cfg -> atcai2c . bus ] ; if ( data -> ref_ct <= 0 ) { tempI2cObj = ( DRV_I2C_Object * ) ( cfg -> cfg_data ) ; drvI2CMasterHandle = DRV_I2C_Open ( tempI2cObj -> i2cDriverInstanceIndex , DRV_IO_INTENT_NONBLOCKING | DRV_IO_INTENT_READWRITE ) ; if ( drvI2CMasterHandle == DRV_HANDLE_INVALID ) { return ATCA_COMM_FAIL ; } data -> bus_index = cfg -> atcai2c . bus ; data -> ref_ct = 1 ; } else { data -> ref_ct ++ ; } ( ( ATCAHAL_t * ) hal ) -> hal_data = data ; return ATCA_SUCCESS ; }
\brief initialize an I2C interface using given config
 \param[in] hal  opaque ptr to HAL data
 \param[in] cfg  interface configuration
 \return ATCA_SUCCESS on success, otherwise an error code.
\ brief initialize an I2C interface using given config \ param [ in ] hal opaque ptr to HAL data \ param [ in ] cfg interface configuration \ return ATCA_SUCCESS on success otherwise an error code .
// Bus isn't being used, enable it // store this for use during the release phase // buses are shared, this is the first instance // Bus is already is use, increment reference counter