components/esp-cryptoauthlib/cryptoauthlib/lib/hal/hal_uc3_i2c_asf.c
ATCA_STATUS hal_i2c_receive(ATCAIface iface, uint8_t *rxdata, uint16_t *rxlength)
{
    ATCAIfaceCfg *cfg = atgetifacecfg(iface);
    int retries = cfg->rx_retries;
    uint32_t status = !ATCA_SUCCESS;
    uint16_t rxdata_max_size = *rxlength;

    twi_package_t packet = {
        .chip           = cfg->atcai2c.slave_address >> 1,
        .addr[0]        = { 0 },
        .addr[1]        = { 0 },
        .addr[2]        = { 0 },
        .addr_length    = 0,
        .buffer         = (void*)rxdata,
        .length         = (uint32_t)1
    };

    *rxlength = 0;
    if (rxdata_max_size < 1)
    {
        return ATCA_SMALL_BUFFER;
    }

    while (retries-- > 0 && status != ATCA_SUCCESS)
    {
        if (twi_master_read(i2c_hal_data[cfg->atcai2c.bus].twi_master_instance, &packet) != TWI_SUCCESS)
        {
            status = ATCA_COMM_FAIL;
        }
        else
        {
            status = ATCA_SUCCESS;
        }
    }

    if (status != ATCA_SUCCESS)
    {
        return status;
    }
    if (rxdata[0] < ATCA_RSP_SIZE_MIN)
    {
        return ATCA_INVALID_SIZE;
    }
    if (rxdata[0] > rxdata_max_size)
    {
        return ATCA_SMALL_BUFFER;
    }

    atca_delay_ms(1);

    //Update receive length with first byte received and set to read rest of the data
    packet.length = rxdata[0] - 1;
    packet.buffer = &rxdata[1];

    if (twi_master_read(i2c_hal_data[cfg->atcai2c.bus].twi_master_instance, &packet) != TWI_SUCCESS)
    {
        status = ATCA_COMM_FAIL;
    }
    else
    {
        status = ATCA_SUCCESS;
    }
    if (status != ATCA_SUCCESS)
    {
        return status;
    }

    *rxlength = rxdata[0];

    return ATCA_SUCCESS;
}
/** \brief HAL implementation of I2C receive function for ASF I2C
 * \param[in]    iface     Device to interact with.
 * \param[out]   rxdata    Data received will be returned here.
 * \param[inout] rxlength  As input, the size of the rxdata buffer.
 *                         As output, the number of bytes received.
 * \return ATCA_SUCCESS on success, otherwise an error code.
 */
\brief HAL implementation of I2C receive function for ASF I2C
\param[in]    iface     Device to interact with.
\param[out]   rxdata    Data received will be returned here.
\param[inout] rxlength  As input, the size of the rxdata buffer.
As output, the number of bytes received.
\return ATCA_SUCCESS on success, otherwise an error code.
\ brief HAL implementation of I2C receive function for ASF I2C \ param [ in ] iface Device to interact with . \ param [ out ] rxdata Data received will be returned here . \ param [ inout ] rxlength As input the size of the rxdata buffer . As output the number of bytes received . \ return ATCA_SUCCESS on success otherwise an error code .
ATCA_STATUS hal_i2c_receive(ATCAIface iface, uint8_t *rxdata, uint16_t *rxlength)
{
    ATCAIfaceCfg *cfg = atgetifacecfg(iface);
    int retries = cfg->rx_retries;
    uint32_t status = !ATCA_SUCCESS;
    uint16_t rxdata_max_size = *rxlength;
    twi_package_t packet = {
        .chip           = cfg->atcai2c.slave_address >> 1,
        .addr[0]        = { 0 },
        .addr[1]        = { 0 },
        .addr[2]        = { 0 },
        .addr_length    = 0,
        .buffer         = (void*)rxdata,
        .length         = (uint32_t)1
    };
    *rxlength = 0;
    if (rxdata_max_size < 1)
    {
        return ATCA_SMALL_BUFFER;
    }
    while (retries-- > 0 && status != ATCA_SUCCESS)
    {
        if (twi_master_read(i2c_hal_data[cfg->atcai2c.bus].twi_master_instance, &packet) != TWI_SUCCESS)
        {
            status = ATCA_COMM_FAIL;
        }
        else
        {
            status = ATCA_SUCCESS;
        }
    }
    if (status != ATCA_SUCCESS)
    {
        return status;
    }
    if (rxdata[0] < ATCA_RSP_SIZE_MIN)
    {
        return ATCA_INVALID_SIZE;
    }
    if (rxdata[0] > rxdata_max_size)
    {
        return ATCA_SMALL_BUFFER;
    }
    atca_delay_ms(1);
    packet.length = rxdata[0] - 1;
    packet.buffer = &rxdata[1];
    if (twi_master_read(i2c_hal_data[cfg->atcai2c.bus].twi_master_instance, &packet) != TWI_SUCCESS)
    {
        status = ATCA_COMM_FAIL;
    }
    else
    {
        status = ATCA_SUCCESS;
    }
    if (status != ATCA_SUCCESS)
    {
        return status;
    }
    *rxlength = rxdata[0];
    return ATCA_SUCCESS;
}
ATCA_STATUS hal_i2c_receive ( ATCAIface iface , uint8_t * rxdata , uint16_t * rxlength ) { ATCAIfaceCfg * cfg = atgetifacecfg ( iface ) ; int retries = cfg -> rx_retries ; uint32_t status = ! ATCA_SUCCESS ; uint16_t rxdata_max_size = * rxlength ; twi_package_t packet = { . chip = cfg -> atcai2c . slave_address >> 1 , . addr [ 0 ] = { 0 } , . addr [ 1 ] = { 0 } , . addr [ 2 ] = { 0 } , . addr_length = 0 , . buffer = ( void * ) rxdata , . length = ( uint32_t ) 1 } ; * rxlength = 0 ; if ( rxdata_max_size < 1 ) { return ATCA_SMALL_BUFFER ; } while ( retries -- > 0 && status != ATCA_SUCCESS ) { if ( twi_master_read ( i2c_hal_data [ cfg -> atcai2c . bus ] . twi_master_instance , & packet ) != TWI_SUCCESS ) { status = ATCA_COMM_FAIL ; } else { status = ATCA_SUCCESS ; } } if ( status != ATCA_SUCCESS ) { return status ; } if ( rxdata [ 0 ] < ATCA_RSP_SIZE_MIN ) { return ATCA_INVALID_SIZE ; } if ( rxdata [ 0 ] > rxdata_max_size ) { return ATCA_SMALL_BUFFER ; } atca_delay_ms ( 1 ) ; packet . length = rxdata [ 0 ] - 1 ; packet . buffer = & rxdata [ 1 ] ; if ( twi_master_read ( i2c_hal_data [ cfg -> atcai2c . bus ] . twi_master_instance , & packet ) != TWI_SUCCESS ) { status = ATCA_COMM_FAIL ; } else { status = ATCA_SUCCESS ; } if ( status != ATCA_SUCCESS ) { return status ; } * rxlength = rxdata [ 0 ] ; return ATCA_SUCCESS ; }
\brief HAL implementation of I2C receive function for ASF I2C
 \param[in]    iface     Device to interact with.
\ brief HAL implementation of I2C receive function for ASF I2C \ param [ in ] iface Device to interact with .
//Update receive length with first byte received and set to read rest of the data