firmware icon indicating copy to clipboard operation
firmware copied to clipboard

[Feature Request]: Change default Grove port function to I2C for M5Stack C6L

Open shlorpian opened this issue 3 months ago • 4 comments

Platform

ESP32

Description

Hi,

I would like to suggest a feature or firmware layout optimization for the M5Stack C6L target.

Currently, the Grove port on the M5Stack C6L is configured to expose UART (for GPS functionality) by default. While this is great for a portable tracker node, many users (including myself) prefer using this compact hardware as a stationary sensor node powered by a wall outlet, rather than a battery pack. In a stationary deployment, having a GPS module attached makes little sense, whereas connecting environmental sensors (like temperature, humidity, or telemetry sensors over I2C) is highly desirable.

Right now, trying to connect I2C sensors to the external port causes issues because the firmware expects a GPS module there.

Proposed Solutions:

To make this node much more versatile and simplify its intended deployment role, I propose one of the following approaches:

  1. Switch Grove Port Defaults: Change the default pin mapping for the external Grove port from UART (GPS) to I2C (SDA/SCL), allowing plug-and-play compatibility with environmental sensors out of the box.
  2. Provide Two Firmware Variants: Release two distinct firmware binaries for the M5Stack C6L:
    • Portable/Tracker Version: Keeps the current UART configuration for GPS and power bank operations.
    • Stationary/Sensor Version: Configures the Grove port for I2C, allowing the node to easily adopt the Meshtastic SENSOR role (e.g., for reporting temperature and humidity).

This addition would greatly expand the usability of the C6L hardware within the Meshtastic ecosystem. Thank you for your time and all the amazing work on this firmware!

shlorpian avatar Aug 14 '26 16:08 shlorpian

@shlorpian, Welcome to Meshtastic! :wave:

Thanks for opening your first issue. If it's helpful, an easy way to get logs is the "Open Serial Monitor" button on the Web Flasher.

If you have ideas for features, note that we often debate big ideas in the discussions tab first. This tracker tends to be for ideas that have community consensus and a clear implementation.

We're very active on discord, especially the #firmware and #alphanauts-testing channels. If you'll be around for a while, we'd love to see you there!

Welcome to the community! :heart:

github-actions[bot] avatar Aug 14 '26 16:08 github-actions[bot]

duplicate of #10334 design decision, wontfix

caveman99 avatar Aug 17 '26 07:08 caveman99

It's a pity that the developers didn't respond to our request.

However, with Claude's help I was able to get the Grove port working via I2C, perhaps this will help others as well. Dev branch 2.8.0

/variants/esp32c6/m5stack_unitc6l/variant.cpp

#include "driver/gpio.h"
#include <Arduino.h>
#include <Wire.h>
// I2C device addr
#define PI4IO_M_ADDR 0x43
#define PI4IO_SDA_PIN 10
#define PI4IO_SCL_PIN 8

// PI4IO registers
#define PI4IO_REG_CHIP_RESET 0x01
#define PI4IO_REG_IO_DIR 0x03
#define PI4IO_REG_OUT_SET 0x05
#define PI4IO_REG_OUT_H_IM 0x07
#define PI4IO_REG_IN_DEF_STA 0x09
#define PI4IO_REG_PULL_EN 0x0B
#define PI4IO_REG_PULL_SEL 0x0D
#define PI4IO_REG_IN_STA 0x0F
#define PI4IO_REG_INT_MASK 0x11
#define PI4IO_REG_IRQ_STA 0x13
// PI4IO

#define setbit(x, y) x |= (0x01 << y)
#define clrbit(x, y) x &= ~(0x01 << y)
#define reversebit(x, y) x ^= (0x01 << y)
#define getbit(x, y) ((x) >> (y)&0x01)

#define SW_I2C_DELAY_US 5

