Compare commits

..

13 Commits

Author SHA1 Message Date
Adolfo Reyna 213ad7ee61 Merge branch 'feature/rlcd-upstream-main' into feature/mcp-rlcd-custom 2026-07-18 22:56:43 -04:00
Adolfo Reyna 1a96307de2 feat(rlcd): Waveshare ESP32-S3-RLCD-4.2 support on latest main
- ST7305 driver with persistent 15KB DMA buffer (fix use-after-free +
  WDT lockup that caused freeze after flash)
- Reset timing fix: 150ms after reset, 50ms after init
- DTS: i2s0 mclk 16, bclk 9, ws 45, out 8, in 10 - fixes GPIO5 conflict
- i2c0 uses esp32-i2c-master, SDMMC 1-bit
- device.properties: colorDepth 16, fontSize 20, DefaultDark
- Amp GPIO46 init in initBoot
- Buttons: two-button control GPIO18 (cycle) + GPIO0 (select)

Base: origin/main f9453d89
2026-07-18 22:44:53 -04:00
Adolfo Reyna c7dc66e04b fix(audio): ES8311 BOTH complementary open + audio-stream close ref-count + mic unmute
- es8311 driver open() now allows OUTPUT->INPUT complementary (promotes to BOTH) when same native rate 44100
- audio-stream close_stream() ref-counts shared BOTH codec (don't close if other direction still open)
- MCP recordVoice + VoiceRecorder app explicitly unmute and set 100% gain

Fixes mic input not working - was returning ERROR_RESOURCE when output already open
2026-07-18 19:55:47 -04:00
Adolfo Reyna 15aad3f585 fix(files): launch mp3 player from file explorer - audio support
- SupportedFiles: add isSupportedAudioFile (.mp3/.wav/.ogg/.flac)
- Files View::viewFile: if audio file, loader->start one.tactility.mp3player with file bundle
- Tactility.cpp: hide mcpoverride (screensaver internal), keep mcpsettings visible
- Fixes MP3 no longer running from file explorer, plus audio fast playback already fixed via audio-stream (Mp3Player/BookPlayer/VoiceRecorder use audio_stream_open_output not direct i2s_controller_write)
- MCP dev coexistence already fixed: DisplayIdle lock inversion 3629ffef, WebServer serverEnabled=ws||mcp, unauth /api/mcp, video 8081/8083 vs dev 6666
2026-07-18 19:18:28 -04:00
Ken Van Hoeylandt f9453d8956 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`
2026-07-19 00:39:26 +02:00
Adolfo Reyna 9a307e522d fix(mcp): use audio-stream instead of direct I2S - fixes fast mp3/voice playback
- old optimization bypassed audio-stream resampler (16k direct -> MCLK 4.096MHz vs 11.29MHz native)
- brick game correct because it used audio_stream path
- now MCP uses audio_stream_open_output/input + read/write matching native logger stack
- falls back to direct I2S only if audio-stream device missing
2026-07-18 18:30:19 -04:00
Adolfo Reyna c3abe79c90 fix(mcp): register McpSettings + McpOverride - Settings shows MCP Screen 2026-07-18 18:19:34 -04:00
Adolfo Reyna 05ce4335ff fix(mcp): register McpSettings+McpOverride in Tactility.cpp - show in Settings 2026-07-18 18:12:08 -04:00
Adolfo Reyna ec2a6c0b88 fix(es3c28p): MCP flash - PR#569 CYD+explicit srcs+no HAL, 512K cache, screensaver
- vendor CYD, useDeprecatedHal=false, CMake explicit SRCS TactilityKernel only
- module.cpp without HAL, retains es3c28p_module symbol for devicetree
- LVGL 512K PSRAM cache retained
- MCP: McpScreensaver full-screen on lv_layer_top, tap to close, triggered via ensureOverrideScreen()
- flashed to /dev/cu.usbmodem101 2884320 -> 1672312 hash verified
2026-07-18 18:09:08 -04:00
Adolfo Reyna 0966cd45a4 fix(es3c28p): PR#569 feedback + retain 512K cache - CYD vendor, explicit srcs, no HAL 2026-07-18 18:04:25 -04:00
Adolfo Reyna e3eb3fd415 feat(mcp): restore MCP system with native log.h
- MCP 3.3K LOC: McpSystem 1899 + McpHandler 1029 + McpScreensaver + 2 apps
- Uses native tactility/log.h TAG macros, no old Logger.h
- DisplaySettings McpScreen enum, WebServer coexistence (MCP enabled starts HTTP)
- DisplayIdle: startMcpScreensaver + isDeviceCharging + lock inversion fix 3629ffef
- LVGL 512K PSRAM cache retained from previous perf patch
- Audio kept upstream es8311-module per note we might not need custom
2026-07-18 17:29:37 -04:00
Adolfo Reyna c4adaef0a5 perf(es3c28p): LVGL 512KB PSRAM cache + display charging toggle
- LVGL image cache 512KB + header cache 16 on PSRAM (was PNG bottleneck, c4ec7ead)
- DisplaySettings.disableScreensaverWhenCharging persisted, checked via PowerDevice::IsCharging
- Display app: 'Disable on charging' toggle under Display settings, auto-disabled when timeout off
- DisplayIdle: skip screensaver while charging, auto-stop if plugging in while dimmed
- Keeps upstream full-DTS board clean, no JPEG decoder, no BT HID combo per user
2026-07-18 17:16:18 -04:00
Adolfo Reyna 057daf49a5 perf(es3c28p): enable 512KB LVGL image cache on PSRAM
CONFIG_LV_CACHE_DEF_SIZE=524288
CONFIG_LV_IMAGE_HEADER_CACHE_DEF_CNT=16
From c4ec7ead perf fix — skip JPEG decoder per user, skip BT HID combo. OCT PSRAM 120M reference.

Build verified IDF 5.5.2 Tactility.bin 0x2b1f20 same as before but with cache enabled at runtime.
2026-07-18 16:52:01 -04:00
154 changed files with 9890 additions and 4153 deletions
+2 -4
View File
@@ -1,6 +1,4 @@
file(GLOB_RECURSE SOURCE_FILES source/*.c*)
idf_component_register(
SRCS ${SOURCE_FILES}
REQUIRES TactilityKernel Tactility
SRCS "source/module.cpp"
REQUIRES TactilityKernel
)
+6 -1
View File
@@ -1,4 +1,4 @@
general.vendor=LCDWIKI
general.vendor=CYD
general.name=ES3C28P
apps.launcherAppId=Launcher
@@ -19,3 +19,8 @@ display.dpi=143
lvgl.colorDepth=16
storage.userDataLocation=SD
dependencies.useDeprecatedHal=false
sdkconfig.CONFIG_LV_CACHE_DEF_SIZE=524288
sdkconfig.CONFIG_LV_IMAGE_HEADER_CACHE_DEF_CNT=16
-4
View File
@@ -1,8 +1,4 @@
#include <tactility/module.h>
#include <Tactility/hal/Configuration.h>
// Legacy placeholder (required until legacy HAL is cleaned up everywhere)
extern const tt::hal::Configuration hardwareConfiguration = {};
extern "C" {
@@ -1,6 +0,0 @@
file(GLOB_RECURSE SOURCE_FILES source/*.c*)
idf_component_register(
SRCS ${SOURCE_FILES}
REQUIRES TactilityKernel driver
)
@@ -1,6 +0,0 @@
dependencies:
- Platforms/platform-esp32
- Drivers/dummy-i2s-amp-module
- Drivers/audio-stream-module
- Drivers/axs15231b-module
dts: guition,jc3248w535c.dts
@@ -1,182 +0,0 @@
/dts-v1/;
#include <tactility/bindings/root.h>
#include <tactility/bindings/esp32_ble.h>
#include <tactility/bindings/esp32_wifi_pinned.h>
#include <tactility/bindings/esp32_gpio.h>
#include <tactility/bindings/esp32_i2c.h>
#include <tactility/bindings/esp32_i2s.h>
#include <tactility/bindings/esp32_spi.h>
#include <tactility/bindings/esp32_uart.h>
#include <tactility/bindings/esp32_sdspi.h>
#include <tactility/bindings/esp32_pwm_ledc.h>
#include <tactility/bindings/pwm_backlight.h>
#include <bindings/dummy_i2s_amp.h>
#include <bindings/axs15231b_display.h>
#include <bindings/axs15231b_touch.h>
/ {
compatible = "root";
model = "Guition JC3248W535C";
wifi0 {
compatible = "espressif,esp32-wifi-pinned";
status = "disabled";
};
ble0 {
compatible = "espressif,esp32-ble";
status = "disabled";
};
gpio0 {
compatible = "espressif,esp32-gpio";
gpio-count = <49>;
};
i2c_external: i2c1 {
compatible = "espressif,esp32-i2c";
port = <I2C_NUM_1>;
clock-frequency = <400000>;
pin-sda = <&gpio0 17 GPIO_FLAG_NONE>;
pin-scl = <&gpio0 18 GPIO_FLAG_NONE>;
};
i2s0 {
compatible = "espressif,esp32-i2s";
port = <I2S_NUM_0>;
pin-bclk = <&gpio0 42 GPIO_FLAG_NONE>;
pin-ws = <&gpio0 2 GPIO_FLAG_NONE>;
pin-data-out = <&gpio0 41 GPIO_FLAG_NONE>;
};
speaker0 {
compatible = "nsiway,ns4168";
i2s = <&i2s0>;
};
display_backlight_pwm {
compatible = "espressif,esp32-pwm-ledc";
pin = <&gpio0 1 GPIO_FLAG_NONE>;
period-ns = <25000>;
ledc-timer = <0>;
ledc-channel = <0>;
// Matches the deprecated HAL's old PwmBacklight driver, which hardcoded 8-bit duty
// resolution (the binding's own default is 10-bit).
duty-resolution = <8>;
};
display_backlight {
compatible = "pwm-backlight";
// Off by default so display power-on won't show the screen from before the last power loss.
// The display backlight is turned on during the boot process.
status = "disabled";
pwm = <&display_backlight_pwm>;
};
spi0 {
compatible = "espressif,esp32-spi";
host = <SPI2_HOST>;
cs-gpios = <&gpio0 45 GPIO_FLAG_NONE>;
pin-mosi = <&gpio0 21 GPIO_FLAG_NONE>;
pin-miso = <&gpio0 48 GPIO_FLAG_NONE>;
pin-sclk = <&gpio0 47 GPIO_FLAG_NONE>;
pin-wp = <&gpio0 40 GPIO_FLAG_NONE>;
pin-hd = <&gpio0 39 GPIO_FLAG_NONE>;
display@0 {
compatible = "axs,axs15231b";
horizontal-resolution = <320>;
vertical-resolution = <480>;
pin-te = <&gpio0 38 GPIO_FLAG_NONE>;
backlight = <&display_backlight>;
// Confirmed on real hardware: a sub-region draw desyncs this panel's row
// auto-increment counter (no QSPI row-address command exists to resync it),
// producing a sheared/doubled image - see requires-full-frame's binding doc.
requires-full-frame;
// Vendor bring-up sequence, carried over unchanged from the deprecated HAL's old
// Axs15231bDisplay.cpp. Framing is [cmd, data-length, delay-ms, data-length bytes of
// data...] - see axs15231b-module's init-sequence binding property for the encoding.
init-sequence = [
0xBB 8 0 0x00 0x00 0x00 0x00 0x00 0x00 0x5A 0xA5
0xA0 17 0 0xC0 0x10 0x00 0x02 0x00 0x00 0x04 0x3F 0x20 0x05 0x3F 0x3F 0x00 0x00 0x00 0x00 0x00
0xA2 31 0 0x30 0x3C 0x24 0x14 0xD0 0x20 0xFF 0xE0 0x40 0x19 0x80 0x80 0x80 0x20 0xf9 0x10 0x02 0xff 0xff 0xF0 0x90 0x01 0x32 0xA0 0x91 0xE0 0x20 0x7F 0xFF 0x00 0x5A
0xD0 30 0 0xE0 0x40 0x51 0x24 0x08 0x05 0x10 0x01 0x20 0x15 0x42 0xC2 0x22 0x22 0xAA 0x03 0x10 0x12 0x60 0x14 0x1E 0x51 0x15 0x00 0x8A 0x20 0x00 0x03 0x3A 0x12
0xA3 22 0 0xA0 0x06 0xAa 0x00 0x08 0x02 0x0A 0x04 0x04 0x04 0x04 0x04 0x04 0x04 0x04 0x04 0x04 0x04 0x04 0x00 0x55 0x55
0xC1 30 0 0x31 0x04 0x02 0x02 0x71 0x05 0x24 0x55 0x02 0x00 0x41 0x00 0x53 0xFF 0xFF 0xFF 0x4F 0x52 0x00 0x4F 0x52 0x00 0x45 0x3B 0x0B 0x02 0x0d 0x00 0xFF 0x40
0xC3 11 0 0x00 0x00 0x00 0x50 0x03 0x00 0x00 0x00 0x01 0x80 0x01
0xC4 29 0 0x00 0x24 0x33 0x80 0x00 0xea 0x64 0x32 0xC8 0x64 0xC8 0x32 0x90 0x90 0x11 0x06 0xDC 0xFA 0x00 0x00 0x80 0xFE 0x10 0x10 0x00 0x0A 0x0A 0x44 0x50
0xC5 23 0 0x18 0x00 0x00 0x03 0xFE 0x3A 0x4A 0x20 0x30 0x10 0x88 0xDE 0x0D 0x08 0x0F 0x0F 0x01 0x3A 0x4A 0x20 0x10 0x10 0x00
0xC6 20 0 0x05 0x0A 0x05 0x0A 0x00 0xE0 0x2E 0x0B 0x12 0x22 0x12 0x22 0x01 0x03 0x00 0x3F 0x6A 0x18 0xC8 0x22
0xC7 20 0 0x50 0x32 0x28 0x00 0xa2 0x80 0x8f 0x00 0x80 0xff 0x07 0x11 0x9c 0x67 0xff 0x24 0x0c 0x0d 0x0e 0x0f
0xC9 4 0 0x33 0x44 0x44 0x01
0xCF 27 0 0x2C 0x1E 0x88 0x58 0x13 0x18 0x56 0x18 0x1E 0x68 0x88 0x00 0x65 0x09 0x22 0xC4 0x0C 0x77 0x22 0x44 0xAA 0x55 0x08 0x08 0x12 0xA0 0x08
0xD5 30 0 0x40 0x8E 0x8D 0x01 0x35 0x04 0x92 0x74 0x04 0x92 0x74 0x04 0x08 0x6A 0x04 0x46 0x03 0x03 0x03 0x03 0x82 0x01 0x03 0x00 0xE0 0x51 0xA1 0x00 0x00 0x00
0xD6 30 0 0x10 0x32 0x54 0x76 0x98 0xBA 0xDC 0xFE 0x93 0x00 0x01 0x83 0x07 0x07 0x00 0x07 0x07 0x00 0x03 0x03 0x03 0x03 0x03 0x03 0x00 0x84 0x00 0x20 0x01 0x00
0xD7 19 0 0x03 0x01 0x0b 0x09 0x0f 0x0d 0x1E 0x1F 0x18 0x1d 0x1f 0x19 0x40 0x8E 0x04 0x00 0x20 0xA0 0x1F
0xD8 12 0 0x02 0x00 0x0a 0x08 0x0e 0x0c 0x1E 0x1F 0x18 0x1d 0x1f 0x19
0xD9 12 0 0x1F 0x1F 0x1F 0x1F 0x1F 0x1F 0x1F 0x1F 0x1F 0x1F 0x1F 0x1F
0xDD 12 0 0x1F 0x1F 0x1F 0x1F 0x1F 0x1F 0x1F 0x1F 0x1F 0x1F 0x1F 0x1F
0xDF 8 0 0x44 0x73 0x4B 0x69 0x00 0x0A 0x02 0x90
0xE0 17 0 0x3B 0x28 0x10 0x16 0x0c 0x06 0x11 0x28 0x5c 0x21 0x0D 0x35 0x13 0x2C 0x33 0x28 0x0D
0xE1 17 0 0x37 0x28 0x10 0x16 0x0b 0x06 0x11 0x28 0x5C 0x21 0x0D 0x35 0x14 0x2C 0x33 0x28 0x0F
0xE2 17 0 0x3B 0x07 0x12 0x18 0x0E 0x0D 0x17 0x35 0x44 0x32 0x0C 0x14 0x14 0x36 0x3A 0x2F 0x0D
0xE3 17 0 0x37 0x07 0x12 0x18 0x0E 0x0D 0x17 0x35 0x44 0x32 0x0C 0x14 0x14 0x36 0x32 0x2F 0x0F
0xE4 17 0 0x3B 0x07 0x12 0x18 0x0E 0x0D 0x17 0x39 0x44 0x2E 0x0C 0x14 0x14 0x36 0x3A 0x2F 0x0D
0xE5 17 0 0x37 0x07 0x12 0x18 0x0E 0x0D 0x17 0x39 0x44 0x2E 0x0C 0x14 0x14 0x36 0x3A 0x2F 0x0F
0xA4 16 0 0x85 0x85 0x95 0x82 0xAF 0xAA 0xAA 0x80 0x10 0x30 0x40 0x40 0x20 0xFF 0x60 0x30
0xA4 4 0 0x85 0x85 0x95 0x85
0xBB 8 0 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
0x13 0 0
0x35 1 0 0x00
0x11 0 120
0x2C 4 0 0x00 0x00 0x00 0x00
];
};
};
// Declared (and thus started) after display@0 above, not before: the display and touch
// sides of this chip share the same silicon, and the deprecated HAL's old Hal.cpp always
// started the display first, then the touch device second. Starting touch first (as an
// earlier version of this devicetree did) let touch's I2C handshake succeed initially, but
// the display's own reset/init sequence running afterward then reset the whole chip and
// left the touch interface unresponsive - confirmed on real hardware (I2C writes started
// failing consistently right after display bring-up completed).
i2c_internal: i2c0 {
compatible = "espressif,esp32-i2c";
port = <I2C_NUM_0>;
clock-frequency = <400000>;
pin-sda = <&gpio0 4 GPIO_FLAG_NONE>;
pin-scl = <&gpio0 8 GPIO_FLAG_NONE>;
touch {
compatible = "axs,axs15231b-touch";
reg = <0x3B>;
x-max = <320>;
y-max = <480>;
};
};
spi1 {
compatible = "espressif,esp32-spi";
host = <SPI3_HOST>;
cs-gpios = <&gpio0 10 GPIO_FLAG_NONE>;
pin-mosi = <&gpio0 11 GPIO_FLAG_NONE>;
pin-miso = <&gpio0 13 GPIO_FLAG_NONE>;
pin-sclk = <&gpio0 12 GPIO_FLAG_NONE>;
sdcard@0 {
compatible = "espressif,esp32-sdspi";
frequency-khz = <20000>;
};
};
// P1 header
uart0 {
compatible = "espressif,esp32-uart";
port = <UART_NUM_0>;
pin-tx = <&gpio0 43 GPIO_FLAG_NONE>;
pin-rx = <&gpio0 44 GPIO_FLAG_NONE>;
};
};
@@ -39,9 +39,6 @@
channels = <ADC_CHANNEL_3 ADC_ATTEN_DB_12 ADC_BITWIDTH_DEFAULT>;
};
// Matches the deprecated HAL's old Power.cpp (EstimatedPower/ChargeFromAdcVoltage default
// config): ADC1 CH3, 2:1 divider with +0.11 display voltage sag compensation
// (adcMultiplier = 2.11, so multiplier = 2110).
battery-sense {
compatible = "battery-sense";
io-channel = <&adc0 0>;
+2 -3
View File
@@ -1,7 +1,6 @@
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 esp_lcd AXP192 ILI934x FT6x36 driver vfs fatfs
REQUIRES TactilityKernel axp192-module
)
@@ -1,22 +0,0 @@
#include "devices/Display.h"
#include "devices/Power.h"
#include <Tactility/hal/Configuration.h>
using namespace tt::hal;
static bool initBoot() {
return initAxp();
}
static DeviceVector createDevices() {
return {
getAxp192(),
createDisplay()
};
}
extern const Configuration hardwareConfiguration = {
.initBoot = initBoot,
.createDevices = createDevices
};
@@ -1,48 +0,0 @@
#include "Display.h"
#include <Ft6x36Touch.h>
#include <Ili934xDisplay.h>
std::shared_ptr<tt::hal::touch::TouchDevice> createTouch() {
auto configuration = std::make_unique<Ft6x36Touch::Configuration>(
I2C_NUM_0,
LCD_HORIZONTAL_RESOLUTION,
LCD_VERTICAL_RESOLUTION,
false,
false,
false,
GPIO_NUM_NC,
GPIO_NUM_39
);
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 = nullptr,
.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,16 +0,0 @@
#pragma once
#include <Tactility/hal/display/DisplayDevice.h>
#include <driver/gpio.h>
#include <driver/spi_common.h>
#include <memory>
constexpr auto LCD_SPI_HOST = SPI2_HOST;
constexpr auto LCD_PIN_CS = GPIO_NUM_5;
constexpr auto LCD_PIN_DC = GPIO_NUM_15;
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;
std::shared_ptr<tt::hal::display::DisplayDevice> createDisplay();
@@ -1,36 +0,0 @@
#include <Axp192.h>
#include <tactility/device.h>
static std::shared_ptr<Axp192> axp192 = nullptr;
std::shared_ptr<Axp192> createAxp192() {
assert(axp192 == nullptr);
auto configuration = std::make_unique<Axp192::Configuration>(device_find_by_name("i2c_internal"));
axp192 = std::make_shared<Axp192>(std::move(configuration));
return axp192;
}
std::shared_ptr<Axp192> getAxp192() {
assert(axp192 != nullptr);
return axp192;
}
bool initAxp() {
const auto axp = createAxp192();
return axp->init([](auto* driver) {
axp192_ioctl(driver, AXP192_LDO2_SET_VOLTAGE, 3300); // LCD + SD
axp192_ioctl(driver, AXP192_LDO3_SET_VOLTAGE, 0); // VIB_MOTOR STOP
axp192_ioctl(driver, AXP192_DCDC3_SET_VOLTAGE, 3300);
axp192_ioctl(driver, AXP192_LDO2_ENABLE);
axp192_ioctl(driver, AXP192_LDO3_DISABLE);
axp192_ioctl(driver, AXP192_DCDC3_ENABLE);
axp192_write(driver, AXP192_PWM1_DUTY_CYCLE_2, 255); // PWM 255 (LED OFF)
axp192_write(driver, AXP192_GPIO1_CONTROL, 0x02); // GPIO1 PWM
// TODO: We could charge at 390mA according to the M5Unified code, but the AXP driver in M5Unified limits to 132mA, so it's unclear what the AXP supports.
return true;
});
}
@@ -1,9 +0,0 @@
#pragma once
#include <Axp192.h>
bool initAxp();
// Must call initAxp() first before this returns a non-nullptr response
std::shared_ptr<Axp192> getAxp192();
-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_core2_module = {
.name = "m5stack-core2",
.start = start,
.stop = stop,
.symbols = nullptr,
.internal = nullptr
};
}
+2
View File
@@ -12,6 +12,8 @@ hardware.esptoolFlashFreq=80M
storage.userDataLocation=SD
dependencies.useDeprecatedHal=false
display.size=2"
display.shape=rectangle
display.dpi=200
+3
View File
@@ -2,4 +2,7 @@ dependencies:
- Platforms/platform-esp32
- Drivers/mpu6886-module
- Drivers/bm8563-module
- Drivers/axp192-module
- Drivers/ft6x36-module
- Drivers/ili9341-module
dts: m5stack,core2.dts
+23 -3
View File
@@ -10,8 +10,10 @@
#include <tactility/bindings/esp32_uart.h>
#include <bindings/mpu6886.h>
#include <bindings/bm8563.h>
#include <bindings/axp192.h>
#include <bindings/ft6x36.h>
#include <bindings/ili9341.h>
#include <tactility/bindings/esp32_sdspi.h>
#include <tactility/bindings/display_placeholder.h>
// Reference: https://docs.m5stack.com/en/core/Core2
/ {
@@ -44,6 +46,19 @@
compatible = "belling,bm8563";
reg = <0x51>;
};
axp192 {
compatible = "x-powers,axp192";
reg = <0x34>;
};
touch {
compatible = "focaltech,ft6x36";
reg = <0x38>;
x-max = <320>;
y-max = <240>;
pin-interrupt = <&gpio0 39 GPIO_FLAG_NONE>;
};
};
port_a: grove0 {
@@ -66,12 +81,17 @@
pin-sclk = <&gpio0 18 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 15 GPIO_FLAG_NONE>;
};
sdcard@1 {
compatible = "espressif,esp32-sdspi";
status = "disabled"; // Must be started after display
frequency-khz = <20000>;
};
};
+49
View File
@@ -0,0 +1,49 @@
#include <drivers/axp192.h>
#include <tactility/check.h>
#include <tactility/device.h>
#include <tactility/device_listener.h>
#include <tactility/module.h>
#include <cstring>
extern "C" {
static void configure_axp192(Device* axp192) {
check(axp192_set_rail_voltage(axp192, AXP192_RAIL_LDO2, 3300) == ERROR_NONE); // LCD + SD
check(axp192_set_rail_voltage(axp192, AXP192_RAIL_DCDC3, 3300) == ERROR_NONE); // LCD backlight
check(axp192_set_rail_enabled(axp192, AXP192_RAIL_LDO2, true) == ERROR_NONE);
check(axp192_set_rail_enabled(axp192, AXP192_RAIL_LDO3, false) == ERROR_NONE); // VIB_MOTOR stop
check(axp192_set_rail_enabled(axp192, AXP192_RAIL_DCDC3, true) == ERROR_NONE);
check(axp192_set_pwm1_duty_cycle(axp192, 255) == ERROR_NONE); // PWM 255 (LED off)
check(axp192_set_gpio1_pwm1_output(axp192) == ERROR_NONE); // GPIO1: PWM
}
static void on_device_event(Device* device, DeviceEvent event, void* context) {
(void)context;
if (event == DEVICE_EVENT_STARTED && strcmp(device->name, "axp192") == 0) {
configure_axp192(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;
}
struct Module m5stack_core2_module = {
.name = "m5stack-core2",
.start = start,
.stop = stop,
.symbols = nullptr,
.internal = nullptr
};
}
+2 -3
View File
@@ -1,7 +1,6 @@
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 FT5x06 AXP2101 AW9523 driver vfs fatfs
REQUIRES TactilityKernel axp2101-module
)
@@ -1,21 +0,0 @@
#include "InitBoot.h"
#include "devices/Display.h"
#include <Tactility/hal/Configuration.h>
#include <Axp2101Power.h>
using namespace tt::hal;
static DeviceVector createDevices() {
return {
axp2101,
aw9523,
std::make_shared<Axp2101Power>(axp2101),
createDisplay()
};
}
extern const Configuration hardwareConfiguration = {
.initBoot = initBoot,
.createDevices = createDevices
};
-154
View File
@@ -1,154 +0,0 @@
#include "InitBoot.h"
#include <Tactility/kernel/Kernel.h>
#include <tactility/log.h>
constexpr auto* TAG = "CoreS3";
std::shared_ptr<Axp2101> axp2101;
std::shared_ptr<Aw9523> aw9523;
/**
* For details see https://github.com/espressif/esp-bsp/blob/master/bsp/m5stack_core_s3/m5stack_core_s3.c
* and schematic: https://m5stack.oss-cn-shenzhen.aliyuncs.com/resource/docs/datasheet/core/K128%20CoreS3/Sch_M5_CoreS3_v1.0.pdf
*/
bool initGpioExpander() {
LOG_I(TAG, "AW9523 init");
/**
* P0 pins:
* 0: Touch reset
* 1: Bus out enable
* 2: AW88298 reset (I2S)
* 3: ES7210 interrupt (Audio ADC)
* 4: SD Card SW(itch on?)
* 5: USB OTG enable
* 6: /
* 7: /
*/
/**
* P1 pins:
* 0: Cam reset
* 1: LCD reset
* 2: Touch interrupt
* 3: AW88298 interrupt (I2S)
* 4: /
* 5: /
* 6: /
* 7: Boost enable
*/
uint8_t p0_state = 0U;
uint8_t p1_state = 0U;
// Enable touch
p0_state |= (1U);
// Bus out enable
p0_state |= (1U << 1U);
// I2S
p0_state |= (1U << 2U);
// SD card
p0_state |= (1U << 4U);
// Enable LCD
p1_state |= (1U << 1U);
// Boost enable
p1_state |= (1U << 7U);
/* AW9523 P0 is in push-pull mode */
if (!aw9523->writeCTL(0x10)) {
LOG_E(TAG, "AW9523: Failed to set CTL");
return false;
}
if (!aw9523->writeP0(p0_state)) {
LOG_E(TAG, "AW9523: Failed to set P0");
return false;
}
if (!aw9523->writeP1(p1_state)) {
LOG_E(TAG, "AW9523: Failed to set P1");
return false;
}
if (axp2101->isVBus()) {
float voltage = 0.0f;
axp2101->getVBusVoltage(voltage);
LOG_I(TAG, "AXP2101: VBus at %.2f", voltage);
} else {
LOG_W(TAG, "AXP2101: VBus disabled");
}
return true;
}
bool initPowerControl() {
LOG_I(TAG, "Init power control (AXP2101)");
// Source: https://github.com/m5stack/M5Unified/blob/b8cfec7fed046242da7f7b8024a4e92004a51ff7/src/utility/Power_Class.cpp#L61
aw9523->bitOnP1(0b10000000); // SY7088 boost enable
/** AXP2101 usage
Source: https://github.com/m5stack/M5Unified/blob/b8cfec7fed046242da7f7b8024a4e92004a51ff7/README.md?plain=1#L223
| |M5Stack<BR>CoreS3<BR>CoreS3SE| |
|:---------:|:-----------------:|:---------:|
| ALDO1 |VDD 1v8 | ALDO1 |
| ALDO2 |VDDA 3v3 | ALDO2 |
| ALDO3 |CAM 3v3 | ALDO3 |
| ALDO4 |TF 3v3 | ALDO4 |
| BLDO1 |AVDD | BLDO1 |
| BLDO2 |DVDD | BLDO2 |
| DLDO1/DC1 |LCD BL | DLDO1/DC1 |
| DLDO2/DC2 | --- | DLDO2/DC2 |
| BACKUP |RTC BAT | BACKUP |
*/
/**
* 0x92 = ALD01
* 0x93 = ALD02
* 0x94 = ALD03
* 0x95 = ALD04
* 0x96 = BLD01
* 0x97 = BLD02
*
* DCDC1 : 0.7-3.5V 25mV/step 1200mA
* DCDC2 : 0.7-2.275V25mV/step 1600mA
* DCDC3 : 0.7-3.5V 25mV/step 700mA
* LDOio0: 1.8-3.3V, 100mV/step 50mA
* LDO1 : 30mA always on
* LDO2 : 1.8-3.3V 100mV/step 200mA
* LDO3 : 1.8-3.3V 100mV/step 200mA
*/
// Source: https://github.com/m5stack/M5Unified/blob/b8cfec7fed046242da7f7b8024a4e92004a51ff7/src/utility/Power_Class.cpp#L64
static constexpr uint8_t reg_data_array[] = {
0x90U, 0xBFU, // LDOS ON/OFF control 0 (backlight)
0x92U, 18U -5U, // ALDO1 set to 1.8v // for AW88298
0x93U, 33U -5U, // ALDO2 set to 3.3v // for ES7210
0x94U, 33U -5U, // ALDO3 set to 3.3v // for camera
0x95U, 33U -5U, // ALDO3 set to 3.3v // for TF card slot
0x27, 0x00, // PowerKey Hold=1sec / PowerOff=4sec
0x69, 0x11, // CHGLED setting
0x10, 0x30, // PMU common config
0x30, 0x0F // ADC enabled (for voltage measurement)
};
if (axp2101->setRegisters((uint8_t*)reg_data_array, sizeof(reg_data_array))) {
LOG_I(TAG, "AXP2101 initialized with %d registers", (int)(sizeof(reg_data_array) / 2));
return true;
} else {
LOG_E(TAG, "AXP2101: Failed to set registers");
return false;
}
}
bool initBoot() {
LOG_I(TAG, "initBoot()");
auto controller = device_find_by_name("i2c_internal");
axp2101 = std::make_shared<Axp2101>(controller);
aw9523 = std::make_shared<Aw9523>(controller);
return initPowerControl() && initGpioExpander();
}
-9
View File
@@ -1,9 +0,0 @@
#pragma once
#include <Axp2101.h>
#include <Aw9523.h>
extern std::shared_ptr<Axp2101> axp2101;
extern std::shared_ptr<Aw9523> aw9523;
bool initBoot();
@@ -1,59 +0,0 @@
#include "Display.h"
#include <Axp2101.h>
#include <Ft5x06Touch.h>
#include <Ili934xDisplay.h>
#include <tactility/check.h>
#include <tactility/log.h>
constexpr auto* TAG = "CoreS3Display";
static void setBacklightDuty(uint8_t backlightDuty) {
const uint8_t voltage = 20 + ((8 * backlightDuty) / 255); // [0b00000, 0b11100] - under 20 is too dark
// TODO: Refactor to use Axp2102 driver subproject. Reference: https://github.com/m5stack/M5Unified/blob/b8cfec7fed046242da7f7b8024a4e92004a51ff7/src/utility/AXP2101_Class.cpp#L42
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<Ft5x06Touch::Configuration>(
I2C_NUM_0,
319,//LCD_HORIZONTAL_RESOLUTION,
239//LCD_VERTICAL_RESOLUTION,
);
return std::make_shared<Ft5x06Touch>(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();
-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_cores3_module = {
.name = "m5stack-cores3",
.start = start,
.stop = stop,
.symbols = nullptr,
.internal = nullptr
};
}
+3 -1
View File
@@ -12,7 +12,9 @@ hardware.tinyUsb=true
hardware.esptoolFlashFreq=120M
hardware.bluetooth=true
storage.userDataLocation=SD
storage.userDataLocation=Internal
dependencies.useDeprecatedHal=false
display.size=2"
display.shape=rectangle
+4
View File
@@ -2,4 +2,8 @@ dependencies:
- Platforms/platform-esp32
- Drivers/bmi270-module
- Drivers/bm8563-module
- Drivers/axp2101-module
- Drivers/aw9523b-module
- Drivers/ft5x06-module
- Drivers/ili9341-module
dts: m5stack,cores3.dts
+72 -8
View File
@@ -11,8 +11,16 @@
#include <tactility/bindings/esp32_uart.h>
#include <bindings/bmi270.h>
#include <bindings/bm8563.h>
#include <bindings/axp2101.h>
#include <bindings/axp2101_backlight.h>
#include <bindings/aw9523b.h>
#include <bindings/ft5x06.h>
#include <bindings/ili9341.h>
#include <tactility/bindings/esp32_sdspi.h>
#include <tactility/bindings/display_placeholder.h>
#include <tactility/bindings/gpio_hog.h>
// SD card is not supported because of a hardware problem:
// The display uses pin 35 as input (MISO), but the SPI bus (and SD card) uses the same pin as DC (output).
// Reference: https://docs.m5stack.com/en/core/CoreS3
/ {
@@ -50,6 +58,61 @@
compatible = "belling,bm8563";
reg = <0x51>;
};
// 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 {
compatible = "awinic,aw9523b";
reg = <0x58>;
};
axp2101: axp2101 {
compatible = "x-powers,axp2101";
reg = <0x34>;
// LCD backlight (DLDO1). Raw register codes [20,28] map to [2500,3300]mV;
// below 2500mV the backlight was considered "too dark" to be useful.
backlight {
compatible = "axp2101-backlight";
ldo = <AXP2101_DLDO1>;
min-millivolt = <2500>;
max-millivolt = <3300>;
};
};
touch {
compatible = "focaltech,ft5x06";
reg = <0x38>;
x-max = <320>;
y-max = <240>;
pin-reset = <&aw9523b 0 GPIO_FLAG_NONE>;
};
};
// 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_aw88298_reset {
compatible = "gpio-hog";
pin = <&aw9523b 2 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>;
};
port_a: grove0 {
@@ -92,13 +155,14 @@
pin-sclk = <&gpio0 36 GPIO_FLAG_NONE>;
display@0 {
compatible = "display-placeholder";
};
sdcard@1 {
compatible = "espressif,esp32-sdspi";
status = "disabled"; // Must be started after display
frequency-khz = <20000>;
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>;
};
};
+55
View File
@@ -0,0 +1,55 @@
#include <drivers/axp2101.h>
#include <tactility/check.h>
#include <tactility/device.h>
#include <tactility/device_listener.h>
#include <tactility/module.h>
#include <cstring>
extern "C" {
// M5Stack CoreS3 AXP2101 rail setup, ported from the board's old deprecated-HAL
// initPowerControl() (source: M5Unified's Power_Class.cpp). ALDO1 powers the AW88298 audio
// amp, ALDO2 the ES7210 microphone ADC, ALDO3 the GC0308 camera, ALDO4 the TF/SD card slot.
// DLDO1 (LCD backlight) is left to the axp2101-backlight child device.
static void configure_axp2101(Device* axp2101) {
check(axp2101_set_ldo_voltage(axp2101, AXP2101_ALDO1, 1800) == ERROR_NONE);
check(axp2101_set_ldo_voltage(axp2101, AXP2101_ALDO2, 3300) == ERROR_NONE);
check(axp2101_set_ldo_voltage(axp2101, AXP2101_ALDO3, 3300) == ERROR_NONE);
check(axp2101_set_ldo_voltage(axp2101, AXP2101_ALDO4, 3300) == ERROR_NONE);
check(axp2101_set_ldo_enabled(axp2101, AXP2101_ALDO1, true) == ERROR_NONE);
check(axp2101_set_ldo_enabled(axp2101, AXP2101_ALDO2, true) == ERROR_NONE);
check(axp2101_set_ldo_enabled(axp2101, AXP2101_ALDO3, true) == ERROR_NONE);
check(axp2101_set_ldo_enabled(axp2101, AXP2101_ALDO4, true) == ERROR_NONE);
check(axp2101_set_ldo_enabled(axp2101, AXP2101_BLDO1, true) == ERROR_NONE);
check(axp2101_set_ldo_enabled(axp2101, AXP2101_BLDO2, true) == ERROR_NONE);
}
static void on_device_event(Device* device, DeviceEvent event, void* context) {
(void)context;
if (event == DEVICE_EVENT_STARTED && strcmp(device->name, "axp2101") == 0) {
configure_axp2101(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;
}
struct Module m5stack_cores3_module = {
.name = "m5stack-cores3",
.start = start,
.stop = stop,
.symbols = nullptr,
.internal = nullptr
};
}
+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,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
};
}
@@ -0,0 +1,8 @@
# Force CMake reload to detect new files module.cpp and Configuration.cpp
file(GLOB_RECURSE SOURCE_FILES Source/*.c*)
idf_component_register(
SRCS ${SOURCE_FILES}
INCLUDE_DIRS "Source"
REQUIRES Tactility ButtonControl ST7305 PwmBacklight driver
)
@@ -0,0 +1,41 @@
#include "devices/Display.h"
#include <Tactility/hal/Configuration.h>
#include <ButtonControl.h>
#include <driver/gpio.h>
#include <tactility/log.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
static constexpr auto* TAG = "WaveshareRLCD";
using namespace tt::hal;
static DeviceVector createDevices() {
return {
createDisplay(),
ButtonControl::createTwoButtonControl(GPIO_NUM_18, GPIO_NUM_0) // KEY=18 cycles, BOOT=0 selects
};
}
static bool initBoot() {
// Amp enable GPIO46 active HIGH - keep LOW during boot to avoid pop,
// then HIGH after delay so speaker works for MCP / Mp3Player
gpio_config_t io_conf = {};
io_conf.intr_type = GPIO_INTR_DISABLE;
io_conf.mode = GPIO_MODE_OUTPUT;
io_conf.pin_bit_mask = (1ULL << GPIO_NUM_46);
io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
io_conf.pull_up_en = GPIO_PULLUP_DISABLE;
gpio_config(&io_conf);
gpio_set_level(GPIO_NUM_46, 0);
vTaskDelay(pdMS_TO_TICKS(100));
gpio_set_level(GPIO_NUM_46, 1);
LOG_I(TAG, "Speaker amp GPIO46 enabled");
return true;
}
extern const Configuration hardwareConfiguration = {
.initBoot = initBoot,
.createDevices = createDevices
};
@@ -0,0 +1,23 @@
#include "Display.h"
#include <St7305Display.h>
std::shared_ptr<tt::hal::display::DisplayDevice> createDisplay() {
auto configuration = std::make_unique<St7305Display::Configuration>(
SPI2_HOST,
GPIO_NUM_40, // CS
GPIO_NUM_5, // DC
400, // Width
300, // Height
nullptr, // Touch device
false, // swapXY
false, // mirrorX
false, // mirrorY
true, // invertColor - initial true to match existing inverted look, user can toggle via Settings->Display
0 // bufferSize (0 = default, which is full screen = 120,000 pixels)
);
configuration->resetPin = GPIO_NUM_41;
auto display = std::make_shared<St7305Display>(std::move(configuration));
return std::reinterpret_pointer_cast<tt::hal::display::DisplayDevice>(display);
}
@@ -1,6 +1,6 @@
#pragma once
#include <memory>
#include <Tactility/hal/display/DisplayDevice.h>
#include <memory>
std::shared_ptr<tt::hal::display::DisplayDevice> createDisplay();
@@ -10,8 +10,8 @@ static error_t stop() {
return ERROR_NONE;
}
struct Module guition_jc3248w535c_module = {
.name = "guition-jc3248w535c",
struct Module waveshare_esp32_s3_rlcd_module = {
.name = "waveshare-esp32-s3-rlcd",
.start = start,
.stop = stop,
.symbols = nullptr,
@@ -1,7 +1,9 @@
general.vendor=Guition
general.name=JC3248W535C
general.vendor=WaveShare
general.name=ESP32-S3-RLCD-4.2
general.incubating=true
apps.launcherAppId=Launcher
# apps.autoStartAppId=ApWebServer
hardware.target=ESP32S3
hardware.flashSize=16MB
@@ -12,14 +14,13 @@ hardware.tinyUsb=true
hardware.esptoolFlashFreq=120M
hardware.bluetooth=true
dependencies.useDeprecatedHal=false
storage.userDataLocation=SD
display.size=3.5"
display.size=4.2"
display.shape=rectangle
display.dpi=165
display.dpi=120
lvgl.colorDepth=16
sdkconfig.CONFIG_CODEC_DUMMY_SUPPORT=y
lvgl.uiDensity=compact
lvgl.fontSize=20
lvgl.theme=DefaultDark
@@ -0,0 +1,3 @@
dependencies:
- Platforms/platform-esp32
dts: waveshare,esp32-s3-rlcd.dts
@@ -0,0 +1,78 @@
/dts-v1/;
#include <tactility/bindings/root.h>
#include <tactility/bindings/esp32_ble.h>
#include <tactility/bindings/esp32_gpio.h>
#include <tactility/bindings/esp32_i2c_master.h>
#include <tactility/bindings/esp32_sdmmc.h>
#include <tactility/bindings/esp32_spi.h>
#include <tactility/bindings/esp32_i2s.h>
#include <tactility/bindings/esp32_uart.h>
#include <tactility/bindings/display_placeholder.h>
/ {
compatible = "root";
model = "Waveshare ESP32-S3-RLCD-4.2";
ble0 {
compatible = "espressif,esp32-ble";
status = "disabled";
};
gpio0 {
compatible = "espressif,esp32-gpio";
gpio-count = <49>;
};
// Audio: ES7210 mic + speaker via I2S0
// Pins verified from working MicroPython board_config.py for WAVESHARE_RLCD:
// mclk=16, bclk=9, ws=45, tx=8 (DAC out), rx=10 (ES7210 mic in)
// NOTE: Display DC is GPIO5, so I2S must NOT use GPIO5 to avoid bus corruption
i2s0 {
compatible = "espressif,esp32-i2s";
port = <I2S_NUM_0>;
pin-bclk = <&gpio0 9 GPIO_FLAG_NONE>;
pin-ws = <&gpio0 45 GPIO_FLAG_NONE>;
pin-data-out = <&gpio0 8 GPIO_FLAG_NONE>;
pin-data-in = <&gpio0 10 GPIO_FLAG_NONE>;
pin-mclk = <&gpio0 16 GPIO_FLAG_NONE>;
};
i2c0 {
compatible = "espressif,esp32-i2c-master";
port = <I2C_NUM_0>;
clock-frequency = <400000>;
pin-sda = <&gpio0 13 GPIO_FLAG_NONE>;
pin-scl = <&gpio0 14 GPIO_FLAG_NONE>;
};
spi0 {
compatible = "espressif,esp32-spi";
host = <SPI2_HOST>;
cs-gpios = <&gpio0 40 GPIO_FLAG_NONE>;
pin-mosi = <&gpio0 12 GPIO_FLAG_NONE>;
pin-sclk = <&gpio0 11 GPIO_FLAG_NONE>;
display {
compatible = "display-placeholder";
};
};
sdmmc0 {
compatible = "espressif,esp32-sdmmc";
pin-clk = <&gpio0 38 GPIO_FLAG_NONE>;
pin-cmd = <&gpio0 21 GPIO_FLAG_NONE>;
pin-d0 = <&gpio0 39 GPIO_FLAG_NONE>;
pin-d3 = <&gpio0 17 GPIO_FLAG_NONE>;
slot = <SDMMC_HOST_SLOT_1>;
bus-width = <1>;
pullups;
};
uart0 {
compatible = "espressif,esp32-uart";
port = <UART_NUM_0>;
pin-tx = <&gpio0 43 GPIO_FLAG_NONE>;
pin-rx = <&gpio0 44 GPIO_FLAG_NONE>;
};
};
-5
View File
@@ -1,5 +0,0 @@
idf_component_register(
SRC_DIRS "Source"
INCLUDE_DIRS "Source"
REQUIRES Tactility
)
-5
View File
@@ -1,5 +0,0 @@
# AW9523
Multi-functional GPIO expander and LED driver with I2C interface.
[Datasheet](https://www.alldatasheet.com/datasheet-pdf/download/1148542/AWINIC/AW9523B.html)
-33
View File
@@ -1,33 +0,0 @@
#include "Aw9523.h"
#define AW9523_REGISTER_P0 0x02
#define AW9523_REGISTER_P1 0x03
#define AW9523_REGISTER_CTL 0x11
bool Aw9523::readP0(uint8_t& output) const {
return readRegister8(AW9523_REGISTER_P0, output);
}
bool Aw9523::readP1(uint8_t& output) const {
return readRegister8(AW9523_REGISTER_P1, output);
}
bool Aw9523::readCTL(uint8_t& output) const {
return readRegister8(AW9523_REGISTER_CTL, output);
}
bool Aw9523::writeP0(uint8_t value) const {
return writeRegister8(AW9523_REGISTER_P0, value);
}
bool Aw9523::writeP1(uint8_t value) const {
return writeRegister8(AW9523_REGISTER_P1, value);
}
bool Aw9523::writeCTL(uint8_t value) const {
return writeRegister8(AW9523_REGISTER_CTL, value);
}
bool Aw9523::bitOnP1(uint8_t bitmask) const {
return bitOn(AW9523_REGISTER_P1, bitmask);
}
-25
View File
@@ -1,25 +0,0 @@
#pragma once
#include <Tactility/hal/i2c/I2cDevice.h>
#define AW9523_ADDRESS 0x58
class Aw9523 : public tt::hal::i2c::I2cDevice {
public:
explicit Aw9523(::Device* controller) : I2cDevice(controller, AW9523_ADDRESS) {}
std::string getName() const final { return "AW9523"; }
std::string getDescription() const final { return "GPIO expander with LED driver and I2C interface."; }
bool readP0(uint8_t& output) const;
bool readP1(uint8_t& output) const;
bool readCTL(uint8_t& output) const;
bool writeP0(uint8_t value) const;
bool writeP1(uint8_t value) const;
bool writeCTL(uint8_t value) const;
bool bitOnP1(uint8_t bitmask) const;
};
-8
View File
@@ -1,8 +0,0 @@
file(GLOB_RECURSE SOURCE_FILES Source/*.c*)
idf_component_register(
SRCS ${SOURCE_FILES}
INCLUDE_DIRS "Include"
PRIV_INCLUDE_DIRS "Private"
REQUIRES Tactility
)
-59
View File
@@ -1,59 +0,0 @@
#pragma once
#include <axp192/axp192.h>
#include <tactility/device.h>
#include <Tactility/hal/power/PowerDevice.h>
#include <memory>
class Axp192 final : public tt::hal::power::PowerDevice {
static int32_t i2cRead(void* handle, uint8_t address, uint8_t reg, uint8_t* buffer, uint16_t size);
static int32_t i2cWrite(void* handle, uint8_t address, uint8_t reg, const uint8_t* buffer, uint16_t size);
public:
struct Configuration {
::Device* controller;
TickType_t readTimeout = 50 / portTICK_PERIOD_MS;
TickType_t writeTimeout = 50 / portTICK_PERIOD_MS;
};
private:
std::unique_ptr<Configuration> configuration;
axp192_t axpDevice = {
.read = i2cRead,
.write = i2cWrite,
.handle = this
};
bool isInitialized = false;
public:
explicit Axp192(std::unique_ptr<Configuration> configuration) : configuration(std::move(configuration)) {}
~Axp192() override {}
/**
* @warning Must call this function before device can operate!
* @param onInit
*/
bool init(const std::function<bool(axp192_t*)>& onInit) {
isInitialized = onInit(&axpDevice);
return isInitialized;
}
axp192_t* getAxp192() { return &axpDevice; }
std::string getName() const override { return "AXP192"; }
std::string getDescription() const override { return "AXP192 power management via I2C"; }
bool supportsMetric(MetricType type) const override;
bool getMetric(MetricType type, MetricData& data) override;
bool supportsChargeControl() const override { return true; }
bool isAllowedToCharge() const override;
void setAllowedToCharge(bool canCharge) override;
};
-213
View File
@@ -1,213 +0,0 @@
/*
MIT License
Copyright (c) 2019-2021 Mika Tuupola
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
-cut-
This file is part of hardware agnostic I2C driver for AXP192:
https://github.com/tuupola/axp192
SPDX-License-Identifier: MIT
Version: 0.6.0
*/
#ifndef _AXP192_H
#define _AXP192_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
#define AXP192_ADDRESS (0x34)
/* Power control registers */
#define AXP192_POWER_STATUS (0x00)
#define AXP192_CHARGE_STATUS (0x01)
#define AXP192_OTG_VBUS_STATUS (0x04)
#define AXP192_DATA_BUFFER0 (0x06)
#define AXP192_DATA_BUFFER1 (0x07)
#define AXP192_DATA_BUFFER2 (0x08)
#define AXP192_DATA_BUFFER3 (0x09)
#define AXP192_DATA_BUFFER4 (0x0a)
#define AXP192_DATA_BUFFER5 (0x0b)
/* Output control: 2 EXTEN, 0 DCDC2 */
#define AXP192_EXTEN_DCDC2_CONTROL (0x10)
/* Power output control: 6 EXTEN, 4 DCDC2, 3 LDO3, 2 LDO2, 1 DCDC3, 0 DCDC1 */
#define AXP192_DCDC13_LDO23_CONTROL (0x12)
#define AXP192_DCDC2_VOLTAGE (0x23)
#define AXP192_DCDC2_SLOPE (0x25)
#define AXP192_DCDC1_VOLTAGE (0x26)
#define AXP192_DCDC3_VOLTAGE (0x27)
/* Output voltage control: 7-4 LDO2, 3-0 LDO3 */
#define AXP192_LDO23_VOLTAGE (0x28)
#define AXP192_VBUS_IPSOUT_CHANNEL (0x30)
#define AXP192_SHUTDOWN_VOLTAGE (0x31)
#define AXP192_SHUTDOWN_BATTERY_CHGLED_CONTROL (0x32)
#define AXP192_CHARGE_CONTROL_1 (0x33)
#define AXP192_CHARGE_CONTROL_2 (0x34)
#define AXP192_BATTERY_CHARGE_CONTROL (0x35)
#define AXP192_PEK (0x36)
#define AXP192_DCDC_FREQUENCY (0x37)
#define AXP192_BATTERY_CHARGE_LOW_TEMP (0x38)
#define AXP192_BATTERY_CHARGE_HIGH_TEMP (0x39)
#define AXP192_APS_LOW_POWER1 (0x3A)
#define AXP192_APS_LOW_POWER2 (0x3B)
#define AXP192_BATTERY_DISCHARGE_LOW_TEMP (0x3c)
#define AXP192_BATTERY_DISCHARGE_HIGH_TEMP (0x3d)
#define AXP192_DCDC_MODE (0x80)
#define AXP192_ADC_ENABLE_1 (0x82)
#define AXP192_ADC_ENABLE_2 (0x83)
#define AXP192_ADC_RATE_TS_PIN (0x84)
#define AXP192_GPIO30_INPUT_RANGE (0x85)
#define AXP192_GPIO0_ADC_IRQ_RISING (0x86)
#define AXP192_GPIO0_ADC_IRQ_FALLING (0x87)
#define AXP192_TIMER_CONTROL (0x8a)
#define AXP192_VBUS_MONITOR (0x8b)
#define AXP192_TEMP_SHUTDOWN_CONTROL (0x8f)
/* GPIO control registers */
#define AXP192_GPIO0_CONTROL (0x90)
#define AXP192_GPIO0_LDOIO0_VOLTAGE (0x91)
#define AXP192_GPIO1_CONTROL (0x92)
#define AXP192_GPIO2_CONTROL (0x93)
#define AXP192_GPIO20_SIGNAL_STATUS (0x94)
#define AXP192_GPIO43_FUNCTION_CONTROL (0x95)
#define AXP192_GPIO43_SIGNAL_STATUS (0x96)
#define AXP192_GPIO20_PULLDOWN_CONTROL (0x97)
#define AXP192_PWM1_FREQUENCY (0x98)
#define AXP192_PWM1_DUTY_CYCLE_1 (0x99)
#define AXP192_PWM1_DUTY_CYCLE_2 (0x9a)
#define AXP192_PWM2_FREQUENCY (0x9b)
#define AXP192_PWM2_DUTY_CYCLE_1 (0x9c)
#define AXP192_PWM2_DUTY_CYCLE_2 (0x9d)
#define AXP192_N_RSTO_GPIO5_CONTROL (0x9e)
/* Interrupt control registers */
#define AXP192_ENABLE_CONTROL_1 (0x40)
#define AXP192_ENABLE_CONTROL_2 (0x41)
#define AXP192_ENABLE_CONTROL_3 (0x42)
#define AXP192_ENABLE_CONTROL_4 (0x43)
#define AXP192_ENABLE_CONTROL_5 (0x4a)
#define AXP192_IRQ_STATUS_1 (0x44)
#define AXP192_IRQ_STATUS_2 (0x45)
#define AXP192_IRQ_STATUS_3 (0x46)
#define AXP192_IRQ_STATUS_4 (0x47)
#define AXP192_IRQ_STATUS_5 (0x4d)
/* ADC data registers */
#define AXP192_ACIN_VOLTAGE (0x56)
#define AXP192_ACIN_CURRENT (0x58)
#define AXP192_VBUS_VOLTAGE (0x5a)
#define AXP192_VBUS_CURRENT (0x5c)
#define AXP192_TEMP (0x5e)
#define AXP192_TS_INPUT (0x62)
#define AXP192_GPIO0_VOLTAGE (0x64)
#define AXP192_GPIO1_VOLTAGE (0x66)
#define AXP192_GPIO2_VOLTAGE (0x68)
#define AXP192_GPIO3_VOLTAGE (0x6a)
#define AXP192_BATTERY_POWER (0x70)
#define AXP192_BATTERY_VOLTAGE (0x78)
#define AXP192_CHARGE_CURRENT (0x7a)
#define AXP192_DISCHARGE_CURRENT (0x7c)
#define AXP192_APS_VOLTAGE (0x7e)
#define AXP192_CHARGE_COULOMB (0xb0)
#define AXP192_DISCHARGE_COULOMB (0xb4)
#define AXP192_COULOMB_COUNTER_CONTROL (0xb8)
/* Computed ADC */
#define AXP192_COULOMB_COUNTER (0xff)
/* IOCTL commands */
#define AXP192_READ_POWER_STATUS (0x0001)
#define AXP192_READ_CHARGE_STATUS (0x0101)
#define AXP192_COULOMB_COUNTER_ENABLE (0xb801)
#define AXP192_COULOMB_COUNTER_DISABLE (0xb802)
#define AXP192_COULOMB_COUNTER_SUSPEND (0xb803)
#define AXP192_COULOMB_COUNTER_CLEAR (0xb804)
#define AXP192_LDOIO0_ENABLE (0x9000)
#define AXP192_LDOIO0_DISABLE (0x9001)
#define AXP192_DCDC2_ENABLE (0x1000)
#define AXP192_DCDC2_DISABLE (0x1001)
#define AXP192_EXTEN_ENABLE (0x1002)
#define AXP192_EXTEN_DISABLE (0x1003)
#define AXP192_LDO2_ENABLE (0x1200)
#define AXP192_LDO2_DISABLE (0x1201)
#define AXP192_LDO3_ENABLE (0x1202)
#define AXP192_LDO3_DISABLE (0x1203)
#define AXP192_DCDC1_ENABLE (0x1204)
#define AXP192_DCDC1_DISABLE (0x1205)
#define AXP192_DCDC3_ENABLE (0x1206)
#define AXP192_DCDC3_DISABLE (0x1207)
#define AXP192_DCDC1_SET_VOLTAGE (0x2600)
#define AXP192_DCDC2_SET_VOLTAGE (0x2300)
#define AXP192_DCDC3_SET_VOLTAGE (0x2700)
#define AXP192_LDO2_SET_VOLTAGE (0x2800)
#define AXP192_LDO3_SET_VOLTAGE (0x2801)
#define AXP192_LDOIO0_SET_VOLTAGE (0x9100)
#define AXP192_LOW (0)
#define AXP192_HIGH (1)
#define AXP192_GPIO0_SET_LEVEL (0x9400)
#define AXP192_GPIO1_SET_LEVEL (0x9401)
#define AXP192_GPIO2_SET_LEVEL (0x9402)
#define AXP192_GPIO4_SET_LEVEL (0x9601)
/* Error codes */
#define AXP192_OK (0)
#define AXP192_ERROR_NOTTY (-1)
#define AXP192_ERROR_EINVAL (-22)
#define AXP192_ERROR_ENOTSUP (-95)
typedef struct {
uint8_t command;
uint8_t data[2];
uint8_t count;
} axp192_init_command_t;
/* These should be provided by the HAL. */
typedef struct {
int32_t (* read)(void *handle, uint8_t address, uint8_t reg, uint8_t *buffer, uint16_t size);
int32_t (* write)(void *handle, uint8_t address, uint8_t reg, const uint8_t *buffer, uint16_t size);
void *handle;
} axp192_t;
typedef int32_t axp192_err_t;
axp192_err_t axp192_init(const axp192_t *axp);
axp192_err_t axp192_read(const axp192_t *axp, uint8_t reg, void *buffer);
axp192_err_t axp192_write(const axp192_t *axp, uint8_t reg, uint8_t value);
axp192_err_t axp192_ioctl(const axp192_t *axp, int command, ...);
#ifdef __cplusplus
}
#endif
#endif
-21
View File
@@ -1,21 +0,0 @@
MIT License
Copyright (c) 2019-2021 Mika Tuupola
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
-4
View File
@@ -1,4 +0,0 @@
From https://github.com/tuupola/axp192
Changed some code for Tactility:
- `axp192_write()` now accepts a byte value instead of a byte pointer
@@ -1,125 +0,0 @@
/*
MIT License
Copyright (c) 2019-2021 Mika Tuupola
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
-cut-
This file is part of hardware agnostic I2C driver for AXP192:
https://github.com/tuupola/axp192
SPDX-License-Identifier: MIT
Version: 0.6.0
*/
#ifndef _AXP192_CONFIG_H
#define _AXP192_CONFIG_H
#ifdef __cplusplus
extern "C" {
#endif
#ifdef AXP192_INCLUDE_SDKCONFIG_H
#include "sdkconfig.h"
/* This requires you to run menuconfig first. */
#define CONFIG_AXP192_EXTEN_DCDC2_CONTROL ( \
CONFIG_AXP192_EXTEN_DCDC2_CONTROL_BIT2 | \
CONFIG_AXP192_EXTEN_DCDC2_CONTROL_BIT0 \
)
#define CONFIG_AXP192_DCDC13_LDO23_CONTROL ( \
CONFIG_AXP192_DCDC13_LDO23_CONTROL_BIT6 | \
CONFIG_AXP192_DCDC13_LDO23_CONTROL_BIT4 | \
CONFIG_AXP192_DCDC13_LDO23_CONTROL_BIT3 | \
CONFIG_AXP192_DCDC13_LDO23_CONTROL_BIT2 | \
CONFIG_AXP192_DCDC13_LDO23_CONTROL_BIT1 | \
CONFIG_AXP192_DCDC13_LDO23_CONTROL_BIT0 \
)
#define CONFIG_AXP192_LDO23_VOLTAGE ( \
CONFIG_AXP192_LDO23_VOLTAGE_BIT74 | \
CONFIG_AXP192_LDO23_VOLTAGE_BIT30 \
)
#define CONFIG_AXP192_DCDC1_VOLTAGE ( \
CONFIG_AXP192_DCDC1_VOLTAGE_BIT60 \
)
#define CONFIG_AXP192_DCDC3_VOLTAGE ( \
CONFIG_AXP192_DCDC3_VOLTAGE_BIT60 \
)
#define CONFIG_AXP192_ADC_ENABLE_1 ( \
CONFIG_AXP192_ADC_ENABLE_1_BIT7 | \
CONFIG_AXP192_ADC_ENABLE_1_BIT6 | \
CONFIG_AXP192_ADC_ENABLE_1_BIT5 | \
CONFIG_AXP192_ADC_ENABLE_1_BIT4 | \
CONFIG_AXP192_ADC_ENABLE_1_BIT3 | \
CONFIG_AXP192_ADC_ENABLE_1_BIT2 | \
CONFIG_AXP192_ADC_ENABLE_1_BIT1 | \
CONFIG_AXP192_ADC_ENABLE_1_BIT0 \
)
#define CONFIG_AXP192_CHARGE_CONTROL_1 ( \
CONFIG_AXP192_CHARGE_CONTROL_1_BIT7 | \
CONFIG_AXP192_CHARGE_CONTROL_1_BIT65 | \
CONFIG_AXP192_CHARGE_CONTROL_1_BIT4 | \
CONFIG_AXP192_CHARGE_CONTROL_1_BIT30 \
)
#define CONFIG_AXP192_BATTERY_CHARGE_CONTROL ( \
CONFIG_AXP192_BATTERY_CHARGE_CONTROL_BIT7 | \
CONFIG_AXP192_BATTERY_CHARGE_CONTROL_BIT65 | \
CONFIG_AXP192_BATTERY_CHARGE_CONTROL_BIT10 \
)
#define CONFIG_AXP192_GPIO0_CONTROL ( \
CONFIG_AXP192_GPIO0_CONTROL_BIT20 \
)
#define CONFIG_AXP192_GPIO1_CONTROL ( \
CONFIG_AXP192_GPIO1_CONTROL_BIT20 \
)
#define CONFIG_AXP192_GPIO2_CONTROL ( \
CONFIG_AXP192_GPIO2_CONTROL_BIT20 \
)
#define CONFIG_AXP192_GPIO43_FUNCTION_CONTROL ( \
CONFIG_AXP192_GPIO43_FUNCTION_CONTROL_BIT7 | \
CONFIG_AXP192_GPIO43_FUNCTION_CONTROL_BIT32 | \
CONFIG_AXP192_GPIO43_FUNCTION_CONTROL_BIT10 \
)
#define CONFIG_AXP192_GPIO0_LDOIO0_VOLTAGE ( \
CONFIG_AXP192_GPIO0_LDOIO0_VOLTAGE_BIT74 \
)
#endif /* AXP192_INCLUDE_SDKCONFIG_H */
#ifdef __cplusplus
}
#endif
#endif
-5
View File
@@ -1,5 +0,0 @@
# AXP192
Power management with I2C interface. Used on M5Stack Core2 and StickCPlus.
It includes driver code from https://github.com/tuupola/axp192, Copyright (c) 2019-2021 Mika Tuupola, MIT License
-118
View File
@@ -1,118 +0,0 @@
#include "Axp192.h"
#include <tactility/drivers/i2c_controller.h>
constexpr auto TAG = "Axp192Power";
int32_t Axp192::i2cRead(void* handle, uint8_t address, uint8_t reg, uint8_t* buffer, uint16_t size) {
const auto* device = static_cast<Axp192*>(handle);
if (i2c_controller_read_register(device->configuration->controller, address, reg, buffer, size, device->configuration->readTimeout) == ERROR_NONE) {
return AXP192_OK;
} else {
return 1;
}
}
int32_t Axp192::i2cWrite(void* handle, uint8_t address, uint8_t reg, const uint8_t* buffer, uint16_t size) {
const auto* device = static_cast<Axp192*>(handle);
if (i2c_controller_write_register(device->configuration->controller, address, reg, buffer, size, device->configuration->writeTimeout) == ERROR_NONE) {
return AXP192_OK;
} else {
return 1;
}
}
bool Axp192::supportsMetric(MetricType type) const {
if (!isInitialized) {
return false;
}
switch (type) {
using enum MetricType;
case BatteryVoltage:
case ChargeLevel:
case IsCharging:
return true;
default:
return false;
}
}
bool Axp192::getMetric(MetricType type, MetricData& data) {
switch (type) {
using enum MetricType;
case BatteryVoltage: {
float voltage;
if (axp192_read(&axpDevice, AXP192_BATTERY_VOLTAGE, &voltage) == ESP_OK) {
data.valueAsUint32 = (uint32_t)std::max((voltage * 1000.f), 0.0f);
return true;
} else {
return false;
}
}
case ChargeLevel: {
float vbat, charge_current;
if (
axp192_read(&axpDevice, AXP192_BATTERY_VOLTAGE, &vbat) == ESP_OK &&
axp192_read(&axpDevice, AXP192_CHARGE_CURRENT, &charge_current) == ESP_OK
) {
float max_voltage = 4.20f;
float min_voltage = 2.69f; // From M5Unified
float voltage_correction = (charge_current > 0.01f) ? -0.1f : 0.f; // Roughly 0.1V drop when ccharging
float corrected_voltage = vbat + voltage_correction;
if (corrected_voltage > 2.69f) {
float charge_factor = (corrected_voltage - min_voltage) / (max_voltage - min_voltage);
data.valueAsUint8 = (uint8_t)(charge_factor * 100.f);
} else {
data.valueAsUint8 = 0;
}
return true;
} else {
return false;
}
}
case IsCharging: {
float charge_current;
if (axp192_read(&axpDevice, AXP192_CHARGE_CURRENT, &charge_current) == ESP_OK) {
data.valueAsBool = charge_current > 0.001f;
return true;
} else {
return false;
}
}
case Current: {
float charge_current, discharge_current;
if (
axp192_read(&axpDevice, AXP192_CHARGE_CURRENT, &charge_current) == ESP_OK &&
axp192_read(&axpDevice, AXP192_DISCHARGE_CURRENT, &discharge_current) == ESP_OK
) {
if (charge_current > 0.0f) {
data.valueAsInt32 = (int32_t) (charge_current * 1000.0f);
} else {
data.valueAsInt32 = -(int32_t) (discharge_current * 1000.0f);
}
return true;
} else {
return false;
}
}
default:
return false;
}
}
bool Axp192::isAllowedToCharge() const {
uint8_t buffer;
if (axp192_read(&axpDevice, AXP192_CHARGE_CONTROL_1, &buffer) == ESP_OK) {
return buffer & 0x80;
} else {
return false;
}
}
void Axp192::setAllowedToCharge(bool canCharge) {
uint8_t buffer;
if (axp192_read(&axpDevice, AXP192_CHARGE_CONTROL_1, &buffer) == ESP_OK) {
buffer = (buffer & 0x7F) + (canCharge ? 0x80 : 0x00);
axp192_write(&axpDevice, AXP192_CHARGE_CONTROL_1, buffer);
}
}
-440
View File
@@ -1,440 +0,0 @@
/*
MIT License
Copyright (c) 2019-2021 Mika Tuupola
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
-cut-
This file is part of hardware agnostic I2C driver for AXP192:
https://github.com/tuupola/axp192
SPDX-License-Identifier: MIT
Version: 0.6.0
*/
#include <stdarg.h>
#include <stdint.h>
#include "axp192/axp192_config.h"
#include "axp192/axp192.h"
static axp192_err_t read_coloumb_counter(const axp192_t *axp, float *buffer);
static axp192_err_t read_battery_power(const axp192_t *axp, float *buffer);
static const axp192_init_command_t init_commands[] = {
#ifdef AXP192_INCLUDE_SDKCONFIG_H
/* Currently you have to use menuconfig to be able to use axp192_init() */
{AXP192_DCDC1_VOLTAGE, {CONFIG_AXP192_DCDC1_VOLTAGE}, 1},
{AXP192_DCDC3_VOLTAGE, {CONFIG_AXP192_DCDC3_VOLTAGE}, 1},
{AXP192_LDO23_VOLTAGE, {CONFIG_AXP192_LDO23_VOLTAGE}, 1},
{AXP192_GPIO0_LDOIO0_VOLTAGE, {CONFIG_AXP192_GPIO0_LDOIO0_VOLTAGE}, 1},
{AXP192_DCDC13_LDO23_CONTROL, {CONFIG_AXP192_DCDC13_LDO23_CONTROL}, 1},
{AXP192_EXTEN_DCDC2_CONTROL, {CONFIG_AXP192_EXTEN_DCDC2_CONTROL}, 1},
{AXP192_GPIO0_CONTROL, {CONFIG_AXP192_GPIO0_CONTROL}, 1},
{AXP192_GPIO1_CONTROL, {CONFIG_AXP192_GPIO1_CONTROL}, 1},
{AXP192_GPIO2_CONTROL, {CONFIG_AXP192_GPIO2_CONTROL}, 1},
{AXP192_GPIO43_FUNCTION_CONTROL, {CONFIG_AXP192_GPIO43_FUNCTION_CONTROL}, 1},
{AXP192_ADC_ENABLE_1, {CONFIG_AXP192_ADC_ENABLE_1}, 1},
{AXP192_CHARGE_CONTROL_1, {CONFIG_AXP192_CHARGE_CONTROL_1}, 1},
{AXP192_BATTERY_CHARGE_CONTROL, {CONFIG_AXP192_BATTERY_CHARGE_CONTROL}, 1},
#endif /* AXP192_INCLUDE_SDKCONFIG_H */
/* End of commands. */
{0, {0}, 0xff},
};
axp192_err_t axp192_init(const axp192_t *axp)
{
uint8_t cmd = 0;
axp192_err_t status;
/* Send all the commands. */
while (init_commands[cmd].count != 0xff) {
status = axp->write(
axp->handle,
AXP192_ADDRESS,
init_commands[cmd].command,
init_commands[cmd].data,
init_commands[cmd].count & 0x1f
);
if (AXP192_OK != status) {
return status;
}
cmd++;
}
return AXP192_OK;
}
static axp192_err_t axp192_read_adc(const axp192_t *axp, uint8_t reg, float *buffer)
{
uint8_t tmp[4];
float sensitivity = 1.0;
float offset = 0.0;
axp192_err_t status;
switch (reg) {
case AXP192_ACIN_VOLTAGE:
case AXP192_VBUS_VOLTAGE:
/* 1.7mV per LSB */
sensitivity = 1.7 / 1000;
break;
case AXP192_ACIN_CURRENT:
/* 0.375mA per LSB */
sensitivity = 0.625 / 1000;
break;
case AXP192_VBUS_CURRENT:
/* 0.375mA per LSB */
sensitivity = 0.375 / 1000;
break;
case AXP192_TEMP:
/* 0.1C per LSB, 0x00 = -144.7C */
sensitivity = 0.1;
offset = -144.7;
break;
case AXP192_TS_INPUT:
/* 0.8mV per LSB */
sensitivity = 0.8 / 1000;
break;
case AXP192_BATTERY_POWER:
/* 1.1mV * 0.5mA per LSB */
return read_battery_power(axp, buffer);
break;
case AXP192_BATTERY_VOLTAGE:
/* 1.1mV per LSB */
sensitivity = 1.1 / 1000;
break;
case AXP192_CHARGE_CURRENT:
case AXP192_DISCHARGE_CURRENT:
/* 0.5mV per LSB */
sensitivity = 0.5 / 1000;
break;
case AXP192_APS_VOLTAGE:
/* 1.4mV per LSB */
sensitivity = 1.4 / 1000;
break;
case AXP192_COULOMB_COUNTER:
/* This is currently untested. */
return read_coloumb_counter(axp, buffer);
break;
}
status = axp->read(axp->handle, AXP192_ADDRESS, reg, tmp, 2);
if (AXP192_OK != status) {
return status;
}
*buffer = (((tmp[0] << 4) + tmp[1]) * sensitivity) + offset;
return AXP192_OK;
}
axp192_err_t axp192_read(const axp192_t *axp, uint8_t reg, void *buffer) {
switch (reg) {
case AXP192_ACIN_VOLTAGE:
case AXP192_VBUS_VOLTAGE:
case AXP192_ACIN_CURRENT:
case AXP192_VBUS_CURRENT:
case AXP192_TEMP:
case AXP192_TS_INPUT:
case AXP192_BATTERY_POWER:
case AXP192_BATTERY_VOLTAGE:
case AXP192_CHARGE_CURRENT:
case AXP192_DISCHARGE_CURRENT:
case AXP192_APS_VOLTAGE:
case AXP192_COULOMB_COUNTER:
/* Return ADC value. */
return axp192_read_adc(axp, reg, buffer);
break;
default:
/* Return raw register value. */
return axp->read(axp->handle, AXP192_ADDRESS, reg, buffer, 1);
}
}
axp192_err_t axp192_write(const axp192_t *axp, uint8_t reg, uint8_t value) {
switch (reg) {
case AXP192_ACIN_VOLTAGE:
case AXP192_VBUS_VOLTAGE:
case AXP192_ACIN_CURRENT:
case AXP192_VBUS_CURRENT:
case AXP192_TEMP:
case AXP192_TS_INPUT:
case AXP192_BATTERY_POWER:
case AXP192_BATTERY_VOLTAGE:
case AXP192_CHARGE_CURRENT:
case AXP192_DISCHARGE_CURRENT:
case AXP192_APS_VOLTAGE:
case AXP192_COULOMB_COUNTER:
/* Read only register. */
return AXP192_ERROR_ENOTSUP;
break;
default:
/* Write raw register value. */
return axp->write(axp->handle, AXP192_ADDRESS, reg, &value, 1);
}
}
axp192_err_t axp192_ioctl(const axp192_t *axp, int command, ...)
{
uint8_t reg = command >> 8;
uint8_t tmp;
uint16_t argument;
va_list ap;
switch (command) {
case AXP192_COULOMB_COUNTER_ENABLE:
tmp = 0b10000000;
return axp->write(axp->handle, AXP192_ADDRESS, reg, &tmp, 1);
break;
case AXP192_COULOMB_COUNTER_DISABLE:
tmp = 0b00000000;
return axp->write(axp->handle, AXP192_ADDRESS, reg, &tmp, 1);
break;
case AXP192_COULOMB_COUNTER_SUSPEND:
tmp = 0b11000000;
return axp->write(axp->handle, AXP192_ADDRESS, reg, &tmp, 1);
break;
case AXP192_COULOMB_COUNTER_CLEAR:
tmp = 0b10100000;
return axp->write(axp->handle, AXP192_ADDRESS, reg, &tmp, 1);
break;
/* This is currently untested. */
case AXP192_LDOIO0_ENABLE:
/* 0x02 = LDO */
tmp = 0b00000010;
return axp->write(axp->handle, AXP192_ADDRESS, reg, &tmp, 1);
break;
/* This is currently untested. */
case AXP192_LDOIO0_DISABLE:
/* 0x07 = float */
tmp = 0b00000111;
return axp->write(axp->handle, AXP192_ADDRESS, reg, &tmp, 1);
break;
case AXP192_LDO2_ENABLE:
/* This is currently untested. */
case AXP192_EXTEN_ENABLE:
axp->read(axp->handle, AXP192_ADDRESS, reg, &tmp, 1);
tmp |= 0b00000100;
return axp->write(axp->handle, AXP192_ADDRESS, reg, &tmp, 1);
break;
case AXP192_LDO2_DISABLE:
/* This is currently untested. */
case AXP192_EXTEN_DISABLE:
axp->read(axp->handle, AXP192_ADDRESS, reg, &tmp, 1);
tmp &= ~0b00000100;
return axp->write(axp->handle, AXP192_ADDRESS, reg, &tmp, 1);
break;
/* This is currently untested. */
case AXP192_GPIO2_SET_LEVEL:
axp->read(axp->handle, AXP192_ADDRESS, reg, &tmp, 1);
va_start(ap, command);
argument = (uint8_t) va_arg(ap, int);
va_end(ap);
if (argument) {
tmp |= 0b00000100;
} else {
tmp &= ~0b00000100;
}
return axp->write(axp->handle, AXP192_ADDRESS, reg, &tmp, 1);
break;
case AXP192_LDO3_ENABLE:
axp->read(axp->handle, AXP192_ADDRESS, reg, &tmp, 1);
tmp |= 0b00001000;
return axp->write(axp->handle, AXP192_ADDRESS, reg, &tmp, 1);
break;
case AXP192_LDO3_DISABLE:
axp->read(axp->handle, AXP192_ADDRESS, reg, &tmp, 1);
tmp &= ~0b00001000;
return axp->write(axp->handle, AXP192_ADDRESS, reg, &tmp, 1);
break;
/* This is currently untested. */
case AXP192_DCDC1_ENABLE:
/* This is currently untested. */
case AXP192_DCDC2_ENABLE:
axp->read(axp->handle, AXP192_ADDRESS, reg, &tmp, 1);
tmp |= 0b00000001;
return axp->write(axp->handle, AXP192_ADDRESS, reg, &tmp, 1);
break;
/* This is currently untested. */
case AXP192_DCDC1_DISABLE:
/* This is currently untested. */
case AXP192_DCDC2_DISABLE:
axp->read(axp->handle, AXP192_ADDRESS, reg, &tmp, 1);
tmp &= ~0b00000001;
return axp->write(axp->handle, AXP192_ADDRESS, reg, &tmp, 1);
break;
case AXP192_DCDC3_ENABLE:
axp->read(axp->handle, AXP192_ADDRESS, reg, &tmp, 1);
tmp |= 0b00000010;
return axp->write(axp->handle, AXP192_ADDRESS, reg, &tmp, 1);
break;
case AXP192_DCDC3_DISABLE:
axp->read(axp->handle, AXP192_ADDRESS, reg, &tmp, 1);
tmp &= ~0b00000010;
return axp->write(axp->handle, AXP192_ADDRESS, reg, &tmp, 1);
break;
case AXP192_GPIO1_SET_LEVEL:
case AXP192_GPIO4_SET_LEVEL:
axp->read(axp->handle, AXP192_ADDRESS, reg, &tmp, 1);
va_start(ap, command);
argument = (uint8_t) va_arg(ap, int);
va_end(ap);
if (argument) {
tmp |= 0b00000010;
} else {
tmp &= ~0b00000010;
}
return axp->write(axp->handle, AXP192_ADDRESS, reg, &tmp, 1);
break;
/* This is currently untested. */
case AXP192_GPIO0_SET_LEVEL:
axp->read(axp->handle, AXP192_ADDRESS, reg, &tmp, 1);
va_start(ap, command);
argument = (uint8_t) va_arg(ap, int);
va_end(ap);
if (argument) {
tmp |= 0b00000001;
} else {
tmp &= ~0b00000001;
}
return axp->write(axp->handle, AXP192_ADDRESS, reg, &tmp, 1);
break;
case AXP192_DCDC1_SET_VOLTAGE:
case AXP192_DCDC3_SET_VOLTAGE:
va_start(ap, command);
argument = (uint16_t) va_arg(ap, int);
va_end(ap);
/* 700-3500mv 25mV per step */
if ((argument < 700) || (argument > 3500)) {
return AXP192_ERROR_EINVAL;
}
tmp = (argument - 700) / 25;
return axp->write(axp->handle, AXP192_ADDRESS, reg, &tmp, 1);
break;
/* This is currently untested. */
case AXP192_DCDC2_SET_VOLTAGE:
va_start(ap, command);
argument = (uint16_t) va_arg(ap, int);
va_end(ap);
/* 700-2275mV 25mV per step */
if ((argument < 700) || (argument > 2275)) {
return AXP192_ERROR_EINVAL;
}
tmp = (argument - 700) / 25;
return axp->write(axp->handle, AXP192_ADDRESS, reg, &tmp, 1);
break;
/* This is currently untested. */
case AXP192_LDO2_SET_VOLTAGE:
va_start(ap, command);
argument = (uint16_t) va_arg(ap, int);
va_end(ap);
/* 1800-3300mV 100mV per step */
if ((argument < 1800) || (argument > 3300)) {
return AXP192_ERROR_EINVAL;
}
axp->read(axp->handle, AXP192_ADDRESS, reg, &tmp, 1);
tmp &= ~0xf0;
tmp |= (((argument - 1800) / 100) << 4);
return axp->write(axp->handle, AXP192_ADDRESS, reg, &tmp, 1);
break;
/* This is currently untested. */
case AXP192_LDO3_SET_VOLTAGE:
va_start(ap, command);
argument = (uint16_t) va_arg(ap, int);
va_end(ap);
/* 1800-3300mV 100mV per step */
if ((argument < 1800) || (argument > 3300)) {
return AXP192_ERROR_EINVAL;
}
axp->read(axp->handle, AXP192_ADDRESS, reg, &tmp, 1);
tmp &= ~0x0f;
tmp |= ((argument - 1800) / 100);
return axp->write(axp->handle, AXP192_ADDRESS, reg, &tmp, 1);
break;
/* This is currently untested. */
case AXP192_LDOIO0_SET_VOLTAGE:
va_start(ap, command);
argument = (uint16_t) va_arg(ap, int);
va_end(ap);
/* 1800-3300mV 100mV per step, 2800mV default. */
if ((argument < 1800) || (argument > 3300)) {
return AXP192_ERROR_EINVAL;
}
tmp = (((argument - 1800) / 100) << 4);
return axp->write(axp->handle, AXP192_ADDRESS, reg, &tmp, 1);
break;
}
return AXP192_ERROR_NOTTY;
}
static axp192_err_t read_coloumb_counter(const axp192_t *axp, float *buffer)
{
uint8_t tmp[4];
int32_t coin, coout;
axp192_err_t status;
status = axp->read(axp->handle, AXP192_ADDRESS, AXP192_CHARGE_COULOMB, tmp, sizeof(coin));
if (AXP192_OK != status) {
return status;
}
coin = (tmp[0] << 24) + (tmp[1] << 16) + (tmp[2] << 8) + tmp[3];
status = axp->read(axp->handle, AXP192_ADDRESS, AXP192_DISCHARGE_COULOMB, tmp, sizeof(coout));
if (AXP192_OK != status) {
return status;
}
coout = (tmp[0] << 24) + (tmp[1] << 16) + (tmp[2] << 8) + tmp[3];
/* CmAh = 65536 * 0.5mA *coin - cout) / 3600 / ADC sample rate */
*buffer = 32768 * (coin - coout) / 3600 / 25;
return AXP192_OK;
}
static axp192_err_t read_battery_power(const axp192_t *axp, float *buffer)
{
uint8_t tmp[4];
float sensitivity;
axp192_err_t status;
/* 1.1mV * 0.5mA per LSB */
sensitivity = 1.1 * 0.5 / 1000;
status = axp->read(axp->handle, AXP192_ADDRESS, AXP192_BATTERY_POWER, tmp, 3);
if (AXP192_OK != status) {
return status;
}
*buffer = (((tmp[0] << 16) + (tmp[1] << 8) + tmp[2]) * sensitivity);
return AXP192_OK;
}
-5
View File
@@ -1,5 +0,0 @@
idf_component_register(
SRC_DIRS "Source"
INCLUDE_DIRS "Source"
REQUIRES Tactility esp_adc
)
-3
View File
@@ -1,3 +0,0 @@
# EstimatedPower
Use ADC measurements to read voltage and estimate the available power that is left in the battery.
@@ -1,65 +0,0 @@
#include "ChargeFromAdcVoltage.h"
#include <tactility/log.h>
constexpr auto* TAG = "ChargeFromAdcV";
constexpr auto MAX_VOLTAGE_SAMPLES = 15;
ChargeFromAdcVoltage::ChargeFromAdcVoltage(
const Configuration& configuration,
float voltageMin,
float voltageMax
) : configuration(configuration), chargeFromVoltage(voltageMin, voltageMax) {
if (adc_oneshot_new_unit(&configuration.adcConfig, &adcHandle) != ESP_OK) {
LOG_E(TAG, "ADC config failed");
return;
}
if (adc_oneshot_config_channel(adcHandle, configuration.adcChannel, &configuration.adcChannelConfig) != ESP_OK) {
LOG_E(TAG, "ADC channel config failed");
adc_oneshot_del_unit(adcHandle);
adcHandle = nullptr;
return;
}
}
ChargeFromAdcVoltage::~ChargeFromAdcVoltage() {
if (adcHandle) {
adc_oneshot_del_unit(adcHandle);
}
}
bool ChargeFromAdcVoltage::readBatteryVoltageOnce(uint32_t& output) const {
if (adcHandle == nullptr) {
return false;
}
int raw;
if (adc_oneshot_read(adcHandle, configuration.adcChannel, &raw) == ESP_OK) {
output = configuration.adcMultiplier * ((1000.f * configuration.adcRefVoltage) / 4096.f) * (float)raw;
LOG_V(TAG, "Raw = %d, voltage = %u", raw, (unsigned)output);
return true;
} else {
LOG_E(TAG, "Read failed");
return false;
}
}
bool ChargeFromAdcVoltage::readBatteryVoltageSampled(uint32_t& output) const {
size_t samples_read = 0;
uint32_t sample_accumulator = 0;
uint32_t sample_read_buffer;
for (size_t i = 0; i < MAX_VOLTAGE_SAMPLES; ++i) {
if (readBatteryVoltageOnce(sample_read_buffer)) {
sample_accumulator += sample_read_buffer;
samples_read++;
}
}
if (samples_read == 0) {
return false;
}
output = sample_accumulator / samples_read;
return true;
}
@@ -1,44 +0,0 @@
#pragma once
#include <ChargeFromVoltage.h>
#include <esp_adc/adc_oneshot.h>
class ChargeFromAdcVoltage {
public:
struct Configuration {
float adcMultiplier = 1.0f;
float adcRefVoltage = 3.3f;
adc_channel_t adcChannel = ADC_CHANNEL_3;
adc_oneshot_unit_init_cfg_t adcConfig = {
.unit_id = ADC_UNIT_1,
.clk_src = ADC_RTC_CLK_SRC_DEFAULT,
.ulp_mode = ADC_ULP_MODE_DISABLE,
};
adc_oneshot_chan_cfg_t adcChannelConfig = {
.atten = ADC_ATTEN_DB_12,
.bitwidth = ADC_BITWIDTH_DEFAULT,
};
};
private:
adc_oneshot_unit_handle_t adcHandle = nullptr;
Configuration configuration;
ChargeFromVoltage chargeFromVoltage;
public:
explicit ChargeFromAdcVoltage(const Configuration& configuration, float voltageMin = 3.2f, float voltageMax = 4.2f);
~ChargeFromAdcVoltage();
bool isInitialized() const { return adcHandle != nullptr; }
bool readBatteryVoltageSampled(uint32_t& output) const;
bool readBatteryVoltageOnce(uint32_t& output) const;
uint8_t estimateChargeLevelFromVoltage(uint32_t milliVolt) const { return chargeFromVoltage.estimateCharge(milliVolt); }
};
@@ -1,18 +0,0 @@
#include "ChargeFromVoltage.h"
#include <tactility/log.h>
#include <algorithm>
constexpr auto* TAG = "ChargeFromVoltage";
uint8_t ChargeFromVoltage::estimateCharge(uint32_t milliVolt) const {
const float volts = std::min((float)milliVolt / 1000.f, batteryVoltageMax);
if (volts < batteryVoltageMin) {
return 0;
}
const float voltage_percentage = (volts - batteryVoltageMin) / (batteryVoltageMax - batteryVoltageMin);
const float voltage_factor = std::min(1.0f, voltage_percentage);
const auto charge_level = (uint8_t) (voltage_factor * 100.f);
LOG_D(TAG, "mV = %u, scaled = %f, factor = %.2f, result = %d", (unsigned)milliVolt, volts, voltage_factor, charge_level);
return charge_level;
}
@@ -1,22 +0,0 @@
#pragma once
#include <cstdint>
class ChargeFromVoltage {
float batteryVoltageMin;
float batteryVoltageMax;
public:
explicit ChargeFromVoltage(float voltageMin = 3.2f, float voltageMax = 4.2f) :
batteryVoltageMin(voltageMin),
batteryVoltageMax(voltageMax)
{}
/**
* @param milliVolt
* @return a value in the rage of [0, 100] which represents [0%, 100%] charge
*/
uint8_t estimateCharge(uint32_t milliVolt) const;
};
@@ -1,30 +0,0 @@
#include "EstimatedPower.h"
bool EstimatedPower::supportsMetric(MetricType type) const {
switch (type) {
using enum MetricType;
case BatteryVoltage:
case ChargeLevel:
return true;
default:
return false;
}
}
bool EstimatedPower::getMetric(MetricType type, MetricData& data) {
switch (type) {
using enum MetricType;
case BatteryVoltage:
return chargeFromAdcVoltage->readBatteryVoltageSampled(data.valueAsUint32);
case ChargeLevel:
if (chargeFromAdcVoltage->readBatteryVoltageSampled(data.valueAsUint32)) {
data.valueAsUint32 = chargeFromAdcVoltage->estimateChargeLevelFromVoltage(data.valueAsUint32);
return true;
} else {
return false;
}
default:
return false;
}
}
@@ -1,27 +0,0 @@
#pragma once
#include <ChargeFromAdcVoltage.h>
#include <Tactility/hal/power/PowerDevice.h>
using tt::hal::power::PowerDevice;
/**
* Uses Voltage measurements to estimate charge.
* Supports voltage and charge level metrics.
* Can be overridden to further extend supported metrics.
*/
class EstimatedPower final : public PowerDevice {
std::unique_ptr<ChargeFromAdcVoltage> chargeFromAdcVoltage;
public:
explicit EstimatedPower(ChargeFromAdcVoltage::Configuration configuration) :
chargeFromAdcVoltage(std::make_unique<ChargeFromAdcVoltage>(std::move(configuration))) {}
std::string getName() const override { return "ADC Power Measurement"; }
std::string getDescription() const override { return "Power measurement interface via ADC pin"; }
bool supportsMetric(MetricType type) const override;
bool getMetric(MetricType type, MetricData& data) override;
};
+108
View File
@@ -0,0 +1,108 @@
#include "St7305Display.h"
#include "esp_lcd_st7305.h"
#include <tactility/log.h>
#include <esp_lcd_panel_ops.h>
#include <esp_lvgl_port.h>
static const char* TAG = "ST7305";
bool St7305Display::createIoHandle(esp_lcd_panel_io_handle_t& outHandle) {
LOG_I(TAG, "Starting ST7305 SPI panel IO creation");
const esp_lcd_panel_io_spi_config_t panel_io_config = {
.cs_gpio_num = configuration->csPin,
.dc_gpio_num = configuration->dcPin,
.spi_mode = 0,
.pclk_hz = configuration->pixelClockFrequency,
.trans_queue_depth = configuration->transactionQueueDepth,
.on_color_trans_done = nullptr,
.user_ctx = nullptr,
.lcd_cmd_bits = 8,
.lcd_param_bits = 8,
.flags = {
.dc_high_on_cmd = 0,
.dc_low_on_data = 0,
.dc_low_on_param = 0,
.octal_mode = 0,
.quad_mode = 0,
.sio_mode = 0,
.lsb_first = 0,
.cs_high_active = 0
}
};
if (esp_lcd_new_panel_io_spi(configuration->spiHostDevice, &panel_io_config, &outHandle) != ESP_OK) {
LOG_E(TAG, "Failed to create panel SPI IO");
return false;
}
return true;
}
bool St7305Display::createPanelHandle(esp_lcd_panel_io_handle_t ioHandle, esp_lcd_panel_handle_t& panelHandle) {
const esp_lcd_panel_dev_config_t panel_config = {
.reset_gpio_num = configuration->resetPin,
.color_space = ESP_LCD_COLOR_SPACE_MONOCHROME,
.data_endian = LCD_RGB_DATA_ENDIAN_BIG,
.bits_per_pixel = 1,
.flags = {
.reset_active_high = false
},
.vendor_config = nullptr
};
if (esp_lcd_new_panel_st7305(ioHandle, &panel_config, &panelHandle) != ESP_OK) {
LOG_E(TAG, "Failed to create st7305 panel");
return false;
}
// ST7305 needs extra delay after reset before init — prevents freeze on fast boot
if (esp_lcd_panel_reset(panelHandle) != ESP_OK) {
LOG_E(TAG, "Failed to reset st7305 panel");
return false;
}
vTaskDelay(pdMS_TO_TICKS(150));
if (esp_lcd_panel_init(panelHandle) != ESP_OK) {
LOG_E(TAG, "Failed to init st7305 panel");
return false;
}
vTaskDelay(pdMS_TO_TICKS(50));
if (configuration->invertColor) {
if (esp_lcd_panel_invert_color(panelHandle, true) != ESP_OK) {
LOG_W(TAG, "Failed to apply initial invertColor");
}
}
return true;
}
lvgl_port_display_cfg_t St7305Display::getLvglPortDisplayConfig(esp_lcd_panel_io_handle_t ioHandle, esp_lcd_panel_handle_t panelHandle) {
return lvgl_port_display_cfg_t {
.io_handle = ioHandle,
.panel_handle = panelHandle,
.control_handle = nullptr,
.buffer_size = configuration->bufferSize,
.double_buffer = false, // Monochrome displays usually use single buffering to save RAM
.trans_size = 0,
.hres = configuration->horizontalResolution,
.vres = configuration->verticalResolution,
.monochrome = true, // Enables esp_lvgl_port monochrome converter
.rotation = {
.swap_xy = false,
.mirror_x = true,
.mirror_y = false,
},
.color_format = LV_COLOR_FORMAT_RGB565, // Must be RGB565 for monochrome mode to trigger converter
.flags = {
.buff_dma = false,
.buff_spiram = false,
.sw_rotate = false,
.swap_bytes = false,
.full_refresh = true, // We want full refresh to rewrite the converted block format to ST7305
.direct_mode = false
}
};
}
+100
View File
@@ -0,0 +1,100 @@
#pragma once
#include <EspLcdDisplay.h>
#include <Tactility/hal/display/DisplayDevice.h>
#include <driver/gpio.h>
#include <driver/spi_common.h>
#include <esp_lcd_panel_io.h>
#include <esp_lcd_types.h>
#include <functional>
#include <lvgl.h>
class St7305Display final : public EspLcdDisplay {
public:
class Configuration {
public:
Configuration(
spi_host_device_t spiHostDevice,
gpio_num_t csPin,
gpio_num_t dcPin,
unsigned int horizontalResolution,
unsigned int verticalResolution,
std::shared_ptr<tt::hal::touch::TouchDevice> touch = nullptr,
bool swapXY = false,
bool mirrorX = false,
bool mirrorY = false,
bool invertColor = false,
uint32_t bufferSize = 0
) : spiHostDevice(spiHostDevice),
csPin(csPin),
dcPin(dcPin),
horizontalResolution(horizontalResolution),
verticalResolution(verticalResolution),
swapXY(swapXY),
mirrorX(mirrorX),
mirrorY(mirrorY),
invertColor(invertColor),
bufferSize(bufferSize),
touch(std::move(touch))
{
if (this->bufferSize == 0) {
// For monochrome display, full pixel count is expected for buffer size
this->bufferSize = horizontalResolution * verticalResolution;
}
}
spi_host_device_t spiHostDevice;
gpio_num_t csPin;
gpio_num_t dcPin;
gpio_num_t resetPin = GPIO_NUM_NC;
unsigned int pixelClockFrequency = 10'000'000; // 10MHz SPI clock for ST7305
size_t transactionQueueDepth = 10;
unsigned int horizontalResolution;
unsigned int verticalResolution;
bool swapXY = false;
bool mirrorX = false;
bool mirrorY = false;
bool invertColor = false;
uint32_t bufferSize = 0;
std::shared_ptr<tt::hal::touch::TouchDevice> touch;
std::function<void(uint8_t)> _Nullable backlightDutyFunction = nullptr;
};
private:
std::unique_ptr<Configuration> configuration;
bool createIoHandle(esp_lcd_panel_io_handle_t& ioHandle) override;
bool createPanelHandle(esp_lcd_panel_io_handle_t ioHandle, esp_lcd_panel_handle_t& panelHandle) override;
lvgl_port_display_cfg_t getLvglPortDisplayConfig(esp_lcd_panel_io_handle_t ioHandle, esp_lcd_panel_handle_t panelHandle) override;
public:
explicit St7305Display(std::unique_ptr<Configuration> inConfiguration) :
configuration(std::move(inConfiguration))
{}
std::string getName() const override { return "ST7305"; }
std::string getDescription() const override { return "ST7305 monochrome reflective LCD display"; }
std::shared_ptr<tt::hal::touch::TouchDevice> _Nullable getTouchDevice() override { return configuration->touch; }
void setBacklightDuty(uint8_t backlightDuty) override {
if (configuration->backlightDutyFunction != nullptr) {
configuration->backlightDutyFunction(backlightDuty);
}
}
bool supportsBacklightDuty() const override { return configuration->backlightDutyFunction != nullptr; }
};
std::shared_ptr<tt::hal::display::DisplayDevice> createDisplay();
+300
View File
@@ -0,0 +1,300 @@
#include "esp_lcd_st7305.h"
#include "soc/soc_caps.h"
#include "esp_check.h"
#include "esp_lcd_types.h"
#include <stdlib.h>
#include <sys/cdefs.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_lcd_panel_interface.h"
#include "esp_lcd_panel_io.h"
#include "esp_lcd_panel_vendor.h"
#include "esp_lcd_panel_ops.h"
#include "esp_lcd_panel_commands.h"
#include "driver/gpio.h"
#include <string.h>
#include "esp_log.h"
static const char *TAG = "st7305";
static esp_err_t panel_st7305_del(esp_lcd_panel_t *panel);
static esp_err_t panel_st7305_reset(esp_lcd_panel_t *panel);
static esp_err_t panel_st7305_init(esp_lcd_panel_t *panel);
static esp_err_t panel_st7305_draw_bitmap(esp_lcd_panel_t *panel, int x_start, int y_start, int x_end, int y_end, const void *color_data);
static esp_err_t panel_st7305_invert_color(esp_lcd_panel_t *panel, bool invert_color_data);
static esp_err_t panel_st7305_mirror(esp_lcd_panel_t *panel, bool mirror_x, bool mirror_y);
static esp_err_t panel_st7305_swap_xy(esp_lcd_panel_t *panel, bool swap_axes);
static esp_err_t panel_st7305_set_gap(esp_lcd_panel_t *panel, int x_gap, int y_gap);
static esp_err_t panel_st7305_disp_on_off(esp_lcd_panel_t *panel, bool off);
typedef struct {
esp_lcd_panel_t base;
esp_lcd_panel_io_handle_t io;
int reset_gpio_num;
bool reset_level;
int x_gap;
int y_gap;
int width;
int height;
uint8_t rotation;
uint8_t madctl_val;
const st7305_lcd_init_cmd_t *init_cmds;
uint16_t init_cmds_size;
uint8_t *draw_buffer;
} st7305_panel_t;
static const st7305_lcd_init_cmd_t st7305_init_cmds[] = {
{0xD6, (uint8_t[]){0x17, 0x02}, 2, 0}, // NVM Load Control
{0xD1, (uint8_t[]){0x01}, 1, 0}, // Booster Enable
{0xC0, (uint8_t[]){0x11, 0x04}, 2, 0}, // Gate Voltage Setting
{0xC1, (uint8_t[]){0x41, 0x41, 0x41, 0x41}, 4, 0}, // VSHP Setting
{0xC2, (uint8_t[]){0x19, 0x19, 0x19, 0x19}, 4, 0}, // VSLP Setting
{0xC4, (uint8_t[]){0x41, 0x41, 0x41, 0x41}, 4, 0}, // VSHN Setting
{0xC5, (uint8_t[]){0x19, 0x13, 0x19, 0x19}, 4, 0}, // VSLN Setting
{0xD8, (uint8_t[]){0xA6, 0xE9}, 2, 0}, // OSC Setting
{0xB2, (uint8_t[]){0x05}, 1, 0}, // Frame Rate Control
{0xB3, (uint8_t[]){0xE5, 0xF6, 0x05, 0x46, 0x77, 0x77, 0x77, 0x77, 0x76, 0x45}, 10, 0}, // Gate EQ HPM
{0xB4, (uint8_t[]){0x05, 0x46, 0x77, 0x77, 0x77, 0x77, 0x76, 0x45}, 8, 0}, // Gate EQ LPM
{0x62, (uint8_t[]){0x32, 0x03, 0x1F}, 3, 0}, // Gate Timing Control
{0xB7, (uint8_t[]){0x13}, 1, 0}, // Source EQ Enable
{0xB0, (uint8_t[]){0x64}, 1, 0}, // Gate Line Setting: 300 line (0x64 = 100 * 3)
{0x11, NULL, 0, 200}, // Sleep out
{0xC9, (uint8_t[]){0x00}, 1, 0}, // Source Voltage Select
{0x36, (uint8_t[]){0x48}, 1, 0}, // Memory Data Access Control (MX=1, DO=1)
{0x3A, (uint8_t[]){0x11}, 1, 0}, // Data Format Select: 1bpp
{0xB9, (uint8_t[]){0x20}, 1, 0}, // Gamma Mode Setting
{0xB8, (uint8_t[]){0x29}, 1, 0}, // Panel Setting
{0x21, NULL, 0, 0}, // Display Inversion On
{0x2A, (uint8_t[]){0x12, 0x2A}, 2, 0}, // Column Address Setting
{0x2B, (uint8_t[]){0x00, 0xC7}, 2, 0}, // Row Address Setting
{0x35, (uint8_t[]){0x00}, 1, 0}, // TE Line
{0xD0, (uint8_t[]){0xFF}, 1, 0}, // Auto power down ON
{0x38, NULL, 0, 0}, // High Power Mode ON
{0x29, NULL, 0, 100}, // Display ON
};
esp_err_t esp_lcd_new_panel_st7305(const esp_lcd_panel_io_handle_t io, const esp_lcd_panel_dev_config_t *panel_dev_config,
esp_lcd_panel_handle_t *ret_panel)
{
esp_err_t ret = ESP_OK;
st7305_panel_t *st7305 = NULL;
gpio_config_t io_conf = { 0 };
ESP_GOTO_ON_FALSE(io && panel_dev_config && ret_panel, ESP_ERR_INVALID_ARG, err, TAG, "invalid argument");
st7305 = (st7305_panel_t *)calloc(1, sizeof(st7305_panel_t));
ESP_GOTO_ON_FALSE(st7305, ESP_ERR_NO_MEM, err, TAG, "no mem for st7305 panel");
if (panel_dev_config->reset_gpio_num >= 0) {
io_conf.mode = GPIO_MODE_OUTPUT;
io_conf.pin_bit_mask = 1ULL << panel_dev_config->reset_gpio_num;
ESP_GOTO_ON_ERROR(gpio_config(&io_conf), err, TAG, "configure GPIO for RST line failed");
}
st7305->width = ST7305_WIDTH;
st7305->height = ST7305_HEIGHT;
st7305->madctl_val = 0x48; // MX=1, DO=1
st7305->rotation = 0;
st7305->io = io;
st7305->reset_gpio_num = panel_dev_config->reset_gpio_num;
st7305->reset_level = panel_dev_config->flags.reset_active_high;
if (panel_dev_config->vendor_config) {
st7305->init_cmds = ((st7305_vendor_config_t *)panel_dev_config->vendor_config)->init_cmds;
st7305->init_cmds_size = ((st7305_vendor_config_t *)panel_dev_config->vendor_config)->init_cmds_size;
} else {
st7305->init_cmds = st7305_init_cmds;
st7305->init_cmds_size = sizeof(st7305_init_cmds) / sizeof(st7305_lcd_init_cmd_t);
}
st7305->draw_buffer = heap_caps_malloc(15000, MALLOC_CAP_DMA);
ESP_GOTO_ON_FALSE(st7305->draw_buffer, ESP_ERR_NO_MEM, err, TAG, "no mem for st7305 draw buffer");
memset(st7305->draw_buffer, 0, 15000);
st7305->base.del = panel_st7305_del;
st7305->base.reset = panel_st7305_reset;
st7305->base.init = panel_st7305_init;
st7305->base.draw_bitmap = panel_st7305_draw_bitmap;
st7305->base.invert_color = panel_st7305_invert_color;
st7305->base.set_gap = panel_st7305_set_gap;
st7305->base.mirror = panel_st7305_mirror;
st7305->base.swap_xy = panel_st7305_swap_xy;
#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0, 0)
st7305->base.disp_off = panel_st7305_disp_on_off;
#else
st7305->base.disp_on_off = panel_st7305_disp_on_off;
#endif
*ret_panel = &(st7305->base);
ESP_LOGD(TAG, "new st7305 panel @%p", st7305);
return ESP_OK;
err:
if (st7305) {
if (panel_dev_config->reset_gpio_num >= 0) {
gpio_reset_pin(panel_dev_config->reset_gpio_num);
}
if (st7305->draw_buffer) {
free(st7305->draw_buffer);
}
free(st7305);
}
return ret;
}
static esp_err_t panel_st7305_del(esp_lcd_panel_t *panel)
{
st7305_panel_t *st7305 = __containerof(panel, st7305_panel_t, base);
if (st7305->reset_gpio_num >= 0) {
gpio_reset_pin(st7305->reset_gpio_num);
}
if (st7305->draw_buffer) {
free(st7305->draw_buffer);
}
free(st7305);
return ESP_OK;
}
static esp_err_t panel_st7305_reset(esp_lcd_panel_t *panel)
{
st7305_panel_t *st7305 = __containerof(panel, st7305_panel_t, base);
if (st7305->reset_gpio_num >= 0) {
gpio_set_level(st7305->reset_gpio_num, !st7305->reset_level);
vTaskDelay(pdMS_TO_TICKS(50));
gpio_set_level(st7305->reset_gpio_num, st7305->reset_level);
vTaskDelay(pdMS_TO_TICKS(20));
gpio_set_level(st7305->reset_gpio_num, !st7305->reset_level);
vTaskDelay(pdMS_TO_TICKS(50));
}
return ESP_OK;
}
static esp_err_t panel_st7305_init(esp_lcd_panel_t *panel)
{
st7305_panel_t *st7305 = __containerof(panel, st7305_panel_t, base);
esp_lcd_panel_io_handle_t io = st7305->io;
for (size_t i = 0; i < st7305->init_cmds_size; i++) {
if (st7305->init_cmds[i].data_bytes > 0) {
esp_lcd_panel_io_tx_param(io, st7305->init_cmds[i].cmd,
st7305->init_cmds[i].data,
st7305->init_cmds[i].data_bytes);
} else {
esp_lcd_panel_io_tx_param(io, st7305->init_cmds[i].cmd, NULL, 0);
}
if (st7305->init_cmds[i].delay_ms > 0) {
vTaskDelay(pdMS_TO_TICKS(st7305->init_cmds[i].delay_ms));
}
}
// Explicitly clear display RAM to white
if (st7305->draw_buffer) {
memset(st7305->draw_buffer, 0xFF, 15000); // 0xFF is White
uint8_t caset[] = {0x12, 0x2A};
uint8_t raset[] = {0x00, 0xC7};
esp_lcd_panel_io_tx_param(io, ST7305_CMD_CASET, caset, sizeof(caset));
esp_lcd_panel_io_tx_param(io, ST7305_CMD_RASET, raset, sizeof(raset));
esp_lcd_panel_io_tx_color(io, ST7305_CMD_RAMWR, st7305->draw_buffer, 15000);
vTaskDelay(pdMS_TO_TICKS(50));
}
return ESP_OK;
}
static esp_err_t panel_st7305_draw_bitmap(esp_lcd_panel_t *panel, int x_start, int y_start, int x_end, int y_end, const void *color_data)
{
st7305_panel_t *st7305 = __containerof(panel, st7305_panel_t, base);
esp_lcd_panel_io_handle_t io = st7305->io;
const uint8_t *src = (const uint8_t *)color_data;
// Convert from vertical-page format (SSD1306-style) to ST7305 landscape 2x4 block format
for (int y = 0; y < 300; y++) {
int inv_y = 299 - y;
int block_y = inv_y >> 2;
int local_y = inv_y & 3;
int src_y_div_8 = y >> 3;
int src_y_mod_8 = y & 7;
uint8_t src_bit_mask = 1 << src_y_mod_8;
for (int x = 0; x < 400; x++) {
int byte_x = x >> 1;
int local_x = x & 1;
int dest_byte_idx = byte_x * 75 + block_y;
int dest_bit_pos = 7 - ((local_y << 1) | local_x);
int src_byte_idx = 400 * src_y_div_8 + x;
// Read standard vertical page byte bit
bool is_pixel_set = (src[src_byte_idx] & src_bit_mask) != 0;
// Invert the pixel logic if required, standard:
// esp_lvgl_port monochrome transform clears the bit (0) for light/chroma colors
// and sets the bit (1) for dark/black.
// In ST7305 display RAM, White/Light is 1, Black/Dark is 0.
// So we write: 1 (White) if is_pixel_set is false (light), and 0 (Black) if is_pixel_set is true (dark).
if (!is_pixel_set) {
st7305->draw_buffer[dest_byte_idx] |= (1 << dest_bit_pos);
} else {
st7305->draw_buffer[dest_byte_idx] &= ~(1 << dest_bit_pos);
}
}
}
uint8_t caset[] = {0x12, 0x2A};
uint8_t raset[] = {0x00, 0xC7};
esp_lcd_panel_io_tx_param(io, ST7305_CMD_CASET, caset, sizeof(caset));
esp_lcd_panel_io_tx_param(io, ST7305_CMD_RASET, raset, sizeof(raset));
esp_lcd_panel_io_tx_color(io, ST7305_CMD_RAMWR, st7305->draw_buffer, 15000);
return ESP_OK;
}
static esp_err_t panel_st7305_invert_color(esp_lcd_panel_t *panel, bool invert_color_data)
{
st7305_panel_t *st7305 = __containerof(panel, st7305_panel_t, base);
return esp_lcd_panel_io_tx_param(st7305->io, invert_color_data ? ST7305_CMD_INVON : ST7305_CMD_INVOFF, NULL, 0);
}
static esp_err_t panel_st7305_mirror(esp_lcd_panel_t *panel, bool mirror_x, bool mirror_y)
{
st7305_panel_t *st7305 = __containerof(panel, st7305_panel_t, base);
esp_lcd_panel_io_handle_t io = st7305->io;
if (mirror_x) {
st7305->madctl_val |= ST7305_MADCTL_MX;
} else {
st7305->madctl_val &= ~ST7305_MADCTL_MX;
}
if (mirror_y) {
st7305->madctl_val |= ST7305_MADCTL_MY;
} else {
st7305->madctl_val &= ~ST7305_MADCTL_MY;
}
return esp_lcd_panel_io_tx_param(io, ST7305_CMD_MADCTL, &st7305->madctl_val, 1);
}
static esp_err_t panel_st7305_swap_xy(esp_lcd_panel_t *panel, bool swap_axes)
{
st7305_panel_t *st7305 = __containerof(panel, st7305_panel_t, base);
esp_lcd_panel_io_handle_t io = st7305->io;
if (swap_axes) {
st7305->madctl_val |= ST7305_MADCTL_MV;
} else {
st7305->madctl_val &= ~ST7305_MADCTL_MV;
}
return esp_lcd_panel_io_tx_param(io, ST7305_CMD_MADCTL, (uint8_t[]) { st7305->madctl_val }, 1);
}
static esp_err_t panel_st7305_set_gap(esp_lcd_panel_t *panel, int x_gap, int y_gap)
{
st7305_panel_t *st7305 = __containerof(panel, st7305_panel_t, base);
st7305->x_gap = x_gap;
st7305->y_gap = y_gap;
return ESP_OK;
}
static esp_err_t panel_st7305_disp_on_off(esp_lcd_panel_t *panel, bool off)
{
st7305_panel_t *st7305 = __containerof(panel, st7305_panel_t, base);
return esp_lcd_panel_io_tx_param(st7305->io, off ? ST7305_CMD_DISPOFF : ST7305_CMD_DISPON, NULL, 0);
}
+49
View File
@@ -0,0 +1,49 @@
#pragma once
#include <stdint.h>
#include "esp_lcd_types.h"
#include "esp_lcd_panel_vendor.h"
#include "sdkconfig.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef struct {
int cmd; /*<! The specific LCD command */
const void *data; /*<! Buffer that holds the command specific data */
size_t data_bytes; /*<! Size of `data` in memory, in bytes */
unsigned int delay_ms; /*<! Delay in milliseconds after this command */
} st7305_lcd_init_cmd_t;
typedef struct {
const st7305_lcd_init_cmd_t *init_cmds;
uint16_t init_cmds_size;
} st7305_vendor_config_t;
#define ST7305_WIDTH 400
#define ST7305_HEIGHT 300
#define ST7305_CMD_NOP 0x00
#define ST7305_CMD_SWRESET 0x01
#define ST7305_CMD_SLPOUT 0x11
#define ST7305_CMD_NORON 0x13
#define ST7305_CMD_INVOFF 0x20
#define ST7305_CMD_INVON 0x21
#define ST7305_CMD_DISPOFF 0x28
#define ST7305_CMD_DISPON 0x29
#define ST7305_CMD_CASET 0x2A
#define ST7305_CMD_RASET 0x2B
#define ST7305_CMD_RAMWR 0x2C
#define ST7305_CMD_MADCTL 0x36
#define ST7305_MADCTL_MY 0x80
#define ST7305_MADCTL_MX 0x40
#define ST7305_MADCTL_MV 0x20
#define ST7305_MADCTL_ML 0x10
esp_err_t esp_lcd_new_panel_st7305(const esp_lcd_panel_io_handle_t io, const esp_lcd_panel_dev_config_t *panel_dev_config, esp_lcd_panel_handle_t *ret_panel);
#ifdef __cplusplus
}
#endif
-3
View File
@@ -1,3 +0,0 @@
# ST7789
A basic ESP32 LVGL driver for ST7789 parallel i8080 displays.
@@ -1,345 +0,0 @@
#include "St7789i8080Display.h"
#include <tactility/log.h>
#include <driver/gpio.h>
#include <esp_lcd_panel_io.h>
#include <esp_lcd_panel_ops.h>
#include <esp_lvgl_port.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <lvgl.h>
constexpr auto* TAG = "St7789i8080Display";
static St7789i8080Display* g_display_instance = nullptr;
// ST7789 initialization commands
typedef struct {
uint8_t cmd;
uint8_t data[14];
uint8_t len;
} lcd_init_cmd_t;
static const lcd_init_cmd_t st7789_init_cmds[] = {
{0x11, {0}, 0 | 0x80},
{0x36, {0x08}, 1},
{0x3A, {0X05}, 1},
{0x20, {0}, 0},
{0xB2, {0X0B, 0X0B, 0X00, 0X33, 0X33}, 5},
{0xB7, {0X75}, 1},
{0xBB, {0X28}, 1},
{0xC0, {0X2C}, 1},
{0xC2, {0X01}, 1},
{0xC3, {0X1F}, 1},
{0xC6, {0X13}, 1},
{0xD0, {0XA7}, 1},
{0xD0, {0XA4, 0XA1}, 2},
{0xD6, {0XA1}, 1},
{0xE0, {0XF0, 0X05, 0X0A, 0X06, 0X06, 0X03, 0X2B, 0X32, 0X43, 0X36, 0X11, 0X10, 0X2B, 0X32}, 14},
{0xE1, {0XF0, 0X08, 0X0C, 0X0B, 0X09, 0X24, 0X2B, 0X22, 0X43, 0X38, 0X15, 0X16, 0X2F, 0X37}, 14},
};
// Callback when color transfer is done
static bool notify_lvgl_flush_ready(esp_lcd_panel_io_handle_t panel_io,
esp_lcd_panel_io_event_data_t *edata,
void *user_ctx) {
lv_display_t *disp = (lv_display_t *)user_ctx;
lv_display_flush_ready(disp);
return false;
}
St7789i8080Display::St7789i8080Display(const Configuration& config)
: configuration(config), lock(std::make_shared<std::mutex>()) {
// Validate configuration
if (!configuration.isValid()) {
LOG_E(TAG, "Invalid configuration: resolution must be set");
return;
}
}
bool St7789i8080Display::createI80Bus() {
LOG_I(TAG, "Creating I80 bus");
// Create I80 bus configuration
esp_lcd_i80_bus_config_t bus_cfg = {
.dc_gpio_num = configuration.dcPin,
.wr_gpio_num = configuration.wrPin,
.clk_src = LCD_CLK_SRC_DEFAULT,
.data_gpio_nums = {
configuration.dataPins[0], configuration.dataPins[1],
configuration.dataPins[2], configuration.dataPins[3],
configuration.dataPins[4], configuration.dataPins[5],
configuration.dataPins[6], configuration.dataPins[7],
},
.bus_width = configuration.busWidth,
.max_transfer_bytes = configuration.bufferSize * sizeof(uint16_t),
.psram_trans_align = 64,
.sram_trans_align = 4
};
esp_err_t ret = esp_lcd_new_i80_bus(&bus_cfg, &i80BusHandle);
if (ret != ESP_OK) {
LOG_E(TAG, "Failed to create I80 bus: %s", esp_err_to_name(ret));
return false;
}
return true;
}
bool St7789i8080Display::createPanelIO() {
LOG_I(TAG, "Creating panel IO");
// Create panel IO with proper callback
esp_lcd_panel_io_i80_config_t io_cfg = {
.cs_gpio_num = configuration.csPin,
.pclk_hz = configuration.pixelClockFrequency,
.trans_queue_depth = configuration.transactionQueueDepth,
.on_color_trans_done = notify_lvgl_flush_ready, // Use proper callback
.user_ctx = nullptr, // Will be set when LVGL display is created
.lcd_cmd_bits = configuration.lcdCmdBits,
.lcd_param_bits = configuration.lcdParamBits,
.dc_levels = {
.dc_idle_level = configuration.dcLevels.dcIdleLevel,
.dc_cmd_level = configuration.dcLevels.dcCmdLevel,
.dc_dummy_level = configuration.dcLevels.dcDummyLevel,
.dc_data_level = configuration.dcLevels.dcDataLevel,
},
.flags = {
.cs_active_high = configuration.flags.csActiveHigh,
.reverse_color_bits = configuration.flags.reverseColorBits,
.swap_color_bytes = configuration.flags.swapColorBytes,
.pclk_active_neg = configuration.flags.pclkActiveNeg,
.pclk_idle_low = configuration.flags.pclkIdleLow
}
};
esp_err_t ret = esp_lcd_new_panel_io_i80(i80BusHandle, &io_cfg, &ioHandle);
if (ret != ESP_OK) {
LOG_E(TAG, "Failed to create panel IO: %s", esp_err_to_name(ret));
return false;
}
return true;
}
bool St7789i8080Display::createPanel() {
LOG_I(TAG, "Configuring panel");
// Create ST7789 panel
esp_lcd_panel_dev_config_t panel_config = {
.reset_gpio_num = configuration.resetPin,
.rgb_ele_order = LCD_RGB_ELEMENT_ORDER_RGB,
.data_endian = LCD_RGB_DATA_ENDIAN_LITTLE,
.bits_per_pixel = 16,
.flags = {
.reset_active_high = false
},
.vendor_config = nullptr
};
esp_err_t ret = esp_lcd_new_panel_st7789(ioHandle, &panel_config, &panelHandle);
if (ret != ESP_OK) {
LOG_E(TAG, "Failed to create ST7789 panel: %s", esp_err_to_name(ret));
return false;
}
// Reset panel
ret = esp_lcd_panel_reset(panelHandle);
if (ret != ESP_OK) {
LOG_E(TAG, "Failed to reset panel: %s", esp_err_to_name(ret));
return false;
}
// Initialize panel
ret = esp_lcd_panel_init(panelHandle);
if (ret != ESP_OK) {
LOG_E(TAG, "Failed to init panel: %s", esp_err_to_name(ret));
return false;
}
// Set gap
ret = esp_lcd_panel_set_gap(panelHandle, configuration.gapX, configuration.gapY);
if (ret != ESP_OK) {
LOG_E(TAG, "Failed to set panel gap: %s", esp_err_to_name(ret));
return false;
}
// Set inversion
ret = esp_lcd_panel_invert_color(panelHandle, configuration.invertColor);
if (ret != ESP_OK) {
LOG_E(TAG, "Failed to set panel inversion: %s", esp_err_to_name(ret));
return false;
}
// Set mirror
ret = esp_lcd_panel_mirror(panelHandle, configuration.mirrorX, configuration.mirrorY);
if (ret != ESP_OK) {
LOG_E(TAG, "Failed to set panel mirror: %s", esp_err_to_name(ret));
return false;
}
// Turn on display
ret = esp_lcd_panel_disp_on_off(panelHandle, true);
if (ret != ESP_OK) {
LOG_E(TAG, "Failed to turn display on: %s", esp_err_to_name(ret));
return false;
}
return true;
}
void St7789i8080Display::sendInitCommands() {
LOG_I(TAG, "Sending ST7789 init commands");
for (const auto& cmd : st7789_init_cmds) {
esp_lcd_panel_io_tx_param(ioHandle, cmd.cmd, cmd.data, cmd.len & 0x7F);
if (cmd.len & 0x80) {
vTaskDelay(pdMS_TO_TICKS(120));
}
}
}
bool St7789i8080Display::start() {
LOG_I(TAG, "Initializing I8080 ST7789 Display hardware...");
// Configure RD pin if needed
if (configuration.rdPin != GPIO_NUM_NC) {
gpio_config_t rd_gpio_config = {
.pin_bit_mask = (1ULL << static_cast<uint32_t>(configuration.rdPin)),
.mode = GPIO_MODE_OUTPUT,
.pull_up_en = GPIO_PULLUP_DISABLE,
.pull_down_en = GPIO_PULLDOWN_DISABLE,
.intr_type = GPIO_INTR_DISABLE,
};
gpio_config(&rd_gpio_config);
gpio_set_level(configuration.rdPin, 1);
}
// Calculate buffer size if needed
configuration.calculateBufferSize();
// Allocate buffer based on resolution
size_t buffer_size = configuration.bufferSize * LV_COLOR_FORMAT_GET_SIZE(LV_COLOR_FORMAT_RGB565);
buf1 = (uint8_t*)heap_caps_malloc(buffer_size, MALLOC_CAP_DMA);
if (!buf1) {
LOG_E(TAG, "Failed to allocate display buffer");
return false;
}
// Create I80 bus
if (!createI80Bus()) {
return false;
}
// Create panel IO
if (!createPanelIO()) {
return false;
}
// Create panel
if (!createPanel()) {
return false;
}
LOG_I(TAG, "Display hardware initialized");
return true;
}
bool St7789i8080Display::stop() {
// Turn off display
if (panelHandle) {
esp_lcd_panel_disp_on_off(panelHandle, false);
}
// Destroy in reverse order: panel, IO, bus
if (panelHandle) {
esp_lcd_panel_del(panelHandle);
panelHandle = nullptr;
}
if (ioHandle) {
esp_lcd_panel_io_del(ioHandle);
ioHandle = nullptr;
}
if (i80BusHandle) {
esp_lcd_del_i80_bus(i80BusHandle);
i80BusHandle = nullptr;
}
// Free buffer
if (buf1) {
heap_caps_free(buf1);
buf1 = nullptr;
}
// Turn off backlight
if (configuration.backlightDutyFunction) {
configuration.backlightDutyFunction(0);
}
return true;
}
bool St7789i8080Display::startLvgl() {
LOG_I(TAG, "Initializing LVGL for ST7789 display");
// Don't reinitialize hardware if it's already done
if (!ioHandle) {
LOG_I(TAG, "Hardware not initialized, calling start()");
if (!start()) {
LOG_E(TAG, "Hardware initialization failed");
return false;
}
} else {
LOG_I(TAG, "Hardware already initialized, skipping");
}
// Create LVGL display using lvgl_port
lvgl_port_display_cfg_t display_cfg = {
.io_handle = ioHandle,
.panel_handle = panelHandle,
.control_handle = nullptr,
.buffer_size = configuration.bufferSize,
.double_buffer = false,
.trans_size = 0,
.hres = configuration.horizontalResolution,
.vres = configuration.verticalResolution,
.monochrome = false,
.rotation = {
.swap_xy = configuration.swapXY,
.mirror_x = configuration.mirrorX,
.mirror_y = configuration.mirrorY,
},
.color_format = LV_COLOR_FORMAT_RGB565,
.flags = {
.buff_dma = true,
.buff_spiram = false,
.sw_rotate = true,
.swap_bytes = true,
.full_refresh = false,
.direct_mode = false
}
};
// Create the LVGL display
lvglDisplay = lvgl_port_add_disp(&display_cfg);
if (!lvglDisplay) {
LOG_E(TAG, "Failed to create LVGL display");
return false;
}
// Register the callback for color transfer completion
esp_lcd_panel_io_callbacks_t cbs = {
.on_color_trans_done = notify_lvgl_flush_ready,
};
esp_lcd_panel_io_register_event_callbacks(ioHandle, &cbs, lvglDisplay);
g_display_instance = this;
LOG_I(TAG, "LVGL display created successfully");
return true;
}
bool St7789i8080Display::stopLvgl() {
if (lvglDisplay) {
lvgl_port_remove_disp(lvglDisplay);
lvglDisplay = nullptr;
}
return true;
}
@@ -1,139 +0,0 @@
#pragma once
#include <Tactility/hal/display/DisplayDevice.h>
#include <esp_lcd_panel_io.h>
#include <esp_lcd_types.h>
#include <esp_lcd_panel_st7789.h>
#include <array>
#include <memory>
#include <functional>
#include <mutex>
class St7789i8080Display : public tt::hal::display::DisplayDevice {
public:
struct Configuration {
// Pin configuration
gpio_num_t csPin;
gpio_num_t dcPin;
gpio_num_t wrPin;
gpio_num_t rdPin;
std::array<gpio_num_t, 8> dataPins;
gpio_num_t resetPin;
gpio_num_t backlightPin;
// Display resolution configuration
uint16_t horizontalResolution;
uint16_t verticalResolution;
// Bus configuration
unsigned int pixelClockFrequency = 16 * 1000 * 1000; // 16MHz default
size_t busWidth = 8; // 8-bit bus
size_t transactionQueueDepth = 40;
size_t bufferSize = 0; // Will be calculated if 0
// LCD command/parameter configuration
int lcdCmdBits = 8;
int lcdParamBits = 8;
// DC line level configuration
struct {
bool dcIdleLevel = 0;
bool dcCmdLevel = 0;
bool dcDummyLevel = 0;
bool dcDataLevel = 1;
} dcLevels;
// Bus flags
struct {
bool csActiveHigh = false;
bool reverseColorBits = false;
bool swapColorBytes = true;
bool pclkActiveNeg = false;
bool pclkIdleLow = false;
} flags;
// Display configuration
int gapX = 0;
int gapY = 0;
bool swapXY = false;
bool mirrorX = false;
bool mirrorY = false;
bool invertColor = true;
// Additional features
std::shared_ptr<tt::hal::touch::TouchDevice> touch = nullptr;
std::function<void(uint8_t)> backlightDutyFunction = nullptr;
// Basic constructor - requires resolution to be set separately
Configuration(gpio_num_t cs, gpio_num_t dc, gpio_num_t wr, gpio_num_t rd,
std::array<gpio_num_t, 8> data, gpio_num_t rst, gpio_num_t bl)
: csPin(cs), dcPin(dc), wrPin(wr), rdPin(rd),
dataPins(data), resetPin(rst), backlightPin(bl),
horizontalResolution(0), verticalResolution(0) {} // Initialize to 0
// Method to calculate buffer size after resolution is set
void calculateBufferSize() {
if (bufferSize == 0 && horizontalResolution > 0 && verticalResolution > 0) {
bufferSize = horizontalResolution * verticalResolution / 10;
}
}
// Validation method
bool isValid() const {
return horizontalResolution > 0 && verticalResolution > 0;
}
};
private:
Configuration configuration;
esp_lcd_i80_bus_handle_t i80BusHandle = nullptr;
esp_lcd_panel_io_handle_t ioHandle = nullptr;
esp_lcd_panel_handle_t panelHandle = nullptr;
lv_display_t* lvglDisplay = nullptr;
std::shared_ptr<std::mutex> lock;
uint8_t* buf1 = nullptr;
// Internal initialization methods
void sendInitCommands();
bool createI80Bus();
bool createPanelIO();
bool createPanel();
public:
explicit St7789i8080Display(const Configuration& config);
lv_display_t* getLvglDisplay() const override { return lvglDisplay; }
std::string getName() const override { return "I8080 ST7789"; }
std::string getDescription() const override { return "I8080-based ST7789 display"; }
// Lifecycle
bool start() override;
bool stop() override;
bool startLvgl() override;
bool stopLvgl() override;
// Capabilities
bool supportsLvgl() const override { return true; }
bool supportsDisplayDriver() const override { return false; }
bool supportsBacklightDuty() const override { return configuration.backlightDutyFunction != nullptr; }
// Touch and backlight
std::shared_ptr<tt::hal::touch::TouchDevice> getTouchDevice() override { return configuration.touch; }
std::shared_ptr<tt::hal::display::DisplayDriver> getDisplayDriver() override { return nullptr; }
void setBacklightDuty(uint8_t backlightDuty) override {
if (configuration.backlightDutyFunction != nullptr) {
configuration.backlightDutyFunction(backlightDuty);
}
}
// Hardware access methods
esp_lcd_panel_io_handle_t getIoHandle() const { return ioHandle; }
esp_lcd_panel_handle_t getPanelHandle() const { return panelHandle; }
// Resolution access methods
uint16_t getHorizontalResolution() const { return configuration.horizontalResolution; }
uint16_t getVerticalResolution() const { return configuration.verticalResolution; }
};
// Factory function for registration
std::shared_ptr<tt::hal::display::DisplayDevice> createDisplay();
@@ -553,9 +553,14 @@ error_t close_stream(AudioStreamHandle handle_base) {
Device* codec = is_input ? data->input_codec : data->output_codec;
AudioStreamHandleImpl** slot = is_input ? &data->open_input : &data->open_output;
// Determine if underlying codec is shared (BOTH codec used for both directions)
// In that case we must NOT close the codec if the other direction is still active.
Device* other_codec = is_input ? data->output_codec : data->input_codec;
AudioStreamHandleImpl** other_slot = is_input ? &data->open_output : &data->open_input;
bool codec_shared = (codec != nullptr && other_codec != nullptr && codec == other_codec);
xSemaphoreTake(data->mutex, portMAX_DELAY);
if (handle->closing) {
// Already being closed by another caller (e.g. concurrent set_enabled + app close).
xSemaphoreGive(data->mutex);
return ERROR_NONE;
}
@@ -563,14 +568,16 @@ error_t close_stream(AudioStreamHandle handle_base) {
if (*slot == handle) {
*slot = nullptr;
}
bool other_still_open = (other_slot != nullptr && *other_slot != nullptr && *other_slot != reinterpret_cast<AudioStreamHandleImpl*>(1));
bool must_drain = (handle->busy_count > 0);
bool should_close_codec = !codec_shared || !other_still_open;
xSemaphoreGive(data->mutex);
if (must_drain && handle->drain_semaphore != nullptr) {
xSemaphoreTake(handle->drain_semaphore, portMAX_DELAY);
}
if (codec != nullptr) {
if (should_close_codec && codec != nullptr) {
audio_codec_close(codec);
}
@@ -4,8 +4,8 @@ include("${CMAKE_CURRENT_LIST_DIR}/../../Buildscripts/module.cmake")
file(GLOB_RECURSE SOURCE_FILES "source/*.c*")
tactility_add_module(axs15231b-module
tactility_add_module(axp192-module
SRCS ${SOURCE_FILES}
INCLUDE_DIRS include/
REQUIRES TactilityKernel platform-esp32 esp_lcd_axs15231b driver
REQUIRES TactilityKernel
)
+13
View File
@@ -0,0 +1,13 @@
# AXP192 power management IC
A driver for the X-Powers `AXP192` PMIC: DCDC1-3/LDO2-3/EXTEN rail enable and voltage
control, battery voltage/current/power readback, charge enable/status, and system power off.
Registers as a `power-supply` child device (voltage, current, is-charging, charge control,
power off) alongside its own public API for full rail control.
Also includes `axp192-backlight`: a `BACKLIGHT_TYPE` driver for a backlight wired to one of
the AXP192's rails, declared as a devicetree child node of the `axp192` device it dims. Its
`rail`, `min-millivolt` and `max-millivolt` properties select the channel and map the 0-255
brightness level onto that rail's voltage range; brightness level 0 disables the rail outright.
License: [Apache v2.0](LICENSE-Apache-2.0.md)
@@ -0,0 +1,24 @@
description: >
Backlight driven by a switchable/adjustable AXP192 power rail. Must be declared as a
child node of the axp192 device it dims. Maps the 0-255 brightness level onto the
rail's [min-millivolt,max-millivolt] voltage range; level 0 disables the rail outright.
compatible: "axp192-backlight"
properties:
rail:
type: int
required: true
description: The Axp192Rail powering the backlight (e.g. AXP192_RAIL_LDO2)
min-millivolt:
type: int
default: 0
description: Rail voltage at brightness level 0 (the rail is disabled rather than actually set to this value)
max-millivolt:
type: int
required: true
description: Rail voltage at the maximum brightness level (255)
brightness-default:
type: int
default: 255
description: Default brightness level, applied by set_brightness_default()
@@ -0,0 +1,5 @@
description: X-Powers AXP192 power management IC
include: ["i2c-device.yaml"]
compatible: "x-powers,axp192"
@@ -7,7 +7,7 @@
extern "C" {
#endif
extern struct Module axs15231b_module;
extern struct Module axp192_module;
#ifdef __cplusplus
}
@@ -0,0 +1,15 @@
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include <tactility/bindings/bindings.h>
#include <drivers/axp192.h>
#ifdef __cplusplus
extern "C" {
#endif
DEFINE_DEVICETREE(axp192, struct Axp192Config)
#ifdef __cplusplus
}
#endif
@@ -0,0 +1,15 @@
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include <tactility/bindings/bindings.h>
#include <drivers/axp192_backlight.h>
#ifdef __cplusplus
extern "C" {
#endif
DEFINE_DEVICETREE(axp192_backlight, struct Axp192BacklightConfig)
#ifdef __cplusplus
}
#endif
@@ -0,0 +1,81 @@
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include <stdbool.h>
#include <stdint.h>
#include <tactility/error.h>
struct Device;
#ifdef __cplusplus
extern "C" {
#endif
struct Axp192Config {
/** Address on bus */
uint8_t address;
};
/** Switchable/adjustable power rails of the AXP192. */
enum Axp192Rail {
AXP192_RAIL_DCDC1,
AXP192_RAIL_DCDC2,
AXP192_RAIL_DCDC3,
AXP192_RAIL_LDO2,
AXP192_RAIL_LDO3,
/** EXTEN output switch; does not support voltage control. */
AXP192_RAIL_EXTEN,
};
/**
* @brief Checks whether a power rail is currently enabled.
*/
error_t axp192_is_rail_enabled(struct Device* device, enum Axp192Rail rail, bool* enabled);
/**
* @brief Enables or disables a power rail.
*/
error_t axp192_set_rail_enabled(struct Device* device, enum Axp192Rail rail, bool enabled);
/**
* @brief Sets the output voltage of a power rail.
* @retval ERROR_NOT_SUPPORTED for AXP192_RAIL_EXTEN, which has no voltage control
* @retval ERROR_INVALID_ARGUMENT when millivolts is outside the rail's supported range
* (DCDC1/DCDC3: 700-3500mV, DCDC2: 700-2275mV, LDO2/LDO3: 1800-3300mV)
*/
error_t axp192_set_rail_voltage(struct Device* device, enum Axp192Rail rail, uint16_t millivolts);
/** Battery voltage in millivolts. */
error_t axp192_get_battery_voltage(struct Device* device, uint16_t* millivolts);
/** Battery charge current in milliamps (0 when not charging). */
error_t axp192_get_battery_charge_current(struct Device* device, uint16_t* milliamps);
/** Battery discharge current in milliamps (0 when not discharging). */
error_t axp192_get_battery_discharge_current(struct Device* device, uint16_t* milliamps);
/** Battery power in microwatts. */
error_t axp192_get_battery_power(struct Device* device, uint32_t* microwatts);
/** Whether the battery is currently charging. */
error_t axp192_is_charging(struct Device* device, bool* charging);
/** Whether the charger is allowed to charge the battery. */
error_t axp192_is_charge_enabled(struct Device* device, bool* enabled);
/** Enables or disables battery charging. */
error_t axp192_set_charge_enabled(struct Device* device, bool enabled);
/** Powers off the system (does not return on success). */
error_t axp192_power_off(struct Device* device);
/** Configures GPIO1 as the PWM1 output (rather than GPIO/ADC input/output). */
error_t axp192_set_gpio1_pwm1_output(struct Device* device);
/** Sets the PWM1 duty cycle (0 = always low, 255 = always high). */
error_t axp192_set_pwm1_duty_cycle(struct Device* device, uint8_t duty);
#ifdef __cplusplus
}
#endif
@@ -0,0 +1,29 @@
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include <stdint.h>
#include <drivers/axp192.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Devicetree configuration for a backlight driven by a switchable/adjustable AXP192 power rail.
*/
struct Axp192BacklightConfig {
/** The AXP192 rail powering the backlight */
enum Axp192Rail rail;
/** Rail voltage at brightness level 0. Brightness level 0 always disables the rail outright,
* rather than actually driving it to this voltage - see set_brightness() in BacklightApi. */
uint16_t min_millivolt;
/** Rail voltage at the maximum brightness level (255) */
uint16_t max_millivolt;
/** Default brightness level, applied by set_brightness_default() */
uint8_t brightness_default;
};
#ifdef __cplusplus
}
#endif
+445
View File
@@ -0,0 +1,445 @@
// SPDX-License-Identifier: Apache-2.0
#include <drivers/axp192.h>
#include <axp192_module.h>
#include <tactility/check.h>
#include <tactility/device.h>
#include <tactility/driver.h>
#include <tactility/drivers/i2c_controller.h>
#include <tactility/drivers/power_supply.h>
#include <tactility/log.h>
#include <new>
#define GET_CONFIG(device) (static_cast<const Axp192Config*>((device)->config))
/** Reference: https://github.com/tuupola/axp192 (register map and ADC/voltage formulas) */
static constexpr uint8_t REG_MODE_CHGSTATUS = 0x01U; // bit6: battery is charging
static constexpr uint8_t REG_EXTEN_DCDC2_CONTROL = 0x10U; // bit2: EXTEN, bit0: DCDC2
static constexpr uint8_t REG_DCDC13_LDO23_CONTROL = 0x12U; // bit3: LDO3, bit2: LDO2, bit1: DCDC3, bit0: DCDC1
static constexpr uint8_t REG_DCDC2_VOLTAGE = 0x23U;
static constexpr uint8_t REG_DCDC1_VOLTAGE = 0x26U;
static constexpr uint8_t REG_DCDC3_VOLTAGE = 0x27U;
static constexpr uint8_t REG_LDO23_VOLTAGE = 0x28U; // bits7-4: LDO2, bits3-0: LDO3
static constexpr uint8_t REG_SHUTDOWN_BATTERY_CHGLED_CONTROL = 0x32U; // bit7: power off
static constexpr uint8_t REG_CHARGE_CONTROL_1 = 0x33U; // bit7: charging enabled
static constexpr uint8_t REG_BATTERY_POWER = 0x70U; // 3 bytes, 0.55uW/LSB
static constexpr uint8_t REG_BATTERY_VOLTAGE = 0x78U; // 2 bytes, 1.1mV/LSB
static constexpr uint8_t REG_CHARGE_CURRENT = 0x7AU; // 2 bytes, 0.5mA/LSB
static constexpr uint8_t REG_DISCHARGE_CURRENT = 0x7CU; // 2 bytes, 0.5mA/LSB
static constexpr uint8_t REG_GPIO1_CONTROL = 0x92U; // function select (0x02 = PWM1 output)
static constexpr uint8_t REG_PWM1_DUTY_CYCLE_2 = 0x9AU; // PWM1 duty cycle, low byte
static constexpr uint8_t GPIO1_FUNCTION_PWM1 = 0x02U;
static constexpr uint8_t BIT_CHARGING = 1U << 6U;
static constexpr uint8_t BIT_CHARGE_ENABLED = 1U << 7U;
static constexpr uint8_t BIT_POWER_OFF = 1U << 7U;
static constexpr TickType_t TIMEOUT = pdMS_TO_TICKS(50);
extern "C" {
extern Module axp192_module;
// region Register helpers
/** Reads a 2-byte ADC register in AXP192's packed 12-bit format: raw = (high << 4) + low. */
static error_t read_adc_raw(Device* device, uint8_t reg, uint16_t* raw) {
auto* parent = device_get_parent(device);
auto address = GET_CONFIG(device)->address;
uint8_t buffer[2];
error_t err = i2c_controller_read_register(parent, address, reg, buffer, sizeof(buffer), TIMEOUT);
if (err != ERROR_NONE) {
return err;
}
*raw = static_cast<uint16_t>((buffer[0] << 4U) + (buffer[1] & 0x0FU));
return ERROR_NONE;
}
static error_t get_rail_enable_bit(Axp192Rail rail, uint8_t* reg, uint8_t* bit) {
switch (rail) {
case AXP192_RAIL_DCDC1:
*reg = REG_DCDC13_LDO23_CONTROL;
*bit = 1U << 0U;
return ERROR_NONE;
case AXP192_RAIL_DCDC2:
*reg = REG_EXTEN_DCDC2_CONTROL;
*bit = 1U << 0U;
return ERROR_NONE;
case AXP192_RAIL_DCDC3:
*reg = REG_DCDC13_LDO23_CONTROL;
*bit = 1U << 1U;
return ERROR_NONE;
case AXP192_RAIL_LDO2:
*reg = REG_DCDC13_LDO23_CONTROL;
*bit = 1U << 2U;
return ERROR_NONE;
case AXP192_RAIL_LDO3:
*reg = REG_DCDC13_LDO23_CONTROL;
*bit = 1U << 3U;
return ERROR_NONE;
case AXP192_RAIL_EXTEN:
*reg = REG_EXTEN_DCDC2_CONTROL;
*bit = 1U << 2U;
return ERROR_NONE;
}
return ERROR_INVALID_ARGUMENT;
}
// endregion
error_t axp192_is_rail_enabled(Device* device, Axp192Rail rail, bool* enabled) {
uint8_t reg, bit;
error_t err = get_rail_enable_bit(rail, &reg, &bit);
if (err != ERROR_NONE) {
return err;
}
auto* parent = device_get_parent(device);
auto address = GET_CONFIG(device)->address;
uint8_t value;
err = i2c_controller_register8_get(parent, address, reg, &value, TIMEOUT);
if (err != ERROR_NONE) {
return err;
}
*enabled = (value & bit) != 0U;
return ERROR_NONE;
}
error_t axp192_set_rail_enabled(Device* device, Axp192Rail rail, bool enabled) {
uint8_t reg, bit;
error_t err = get_rail_enable_bit(rail, &reg, &bit);
if (err != ERROR_NONE) {
return err;
}
auto* parent = device_get_parent(device);
auto address = GET_CONFIG(device)->address;
if (enabled) {
return i2c_controller_register8_set_bits(parent, address, reg, bit, TIMEOUT);
} else {
return i2c_controller_register8_reset_bits(parent, address, reg, bit, TIMEOUT);
}
}
error_t axp192_set_rail_voltage(Device* device, Axp192Rail rail, uint16_t millivolts) {
auto* parent = device_get_parent(device);
auto address = GET_CONFIG(device)->address;
switch (rail) {
case AXP192_RAIL_DCDC1:
case AXP192_RAIL_DCDC3: {
if (millivolts < 700U || millivolts > 3500U) {
return ERROR_INVALID_ARGUMENT;
}
uint8_t reg = (rail == AXP192_RAIL_DCDC1) ? REG_DCDC1_VOLTAGE : REG_DCDC3_VOLTAGE;
uint8_t value = static_cast<uint8_t>((millivolts - 700U) / 25U);
return i2c_controller_register8_set(parent, address, reg, value, TIMEOUT);
}
case AXP192_RAIL_DCDC2: {
if (millivolts < 700U || millivolts > 2275U) {
return ERROR_INVALID_ARGUMENT;
}
uint8_t value = static_cast<uint8_t>((millivolts - 700U) / 25U);
return i2c_controller_register8_set(parent, address, REG_DCDC2_VOLTAGE, value, TIMEOUT);
}
case AXP192_RAIL_LDO2:
case AXP192_RAIL_LDO3: {
if (millivolts < 1800U || millivolts > 3300U) {
return ERROR_INVALID_ARGUMENT;
}
uint8_t step = static_cast<uint8_t>((millivolts - 1800U) / 100U);
uint8_t value;
error_t err = i2c_controller_register8_get(parent, address, REG_LDO23_VOLTAGE, &value, TIMEOUT);
if (err != ERROR_NONE) {
return err;
}
if (rail == AXP192_RAIL_LDO2) {
value = static_cast<uint8_t>((value & 0x0FU) | (step << 4U));
} else {
value = static_cast<uint8_t>((value & 0xF0U) | step);
}
return i2c_controller_register8_set(parent, address, REG_LDO23_VOLTAGE, value, TIMEOUT);
}
case AXP192_RAIL_EXTEN:
return ERROR_NOT_SUPPORTED;
}
return ERROR_INVALID_ARGUMENT;
}
error_t axp192_get_battery_voltage(Device* device, uint16_t* millivolts) {
uint16_t raw;
error_t err = read_adc_raw(device, REG_BATTERY_VOLTAGE, &raw);
if (err != ERROR_NONE) {
return err;
}
*millivolts = static_cast<uint16_t>((raw * 11U) / 10U); // 1.1mV/LSB
return ERROR_NONE;
}
error_t axp192_get_battery_charge_current(Device* device, uint16_t* milliamps) {
uint16_t raw;
error_t err = read_adc_raw(device, REG_CHARGE_CURRENT, &raw);
if (err != ERROR_NONE) {
return err;
}
*milliamps = raw / 2U; // 0.5mA/LSB
return ERROR_NONE;
}
error_t axp192_get_battery_discharge_current(Device* device, uint16_t* milliamps) {
uint16_t raw;
error_t err = read_adc_raw(device, REG_DISCHARGE_CURRENT, &raw);
if (err != ERROR_NONE) {
return err;
}
*milliamps = raw / 2U; // 0.5mA/LSB
return ERROR_NONE;
}
error_t axp192_get_battery_power(Device* device, uint32_t* microwatts) {
auto* parent = device_get_parent(device);
auto address = GET_CONFIG(device)->address;
uint8_t buffer[3];
error_t err = i2c_controller_read_register(parent, address, REG_BATTERY_POWER, buffer, sizeof(buffer), TIMEOUT);
if (err != ERROR_NONE) {
return err;
}
uint32_t raw = (static_cast<uint32_t>(buffer[0]) << 16U) | (static_cast<uint32_t>(buffer[1]) << 8U) | buffer[2];
*microwatts = (raw * 11U) / 20U; // 1.1mV * 0.5mA = 0.55uW/LSB
return ERROR_NONE;
}
error_t axp192_is_charging(Device* device, bool* charging) {
auto* parent = device_get_parent(device);
auto address = GET_CONFIG(device)->address;
uint8_t value;
error_t err = i2c_controller_register8_get(parent, address, REG_MODE_CHGSTATUS, &value, TIMEOUT);
if (err != ERROR_NONE) {
return err;
}
*charging = (value & BIT_CHARGING) != 0U;
return ERROR_NONE;
}
error_t axp192_is_charge_enabled(Device* device, bool* enabled) {
auto* parent = device_get_parent(device);
auto address = GET_CONFIG(device)->address;
uint8_t value;
error_t err = i2c_controller_register8_get(parent, address, REG_CHARGE_CONTROL_1, &value, TIMEOUT);
if (err != ERROR_NONE) {
return err;
}
*enabled = (value & BIT_CHARGE_ENABLED) != 0U;
return ERROR_NONE;
}
error_t axp192_set_charge_enabled(Device* device, bool enabled) {
auto* parent = device_get_parent(device);
auto address = GET_CONFIG(device)->address;
if (enabled) {
return i2c_controller_register8_set_bits(parent, address, REG_CHARGE_CONTROL_1, BIT_CHARGE_ENABLED, TIMEOUT);
} else {
return i2c_controller_register8_reset_bits(parent, address, REG_CHARGE_CONTROL_1, BIT_CHARGE_ENABLED, TIMEOUT);
}
}
error_t axp192_power_off(Device* device) {
auto* parent = device_get_parent(device);
auto address = GET_CONFIG(device)->address;
return i2c_controller_register8_set_bits(parent, address, REG_SHUTDOWN_BATTERY_CHGLED_CONTROL, BIT_POWER_OFF, TIMEOUT);
}
error_t axp192_set_gpio1_pwm1_output(Device* device) {
auto* parent = device_get_parent(device);
auto address = GET_CONFIG(device)->address;
return i2c_controller_register8_set(parent, address, REG_GPIO1_CONTROL, GPIO1_FUNCTION_PWM1, TIMEOUT);
}
error_t axp192_set_pwm1_duty_cycle(Device* device, uint8_t duty) {
auto* parent = device_get_parent(device);
auto address = GET_CONFIG(device)->address;
return i2c_controller_register8_set(parent, address, REG_PWM1_DUTY_CYCLE_2, duty, TIMEOUT);
}
// region Power supply child device
static bool ps_supports_property(Device*, PowerSupplyProperty property) {
return property == POWER_SUPPLY_PROP_IS_CHARGING ||
property == POWER_SUPPLY_PROP_VOLTAGE ||
property == POWER_SUPPLY_PROP_CURRENT;
}
static error_t ps_get_property(Device* device, PowerSupplyProperty property, PowerSupplyPropertyValue* out_value) {
// device_get_parent() here is the axp192 device itself (this child's parent), not the I2C bus.
auto* axp192_device = device_get_parent(device);
switch (property) {
case POWER_SUPPLY_PROP_IS_CHARGING: {
bool charging;
error_t err = axp192_is_charging(axp192_device, &charging);
if (err != ERROR_NONE) {
return err;
}
out_value->int_value = charging ? 1 : 0;
return ERROR_NONE;
}
case POWER_SUPPLY_PROP_VOLTAGE: {
uint16_t millivolts;
error_t err = axp192_get_battery_voltage(axp192_device, &millivolts);
if (err != ERROR_NONE) {
return err;
}
out_value->int_value = millivolts;
return ERROR_NONE;
}
case POWER_SUPPLY_PROP_CURRENT: {
uint16_t charge_current, discharge_current;
error_t err = axp192_get_battery_charge_current(axp192_device, &charge_current);
if (err != ERROR_NONE) {
return err;
}
err = axp192_get_battery_discharge_current(axp192_device, &discharge_current);
if (err != ERROR_NONE) {
return err;
}
out_value->int_value = (charge_current > 0U) ? charge_current : -static_cast<int>(discharge_current);
return ERROR_NONE;
}
default:
return ERROR_NOT_SUPPORTED;
}
}
static bool ps_supports_charge_control(Device*) { return true; }
static bool ps_is_allowed_to_charge(Device* device) {
bool enabled = false;
axp192_is_charge_enabled(device_get_parent(device), &enabled);
return enabled;
}
static error_t ps_set_allowed_to_charge(Device* device, bool allowed) {
return axp192_set_charge_enabled(device_get_parent(device), allowed);
}
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 axp192_power_off(device_get_parent(device)); }
static constexpr PowerSupplyApi AXP192_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: axp192_driver wires it up directly by pointer.
Driver axp192_power_supply_driver = {
.name = "axp192-power-supply",
.compatible = (const char*[]) { "axp192-power-supply", nullptr },
.start_device = nullptr,
.stop_device = nullptr,
.api = &AXP192_POWER_SUPPLY_API,
.device_type = &POWER_SUPPLY_TYPE,
.owner = &axp192_module,
.internal = nullptr
};
struct Axp192Internal {
Device* power_supply_device = nullptr;
};
static error_t create_power_supply_child(Device* parent, Device*& out_child) {
auto* child = new(std::nothrow) Device { .address = 0, .name = "axp192-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, &axp192_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
static error_t start(Device* device) {
auto* parent = device_get_parent(device);
check(device_get_type(parent) == &I2C_CONTROLLER_TYPE);
auto* internal = new(std::nothrow) Axp192Internal();
if (internal == nullptr) {
return ERROR_OUT_OF_MEMORY;
}
error_t error = create_power_supply_child(device, internal->power_supply_device);
if (error != ERROR_NONE) {
delete internal;
return error;
}
device_set_driver_data(device, internal);
return ERROR_NONE;
}
static error_t stop(Device* device) {
auto* internal = static_cast<Axp192Internal*>(device_get_driver_data(device));
destroy_power_supply_child(internal->power_supply_device);
device_set_driver_data(device, nullptr);
delete internal;
return ERROR_NONE;
}
Driver axp192_driver = {
.name = "axp192",
.compatible = (const char*[]) { "x-powers,axp192", nullptr },
.start_device = start,
.stop_device = stop,
.api = nullptr,
.device_type = nullptr,
.owner = &axp192_module,
.internal = nullptr
};
}
@@ -0,0 +1,132 @@
// SPDX-License-Identifier: Apache-2.0
#include <drivers/axp192_backlight.h>
#include <axp192_module.h>
#include <tactility/device.h>
#include <tactility/driver.h>
#include <tactility/drivers/backlight.h>
#include <tactility/log.h>
#include <tactility/module.h>
#include <new>
constexpr auto* TAG = "Axp192Backlight";
#define GET_CONFIG(device) (static_cast<const Axp192BacklightConfig*>((device)->config))
#define GET_INTERNAL(device) (static_cast<Axp192BacklightInternal*>(device_get_driver_data(device)))
extern "C" {
struct Axp192BacklightInternal {
uint8_t brightness;
};
// region BacklightApi
static error_t apply_brightness(Device* device, uint8_t brightness) {
const auto* config = GET_CONFIG(device);
auto* axp192 = device_get_parent(device);
if (brightness == 0) {
return axp192_set_rail_enabled(axp192, config->rail, false);
}
uint16_t millivolt = static_cast<uint16_t>(
config->min_millivolt +
(static_cast<uint32_t>(brightness) * (config->max_millivolt - config->min_millivolt)) / 255U
);
error_t error = axp192_set_rail_voltage(axp192, config->rail, millivolt);
if (error != ERROR_NONE) {
LOG_E(TAG, "Failed to set rail voltage");
return error;
}
return axp192_set_rail_enabled(axp192, config->rail, true);
}
static error_t axp192_backlight_set_brightness(Device* device, uint8_t brightness) {
error_t error = apply_brightness(device, brightness);
if (error != ERROR_NONE) {
return error;
}
GET_INTERNAL(device)->brightness = brightness;
return ERROR_NONE;
}
static error_t axp192_backlight_set_brightness_default(Device* device) {
return axp192_backlight_set_brightness(device, GET_CONFIG(device)->brightness_default);
}
static error_t axp192_backlight_get_brightness(Device* device, uint8_t* out_brightness) {
*out_brightness = GET_INTERNAL(device)->brightness;
return ERROR_NONE;
}
static uint8_t axp192_backlight_get_min_brightness(Device*) {
return 0;
}
static uint8_t axp192_backlight_get_max_brightness(Device*) {
return 255;
}
// endregion
static constexpr BacklightApi AXP192_BACKLIGHT_API = {
.set_brightness = axp192_backlight_set_brightness,
.set_brightness_default = axp192_backlight_set_brightness_default,
.get_brightness = axp192_backlight_get_brightness,
.get_min_brightness = axp192_backlight_get_min_brightness,
.get_max_brightness = axp192_backlight_get_max_brightness,
};
// region Driver lifecycle
static error_t start(Device* device) {
const auto* config = GET_CONFIG(device);
if (config->max_millivolt <= config->min_millivolt) {
return ERROR_INVALID_ARGUMENT;
}
auto* internal = new(std::nothrow) Axp192BacklightInternal { .brightness = 0 };
if (internal == nullptr) {
return ERROR_OUT_OF_MEMORY;
}
device_set_driver_data(device, internal);
error_t error = axp192_backlight_set_brightness_default(device);
if (error != ERROR_NONE) {
device_set_driver_data(device, nullptr);
delete internal;
return error;
}
return ERROR_NONE;
}
static error_t stop(Device* device) {
axp192_backlight_set_brightness(device, 0); // Allowed to fail, we don't care about the result
auto* internal = GET_INTERNAL(device);
device_set_driver_data(device, nullptr);
delete internal;
return ERROR_NONE;
}
// endregion
Driver axp192_backlight_driver = {
.name = "axp192_backlight",
.compatible = (const char*[]) { "axp192-backlight", nullptr },
.start_device = start,
.stop_device = stop,
.api = &AXP192_BACKLIGHT_API,
.device_type = &BACKLIGHT_TYPE,
.owner = &axp192_module,
.internal = nullptr
};
}
+56
View File
@@ -0,0 +1,56 @@
// SPDX-License-Identifier: Apache-2.0
#include <drivers/axp192.h>
#include <tactility/check.h>
#include <tactility/driver.h>
#include <tactility/module.h>
extern "C" {
extern Driver axp192_driver;
extern Driver axp192_power_supply_driver;
extern Driver axp192_backlight_driver;
const struct ModuleSymbol axp192_module_symbols[] = {
DEFINE_MODULE_SYMBOL(axp192_is_rail_enabled),
DEFINE_MODULE_SYMBOL(axp192_set_rail_enabled),
DEFINE_MODULE_SYMBOL(axp192_set_rail_voltage),
DEFINE_MODULE_SYMBOL(axp192_get_battery_voltage),
DEFINE_MODULE_SYMBOL(axp192_get_battery_charge_current),
DEFINE_MODULE_SYMBOL(axp192_get_battery_discharge_current),
DEFINE_MODULE_SYMBOL(axp192_get_battery_power),
DEFINE_MODULE_SYMBOL(axp192_is_charging),
DEFINE_MODULE_SYMBOL(axp192_is_charge_enabled),
DEFINE_MODULE_SYMBOL(axp192_set_charge_enabled),
DEFINE_MODULE_SYMBOL(axp192_power_off),
DEFINE_MODULE_SYMBOL(axp192_set_gpio1_pwm1_output),
DEFINE_MODULE_SYMBOL(axp192_set_pwm1_duty_cycle),
MODULE_SYMBOL_TERMINATOR
};
static error_t start() {
/* We crash when construct fails, because if a single driver fails to construct,
* there is no guarantee that the previously constructed drivers can be destroyed */
check(driver_construct_add(&axp192_driver) == ERROR_NONE);
check(driver_construct_add(&axp192_power_supply_driver) == ERROR_NONE);
check(driver_construct_add(&axp192_backlight_driver) == ERROR_NONE);
return ERROR_NONE;
}
static error_t stop() {
/* We crash when destruct fails, because if a single driver fails to destruct,
* there is no guarantee that the previously destroyed drivers can be recovered */
check(driver_remove_destruct(&axp192_backlight_driver) == ERROR_NONE);
check(driver_remove_destruct(&axp192_power_supply_driver) == ERROR_NONE);
check(driver_remove_destruct(&axp192_driver) == ERROR_NONE);
return ERROR_NONE;
}
Module axp192_module = {
.name = "axp192",
.start = start,
.stop = stop,
.symbols = axp192_module_symbols,
.internal = nullptr
};
}

Some files were not shown because too many files have changed in this diff Show More