Is I2C thread safe on VF50

Hi,
is the I2C Library from the Toradex Libraries 2.4-20200306 thread safe on Win CE 2013?
(And for the future, where can I find these Informations?)

Thank you!

Hi @Sebastian ,

Not sure exactly what you mean by thread safe, but i suppose you want to know if access to the I2C Bus is only possible for one instance at a time. This is the case. Once somebody calls I2c_Open() a system wide mutex is taken and everybody else (even from another process) that calls I2c_Open() will wait until the first instance calls I2c_Close().

But this is only true if everybody in the system uses the i2c library and does not try to access the I2c directly.
NOTE: On Vybrid we used a I2C Driver in older images, make sure you also use newer versions of the image and don’t re-enable the driver or this could break the exclusion concept.

Hi @germano.tx ,

thank you for your quick response. Yes, we are using the i2c library and did not change anything on the dll side. As I understand you, we should be thread safe.

We have two instances of the same i2c channel (I2C1) in our Program. Sometimes the main thread needs to connect to a device and sometimes the second thread needs to connect to another device. Both have their own handle and open the bus before configurating the bus and close it immediately after sending.

But when the first thread had accessed the Bus, then the second thread crashes on opening the Bus with “Exception thrown at 0x415E5EDF (cspddk.dll)…”.

any chance for an easy fix?

Thank you!

Hi @Sebastian ,

I think your problem is not a thread safe issue in the i2c library.

The cspddk.dll (which generates the exception) is only used in the clock lib (which is used by the i2c lib).
I guess there is an issue with init/deinit of the same i2c instance you’re using from the 2 threads.

Our libraries were not designed/tested with this use case in mind (same instance accessed twice from the same process). It should work anyway, but i think you probably do something differently then it was meant to be. Are both threads also calling I2c_Init()/I2c_Deinit() on each HW access? Or only I2c_Open()/I2c_Close()?

Do you get the exception on I2c_Open()? Or on I2c_Init()? or any other function?

Is is possible for you to share part of the code so i can see how you’re using the library functions?

NOTE: I also think that a better idea would be to Call I2c_Init() only once for both threads and share the same handle (we have also a critical section that is stored with the handle, so library calls with the same handle are serialized.

Thank you for your answer.

It is too much hassle. We are just transferring the needed functions into the other thread.
A Handle Exchange was an Option we didn’t think of.

The exceptions were Happening when I2c_Open() was called.

In the second thread we had the Handle initialised permanently and in the main thread (GUI) it was just opened shortly when the user demanded it.

Hi @Sebastian ,

I’m not sure if we still need to investigate more or you will find a workaround?

Let me know if you still waiting for something from our side.

Hi @germano.tx ,
the Programmer working on that part of the Program has added these funktions to his Thread today.
Everything works flawless now.

Thank you for your advice!

You’re welcome