espressif / esp-idf

Espressif IoT Development Framework. Official development framework for Espressif SoCs.
Apache License 2.0
13.19k stars 7.17k forks source link

I2C communication failures on ESP32-S3 (works on ESP32) (IDFGH-7138) #8741

Open CSHaze opened 2 years ago

CSHaze commented 2 years ago

Environment

Problem Description

I2C device communication that works on the standard ESP32 does not work on the ESP32-s3.

Expected Behavior

I would expect I2C functionality to work similarly on both devices.

Actual Behavior

I2C writes work correctly on the standard ESP32 but do not work on the ESP32-s3.

Steps to reproduce

  1. Attach an I2C device (I am using an HTU21d)
  2. Run the scan program on a standard ESP32 (https://github.com/UncleRus/esp-idf-i2cscan)
  3. Observe the scan shows the correct output.
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
    00:          -- -- -- -- -- -- -- -- -- -- -- -- --
    10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    40: 40 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    70: -- -- -- -- -- -- -- --
  4. Run the scan program on an ESP32-s3
  5. Observe the scan does not find the device
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --

Note: I have tried other libraries, communication calls, and configurations (pullups, clk freq, gpio, etc) through IC2, all of which are unable to write to the device. I figured this was the simplest example.

Code to reproduce this issue

#include <driver/i2c.h>
#include <esp_log.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <stdio.h>

#ifndef APP_CPU_NUM
#define APP_CPU_NUM PRO_CPU_NUM
#endif

#define SDA_PIN 15
#define SCL_PIN 16

static const char *TAG = "i2cscanner";

void task(void *ignore)
{

    i2c_config_t conf;
    conf.mode = I2C_MODE_MASTER;
    conf.sda_io_num = SDA_PIN;
    conf.scl_io_num = SCL_PIN;
    conf.sda_pullup_en = false;
    conf.scl_pullup_en = false;
    conf.master.clk_speed = 400000;
    i2c_param_config(I2C_NUM_0, &conf);

   i2c_driver_install(I2C_NUM_0, I2C_MODE_MASTER, 0, 0, 0);

    while (1)
    {
        esp_err_t res;
        printf("     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f\n");
        printf("00:         ");
        for (uint8_t i = 3; i < 0x78; i++)
        {
            i2c_cmd_handle_t cmd = i2c_cmd_link_create();
            i2c_master_start(cmd);
            i2c_master_write_byte(cmd, (i << 1) | I2C_MASTER_WRITE, 1 /* expect ack */);
            i2c_master_stop(cmd);

            res = i2c_master_cmd_begin(I2C_NUM_0, cmd, 10 / portTICK_PERIOD_MS);
            if (i % 16 == 0)
                printf("\n%.2x:", i);
            if (res == 0)
                printf(" %.2x", i);
            else
                printf(" --");
            i2c_cmd_link_delete(cmd);
        }
        printf("\n\n");
        vTaskDelay(pdMS_TO_TICKS(1000));
    }
}

void app_main()
{
    // Start task
    xTaskCreatePinnedToCore(task, TAG, configMINIMAL_STACK_SIZE * 8, NULL, 5, NULL, APP_CPU_NUM);
}
ginkgm commented 2 years ago

Hi @CSHaze , Have you checked what's on the lines by a logic analyzer? And are the lines pulled up by resistors correctly?

Michael

CSHaze commented 2 years ago

Hi @ginkgm, unfortunately I do not have access to a logic analyzer.

The Htu21d board I am using has built in pull up resistors. I have also tried unsoldering them and using the esp32-s3 internal pull ups and neither seems to work on the esp32-s3. The weird thing is, everything works correctly on the old esp32 I have. I am trying to swap to the newer s3 board.

I should be getting a bme280 board in the next couple days and I will test with that.

Are there differences in the i2c config or calls between the esp32 and esp32-s3? I did not see any differences in the docs.

igrr commented 2 years ago

It might not be the cause of the issue here, but could you try to zero-initialize the config structure (i2c_config_t) before filling it? Currently there is a field clk_flags which isn't initialized and may contain random value from the stack.

CSHaze commented 2 years ago

Hi @ginkgm and @igrr. I just received a couple bme280 modules and i2c seems to be working fine.

Maybe I just have a couple of shoddy HTU21d modules. It is a little weird they seemed to work on the old esp32 though. Maybe due to differences in the internal clocks between the new and old esp32s?

dhalbert commented 2 years ago

We are also seeing some I2C issues on ESP32-S3, though it's in comparison with ESP32-S2. Specifically, LC709203, an I2C battery meter, does not work reliably. It can work longer at slow I2C bus speeds (tested at 10kHz) but eventually produces I2C protocol errors. Other devices such as MSA301 or BNO055 (which can do clock stretching) seen OK at normal speeds. We're still trying to characterize the problem.

dhalbert commented 2 years ago

(EDITED several times after I got a better comparison trace)

LC709203F comparison: maybe seeing a clock-stretching handling issue?

The first four transactions work, then the sensor does a long clock stretch. The clock stretch works on the S2 (bottom trace), but not the S3 (top trace)

image

Note that SDA goes high in the bad trace, which is odd.

I tried setting a 0.1sec I2C timeout, did not seem to help.

dhalbert commented 2 years ago

I cannot reproduce https://github.com/espressif/esp-idf/issues/8741#issuecomment-1124457602 in Arduino on S3, using https://github.com/adafruit/Adafruit_LC709203F/tree/master/examples/LC709203F_demo and removing the delay in loop(). There are clock-stretches, but they don't cause issues above. The example above is from CircuitPython, which has an extra task and some other complications.

lebossejames commented 1 year ago

Hello,

I have same issue with ESP32-S3 Arduino IDE, i2C plug to lcd screen.

Papirrruqui commented 1 year ago

Hello, I just got a ESP32-S3HDevKitv1, and can't connect a I2C device to it. Can any one point me an example of how to configure the pins for I2C communication

riker65 commented 1 year ago

same for me with heltec wifi kit 32 V3, no platformio module found for i2c but in arduino it is working with V3

illuminati-vs commented 1 year ago

Hey how did you configure pin 15&16 as i2c pins in ESP32-S3-Wroom-1, or are they by default i2c pins. Please help!!

riker65 commented 1 year ago

Hi, on the heltec I used

`i2c:

sda: GPIO17 scl: GPIO18

scan: false frequency: 50khz`

Martius108 commented 1 year ago

Hi riker65,

could you please post a full code example for Heltec boards? I tried to establish I2C connections to a BME280 sensor on CubeCell HTTC AB-02 and an ESP32 LoRa board (both V3) but nothing works. I always get "no I2C device found" I tried the second I2C channel which should be SDA = pin9 and SCL = pin8. I also tried different I2C scanners and was wondering why they couldn't find the build in OLED display ..

riker65 commented 1 year ago

Hi

see here,

just my prototype to get it working

hope this helps

pasting it as code leads to strange formating

here without code tags

---again: strange format. How do I supress those formating tags?

unfortunately have to redo it without the # inside

`#https://forum.hassiohelp.eu/d/536-display-oled-su-esp32-con-esphome

https://github.com/Heltec-Aaron-Lee

esphome: name: heltec2 platform: ESP32 board: esp32-s3-devkitc-1

wifi: ssid: !secret wifi_ssid password: !secret wifi_password

Enable fallback hotspot (captive portal) in case wifi connection fails

ap: ssid: "Oled2 Fallback Hotspot" password: "xxxx" captive_portal:

Enable logging

logger:

ota:

Example configuration entry

web_server: port: 80

https://esphome.io/components/switch/gpio.html

switch:

i2c:

sda: GPIO17 scl: GPIO18 scan: false frequency: 50khz

font:

time:

display:

https://esphome.io/components/mqtt.html

mqtt: broker: 192.168.0.1 username: !secret mqtt_user password: !secret mqtt_password

text_sensor:

slimcdk commented 1 year ago

@riker65 Not relevant here. ESPHome currently uses Arduino as core unless you explicitly define otherwise. Your question should be on their project instead.

wBrhy2 commented 11 months ago

I simply want to comment that I have observed similar issues as well where certain i2c chips behave differently between Wroom-32 and Wroom-S2/S3 as well. Specifically VL6180X chips and i2c touch panel. It renders those devices completely unusable on newer ESP32 so I look forward to seeing what you find.