Skip to content

Instantly share code, notes, and snippets.

@dexter93
Created July 16, 2026 09:03
Show Gist options
  • Select an option

  • Save dexter93/72b41d22cf5b8a61aadc5758299d34eb to your computer and use it in GitHub Desktop.

Select an option

Save dexter93/72b41d22cf5b8a61aadc5758299d34eb to your computer and use it in GitHub Desktop.
i2c fallback driver qmk patch
diff --git a/builddefs/common_features.mk b/builddefs/common_features.mk
index ff805ec2cb..c1ba33dfeb 100644
--- a/builddefs/common_features.mk
+++ b/builddefs/common_features.mk
@@ -1028,6 +1028,12 @@ endif
ifeq ($(strip $(I2C_DRIVER_REQUIRED)), yes)
OPT_DEFS += -DHAL_USE_I2C=TRUE
QUANTUM_LIB_SRC += i2c_master.c
+
+ ifeq ($(strip $(PLATFORM)), CHIBIOS)
+ ifeq ($(strip $(USE_HAL_I2C_FALLBACK)), yes)
+ OPT_DEFS += -DUSE_HAL_I2C_FALLBACK
+ endif
+ endif
endif
ifeq ($(strip $(SPI_DRIVER_REQUIRED)), yes)
diff --git a/platforms/chibios/drivers/i2c_master.c b/platforms/chibios/drivers/i2c_master.c
index e67729f7e3..d6651aabd4 100644
--- a/platforms/chibios/drivers/i2c_master.c
+++ b/platforms/chibios/drivers/i2c_master.c
@@ -90,8 +90,41 @@
# endif
#endif
+#if defined(USE_HAL_I2C_FALLBACK)
+# ifndef I2C1_CLOCK_SPEED
+# define I2C1_CLOCK_SPEED 100000 /* 400000 */
+# endif
+# if SW_I2C_USE_OSAL_DELAY == TRUE
+# ifndef SW_I2C_DELAY_TICKS
+# define SW_I2C_DELAY_TICKS ((CH_CFG_ST_FREQUENCY + I2C1_CLOCK_SPEED - 1) / (2 * I2C1_CLOCK_SPEED))
+# endif
+# else
+# ifndef SW_I2C_DELAY_LOOP_CYCLES
+# define SW_I2C_DELAY_LOOP_CYCLES 16
+# endif
+__attribute__((weak)) void i2c_sw_delay(void)
+{
+ volatile uint32_t cycles =
+ SystemCoreClock / (2 * I2C1_CLOCK_SPEED * SW_I2C_DELAY_LOOP_CYCLES);
+
+ while (cycles--) {
+ __asm__ volatile("nop");
+ }
+}
+# endif
+#endif
+
static const I2CConfig i2cconfig = {
-#if defined(USE_I2CV1_CONTRIB)
+#if defined(USE_HAL_I2C_FALLBACK)
+ .addr10 = false,
+ .scl = I2C1_SCL_PIN,
+ .sda = I2C1_SDA_PIN,
+# if SW_I2C_USE_OSAL_DELAY == TRUE
+ .ticks = SW_I2C_DELAY_TICKS,
+# else
+ .delay = i2c_sw_delay,
+# endif
+#elif defined(USE_I2CV1_CONTRIB)
I2C1_CLOCK_SPEED,
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment