components/esp-cryptoauthlib/cryptoauthlib/lib/hal/hal_uc3_i2c_asf.c
ATCA_STATUS hal_i2c_init(void *hal, ATCAIfaceCfg *cfg)
{
    twi_options_t opt;
    ATCA_STATUS status = !ATCA_SUCCESS;
    ATCAI2CMaster_t* data = &i2c_hal_data[cfg->atcai2c.bus];


    if (data->ref_ct <= 0)
    {
        // Bus isn't being used, enable it
        switch (cfg->atcai2c.bus)
        {

        case 2:
            data->twi_master_instance = &AVR32_TWI;
            static const gpio_map_t TWI_GPIO_MAP =
            {
                { AVR32_TWI_SDA_0_0_PIN, AVR32_TWI_SDA_0_0_FUNCTION },
                { AVR32_TWI_SCL_0_0_PIN, AVR32_TWI_SCL_0_0_FUNCTION }
            };

            gpio_enable_module(TWI_GPIO_MAP, sizeof(TWI_GPIO_MAP) / sizeof(TWI_GPIO_MAP[0]));
            // options settings
            opt.pba_hz = 48000000;
            opt.speed = 100000;
            opt.chip = 0;

            // initialize TWI driver with options
            twi_master_init(&AVR32_TWI, &opt);
            break;
        default:
            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)
{
    twi_options_t opt;
    ATCA_STATUS status = !ATCA_SUCCESS;
    ATCAI2CMaster_t* data = &i2c_hal_data[cfg->atcai2c.bus];
    if (data->ref_ct <= 0)
    {
        switch (cfg->atcai2c.bus)
        {
        case 2:
            data->twi_master_instance = &AVR32_TWI;
            static const gpio_map_t TWI_GPIO_MAP =
            {
                { AVR32_TWI_SDA_0_0_PIN, AVR32_TWI_SDA_0_0_FUNCTION },
                { AVR32_TWI_SCL_0_0_PIN, AVR32_TWI_SCL_0_0_FUNCTION }
            };
            gpio_enable_module(TWI_GPIO_MAP, sizeof(TWI_GPIO_MAP) / sizeof(TWI_GPIO_MAP[0]));
            opt.pba_hz = 48000000;
            opt.speed = 100000;
            opt.chip = 0;
            twi_master_init(&AVR32_TWI, &opt);
            break;
        default:
            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 ) { twi_options_t opt ; ATCA_STATUS status = ! ATCA_SUCCESS ; ATCAI2CMaster_t * data = & i2c_hal_data [ cfg -> atcai2c . bus ] ; if ( data -> ref_ct <= 0 ) { switch ( cfg -> atcai2c . bus ) { case 2 : data -> twi_master_instance = & AVR32_TWI ; static const gpio_map_t TWI_GPIO_MAP = { { AVR32_TWI_SDA_0_0_PIN , AVR32_TWI_SDA_0_0_FUNCTION } , { AVR32_TWI_SCL_0_0_PIN , AVR32_TWI_SCL_0_0_FUNCTION } } ; gpio_enable_module ( TWI_GPIO_MAP , sizeof ( TWI_GPIO_MAP ) / sizeof ( TWI_GPIO_MAP [ 0 ] ) ) ; opt . pba_hz = 48000000 ; opt . speed = 100000 ; opt . chip = 0 ; twi_master_init ( & AVR32_TWI , & opt ) ; break ; default : 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 // options settings // initialize TWI driver with options // store this for use during the release phase // buses are shared, this is the first instance // Bus is already is use, increment reference counter