Skip to content

Commit

Permalink
Merge pull request #2600 from SimonGAndrews/master
Browse files Browse the repository at this point in the history
Fix for issue #2589  - i2Csetup for ESP 32-C3
  • Loading branch information
gfwilliams authored Jan 20, 2025
2 parents ef27cea + f50ed22 commit f7442b4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 23 deletions.
6 changes: 4 additions & 2 deletions boards/ESP32.py
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
46 changes: 25 additions & 21 deletions targets/esp32/jshardwareI2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit f7442b4

Please sign in to comment.