From 645a1b8364c3110f706db0f976ac5fa20b968c36 Mon Sep 17 00:00:00 2001
From: BIGTREETECH <38851044+bigtreetech@users.noreply.github.com>
Date: Thu, 8 Jun 2023 08:55:46 +0800
Subject: [PATCH] i2c_software: Implementation of software i2c (#6141)

Signed-off-by: Alan.Ma from BigTreeTech <tech@biqu3d.com>
---
 docs/Config_Reference.md |  24 ++++++
 klippy/extras/bus.py     |  36 ++++++--
 src/Makefile             |   2 +-
 src/i2c_software.c       | 181 +++++++++++++++++++++++++++++++++++++++
 src/i2c_software.h       |  13 +++
 src/i2ccmds.c            |  55 ++++++++++--
 src/i2ccmds.h            |   3 +
 src/stm32/Kconfig        |   2 +-
 8 files changed, 299 insertions(+), 17 deletions(-)
 create mode 100644 src/i2c_software.c
 create mode 100644 src/i2c_software.h

diff --git a/docs/Config_Reference.md b/docs/Config_Reference.md
index dd4449567..549b96ec8 100644
--- a/docs/Config_Reference.md
+++ b/docs/Config_Reference.md
@@ -1650,6 +1650,8 @@ accelerometers (one may define any number of sections with an
 #   Default is 104 (0x68). If AD0 is high, it would be 0x69 instead.
 #i2c_mcu:
 #i2c_bus:
+#i2c_software_scl_pin:
+#i2c_software_sda_pin:
 #i2c_speed: 400000
 #   See the "common I2C settings" section for a description of the
 #   above parameters. The default "i2c_speed" is 400000.
@@ -2370,6 +2372,8 @@ sensor_type: BME280
 #   (0x77).
 #i2c_mcu:
 #i2c_bus:
+#i2c_software_scl_pin:
+#i2c_software_sda_pin:
 #i2c_speed:
 #   See the "common I2C settings" section for a description of the
 #   above parameters.
@@ -2415,6 +2419,8 @@ sensor_type:
 #   Default is 64 (0x40).
 #i2c_mcu:
 #i2c_bus:
+#i2c_software_scl_pin:
+#i2c_software_sda_pin:
 #i2c_speed:
 #   See the "common I2C settings" section for a description of the
 #   above parameters.
@@ -2448,6 +2454,8 @@ sensor_type: LM75
 #   (usually with jumpers or hard wired).
 #i2c_mcu:
 #i2c_bus:
+#i2c_software_scl_pin:
+#i2c_software_sda_pin:
 #i2c_speed:
 #   See the "common I2C settings" section for a description of the
 #   above parameters.
@@ -2855,6 +2863,8 @@ PCA9533 LED support. The PCA9533 is used on the mightyboard.
 #   the PCA9533/1, 99 for the PCA9533/2. The default is 98.
 #i2c_mcu:
 #i2c_bus:
+#i2c_software_scl_pin:
+#i2c_software_sda_pin:
 #i2c_speed:
 #   See the "common I2C settings" section for a description of the
 #   above parameters.
@@ -2876,6 +2886,8 @@ PCA9632 LED support. The PCA9632 is used on the FlashForge Dreamer.
 #   96, 97, 98, or 99.  The default is 98.
 #i2c_mcu:
 #i2c_bus:
+#i2c_software_scl_pin:
+#i2c_software_sda_pin:
 #i2c_speed:
 #   See the "common I2C settings" section for a description of the
 #   above parameters.
@@ -3622,6 +3634,8 @@ i2c_address:
 #   parameter must be provided.
 #i2c_mcu:
 #i2c_bus:
+#i2c_software_scl_pin:
+#i2c_software_sda_pin:
 #i2c_speed:
 #   See the "common I2C settings" section for a description of the
 #   above parameters.
@@ -3658,6 +3672,8 @@ prefix).
 #   is 96.
 #i2c_mcu:
 #i2c_bus:
+#i2c_software_scl_pin:
+#i2c_software_sda_pin:
 #i2c_speed:
 #   See the "common I2C settings" section for a description of the
 #   above parameters.
@@ -3928,6 +3944,8 @@ lcd_type:
 #   Set to either "ssd1306" or "sh1106" for the given display type.
 #i2c_mcu:
 #i2c_bus:
+#i2c_software_scl_pin:
+#i2c_software_sda_pin:
 #i2c_speed:
 #   Optional parameters available for displays connected via an i2c
 #   bus. See the "common I2C settings" section for a description of
@@ -4308,10 +4326,14 @@ i2c_address:
 #   113. This parameter must be provided.
 #i2c_mcu:
 #i2c_bus:
+#i2c_software_scl_pin:
+#i2c_software_sda_pin:
 #i2c_speed:
 #   See the "common I2C settings" section for a description of the
 #   above parameters.
 #i2c_bus:
+#i2c_software_scl_pin:
+#i2c_software_sda_pin:
 #   If the I2C implementation of your micro-controller supports
 #   multiple I2C busses, you may specify the bus name here. The
 #   default is to use the default micro-controller i2c bus.
@@ -4561,6 +4583,8 @@ via the `i2c_speed` parameter. All other Klipper micro-controllers use a
 #   The name of the micro-controller that the chip is connected to.
 #   The default is "mcu".
 #i2c_bus:
+#i2c_software_scl_pin:
+#i2c_software_sda_pin:
 #   If the micro-controller supports multiple I2C busses then one may
 #   specify the micro-controller bus name here. The default depends on
 #   the type of micro-controller.
diff --git a/klippy/extras/bus.py b/klippy/extras/bus.py
index f37897d83..9b2ec371f 100644
--- a/klippy/extras/bus.py
+++ b/klippy/extras/bus.py
@@ -142,13 +142,22 @@ def MCU_SPI_from_config(config, mode, pin_option="cs_pin",
 
 # Helper code for working with devices connected to an MCU via an I2C bus
 class MCU_I2C:
-    def __init__(self, mcu, bus, addr, speed):
+    def __init__(self, mcu, bus, addr, speed, sw_pins=None):
         self.mcu = mcu
         self.bus = bus
         self.i2c_address = addr
         self.oid = self.mcu.create_oid()
-        self.config_fmt = "config_i2c oid=%d i2c_bus=%%s rate=%d address=%d" % (
-            self.oid, speed, addr)
+        mcu.add_config_cmd("config_i2c oid=%d" % (self.oid,))
+        # Generate I2C bus config message
+        if sw_pins is not None:
+            self.config_fmt = (
+                "i2c_set_software_bus oid=%d"
+                " scl_pin=%s sda_pin=%s rate=%d address=%d"
+                % (self.oid, sw_pins[0], sw_pins[1], speed, addr))
+        else:
+            self.config_fmt = (
+                "i2c_set_bus oid=%d i2c_bus=%%s rate=%d address=%d"
+                % (self.oid, speed, addr))
         self.cmd_queue = self.mcu.alloc_command_queue()
         self.mcu.register_config_callback(self.build_config)
         self.i2c_write_cmd = self.i2c_read_cmd = self.i2c_modify_bits_cmd = None
@@ -161,8 +170,10 @@ class MCU_I2C:
     def get_command_queue(self):
         return self.cmd_queue
     def build_config(self):
-        bus = resolve_bus_name(self.mcu, "i2c_bus", self.bus)
-        self.mcu.add_config_cmd(self.config_fmt % (bus,))
+        if '%' in self.config_fmt:
+            bus = resolve_bus_name(self.mcu, "i2c_bus", self.bus)
+            self.config_fmt = self.config_fmt % (bus,)
+        self.mcu.add_config_cmd(self.config_fmt)
         self.i2c_write_cmd = self.mcu.lookup_command(
             "i2c_write oid=%c data=%*s", cq=self.cmd_queue)
         self.i2c_read_cmd = self.mcu.lookup_query_command(
@@ -202,13 +213,24 @@ def MCU_I2C_from_config(config, default_addr=None, default_speed=100000):
     printer = config.get_printer()
     i2c_mcu = mcu.get_printer_mcu(printer, config.get('i2c_mcu', 'mcu'))
     speed = config.getint('i2c_speed', default_speed, minval=100000)
-    bus = config.get('i2c_bus', None)
     if default_addr is None:
         addr = config.getint('i2c_address', minval=0, maxval=127)
     else:
         addr = config.getint('i2c_address', default_addr, minval=0, maxval=127)
+    # Determine pin from config
+    ppins = config.get_printer().lookup_object("pins")
+    if config.get('i2c_software_scl_pin', None) is not None:
+        sw_pin_names = ['i2c_software_%s_pin' % (name,)
+                        for name in ['scl', 'sda']]
+        sw_pin_params = [ppins.lookup_pin(config.get(name), share_type=name)
+                         for name in sw_pin_names]
+        sw_pins = tuple([pin_params['pin'] for pin_params in sw_pin_params])
+        bus = None
+    else:
+        bus = config.get('i2c_bus', None)
+        sw_pins = None
     # Create MCU_I2C object
-    return MCU_I2C(i2c_mcu, bus, addr, speed)
+    return MCU_I2C(i2c_mcu, bus, addr, speed, sw_pins)
 
 
 ######################################################################
diff --git a/src/Makefile b/src/Makefile
index dc0427c70..7881622b0 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -9,6 +9,6 @@ src-$(CONFIG_HAVE_GPIO_SDIO) += sdiocmds.c
 src-$(CONFIG_HAVE_GPIO_I2C) += i2ccmds.c
 src-$(CONFIG_HAVE_GPIO_HARD_PWM) += pwmcmds.c
 bb-src-$(CONFIG_HAVE_GPIO_SPI) := spi_software.c sensor_adxl345.c sensor_angle.c
-bb-src-$(CONFIG_HAVE_GPIO_I2C) += sensor_mpu9250.c
+bb-src-$(CONFIG_HAVE_GPIO_I2C) += i2c_software.c sensor_mpu9250.c
 src-$(CONFIG_HAVE_GPIO_BITBANGING) += $(bb-src-y) lcd_st7920.c lcd_hd44780.c \
     buttons.c tmcuart.c neopixel.c pulse_counter.c
diff --git a/src/i2c_software.c b/src/i2c_software.c
new file mode 100644
index 000000000..ed0efe536
--- /dev/null
+++ b/src/i2c_software.c
@@ -0,0 +1,181 @@
+// Software I2C emulation
+//
+// Copyright (C) 2023  Kevin O'Connor <kevin@koconnor.net>
+// Copyright (C) 2023  Alan.Ma <tech@biqu3d.com>
+//
+// This file may be distributed under the terms of the GNU GPLv3 license.
+
+#include <string.h> // memcpy
+#include "board/gpio.h" // gpio_out_setup
+#include "board/internal.h" // gpio_peripheral
+#include "board/misc.h" // timer_read_time
+#include "basecmd.h" // oid_alloc
+#include "command.h" // DECL_COMMAND
+#include "sched.h" // sched_shutdown
+#include "i2ccmds.h" // i2cdev_set_software_bus
+
+struct i2c_software {
+    struct gpio_out scl_out, sda_out;
+    struct gpio_in scl_in, sda_in;
+    uint8_t addr;
+    unsigned int ticks;
+};
+
+void
+command_i2c_set_software_bus(uint32_t *args)
+{
+    struct i2cdev_s *i2c = i2cdev_oid_lookup(args[0]);
+    struct i2c_software *is = alloc_chunk(sizeof(*is));
+    is->ticks = 1000000 / 100 / 2; // 100KHz
+    is->addr = (args[4] & 0x7f) << 1; // address format shifted
+    is->scl_in = gpio_in_setup(args[1], 1);
+    is->scl_out = gpio_out_setup(args[1], 1);
+    is->sda_in = gpio_in_setup(args[2], 1);
+    is->sda_out = gpio_out_setup(args[2], 1);
+    i2cdev_set_software_bus(i2c, is);
+}
+DECL_COMMAND(command_i2c_set_software_bus,
+             "i2c_set_software_bus oid=%c scl_pin=%u sda_pin=%u"
+             " rate=%u address=%u");
+
+// The AVR micro-controllers require specialized timing
+#if CONFIG_MACH_AVR
+
+#define i2c_delay(ticks) (void)(ticks)
+
+#else
+
+static unsigned int
+nsecs_to_ticks(uint32_t ns)
+{
+    return timer_from_us(ns * 1000) / 1000000;
+}
+
+static void
+i2c_delay(unsigned int ticks) {
+    unsigned int t = timer_read_time() + nsecs_to_ticks(ticks);
+    while (t > timer_read_time());
+}
+
+#endif
+
+static void
+i2c_software_send_ack(struct i2c_software *is, const uint8_t ack)
+{
+    if (ack) {
+        gpio_in_reset(is->sda_in, 1);
+    } else {
+        gpio_out_reset(is->sda_out, 0);
+    }
+    i2c_delay(is->ticks);
+    gpio_in_reset(is->scl_in, 1);
+    i2c_delay(is->ticks);
+    gpio_out_reset(is->scl_out, 0);
+}
+
+static uint8_t
+i2c_software_read_ack(struct i2c_software *is)
+{
+    uint8_t nack = 0;
+    gpio_in_reset(is->sda_in, 1);
+    i2c_delay(is->ticks);
+    gpio_in_reset(is->scl_in, 1);
+    nack = gpio_in_read(is->sda_in);
+    i2c_delay(is->ticks);
+    gpio_out_reset(is->scl_out, 0);
+    gpio_in_reset(is->sda_in, 1);
+    return nack;
+}
+
+static void
+i2c_software_send_byte(struct i2c_software *is, uint8_t b)
+{
+    for (uint_fast8_t i = 0; i < 8; i++) {
+        if (b & 0x80) {
+            gpio_in_reset(is->sda_in, 1);
+        } else {
+            gpio_out_reset(is->sda_out, 0);
+        }
+        b <<= 1;
+        i2c_delay(is->ticks);
+        gpio_in_reset(is->scl_in, 1);
+        i2c_delay(is->ticks);
+        gpio_out_reset(is->scl_out, 0);
+    }
+
+    if (i2c_software_read_ack(is)) {
+        shutdown("soft_i2c NACK");
+    }
+}
+
+static uint8_t
+i2c_software_read_byte(struct i2c_software *is, uint8_t remaining)
+{
+    uint8_t b = 0;
+    gpio_in_reset(is->sda_in, 1);
+    for (uint_fast8_t i = 0; i < 8; i++) {
+        i2c_delay(is->ticks);
+        gpio_in_reset(is->scl_in, 1);
+        i2c_delay(is->ticks);
+        b <<= 1;
+        b |= gpio_in_read(is->sda_in);
+        gpio_out_reset(is->scl_out, 0);
+    }
+    gpio_in_reset(is->sda_in, 1);
+    i2c_software_send_ack(is, remaining == 0);
+    return b;
+}
+
+static void
+i2c_software_start(struct i2c_software *is, uint8_t addr)
+{
+    i2c_delay(is->ticks);
+    gpio_in_reset(is->sda_in, 1);
+    gpio_in_reset(is->scl_in, 1);
+    i2c_delay(is->ticks);
+    gpio_out_reset(is->sda_out, 0);
+    i2c_delay(is->ticks);
+    gpio_out_reset(is->scl_out, 0);
+
+    i2c_software_send_byte(is, addr);
+}
+
+static void
+i2c_software_stop(struct i2c_software *is)
+{
+    gpio_out_reset(is->sda_out, 0);
+    i2c_delay(is->ticks);
+    gpio_in_reset(is->scl_in, 1);
+    i2c_delay(is->ticks);
+    gpio_in_reset(is->sda_in, 1);
+}
+
+void
+i2c_software_write(struct i2c_software *is, uint8_t write_len, uint8_t *write)
+{
+    i2c_software_start(is, is->addr);
+    while (write_len--)
+        i2c_software_send_byte(is, *write++);
+    i2c_software_stop(is);
+}
+
+void
+i2c_software_read(struct i2c_software *is, uint8_t reg_len, uint8_t *reg
+                  , uint8_t read_len, uint8_t *read)
+{
+    uint8_t addr = is->addr | 0x01;
+
+    if (reg_len) {
+        // write the register
+        i2c_software_start(is, is->addr);
+        while(reg_len--)
+            i2c_software_send_byte(is, *reg++);
+    }
+    // start/re-start and read data
+    i2c_software_start(is, addr);
+    while(read_len--) {
+        *read = i2c_software_read_byte(is, read_len);
+        read++;
+    }
+    i2c_software_stop(is);
+}
diff --git a/src/i2c_software.h b/src/i2c_software.h
new file mode 100644
index 000000000..9bd54f29a
--- /dev/null
+++ b/src/i2c_software.h
@@ -0,0 +1,13 @@
+#ifndef __I2C_SOFTWARE_H
+#define __I2C_SOFTWARE_H
+
+#include <stdint.h> // uint8_t
+
+struct i2c_software *i2c_software_oid_lookup(uint8_t oid);
+void i2c_software_write(struct i2c_software *sw_i2c
+                        , uint8_t write_len, uint8_t *write);
+void i2c_software_read(struct i2c_software *sw_i2c
+                       , uint8_t reg_len, uint8_t *reg
+                       , uint8_t read_len, uint8_t *read);
+
+#endif // i2c_software.h
diff --git a/src/i2ccmds.c b/src/i2ccmds.c
index 69af011b1..099fe7753 100644
--- a/src/i2ccmds.c
+++ b/src/i2ccmds.c
@@ -9,18 +9,20 @@
 #include "command.h"  //sendf
 #include "sched.h" //DECL_COMMAND
 #include "board/gpio.h" //i2c_write/read/setup
+#include "i2c_software.h" // i2c_software_setup
 #include "i2ccmds.h"
 
+enum {
+    IF_SOFTWARE = 1, IF_HARDWARE = 2
+};
+
 void
 command_config_i2c(uint32_t *args)
 {
-    uint8_t addr = args[3] & 0x7f;
     struct i2cdev_s *i2c = oid_alloc(args[0], command_config_i2c
                                      , sizeof(*i2c));
-    i2c->i2c_config = i2c_setup(args[1], args[2], addr);
 }
-DECL_COMMAND(command_config_i2c,
-             "config_i2c oid=%c i2c_bus=%u rate=%u address=%u");
+DECL_COMMAND(command_config_i2c, "config_i2c oid=%c");
 
 struct i2cdev_s *
 i2cdev_oid_lookup(uint8_t oid)
@@ -28,6 +30,24 @@ i2cdev_oid_lookup(uint8_t oid)
     return oid_lookup(oid, command_config_i2c);
 }
 
+void
+command_i2c_set_bus(uint32_t *args)
+{
+    uint8_t addr = args[3] & 0x7f;
+    struct i2cdev_s *i2c = i2cdev_oid_lookup(args[0]);
+    i2c->i2c_config = i2c_setup(args[1], args[2], addr);
+    i2c->flags |= IF_HARDWARE;
+}
+DECL_COMMAND(command_i2c_set_bus,
+             "i2c_set_bus oid=%c i2c_bus=%u rate=%u address=%u");
+
+void
+i2cdev_set_software_bus(struct i2cdev_s *i2c, struct i2c_software *is)
+{
+    i2c->i2c_software = is;
+    i2c->flags |= IF_SOFTWARE;
+}
+
 void
 command_i2c_write(uint32_t *args)
 {
@@ -35,7 +55,11 @@ command_i2c_write(uint32_t *args)
     struct i2cdev_s *i2c = oid_lookup(oid, command_config_i2c);
     uint8_t data_len = args[1];
     uint8_t *data = command_decode_ptr(args[2]);
-    i2c_write(i2c->i2c_config, data_len, data);
+    uint_fast8_t flags = i2c->flags;
+    if (flags & IF_SOFTWARE)
+        i2c_software_write(i2c->i2c_software, data_len, data);
+    else
+        i2c_write(i2c->i2c_config, data_len, data);
 }
 DECL_COMMAND(command_i2c_write, "i2c_write oid=%c data=%*s");
 
@@ -48,7 +72,13 @@ command_i2c_read(uint32_t * args)
     uint8_t *reg = command_decode_ptr(args[2]);
     uint8_t data_len = args[3];
     uint8_t data[data_len];
-    i2c_read(i2c->i2c_config, reg_len, reg, data_len, data);
+    uint_fast8_t flags = i2c->flags;
+    if (flags & IF_SOFTWARE)
+        i2c_software_read(
+            i2c->i2c_software, reg_len, reg, data_len, data);
+    else
+        i2c_read(
+            i2c->i2c_config, reg_len, reg, data_len, data);
     sendf("i2c_read_response oid=%c response=%*s", oid, data_len, data);
 }
 DECL_COMMAND(command_i2c_read, "i2c_read oid=%c reg=%*s read_len=%u");
@@ -66,13 +96,22 @@ command_i2c_modify_bits(uint32_t *args)
     uint8_t data_len = clear_set_len/2;
     uint8_t *clear_set = command_decode_ptr(args[4]);
     uint8_t receive_data[reg_len + data_len];
+    uint_fast8_t flags = i2c->flags;
     memcpy(receive_data, reg, reg_len);
-    i2c_read(i2c->i2c_config, reg_len, reg, data_len, receive_data + reg_len);
+    if (flags & IF_SOFTWARE)
+        i2c_software_read(
+            i2c->i2c_software, reg_len, reg, data_len, receive_data + reg_len);
+    else
+        i2c_read(
+            i2c->i2c_config, reg_len, reg, data_len, receive_data + reg_len);
     for (int i = 0; i < data_len; i++) {
         receive_data[reg_len + i] &= ~clear_set[i];
         receive_data[reg_len + i] |= clear_set[data_len + i];
     }
-    i2c_write(i2c->i2c_config, reg_len + data_len, receive_data);
+    if (flags & IF_SOFTWARE)
+        i2c_software_write(i2c->i2c_software, reg_len + data_len, receive_data);
+    else
+        i2c_write(i2c->i2c_config, reg_len + data_len, receive_data);
 }
 DECL_COMMAND(command_i2c_modify_bits,
              "i2c_modify_bits oid=%c reg=%*s clear_set_bits=%*s");
