Giter VIP home page Giter VIP logo

esp32-i2cbus's Introduction

I2Cbus

I2C interface library in C++ for working with Espressif ESP32 IoT Development Framework (esp-idf), that supports master mode.

The intention of this library is to read and write to I2C slave devices (most sensors) with ease, by providing quick and specific functions aimed for 8-bit data. It is based on I2Cdev by Jeff Rowberg.

Install

You can clone it right into your project components directory or in your specific library path.

 git clone https://github.com/natanaeljr/esp32-I2Cbus.git I2Cbus

Usage

The ESP32 has two I2C controllers which can control two separated buses, so the library provides two ready-to-use objects:

i2c0 which corresponds to the I2C controller port 0, and

i2c1 which corresponds to the I2C controller port 1.

However you can create your own object as you wish.

Example

// default objects
i2c0.begin(GPIO_NUM_16, GPIO_NUM_17);  // sda, scl, default clock 100 Khz
i2c1.begin(GPIO_NUM_21, GPIO_NUM_22, 400000);  // sda, scl, 400 Khz
// OR create an object which manages controller num 0
I2C_t myI2C(I2C_NUM_0);
// configure and initialize
myI2C.begin(GPIO_NUM_21, GPIO_NUM_22);
myI2C.setTimeout(10);  // default was 1000ms
// start using
myI2C.scanner();
myI2C.writeByte(DEVICE_ADDR, REG_ADDR, DATA);
myI2C.readBytes(DEVICE_ADDR, REG_ADDR, LENGTH, BUFFER);
myI2C.close();

List of methods

// SETUP
esp_err_t begin(gpio_num_t sda_io_num, gpio_num_t scl_io_num, uint32_t clk_speed = I2CBUS_CLOCKSPEED_DEFAULT);
esp_err_t begin(gpio_num_t sda_io_num, gpio_num_t scl_io_num, gpio_pullup_t sda_pullup_en, gpio_pullup_t scl_pullup_en, uint32_t clk_speed = I2CBUS_CLOCKSPEED_DEFAULT);
esp_err_t close();
void setTimeout(uint32_t ms);

// WRITING
esp_err_t writeBit(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint8_t data, int32_t timeout = -1);
esp_err_t writeBits(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint8_t data, int32_t timeout = -1);
esp_err_t writeByte(uint8_t devAddr, uint8_t regAddr, uint8_t data, int32_t timeout = -1);
esp_err_t writeBytes(uint8_t devAddr, uint8_t regAddr, size_t length, const uint8_t *data, int32_t timeout = -1);

// READING
esp_err_t readBit(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint8_t *data, int32_t timeout = -1);
esp_err_t readBits(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint8_t *data, int32_t timeout = -1);
esp_err_t readByte(uint8_t devAddr, uint8_t regAddr, uint8_t *data, int32_t timeout = -1);
esp_err_t readBytes(uint8_t devAddr, uint8_t regAddr, size_t length, uint8_t *data, int32_t timeout = -1);

// TOOLS
esp_err_t testConnection(uint8_t devAddr, int32_t timeout = -1);
void scanner();

Each method have an optional custom timeout for that specific call, if this value is not passed (default -1) or is < 0, it will use the default timeout. You can change the default timeout by calling setTimeout(ms).

@see header file for more notes and descriptions.

Menuconfig

You can change some settings (for debugging) in menuconfig under components and I2Cbus.

menuconfig-I2Cbus


See also: SPIbus library https://github.com/natanaeljr/esp32-SPIbus

Copyright © 2017 Natanael Josue Rabello [[email protected]]

esp32-i2cbus's People

Contributors

amikhalev avatar natanaeljr avatar umateusz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

esp32-i2cbus's Issues

I2C_MASTER_LAST_NACK not declared?

trying to compile i get the following error:

 /src/I2Cbus.cpp:189:40: error: 'I2C_MASTER_LAST_NACK' was not declared in this scope
 i2c_master_read(cmd, data, length, I2C_MASTER_LAST_NACK);

Sporadic read errors in readBytes

Dear Natanael,
got a copy from your I2Cbus.cpp in my project to read several chips as there is an MPU6050, MCP3112 or an MCP4018. At first it worked great, really appreciate your work on that topic, and it is very easy to use.
Hence after a while i found sporadic errors in my data read, while reading 5 times a second, something line every minute one readout gave me data that were not plausible, like g-forces beyond scale.
Investigating this deeper, a snippet of my data i read is below, i found e.g. at timestamp 00:02:28.599 apparently data looks shifted by one byte to the right.
It took me a day to figure out what's going wrong, and finally found the issue. Looking at following code recommend in esp-idf issue 1807: espressif/esp-idf#1807 i see there is a bit different handling for reading bytes.

Instead of the used sequence in I2Cbus.cpp in here, where all is done in one step, exactly:
start
write_byte addr
write_byte reg
start
write_byte addr
read len
stop
cmd_begin

The following two step sequence is recommended:
start
write_byte addr
write_byte reg
stop
cmd_begin

start
write_byte addr
read len
stop
cmd_begin

And doing this all went fine, no more read error, even over night all went fine. While still happy about this success, I wanted to share this change with you, and kindly ask if you could merge it into you very useful library.

The file where i did this change is here:
https://github.com/iltis42/OpenIVario/blob/master/components/I2Cbus/src/I2Cbus.cpp

Best regards and looking forward to hear from you, thanks, Eckhard

I (00:02:28.398) I2Cbus: [port:0, slave:0x68] Read_ 6 bytes from register 0x3B, data: 0x1F 0xA3 0xFF 0xDF 0x01 0x21 ^M
I (00:02:28.599) I2Cbus: [port:0, slave:0x68] Read_ 6 bytes from register 0x3B, data: 0xA3 0xFF 0xE3 0x01 0x0E 0x28 ^M
I (00:02:29.038) I2Cbus: [port:0, slave:0x68] Read_ 6 bytes from register 0x3B, data: 0x1F 0xA3 0xFF 0xDF 0x01 0x21 

share one I2C_t object between more than one FREERTOS task

Hei natanaeljr, i like this class very much because it's super easy to use.
Unfortunately I got some issues, when trying to share one instance of it between more than one tasks in freertos.
What do you recomment or what is best practice to eg. have one task running a keypad and another running a display, both communicating over the same I2C instance? Thanks a lot.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.