static inline void scl_hi() { gpio_set_level((gpio_num_t)PI4IO_SCL_PIN, 1); }
static inline void scl_lo() { gpio_set_level((gpio_num_t)PI4IO_SCL_PIN, 0); }
static inline void sda_hi() { gpio_set_direction((gpio_num_t)PI4IO_SDA_PIN, GPIO_MODE_INPUT); }
static inline void sda_lo() { gpio_set_direction((gpio_num_t)PI4IO_SDA_PIN, GPIO_MODE_OUTPUT); gpio_set_level((gpio_num_t)PI4IO_SDA_PIN, 0); }
static inline int  sda_rd() { return gpio_get_level((gpio_num_t)PI4IO_SDA_PIN); }

static void sw_i2c_init()
{
    gpio_config_t scl_cfg = {};
    scl_cfg.pin_bit_mask = (1ULL << PI4IO_SCL_PIN);
    scl_cfg.mode = GPIO_MODE_OUTPUT;
    scl_cfg.pull_up_en = GPIO_PULLUP_ENABLE;
    gpio_config(&scl_cfg);
    gpio_set_level((gpio_num_t)PI4IO_SCL_PIN, 1);

    gpio_config_t sda_cfg = {};
    sda_cfg.pin_bit_mask = (1ULL << PI4IO_SDA_PIN);
    sda_cfg.mode = GPIO_MODE_INPUT;
    sda_cfg.pull_up_en = GPIO_PULLUP_ENABLE;
    gpio_config(&sda_cfg);
}

static void sw_i2c_start()
{
    sda_hi(); ets_delay_us(SW_I2C_DELAY_US);
    scl_hi(); ets_delay_us(SW_I2C_DELAY_US);
    sda_lo(); ets_delay_us(SW_I2C_DELAY_US);
    scl_lo(); ets_delay_us(SW_I2C_DELAY_US);
}

static void sw_i2c_stop()
{
    sda_lo(); ets_delay_us(SW_I2C_DELAY_US);
    scl_hi(); ets_delay_us(SW_I2C_DELAY_US);
    sda_hi(); ets_delay_us(SW_I2C_DELAY_US);
}

static bool sw_i2c_write_byte(uint8_t byte)
{
    for (int i = 7; i >= 0; i--) {
        if (byte & (1 << i)) sda_hi(); else sda_lo();
        ets_delay_us(SW_I2C_DELAY_US);
        scl_hi(); ets_delay_us(SW_I2C_DELAY_US);
        scl_lo(); ets_delay_us(SW_I2C_DELAY_US);
    }
    sda_hi(); ets_delay_us(SW_I2C_DELAY_US);
    scl_hi(); ets_delay_us(SW_I2C_DELAY_US);
    bool ack = (sda_rd() == 0);
    scl_lo(); ets_delay_us(SW_I2C_DELAY_US);
    return ack;
}

static uint8_t sw_i2c_read_byte(bool ack)
{
    uint8_t val = 0;
    sda_hi();
    for (int i = 7; i >= 0; i--) {
        ets_delay_us(SW_I2C_DELAY_US);
        scl_hi(); ets_delay_us(SW_I2C_DELAY_US);
        if (sda_rd()) val |= (1 << i);
        scl_lo();
    }
    if (ack) sda_lo(); else sda_hi();
    ets_delay_us(SW_I2C_DELAY_US);
    scl_hi(); ets_delay_us(SW_I2C_DELAY_US);
    scl_lo(); ets_delay_us(SW_I2C_DELAY_US);
    sda_hi();
    return val;
}

void i2c_read_byte(uint8_t addr, uint8_t reg, uint8_t *value)
{
    sw_i2c_start();
    sw_i2c_write_byte((addr << 1) | 0);
    sw_i2c_write_byte(reg);
    sw_i2c_start();
    sw_i2c_write_byte((addr << 1) | 1);
    *value = sw_i2c_read_byte(false);
    sw_i2c_stop();
}

void i2c_write_byte(uint8_t addr, uint8_t reg, uint8_t value)
{
    sw_i2c_start();
    sw_i2c_write_byte((addr << 1) | 0);
    sw_i2c_write_byte(reg);
    sw_i2c_write_byte(value);
    sw_i2c_stop();
}

