multi-thread I2C synchronization

rohit269
Posts: 24
Joined: Mon Sep 11, 2017 4:22 am

multi-thread I2C synchronization

Postby rohit269 » Wed Apr 11, 2018 9:02 pm

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

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: multi-thread I2C synchronization

Postby WiFive » Wed Apr 11, 2018 10:00 pm

Slave address within the driver is for when esp32 is acting as i2c slave.

rohit269
Posts: 24
Joined: Mon Sep 11, 2017 4:22 am

Re: multi-thread I2C synchronization

Postby rohit269 » Wed Apr 11, 2018 10:19 pm

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

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: multi-thread I2C synchronization

Postby WiFive » Wed Apr 11, 2018 10:46 pm

Same answer. That slave address is not used when esp32 is i2c master.

rohit269
Posts: 24
Joined: Mon Sep 11, 2017 4:22 am

Re: multi-thread I2C synchronization

Postby rohit269 » Wed Apr 11, 2018 11:01 pm

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.

meowsqueak
Posts: 151
Joined: Thu Jun 15, 2017 4:54 am
Location: New Zealand

Re: multi-thread I2C synchronization

Postby meowsqueak » Mon Apr 16, 2018 6:56 am

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.
Last edited by meowsqueak on Mon Apr 16, 2018 8:16 pm, edited 2 times in total.

vonnieda
Posts: 145
Joined: Tue Nov 07, 2017 3:42 pm

Re: multi-thread I2C synchronization

Postby vonnieda » Mon Apr 16, 2018 2:06 pm

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

rohit269
Posts: 24
Joined: Mon Sep 11, 2017 4:22 am

Re: multi-thread I2C synchronization

Postby rohit269 » Mon Apr 16, 2018 3:52 pm

* 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

meowsqueak
Posts: 151
Joined: Thu Jun 15, 2017 4:54 am
Location: New Zealand

Re: multi-thread I2C synchronization

Postby meowsqueak » Mon Apr 16, 2018 8:15 pm

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.

User avatar
fly135
Posts: 606
Joined: Wed Jan 03, 2018 8:33 pm
Location: Orlando, FL

Re: multi-thread I2C synchronization

Postby fly135 » Tue Apr 17, 2018 2:07 am

Well, if nothing else you guys got me to put a global mutex on accessing my i2c devices. :)

John A

Who is online

Users browsing this forum: Google [Bot] and 209 guests