From 0e2faff6dcd6d18b61c93bc0d92df45938e9048c Mon Sep 17 00:00:00 2001 From: Jay Blackman Date: Sat, 14 Jun 2025 07:16:49 +1000 Subject: [PATCH] i2c changes required for I2CDEV_0 usage (#14443) * i2c changes required for I2CDEV_0 usage * Corrected offsets --- src/main/cli/settings.c | 20 ++++++++++++-------- src/main/fc/init.c | 3 +++ src/main/pg/bus_i2c.c | 9 +++++++++ src/main/target/common_defaults_post.h | 5 +++++ 4 files changed, 29 insertions(+), 8 deletions(-) diff --git a/src/main/cli/settings.c b/src/main/cli/settings.c index 7cbcabfc6b..5b5b0c4790 100644 --- a/src/main/cli/settings.c +++ b/src/main/cli/settings.c @@ -1845,21 +1845,25 @@ const clivalue_t valueTable[] = { { "gyro_2_align_yaw", VAR_INT16 | HARDWARE_VALUE, .config.minmax = { -3600, 3600 }, PG_GYRO_DEVICE_CONFIG, PG_ARRAY_ELEMENT_OFFSET(gyroDeviceConfig_t, 1, customAlignment.yaw) }, #endif #ifdef I2C_FULL_RECONFIGURABILITY +#ifdef USE_I2C_DEVICE_0 + { "i2c0_pullup", VAR_UINT8 | HARDWARE_VALUE | MODE_LOOKUP, .config.lookup = { TABLE_OFF_ON }, PG_I2C_CONFIG, PG_ARRAY_ELEMENT_OFFSET(i2cConfig_t, I2CDEV_0, pullUp) }, + { "i2c0_clockspeed_khz", VAR_UINT16 | HARDWARE_VALUE, .config.minmax = { I2C_CLOCKSPEED_MIN_KHZ, I2C_CLOCKSPEED_MAX_KHZ }, PG_I2C_CONFIG, PG_ARRAY_ELEMENT_OFFSET(i2cConfig_t, I2CDEV_0, clockSpeed) }, +#endif #ifdef USE_I2C_DEVICE_1 - { "i2c1_pullup", VAR_UINT8 | HARDWARE_VALUE | MODE_LOOKUP, .config.lookup = { TABLE_OFF_ON }, PG_I2C_CONFIG, PG_ARRAY_ELEMENT_OFFSET(i2cConfig_t, 0, pullUp) }, - { "i2c1_clockspeed_khz", VAR_UINT16 | HARDWARE_VALUE, .config.minmax = { I2C_CLOCKSPEED_MIN_KHZ, I2C_CLOCKSPEED_MAX_KHZ }, PG_I2C_CONFIG, PG_ARRAY_ELEMENT_OFFSET(i2cConfig_t, 0, clockSpeed) }, + { "i2c1_pullup", VAR_UINT8 | HARDWARE_VALUE | MODE_LOOKUP, .config.lookup = { TABLE_OFF_ON }, PG_I2C_CONFIG, PG_ARRAY_ELEMENT_OFFSET(i2cConfig_t, I2CDEV_1, pullUp) }, + { "i2c1_clockspeed_khz", VAR_UINT16 | HARDWARE_VALUE, .config.minmax = { I2C_CLOCKSPEED_MIN_KHZ, I2C_CLOCKSPEED_MAX_KHZ }, PG_I2C_CONFIG, PG_ARRAY_ELEMENT_OFFSET(i2cConfig_t, I2CDEV_1, clockSpeed) }, #endif #ifdef USE_I2C_DEVICE_2 - { "i2c2_pullup", VAR_UINT8 | HARDWARE_VALUE | MODE_LOOKUP, .config.lookup = { TABLE_OFF_ON }, PG_I2C_CONFIG, PG_ARRAY_ELEMENT_OFFSET(i2cConfig_t, 1, pullUp) }, - { "i2c2_clockspeed_khz", VAR_UINT16 | HARDWARE_VALUE, .config.minmax = { I2C_CLOCKSPEED_MIN_KHZ, I2C_CLOCKSPEED_MAX_KHZ }, PG_I2C_CONFIG, PG_ARRAY_ELEMENT_OFFSET(i2cConfig_t, 1, clockSpeed) }, + { "i2c2_pullup", VAR_UINT8 | HARDWARE_VALUE | MODE_LOOKUP, .config.lookup = { TABLE_OFF_ON }, PG_I2C_CONFIG, PG_ARRAY_ELEMENT_OFFSET(i2cConfig_t, I2CDEV_2, pullUp) }, + { "i2c2_clockspeed_khz", VAR_UINT16 | HARDWARE_VALUE, .config.minmax = { I2C_CLOCKSPEED_MIN_KHZ, I2C_CLOCKSPEED_MAX_KHZ }, PG_I2C_CONFIG, PG_ARRAY_ELEMENT_OFFSET(i2cConfig_t, I2CDEV_2, clockSpeed) }, #endif #ifdef USE_I2C_DEVICE_3 - { "i2c3_pullup", VAR_UINT8 | HARDWARE_VALUE | MODE_LOOKUP, .config.lookup = { TABLE_OFF_ON }, PG_I2C_CONFIG, PG_ARRAY_ELEMENT_OFFSET(i2cConfig_t, 2, pullUp) }, - { "i2c3_clockspeed_khz", VAR_UINT16 | HARDWARE_VALUE, .config.minmax = { I2C_CLOCKSPEED_MIN_KHZ, I2C_CLOCKSPEED_MAX_KHZ }, PG_I2C_CONFIG, PG_ARRAY_ELEMENT_OFFSET(i2cConfig_t, 2, clockSpeed) }, + { "i2c3_pullup", VAR_UINT8 | HARDWARE_VALUE | MODE_LOOKUP, .config.lookup = { TABLE_OFF_ON }, PG_I2C_CONFIG, PG_ARRAY_ELEMENT_OFFSET(i2cConfig_t, I2CDEV_3, pullUp) }, + { "i2c3_clockspeed_khz", VAR_UINT16 | HARDWARE_VALUE, .config.minmax = { I2C_CLOCKSPEED_MIN_KHZ, I2C_CLOCKSPEED_MAX_KHZ }, PG_I2C_CONFIG, PG_ARRAY_ELEMENT_OFFSET(i2cConfig_t, I2CDEV_3, clockSpeed) }, #endif #ifdef USE_I2C_DEVICE_4 - { "i2c4_pullup", VAR_UINT8 | HARDWARE_VALUE | MODE_LOOKUP, .config.lookup = { TABLE_OFF_ON }, PG_I2C_CONFIG, PG_ARRAY_ELEMENT_OFFSET(i2cConfig_t, 3, pullUp) }, - { "i2c4_clockspeed_khz", VAR_UINT16 | HARDWARE_VALUE, .config.minmax = { I2C_CLOCKSPEED_MIN_KHZ, I2C_CLOCKSPEED_MAX_KHZ }, PG_I2C_CONFIG, PG_ARRAY_ELEMENT_OFFSET(i2cConfig_t, 3, clockSpeed) }, + { "i2c4_pullup", VAR_UINT8 | HARDWARE_VALUE | MODE_LOOKUP, .config.lookup = { TABLE_OFF_ON }, PG_I2C_CONFIG, PG_ARRAY_ELEMENT_OFFSET(i2cConfig_t, I2CDEV_4, pullUp) }, + { "i2c4_clockspeed_khz", VAR_UINT16 | HARDWARE_VALUE, .config.minmax = { I2C_CLOCKSPEED_MIN_KHZ, I2C_CLOCKSPEED_MAX_KHZ }, PG_I2C_CONFIG, PG_ARRAY_ELEMENT_OFFSET(i2cConfig_t, I2CDEV_4, clockSpeed) }, #endif #endif #ifdef USE_MCO diff --git a/src/main/fc/init.c b/src/main/fc/init.c index 0b22ec9ca0..c156a4d0a6 100644 --- a/src/main/fc/init.c +++ b/src/main/fc/init.c @@ -629,6 +629,9 @@ void init(void) // Note: Unlike UARTs which are configured when client is present, // I2C buses are initialized unconditionally if they are configured. +#ifdef USE_I2C_DEVICE_0 + i2cInit(I2CDEV_0); +#endif #ifdef USE_I2C_DEVICE_1 i2cInit(I2CDEV_1); #endif diff --git a/src/main/pg/bus_i2c.c b/src/main/pg/bus_i2c.c index 20ce63365d..3b83ac0a1a 100644 --- a/src/main/pg/bus_i2c.c +++ b/src/main/pg/bus_i2c.c @@ -41,6 +41,12 @@ PG_REGISTER_ARRAY_WITH_RESET_FN(i2cConfig_t, I2CDEV_COUNT, i2cConfig, PG_I2C_CONFIG, 1); +#ifndef I2C0_SCL_PIN +#define I2C0_SCL_PIN NONE +#endif +#ifndef I2C0_SDA_PIN +#define I2C0_SDA_PIN NONE +#endif #ifndef I2C1_SCL_PIN #define I2C1_SCL_PIN NONE #endif @@ -74,6 +80,9 @@ typedef struct i2cDefaultConfig_s { } i2cDefaultConfig_t; static const i2cDefaultConfig_t i2cDefaultConfig[] = { +#ifdef USE_I2C_DEVICE_0 + { I2CDEV_0, IO_TAG(I2C0_SCL_PIN), IO_TAG(I2C0_SDA_PIN), I2C0_PULLUP, I2C0_CLOCKSPEED }, +#endif #ifdef USE_I2C_DEVICE_1 { I2CDEV_1, IO_TAG(I2C1_SCL_PIN), IO_TAG(I2C1_SDA_PIN), I2C1_PULLUP, I2C1_CLOCKSPEED }, #endif diff --git a/src/main/target/common_defaults_post.h b/src/main/target/common_defaults_post.h index e8b06bdf41..af162d6b5e 100644 --- a/src/main/target/common_defaults_post.h +++ b/src/main/target/common_defaults_post.h @@ -24,6 +24,9 @@ #define DEBUG_MODE DEBUG_NONE #endif +#ifndef I2C0_CLOCKSPEED +#define I2C0_CLOCKSPEED 800 +#endif #ifndef I2C1_CLOCKSPEED #define I2C1_CLOCKSPEED 800 #endif @@ -40,11 +43,13 @@ // Default values for internal pullup #if defined(USE_I2C_PULLUP) +#define I2C0_PULLUP true #define I2C1_PULLUP true #define I2C2_PULLUP true #define I2C3_PULLUP true #define I2C4_PULLUP true #else +#define I2C0_PULLUP false #define I2C1_PULLUP false #define I2C2_PULLUP false #define I2C3_PULLUP false