void c6l_init()
{
    sw_i2c_init();

    printf("pi4io_init (bit-bang on SDA=%d SCL=%d)\n", PI4IO_SDA_PIN, PI4IO_SCL_PIN);
    uint8_t in_data;
    i2c_write_byte(PI4IO_M_ADDR, PI4IO_REG_CHIP_RESET, 0xFF);
    vTaskDelay(10 / portTICK_PERIOD_MS);
    i2c_read_byte(PI4IO_M_ADDR, PI4IO_REG_CHIP_RESET, &in_data);
    vTaskDelay(10 / portTICK_PERIOD_MS);
    i2c_write_byte(PI4IO_M_ADDR, PI4IO_REG_IO_DIR, 0b11100000); // P5,P6,P7 as outputs
    vTaskDelay(10 / portTICK_PERIOD_MS);
    i2c_write_byte(PI4IO_M_ADDR, PI4IO_REG_OUT_H_IM, 0b00011100); // High-Impedance
    vTaskDelay(10 / portTICK_PERIOD_MS);
    i2c_write_byte(PI4IO_M_ADDR, PI4IO_REG_PULL_SEL, 0b11100011); // pull up/down select, 0 down, 1 up
    vTaskDelay(10 / portTICK_PERIOD_MS);
    i2c_write_byte(PI4IO_M_ADDR, PI4IO_REG_PULL_EN, 0b11100011); // pull up/down enable, 0 disable, 1 enable
    vTaskDelay(10 / portTICK_PERIOD_MS);
    i2c_write_byte(PI4IO_M_ADDR, PI4IO_REG_IN_DEF_STA,
                   0b00000011); // P0 P1 default to high level; button press triggers the interrupt
    vTaskDelay(10 / portTICK_PERIOD_MS);
    i2c_write_byte(PI4IO_M_ADDR, PI4IO_REG_INT_MASK, 0b11111100); // P0 P1 interrupts enabled (0 = enable, 1 = disable)
    vTaskDelay(10 / portTICK_PERIOD_MS);
    i2c_write_byte(PI4IO_M_ADDR, PI4IO_REG_OUT_SET, 0b10000000); // default output is 0
    vTaskDelay(10 / portTICK_PERIOD_MS);
    i2c_read_byte(PI4IO_M_ADDR, PI4IO_REG_IRQ_STA, &in_data); // read IRQ_STA to clear the flag
    vTaskDelay(10 / portTICK_PERIOD_MS);

    i2c_read_byte(PI4IO_M_ADDR, PI4IO_REG_OUT_SET, &in_data);
    setbit(in_data, 6); // enable SX_ANT_SW
    setbit(in_data, 5); // enable SX_LNA_EN
    i2c_write_byte(PI4IO_M_ADDR, PI4IO_REG_OUT_SET, in_data);
}

/variants/esp32c6/m5stack_unitc6l/variant.h

void c6l_init();

// #define HAS_GPS 1
// #define GPS_RX_PIN 4
// #define GPS_TX_PIN 5

#define I2C_SDA 5
#define I2C_SCL 4

// #define PIN_BUZZER 11

#define HAS_NEOPIXEL                         // Enable the use of neopixels
#define NEOPIXEL_COUNT 1                     // How many neopixels are connected
#define NEOPIXEL_DATA 2                      // gpio pin used to send data to the neopixels
#define NEOPIXEL_TYPE (NEO_GRB + NEO_KHZ800) // type of neopixels in use
#define ENABLE_AMBIENTLIGHTING               // Turn on Ambient Lighting

#define OLED_COMPACT_UI

// #define BUTTON_PIN 9
#define BUTTON_EXTENDER

#undef LORA_SCK
#undef LORA_MISO
#undef LORA_MOSI
#undef LORA_CS

// WaveShare Core1262-868M OK
// https://www.waveshare.com/wiki/Core1262-868M
#define USE_SX1262

