2025-07-01 8:27 AM - last edited on 2025-07-01 9:08 AM by Andrew Neil
Hello, i see trouble with I2C
HAL_I2C_Master_Transmit(&hi2c2, addr==0 ? I2C_ADDR_DACR : addr, Buff,3,5);
in project with TouchGFX RTOS i have two threads. One for GUI and second is this hw I2C.
On scope i see, that I2C sometimes (random) split send or receive with clock stretch and sometimes complete hang or skip bytes. How is right setup for use it in multitask envi?
Seems when thread block more as timeout in this cmd , then cmd skip bytes or...
Now on U5, but i mean issue is all HAL. And i ask some tips, except DMA and IT, this ofcourse can change situation.
2025-07-01 10:40 AM
A 5ms timeout is small, particularly if other interrupts or task switching is happening. Perhaps it is timing out. SCL stretching when the CPU hasn't sent data yet is expected.
Can you show scope traces where this happens?
Does the function return HAL_OK?
2025-07-01 11:04 AM
I mean timeout is ok , real 3 bytes in fast mode is around 65us. Use bigger timeout isnt solution. I need info how RTOS handle this. Example for priority equal and upper lower as GUI or second next threads...
2025-07-01 11:09 AM
If task switches away for 5ms, it's not enough. Evidence suggests that's happening.
2025-07-02 1:02 PM
Today tests results. My code thread1 TGFX gui using I2C1 for read touch panel. Thread2 using I2C2 manage all other hw.
Test 1 priorityT1 > priorityT2 = I2C2 breaks SCL in the middle or complete hang low... Errorstate 32 Timeout
Test 2 priorityT1 < priorityT2 = I2C2 work perfect , but I2C1 crash on some touch events same situation
Test 3 priorityT1 = priorityT2 = both I2C create SCL stretching , i dont test long time for crash.
Test 4 priorityT1 < priorityT2 + I2C1 one byte write and read leave use direct HAL , more byte read touch changed to IT variant seems dont hangs and on testing works.
I still dont have info how create clean solution for this, because use for example semaphore on one thread can block second and vice versa.
2025-07-02 3:34 PM - edited 2025-07-02 3:34 PM
Yes, the HAL library with its funny "locks" is not RTOS friendly. Be creative.