multi-thread I2C synchronization
multi-thread I2C synchronization
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
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
Slave address within the driver is for when esp32 is acting as i2c slave.
Re: multi-thread I2C synchronization
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
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
Same answer. That slave address is not used when esp32 is i2c master.
Re: multi-thread I2C synchronization
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.
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.
-
- Posts: 151
- Joined: Thu Jun 15, 2017 4:54 am
- Location: New Zealand
Re: multi-thread I2C synchronization
You can use a single I2C master controller for both of your I2C slaves provided they will work at the same SCK rate.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?
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.
Re: multi-thread I2C synchronization
@meowsqueak, are you sure about that? The docs say otherwise:meowsqueak wrote:You can use a single I2C master controller for both of your I2C slaves provided they will work at the same SCK rate.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?
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.
* 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
Yes, I too had a doubt this. Any clarification regarding how to go about it?* 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.
Thanks
-
- Posts: 151
- Joined: Thu Jun 15, 2017 4:54 am
- Location: New Zealand
Re: multi-thread I2C synchronization
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
Well, if nothing else you guys got me to put a global mutex on accessing my i2c devices.
John A
John A
Who is online
Users browsing this forum: No registered users and 79 guests