#define LORA_MISO 22
#define LORA_SCK 20
#define LORA_MOSI 21
#define LORA_CS 23
#define LORA_RESET RADIOLIB_NC
#define LORA_DIO1 7
#define LORA_BUSY 19
#define SX126X_CS LORA_CS
#define SX126X_DIO1 LORA_DIO1
#define SX126X_BUSY LORA_BUSY
#define SX126X_RESET LORA_RESET
#define SX126X_DIO2_AS_RF_SWITCH
#define SX126X_DIO3_TCXO_VOLTAGE 3.0

#define USE_SPISSD1306
#define DISPLAY_FORCE_SMALL_FONTS
#define SSD1306_NSS 6 // CS
#define SSD1306_RS 18 // DC
#define SSD1306_RESET 15
// #define OLED_DG 1
// Tiny OLED panel - opts into compile-time layout/font/feature substitutions
// gated on OLED_TINY across the graphics stack.
#define OLED_TINY
#define SCREEN_TRANSITION_FRAMERATE 10
#define BRIGHTNESS_DEFAULT 130 // Medium Low Brightness

#define SERIAL_PRINT_PORT 1

shlorpian avatar Aug 17 '26 19:08 shlorpian

From Meshtastic's perspective on the M5Stack Unit-C6L, the "primary" I2C bus (I2C Bus 0) is indeed the internal, 3v3 one. From my own notes:

Internal _3v3_ I2C Bus, also available via Test Points  (TP) on rear of PCB:
• 3v3 -- Test Point 8 (TP8)
• SCL -- Test Point 5 (TP5) / ESP32-C6 GPIO 8
• SDA -- Test Point 6 (TP6) / ESP32-C6 GPIO 10
• GND -- Test Point 3 (TP3)

Assigned in Meshtastic firmware via variant.h

#define I2C_SCL 8
#define I2C_SDA 10

While the external (white Port.X) Grove port on the M5Stack Unit-C6L is capable of all three M5Stack personalities (Port.A, Port.B, Port.C) … the Meshtastic firmware implementation elected a logical choice of "configure as Port.C / UART" so that GPS receivers would be plug-and-play. From my own notes:

NOTE: Depending on sticker orientation, may read left-to-right { White, Yellow, Red, Black } or { Black, Red, Yellow, White }.

Grove Port
HY2.0-4P                Black   Red   Yellow   White

White Connector -- I2C, GPIO, UART, I2S, CANbus, etc…
PORT.X (Host Configurable / Custom)
                        GND     5v0   S2       S1

Red Connector -- I2C
PORT.A (I2C)
                        GND     5v0   SDA      SCL

Black Connector -- GPIO
PORT.B (GPIO, PWM, Servo, 10-12 bit ADC Sample, RGB Light, …)
                        GND     5v0   .        .

Blue Connector -- UART
PORT.C (UART)
                        GND     5v0   RxD      TxD

'.' == software-implementation configurable.

Configuring the external Grove port for Port.A I2C mode in looking at Meshtastic 2.8.0 code is a substantial amount of work. It would require adding a second I2C bus pin-pair definition (I2C Bus 1), backing code (not currently a generic Meshtastic feature, only some variants/… implement I2C Bus 1+ via special-casing), figuring out how to add a UART vs. I2C mode awareness in multiple UIs, among other changes, not to mention requisite QA load.

Splitting to a "UART vs. I2C Bus 1" firmware pair for the M5Stack Unit-C6L only addresses a small effort part of the above work involved.

The sample code generated by Claude mentioned in https://github.com/meshtastic/firmware/issues/11501#issuecomment-5319581609 is interesting, swapping I2C Bus 0 out to the Grove port, and altering PI4IO init routines … however it would mean the code is far less portable, as there is more than one finished product using the M5Stack Stamp C6LoRa module (core internals of the Unit C6L), and at least one of them (Arduino Nesso N1) have other I2C devices on I2C Bus 0 beyond what the Unit C6L does.

virtualjmills avatar Aug 26 '26 00:08 virtualjmills