NameOfTheDragon
Posts: 21
Joined: Mon Nov 16, 2015 12:49 am

I2C access in multi-threaded app

Thu Feb 11, 2016 4:49 pm

Does anyone have an example of a C# app that uses I2C devices in multiple threads? I'm having a bit of a problem with mine, it's one of those where if I single step it everything works and if I run it full sped everything messes up. The i2C traffic is actually getting corrupted (as viewed on my logic analyzer) which surprised me a bit. I kind of assumed that I2C operations would be atomic, but it looks like that's not necessarily the case. This clearly seems like a race condition.

So, if anyone has done multi-threaded I2C access, what do you need to do to ensure thread-safety?

I have some ideas on how to approach this but the documentation is a bit silent on threading requirements, so I thought I'd ask if anyone has already done it successfully before I go off down some dead ends :)

Thanks,
Tim

rzusman
Posts: 346
Joined: Fri Jan 01, 2016 10:27 pm

Re: I2C access in multi-threaded app

Thu Feb 11, 2016 5:23 pm

I haven't done this myself, but if I had to, I'd have one thread handle ALL the I2C communication, and have that thread share the data with all the other ones.

danjperron
Posts: 3434
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: I2C access in multi-threaded app

Thu Feb 11, 2016 5:30 pm

Did you try to add some semaphore in there.

I do use a lot of multithreated code and when I do have multiple thread which need to send commands to a serial port I do use a locking mecanism to prevent collision.

Just add a semaphore to lock in your threads and unlock it when you got the reception.

B.T.W. you need really to emphase about timing and synchronisation. Who has priority , How do I lock, When I unlock. What do I do in case of lock down. etc...

NameOfTheDragon
Posts: 21
Joined: Mon Nov 16, 2015 12:49 am

Re: I2C access in multi-threaded app

Fri Feb 12, 2016 6:05 am

Thanks, yes I considered that. However, locking is not always sufficient these days as asynchronous operations can happen on any thread, including the same thread that is holding the lock, so if you're not careful you can get into trouble with re-entrancy. I'm not saying you can't do it, just that you have to have your wits about you :)

I'm currently considering a solution based on the Reactive Extensions for .NET ("Rx"), I have used them before on desktop Windows to solve sequencing and thread-safety issues with serial ports and I really like the "push" (reactive, event driven) style of programming. I wasn't sure if Rx would work on Universal Windows Platform, but I have installed the NuGet packages and Visual Studio says it's all compatible, so I might be all set. I've started to develop some wrapper classes for the built-in I2C stuff and if it works out well then I will push it to a public repository for other people to use.

Thanks,
Tim

selom
Posts: 7
Joined: Thu Jan 28, 2016 6:56 pm

Re: I2C access in multi-threaded app

Thu Feb 18, 2016 8:35 pm

Locking and async/await operations don't work at all. Don't even try to do it. There are some ways around it for example http://www.dzhang.com/blog/2012/08/29/s ... rp-methods which I've used in normal windows apps but not iot.

Seems like you need to rethink the app's core design, rather than attempting to throw in locks.

NameOfTheDragon
Posts: 21
Joined: Mon Nov 16, 2015 12:49 am

Re: I2C access in multi-threaded app

Thu Feb 18, 2016 9:04 pm

I think it is fairly self-evidently a bad idea to mix asynchrony and multi-threading in the way discussed by the linked article. A lock inside an anachronous method is probably a fairly direct path to deadlock. Still, it is a bold claim that locks and asynchrony "don't work at all". Can you clarify what you mean by that?

What I am working on is a library, not an app, although I did create an app to demonstrate use of the library, which pretty quickly threw up this issue as soon as I tried to use two devices on the bus. So you are right about one thing: the core design needs to change. I've been pulled onto another project for the time being, but I'm evaluating several different approaches. I want the library to work under all conditions and not abdicate responsibility for the threading issues.

praktronics
Posts: 1
Joined: Sat Jan 06, 2018 9:12 am

Re: I2C access in multi-threaded app

Sat Jan 06, 2018 9:40 am

Hi

Facing the same issue. Did you find a reliable way to use I2C across multiple threads?

Return to “Windows 10 for IoT”