diff --git a/src/i2ccmds.h b/src/i2ccmds.h
index 49c05c937..9ce54aa06 100644
--- a/src/i2ccmds.h
+++ b/src/i2ccmds.h
@@ -6,8 +6,11 @@
 
 struct i2cdev_s {
     struct i2c_config i2c_config;
+    struct i2c_software *i2c_software;
+    uint8_t flags;
 };
 
 struct i2cdev_s *i2cdev_oid_lookup(uint8_t oid);
+void i2cdev_set_software_bus(struct i2cdev_s *i2c, struct i2c_software *is);
 
 #endif
diff --git a/src/stm32/Kconfig b/src/stm32/Kconfig
index 60a303651..21e78f533 100644
--- a/src/stm32/Kconfig
+++ b/src/stm32/Kconfig
@@ -7,7 +7,7 @@ config STM32_SELECT
     default y
     select HAVE_GPIO
     select HAVE_GPIO_ADC
-    select HAVE_GPIO_I2C if !(MACH_STM32F031)
+    select HAVE_GPIO_I2C if !MACH_STM32F031
     select HAVE_GPIO_SPI if !MACH_STM32F031
     select HAVE_GPIO_SDIO if MACH_STM32F4
     select HAVE_GPIO_HARD_PWM if MACH_STM32F1 || MACH_STM32F4 || MACH_STM32F7 || MACH_STM32G0 || MACH_STM32H7