Page 1 of 1

Is i2c thread safe?

Posted: Sat Jul 24, 2010 1:47 pm
by thomastaranowski
Anyone know if I can call I2CReadBuf and I2CWriteBuf from multiple tasks safely? I'm guessing no, cause I have a couple tasks doing i2c without mutex protection, and I'm seeing some strange i2c behavior.

Re: Is i2c thread safe?

Posted: Sun Jul 25, 2010 8:13 am
by greengene
nope - wrap that i/f with as much duct tape as you can find!
my current project has 10 i2c devices (8 different) and they are
used in a bunch(?) of tasks and it all gets along fine(?) with just
a single semaphore.

Re: Is i2c thread safe?

Posted: Sun Jul 25, 2010 9:52 am
by v8dave
I agree with greenegene, you need to wrap them in a semaphore or critical section.

By the way, some of the Netburner calls use I2C such as the Real Time Clock (assuming you use this) so you need to wrap calls to this too.

I have I2C running in a number of tasks and I use an OSCritEnter and OSCritExit calls to wrap them with no issues. I also do the same with SPI as you will get the same issue here too.

Good luck
Dave...

Re: Is i2c thread safe?

Posted: Sun Jul 25, 2010 11:09 am
by thomastaranowski
Thanks, I just have a couple calls, so the duct-tape ( a.k.a. semaphore wrapping :) )worked reasonably well. Good call on the RTC, I had forgotten it's over i2c as well.