Git Product home page Git Product logo

esp_software_i2c's Introduction

Software I2C master for ESP-IDF (ESP32)

What?

ESP32 has only two hardware I2C ports. Sometimes you are already using both ports for I2C slaves but still need to have an I2C master. Bitbanging to the rescue!

Install

Install the library using by cloning it to the components folder of your ESP-IDF project.

$ cd components
$ git clone [email protected]:tuupola/esp-software-i2c.git

Usage

Software driver API mimics the original ESP-IDF hardware master API. Biggest difference is that there is no command link and instead of queuing commands are sent directly to the I2C bus. Example below reads SLAVE_DATA_LENGTH bytes from SLAVE_ADDRESS and dumps it to console. See tuupola/esp-examples for better example.

static const char *TAG = "sofware_i2c";

uint8_t *data_read = (uint8_t *) malloc(SLAVE_DATA_LENGTH);

sw_i2c_master_start();
sw_i2c_master_write_byte((SLAVE_ADDRESS << 1) | I2C_MASTER_READ);
if (SLAVE_DATA_LENGTH > 1) {
    sw_i2c_master_read(data_read, SLAVE_DATA_LENGTH - 1, ACK);
}
sw_i2c_master_read_byte(data_read + SLAVE_DATA_LENGTH - 1, NAK)
sw_i2c_master_stop();

ESP_LOG_BUFFER_HEXDUMP(TAG, data_read, SLAVE_DATA_LENGTH, ESP_LOG_INFO);}

Same code with the original ESP-IDF hardware API would be.

static const char *TAG = "hardware_i2c";

uint8_t *data_read = (uint8_t *) malloc(SLAVE_DATA_LENGTH);
i2c_cmd_handle_t cmd = i2c_cmd_link_create();

i2c_master_start(cmd);
i2c_master_write_byte(cmd, (SLAVE_ADDRESS << 1) | I2C_MASTER_READ, ACK_CHECK_ENABLE);
if (SLAVE_DATA_LENGTH > 1) {
    i2c_master_read(cmd, data_read, SLAVE_DATA_LENGTH - 1, ACK);
}
i2c_master_read_byte(cmd, data_read + SLAVE_DATA_LENGTH - 1, NAK)
i2c_master_stop(cmd);
i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, 1000 / portTICK_RATE_MS);
i2c_cmd_link_delete(cmd);

ESP_LOG_BUFFER_HEXDUMP(TAG, data_read, SLAVE_DATA_LENGTH, ESP_LOG_INFO);

License

The MIT License (MIT). Please see License File for more information.

esp_software_i2c's People

Contributors

tuupola avatar

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.