Page 1 of 1

Using the ESP32 as an I2C Slave

Posted: Thu Aug 15, 2019 9:55 pm
by furrysalamander
I've been scouring the internet all morning, and although there's some great documentation on using the ESP32 as an I2C master, the documentation for using the ESP32 as an I2C slave is proving pretty difficult to come by. The only actual example that I've found is in the repository for the IDF under the I2C examples:
https://github.com/espressif/esp-idf/bl ... ple_main.c

This is a fairly naive implementation though, and since the ESP32 is also the master, it's capable of requesting and sending data right inline with functions it calls for the slave side of things.
https://docs.espressif.com/projects/esp ... ure-driver
The documentation provided focuses almost entirely on using the ESP32 as a master, and only gives two sentences for the slave functions. I think I have the slave reading properly (or at least well enough) implemented, but I'm having a difficult time getting the ESP32 to write back to the master. Can anyone please help me out here?

In my situation, the master writes a single byte to the ESP32, which determines what set of data the ESP32 will send when the master makes a request. IE, if the master were to request an identifier for itself (I'm using a multi-master system), my code currently looks like the following:

Code: Select all

        I2CdataBuffer[0] = lineNumberIndex++;
        i2c_slave_write_buffer(I2C_NUM_0, I2CdataBuffer, 1, 1000 / portTICK_RATE_MS);
Format for calling the function is copied from the ESP-IDF repository. I didn't have any problems implementing this same system previously using an ATMega as the slave. This is called immediately after the ESP32 receives the last byte of the initial. However, the best that I've been able to accomplish is the master receiving the first byte several bytes late, and most of the time the master is receiving garbage. I don't think the turn-around time should be an issue--the master is just an ATMega. Honestly, I'm just a bit frustrated with the lack of documentation on this feature. Is there anyone out there that can shed some light on the situation or that has had any luck using the ESP32 as a slave? If you need more examples from my code, please let me know.