10 years, 6 months ago.

Sharing I2C with Thread and Ticker

I'm working on a project using the mbed-rtos library, and I need to share the I2C bus between a Thread and a Ticker. I've used a Mutex for this before, but since Ticker functions execute in interrupt context, Mutexes are not allowed. Should I use a Semaphore instead? I've always been told Semaphores should not be used to protect shared resources, but to synchronize threads...

1 Answer

10 years, 6 months ago.

I don't know if it would work, but I don't think it is supposed to work like that. The interrupt is supposed to interrupt the running code, not interrupt it and go waiting until another thread is finished.

Option A I see is using the RTOS timer functions, afaik that one should work properly with a mutex. Option B is to use a ticker, but only use that to signal another thread which does the actual I2C functions, again with mutex protection.

Accepted Answer

The ticker function is periodic, so it wouldn't wait, it would just check the semaphore and give up if it's in use. It needs to run at 2kHz, which means I can't use an RTOS timer. Signaling another thread might work though...

posted by Neil Thiessen 05 May 2014