/* This function handles I2C Communication timeout for specific usage of TXE flag.  */
static ald_status_t i2c_wait_txe_to_timeout(ald_i2c_handle_t *hperh, uint32_t timeout)
/* This function handles I2C Communication timeout for specific usage of TXE flag.  */
static ald_status_t i2c_wait_txe_to_timeout(ald_i2c_handle_t *hperh, uint32_t timeout)
{
    uint32_t tickstart = ald_get_tick();
    while (ALD_I2C_GET_FLAG(hperh, ALD_I2C_STAT_TXE) == RESET)
    {
        if (ALD_I2C_GET_IT_FLAG(hperh, ALD_I2C_IT_ARLO))
        {
            hperh->error_code |= ALD_I2C_ERROR_ARLO;
            return ALD_ERROR;
        }
        if (ALD_I2C_GET_IT_FLAG(hperh, ALD_I2C_IT_NACK) == SET)
        {
            hperh->error_code |= ALD_I2C_ERROR_AF;
            return ALD_ERROR;
        }
        if ((timeout == 0) || ((ald_get_tick() - tickstart) > timeout))
        {
            hperh->error_code |= ALD_I2C_ERROR_TIMEOUT;
            return ALD_ERROR;
        }
    }
    return ALD_OK;
}