How do I fix a HAL_BUSY condition during I2C comms?
Hi Folks,
This has been driving me crazy. I'm using a Nucleo-L031 board and it's connected to a I2C TCA9548A 1:8 multiplexer. I am trying to read 4 sensors connected to the mux chip. I have 4.7K pullups installed at the Nucleo's I2C pins. The sensor boards come with 15K pullups already installed.
My code will operate correctly for anywhere between 15 - 60 seconds and then I get the HAL_BUSY error which doesn't go away and the I2C1 communication fails.
Is there a way to clear the HAL_BUSY condition?
Here's my code:
while (1)
{
HAL_GPIO_WritePin(GREEN_LED_GPIO_Port, GREEN_LED_Pin, GPIO_PIN_RESET); // turn OFF green LED in pushbutton switch
HAL_Delay(500);
HAL_GPIO_WritePin(GREEN_LED_GPIO_Port, GREEN_LED_Pin, GPIO_PIN_SET); // turn ON green LED in pushbutton switch
HAL_Delay(500);
for (uint8_t channel = 0; channel < 3; channel++) {
uint8_t sensor_number = 1 << channel;
ret = HAL_I2C_IsDeviceReady (&hi2c1, TCA9548A_ADDR, 8, 100); // wait for I2C bus to be ready
ret = HAL_I2C_Master_Transmit_IT(&hi2c1, TCA9548A_ADDR, &sensor_number, 1);
HAL_Delay(35);
if ( ret != HAL_OK ) {
reset_i2c_mux_chip(); // Reset I2C mux chip 1:8
ret = HAL_I2C_IsDeviceReady (&hi2c1, TCA9548A_ADDR, 8, 100); // wait for I2C bus to be ready
ret = HAL_I2C_Master_Transmit_IT(&hi2c1, TCA9548A_ADDR, &sensor_number, 1);
HAL_Delay(35);
}
// Sensor #1.................................................................................................
ret = HAL_I2C_IsDeviceReady (&hi2c1, RTA5543_ADDR, 4, 50); // wait for I2C bus to be ready
ret = HAL_I2C_Mem_Read_IT(&hi2c1, RTA5543_ADDR, VOLTAGE_REG, I2C_MEMADD_SIZE_8BIT, (uint8_t*)sensor_data_buf, 2);
if ( ret != HAL_OK ) {
reset_i2c_mux_chip(); // Reset I2C mux chip 1:8
ret = HAL_I2C_Master_Transmit_IT(&hi2c1, TCA9548A_ADDR, &sensor_number, 1);
HAL_Delay(2);
ret = HAL_I2C_Mem_Read_IT(&hi2c1, RTA5543_ADDR, VOLTAGE_REG, I2C_MEMADD_SIZE_8BIT, (uint8_t*)sensor_data_buf, 2);
}
HAL_Delay(2);
sen_data_buf[index_loop] = sensor_data_buf[0];
sen_data_buf[index_loop+1] = sensor_data_buf[1];
index_loop = index_loop + 2;
// Sensor #2...................................................................................................
ret = HAL_I2C_IsDeviceReady (&hi2c1, RTA5543_ADDR, 4, 50); // wait for I2C bus to be ready
ret = HAL_I2C_Mem_Read_IT(&hi2c1, RTA5543_ADDR, CURRENT_REG, I2C_MEMADD_SIZE_8BIT, sensor_data_buf, 2);
HAL_Delay(2);
if ( ret != HAL_OK ) {
reset_i2c_mux_chip(); // Reset I2C mux chip 1:8
ret = HAL_I2C_Master_Transmit_IT(&hi2c1, TCA9548A_ADDR, &sensor_number, 1);
HAL_Delay(2);
ret = HAL_I2C_Mem_Read_IT(&hi2c1, RTA5543_ADDR, CURRENT_REG, I2C_MEMADD_SIZE_8BIT, sensor_data_buf, 2);
}
sen_data_buf[index_loop] = sensor_data_buf[0];
sen_data_buf[index_loop+1] = sensor_data_buf[1];
index_loop = index_loop + 2;
// Sensor #3..................................................................................................
ret = HAL_I2C_IsDeviceReady (&hi2c1, RTA5543_ADDR, 4, 50); // wait for I2C bus to be ready
ret = HAL_I2C_Mem_Read_IT(&hi2c1, RTA5543_ADDR, TEMP_REG, I2C_MEMADD_SIZE_8BIT, sensor_data_buf, 2);
HAL_Delay(2);
if ( ret != HAL_OK ) {
reset_i2c_mux_chip(); // Reset I2C mux chip 1:8
ret = HAL_I2C_Master_Transmit_IT(&hi2c1, TCA9548A_ADDR, &sensor_number, 1);
HAL_Delay(2);
ret = HAL_I2C_Mem_Read_IT(&hi2c1, RTA5543_ADDR, REMAINING_CAP_REG, I2C_MEMADD_SIZE_8BIT, sensor_data_buf, 2);
}
sen_data_buf[index_loop] = sensor_data_buf[0];
sen_data_buf[index_loop+1] = sensor_data_buf[1];
index_loop = index_loop + 2;
} // end of for(channel = 0)
index_loop = 0;
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
}
Thanks,
Richard
