Skip to main content
Visitor II
December 3, 2021
Solved

Multiple I²C slaves with FREERTOS

  • December 3, 2021
  • 1 reply
  • 1442 views

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

    This topic has been closed for replies.
    Best answer by Muhammed Güler

    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.

    1 reply

    Graduate II
    December 3, 2021

    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.

    MMerc.1Author
    Visitor II
    December 3, 2021

    Hi,

    Thank you for your reply !

    I only replaced HAL_Delay with osDelay and it works !