components/esp-cryptoauthlib/cryptoauthlib/lib/hal/hal_pic32mz2048efm_i2c.c
ATCA_STATUS hal_i2c_receive(ATCAIface iface, uint8_t *rxdata, uint16_t *rxlength)
{
    ATCAIfaceCfg *cfg = atgetifacecfg(iface);
    DRV_I2C_BUFFER_EVENT Transaction = DRV_I2C_BUFFER_EVENT_ERROR;
    uint16_t count;
    uint16_t rxdata_max_size = *rxlength;

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

    read_bufHandle = DRV_I2C_Receive(drvI2CMasterHandle, cfg->atcai2c.slave_address, rxdata, 1, NULL);
    do
    {
        Transaction = DRV_I2C_TransferStatusGet(drvI2CMasterHandle, read_bufHandle);
        if (Transaction == DRV_I2C_BUFFER_EVENT_ERROR)
        {
            return ATCA_COMM_FAIL;
        }
    }
    while (Transaction != DRV_I2C_BUFFER_EVENT_COMPLETE && Transaction != DRV_I2C_BUFFER_EVENT_ERROR);

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

    count = rxdata[0] - 1;
    read_bufHandle = DRV_I2C_Receive(drvI2CMasterHandle, cfg->atcai2c.slave_address, &rxdata[1], count, NULL);
    do
    {
        Transaction = DRV_I2C_TransferStatusGet(drvI2CMasterHandle, read_bufHandle);
        if (Transaction == DRV_I2C_BUFFER_EVENT_ERROR)
        {
            return ATCA_COMM_FAIL;
        }
    }
    while (Transaction != DRV_I2C_BUFFER_EVENT_COMPLETE && Transaction != DRV_I2C_BUFFER_EVENT_ERROR);

    *rxlength = rxdata[0];

    return ATCA_SUCCESS;
}
/** \brief HAL implementation of I2C receive function
 * \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
\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 \ 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);
    DRV_I2C_BUFFER_EVENT Transaction = DRV_I2C_BUFFER_EVENT_ERROR;
    uint16_t count;
    uint16_t rxdata_max_size = *rxlength;
    *rxlength = 0;
    if (rxdata_max_size < 1)
    {
        return ATCA_SMALL_BUFFER;
    }
    read_bufHandle = DRV_I2C_Receive(drvI2CMasterHandle, cfg->atcai2c.slave_address, rxdata, 1, NULL);
    do
    {
        Transaction = DRV_I2C_TransferStatusGet(drvI2CMasterHandle, read_bufHandle);
        if (Transaction == DRV_I2C_BUFFER_EVENT_ERROR)
        {
            return ATCA_COMM_FAIL;
        }
    }
    while (Transaction != DRV_I2C_BUFFER_EVENT_COMPLETE && Transaction != DRV_I2C_BUFFER_EVENT_ERROR);
    if (rxdata[0] < ATCA_RSP_SIZE_MIN)
    {
        return ATCA_INVALID_SIZE;
    }
    if (rxdata[0] > rxdata_max_size)
    {
        return ATCA_SMALL_BUFFER;
    }
    count = rxdata[0] - 1;
    read_bufHandle = DRV_I2C_Receive(drvI2CMasterHandle, cfg->atcai2c.slave_address, &rxdata[1], count, NULL);
    do
    {
        Transaction = DRV_I2C_TransferStatusGet(drvI2CMasterHandle, read_bufHandle);
        if (Transaction == DRV_I2C_BUFFER_EVENT_ERROR)
        {
            return ATCA_COMM_FAIL;
        }
    }
    while (Transaction != DRV_I2C_BUFFER_EVENT_COMPLETE && Transaction != DRV_I2C_BUFFER_EVENT_ERROR);
    *rxlength = rxdata[0];
    return ATCA_SUCCESS;
}
ATCA_STATUS hal_i2c_receive ( ATCAIface iface , uint8_t * rxdata , uint16_t * rxlength ) { ATCAIfaceCfg * cfg = atgetifacecfg ( iface ) ; DRV_I2C_BUFFER_EVENT Transaction = DRV_I2C_BUFFER_EVENT_ERROR ; uint16_t count ; uint16_t rxdata_max_size = * rxlength ; * rxlength = 0 ; if ( rxdata_max_size < 1 ) { return ATCA_SMALL_BUFFER ; } read_bufHandle = DRV_I2C_Receive ( drvI2CMasterHandle , cfg -> atcai2c . slave_address , rxdata , 1 , NULL ) ; do { Transaction = DRV_I2C_TransferStatusGet ( drvI2CMasterHandle , read_bufHandle ) ; if ( Transaction == DRV_I2C_BUFFER_EVENT_ERROR ) { return ATCA_COMM_FAIL ; } } while ( Transaction != DRV_I2C_BUFFER_EVENT_COMPLETE && Transaction != DRV_I2C_BUFFER_EVENT_ERROR ) ; if ( rxdata [ 0 ] < ATCA_RSP_SIZE_MIN ) { return ATCA_INVALID_SIZE ; } if ( rxdata [ 0 ] > rxdata_max_size ) { return ATCA_SMALL_BUFFER ; } count = rxdata [ 0 ] - 1 ; read_bufHandle = DRV_I2C_Receive ( drvI2CMasterHandle , cfg -> atcai2c . slave_address , & rxdata [ 1 ] , count , NULL ) ; do { Transaction = DRV_I2C_TransferStatusGet ( drvI2CMasterHandle , read_bufHandle ) ; if ( Transaction == DRV_I2C_BUFFER_EVENT_ERROR ) { return ATCA_COMM_FAIL ; } } while ( Transaction != DRV_I2C_BUFFER_EVENT_COMPLETE && Transaction != DRV_I2C_BUFFER_EVENT_ERROR ) ; * rxlength = rxdata [ 0 ] ; return ATCA_SUCCESS ; }
\brief HAL implementation of I2C receive function
 \param[in]    iface     Device to interact with.
\ brief HAL implementation of I2C receive function \ param [ in ] iface Device to interact with .