CMSIS-RTOS2 - I2C read/write
Hi
I use the HAL library and CMSIS-RTOS2
infinite loops should not be used in the thread, but I need to wait, for example, for the packet to be sent via I2C, because I need to write and read right after each other, and if I don't wait for the HAL_I2C_STATE_READY state, only writing is performed, but reading does not take place
is the solution to use osThreadYield() ?
static void Task1(void *argument)
{
while(1)
{
HAL_I2C_Master_Transmit_IT(&hi2c1, I2C_addr, write_data_array,sizeof(write_data_array));
while(hi2c1.State != HAL_I2C_STATE_READY)
{
osThreadYield() ;
}
HAL_I2C_Master_Receive_IT(&hi2c1, I2C_addr, read_data_array,sizeof(read_data_array));
while(hi2c1.State != HAL_I2C_STATE_READY)
{
osThreadYield() ;
}
}
}or set a semaphore(event) in the I2C interrupt handler and watch it in the thread?
osSemaphoreId_t I2C_semaphore;
void I2C1_EV_IRQHandler(void)
{
HAL_I2C_EV_IRQHandler(&hi2c1);
if(hi2c1.State == HAL_I2C_STATE_READY)
osSemaphoreRelease(I2C_semaphore);
}
static void Task1(void *argument)
{
while(1)
{
HAL_I2C_Master_Transmit_IT(&hi2c1, I2C_addr, write_data_array,sizeof(write_data_array));
osSemaphoreAcquire(I2C_semaphore, 100);
HAL_I2C_Master_Receive_IT(&hi2c1, I2C_addr, read_data_array,sizeof(read_data_array));
osSemaphoreAcquire(I2C_semaphore, 100);
}
}or not solve it, because it cannot happen that the I2C peripheral never reaches the Ready state?
static void Task1(void *argument)
{
while(1)
{
HAL_I2C_Master_Transmit_IT(&hi2c1, I2C_addr, write_data_array,sizeof(write_data_array));
while(hi2c1.State != HAL_I2C_STATE_READY);
HAL_I2C_Master_Receive_IT(&hi2c1, I2C_addr, read_data_array,sizeof(read_data_array));
while(hi2c1.State != HAL_I2C_STATE_READY);
}
}all variants work, but I'd like to get it right
