lovyan03 / LovyanGFX

SPI LCD graphics library for ESP32 (ESP-IDF/ArduinoESP32) / ESP8266 (ArduinoESP8266) / SAMD51(Seeed ArduinoSAMD51)
Other
1.15k stars 205 forks source link

M5Stack CoreS3 I2C conflict #628

Open knifter opened 3 days ago

knifter commented 3 days ago

Carefully written Issues are more likely to be given priority. 丁寧に記述された報告は優先して対応される可能性が高くなります。

Environment ( 実行環境 )

Problem Description ( 問題の内容 )

M5Stack defines Wire & Wire1, Wire is is the SYS_I2C on bus nr 0, and configured on on G11/G12. LGFX Autodetect configures bus nr 1 (Wire1) on pins G11/G12, breaking 'Wire' from that point on. Configuring my other I2C devices on Wire1 (port 1), on pins 11/12 makes them and LGFX touch work together. But this breaks the second default configured Wire1, which was/should be configured on G19/G20. Setting i2c_port = I2C_NUM_0 (LGFX_AutoDetect_ESP32_all.hpp:76) also fixes the problem as this makes LGFX use the same config as was already present.

Expected Behavior ( 期待される動作 )

LGFX using port nr 0 (== SYS_I2C) for the touch controller.

Actual Behavior ( 実際の動作 )

LGFX configures port nr 1 (in LGFX_AutoDetect_ESP32_all.hpp, line 76)

Steps to reproduce ( 再現のための前提条件 )

  1. LGFX.init()
  2. Use Wire on any device on the internal I2C bus using Wire (also present on MBUS, and used by M5Stack modules)
  3. <device does not work and touch readout very slow: conflict because two i2c controllers on same pins)
  4. Configure i2c devices to use Wire1 (on G11/G12 now)
  5. Devices work, touch works, as workaround

Code to reproduce this issue ( 再現させるためのコード )

Nothing special: lgfx.init() Use Wire for devices on G11/G12, they no longer work after configuring touch callback getTouchRaw is called. I do not use the M5 library/classes itself. But I guess they could break after LGFX reconfigures pins.

tobozo commented 3 days ago

hi,

does it also happen with the develop branch?

knifter commented 3 days ago

I should try, but cannot do that right now (just left). But https://github.com/lovyan03/LovyanGFX/blob/develop/src/lgfx/v1_autodetect/LGFX_AutoDetect_ESP32_all.hpp still contains NUM_1 on line 76, so I suspect the same result. But I will test as soon as I can.

knifter commented 3 days ago

Could test it. Yes, same thing. It still configures I2C port 1 with the same pins as port 0 is already configured to. The workaround (Use Wire1 on G11/G12) and the fix (76: NUM_1 -> NUM_0) Is there a reason to use NUM_1 there? The touch is connected to G11/G12 and Wire is (by Arduino framework) configured on NUM_0 with these pins., configuring I2C_NUM_1 there will configure the second i2c peripheral on the same pins. Even with the workaround I now use.

btw: latest version also gives me a warning (which is an error because I use -Werror):

lib/LovyanGFX/src/lgfx/v1/platforms/esp32/Bus_SPI.cpp:26: error: "REG_SPI_BASE" redefined [-Werror]
   #define REG_SPI_BASE(i)   (DR_REG_SPI1_BASE + (((i)>1) ? (((i)* 0x1000) + 0x20000) : (((~(i)) & 1)* 0x1000 )))
...
C:/Users/tvroon1/.platformio/packages/framework-arduinoespressif32/tools/sdk/esp32s3/include/soc/esp32s3/include/soc/soc.h:122: note: this is the location of the previous definition   
 #define REG_SPI_BASE(i)         (((i)>=2) ? (DR_REG_SPI2_BASE + (i-2) * 0x1000) : (0))    // GPSPI2 and GPSPI3

But let's not dwell on that too much. I can work around it. I can make a separate issue if you need it.

tobozo commented 3 days ago

editing line 76 will affect all M5 devices

does changing the port at line 120 instead _cfg.i2c_port = I2C_NUM_0; work for you?

knifter commented 16 hours ago

That's weird as on line 77+78, just below I2C_NUM_1, the I2C gpio pins are set to GPIO_NUM_12 and GPIO_NUM_11 while all other M5 (core) devices have their internal I2C bus on GPIO21 and GPIO22. So if this part is enabled then, those IO pins do not connect to the internal bus which has the touch controller on in.

I've switched my project to the CoreS2 and these lines get greyed out. Makes sense as CONFIG_IDF_TARGET_ESP32S3 on line 55 is then not defined. Then line 283 gets enabled, setting I2C to GPIO21/22. Also on I2C_NUM_1 btw, so I suspect this problem to arise on the CoreS2 as well.

Don't know about ESP32_S3_BOX version though, which this part seems to be made for as well?

For now autodetection does work in the sense that touch works, but not in the sense that the other I2C devices no longer do. For others currently running into this problem: Either use Wire1 on GPIO11/12 and Wire on different pins or edit LGFX_AutoDetect_ESP32_all.hpp to use I2C_NUM_0 on line 76.

Thanks btw!