Migrate devices, create drivers, other improvements & fixes (#574)

Migrated devices to kernel drivers:

- guition-jc3248w535c
- m5stack-core2
- m5stack-cores3
- m5stack-papers3

New drivers:

- axp192-module
- axp2101-module

Fixes:

- Fix SD card LDO for P4 devices
- Updated PowerOff app to work with kernel displays
- Fix for `lvgl_try_lock()` timing
- Fix for touch events with slow updating displays

Improvements:

- `GuiService` now keeps trying to lock to prevent silent failures caused by drivers.
- `GuiService` now uses lvgl-module calls for locking/unlocking
- display driver now has capability `DISPLAY_CAPABILITY_SLOW_REFRESH`
This commit is contained in:
Ken Van Hoeylandt
2026-07-19 00:39:26 +02:00
committed by GitHub
parent 5f54f7ca3d
commit f9453d8956
119 changed files with 3327 additions and 3994 deletions
+5 -4
View File
@@ -1,6 +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 EPDiyDisplay GT911 Tactility driver EstimatedPower
idf_component_register(
SRCS ${SOURCE_FILES}
INCLUDE_DIRS "source"
REQUIRES TactilityKernel epdiy
)
@@ -1,20 +0,0 @@
#include "devices/Display.h"
#include "devices/Power.h"
#include <Tactility/hal/Configuration.h>
using namespace tt::hal;
bool initBoot();
static DeviceVector createDevices() {
return {
createPower(),
createDisplay(),
};
}
extern const Configuration hardwareConfiguration = {
.initBoot = initBoot,
.createDevices = createDevices
};
-48
View File
@@ -1,48 +0,0 @@
#include <driver/gpio.h>
#include <tactility/log.h>
constexpr auto* TAG = "Paper S3";
constexpr gpio_num_t VBAT_PIN = GPIO_NUM_3;
constexpr gpio_num_t CHARGE_STATUS_PIN = GPIO_NUM_4;
constexpr gpio_num_t USB_DETECT_PIN = GPIO_NUM_5;
static bool powerOn() {
if (gpio_reset_pin(CHARGE_STATUS_PIN) != ESP_OK) {
LOG_E(TAG, "Failed to reset CHARGE_STATUS_PIN");
return false;
}
if (gpio_set_direction(CHARGE_STATUS_PIN, GPIO_MODE_INPUT) != ESP_OK) {
LOG_E(TAG, "Failed to set direction for CHARGE_STATUS_PIN");
return false;
}
if (gpio_reset_pin(USB_DETECT_PIN) != ESP_OK) {
LOG_E(TAG, "Failed to reset USB_DETECT_PIN");
return false;
}
if (gpio_set_direction(USB_DETECT_PIN, GPIO_MODE_INPUT) != ESP_OK) {
LOG_E(TAG, "Failed to set direction for USB_DETECT_PIN");
return false;
}
// VBAT_PIN is used as ADC input; only reset it here to clear any previous
// configuration. The ADC driver (ChargeFromAdcVoltage) configures it for ADC use.
if (gpio_reset_pin(VBAT_PIN) != ESP_OK) {
LOG_E(TAG, "Failed to reset VBAT_PIN");
return false;
}
return true;
}
bool initBoot() {
LOG_I(TAG, "Power on");
if (!powerOn()) {
LOG_E(TAG, "Power on failed");
return false;
}
return true;
}
@@ -1,27 +0,0 @@
#include "Display.h"
#include <Gt911Touch.h>
#include <EpdiyDisplayHelper.h>
#include <tactility/check.h>
#include <tactility/device.h>
std::shared_ptr<tt::hal::touch::TouchDevice> createTouch() {
auto* i2c = device_find_by_name("i2c_internal");
check(i2c);
auto configuration = std::make_unique<Gt911Touch::Configuration>(
i2c,
540,
960,
true, // swapXy
true, // mirrorX
false, // mirrorY
GPIO_NUM_NC, // pinReset
GPIO_NUM_NC //48 pinInterrupt
);
return std::make_shared<Gt911Touch>(std::move(configuration));
}
std::shared_ptr<tt::hal::display::DisplayDevice> createDisplay() {
auto touch = createTouch();
return EpdiyDisplayHelper::createM5PaperS3Display(touch);
}
@@ -1,6 +0,0 @@
#pragma once
#include <memory>
#include <Tactility/hal/display/DisplayDevice.h>
std::shared_ptr<tt::hal::display::DisplayDevice> createDisplay();
@@ -1,282 +0,0 @@
#include "Power.h"
#include <tactility/log.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <driver/ledc.h>
using namespace tt::hal::power;
constexpr auto* TAG = "PaperS3Power";
// M5Stack PaperS3 hardware pin definitions
constexpr gpio_num_t VBAT_PIN = GPIO_NUM_3; // Battery voltage with 2x divider
constexpr adc_channel_t VBAT_ADC_CHANNEL = ADC_CHANNEL_2; // GPIO3 = ADC1_CHANNEL_2
constexpr gpio_num_t CHARGE_STATUS_PIN = GPIO_NUM_4; // Charge IC status: 0 = charging, 1 = full/no USB
constexpr gpio_num_t USB_DETECT_PIN = GPIO_NUM_5; // USB detect: 1 = USB connected
constexpr gpio_num_t POWER_OFF_PIN = GPIO_NUM_44; // Pull high to trigger shutdown
constexpr gpio_num_t BUZZER_PIN = GPIO_NUM_21;
// Battery voltage divider ratio (voltage is divided by 2)
constexpr float VOLTAGE_DIVIDER_MULTIPLIER = 2.0f;
// Battery voltage range for LiPo batteries
constexpr float MIN_BATTERY_VOLTAGE = 3.3f;
constexpr float MAX_BATTERY_VOLTAGE = 4.2f;
// Power-off signal timing
constexpr int POWER_OFF_PULSE_COUNT = 5;
constexpr int POWER_OFF_PULSE_DURATION_MS = 100;
constexpr uint32_t BUZZER_DUTY_50_PERCENT = 4096; // 50% of 13-bit (8192)
PaperS3Power::PaperS3Power(
std::unique_ptr<ChargeFromAdcVoltage> chargeFromAdcVoltage,
gpio_num_t powerOffPin
)
: chargeFromAdcVoltage(std::move(chargeFromAdcVoltage)),
powerOffPin(powerOffPin) {
LOG_I(TAG, "Initialized M5Stack PaperS3 power management");
}
void PaperS3Power::buzzerLedcInit() {
if (buzzerInitialized) {
LOG_I(TAG, "Buzzer already initialized");
return;
}
ledc_timer_config_t timer_cfg = {
.speed_mode = LEDC_LOW_SPEED_MODE,
.duty_resolution = LEDC_TIMER_13_BIT,
.timer_num = LEDC_TIMER_0,
.freq_hz = 1000,
.clk_cfg = LEDC_AUTO_CLK,
.deconfigure = false
};
esp_err_t err = ledc_timer_config(&timer_cfg);
if (err != ESP_OK) {
LOG_E(TAG, "LEDC timer config failed: %s", esp_err_to_name(err));
return;
}
ledc_channel_config_t channel_cfg = {
.gpio_num = BUZZER_PIN,
.speed_mode = LEDC_LOW_SPEED_MODE,
.channel = LEDC_CHANNEL_0,
.intr_type = LEDC_INTR_DISABLE,
.timer_sel = LEDC_TIMER_0,
.duty = 0,
.hpoint = 0,
.sleep_mode = LEDC_SLEEP_MODE_NO_ALIVE_NO_PD,
.flags = {
.output_invert = 0
}
};
err = ledc_channel_config(&channel_cfg);
if (err != ESP_OK) {
LOG_E(TAG, "LEDC channel config failed: %s", esp_err_to_name(err));
return;
}
buzzerInitialized = true;
}
void PaperS3Power::initializePowerOff() {
if (powerOffInitialized) {
return;
}
gpio_config_t io_conf = {
.pin_bit_mask = (1ULL << powerOffPin),
.mode = GPIO_MODE_OUTPUT,
.pull_up_en = GPIO_PULLUP_DISABLE,
.pull_down_en = GPIO_PULLDOWN_DISABLE,
.intr_type = GPIO_INTR_DISABLE,
};
esp_err_t err = gpio_config(&io_conf);
if (err != ESP_OK) {
LOG_E(TAG, "Failed to configure power-off pin GPIO%d: %s", powerOffPin, esp_err_to_name(err));
return;
}
gpio_set_level(powerOffPin, 0);
powerOffInitialized = true;
LOG_I(TAG, "Power-off control initialized on GPIO%d", powerOffPin);
buzzerLedcInit();
}
// TODO: Fix USB Detection
bool PaperS3Power::isUsbConnected() {
// USB_DETECT_PIN is configured as input with pull-down by initBoot() in Init.cpp.
// Level 1 = USB VBUS present (per M5PaperS3 hardware spec).
bool usbConnected = gpio_get_level(USB_DETECT_PIN) == 1;
LOG_D(TAG, "USB_STATUS(GPIO%d)=%d", USB_DETECT_PIN, (int)usbConnected);
return usbConnected;
}
bool PaperS3Power::isCharging() {
// CHARGE_STATUS_PIN is configured as GPIO_MODE_INPUT by initBoot() in Init.cpp.
int chargePin = gpio_get_level(CHARGE_STATUS_PIN);
LOG_D(TAG, "CHG_STATUS(GPIO%d)=%d", CHARGE_STATUS_PIN, chargePin);
return chargePin == 0;
}
bool PaperS3Power::supportsMetric(MetricType type) const {
switch (type) {
using enum MetricType;
case BatteryVoltage:
case ChargeLevel:
case IsCharging:
return true;
default:
return false;
}
}
bool PaperS3Power::getMetric(MetricType type, MetricData& data) {
switch (type) {
using enum MetricType;
case BatteryVoltage:
return chargeFromAdcVoltage->readBatteryVoltageSampled(data.valueAsUint32);
case ChargeLevel: {
uint32_t voltage = 0;
if (chargeFromAdcVoltage->readBatteryVoltageSampled(voltage)) {
data.valueAsUint8 = chargeFromAdcVoltage->estimateChargeLevelFromVoltage(voltage);
return true;
}
return false;
}
case IsCharging:
// isUsbConnected() is tracked separately but not used as a gate here:
// when USB is absent the charge IC's CHG pin is inactive (high), so
// isCharging() already returns false correctly.
data.valueAsBool = isCharging();
return true;
default:
return false;
}
}
void PaperS3Power::toneOn(int frequency, int duration) {
if (!buzzerInitialized) {
LOG_I(TAG, "Buzzer not initialized");
return;
}
if (frequency <= 0) {
LOG_I(TAG, "Invalid frequency: %d", frequency);
return;
}
esp_err_t err = ledc_set_freq(LEDC_LOW_SPEED_MODE, LEDC_TIMER_0, frequency);
if (err != ESP_OK) {
LOG_E(TAG, "LEDC set freq failed: %s", esp_err_to_name(err));
return;
}
err = ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, BUZZER_DUTY_50_PERCENT);
if (err != ESP_OK) {
LOG_E(TAG, "LEDC set duty failed: %s", esp_err_to_name(err));
return;
}
err = ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0);
if (err != ESP_OK) {
LOG_E(TAG, "LEDC update duty failed: %s", esp_err_to_name(err));
return;
}
if (duration > 0) {
vTaskDelay(pdMS_TO_TICKS(duration));
toneOff();
}
}
void PaperS3Power::toneOff() {
if (!buzzerInitialized) {
LOG_I(TAG, "Buzzer not initialized");
return;
}
esp_err_t err = ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, 0);
if (err != ESP_OK) {
LOG_E(TAG, "LEDC set duty failed: %s", esp_err_to_name(err));
return;
}
err = ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0);
if (err != ESP_OK) {
LOG_E(TAG, "LEDC update duty failed: %s", esp_err_to_name(err));
return;
}
}
void PaperS3Power::powerOff() {
LOG_W(TAG, "Power-off requested");
// Note: callers are responsible for stopping the display (e.g. EPD refresh) before
// calling powerOff(). The beep sequence below (~500 ms) provides some lead time,
// but a full EPD refresh can take up to ~1500 ms. If a refresh is still in flight
// when GPIO44 cuts power, the current frame will be incomplete; the display will
// recover correctly on next boot via a full-screen clear.
if (!powerOffInitialized) {
initializePowerOff();
if (!powerOffInitialized) {
LOG_E(TAG, "Power-off failed: GPIO not initialized");
return;
}
}
//beep on
toneOn(440, 200);
vTaskDelay(pdMS_TO_TICKS(100));
//beep on
toneOn(440, 200);
LOG_W(TAG, "Triggering shutdown via GPIO%d (sending %d pulses)...", powerOffPin, POWER_OFF_PULSE_COUNT);
for (int i = 0; i < POWER_OFF_PULSE_COUNT; i++) {
gpio_set_level(powerOffPin, 1);
vTaskDelay(pdMS_TO_TICKS(POWER_OFF_PULSE_DURATION_MS));
gpio_set_level(powerOffPin, 0);
vTaskDelay(pdMS_TO_TICKS(POWER_OFF_PULSE_DURATION_MS));
}
gpio_set_level(powerOffPin, 1); // Final high state
LOG_W(TAG, "Shutdown signal sent. Waiting for power-off...");
vTaskDelay(pdMS_TO_TICKS(1000));
LOG_E(TAG, "Device did not power off as expected");
}
std::shared_ptr<PowerDevice> createPower() {
ChargeFromAdcVoltage::Configuration config = {
.adcMultiplier = VOLTAGE_DIVIDER_MULTIPLIER,
.adcRefVoltage = 3.3f,
.adcChannel = VBAT_ADC_CHANNEL,
.adcConfig = {
.unit_id = ADC_UNIT_1,
.clk_src = ADC_RTC_CLK_SRC_DEFAULT,
.ulp_mode = ADC_ULP_MODE_DISABLE,
},
.adcChannelConfig = {
.atten = ADC_ATTEN_DB_12,
.bitwidth = ADC_BITWIDTH_DEFAULT,
},
};
auto adc = std::make_unique<ChargeFromAdcVoltage>(config, MIN_BATTERY_VOLTAGE, MAX_BATTERY_VOLTAGE);
if (!adc->isInitialized()) {
LOG_E(TAG, "ADC initialization failed; power monitoring unavailable");
return nullptr;
}
return std::make_shared<PaperS3Power>(std::move(adc), POWER_OFF_PIN);
}
@@ -1,55 +0,0 @@
#pragma once
#include <memory>
#include <ChargeFromAdcVoltage.h>
#include <Tactility/hal/power/PowerDevice.h>
#include <driver/gpio.h>
using tt::hal::power::PowerDevice;
/**
* @brief Power management for M5Stack PaperS3
*
* Hardware configuration:
* - Battery voltage: GPIO3 (ADC1_CHANNEL_2) with 2x voltage divider
* - Charge status: GPIO4 - digital signal (0 = charging, 1 = not charging)
* - USB detect: GPIO5 - digital signal (1 = USB connected)
* - Power off: GPIO44 - pull high to trigger shutdown
*/
class PaperS3Power final : public PowerDevice {
private:
std::unique_ptr<::ChargeFromAdcVoltage> chargeFromAdcVoltage;
gpio_num_t powerOffPin;
bool powerOffInitialized = false;
bool buzzerInitialized = false;
public:
explicit PaperS3Power(
std::unique_ptr<::ChargeFromAdcVoltage> chargeFromAdcVoltage,
gpio_num_t powerOffPin
);
~PaperS3Power() override = default;
std::string getName() const override { return "M5Stack PaperS3 Power"; }
std::string getDescription() const override { return "Battery monitoring with charge detection and power-off"; }
bool supportsMetric(MetricType type) const override;
bool getMetric(MetricType type, MetricData& data) override;
bool supportsPowerOff() const override { return true; }
void powerOff() override;
private:
void initializePowerOff();
bool isCharging();
// TODO: Fix USB Detection
bool isUsbConnected();
// Buzzer functions only used for the power off signal sound.
// So the user actually knows the epaper display is turning off.
void buzzerLedcInit();
void toneOn(int frequency, int duration);
void toneOff();
};
std::shared_ptr<tt::hal::power::PowerDevice> createPower();
-23
View File
@@ -1,23 +0,0 @@
#include <tactility/module.h>
extern "C" {
static error_t start() {
// Empty for now
return ERROR_NONE;
}
static error_t stop() {
// Empty for now
return ERROR_NONE;
}
struct Module m5stack_papers3_module = {
.name = "m5stack-papers3",
.start = start,
.stop = stop,
.symbols = nullptr,
.internal = nullptr
};
}
@@ -0,0 +1,20 @@
description: >
M5Stack PaperS3 E-Ink display (EPDiy library, ED047TC1 panel over the board's dedicated
parallel bus - epd_board_m5papers3 hardcodes all of its pins internally, so this node takes
no pin properties of its own).
compatible: "m5stack,papers3-display"
properties:
temperature-celsius:
type: int
default: 20
description: Ambient temperature in °C, used for waveform timing compensation
draw-mode:
type: int
default: MODE_DU
description: Default EpdDrawMode waveform used for screen updates (e.g. MODE_DU, MODE_GC16)
rotation:
type: int
default: EPD_ROT_PORTRAIT
description: Fixed EpdRotation applied at start - not changeable at runtime (see driver README/comments)
@@ -0,0 +1,23 @@
description: >
M5Stack PaperS3 charge/power control: charge IC status readback and shutdown-pulse power off.
Battery voltage/capacity are handled separately by a generic battery-sense node.
compatible: "m5stack,papers3-power"
properties:
pin-charge-status:
type: phandles
required: true
description: Charge IC status pin (active-low - 0 = charging, 1 = full/no USB)
pin-usb-detect:
type: phandles
required: true
description: USB VBUS detect pin (1 = USB connected)
pin-power-off:
type: phandles
required: true
description: Shutdown control pin - pulled high to trigger power-off
pwm:
type: phandles
required: true
description: Tone generator (PWM_TYPE device) used for the power-off confirmation beep
@@ -15,6 +15,8 @@ hardware.bluetooth=true
storage.userDataLocation=SD
dependencies.useDeprecatedHal=false
display.size=4.7"
display.shape=rectangle
display.dpi=235
@@ -24,3 +26,4 @@ lvgl.fontSize=24
lvgl.theme=Mono
sdkconfig.CONFIG_EPD_DISPLAY_TYPE_ED047TC2=y
sdkconfig.CONFIG_LV_THEME_DEFAULT_TRANSITION_TIME=0
+2
View File
@@ -2,4 +2,6 @@ dependencies:
- Platforms/platform-esp32
- Drivers/bmi270-module
- Drivers/bm8563-module
- Drivers/gt911-module
bindings: bindings
dts: m5stack,papers3.dts
+54 -1
View File
@@ -6,9 +6,15 @@
#include <tactility/bindings/esp32_gpio.h>
#include <tactility/bindings/esp32_i2c.h>
#include <tactility/bindings/esp32_spi.h>
#include <tactility/bindings/esp32_adc_oneshot.h>
#include <tactility/bindings/esp32_pwm_ledc.h>
#include <tactility/bindings/battery_sense.h>
#include <bindings/bmi270.h>
#include <bindings/bm8563.h>
#include <bindings/gt911.h>
#include <tactility/bindings/esp32_sdspi.h>
#include <bindings/papers3_power.h>
#include <bindings/papers3_display.h>
/ {
compatible = "root";
@@ -45,6 +51,14 @@
compatible = "belling,bm8563";
reg = <0x51>;
};
touch {
compatible = "goodix,gt911";
reg = <0x5D>;
x-max = <540>;
y-max = <960>;
pin-interrupt = <&gpio0 48 GPIO_FLAG_NONE>;
};
};
spi0 {
@@ -55,10 +69,49 @@
pin-miso = <&gpio0 40 GPIO_FLAG_NONE>;
pin-sclk = <&gpio0 39 GPIO_FLAG_NONE>;
max-transfer-size = <4096>;
sdcard@0 {
compatible = "espressif,esp32-sdspi";
frequency-khz = <20000>;
};
};
adc0 {
compatible = "espressif,esp32-adc-oneshot";
unit-id = <ADC_UNIT_1>;
channels = <ADC_CHANNEL_2 ADC_ATTEN_DB_12 ADC_BITWIDTH_DEFAULT>;
};
battery-sense {
compatible = "battery-sense";
io-channel = <&adc0 0>;
reference-voltage-mv = <3300>;
multiplier = <2000>;
};
buzzer_pwm {
compatible = "espressif,esp32-pwm-ledc";
pin = <&gpio0 21 GPIO_FLAG_NONE>;
period-ns = <1000000>;
ledc-timer = <0>;
ledc-channel = <0>;
};
power {
compatible = "m5stack,papers3-power";
pin-charge-status = <&gpio0 4 GPIO_FLAG_NONE>;
pin-usb-detect = <&gpio0 5 GPIO_FLAG_NONE>;
pin-power-off = <&gpio0 44 GPIO_FLAG_NONE>;
pwm = <&buzzer_pwm>;
};
// M5Stack PaperS3 E-Ink display (EPDiy library, ED047TC1 panel)
display {
compatible = "m5stack,papers3-display";
temperature-celsius = <20>;
// EPD_ROT_PORTRAIT rendered 180deg rotated on real hardware; INVERTED_PORTRAIT is
// exactly 180deg from it (verified against epdiy's _rotate()) and is this panel's
// correct orientation.
rotation = <EPD_ROT_INVERTED_PORTRAIT>;
};
};
@@ -0,0 +1,14 @@
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
#include <tactility/bindings/bindings.h>
#include <drivers/papers3_display.h>
DEFINE_DEVICETREE(papers3_display, struct Papers3DisplayConfig)
#ifdef __cplusplus
}
#endif
@@ -0,0 +1,14 @@
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
#include <tactility/bindings/bindings.h>
#include <drivers/papers3_power.h>
DEFINE_DEVICETREE(papers3_power, struct Papers3PowerConfig)
#ifdef __cplusplus
}
#endif
@@ -0,0 +1,292 @@
// SPDX-License-Identifier: Apache-2.0
#include "papers3_display.h"
#include <tactility/device.h>
#include <tactility/driver.h>
#include <tactility/drivers/display.h>
#include <tactility/error.h>
#include <tactility/log.h>
#include <tactility/module.h>
#include <epd_board.h>
#include <epdiy.h>
#include <cstdlib>
#include <cstring>
#define TAG "Papers3Display"
#define GET_CONFIG(device) (static_cast<const Papers3DisplayConfig*>((device)->config))
// Maps each src byte (8px, MSB-first, bit=1 -> white/0x0F) to the 4 packed dst bytes
// (2px/byte, EPDiy MODE_PACKING_2PPB nibble order) it produces, replacing a per-pixel
// branch loop with a table lookup.
static uint32_t s_unpack_lut[256];
static void init_unpack_lut() {
for (uint32_t byte = 0; byte < 256; byte++) {
uint8_t dst[4];
for (int32_t pair = 0; pair < 4; pair++) {
const uint8_t bit0 = (byte >> (7 - pair * 2)) & 0x01U;
const uint8_t bit1 = (byte >> (7 - pair * 2 - 1)) & 0x01U;
const uint8_t p0 = bit0 ? 0x0FU : 0x00U;
const uint8_t p1 = bit1 ? 0x0FU : 0x00U;
dst[pair] = static_cast<uint8_t>((p1 << 4U) | p0);
}
memcpy(&s_unpack_lut[byte], dst, sizeof(dst));
}
}
extern "C" {
extern Module m5stack_papers3_module;
// epd_hl_init() sets an internal already_initialized flag and has no matching deinit, so the
// highlevel state (and the framebuffer it owns) must persist across stop()/start() cycles and be
// reused rather than recreated - ported from the old deprecated-HAL EpdiyDisplay's identical
// s_hlInitialized/s_hlState statics.
static bool s_hl_initialized = false;
static EpdiyHighlevelState s_hl_state = {};
struct Papers3DisplayInternal {
EpdiyHighlevelState hl_state;
uint8_t* framebuffer;
// Scratch buffer for the I1(1bpp)->EPDiy(4bpp packed, 2px/byte) conversion in draw_bitmap().
uint8_t* packed_buffer;
bool powered;
};
static void power_on(Papers3DisplayInternal* internal) {
if (!internal->powered) {
epd_poweron();
internal->powered = true;
}
}
// region DisplayApi
static error_t papers3_display_reset(Device* device) {
auto* internal = static_cast<Papers3DisplayInternal*>(device_get_driver_data(device));
// EPD has no discrete reset pin/sequence the way SPI TFT panels do - epd_init() (in start())
// already performs the real one-time hardware bring-up. A power-cycle is the closest
// equivalent available at runtime.
epd_poweroff();
internal->powered = false;
power_on(internal);
return ERROR_NONE;
}
static error_t papers3_display_init(Device* device) {
auto* internal = static_cast<Papers3DisplayInternal*>(device_get_driver_data(device));
power_on(internal);
epd_clear();
epd_hl_set_all_white(&internal->hl_state);
return ERROR_NONE;
}
// LVGL only ever calls this with the full frame: DISPLAY_COLOR_FORMAT_MONOCHROME forces
// LV_DISPLAY_RENDER_MODE_FULL in the generic kernel LVGL bridge (lvgl_display.c), and FULL mode
// only presents (calls draw_bitmap) once per render cycle, with the complete 0,0..hres,vres rect.
static error_t papers3_display_draw_bitmap(Device* device, int32_t x_start, int32_t y_start, int32_t x_end, int32_t y_end, const void* color_data) {
auto* internal = static_cast<Papers3DisplayInternal*>(device_get_driver_data(device));
const auto* config = GET_CONFIG(device);
const int32_t width = x_end - x_start;
const int32_t height = y_end - y_start;
// color_data is DISPLAY_COLOR_FORMAT_MONOCHROME: row-major, MSB-first 1bpp (LVGL's LV_COLOR_FORMAT_I1
// with the palette header already stripped by the caller). Bit 1 = white/lit (LVGL's I1 blend
// sets a bit when the source luminance is above its threshold), bit 0 = black.
const auto* src = static_cast<const uint8_t*>(color_data);
const size_t src_stride = static_cast<size_t>(width + 7) / 8;
const size_t packed_stride = static_cast<size_t>(width + 1) / 2;
for (int32_t row = 0; row < height; row++) {
const uint8_t* src_row = src + static_cast<size_t>(row) * src_stride;
uint8_t* dst_row = internal->packed_buffer + static_cast<size_t>(row) * packed_stride;
int32_t col = 0;
// Bulk path: one LUT lookup + 4-byte copy per 8 source pixels.
for (; col + 8 <= width; col += 8) {
memcpy(dst_row + col / 2, &s_unpack_lut[src_row[col / 8]], 4);
}
// Tail: fewer than 8 pixels left (width not a multiple of 8).
for (; col < width; col += 2) {
const uint8_t bit0 = (src_row[col / 8] >> (7 - (col % 8))) & 0x01U;
const uint8_t p0 = bit0 ? 0x0FU : 0x00U;
uint8_t p1 = 0;
if (col + 1 < width) {
const uint8_t bit1 = (src_row[(col + 1) / 8] >> (7 - ((col + 1) % 8))) & 0x01U;
p1 = bit1 ? 0x0FU : 0x00U;
}
dst_row[col / 2] = static_cast<uint8_t>((p1 << 4U) | p0);
}
}
const EpdRect update_area = {
.x = x_start,
.y = y_start,
.width = static_cast<uint16_t>(width),
.height = static_cast<uint16_t>(height)
};
power_on(internal);
epd_draw_rotated_image(update_area, internal->packed_buffer, internal->framebuffer);
auto draw_result = epd_hl_update_area(
&internal->hl_state,
static_cast<EpdDrawMode>(config->draw_mode | MODE_PACKING_2PPB),
config->temperature_celsius,
update_area
);
return draw_result == EPD_DRAW_SUCCESS ? ERROR_NONE : ERROR_RESOURCE;
}
static error_t papers3_display_disp_on_off(Device* device, bool on_off) {
auto* internal = static_cast<Papers3DisplayInternal*>(device_get_driver_data(device));
if (on_off) {
power_on(internal);
} else if (internal->powered) {
epd_poweroff();
internal->powered = false;
}
return ERROR_NONE;
}
static DisplayColorFormat papers3_display_get_color_format(Device*) {
return DISPLAY_COLOR_FORMAT_MONOCHROME;
}
// epd_width()/epd_height() are the panel's native, unrotated dimensions (display->width/height in
// epdiy.c) - epd_rotated_display_width()/height() swap them for EPD_ROT_PORTRAIT/INVERTED_PORTRAIT.
// epd_draw_rotated_image() clamps its input rect against the *rotated* dims and epd_draw_pixel()
// applies the rotation transform on top of that (see _rotate() in epdiy.c), so both LVGL's canvas
// size and draw_bitmap()'s rect must be in rotated-space, not native-space - using the native
// epd_width()/epd_height() here fed rotated-space code a landscape-sized canvas, which produced
// exactly the "rotated + landscape" symptom this was fixed for.
static uint16_t papers3_display_get_resolution_x(Device*) {
return static_cast<uint16_t>(epd_rotated_display_width());
}
static uint16_t papers3_display_get_resolution_y(Device*) {
return static_cast<uint16_t>(epd_rotated_display_height());
}
static void papers3_display_get_frame_buffer(Device*, uint8_t, void** out_buffer) {
// Not exposed via the generic fb-direct path: EPDiy's framebuffer is its own 4bpp packed
// format, not the DISPLAY_COLOR_FORMAT_MONOCHROME (1bpp) this driver reports - see
// get_frame_buffer_count() and draw_bitmap()'s conversion.
*out_buffer = nullptr;
}
static uint8_t papers3_display_get_frame_buffer_count(Device*) {
return 0;
}
// endregion
static const DisplayApi papers3_display_api = {
.capabilities = DISPLAY_CAPABILITY_ON_OFF | DISPLAY_CAPABILITY_REQUIRES_FULL_FRAME | DISPLAY_CAPABILITY_SLOW_REFRESH,
.reset = papers3_display_reset,
.init = papers3_display_init,
.draw_bitmap = papers3_display_draw_bitmap,
.mirror = nullptr,
.swap_xy = nullptr,
.get_swap_xy = nullptr,
.get_mirror_x = nullptr,
.get_mirror_y = nullptr,
.set_gap = nullptr,
.get_gap_x = nullptr,
.get_gap_y = nullptr,
.invert_color = nullptr,
.disp_on_off = papers3_display_disp_on_off,
.disp_sleep = nullptr,
.get_color_format = papers3_display_get_color_format,
.get_resolution_x = papers3_display_get_resolution_x,
.get_resolution_y = papers3_display_get_resolution_y,
.get_frame_buffer = papers3_display_get_frame_buffer,
.get_frame_buffer_count = papers3_display_get_frame_buffer_count,
.get_backlight = nullptr,
.has_capability = nullptr,
};
// region Driver lifecycle
static error_t start(Device* device) {
const auto* config = GET_CONFIG(device);
static bool s_lut_initialized = false;
if (!s_lut_initialized) {
init_unpack_lut();
s_lut_initialized = true;
}
auto* internal = static_cast<Papers3DisplayInternal*>(malloc(sizeof(Papers3DisplayInternal)));
if (internal == nullptr) {
return ERROR_OUT_OF_MEMORY;
}
internal->powered = false;
epd_init(&epd_board_m5papers3, &ED047TC1, static_cast<EpdInitOptions>(EPD_LUT_1K | EPD_FEED_QUEUE_32));
epd_set_rotation(config->rotation);
if (!s_hl_initialized) {
s_hl_state = epd_hl_init(EPD_BUILTIN_WAVEFORM);
if (s_hl_state.front_fb == nullptr) {
LOG_E(TAG, "Failed to initialize EPDiy highlevel state");
epd_deinit();
free(internal);
return ERROR_RESOURCE;
}
s_hl_initialized = true;
} else {
LOG_I(TAG, "Reusing existing EPDiy highlevel state");
}
internal->hl_state = s_hl_state;
internal->framebuffer = epd_hl_get_framebuffer(&internal->hl_state);
// Sized for the rotated (LVGL-facing) resolution - see get_resolution_x()/y()'s comment.
const size_t packed_buffer_size = static_cast<size_t>((epd_rotated_display_width() + 1) / 2) * static_cast<size_t>(epd_rotated_display_height());
internal->packed_buffer = static_cast<uint8_t*>(malloc(packed_buffer_size));
if (internal->packed_buffer == nullptr) {
LOG_E(TAG, "Failed to allocate packed pixel buffer");
epd_deinit();
free(internal);
return ERROR_OUT_OF_MEMORY;
}
device_set_driver_data(device, internal);
LOG_I(TAG, "EPDiy initialized (%dx%d native, %dx%d rotated)", epd_width(), epd_height(), epd_rotated_display_width(), epd_rotated_display_height());
return ERROR_NONE;
}
static error_t stop(Device* device) {
auto* internal = static_cast<Papers3DisplayInternal*>(device_get_driver_data(device));
if (internal->powered) {
epd_poweroff();
internal->powered = false;
}
epd_deinit();
free(internal->packed_buffer);
free(internal);
device_set_driver_data(device, nullptr);
return ERROR_NONE;
}
// endregion
Driver papers3_display_driver = {
.name = "papers3-display",
.compatible = (const char*[]) { "m5stack,papers3-display", nullptr },
.start_device = start,
.stop_device = stop,
.api = &papers3_display_api,
.device_type = &DISPLAY_TYPE,
.owner = &m5stack_papers3_module,
.internal = nullptr
};
}
@@ -0,0 +1,19 @@
#pragma once
#include <stdint.h>
#include <epdiy.h>
#ifdef __cplusplus
extern "C" {
#endif
struct Papers3DisplayConfig {
int temperature_celsius;
enum EpdDrawMode draw_mode;
enum EpdRotation rotation;
};
#ifdef __cplusplus
}
#endif
@@ -0,0 +1,280 @@
// SPDX-License-Identifier: Apache-2.0
#include "papers3_power.h"
#include <tactility/check.h>
#include <tactility/driver.h>
#include <tactility/drivers/gpio_controller.h>
#include <tactility/drivers/power_supply.h>
#include <tactility/drivers/pwm.h>
#include <tactility/log.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <new>
#define TAG "Papers3Power"
#define GET_CONFIG(device) (static_cast<const Papers3PowerConfig*>((device)->config))
// Power-off signal timing, ported from the old deprecated-HAL PaperS3Power::powerOff().
static constexpr int POWER_OFF_PULSE_COUNT = 5;
static constexpr TickType_t POWER_OFF_PULSE_DURATION = pdMS_TO_TICKS(100);
static constexpr int BEEP_FREQUENCY_HZ = 440;
static constexpr TickType_t BEEP_DURATION = pdMS_TO_TICKS(200);
static constexpr TickType_t BEEP_GAP = pdMS_TO_TICKS(100);
extern "C" {
extern Module m5stack_papers3_module;
struct Papers3PowerInternal {
GpioDescriptor* charge_status_descriptor = nullptr;
GpioDescriptor* usb_detect_descriptor = nullptr;
GpioDescriptor* power_off_descriptor = nullptr;
Device* power_supply_device = nullptr;
};
error_t papers3_power_is_charging(Device* device, bool* charging) {
auto* internal = static_cast<Papers3PowerInternal*>(device_get_driver_data(device));
bool level;
error_t error = gpio_descriptor_get_level(internal->charge_status_descriptor, &level);
if (error != ERROR_NONE) {
return error;
}
// Charge IC status is active-low: 0 = charging, 1 = full/no USB.
*charging = !level;
return ERROR_NONE;
}
error_t papers3_power_is_usb_connected(Device* device, bool* connected) {
auto* internal = static_cast<Papers3PowerInternal*>(device_get_driver_data(device));
return gpio_descriptor_get_level(internal->usb_detect_descriptor, connected);
}
static void beep(Device* pwm, int frequency_hz, TickType_t duration) {
uint32_t period_ns = 1000000000U / static_cast<uint32_t>(frequency_hz);
if (pwm_set_period(pwm, period_ns) != ERROR_NONE ||
pwm_set_duty(pwm, period_ns / 2) != ERROR_NONE ||
pwm_enable(pwm) != ERROR_NONE) {
LOG_E(TAG, "Failed to start buzzer tone");
return;
}
vTaskDelay(duration);
pwm_disable(pwm);
}
error_t papers3_power_off(Device* device) {
LOG_W(TAG, "Power-off requested");
// Note: callers are responsible for stopping the display (e.g. EPD refresh) before calling
// this. The beep sequence below (~500ms) provides some lead time, but a full EPD refresh can
// take up to ~1500ms; the display recovers correctly on next boot via a full-screen clear.
auto* internal = static_cast<Papers3PowerInternal*>(device_get_driver_data(device));
const auto* config = GET_CONFIG(device);
beep(config->pwm, BEEP_FREQUENCY_HZ, BEEP_DURATION);
vTaskDelay(BEEP_GAP);
beep(config->pwm, BEEP_FREQUENCY_HZ, BEEP_DURATION);
LOG_W(TAG, "Triggering shutdown (sending %d pulses)...", POWER_OFF_PULSE_COUNT);
for (int i = 0; i < POWER_OFF_PULSE_COUNT; i++) {
gpio_descriptor_set_level(internal->power_off_descriptor, true);
vTaskDelay(POWER_OFF_PULSE_DURATION);
gpio_descriptor_set_level(internal->power_off_descriptor, false);
vTaskDelay(POWER_OFF_PULSE_DURATION);
}
gpio_descriptor_set_level(internal->power_off_descriptor, true); // Final high state
LOG_W(TAG, "Shutdown signal sent. Waiting for power-off...");
vTaskDelay(pdMS_TO_TICKS(1000));
LOG_E(TAG, "Device did not power off as expected");
return ERROR_NONE;
}
// region Power supply child device
static bool ps_supports_property(Device*, PowerSupplyProperty property) {
return property == POWER_SUPPLY_PROP_IS_CHARGING;
}
static error_t ps_get_property(Device* device, PowerSupplyProperty property, PowerSupplyPropertyValue* out_value) {
if (property != POWER_SUPPLY_PROP_IS_CHARGING) {
return ERROR_NOT_SUPPORTED;
}
// device_get_parent() here is the papers3-power device itself (this child's parent).
bool charging;
error_t error = papers3_power_is_charging(device_get_parent(device), &charging);
if (error != ERROR_NONE) {
return error;
}
out_value->int_value = charging ? 1 : 0;
return ERROR_NONE;
}
static bool ps_supports_charge_control(Device*) { return false; }
static bool ps_is_allowed_to_charge(Device*) { return false; }
static error_t ps_set_allowed_to_charge(Device*, bool) { return ERROR_NOT_SUPPORTED; }
static bool ps_supports_quick_charge(Device*) { return false; }
static bool ps_is_quick_charge_enabled(Device*) { return false; }
static error_t ps_set_quick_charge_enabled(Device*, bool) { return ERROR_NOT_SUPPORTED; }
static bool ps_supports_power_off(Device*) { return true; }
static error_t ps_power_off(Device* device) { return papers3_power_off(device_get_parent(device)); }
static constexpr PowerSupplyApi PAPERS3_POWER_SUPPLY_API = {
.supports_property = ps_supports_property,
.get_property = ps_get_property,
.supports_charge_control = ps_supports_charge_control,
.is_allowed_to_charge = ps_is_allowed_to_charge,
.set_allowed_to_charge = ps_set_allowed_to_charge,
.supports_quick_charge = ps_supports_quick_charge,
.is_quick_charge_enabled = ps_is_quick_charge_enabled,
.set_quick_charge_enabled = ps_set_quick_charge_enabled,
.supports_power_off = ps_supports_power_off,
.power_off = ps_power_off,
};
// Registered (driver_construct_add() in module.cpp) so driver_bind() has a valid ->internal, but
// never matched against a devicetree node: papers3_power_driver wires it up directly by pointer.
Driver papers3_power_supply_driver = {
.name = "papers3-power-supply",
.compatible = (const char*[]) { "papers3-power-supply", nullptr },
.start_device = nullptr,
.stop_device = nullptr,
.api = &PAPERS3_POWER_SUPPLY_API,
.device_type = &POWER_SUPPLY_TYPE,
.owner = &m5stack_papers3_module,
.internal = nullptr
};
static error_t create_power_supply_child(Device* parent, Device*& out_child) {
auto* child = new(std::nothrow) Device { .address = 0, .name = "papers3-power-supply", .config = nullptr, .parent = nullptr, .internal = nullptr };
if (child == nullptr) {
return ERROR_OUT_OF_MEMORY;
}
error_t error = device_construct(child);
if (error != ERROR_NONE) {
delete child;
return error;
}
device_set_parent(child, parent);
device_set_driver(child, &papers3_power_supply_driver);
error = device_add(child);
if (error != ERROR_NONE) {
device_destruct(child);
delete child;
return error;
}
error = device_start(child);
if (error != ERROR_NONE) {
device_remove(child);
device_destruct(child);
delete child;
return error;
}
out_child = child;
return ERROR_NONE;
}
static void destroy_power_supply_child(Device* child) {
check(device_stop(child) == ERROR_NONE);
check(device_remove(child) == ERROR_NONE);
check(device_destruct(child) == ERROR_NONE);
delete child;
}
// endregion
// region Driver lifecycle
static error_t acquire_input(const GpioPinSpec& pin, GpioDescriptor** out_descriptor) {
auto* descriptor = gpio_descriptor_acquire(pin.gpio_controller, pin.pin, GPIO_OWNER_GPIO);
if (descriptor == nullptr) {
return ERROR_RESOURCE;
}
error_t error = gpio_descriptor_set_flags(descriptor, pin.flags | GPIO_FLAG_DIRECTION_INPUT);
if (error != ERROR_NONE) {
gpio_descriptor_release(descriptor);
return error;
}
*out_descriptor = descriptor;
return ERROR_NONE;
}
static error_t start(Device* device) {
const auto* config = GET_CONFIG(device);
auto* internal = new(std::nothrow) Papers3PowerInternal();
if (internal == nullptr) {
return ERROR_OUT_OF_MEMORY;
}
if (acquire_input(config->pin_charge_status, &internal->charge_status_descriptor) != ERROR_NONE) {
LOG_E(TAG, "Failed to configure charge-status pin");
delete internal;
return ERROR_RESOURCE;
}
if (acquire_input(config->pin_usb_detect, &internal->usb_detect_descriptor) != ERROR_NONE) {
LOG_E(TAG, "Failed to configure usb-detect pin");
gpio_descriptor_release(internal->charge_status_descriptor);
delete internal;
return ERROR_RESOURCE;
}
internal->power_off_descriptor = gpio_descriptor_acquire(config->pin_power_off.gpio_controller, config->pin_power_off.pin, GPIO_OWNER_GPIO);
if (internal->power_off_descriptor == nullptr ||
gpio_descriptor_set_flags(internal->power_off_descriptor, config->pin_power_off.flags | GPIO_FLAG_DIRECTION_OUTPUT) != ERROR_NONE) {
LOG_E(TAG, "Failed to configure power-off pin");
if (internal->power_off_descriptor != nullptr) {
gpio_descriptor_release(internal->power_off_descriptor);
}
gpio_descriptor_release(internal->usb_detect_descriptor);
gpio_descriptor_release(internal->charge_status_descriptor);
delete internal;
return ERROR_RESOURCE;
}
gpio_descriptor_set_level(internal->power_off_descriptor, false);
error_t error = create_power_supply_child(device, internal->power_supply_device);
if (error != ERROR_NONE) {
gpio_descriptor_release(internal->power_off_descriptor);
gpio_descriptor_release(internal->usb_detect_descriptor);
gpio_descriptor_release(internal->charge_status_descriptor);
delete internal;
return error;
}
device_set_driver_data(device, internal);
return ERROR_NONE;
}
static error_t stop(Device* device) {
auto* internal = static_cast<Papers3PowerInternal*>(device_get_driver_data(device));
destroy_power_supply_child(internal->power_supply_device);
gpio_descriptor_release(internal->power_off_descriptor);
gpio_descriptor_release(internal->usb_detect_descriptor);
gpio_descriptor_release(internal->charge_status_descriptor);
device_set_driver_data(device, nullptr);
delete internal;
return ERROR_NONE;
}
// endregion
Driver papers3_power_driver = {
.name = "papers3-power",
.compatible = (const char*[]) { "m5stack,papers3-power", nullptr },
.start_device = start,
.stop_device = stop,
.api = nullptr,
.device_type = nullptr,
.owner = &m5stack_papers3_module,
.internal = nullptr
};
}
@@ -0,0 +1,37 @@
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
#include <stdbool.h>
#include <tactility/device.h>
#include <tactility/drivers/gpio.h>
#include <tactility/error.h>
struct Papers3PowerConfig {
struct GpioPinSpec pin_charge_status;
struct GpioPinSpec pin_usb_detect;
struct GpioPinSpec pin_power_off;
/** Tone generator used for the power-off confirmation beep */
struct Device* pwm;
};
/** Charge IC status: true while the battery is charging. */
error_t papers3_power_is_charging(struct Device* device, bool* charging);
/**
* @brief Whether USB VBUS is currently present.
* @warning Ported as-is from the old deprecated-HAL driver, which never actually wired this into
* its metrics and flagged it "TODO: Fix USB Detection" - the read itself hasn't been independently
* re-verified against real hardware here either, just carried forward with the same caveat.
*/
error_t papers3_power_is_usb_connected(struct Device* device, bool* connected);
/** Beeps twice, then pulses the shutdown pin (does not return on success). */
error_t papers3_power_off(struct Device* device);
#ifdef __cplusplus
}
#endif
+34
View File
@@ -0,0 +1,34 @@
#include <tactility/check.h>
#include <tactility/driver.h>
#include <tactility/error.h>
#include <tactility/module.h>
extern "C" {
extern Driver papers3_power_driver;
extern Driver papers3_power_supply_driver;
extern Driver papers3_display_driver;
static error_t start() {
check(driver_construct_add(&papers3_power_supply_driver) == ERROR_NONE);
check(driver_construct_add(&papers3_power_driver) == ERROR_NONE);
check(driver_construct_add(&papers3_display_driver) == ERROR_NONE);
return ERROR_NONE;
}
static error_t stop() {
check(driver_remove_destruct(&papers3_display_driver) == ERROR_NONE);
check(driver_remove_destruct(&papers3_power_driver) == ERROR_NONE);
check(driver_remove_destruct(&papers3_power_supply_driver) == ERROR_NONE);
return ERROR_NONE;
}
Module m5stack_papers3_module = {
.name = "m5stack-papers3",
.start = start,
.stop = stop,
.symbols = nullptr,
.internal = nullptr
};
}