From 22f60538647fae06c7abe551eadd826fdeaa950e Mon Sep 17 00:00:00 2001 From: Simon Andrews Date: Mon, 6 Jan 2025 21:33:20 +0000 Subject: [PATCH 1/4] I2C Setup changes for testing against issue #2589 --- targets/esp32/jshardwareI2c.c | 39 +++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/targets/esp32/jshardwareI2c.c b/targets/esp32/jshardwareI2c.c index 62ad04177b..336763d371 100644 --- a/targets/esp32/jshardwareI2c.c +++ b/targets/esp32/jshardwareI2c.c @@ -84,8 +84,8 @@ int getI2cFromDevice( IOEventFlags device ) { } } -/** Set-up I2C master for ESP32, default pins are SCL:21, SDA:22. Only device I2C1 is supported - * and only master mode. */ +/** Set-up I2C master for ESP32, default pins are target dependent, defined in board.py file + * Only device I2C1 is supported and only master mode. */ void jshI2CSetup(IOEventFlags device, JshI2CInfo *info) { int i2c_master_port = getI2cFromDevice(device); if (i2c_master_port == -1) { @@ -93,37 +93,44 @@ void jshI2CSetup(IOEventFlags device, JshI2CInfo *info) { return; } if(jshIsDeviceInitialised(device)){ - i2c_driver_delete(i2c_master_port); - } - Pin scl; - Pin sda; - if ( i2c_master_port == I2C_NUM_0 ) { - scl = info->pinSCL != PIN_UNDEFINED ? info->pinSCL : 21; - sda = info->pinSDA != PIN_UNDEFINED ? info->pinSDA : 22; + i2c_driver_delete(i2c_master_port); } + + if (!jshIsPinValid(info->pinSCL)) info->pinSCL = jshFindPinForFunction(device, JSH_I2C_SCL); + if (!jshIsPinValid(info->pinSDA)) info->pinSDA = jshFindPinForFunction(device, JSH_I2C_SDA); + jsDebug(DBG_INFO, "jshI2CSetup: I2C pins default or provided as , sda: %d scl: %d \n", info->pinSDA, info->pinSCL); + + #if ESPR_I2C_COUNT>1 - // Unsure on what to default these pins to? + // I2C2 not currently implemented. if ( i2c_master_port == I2C_NUM_1 ) { - scl = info->pinSCL != PIN_UNDEFINED ? info->pinSCL : 16; - sda = info->pinSDA != PIN_UNDEFINED ? info->pinSDA : 17; + scl = info->pinSCL != PIN_UNDEFINED ? info->pinSCL : 26; + sda = info->pinSDA != PIN_UNDEFINED ? info->pinSDA : 25; } #endif i2c_config_t conf; conf.mode = I2C_MODE_MASTER; - conf.sda_io_num = pinToESP32Pin(sda); + conf.sda_io_num = pinToESP32Pin(info->pinSDA); conf.sda_pullup_en = GPIO_PULLUP_ENABLE; - conf.scl_io_num = pinToESP32Pin(scl); + conf.scl_io_num = pinToESP32Pin(info->pinSCL); conf.scl_pullup_en = GPIO_PULLUP_ENABLE; conf.master.clk_speed = info->bitrate; + + #if defined(CONFIG_IDF_TARGET_ESP32C3) || defined(CONFIG_IDF_TARGET_ESP32S3) // added to resolve issue #2589 + conf.clk_flags = 0; // will always select 2MZ XTAL clock - Although speed set as in conf.master.clk_speed + // conf.clk_flags = 1; // or set driver to ignore XTAL clock and use 1MHz RTC clock (better for low power?) + // ref https://docs.espressif.com/projects/esp-idf/en/v4.4/esp32s3/api-reference/peripherals/i2c.html#source-clock-configuration + #endif + esp_err_t err=i2c_param_config(i2c_master_port, &conf); if ( err == ESP_ERR_INVALID_ARG ) { jsExceptionHere(JSET_ERROR,"jshI2CSetup: Invalid arguments"); - return; + return; } err=i2c_driver_install(i2c_master_port, conf.mode, 0, 0, 0); if ( err == ESP_OK ) { - jsDebug(DBG_INFO, "jshI2CSetup: driver installed, sda: %d scl: %d freq: %d, \n", sda, scl, info->bitrate); + jsDebug(DBG_INFO, "jshI2CSetup: driver installed with, sda: %d scl: %d freq: %d, \n", info->pinSDA, info->pinSCL, conf.master.clk_speed); jshSetDeviceInitialised(device, true); } else { checkError("jshI2CSetup",err); From 23b697fef5c0f3bbd7a281e6d38856d815f6907c Mon Sep 17 00:00:00 2001 From: Simon Andrews Date: Thu, 9 Jan 2025 22:17:54 +0000 Subject: [PATCH 2/4] I2c Changes for testing Round2 against #2589 --- targets/esp32/jshardwareI2c.c | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/targets/esp32/jshardwareI2c.c b/targets/esp32/jshardwareI2c.c index 336763d371..80e985ef6f 100644 --- a/targets/esp32/jshardwareI2c.c +++ b/targets/esp32/jshardwareI2c.c @@ -85,29 +85,26 @@ int getI2cFromDevice( IOEventFlags device ) { } /** Set-up I2C master for ESP32, default pins are target dependent, defined in board.py file - * Only device I2C1 is supported and only master mode. */ + * Master mode only - handles I2C1 and I2C2 (if available eg on ESP32) */ void jshI2CSetup(IOEventFlags device, JshI2CInfo *info) { int i2c_master_port = getI2cFromDevice(device); if (i2c_master_port == -1) { - jsExceptionHere(JSET_ERROR,"Only I2C1 and I2C2 supported"); + jsExceptionHere(JSET_ERROR,"Only I2C1 and I2C2 (if available on target) supported"); return; } if(jshIsDeviceInitialised(device)){ i2c_driver_delete(i2c_master_port); } + JshPinFunction funcType = jshGetPinFunctionFromDevice(device); + if (!jshIsPinValid(info->pinSCL)) info->pinSCL = jshFindPinForFunction(funcType, JSH_I2C_SCL); + if (!jshIsPinValid(info->pinSDA)) info->pinSDA = jshFindPinForFunction(funcType, JSH_I2C_SDA); - if (!jshIsPinValid(info->pinSCL)) info->pinSCL = jshFindPinForFunction(device, JSH_I2C_SCL); - if (!jshIsPinValid(info->pinSDA)) info->pinSDA = jshFindPinForFunction(device, JSH_I2C_SDA); - jsDebug(DBG_INFO, "jshI2CSetup: I2C pins default or provided as , sda: %d scl: %d \n", info->pinSDA, info->pinSCL); - - -#if ESPR_I2C_COUNT>1 - // I2C2 not currently implemented. - if ( i2c_master_port == I2C_NUM_1 ) { - scl = info->pinSCL != PIN_UNDEFINED ? info->pinSCL : 26; - sda = info->pinSDA != PIN_UNDEFINED ? info->pinSDA : 25; - } -#endif + #ifdef DEBUG + char funcTypeStr[50]; + jshPinFunctionToString(funcType, JSPFTS_DEVICE | JSPFTS_DEVICE_NUMBER, funcTypeStr, sizeof(funcTypeStr)); + jsDebug(DBG_INFO, "jshI2CSetup: I2C pins on device: %s, identified as sda: %d, scl: %d\n", + funcTypeStr, info->pinSDA, info->pinSCL); + #endif i2c_config_t conf; conf.mode = I2C_MODE_MASTER; @@ -117,7 +114,7 @@ void jshI2CSetup(IOEventFlags device, JshI2CInfo *info) { conf.scl_pullup_en = GPIO_PULLUP_ENABLE; conf.master.clk_speed = info->bitrate; - #if defined(CONFIG_IDF_TARGET_ESP32C3) || defined(CONFIG_IDF_TARGET_ESP32S3) // added to resolve issue #2589 + #if defined(CONFIG_IDF_TARGET_ESP32C3) || defined(CONFIG_IDF_TARGET_ESP32S3) // added to resolve issue #2589 for IDF v4.x conf.clk_flags = 0; // will always select 2MZ XTAL clock - Although speed set as in conf.master.clk_speed // conf.clk_flags = 1; // or set driver to ignore XTAL clock and use 1MHz RTC clock (better for low power?) // ref https://docs.espressif.com/projects/esp-idf/en/v4.4/esp32s3/api-reference/peripherals/i2c.html#source-clock-configuration From 8c526a0ae8bc9c4374a22bb25a12820301baa4d6 Mon Sep 17 00:00:00 2001 From: Simon Andrews <55559464+SimonGAndrews@users.noreply.github.com> Date: Fri, 10 Jan 2025 10:33:44 +0000 Subject: [PATCH 3/4] Update build.yml form RELEASE=1 to DEBUG=1 for ESP32 builds --- .github/workflows/build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 55dcec0a7f..19293dcceb 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -49,7 +49,7 @@ jobs: - name: make ${{ matrix.board }} env: TRAVIS: 1 - RELEASE: 1 + DEBUG: 1 BOARD: ${{ matrix.board }} UPLOADTOKEN: ${{ secrets.UPLOADTOKEN }} run: | From f50ed228a75c857c38b93262d9114c125f14b694 Mon Sep 17 00:00:00 2001 From: Simon Andrews Date: Sun, 19 Jan 2025 14:26:53 +0000 Subject: [PATCH 4/4] Fixes for #2589 I2C1 and I2C2 pin defaults in ESP32.py and Revert Debug =1 for build action workflow testing, --- .github/workflows/build.yml | 2 +- boards/ESP32.py | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 0a0a441425..9a9f301ea2 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -49,7 +49,7 @@ jobs: - name: make ${{ matrix.board }} env: TRAVIS: 1 - DEBUG: 1 + RELEASE: 1 BOARD: ${{ matrix.board }} UPLOADTOKEN: ${{ secrets.UPLOADTOKEN }} run: | diff --git a/boards/ESP32.py b/boards/ESP32.py index 25ea5be96a..c49c6d72b1 100755 --- a/boards/ESP32.py +++ b/boards/ESP32.py @@ -188,9 +188,11 @@ def get_pins(): pinutils.findpin(pins, "PD32", True)["functions"]["USART1_RX"]=0; # doesn't match jshardwareUart? pinutils.findpin(pins, "PD16", True)["functions"]["USART3_RX"]=0; pinutils.findpin(pins, "PD17", True)["functions"]["USART3_TX"]=0; + pinutils.findpin(pins, "PD16", True)["functions"]["I2C2_SCL"]=1; # added for issue #2589 fix + pinutils.findpin(pins, "PD17", True)["functions"]["I2C2_SDA"]=1; # added for issue #2589 fix - pinutils.findpin(pins, "PD21", True)["functions"]["I2C1_SCL"]=0; - pinutils.findpin(pins, "PD22", True)["functions"]["I2C1_SDA"]=0; + pinutils.findpin(pins, "PD22", True)["functions"]["I2C1_SCL"]=0; # SCL moved from P21 for issue #2589 + pinutils.findpin(pins, "PD21", True)["functions"]["I2C1_SDA"]=0; # SDA moved from P22 for issue #2589 pinutils.findpin(pins, "PD14", True)["functions"]["SPI1_SCLK"]=0; pinutils.findpin(pins, "PD12", True)["functions"]["SPI1_MISO"]=0; pinutils.findpin(pins, "PD13", True)["functions"]["SPI1_MOSI"]=0;