Tab5 features, StackChan, fixes, drivers.... (#526)

This commit is contained in:
Shadowtrance
2026-05-29 07:31:25 +10:00
committed by GitHub
parent 5c78d55b04
commit a59fbf4ed5
140 changed files with 2500 additions and 835 deletions
@@ -1,5 +1,6 @@
#include "devices/Display.h"
#include "devices/SdCard.h"
#include "devices/Power.h"
#include <tactility/drivers/gpio_controller.h>
#include <tactility/drivers/i2c_controller.h>
@@ -13,6 +14,7 @@ static constexpr auto* TAG = "Tab5";
static DeviceVector createDevices() {
return {
createPower(),
createDisplay(),
createSdCard(),
};
@@ -195,6 +197,57 @@ static error_t initSound(::Device* i2c_controller, ::Device* io_expander0 = null
return ERROR_NONE;
}
static error_t initMicrophone(::Device* i2c_controller) {
// ES7210 quad-channel microphone ADC at 0x40.
// Register sequence from M5Unified (M5Unified.cpp, _microphone_enabled_cb_tab5).
// Configures 4-slot TDM output at 48kHz/16-bit with MIC1+MIC2 active and MICBIAS enabled.
static constexpr uint8_t ES7210_I2C_ADDR = 0x40;
static constexpr uint8_t INIT_DATA[] = {
0x00, 0xFF, // RESET_CTL: full reset
0x00, 0x41, // RESET_CTL: release reset, keep CSM active
0x01, 0x1F, // CLK_ON_OFF: enable all clocks
0x06, 0x00, // DIGITAL_PDN: power up all digital blocks
0x07, 0x20, // ADC_OSR: OSR=256
0x08, 0x10, // MODE_CFG: I2S slave, TDM mode
0x09, 0x30, // TCT0_CHPINI: chopper init period
0x0A, 0x30, // TCT1_CHPINI
0x20, 0x0A, // ADC34_HPF2
0x21, 0x2A, // ADC34_HPF1
0x22, 0x0A, // ADC12_HPF2
0x23, 0x2A, // ADC12_HPF1
0x02, 0xC1, // CLK_CTRL: MCLK from I2S, PLL off
0x04, 0x01, // SDPOUT_CTL1: TDM output enable
0x05, 0x00, // SDPOUT_CTL0
0x11, 0x60, // DBIAS: adjust reference voltage for P4
0x40, 0x42, // ANALOG_SYS: enable analog supply
0x41, 0x70, // MICBIAS12: enable MICBIAS for MIC1+MIC2
0x42, 0x70, // MICBIAS34: enable MICBIAS for MIC3+MIC4
0x43, 0x1B, // MIC1_GAIN: +30 dB
0x44, 0x1B, // MIC2_GAIN: +30 dB
0x45, 0x00, // MIC3_GAIN: AEC ref, no gain
0x46, 0x00, // MIC4_GAIN: AEC ref, no gain
0x47, 0x00, // MIC1_LP
0x48, 0x00, // MIC2_LP
0x49, 0x00, // MIC3_LP
0x4A, 0x00, // MIC4_LP
0x4B, 0x00, // MIC12_PDN: power up MIC1+MIC2
0x4C, 0xFF, // MIC34_PDN: keep MIC3+MIC4 in power-down (AEC ref not needed)
0x01, 0x14, // CLK_ON_OFF: final clock config
};
error_t error = i2c_controller_write_register_array(
i2c_controller,
ES7210_I2C_ADDR,
INIT_DATA,
sizeof(INIT_DATA),
pdMS_TO_TICKS(1000)
);
if (error != ERROR_NONE) {
LOG_E(TAG, "Failed to init ES7210: %s", error_to_string(error));
}
return error;
}
static bool initBoot() {
auto* i2c0 = device_find_by_name("i2c0");
check(i2c0, "i2c0 not found");
@@ -212,6 +265,11 @@ static bool initBoot() {
LOG_E(TAG, "Failed to enable ES8388");
}
error = initMicrophone(i2c0);
if (error != ERROR_NONE) {
LOG_E(TAG, "Failed to init ES7210");
}
return true;
}
@@ -0,0 +1,192 @@
#include "Power.h"
#include <Tactility/hal/power/PowerDevice.h>
#include <drivers/ina226.h>
#include <tactility/device.h>
#include <tactility/drivers/gpio_controller.h>
#include <tactility/log.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
using namespace tt::hal::power;
static constexpr auto* TAG = "Tab5Power";
// NP-F550 is a 2S Li-ion pack; INA226 measures the pack voltage on BAT_IN
// before the DC-DC converter. Per-cell range 3.2-4.2V → pack range 6.4-8.4V.
static constexpr float MIN_BATTERY_VOLTAGE_MV = 6400.0f;
static constexpr float MAX_BATTERY_VOLTAGE_MV = 8400.0f;
// INA226 convention: negative raw current = charging, positive = discharging.
// After negation in getMetric(Current), >50mA means charging into the battery.
static constexpr float CHARGING_CURRENT_THRESHOLD_AMPS = 0.05f;
// GPIO expander 1 (0x44) pin 4: PWROFF_PULSE
static constexpr int GPIO_EXP1_PIN_DEVICE_POWER = 4;
// GPIO expander 1 (0x44) pin 5: IP2326 nCHG_QC_EN (active-low: LOW = QC enabled)
static constexpr int GPIO_EXP1_PIN_IP2326_NCHG_QC_EN = 5;
// GPIO expander 1 (0x44) pin 7: IP2326 CHG_EN (HIGH = charging enabled, LOW = disabled)
static constexpr int GPIO_EXP1_PIN_IP2326_CHG_EN = 7;
class Tab5Power final : public PowerDevice {
public:
Tab5Power(::Device* ina226Device, ::Device* ioExpander1Device)
: ina226(ina226Device), ioExpander1(ioExpander1Device) {
// Initialize CHG_EN as output HIGH (charging enabled at startup).
setAllowedToCharge(true);
}
std::string getName() const override { return "M5Stack Tab5 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:
case IsCharging:
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;
// INA226 convention: negative = charging, positive = discharging.
// Negate so the HAL value is positive when charging, negative when discharging.
data.valueAsInt32 = static_cast<int32_t>(-amps * 1000.0f);
return true;
}
case IsCharging: {
if (ina226 == nullptr) return false;
float amps = 0.0f;
if (ina226_read_shunt_current(ina226, &amps) != ERROR_NONE) return false;
// Raw INA226: negative = charging. Threshold in raw terms = -0.05A.
data.valueAsBool = amps < -CHARGING_CURRENT_THRESHOLD_AMPS;
return true;
}
default:
return false;
}
}
bool supportsChargeControl() const override { return ioExpander1 != nullptr; }
bool isAllowedToCharge() const override { return chargingAllowed; }
void setAllowedToCharge(bool allowed) override {
if (ioExpander1 == nullptr) return;
auto* pin = gpio_descriptor_acquire(ioExpander1, GPIO_EXP1_PIN_IP2326_CHG_EN, GPIO_OWNER_GPIO);
if (pin == nullptr) {
LOG_W(TAG, "Failed to acquire CHG_EN pin");
return;
}
if (gpio_descriptor_set_flags(pin, GPIO_FLAG_DIRECTION_OUTPUT) != ERROR_NONE) {
LOG_W(TAG, "Failed to set CHG_EN pin direction");
gpio_descriptor_release(pin);
return;
}
if (gpio_descriptor_set_level(pin, allowed) != ERROR_NONE) {
LOG_W(TAG, "Failed to set CHG_EN pin level");
gpio_descriptor_release(pin);
return;
}
gpio_descriptor_release(pin);
chargingAllowed = allowed;
}
bool supportsQuickCharge() const override { return ioExpander1 != nullptr; }
bool isQuickChargeEnabled() const override { return quickChargeEnabled; }
void setQuickChargeEnabled(bool enabled) override {
if (ioExpander1 == nullptr) return;
auto* pin = gpio_descriptor_acquire(ioExpander1, GPIO_EXP1_PIN_IP2326_NCHG_QC_EN, GPIO_OWNER_GPIO);
if (pin == nullptr) {
LOG_W(TAG, "Failed to acquire nCHG_QC_EN pin");
return;
}
if (gpio_descriptor_set_flags(pin, GPIO_FLAG_DIRECTION_OUTPUT) != ERROR_NONE) {
LOG_W(TAG, "Failed to set nCHG_QC_EN pin direction");
gpio_descriptor_release(pin);
return;
}
if (gpio_descriptor_set_level(pin, !enabled) != ERROR_NONE) {
LOG_W(TAG, "Failed to set nCHG_QC_EN pin level");
gpio_descriptor_release(pin);
return;
}
gpio_descriptor_release(pin);
quickChargeEnabled = enabled;
}
bool supportsPowerOff() const override { return ioExpander1 != nullptr; }
void powerOff() override {
if (ioExpander1 == nullptr) return;
auto* pin = gpio_descriptor_acquire(ioExpander1, GPIO_EXP1_PIN_DEVICE_POWER, GPIO_OWNER_GPIO);
if (pin == nullptr) {
LOG_E(TAG, "Failed to acquire DEVICE_POWER pin");
return;
}
for (int i = 0; i < 3; i++) {
gpio_descriptor_set_level(pin, true);
vTaskDelay(pdMS_TO_TICKS(100));
gpio_descriptor_set_level(pin, false);
vTaskDelay(pdMS_TO_TICKS(100));
}
gpio_descriptor_release(pin);
}
private:
::Device* ina226;
::Device* ioExpander1;
bool chargingAllowed = true;
bool quickChargeEnabled = false;
};
std::shared_ptr<PowerDevice> createPower() {
auto* ina226 = device_find_by_name("ina226");
if (ina226 == nullptr) {
LOG_E(TAG, "ina226 device not found");
}
auto* io_expander1 = device_find_by_name("io_expander1");
if (io_expander1 == nullptr) {
LOG_E(TAG, "io_expander1 not found");
}
return std::make_shared<Tab5Power>(ina226, io_expander1);
}
@@ -0,0 +1,6 @@
#pragma once
#include <memory>
#include <Tactility/hal/power/PowerDevice.h>
std::shared_ptr<tt::hal::power::PowerDevice> createPower();
+101 -2
View File
@@ -1,14 +1,113 @@
#include <tactility/module.h>
#include <tactility/device.h>
#include <tactility/drivers/gpio_controller.h>
#include <tactility/log.h>
#include <freertos/FreeRTOS.h>
#include <freertos/timers.h>
#include <atomic>
#define TAG "Tab5"
constexpr auto GPIO_EXP0_PIN_SPEAKER_ENABLE = 1;
constexpr auto GPIO_EXP0_PIN_HEADPHONE_DETECT = 7;
constexpr auto HP_DETECT_POLL_MS = 1000;
// hp_detect_timer is only touched from start()/stop(), which are called serially
// by the module manager — no atomic needed for the handle itself.
static TimerHandle_t hp_detect_timer = nullptr;
static std::atomic<Device*> io_expander0_cached { nullptr };
// Flags are written by the timer daemon task and read by start()/stop() — use atomics.
static std::atomic<bool> hp_detect_last { false };
static std::atomic<bool> hp_detect_initialized { false };
static void headphoneDetectCallback(TimerHandle_t /*timer*/) {
Device* cached = io_expander0_cached.load(std::memory_order_acquire);
if (!cached) {
cached = device_find_by_name("io_expander0");
io_expander0_cached.store(cached, std::memory_order_release);
}
auto* io_expander0 = cached;
if (!io_expander0) {
return; // Not ready yet, will retry on next tick
}
auto* hp_pin = gpio_descriptor_acquire(io_expander0, GPIO_EXP0_PIN_HEADPHONE_DETECT, GPIO_OWNER_GPIO);
if (!hp_pin) {
LOG_W(TAG, "hp_detect: HP_DET pin busy");
return;
}
bool hp = false;
error_t err = gpio_descriptor_get_level(hp_pin, &hp);
gpio_descriptor_release(hp_pin);
if (err != ERROR_NONE) {
LOG_W(TAG, "hp_detect: HP_DET read error: %s", error_to_string(err));
return;
}
LOG_D(TAG, "hp_detect: HP_DET=%d", (int)hp);
if (!hp_detect_initialized || hp != hp_detect_last) {
auto* spk_pin = gpio_descriptor_acquire(io_expander0, GPIO_EXP0_PIN_SPEAKER_ENABLE, GPIO_OWNER_GPIO);
if (!spk_pin) {
LOG_W(TAG, "hp_detect: SPK_EN pin busy, will retry");
return;
}
error_t spk_err = gpio_descriptor_set_level(spk_pin, !hp);
gpio_descriptor_release(spk_pin);
if (spk_err != ERROR_NONE) {
LOG_W(TAG, "hp_detect: SPK_EN set error: %s, will retry", error_to_string(spk_err));
return;
}
hp_detect_last = hp;
hp_detect_initialized = true;
LOG_I(TAG, "Headphones %s, speaker %s", hp ? "detected" : "removed", hp ? "disabled" : "enabled");
}
}
extern "C" {
static error_t start() {
// Empty for now
if (hp_detect_timer != nullptr) {
LOG_W(TAG, "hp_detect timer already running");
return ERROR_NONE;
}
hp_detect_initialized = false;
hp_detect_last = false;
hp_detect_timer = xTimerCreate("hp_detect", pdMS_TO_TICKS(HP_DETECT_POLL_MS), pdTRUE, nullptr, headphoneDetectCallback);
if (!hp_detect_timer) {
LOG_E(TAG, "Failed to create hp_detect timer");
return ERROR_RESOURCE;
}
if (xTimerStart(hp_detect_timer, pdMS_TO_TICKS(100)) != pdPASS) {
LOG_E(TAG, "Failed to start hp_detect timer");
xTimerDelete(hp_detect_timer, pdMS_TO_TICKS(100));
hp_detect_timer = nullptr;
return ERROR_RESOURCE;
}
return ERROR_NONE;
}
static error_t stop() {
// Empty for now
if (hp_detect_timer == nullptr) {
return ERROR_NONE;
}
if (xTimerStop(hp_detect_timer, pdMS_TO_TICKS(100)) != pdPASS) {
LOG_W(TAG, "Failed to stop hp_detect timer");
}
if (xTimerDelete(hp_detect_timer, pdMS_TO_TICKS(100)) != pdPASS) {
LOG_E(TAG, "Failed to delete hp_detect timer");
}
// Always clear the handle — stale non-null handle is worse than a resource leak,
// as it would cause start() to silently skip re-creating the timer.
hp_detect_timer = nullptr;
io_expander0_cached.store(nullptr, std::memory_order_release);
return ERROR_NONE;
}