Device migrations and driver improvements (#576)

This commit is contained in:
Ken Van Hoeylandt
2026-07-22 21:11:12 +02:00
committed by GitHub
parent 2fbc44466a
commit a3fda9ad8f
100 changed files with 707 additions and 3838 deletions
+3 -3
View File
@@ -1,7 +1,7 @@
file(GLOB_RECURSE SOURCE_FILES Source/*.c*)
file(GLOB_RECURSE SOURCE_FILES source/*.c*)
idf_component_register(
SRCS ${SOURCE_FILES}
INCLUDE_DIRS "Source"
REQUIRES Tactility esp_lvgl_port ILI934x FT6x36 AXP2101 driver vfs fatfs ina226-module py32ioexpander-module
INCLUDE_DIRS "source"
REQUIRES TactilityKernel py32ioexpander-module
)
@@ -1,162 +0,0 @@
#include "devices/Display.h"
#include "devices/Power.h"
#include <tactility/device.h>
#include <tactility/drivers/gpio_controller.h>
#include <tactility/drivers/i2c_controller.h>
#include <tactility/log.h>
#include <drivers/py32ioexpander.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <Tactility/hal/Configuration.h>
#include <Axp2101Power.h>
#include <Axp2101.h>
using namespace tt::hal;
static const auto* TAG = "StackChan";
// ---------------------------------------------------------------------------
// I2C addresses
// ---------------------------------------------------------------------------
static constexpr uint8_t AXP2101_ADDR = 0x34;
// ---------------------------------------------------------------------------
// AW9523B GPIO expander pin map — same wiring as CoreS3.
// AW88298 reset (P0_2) is driven directly by the aw88298-module driver itself
// (see m5stack,stackchan.dts pin-reset property), not from here.
// ---------------------------------------------------------------------------
constexpr auto AW9523B_PIN_TOUCH_RESET = 0; // P0_0
constexpr auto AW9523B_PIN_BUS_OUT_ENABLE = 1; // P0_1
constexpr auto AW9523B_PIN_SD_CARD_SWITCH = 4; // P0_4
constexpr auto AW9523B_PIN_LCD_RESET = 8 + 1; // P1_1
constexpr auto AW9523B_PIN_BOOST_ENABLE = 8 + 7; // P1_7 (SY7088)
static bool initGpioExpander(::Device* aw9523b) {
struct PinInit { uint8_t pin; bool level; };
static constexpr PinInit pins[] = {
{ AW9523B_PIN_TOUCH_RESET, true },
{ AW9523B_PIN_BUS_OUT_ENABLE, true },
{ AW9523B_PIN_SD_CARD_SWITCH, true },
{ AW9523B_PIN_LCD_RESET, true },
{ AW9523B_PIN_BOOST_ENABLE, true },
};
for (const auto& pinInit : pins) {
auto* descriptor = gpio_descriptor_acquire(aw9523b, pinInit.pin, GPIO_OWNER_GPIO);
if (descriptor == nullptr) {
LOG_E(TAG, "AW9523B: Failed to acquire pin %u", pinInit.pin);
return false;
}
error_t error = gpio_descriptor_set_flags(descriptor, GPIO_FLAG_DIRECTION_OUTPUT);
if (error == ERROR_NONE) {
error = gpio_descriptor_set_level(descriptor, pinInit.level);
}
gpio_descriptor_release(descriptor);
if (error != ERROR_NONE) {
LOG_E(TAG, "AW9523B: Failed to configure pin %u", pinInit.pin);
return false;
}
}
return true;
}
// ---------------------------------------------------------------------------
// AXP2101 power management — same voltage rails as CoreS3
// ---------------------------------------------------------------------------
static bool initPowerControl(::Device* i2c) {
// Source: https://github.com/m5stack/M5Unified/blob/b8cfec7fed046242da7f7b8024a4e92004a51ff7/src/utility/Power_Class.cpp#L64
static constexpr uint8_t reg_data[] = {
0x90U, 0xBFU, // LDOS ON/OFF control 0 (backlight)
0x92U, 18U - 5U, // ALDO1 = 1.8V (AW88298)
0x93U, 33U - 5U, // ALDO2 = 3.3V (ES7210)
0x94U, 33U - 5U, // ALDO3 = 3.3V (camera)
0x95U, 33U - 5U, // ALDO4 = 3.3V (TF card)
0x27U, 0x00U, // PowerKey Hold=1sec / PowerOff=4sec
0x69U, 0x11U, // CHGLED setting
0x10U, 0x30U, // PMU common config
0x30U, 0x0FU, // ADC enabled
};
if (i2c_controller_write_register_array(i2c, AXP2101_ADDR, reg_data, sizeof(reg_data), pdMS_TO_TICKS(1000)) != ERROR_NONE) {
LOG_E(TAG, "AXP2101: Failed to set registers");
return false;
}
return true;
}
// ---------------------------------------------------------------------------
// initBoot — called after devicetree devices are constructed/started
// ---------------------------------------------------------------------------
static std::shared_ptr<Axp2101> axp2101;
bool initBoot() {
auto* i2c = device_find_by_name("i2c_internal");
if (i2c == nullptr) {
LOG_E(TAG, "i2c_internal not found");
return false;
}
// Boost enable via AXP2101 before GPIO expander init (same as CoreS3)
if (!initPowerControl(i2c)) {
LOG_E(TAG, "AXP2101 init failed");
return false;
}
auto* aw9523b = device_find_by_name("aw9523b");
if (aw9523b == nullptr) {
LOG_E(TAG, "aw9523b not found");
return false;
}
if (!initGpioExpander(aw9523b)) {
LOG_E(TAG, "AW9523B init failed");
return false;
}
// Boot LED pattern — confirms PY32IOExpander is working.
// PY32 pin 0 = servo VM_EN (output), pin 13 = WS2812C data line.
auto* py32 = device_find_by_name("py32");
if (py32 != nullptr) {
static constexpr uint8_t LED_COUNT = 12;
static constexpr uint8_t COLORS[][3] = {
{ 255, 0, 0 }, // red
{ 0, 255, 0 }, // green
{ 0, 0, 255 }, // blue
};
py32_led_set_count(py32, LED_COUNT);
for (auto& c : COLORS) {
for (uint8_t i = 0; i < LED_COUNT; i++) {
py32_led_set_color(py32, i, c[0], c[1], c[2]);
}
py32_led_refresh(py32);
vTaskDelay(pdMS_TO_TICKS(150));
}
py32_led_disable(py32);
} else {
LOG_W(TAG, "py32 not found — LED boot pattern skipped");
}
// Keep Axp2101 C++ wrapper alive for Axp2101Power (backlight + battery)
axp2101 = std::make_shared<Axp2101>(i2c);
return true;
}
// ---------------------------------------------------------------------------
// Device list
// ---------------------------------------------------------------------------
static DeviceVector createDevices() {
return {
axp2101,
std::make_shared<Axp2101Power>(axp2101),
createPower(),
createDisplay(),
};
}
extern const Configuration hardwareConfiguration = {
.initBoot = initBoot,
.createDevices = createDevices
};
@@ -1,61 +0,0 @@
#include "Display.h"
#include <Axp2101.h>
#include <Ft6x36Touch.h>
#include <Ili934xDisplay.h>
#include <tactility/check.h>
#include <tactility/log.h>
constexpr auto* TAG = "StackChanDisplay";
static void setBacklightDuty(uint8_t backlightDuty) {
const uint8_t voltage = 20 + ((8 * backlightDuty) / 255); // [0b00000, 0b11100]
auto controller = device_find_by_name("i2c_internal");
check(controller);
if (i2c_controller_write_register(controller, AXP2101_ADDRESS, 0x99, &voltage, 1, 1000) != ERROR_NONE) { // Sets DLD01
LOG_E(TAG, "Failed to set display backlight voltage");
}
}
static std::shared_ptr<tt::hal::touch::TouchDevice> createTouch() {
auto configuration = std::make_unique<Ft6x36Touch::Configuration>(
I2C_NUM_0,
319,//LCD_HORIZONTAL_RESOLUTION,
239,//LCD_VERTICAL_RESOLUTION,
false,
false,
false
);
return std::make_shared<Ft6x36Touch>(std::move(configuration));
}
std::shared_ptr<tt::hal::display::DisplayDevice> createDisplay() {
Ili934xDisplay::Configuration panel_configuration = {
.horizontalResolution = LCD_HORIZONTAL_RESOLUTION,
.verticalResolution = LCD_VERTICAL_RESOLUTION,
.gapX = 0,
.gapY = 0,
.swapXY = false,
.mirrorX = false,
.mirrorY = false,
.invertColor = true,
.swapBytes = true,
.bufferSize = LCD_BUFFER_SIZE,
.touch = createTouch(),
.backlightDutyFunction = ::setBacklightDuty,
.resetPin = GPIO_NUM_NC,
.rgbElementOrder = LCD_RGB_ELEMENT_ORDER_BGR
};
auto spi_configuration = std::make_shared<Ili934xDisplay::SpiConfiguration>(Ili934xDisplay::SpiConfiguration {
.spiHostDevice = LCD_SPI_HOST,
.csPin = LCD_PIN_CS,
.dcPin = LCD_PIN_DC,
.pixelClockFrequency = 40'000'000,
.transactionQueueDepth = 10
});
return std::make_shared<Ili934xDisplay>(panel_configuration, spi_configuration, true);
}
@@ -1,17 +0,0 @@
#pragma once
#include <Tactility/hal/display/DisplayDevice.h>
#include <driver/gpio.h>
#include <driver/spi_common.h>
// Display
constexpr auto LCD_SPI_HOST = SPI2_HOST;
constexpr auto LCD_PIN_CS = GPIO_NUM_3;
constexpr auto LCD_PIN_DC = GPIO_NUM_35;
constexpr auto LCD_HORIZONTAL_RESOLUTION = 320;
constexpr auto LCD_VERTICAL_RESOLUTION = 240;
constexpr auto LCD_BUFFER_HEIGHT = LCD_VERTICAL_RESOLUTION / 10;
constexpr auto LCD_BUFFER_SIZE = LCD_HORIZONTAL_RESOLUTION * LCD_BUFFER_HEIGHT;
constexpr auto LCD_SPI_TRANSFER_SIZE_LIMIT = LCD_BUFFER_SIZE * LV_COLOR_DEPTH / 8;
std::shared_ptr<tt::hal::display::DisplayDevice> createDisplay();
@@ -1,86 +0,0 @@
#include "Power.h"
#include <Tactility/hal/power/PowerDevice.h>
#include <drivers/ina226.h>
#include <tactility/device.h>
#include <tactility/log.h>
using namespace tt::hal::power;
static constexpr auto* TAG = "StackChanPower";
// 1S Li-ion cell (550 mAh): 3.2V (empty) 4.2V (full)
static constexpr float MIN_BATTERY_VOLTAGE_MV = 3200.0f;
static constexpr float MAX_BATTERY_VOLTAGE_MV = 4200.0f;
class StackChanPower final : public PowerDevice {
public:
explicit StackChanPower(::Device* ina226Device) : ina226(ina226Device) {}
std::string getName() const override { return "M5Stack StackChan Power"; }
std::string getDescription() const override { return "Battery monitoring via INA226 over I2C"; }
bool supportsMetric(MetricType type) const override {
switch (type) {
using enum MetricType;
case BatteryVoltage:
case ChargeLevel:
case Current:
return ina226 != nullptr;
default:
return false;
}
}
bool getMetric(MetricType type, MetricData& data) override {
switch (type) {
using enum MetricType;
case BatteryVoltage: {
if (ina226 == nullptr) return false;
float volts = 0.0f;
if (ina226_read_bus_voltage(ina226, &volts) != ERROR_NONE) return false;
data.valueAsUint32 = static_cast<uint32_t>(volts * 1000.0f);
return true;
}
case ChargeLevel: {
if (ina226 == nullptr) return false;
float volts = 0.0f;
if (ina226_read_bus_voltage(ina226, &volts) != ERROR_NONE) return false;
float voltage_mv = volts * 1000.0f;
if (voltage_mv >= MAX_BATTERY_VOLTAGE_MV) {
data.valueAsUint8 = 100;
} else if (voltage_mv <= MIN_BATTERY_VOLTAGE_MV) {
data.valueAsUint8 = 0;
} else {
float factor = (voltage_mv - MIN_BATTERY_VOLTAGE_MV) / (MAX_BATTERY_VOLTAGE_MV - MIN_BATTERY_VOLTAGE_MV);
data.valueAsUint8 = static_cast<uint8_t>(factor * 100.0f);
}
return true;
}
case Current: {
if (ina226 == nullptr) return false;
float amps = 0.0f;
if (ina226_read_shunt_current(ina226, &amps) != ERROR_NONE) return false;
data.valueAsInt32 = static_cast<int32_t>(amps * 1000.0f);
return true;
}
default:
return false;
}
}
private:
::Device* ina226;
};
std::shared_ptr<PowerDevice> createPower() {
auto* ina226 = device_find_by_name("ina226");
if (ina226 == nullptr) {
LOG_E(TAG, "ina226 device not found");
}
return std::make_shared<StackChanPower>(ina226);
}
@@ -1,6 +0,0 @@
#pragma once
#include <memory>
#include <Tactility/hal/power/PowerDevice.h>
std::shared_ptr<tt::hal::power::PowerDevice> createPower();
@@ -1,14 +0,0 @@
#include <tactility/error.h>
#include <tactility/module.h>
extern "C" {
Module m5stack_stackchan_module = {
.name = "m5stack-stackchan",
.start = [] -> error_t { return ERROR_NONE; },
.stop = [] -> error_t { return ERROR_NONE; },
.symbols = nullptr,
.internal = nullptr
};
}
@@ -12,6 +12,8 @@ hardware.tinyUsb=true
hardware.esptoolFlashFreq=120M
hardware.bluetooth=true
dependencies.useDeprecatedHal=false
storage.userDataLocation=SD
display.size=2"
+8 -5
View File
@@ -1,11 +1,14 @@
dependencies:
- Platforms/platform-esp32
- Drivers/bmi270-module
- Drivers/audio-stream-module
- Drivers/aw88298-module
- Drivers/aw9523b-module
- Drivers/axp2101-module
- Drivers/bm8563-module
- Drivers/bmi270-module
- Drivers/es7210-module
- Drivers/ft6x36-module
- Drivers/ili9341-module
- Drivers/ina226-module
- Drivers/py32ioexpander-module
- Drivers/aw9523b-module
- Drivers/aw88298-module
- Drivers/es7210-module
- Drivers/audio-stream-module
dts: m5stack,stackchan.dts
+74 -12
View File
@@ -9,15 +9,19 @@
#include <tactility/bindings/esp32_i2s.h>
#include <tactility/bindings/esp32_spi.h>
#include <tactility/bindings/esp32_uart.h>
#include <bindings/bmi270.h>
#include <bindings/aw88298.h>
#include <bindings/aw9523b.h>
#include <bindings/axp2101.h>
#include <bindings/axp2101_backlight.h>
#include <bindings/bm8563.h>
#include <bindings/bmi270.h>
#include <bindings/es7210.h>
#include <bindings/ft6x36.h>
#include <bindings/ili9341.h>
#include <bindings/ina226.h>
#include <bindings/py32ioexpander.h>
#include <bindings/aw9523b.h>
#include <bindings/aw88298.h>
#include <bindings/es7210.h>
#include <tactility/bindings/esp32_sdspi.h>
#include <tactility/bindings/display_placeholder.h>
#include <tactility/bindings/gpio_hog.h>
// Reference: https://docs.m5stack.com/en/StackChan
/ {
@@ -40,7 +44,7 @@
};
// AW88298 speaker + ES7210 microphone
i2s0: i2s0 {
i2s0 {
compatible = "espressif,esp32-i2s";
port = <I2S_NUM_0>;
pin-bclk = <&gpio0 34 GPIO_FLAG_NONE>;
@@ -77,14 +81,34 @@
compatible = "ti,ina226";
reg = <0x41>;
shunt-milliohms = <10>;
power-supply;
};
// AW9523B pin map (same wiring as CoreS3):
// P0_0 = touch reset, P0_1 = bus-out enable, P0_2 = AW88298 reset,
// P0_4 = SD card switch, P1_1 = LCD reset, P1_7 = boost enable (SY7088)
aw9523b: aw9523b {
aw9523b {
compatible = "awinic,aw9523b";
reg = <0x58>;
// Bus-out enable, AW88298 (audio amp) reset, SD card power switch and boost enable
// (SY7088) are AW9523B pins with no owning peripheral driver yet.
aw9523_bus_out_enable {
compatible = "gpio-hog";
pin = <&aw9523b 1 GPIO_FLAG_NONE>;
mode = <GPIO_HOG_MODE_OUTPUT_HIGH>;
};
aw9523_sdcard_switch {
compatible = "gpio-hog";
pin = <&aw9523b 4 GPIO_FLAG_NONE>;
mode = <GPIO_HOG_MODE_OUTPUT_HIGH>;
};
aw9523_boost_enable {
compatible = "gpio-hog";
pin = <&aw9523b 15 GPIO_FLAG_NONE>;
mode = <GPIO_HOG_MODE_OUTPUT_HIGH>;
};
};
aw88298 {
@@ -94,6 +118,39 @@
pin-reset = <&aw9523b 2 GPIO_FLAG_NONE>;
};
// Same rail assignment as CoreS3: ALDO1=AW88298, ALDO2=ES7210, ALDO3=camera
// (not yet implemented, but harmless to power), ALDO4=TF/SD card. BLDO1/BLDO2 are
// enabled with no specific voltage requirement.
axp2101 {
compatible = "x-powers,axp2101";
reg = <0x34>;
aldo1-millivolt = <1800>;
aldo1-enabled;
aldo2-millivolt = <3300>;
aldo2-enabled;
aldo3-millivolt = <3300>;
aldo3-enabled;
aldo4-millivolt = <3300>;
aldo4-enabled;
bldo1-enabled;
bldo2-enabled;
display_backlight {
compatible = "axp2101-backlight";
ldo = <AXP2101_DLDO1>;
min-millivolt = <2500>;
max-millivolt = <3300>;
};
};
touch {
compatible = "focaltech,ft6x36";
reg = <0x38>;
x-max = <319>;
y-max = <239>;
pin-reset = <&aw9523b 0 GPIO_FLAG_NONE>;
};
es7210 {
compatible = "everest,es7210";
reg = <0x40>;
@@ -109,9 +166,6 @@
input-gain-percent = <400>;
};
// AXP2101 PMIC @ 0x34 — initialized manually in initBoot()
// FT6336U capacitive touch @ 0x38 — used by Display driver (FT6x36 library)
// TODO: Si12T 3-zone head touch @ 0x68 — INT active-low, 10kΩ pull-up to 3.3V; driver not yet implemented
// TODO: GC0308 camera @ 0x21 — requires i2c_master driver, not yet available
// TODO: LTR-553ALS-WA proximity/light @ 0x23 — no driver yet
@@ -167,7 +221,15 @@
pin-sclk = <&gpio0 36 GPIO_FLAG_NONE>;
display@0 {
compatible = "display-placeholder";
compatible = "ilitek,ili9341";
horizontal-resolution = <320>;
vertical-resolution = <240>;
invert-color;
bgr-order;
pixel-clock-hz = <40000000>;
pin-dc = <&gpio0 35 GPIO_FLAG_NONE>;
pin-reset = <&aw9523b 9 GPIO_FLAG_NONE>;
backlight = <&display_backlight>;
};
sdcard@1 {
@@ -0,0 +1,64 @@
#include <drivers/py32ioexpander.h>
#include <tactility/check.h>
#include <tactility/device.h>
#include <tactility/device_listener.h>
#include <tactility/module.h>
#include <cstring>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
extern "C" {
// Boot LED pattern (red/green/blue sweep across the 12-LED WS2812C ring).
// Confirms the PY32L020 body IO expander is working.
static void run_led_boot_pattern(Device* py32) {
static constexpr uint8_t LED_COUNT = 12;
static constexpr uint8_t COLORS[][3] = {
{ 255, 0, 0 }, // red
{ 0, 255, 0 }, // green
{ 0, 0, 255 }, // blue
};
py32_led_set_count(py32, LED_COUNT);
for (const auto& color : COLORS) {
for (uint8_t i = 0; i < LED_COUNT; i++) {
py32_led_set_color(py32, i, color[0], color[1], color[2]);
}
py32_led_refresh(py32);
vTaskDelay(pdMS_TO_TICKS(150));
}
py32_led_disable(py32);
}
static void on_device_event(Device* device, DeviceEvent event, void* context) {
(void)context;
if (event != DEVICE_EVENT_STARTED) {
return;
}
if (strcmp(device->name, "py32") == 0) {
run_led_boot_pattern(device);
}
}
static error_t start() {
device_listener_add(on_device_event, nullptr);
return ERROR_NONE;
}
static error_t stop() {
device_listener_remove(on_device_event);
return ERROR_NONE;
}
Module m5stack_stackchan_module = {
.name = "m5stack-stackchan",
.start = start,
.stop = stop,
.symbols = nullptr,
.internal = nullptr
};
}