i2c thread safety

73 views
Skip to first unread message

Richard Sewell

unread,
Jan 14, 2015, 8:27:31 AM1/14/15
to libbu...@googlegroups.com
I'm seeing some odd behaviour with a PCF8574 i2c port expander, which I'm trying to track down.

I realise that my current code is setting bits on the expander from more than one worker thread. No single bit should be being changed from more than one thread.

I can't find any statement in the docs about thread safety for these i2c operations - are they meant to be thread-safe, or not ? 

Thanks!

Richard

Richard Sewell

unread,
Jan 16, 2015, 10:54:41 AM1/16/15
to libbu...@googlegroups.com
Well, the mystery I was chasing turned out to be an odd electrical issue, nothing to do with threading. So I'll go on assuming this I2C stuff is thread-safe for now, but I'd love to know if it is meant to be,

R.

Datenheld

unread,
Jan 16, 2015, 12:12:00 PM1/16/15
to libbu...@googlegroups.com
Hi Richard,

The current routines cannot be considered thread safe. If this is a major concern for you, it shouldn't be to difficult to achieve synchronization and maybe I can do it on the weekend.

Kind regards
Datenheld aka Bulldog

Richard Sewell

unread,
Jan 17, 2015, 4:10:04 AM1/17/15
to libbu...@googlegroups.com
That's a very kind offer! I don't want to make you rush, I'm happy to add external synchronisation for right now, but I do need to make sure I know what I'm doing.

My project has grown a few threads, for sensor averaging and for control loops, and I hadn't really thought at the time about the implications of that for the shared hardware underneath it all. 

So, for example, right now I have an i2c A2D with multiple inputs, and one thread per input polling and averaging that input. Which obviously means that several threads are contending for the same i2c device, and that probably explains an intermittent hang that I have yet to investigate.

I can also fix these things by adding external synchronisation, but to do that I need to be clear about exactly which resources I need to avoid contention on. 

So, for example, I presume that I need one lock for each i2c bus ? Or do I need a single lock across all the i2c APIs ?

I realise I also need to worry about thread safety for things like .BBBAnalogInput and BBBDigitalOutput and so on. Do you have any thoughts about that ? 

Thanks again,
Richard
Reply all
Reply to author
Forward
0 new messages