diff --git a/boards/ESP32.py b/boards/ESP32.py index 25ea5be96..c49c6d72b 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; diff --git a/targets/esp32/jshardwareI2c.c b/targets/esp32/jshardwareI2c.c index 62ad04177..80e985ef6 100644 --- a/targets/esp32/jshardwareI2c.c +++ b/targets/esp32/jshardwareI2c.c @@ -84,46 +84,50 @@ 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 + * 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); - } - 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; - } -#if ESPR_I2C_COUNT>1 - // Unsure on what to default these pins to? - if ( i2c_master_port == I2C_NUM_1 ) { - scl = info->pinSCL != PIN_UNDEFINED ? info->pinSCL : 16; - sda = info->pinSDA != PIN_UNDEFINED ? info->pinSDA : 17; + i2c_driver_delete(i2c_master_port); } -#endif + 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); + + #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; - 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 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 + #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);