I2C wire library porting on ESP-IDF

ujurmsde
Posts: 38
Joined: Tue Jan 19, 2021 6:37 am

I2C wire library porting on ESP-IDF

Postby ujurmsde » Thu Jul 01, 2021 7:48 am

Hi all,

I am somewhat confused at the moment. Despite the fact that I have a good knowledge about RTOS and Arduino platform I can't figure out how to use I2C wire library with RTOS/ESP-IDF.

If for example I have bought a sensor which has arduino wire based API and does not have ESP-IDF based I2C API then do I have to write a IDF API using I2C API of ESP-IDF? or I can port a wire based library? If I can use Wire based library what is the need of ESP-IDF I2C API??

https://docs.espressif.com/projects/esp ... s/i2c.html

alqine
Posts: 8
Joined: Thu Jun 24, 2021 8:14 pm

Re: I2C wire library porting on ESP-IDF

Postby alqine » Thu Jul 01, 2021 10:05 am

I would check datasheet for the sensor. Basically you are interested in sensor's I2C address. Some devices have only one pre-defined address. Some have multiple, which are configurable by the user (switch on the device or different wiring of the sensor itself).

Next, check this example of I2C library usage: https://github.com/espressif/esp-idf/tr ... _self_test

Communication with the sensor is always the same:
  • Check which ESP's I2C bus is the sensor connected to. As far as I know, there are two of them. You need to know which bus is used later on when setting up the driver.
  • Create I2C master on the ESP. This is set via .mode = I2C_MODE_MASTER in i2c_config_t. Configuration structure is then passed to i2c_param_config function.
  • Install I2C driver with: i2c_driver_install
  • Start communicating with the sensor. Documentation provides nice order of function calls, you have to use either for writing or reading.
Let me know, if you need more help.

pratik2440
Posts: 25
Joined: Mon Jun 28, 2021 4:55 am

Re: I2C wire library porting on ESP-IDF

Postby pratik2440 » Thu Jul 01, 2021 12:12 pm

If you use Arduino IDE for ESP32, you can use the wire library for I2C devices like you would with any other Arduino project.

If you use ESP-IDF, you will need to write your own code to work according to whatever communication format the I2C device requires you to use (i.e. send addr byte, then read a byte, etc).
The ESP-IDF I2C drivers let you do things like generate an I2C start, send/read a byte, generate ACK/NACK, stop condition, etc.
You have to program the sequence of steps according to your needs.
Check the I2C example sources in IDF examples directory, it is a good place to start.

Also, a good "example" that you can simply build and run on your hardware to talk to the I2C sensor to begin with:
https://github.com/espressif/esp-idf/tr ... /i2c_tools

What sensor is it BTW? Can probably suggest a good place to start if I knew.
Hobbyist and electronic design consultant! (https://PCBArtists.com/)

User avatar
mbratch
Posts: 303
Joined: Fri Jun 11, 2021 1:51 pm

Re: I2C wire library porting on ESP-IDF

Postby mbratch » Fri Jul 02, 2021 1:40 pm

As was asked already, are you using the Arduino framework for ESP32? Then Wire is available. If you are using ESP-IDF there is an option for linking in Arduino libraries.

ujurmsde
Posts: 38
Joined: Tue Jan 19, 2021 6:37 am

Re: I2C wire library porting on ESP-IDF

Postby ujurmsde » Sat Jul 03, 2021 11:31 am

Hi,

Thanks again for your replies.

From the three replies I have got it is clear that my doubt was an obvious one. :roll: :roll:

Well, I dont want to use Arduino IDE as of now. I am using ESP-IDf. From the first answer it got clear how to use I2C API of ESP-IDF.

From the second and third answers, there is still a confusion. The following sentences do not match exactly. Specially @mbratch told that you can use the wire based library with ESP-IDF by porting it.
mbratch wrote: If you are using ESP-IDF there is an option for linking in Arduino libraries.
If you use Arduino IDE for ESP32, you can use the wire library for I2C devices like you would with any other Arduino project.

If you use ESP-IDF, you will need to write your own code to work according to whatever communication format the I2C device requires you to use
By the way I am using current sensor I2C bases INA219.

alqine
Posts: 8
Joined: Thu Jun 24, 2021 8:14 pm

Re: I2C wire library porting on ESP-IDF

Postby alqine » Sat Jul 03, 2021 12:34 pm

There is existing "library" written for ESP-IDF: https://github.com/UncleRus/esp-idf-lib ... nts/ina219

User avatar
mbratch
Posts: 303
Joined: Fri Jun 11, 2021 1:51 pm

Re: I2C wire library porting on ESP-IDF

Postby mbratch » Sat Jul 03, 2021 7:06 pm

ujurmsde wrote:
Sat Jul 03, 2021 11:31 am
...
The following sentences do not match exactly. Specially @mbratch told that you can use the wire based library with ESP-IDF by porting it.
mbratch wrote: If you are using ESP-IDF there is an option for linking in Arduino libraries.
...
As you can see from what I had said, I did not mention porting at all.

pratik2440
Posts: 25
Joined: Mon Jun 28, 2021 4:55 am

Re: I2C wire library porting on ESP-IDF

Postby pratik2440 » Sun Jul 04, 2021 5:53 am

Check this out for ESP-IDF, INA219 and I2C:
https://github.com/pcbreflux/espressif/ ... INA219.cpp

This is a good place to start.
Hobbyist and electronic design consultant! (https://PCBArtists.com/)

Who is online

Users browsing this forum: No registered users and 146 guests