Page 1 of 2

multi-thread I2C synchronization

Posted: Wed Apr 11, 2018 9:02 pm
by rohit269
I am using an ESP32 board to communicate with an LCD and an I/O expander using an I2C bus. I am working the ESP-IDF environment.

I am using 2 separate threads for the LCD and I/O expander each to concurrently access them. I am trying to use a mutex to implement thread-safe I2C. Do I need to install and delete the I2C drivers(basically setting slave address) within each mutex, every time I enter a thread, since they are 2 different slave addresses?

Is there a better way to perform concurrent i2c communication?

Thanks

Re: multi-thread I2C synchronization

Posted: Wed Apr 11, 2018 10:00 pm
by WiFive
Slave address within the driver is for when esp32 is acting as i2c slave.

Re: multi-thread I2C synchronization

Posted: Wed Apr 11, 2018 10:19 pm
by rohit269
Sorry, allow me to rephrase.

I mean during every context switch, do I need to install and delete the I2C driver within a mutex, for concurrent I2C communication?
Installing the I2C driver entails pointing to the buffer where the slave address is stored.

Is there a better way to perform concurrent I2C communication with 2 different slaves?

Thanks

Re: multi-thread I2C synchronization

Posted: Wed Apr 11, 2018 10:46 pm
by WiFive
Same answer. That slave address is not used when esp32 is i2c master.

Re: multi-thread I2C synchronization

Posted: Wed Apr 11, 2018 11:01 pm
by rohit269
Alright. That means I don't need to install and delete the I2C driver everytime I enter the mutex. I would just need to send the I2C address as the first byte within the mutex, for every I2C operation.

Also, I should be using a single I2C controller(I2C 0 OR I2C 1) for both I2C slaves, since I have access to a single I2C bus on the hardware?

Sorry for being repetitive, I just want to confirm this.

Re: multi-thread I2C synchronization

Posted: Mon Apr 16, 2018 6:56 am
by meowsqueak
rohit269 wrote:I should be using a single I2C controller(I2C 0 OR I2C 1) for both I2C slaves, since I have access to a single I2C bus on the hardware?
You can use a single I2C master controller for both of your I2C slaves provided they will work at the same SCK rate.

EDIT: My comment here is most likely wrong - ignore this:
Generally you don't need a mutex around the ESP-IDF I2C driver, it handles concurrent access for you, but if you want to make sure a longer transaction to a particular slave is not broken up or interleaved with another then you can use a top-level mutex to give your tasks exclusive use of the master controller for as long as you want.

Re: multi-thread I2C synchronization

Posted: Mon Apr 16, 2018 2:06 pm
by vonnieda
meowsqueak wrote:
rohit269 wrote:I should be using a single I2C controller(I2C 0 OR I2C 1) for both I2C slaves, since I have access to a single I2C bus on the hardware?
You can use a single I2C master controller for both of your I2C slaves provided they will work at the same SCK rate.

Generally you don't need a mutex around the ESP-IDF I2C driver, it handles concurrent access for you, but if you want to make sure a longer transaction to a particular slave is not broken up or interleaved with another then you can use a top-level mutex to give your tasks exclusive use of the master controller for as long as you want.
@meowsqueak, are you sure about that? The docs say otherwise:

* The I2C APIs are not thread-safe, if you want to use one I2C port in different tasks,
* you need to take care of the multi-thread issue.

From https://esp-idf.readthedocs.io/en/lates ... TickType_t

Thanks,
Jason

Re: multi-thread I2C synchronization

Posted: Mon Apr 16, 2018 3:52 pm
by rohit269
* The I2C APIs are not thread-safe, if you want to use one I2C port in different tasks,
* you need to take care of the multi-thread issue.
Yes, I too had a doubt this. Any clarification regarding how to go about it?

Thanks

Re: multi-thread I2C synchronization

Posted: Mon Apr 16, 2018 8:15 pm
by meowsqueak
No, I'm not sure now - I hadn't seen that warning before. I thought I'd looked at the source in the past and seen a lock. Sorry for sowing confusion. I'll edit my comment. Incidentally I am using a mutex for my own project, as I needed to ensure that tasks had exclusive use of individual slaves.

Re: multi-thread I2C synchronization

Posted: Tue Apr 17, 2018 2:07 am
by fly135
Well, if nothing else you guys got me to put a global mutex on accessing my i2c devices. :)

John A