2021-12-03 04:08 AM
Hi,
I am using 2 slaves on the I²C bus : touchscreen and FRAM.
I am calling BSP_TS_GetState in the sampleTouch function.
bool STM32TouchController::sampleTouch(int32_t& x, int32_t& y)
{
bool ret = false;
if (!i2cUsed)
{
i2cUsed |= 2;
TS_StateTypeDef state = { 0 };
BSP_TS_GetState(&state);
i2cUsed &= ~2;
if (state.touchDetected)
{
ret = true;
x = state.touchX[0];
y = state.touchY[0];
}
}
return ret;
}
void FRAM_Write(uint16_t address, uint8_t value)
{
while (i2cUsed) HAL_Delay(1);
i2cUsed |= 1;
uint8_t writeBuff[3] = { address >> 8, address, value };
if (HAL_I2C_Master_Transmit(&hi2c2, I2C_FRAM_ADDRESS, writeBuff, 3, 1000) != HAL_OK)
{
Error_Handler();
}
HAL_Delay(1);
i2cUsed &= ~1;
}
I am working with FREERTOS and get stuck in while (i2cUsed) at times when I call FRAM_Write but I don't know why.
The problem arises when BSP_TS_GetState is working and the FRAM_Write function is called.
Do you have any ideas ?
Best regards
Solved! Go to Solution.
2021-12-03 05:26 AM
You can use semaphore. If the task initiating I2C communication locks a binary semaphore, the other task will remain in standby until the semaphore is unlocked.
also i suggest to use osDelay instead of HAL_Delay with RTOS. Since you are using HAL_Delay, any task with equal or lower priority will not be able to run. in your scenario the moment you try to write if the touch screen reading is not a higher priority "while (i2cUsed) HAL_Delay(1);" can never get rid of the line.
2021-12-03 05:26 AM
You can use semaphore. If the task initiating I2C communication locks a binary semaphore, the other task will remain in standby until the semaphore is unlocked.
also i suggest to use osDelay instead of HAL_Delay with RTOS. Since you are using HAL_Delay, any task with equal or lower priority will not be able to run. in your scenario the moment you try to write if the touch screen reading is not a higher priority "while (i2cUsed) HAL_Delay(1);" can never get rid of the line.
2021-12-03 06:40 AM
Hi,
Thank you for your reply !
I only replaced HAL_Delay with osDelay and it works !