diff --git a/Devices/guition-jc3248w535c/CMakeLists.txt b/Devices/guition-jc3248w535c/CMakeLists.txt deleted file mode 100644 index a832a83a2..000000000 --- a/Devices/guition-jc3248w535c/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ -file(GLOB_RECURSE SOURCE_FILES source/*.c*) - -idf_component_register( - SRCS ${SOURCE_FILES} - REQUIRES TactilityKernel driver -) diff --git a/Devices/guition-jc3248w535c/device.properties b/Devices/guition-jc3248w535c/device.properties deleted file mode 100644 index ff2553224..000000000 --- a/Devices/guition-jc3248w535c/device.properties +++ /dev/null @@ -1,25 +0,0 @@ -general.vendor=Guition -general.name=JC3248W535C - -apps.launcherAppId=Launcher - -hardware.target=ESP32S3 -hardware.flashSize=16MB -hardware.spiRam=true -hardware.spiRamMode=OCT -hardware.spiRamSpeed=120M -hardware.tinyUsb=true -hardware.esptoolFlashFreq=120M -hardware.bluetooth=true - -dependencies.useDeprecatedHal=false - -storage.userDataLocation=SD - -display.size=3.5" -display.shape=rectangle -display.dpi=165 - -lvgl.colorDepth=16 - -sdkconfig.CONFIG_CODEC_DUMMY_SUPPORT=y diff --git a/Devices/guition-jc3248w535c/devicetree.yaml b/Devices/guition-jc3248w535c/devicetree.yaml deleted file mode 100644 index 6c1745429..000000000 --- a/Devices/guition-jc3248w535c/devicetree.yaml +++ /dev/null @@ -1,6 +0,0 @@ -dependencies: - - Platforms/platform-esp32 - - Drivers/dummy-i2s-amp-module - - Drivers/audio-stream-module - - Drivers/axs15231b-module -dts: guition,jc3248w535c.dts diff --git a/Devices/guition-jc3248w535c/guition,jc3248w535c.dts b/Devices/guition-jc3248w535c/guition,jc3248w535c.dts deleted file mode 100644 index d820fdb78..000000000 --- a/Devices/guition-jc3248w535c/guition,jc3248w535c.dts +++ /dev/null @@ -1,182 +0,0 @@ -/dts-v1/; - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -/ { - 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 = ; - clock-frequency = <400000>; - pin-sda = <&gpio0 17 GPIO_FLAG_NONE>; - pin-scl = <&gpio0 18 GPIO_FLAG_NONE>; - }; - - i2s0 { - compatible = "espressif,esp32-i2s"; - port = ; - 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 = ; - 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 = ; - 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 = ; - 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 = ; - pin-tx = <&gpio0 43 GPIO_FLAG_NONE>; - pin-rx = <&gpio0 44 GPIO_FLAG_NONE>; - }; -}; diff --git a/Devices/guition-jc3248w535c/source/module.cpp b/Devices/guition-jc3248w535c/source/module.cpp deleted file mode 100644 index b649c4192..000000000 --- a/Devices/guition-jc3248w535c/source/module.cpp +++ /dev/null @@ -1,21 +0,0 @@ -#include - -extern "C" { - -static error_t start() { - return ERROR_NONE; -} - -static error_t stop() { - return ERROR_NONE; -} - -struct Module guition_jc3248w535c_module = { - .name = "guition-jc3248w535c", - .start = start, - .stop = stop, - .symbols = nullptr, - .internal = nullptr -}; - -} diff --git a/Devices/lilygo-tdisplay-s3/lilygo,tdisplay-s3.dts b/Devices/lilygo-tdisplay-s3/lilygo,tdisplay-s3.dts index 778c7f0a7..2ec983f1c 100644 --- a/Devices/lilygo-tdisplay-s3/lilygo,tdisplay-s3.dts +++ b/Devices/lilygo-tdisplay-s3/lilygo,tdisplay-s3.dts @@ -39,9 +39,6 @@ channels = ; }; - // 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>; diff --git a/Devices/m5stack-core2/CMakeLists.txt b/Devices/m5stack-core2/CMakeLists.txt index 8d28c5859..6e986ba7e 100644 --- a/Devices/m5stack-core2/CMakeLists.txt +++ b/Devices/m5stack-core2/CMakeLists.txt @@ -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 ) diff --git a/Devices/m5stack-core2/Source/Configuration.cpp b/Devices/m5stack-core2/Source/Configuration.cpp deleted file mode 100644 index c7059fe58..000000000 --- a/Devices/m5stack-core2/Source/Configuration.cpp +++ /dev/null @@ -1,22 +0,0 @@ -#include "devices/Display.h" -#include "devices/Power.h" - -#include - -using namespace tt::hal; - -static bool initBoot() { - return initAxp(); -} - -static DeviceVector createDevices() { - return { - getAxp192(), - createDisplay() - }; -} - -extern const Configuration hardwareConfiguration = { - .initBoot = initBoot, - .createDevices = createDevices -}; diff --git a/Devices/m5stack-core2/Source/devices/Display.cpp b/Devices/m5stack-core2/Source/devices/Display.cpp deleted file mode 100644 index 88d9d2726..000000000 --- a/Devices/m5stack-core2/Source/devices/Display.cpp +++ /dev/null @@ -1,48 +0,0 @@ -#include "Display.h" - -#include -#include - -std::shared_ptr createTouch() { - auto configuration = std::make_unique( - I2C_NUM_0, - LCD_HORIZONTAL_RESOLUTION, - LCD_VERTICAL_RESOLUTION, - false, - false, - false, - GPIO_NUM_NC, - GPIO_NUM_39 - ); - - return std::make_shared(std::move(configuration)); -} - -std::shared_ptr 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 { - .spiHostDevice = LCD_SPI_HOST, - .csPin = LCD_PIN_CS, - .dcPin = LCD_PIN_DC, - .pixelClockFrequency = 40'000'000, - .transactionQueueDepth = 10 - }); - - return std::make_shared(panel_configuration, spi_configuration, true); -} diff --git a/Devices/m5stack-core2/Source/devices/Display.h b/Devices/m5stack-core2/Source/devices/Display.h deleted file mode 100644 index bc66d3105..000000000 --- a/Devices/m5stack-core2/Source/devices/Display.h +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once - -#include -#include -#include -#include - -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 createDisplay(); diff --git a/Devices/m5stack-core2/Source/devices/Power.cpp b/Devices/m5stack-core2/Source/devices/Power.cpp deleted file mode 100644 index da54c9bd6..000000000 --- a/Devices/m5stack-core2/Source/devices/Power.cpp +++ /dev/null @@ -1,36 +0,0 @@ -#include -#include - -static std::shared_ptr axp192 = nullptr; - -std::shared_ptr createAxp192() { - assert(axp192 == nullptr); - auto configuration = std::make_unique(device_find_by_name("i2c_internal")); - axp192 = std::make_shared(std::move(configuration)); - return axp192; -} - -std::shared_ptr 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; - }); - -} diff --git a/Devices/m5stack-core2/Source/devices/Power.h b/Devices/m5stack-core2/Source/devices/Power.h deleted file mode 100644 index 14a008893..000000000 --- a/Devices/m5stack-core2/Source/devices/Power.h +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once - -#include - -bool initAxp(); - -// Must call initAxp() first before this returns a non-nullptr response -std::shared_ptr getAxp192(); - diff --git a/Devices/m5stack-core2/Source/module.cpp b/Devices/m5stack-core2/Source/module.cpp deleted file mode 100644 index 04abb3f7a..000000000 --- a/Devices/m5stack-core2/Source/module.cpp +++ /dev/null @@ -1,23 +0,0 @@ -#include - -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 -}; - -} diff --git a/Devices/m5stack-core2/device.properties b/Devices/m5stack-core2/device.properties index 17156143d..8f887a003 100644 --- a/Devices/m5stack-core2/device.properties +++ b/Devices/m5stack-core2/device.properties @@ -12,6 +12,8 @@ hardware.esptoolFlashFreq=80M storage.userDataLocation=SD +dependencies.useDeprecatedHal=false + display.size=2" display.shape=rectangle display.dpi=200 diff --git a/Devices/m5stack-core2/devicetree.yaml b/Devices/m5stack-core2/devicetree.yaml index d1c990164..f1711cb3b 100644 --- a/Devices/m5stack-core2/devicetree.yaml +++ b/Devices/m5stack-core2/devicetree.yaml @@ -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 diff --git a/Devices/m5stack-core2/m5stack,core2.dts b/Devices/m5stack-core2/m5stack,core2.dts index 876912786..840f35429 100644 --- a/Devices/m5stack-core2/m5stack,core2.dts +++ b/Devices/m5stack-core2/m5stack,core2.dts @@ -10,8 +10,10 @@ #include #include #include +#include +#include +#include #include -#include // 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>; }; }; diff --git a/Devices/m5stack-core2/source/module.cpp b/Devices/m5stack-core2/source/module.cpp new file mode 100644 index 000000000..bedeee431 --- /dev/null +++ b/Devices/m5stack-core2/source/module.cpp @@ -0,0 +1,49 @@ +#include + +#include +#include +#include +#include + +#include + +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 +}; + +} diff --git a/Devices/m5stack-cores3/CMakeLists.txt b/Devices/m5stack-cores3/CMakeLists.txt index b2553f94b..50dfa717c 100644 --- a/Devices/m5stack-cores3/CMakeLists.txt +++ b/Devices/m5stack-cores3/CMakeLists.txt @@ -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 ) diff --git a/Devices/m5stack-cores3/Source/Configuration.cpp b/Devices/m5stack-cores3/Source/Configuration.cpp deleted file mode 100644 index f848637da..000000000 --- a/Devices/m5stack-cores3/Source/Configuration.cpp +++ /dev/null @@ -1,21 +0,0 @@ -#include "InitBoot.h" -#include "devices/Display.h" - -#include -#include - -using namespace tt::hal; - -static DeviceVector createDevices() { - return { - axp2101, - aw9523, - std::make_shared(axp2101), - createDisplay() - }; -} - -extern const Configuration hardwareConfiguration = { - .initBoot = initBoot, - .createDevices = createDevices -}; diff --git a/Devices/m5stack-cores3/Source/InitBoot.cpp b/Devices/m5stack-cores3/Source/InitBoot.cpp deleted file mode 100644 index 8c9e6a44c..000000000 --- a/Devices/m5stack-cores3/Source/InitBoot.cpp +++ /dev/null @@ -1,154 +0,0 @@ -#include "InitBoot.h" - -#include -#include - -constexpr auto* TAG = "CoreS3"; - -std::shared_ptr axp2101; -std::shared_ptr 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
CoreS3
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.275V,25mV/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(controller); - aw9523 = std::make_shared(controller); - - return initPowerControl() && initGpioExpander(); -} \ No newline at end of file diff --git a/Devices/m5stack-cores3/Source/InitBoot.h b/Devices/m5stack-cores3/Source/InitBoot.h deleted file mode 100644 index 701569604..000000000 --- a/Devices/m5stack-cores3/Source/InitBoot.h +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once - -#include -#include - -extern std::shared_ptr axp2101; -extern std::shared_ptr aw9523; - -bool initBoot(); diff --git a/Devices/m5stack-cores3/Source/devices/Display.cpp b/Devices/m5stack-cores3/Source/devices/Display.cpp deleted file mode 100644 index ac868a841..000000000 --- a/Devices/m5stack-cores3/Source/devices/Display.cpp +++ /dev/null @@ -1,59 +0,0 @@ -#include "Display.h" - -#include -#include -#include - -#include -#include - -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 createTouch() { - auto configuration = std::make_unique( - I2C_NUM_0, - 319,//LCD_HORIZONTAL_RESOLUTION, - 239//LCD_VERTICAL_RESOLUTION, - ); - - return std::make_shared(std::move(configuration)); -} - -std::shared_ptr 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 { - .spiHostDevice = LCD_SPI_HOST, - .csPin = LCD_PIN_CS, - .dcPin = LCD_PIN_DC, - .pixelClockFrequency = 40'000'000, - .transactionQueueDepth = 10 - }); - - return std::make_shared(panel_configuration, spi_configuration, true); -} diff --git a/Devices/m5stack-cores3/Source/devices/Display.h b/Devices/m5stack-cores3/Source/devices/Display.h deleted file mode 100644 index bfb965229..000000000 --- a/Devices/m5stack-cores3/Source/devices/Display.h +++ /dev/null @@ -1,17 +0,0 @@ -#pragma once - -#include -#include -#include - -// 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 createDisplay(); diff --git a/Devices/m5stack-cores3/Source/module.cpp b/Devices/m5stack-cores3/Source/module.cpp deleted file mode 100644 index db26f6cc2..000000000 --- a/Devices/m5stack-cores3/Source/module.cpp +++ /dev/null @@ -1,23 +0,0 @@ -#include - -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 -}; - -} diff --git a/Devices/m5stack-cores3/device.properties b/Devices/m5stack-cores3/device.properties index 28e15bcd0..8f3c1245b 100644 --- a/Devices/m5stack-cores3/device.properties +++ b/Devices/m5stack-cores3/device.properties @@ -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 diff --git a/Devices/m5stack-cores3/devicetree.yaml b/Devices/m5stack-cores3/devicetree.yaml index b65b1d7e5..d4639c805 100644 --- a/Devices/m5stack-cores3/devicetree.yaml +++ b/Devices/m5stack-cores3/devicetree.yaml @@ -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 diff --git a/Devices/m5stack-cores3/m5stack,cores3.dts b/Devices/m5stack-cores3/m5stack,cores3.dts index 01e81260b..9c05f5a44 100644 --- a/Devices/m5stack-cores3/m5stack,cores3.dts +++ b/Devices/m5stack-cores3/m5stack,cores3.dts @@ -11,8 +11,16 @@ #include #include #include +#include +#include +#include +#include +#include #include -#include +#include + +// 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 = ; + 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 = ; + }; + + aw9523_aw88298_reset { + compatible = "gpio-hog"; + pin = <&aw9523b 2 GPIO_FLAG_NONE>; + mode = ; + }; + + aw9523_sdcard_switch { + compatible = "gpio-hog"; + pin = <&aw9523b 4 GPIO_FLAG_NONE>; + mode = ; + }; + + aw9523_boost_enable { + compatible = "gpio-hog"; + pin = <&aw9523b 15 GPIO_FLAG_NONE>; + mode = ; }; 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>; }; }; diff --git a/Devices/m5stack-cores3/source/module.cpp b/Devices/m5stack-cores3/source/module.cpp new file mode 100644 index 000000000..96a314887 --- /dev/null +++ b/Devices/m5stack-cores3/source/module.cpp @@ -0,0 +1,55 @@ +#include + +#include +#include +#include +#include + +#include + +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 +}; + +} diff --git a/Devices/m5stack-papers3/CMakeLists.txt b/Devices/m5stack-papers3/CMakeLists.txt index 6b2352f1c..1f19c644b 100644 --- a/Devices/m5stack-papers3/CMakeLists.txt +++ b/Devices/m5stack-papers3/CMakeLists.txt @@ -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 ) diff --git a/Devices/m5stack-papers3/Source/Configuration.cpp b/Devices/m5stack-papers3/Source/Configuration.cpp deleted file mode 100644 index f6d90bb50..000000000 --- a/Devices/m5stack-papers3/Source/Configuration.cpp +++ /dev/null @@ -1,20 +0,0 @@ -#include "devices/Display.h" -#include "devices/Power.h" - -#include - -using namespace tt::hal; - -bool initBoot(); - -static DeviceVector createDevices() { - return { - createPower(), - createDisplay(), - }; -} - -extern const Configuration hardwareConfiguration = { - .initBoot = initBoot, - .createDevices = createDevices -}; diff --git a/Devices/m5stack-papers3/Source/Init.cpp b/Devices/m5stack-papers3/Source/Init.cpp deleted file mode 100644 index 995b53927..000000000 --- a/Devices/m5stack-papers3/Source/Init.cpp +++ /dev/null @@ -1,48 +0,0 @@ -#include -#include - -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; -} diff --git a/Devices/m5stack-papers3/Source/devices/Display.cpp b/Devices/m5stack-papers3/Source/devices/Display.cpp deleted file mode 100644 index acfd5f59a..000000000 --- a/Devices/m5stack-papers3/Source/devices/Display.cpp +++ /dev/null @@ -1,27 +0,0 @@ -#include "Display.h" -#include -#include -#include -#include - -std::shared_ptr createTouch() { - auto* i2c = device_find_by_name("i2c_internal"); - check(i2c); - auto configuration = std::make_unique( - i2c, - 540, - 960, - true, // swapXy - true, // mirrorX - false, // mirrorY - GPIO_NUM_NC, // pinReset - GPIO_NUM_NC //48 pinInterrupt - ); - - return std::make_shared(std::move(configuration)); -} - -std::shared_ptr createDisplay() { - auto touch = createTouch(); - return EpdiyDisplayHelper::createM5PaperS3Display(touch); -} diff --git a/Devices/m5stack-papers3/Source/devices/Display.h b/Devices/m5stack-papers3/Source/devices/Display.h deleted file mode 100644 index 13a1fe144..000000000 --- a/Devices/m5stack-papers3/Source/devices/Display.h +++ /dev/null @@ -1,6 +0,0 @@ -#pragma once - -#include -#include - -std::shared_ptr createDisplay(); diff --git a/Devices/m5stack-papers3/Source/devices/Power.cpp b/Devices/m5stack-papers3/Source/devices/Power.cpp deleted file mode 100644 index 7f853fd4a..000000000 --- a/Devices/m5stack-papers3/Source/devices/Power.cpp +++ /dev/null @@ -1,282 +0,0 @@ -#include "Power.h" - -#include -#include -#include -#include - -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, - 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 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(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(std::move(adc), POWER_OFF_PIN); -} diff --git a/Devices/m5stack-papers3/Source/devices/Power.h b/Devices/m5stack-papers3/Source/devices/Power.h deleted file mode 100644 index c26138a94..000000000 --- a/Devices/m5stack-papers3/Source/devices/Power.h +++ /dev/null @@ -1,55 +0,0 @@ -#pragma once - -#include -#include -#include -#include - -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 createPower(); diff --git a/Devices/m5stack-papers3/Source/module.cpp b/Devices/m5stack-papers3/Source/module.cpp deleted file mode 100644 index 1a7cf0822..000000000 --- a/Devices/m5stack-papers3/Source/module.cpp +++ /dev/null @@ -1,23 +0,0 @@ -#include - -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 -}; - -} diff --git a/Devices/m5stack-papers3/bindings/m5stack,papers3-display.yaml b/Devices/m5stack-papers3/bindings/m5stack,papers3-display.yaml new file mode 100644 index 000000000..5e6836f38 --- /dev/null +++ b/Devices/m5stack-papers3/bindings/m5stack,papers3-display.yaml @@ -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) diff --git a/Devices/m5stack-papers3/bindings/m5stack,papers3-power.yaml b/Devices/m5stack-papers3/bindings/m5stack,papers3-power.yaml new file mode 100644 index 000000000..3bc9d27d4 --- /dev/null +++ b/Devices/m5stack-papers3/bindings/m5stack,papers3-power.yaml @@ -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 diff --git a/Devices/m5stack-papers3/device.properties b/Devices/m5stack-papers3/device.properties index 91cc994f9..ba55255a1 100644 --- a/Devices/m5stack-papers3/device.properties +++ b/Devices/m5stack-papers3/device.properties @@ -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 \ No newline at end of file diff --git a/Devices/m5stack-papers3/devicetree.yaml b/Devices/m5stack-papers3/devicetree.yaml index 855cc0553..690f32fbd 100644 --- a/Devices/m5stack-papers3/devicetree.yaml +++ b/Devices/m5stack-papers3/devicetree.yaml @@ -2,4 +2,6 @@ dependencies: - Platforms/platform-esp32 - Drivers/bmi270-module - Drivers/bm8563-module +- Drivers/gt911-module +bindings: bindings dts: m5stack,papers3.dts diff --git a/Devices/m5stack-papers3/m5stack,papers3.dts b/Devices/m5stack-papers3/m5stack,papers3.dts index e463fedd4..dc7b15c18 100644 --- a/Devices/m5stack-papers3/m5stack,papers3.dts +++ b/Devices/m5stack-papers3/m5stack,papers3.dts @@ -6,9 +6,15 @@ #include #include #include +#include +#include +#include #include #include +#include #include +#include +#include / { 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 = ; + channels = ; + }; + + 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 = ; + }; }; diff --git a/Devices/m5stack-papers3/source/bindings/papers3_display.h b/Devices/m5stack-papers3/source/bindings/papers3_display.h new file mode 100644 index 000000000..6abbc7ad5 --- /dev/null +++ b/Devices/m5stack-papers3/source/bindings/papers3_display.h @@ -0,0 +1,14 @@ +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include + +DEFINE_DEVICETREE(papers3_display, struct Papers3DisplayConfig) + +#ifdef __cplusplus +} +#endif diff --git a/Devices/m5stack-papers3/source/bindings/papers3_power.h b/Devices/m5stack-papers3/source/bindings/papers3_power.h new file mode 100644 index 000000000..49ab73534 --- /dev/null +++ b/Devices/m5stack-papers3/source/bindings/papers3_power.h @@ -0,0 +1,14 @@ +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include + +DEFINE_DEVICETREE(papers3_power, struct Papers3PowerConfig) + +#ifdef __cplusplus +} +#endif diff --git a/Devices/m5stack-papers3/source/drivers/papers3_display.cpp b/Devices/m5stack-papers3/source/drivers/papers3_display.cpp new file mode 100644 index 000000000..d127bafce --- /dev/null +++ b/Devices/m5stack-papers3/source/drivers/papers3_display.cpp @@ -0,0 +1,292 @@ +// SPDX-License-Identifier: Apache-2.0 +#include "papers3_display.h" + +#include +#include +#include +#include +#include +#include + +#include +#include + +#include +#include + +#define TAG "Papers3Display" +#define GET_CONFIG(device) (static_cast((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((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(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(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(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(color_data); + const size_t src_stride = static_cast(width + 7) / 8; + const size_t packed_stride = static_cast(width + 1) / 2; + + for (int32_t row = 0; row < height; row++) { + const uint8_t* src_row = src + static_cast(row) * src_stride; + uint8_t* dst_row = internal->packed_buffer + static_cast(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((p1 << 4U) | p0); + } + } + + const EpdRect update_area = { + .x = x_start, + .y = y_start, + .width = static_cast(width), + .height = static_cast(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(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(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(epd_rotated_display_width()); +} + +static uint16_t papers3_display_get_resolution_y(Device*) { + return static_cast(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(malloc(sizeof(Papers3DisplayInternal))); + if (internal == nullptr) { + return ERROR_OUT_OF_MEMORY; + } + internal->powered = false; + + epd_init(&epd_board_m5papers3, &ED047TC1, static_cast(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((epd_rotated_display_width() + 1) / 2) * static_cast(epd_rotated_display_height()); + internal->packed_buffer = static_cast(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(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 +}; + +} diff --git a/Devices/m5stack-papers3/source/drivers/papers3_display.h b/Devices/m5stack-papers3/source/drivers/papers3_display.h new file mode 100644 index 000000000..f293e83a1 --- /dev/null +++ b/Devices/m5stack-papers3/source/drivers/papers3_display.h @@ -0,0 +1,19 @@ +#pragma once + +#include + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +struct Papers3DisplayConfig { + int temperature_celsius; + enum EpdDrawMode draw_mode; + enum EpdRotation rotation; +}; + +#ifdef __cplusplus +} +#endif diff --git a/Devices/m5stack-papers3/source/drivers/papers3_power.cpp b/Devices/m5stack-papers3/source/drivers/papers3_power.cpp new file mode 100644 index 000000000..8722795f6 --- /dev/null +++ b/Devices/m5stack-papers3/source/drivers/papers3_power.cpp @@ -0,0 +1,280 @@ +// SPDX-License-Identifier: Apache-2.0 +#include "papers3_power.h" + +#include +#include +#include +#include +#include +#include + +#include +#include + +#include + +#define TAG "Papers3Power" +#define GET_CONFIG(device) (static_cast((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(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(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(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(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(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 +}; + +} diff --git a/Devices/m5stack-papers3/source/drivers/papers3_power.h b/Devices/m5stack-papers3/source/drivers/papers3_power.h new file mode 100644 index 000000000..13639dac5 --- /dev/null +++ b/Devices/m5stack-papers3/source/drivers/papers3_power.h @@ -0,0 +1,37 @@ +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +#include +#include +#include + +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 diff --git a/Devices/m5stack-papers3/source/module.cpp b/Devices/m5stack-papers3/source/module.cpp new file mode 100644 index 000000000..00841b137 --- /dev/null +++ b/Devices/m5stack-papers3/source/module.cpp @@ -0,0 +1,34 @@ +#include +#include +#include +#include + +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 +}; + +} diff --git a/Drivers/AW9523/CMakeLists.txt b/Drivers/AW9523/CMakeLists.txt deleted file mode 100644 index 8074f3b38..000000000 --- a/Drivers/AW9523/CMakeLists.txt +++ /dev/null @@ -1,5 +0,0 @@ -idf_component_register( - SRC_DIRS "Source" - INCLUDE_DIRS "Source" - REQUIRES Tactility -) diff --git a/Drivers/AW9523/README.md b/Drivers/AW9523/README.md deleted file mode 100644 index 474c6bfc4..000000000 --- a/Drivers/AW9523/README.md +++ /dev/null @@ -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) \ No newline at end of file diff --git a/Drivers/AW9523/Source/Aw9523.cpp b/Drivers/AW9523/Source/Aw9523.cpp deleted file mode 100644 index bf6c0ee74..000000000 --- a/Drivers/AW9523/Source/Aw9523.cpp +++ /dev/null @@ -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); -} diff --git a/Drivers/AW9523/Source/Aw9523.h b/Drivers/AW9523/Source/Aw9523.h deleted file mode 100644 index 48c7e27b5..000000000 --- a/Drivers/AW9523/Source/Aw9523.h +++ /dev/null @@ -1,25 +0,0 @@ -#pragma once - -#include - -#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; -}; diff --git a/Drivers/AXP192/CMakeLists.txt b/Drivers/AXP192/CMakeLists.txt deleted file mode 100644 index afcd06ed5..000000000 --- a/Drivers/AXP192/CMakeLists.txt +++ /dev/null @@ -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 -) diff --git a/Drivers/AXP192/Include/Axp192.h b/Drivers/AXP192/Include/Axp192.h deleted file mode 100644 index cc11bb82f..000000000 --- a/Drivers/AXP192/Include/Axp192.h +++ /dev/null @@ -1,59 +0,0 @@ -#pragma once - -#include -#include -#include - -#include - -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; - - axp192_t axpDevice = { - .read = i2cRead, - .write = i2cWrite, - .handle = this - }; - - bool isInitialized = false; - -public: - - explicit Axp192(std::unique_ptr configuration) : configuration(std::move(configuration)) {} - ~Axp192() override {} - - /** - * @warning Must call this function before device can operate! - * @param onInit - */ - bool init(const std::function& 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; -}; diff --git a/Drivers/AXP192/Include/axp192/axp192.h b/Drivers/AXP192/Include/axp192/axp192.h deleted file mode 100644 index ff0221175..000000000 --- a/Drivers/AXP192/Include/axp192/axp192.h +++ /dev/null @@ -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 - -#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 diff --git a/Drivers/AXP192/Private/axp192/LICENSE b/Drivers/AXP192/Private/axp192/LICENSE deleted file mode 100644 index bf31dcf49..000000000 --- a/Drivers/AXP192/Private/axp192/LICENSE +++ /dev/null @@ -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. \ No newline at end of file diff --git a/Drivers/AXP192/Private/axp192/README.md b/Drivers/AXP192/Private/axp192/README.md deleted file mode 100644 index c2110a248..000000000 --- a/Drivers/AXP192/Private/axp192/README.md +++ /dev/null @@ -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 \ No newline at end of file diff --git a/Drivers/AXP192/Private/axp192/axp192_config.h b/Drivers/AXP192/Private/axp192/axp192_config.h deleted file mode 100644 index 1a87e24aa..000000000 --- a/Drivers/AXP192/Private/axp192/axp192_config.h +++ /dev/null @@ -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 diff --git a/Drivers/AXP192/README.md b/Drivers/AXP192/README.md deleted file mode 100644 index 91aca5862..000000000 --- a/Drivers/AXP192/README.md +++ /dev/null @@ -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 diff --git a/Drivers/AXP192/Source/Axp192.cpp b/Drivers/AXP192/Source/Axp192.cpp deleted file mode 100644 index 13fe31133..000000000 --- a/Drivers/AXP192/Source/Axp192.cpp +++ /dev/null @@ -1,118 +0,0 @@ -#include "Axp192.h" -#include - -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(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(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); - } -} diff --git a/Drivers/AXP192/Source/axp192/axp192.c b/Drivers/AXP192/Source/axp192/axp192.c deleted file mode 100644 index 25790b542..000000000 --- a/Drivers/AXP192/Source/axp192/axp192.c +++ /dev/null @@ -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 -#include - -#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; -} \ No newline at end of file diff --git a/Drivers/EstimatedPower/CMakeLists.txt b/Drivers/EstimatedPower/CMakeLists.txt deleted file mode 100644 index 6e3b34aa7..000000000 --- a/Drivers/EstimatedPower/CMakeLists.txt +++ /dev/null @@ -1,5 +0,0 @@ -idf_component_register( - SRC_DIRS "Source" - INCLUDE_DIRS "Source" - REQUIRES Tactility esp_adc -) diff --git a/Drivers/EstimatedPower/README.md b/Drivers/EstimatedPower/README.md deleted file mode 100644 index bd8cbb1bf..000000000 --- a/Drivers/EstimatedPower/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# EstimatedPower - -Use ADC measurements to read voltage and estimate the available power that is left in the battery. diff --git a/Drivers/EstimatedPower/Source/ChargeFromAdcVoltage.cpp b/Drivers/EstimatedPower/Source/ChargeFromAdcVoltage.cpp deleted file mode 100644 index 954a3f8e0..000000000 --- a/Drivers/EstimatedPower/Source/ChargeFromAdcVoltage.cpp +++ /dev/null @@ -1,65 +0,0 @@ -#include "ChargeFromAdcVoltage.h" -#include - -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; -} diff --git a/Drivers/EstimatedPower/Source/ChargeFromAdcVoltage.h b/Drivers/EstimatedPower/Source/ChargeFromAdcVoltage.h deleted file mode 100644 index ed40347ec..000000000 --- a/Drivers/EstimatedPower/Source/ChargeFromAdcVoltage.h +++ /dev/null @@ -1,44 +0,0 @@ -#pragma once - -#include -#include - -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); } -}; diff --git a/Drivers/EstimatedPower/Source/ChargeFromVoltage.cpp b/Drivers/EstimatedPower/Source/ChargeFromVoltage.cpp deleted file mode 100644 index c5f7d5df1..000000000 --- a/Drivers/EstimatedPower/Source/ChargeFromVoltage.cpp +++ /dev/null @@ -1,18 +0,0 @@ -#include "ChargeFromVoltage.h" - -#include -#include - -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; -} diff --git a/Drivers/EstimatedPower/Source/ChargeFromVoltage.h b/Drivers/EstimatedPower/Source/ChargeFromVoltage.h deleted file mode 100644 index 262c1a6a8..000000000 --- a/Drivers/EstimatedPower/Source/ChargeFromVoltage.h +++ /dev/null @@ -1,22 +0,0 @@ -#pragma once - -#include - -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; -}; diff --git a/Drivers/EstimatedPower/Source/EstimatedPower.cpp b/Drivers/EstimatedPower/Source/EstimatedPower.cpp deleted file mode 100644 index 93f43cb8f..000000000 --- a/Drivers/EstimatedPower/Source/EstimatedPower.cpp +++ /dev/null @@ -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; - } -} - diff --git a/Drivers/EstimatedPower/Source/EstimatedPower.h b/Drivers/EstimatedPower/Source/EstimatedPower.h deleted file mode 100644 index dd80e57f9..000000000 --- a/Drivers/EstimatedPower/Source/EstimatedPower.h +++ /dev/null @@ -1,27 +0,0 @@ -#pragma once - -#include -#include - -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; - -public: - - explicit EstimatedPower(ChargeFromAdcVoltage::Configuration configuration) : - chargeFromAdcVoltage(std::make_unique(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; -}; diff --git a/Drivers/ST7789-i8080/CMakeLists.txt b/Drivers/ST7789-i8080/CMakeLists.txt deleted file mode 100644 index 7cd677c7a..000000000 --- a/Drivers/ST7789-i8080/CMakeLists.txt +++ /dev/null @@ -1,5 +0,0 @@ -idf_component_register( - SRC_DIRS "Source" - INCLUDE_DIRS "Source" - REQUIRES Tactility driver EspLcdCompat -) diff --git a/Drivers/ST7789-i8080/README.md b/Drivers/ST7789-i8080/README.md deleted file mode 100644 index 753ffb450..000000000 --- a/Drivers/ST7789-i8080/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# ST7789 - -A basic ESP32 LVGL driver for ST7789 parallel i8080 displays. diff --git a/Drivers/ST7789-i8080/Source/St7789i8080Display.cpp b/Drivers/ST7789-i8080/Source/St7789i8080Display.cpp deleted file mode 100644 index c9b815517..000000000 --- a/Drivers/ST7789-i8080/Source/St7789i8080Display.cpp +++ /dev/null @@ -1,345 +0,0 @@ -#include "St7789i8080Display.h" -#include -#include -#include -#include -#include -#include -#include -#include - -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()) { - - // 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(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; -} diff --git a/Drivers/ST7789-i8080/Source/St7789i8080Display.h b/Drivers/ST7789-i8080/Source/St7789i8080Display.h deleted file mode 100644 index 575dfbd5b..000000000 --- a/Drivers/ST7789-i8080/Source/St7789i8080Display.h +++ /dev/null @@ -1,139 +0,0 @@ -#pragma once - -#include -#include -#include -#include -#include -#include -#include -#include - -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 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 touch = nullptr; - std::function 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 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 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 getTouchDevice() override { return configuration.touch; } - std::shared_ptr 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 createDisplay(); \ No newline at end of file diff --git a/Drivers/axs15231b-module/CMakeLists.txt b/Drivers/axp192-module/CMakeLists.txt similarity index 66% rename from Drivers/axs15231b-module/CMakeLists.txt rename to Drivers/axp192-module/CMakeLists.txt index 74f22b263..692aadc98 100644 --- a/Drivers/axs15231b-module/CMakeLists.txt +++ b/Drivers/axp192-module/CMakeLists.txt @@ -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 ) diff --git a/Drivers/axs15231b-module/LICENSE-Apache-2.0.md b/Drivers/axp192-module/LICENSE-Apache-2.0.md similarity index 100% rename from Drivers/axs15231b-module/LICENSE-Apache-2.0.md rename to Drivers/axp192-module/LICENSE-Apache-2.0.md diff --git a/Drivers/axp192-module/README.md b/Drivers/axp192-module/README.md new file mode 100644 index 000000000..5595874b5 --- /dev/null +++ b/Drivers/axp192-module/README.md @@ -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) diff --git a/Drivers/axp192-module/bindings/axp192-backlight.yaml b/Drivers/axp192-module/bindings/axp192-backlight.yaml new file mode 100644 index 000000000..6516a9ab7 --- /dev/null +++ b/Drivers/axp192-module/bindings/axp192-backlight.yaml @@ -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() diff --git a/Drivers/axp192-module/bindings/x-powers,axp192.yaml b/Drivers/axp192-module/bindings/x-powers,axp192.yaml new file mode 100644 index 000000000..1f4f6f03f --- /dev/null +++ b/Drivers/axp192-module/bindings/x-powers,axp192.yaml @@ -0,0 +1,5 @@ +description: X-Powers AXP192 power management IC + +include: ["i2c-device.yaml"] + +compatible: "x-powers,axp192" diff --git a/Drivers/axs15231b-module/devicetree.yaml b/Drivers/axp192-module/devicetree.yaml similarity index 100% rename from Drivers/axs15231b-module/devicetree.yaml rename to Drivers/axp192-module/devicetree.yaml diff --git a/Drivers/axs15231b-module/include/axs15231b_module.h b/Drivers/axp192-module/include/axp192_module.h similarity index 79% rename from Drivers/axs15231b-module/include/axs15231b_module.h rename to Drivers/axp192-module/include/axp192_module.h index 09f7591c7..06e0d7067 100644 --- a/Drivers/axs15231b-module/include/axs15231b_module.h +++ b/Drivers/axp192-module/include/axp192_module.h @@ -7,7 +7,7 @@ extern "C" { #endif -extern struct Module axs15231b_module; +extern struct Module axp192_module; #ifdef __cplusplus } diff --git a/Drivers/axp192-module/include/bindings/axp192.h b/Drivers/axp192-module/include/bindings/axp192.h new file mode 100644 index 000000000..15b557670 --- /dev/null +++ b/Drivers/axp192-module/include/bindings/axp192.h @@ -0,0 +1,15 @@ +// SPDX-License-Identifier: Apache-2.0 +#pragma once + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +DEFINE_DEVICETREE(axp192, struct Axp192Config) + +#ifdef __cplusplus +} +#endif diff --git a/Drivers/axp192-module/include/bindings/axp192_backlight.h b/Drivers/axp192-module/include/bindings/axp192_backlight.h new file mode 100644 index 000000000..a9fe12b54 --- /dev/null +++ b/Drivers/axp192-module/include/bindings/axp192_backlight.h @@ -0,0 +1,15 @@ +// SPDX-License-Identifier: Apache-2.0 +#pragma once + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +DEFINE_DEVICETREE(axp192_backlight, struct Axp192BacklightConfig) + +#ifdef __cplusplus +} +#endif diff --git a/Drivers/axp192-module/include/drivers/axp192.h b/Drivers/axp192-module/include/drivers/axp192.h new file mode 100644 index 000000000..40793d351 --- /dev/null +++ b/Drivers/axp192-module/include/drivers/axp192.h @@ -0,0 +1,81 @@ +// SPDX-License-Identifier: Apache-2.0 +#pragma once + +#include +#include + +#include + +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 diff --git a/Drivers/axp192-module/include/drivers/axp192_backlight.h b/Drivers/axp192-module/include/drivers/axp192_backlight.h new file mode 100644 index 000000000..ac883b3ad --- /dev/null +++ b/Drivers/axp192-module/include/drivers/axp192_backlight.h @@ -0,0 +1,29 @@ +// SPDX-License-Identifier: Apache-2.0 +#pragma once + +#include + +#include + +#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 diff --git a/Drivers/axp192-module/source/axp192.cpp b/Drivers/axp192-module/source/axp192.cpp new file mode 100644 index 000000000..c32959165 --- /dev/null +++ b/Drivers/axp192-module/source/axp192.cpp @@ -0,0 +1,445 @@ +// SPDX-License-Identifier: Apache-2.0 +#include +#include + +#include +#include +#include +#include +#include +#include + +#include + +#define GET_CONFIG(device) (static_cast((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((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, ®, &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, ®, &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((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((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((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((value & 0x0FU) | (step << 4U)); + } else { + value = static_cast((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((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(buffer[0]) << 16U) | (static_cast(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(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(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 +}; + +} diff --git a/Drivers/axp192-module/source/axp192_backlight.cpp b/Drivers/axp192-module/source/axp192_backlight.cpp new file mode 100644 index 000000000..969a989ed --- /dev/null +++ b/Drivers/axp192-module/source/axp192_backlight.cpp @@ -0,0 +1,132 @@ +// SPDX-License-Identifier: Apache-2.0 +#include +#include + +#include +#include +#include +#include +#include + +#include + +constexpr auto* TAG = "Axp192Backlight"; + +#define GET_CONFIG(device) (static_cast((device)->config)) +#define GET_INTERNAL(device) (static_cast(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( + config->min_millivolt + + (static_cast(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 +}; + +} diff --git a/Drivers/axp192-module/source/module.cpp b/Drivers/axp192-module/source/module.cpp new file mode 100644 index 000000000..6ed6069cb --- /dev/null +++ b/Drivers/axp192-module/source/module.cpp @@ -0,0 +1,56 @@ +// SPDX-License-Identifier: Apache-2.0 +#include +#include +#include +#include + +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 +}; + +} diff --git a/Drivers/axp2101-module/CMakeLists.txt b/Drivers/axp2101-module/CMakeLists.txt new file mode 100644 index 000000000..bcbab1296 --- /dev/null +++ b/Drivers/axp2101-module/CMakeLists.txt @@ -0,0 +1,11 @@ +cmake_minimum_required(VERSION 3.20) + +include("${CMAKE_CURRENT_LIST_DIR}/../../Buildscripts/module.cmake") + +file(GLOB_RECURSE SOURCE_FILES "source/*.c*") + +tactility_add_module(axp2101-module + SRCS ${SOURCE_FILES} + INCLUDE_DIRS include/ + REQUIRES TactilityKernel +) diff --git a/Drivers/axp2101-module/LICENSE-Apache-2.0.md b/Drivers/axp2101-module/LICENSE-Apache-2.0.md new file mode 100644 index 000000000..f5f4b8b5e --- /dev/null +++ b/Drivers/axp2101-module/LICENSE-Apache-2.0.md @@ -0,0 +1,195 @@ +Apache License +============== + +_Version 2.0, January 2004_ +_<>_ + +### Terms and Conditions for use, reproduction, and distribution + +#### 1. Definitions + +“License” shall mean the terms and conditions for use, reproduction, and +distribution as defined by Sections 1 through 9 of this document. + +“Licensor” shall mean the copyright owner or entity authorized by the copyright +owner that is granting the License. + +“Legal Entity” shall mean the union of the acting entity and all other entities +that control, are controlled by, or are under common control with that entity. +For the purposes of this definition, “control” means **(i)** the power, direct or +indirect, to cause the direction or management of such entity, whether by +contract or otherwise, or **(ii)** ownership of fifty percent (50%) or more of the +outstanding shares, or **(iii)** beneficial ownership of such entity. + +“You” (or “Your”) shall mean an individual or Legal Entity exercising +permissions granted by this License. + +“Source” form shall mean the preferred form for making modifications, including +but not limited to software source code, documentation source, and configuration +files. + +“Object” form shall mean any form resulting from mechanical transformation or +translation of a Source form, including but not limited to compiled object code, +generated documentation, and conversions to other media types. + +“Work” shall mean the work of authorship, whether in Source or Object form, made +available under the License, as indicated by a copyright notice that is included +in or attached to the work (an example is provided in the Appendix below). + +“Derivative Works” shall mean any work, whether in Source or Object form, that +is based on (or derived from) the Work and for which the editorial revisions, +annotations, elaborations, or other modifications represent, as a whole, an +original work of authorship. For the purposes of this License, Derivative Works +shall not include works that remain separable from, or merely link (or bind by +name) to the interfaces of, the Work and Derivative Works thereof. + +“Contribution” shall mean any work of authorship, including the original version +of the Work and any modifications or additions to that Work or Derivative Works +thereof, that is intentionally submitted to Licensor for inclusion in the Work +by the copyright owner or by an individual or Legal Entity authorized to submit +on behalf of the copyright owner. For the purposes of this definition, +“submitted” means any form of electronic, verbal, or written communication sent +to the Licensor or its representatives, including but not limited to +communication on electronic mailing lists, source code control systems, and +issue tracking systems that are managed by, or on behalf of, the Licensor for +the purpose of discussing and improving the Work, but excluding communication +that is conspicuously marked or otherwise designated in writing by the copyright +owner as “Not a Contribution.” + +“Contributor” shall mean Licensor and any individual or Legal Entity on behalf +of whom a Contribution has been received by Licensor and subsequently +incorporated within the Work. + +#### 2. Grant of Copyright License + +Subject to the terms and conditions of this License, each Contributor hereby +grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, +irrevocable copyright license to reproduce, prepare Derivative Works of, +publicly display, publicly perform, sublicense, and distribute the Work and such +Derivative Works in Source or Object form. + +#### 3. Grant of Patent License + +Subject to the terms and conditions of this License, each Contributor hereby +grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, +irrevocable (except as stated in this section) patent license to make, have +made, use, offer to sell, sell, import, and otherwise transfer the Work, where +such license applies only to those patent claims licensable by such Contributor +that are necessarily infringed by their Contribution(s) alone or by combination +of their Contribution(s) with the Work to which such Contribution(s) was +submitted. If You institute patent litigation against any entity (including a +cross-claim or counterclaim in a lawsuit) alleging that the Work or a +Contribution incorporated within the Work constitutes direct or contributory +patent infringement, then any patent licenses granted to You under this License +for that Work shall terminate as of the date such litigation is filed. + +#### 4. Redistribution + +You may reproduce and distribute copies of the Work or Derivative Works thereof +in any medium, with or without modifications, and in Source or Object form, +provided that You meet the following conditions: + +* **(a)** You must give any other recipients of the Work or Derivative Works a copy of +this License; and +* **(b)** You must cause any modified files to carry prominent notices stating that You +changed the files; and +* **(c)** You must retain, in the Source form of any Derivative Works that You distribute, +all copyright, patent, trademark, and attribution notices from the Source form +of the Work, excluding those notices that do not pertain to any part of the +Derivative Works; and +* **(d)** If the Work includes a “NOTICE” text file as part of its distribution, then any +Derivative Works that You distribute must include a readable copy of the +attribution notices contained within such NOTICE file, excluding those notices +that do not pertain to any part of the Derivative Works, in at least one of the +following places: within a NOTICE text file distributed as part of the +Derivative Works; within the Source form or documentation, if provided along +with the Derivative Works; or, within a display generated by the Derivative +Works, if and wherever such third-party notices normally appear. The contents of +the NOTICE file are for informational purposes only and do not modify the +License. You may add Your own attribution notices within Derivative Works that +You distribute, alongside or as an addendum to the NOTICE text from the Work, +provided that such additional attribution notices cannot be construed as +modifying the License. + +You may add Your own copyright statement to Your modifications and may provide +additional or different license terms and conditions for use, reproduction, or +distribution of Your modifications, or for any such Derivative Works as a whole, +provided Your use, reproduction, and distribution of the Work otherwise complies +with the conditions stated in this License. + +#### 5. Submission of Contributions + +Unless You explicitly state otherwise, any Contribution intentionally submitted +for inclusion in the Work by You to the Licensor shall be under the terms and +conditions of this License, without any additional terms or conditions. +Notwithstanding the above, nothing herein shall supersede or modify the terms of +any separate license agreement you may have executed with Licensor regarding +such Contributions. + +#### 6. Trademarks + +This License does not grant permission to use the trade names, trademarks, +service marks, or product names of the Licensor, except as required for +reasonable and customary use in describing the origin of the Work and +reproducing the content of the NOTICE file. + +#### 7. Disclaimer of Warranty + +Unless required by applicable law or agreed to in writing, Licensor provides the +Work (and each Contributor provides its Contributions) on an “AS IS” BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, +including, without limitation, any warranties or conditions of TITLE, +NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are +solely responsible for determining the appropriateness of using or +redistributing the Work and assume any risks associated with Your exercise of +permissions under this License. + +#### 8. Limitation of Liability + +In no event and under no legal theory, whether in tort (including negligence), +contract, or otherwise, unless required by applicable law (such as deliberate +and grossly negligent acts) or agreed to in writing, shall any Contributor be +liable to You for damages, including any direct, indirect, special, incidental, +or consequential damages of any character arising as a result of this License or +out of the use or inability to use the Work (including but not limited to +damages for loss of goodwill, work stoppage, computer failure or malfunction, or +any and all other commercial damages or losses), even if such Contributor has +been advised of the possibility of such damages. + +#### 9. Accepting Warranty or Additional Liability + +While redistributing the Work or Derivative Works thereof, You may choose to +offer, and charge a fee for, acceptance of support, warranty, indemnity, or +other liability obligations and/or rights consistent with this License. However, +in accepting such obligations, You may act only on Your own behalf and on Your +sole responsibility, not on behalf of any other Contributor, and only if You +agree to indemnify, defend, and hold each Contributor harmless for any liability +incurred by, or claims asserted against, such Contributor by reason of your +accepting any such warranty or additional liability. + +_END OF TERMS AND CONDITIONS_ + +### APPENDIX: How to apply the Apache License to your work + +To apply the Apache License to your work, attach the following boilerplate +notice, with the fields enclosed by brackets `[]` replaced with your own +identifying information. (Don't include the brackets!) The text should be +enclosed in the appropriate comment syntax for the file format. We also +recommend that a file or class name and description of purpose be included on +the same “printed page” as the copyright notice for easier identification within +third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + diff --git a/Drivers/axp2101-module/README.md b/Drivers/axp2101-module/README.md new file mode 100644 index 000000000..2c26300e3 --- /dev/null +++ b/Drivers/axp2101-module/README.md @@ -0,0 +1,13 @@ +# AXP2101 power management IC + +A driver for the X-Powers `AXP2101` PMIC: DCDC1-5 and ALDO1-4/BLDO1-2/CPUSLDO/DLDO1-2 +enable and voltage control, battery/VBUS voltage readback, charge enable/status, and +system power off. Registers as a `power-supply` child device (voltage, is-charging, +charge control, power off) alongside its own public API for full rail control. + +Also includes `axp2101-backlight`: a `BACKLIGHT_TYPE` driver for a backlight wired to one of +the AXP2101's LDOs, declared as a devicetree child node of the `axp2101` device it dims. Its +`ldo`, `min-millivolt` and `max-millivolt` properties select the channel and map the 0-255 +brightness level onto that LDO's voltage range; brightness level 0 disables the LDO outright. + +License: [Apache v2.0](LICENSE-Apache-2.0.md) diff --git a/Drivers/axp2101-module/bindings/axp2101-backlight.yaml b/Drivers/axp2101-module/bindings/axp2101-backlight.yaml new file mode 100644 index 000000000..4139a904c --- /dev/null +++ b/Drivers/axp2101-module/bindings/axp2101-backlight.yaml @@ -0,0 +1,24 @@ +description: > + Backlight driven by a switchable/adjustable AXP2101 LDO. Must be declared as a + child node of the axp2101 device it dims. Maps the 0-255 brightness level onto + the LDO's [min-millivolt,max-millivolt] voltage range; level 0 disables the LDO outright. + +compatible: "axp2101-backlight" + +properties: + ldo: + type: int + required: true + description: The Axp2101Ldo powering the backlight (e.g. AXP2101_DLDO1) + min-millivolt: + type: int + default: 0 + description: LDO voltage at brightness level 0 (the LDO is disabled rather than actually set to this value) + max-millivolt: + type: int + required: true + description: LDO voltage at the maximum brightness level (255) + brightness-default: + type: int + default: 255 + description: Default brightness level, applied by set_brightness_default() diff --git a/Drivers/axp2101-module/bindings/x-powers,axp2101.yaml b/Drivers/axp2101-module/bindings/x-powers,axp2101.yaml new file mode 100644 index 000000000..bee4019b3 --- /dev/null +++ b/Drivers/axp2101-module/bindings/x-powers,axp2101.yaml @@ -0,0 +1,5 @@ +description: X-Powers AXP2101 power management IC + +include: ["i2c-device.yaml"] + +compatible: "x-powers,axp2101" diff --git a/Drivers/axp2101-module/devicetree.yaml b/Drivers/axp2101-module/devicetree.yaml new file mode 100644 index 000000000..a07d6f334 --- /dev/null +++ b/Drivers/axp2101-module/devicetree.yaml @@ -0,0 +1,3 @@ +dependencies: + - TactilityKernel +bindings: bindings diff --git a/Drivers/axp2101-module/include/axp2101_module.h b/Drivers/axp2101-module/include/axp2101_module.h new file mode 100644 index 000000000..8e3fa772c --- /dev/null +++ b/Drivers/axp2101-module/include/axp2101_module.h @@ -0,0 +1,14 @@ +// SPDX-License-Identifier: Apache-2.0 +#pragma once + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +extern struct Module axp2101_module; + +#ifdef __cplusplus +} +#endif diff --git a/Drivers/axp2101-module/include/bindings/axp2101.h b/Drivers/axp2101-module/include/bindings/axp2101.h new file mode 100644 index 000000000..15154adba --- /dev/null +++ b/Drivers/axp2101-module/include/bindings/axp2101.h @@ -0,0 +1,15 @@ +// SPDX-License-Identifier: Apache-2.0 +#pragma once + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +DEFINE_DEVICETREE(axp2101, struct Axp2101Config) + +#ifdef __cplusplus +} +#endif diff --git a/Drivers/axp2101-module/include/bindings/axp2101_backlight.h b/Drivers/axp2101-module/include/bindings/axp2101_backlight.h new file mode 100644 index 000000000..607800805 --- /dev/null +++ b/Drivers/axp2101-module/include/bindings/axp2101_backlight.h @@ -0,0 +1,15 @@ +// SPDX-License-Identifier: Apache-2.0 +#pragma once + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +DEFINE_DEVICETREE(axp2101_backlight, struct Axp2101BacklightConfig) + +#ifdef __cplusplus +} +#endif diff --git a/Drivers/axp2101-module/include/drivers/axp2101.h b/Drivers/axp2101-module/include/drivers/axp2101.h new file mode 100644 index 000000000..00141822c --- /dev/null +++ b/Drivers/axp2101-module/include/drivers/axp2101.h @@ -0,0 +1,96 @@ +// SPDX-License-Identifier: Apache-2.0 +#pragma once + +#include +#include + +#include + +struct Device; + +#ifdef __cplusplus +extern "C" { +#endif + +struct Axp2101Config { + /** Address on bus */ + uint8_t address; +}; + +/** Switchable/adjustable DCDC (buck) converters of the AXP2101. */ +enum Axp2101Dcdc { + AXP2101_DCDC1, + AXP2101_DCDC2, + AXP2101_DCDC3, + AXP2101_DCDC4, + AXP2101_DCDC5, +}; + +/** Switchable/adjustable LDO regulators of the AXP2101. */ +enum Axp2101Ldo { + AXP2101_ALDO1, + AXP2101_ALDO2, + AXP2101_ALDO3, + AXP2101_ALDO4, + AXP2101_BLDO1, + AXP2101_BLDO2, + AXP2101_CPUSLDO, + AXP2101_DLDO1, + AXP2101_DLDO2, +}; + +/** Checks whether a DCDC converter is currently enabled. */ +error_t axp2101_is_dcdc_enabled(struct Device* device, enum Axp2101Dcdc dcdc, bool* enabled); + +/** Enables or disables a DCDC converter. */ +error_t axp2101_set_dcdc_enabled(struct Device* device, enum Axp2101Dcdc dcdc, bool enabled); + +/** + * @brief Sets the output voltage of a DCDC converter. + * @retval ERROR_INVALID_ARGUMENT when millivolts is outside the converter's supported range/step + * (DCDC1: 1500-3400mV/100mV; DCDC2: 500-1200mV/10mV or 1220-1540mV/20mV; + * DCDC3: 500-1200mV/10mV, 1220-1540mV/20mV or 1600-3400mV/100mV; + * DCDC4: 500-1200mV/10mV or 1220-1840mV/20mV; DCDC5: 1200mV, or 1400-3700mV/100mV) + */ +error_t axp2101_set_dcdc_voltage(struct Device* device, enum Axp2101Dcdc dcdc, uint16_t millivolts); + +/** Checks whether an LDO regulator is currently enabled. */ +error_t axp2101_is_ldo_enabled(struct Device* device, enum Axp2101Ldo ldo, bool* enabled); + +/** Enables or disables an LDO regulator. */ +error_t axp2101_set_ldo_enabled(struct Device* device, enum Axp2101Ldo ldo, bool enabled); + +/** + * @brief Sets the output voltage of an LDO regulator. + * @retval ERROR_INVALID_ARGUMENT when millivolts is outside the regulator's supported range/step + * (ALDO1-4/BLDO1-2: 500-3500mV/100mV; CPUSLDO: 500-1400mV/50mV; DLDO1-2: 500-3400mV/100mV) + */ +error_t axp2101_set_ldo_voltage(struct Device* device, enum Axp2101Ldo ldo, uint16_t millivolts); + +/** Battery voltage in millivolts (0 when no battery is connected). */ +error_t axp2101_get_battery_voltage(struct Device* device, uint16_t* millivolts); + +/** Whether a battery is currently detected. */ +error_t axp2101_is_battery_connected(struct Device* device, bool* connected); + +/** Whether VBUS (USB power) is currently present and usable. */ +error_t axp2101_is_vbus_present(struct Device* device, bool* present); + +/** VBUS voltage in millivolts (0 when VBUS is not present). */ +error_t axp2101_get_vbus_voltage(struct Device* device, uint16_t* millivolts); + +/** Whether the battery is currently charging. */ +error_t axp2101_is_charging(struct Device* device, bool* charging); + +/** Whether the charger is allowed to charge the battery. */ +error_t axp2101_is_charge_enabled(struct Device* device, bool* enabled); + +/** Enables or disables battery charging. */ +error_t axp2101_set_charge_enabled(struct Device* device, bool enabled); + +/** Powers off the system (does not return on success). */ +error_t axp2101_power_off(struct Device* device); + +#ifdef __cplusplus +} +#endif diff --git a/Drivers/axp2101-module/include/drivers/axp2101_backlight.h b/Drivers/axp2101-module/include/drivers/axp2101_backlight.h new file mode 100644 index 000000000..49db8280e --- /dev/null +++ b/Drivers/axp2101-module/include/drivers/axp2101_backlight.h @@ -0,0 +1,29 @@ +// SPDX-License-Identifier: Apache-2.0 +#pragma once + +#include + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Devicetree configuration for a backlight driven by a switchable/adjustable AXP2101 LDO. + */ +struct Axp2101BacklightConfig { + /** The AXP2101 LDO powering the backlight */ + enum Axp2101Ldo ldo; + /** LDO voltage at brightness level 0. Brightness level 0 always disables the LDO outright, + * rather than actually driving it to this voltage - see set_brightness() in BacklightApi. */ + uint16_t min_millivolt; + /** LDO 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 diff --git a/Drivers/axp2101-module/source/axp2101.cpp b/Drivers/axp2101-module/source/axp2101.cpp new file mode 100644 index 000000000..58d60f59c --- /dev/null +++ b/Drivers/axp2101-module/source/axp2101.cpp @@ -0,0 +1,555 @@ +// SPDX-License-Identifier: Apache-2.0 +#include +#include + +#include +#include +#include +#include +#include +#include + +#include + +#define TAG "AXP2101" +#define GET_CONFIG(device) (static_cast((device)->config)) + +/** Reference: https://github.com/lewisxhe/XPowersLib (AXP2101 register map and voltage-encoding formulas) */ +static constexpr uint8_t REG_STATUS1 = 0x00U; // bit5: VBUS good +static constexpr uint8_t REG_STATUS2 = 0x01U; // bits[6:5]: charge status (1=charging), bit3: battery connected(?) +static constexpr uint8_t REG_COMMON_CONFIG = 0x10U; // bit0: shutdown +static constexpr uint8_t REG_CHARGE_GAUGE_WDT_CTRL = 0x18U; // bit1: charge enable +static constexpr uint8_t REG_ADC_DATA_RESULT0 = 0x34U; // battery voltage, high 5 bits +static constexpr uint8_t REG_ADC_DATA_RESULT1 = 0x35U; // battery voltage, low 8 bits +static constexpr uint8_t REG_ADC_DATA_RESULT4 = 0x38U; // VBUS voltage, high 6 bits +static constexpr uint8_t REG_ADC_DATA_RESULT5 = 0x39U; // VBUS voltage, low 8 bits +static constexpr uint8_t REG_DC_ONOFF_DVM_CTRL = 0x80U; // bits0-4: DCDC1-5 enable +static constexpr uint8_t REG_DC_VOL0_CTRL = 0x82U; // DCDC1 voltage; DCDC2-5 follow at +1..+4 +static constexpr uint8_t REG_LDO_ONOFF_CTRL0 = 0x90U; // bits0-7: ALDO1-4, BLDO1-2, CPUSLDO, DLDO1 enable +static constexpr uint8_t REG_LDO_ONOFF_CTRL1 = 0x91U; // bit0: DLDO2 enable +static constexpr uint8_t REG_LDO_VOL0_CTRL = 0x92U; // ALDO1 voltage; remaining LDOs follow at +1..+8 +static constexpr uint8_t REG_ADC_CHANNEL_CTRL = 0x30U; // bit0: battery voltage, bit2: VBUS voltage + +static constexpr uint8_t BIT_VBUS_GOOD = 1U << 5U; +static constexpr uint8_t BIT_SHUTDOWN = 1U << 0U; +static constexpr uint8_t BIT_CHARGE_ENABLED = 1U << 1U; + +static constexpr TickType_t TIMEOUT = pdMS_TO_TICKS(50); + +extern "C" { + +extern Module axp2101_module; + +// region Voltage encoding + +struct Axp2101VoltRange { + uint16_t min; + uint16_t max; + uint16_t step; + uint8_t code_base; +}; + +static error_t encode_ranged_voltage(uint16_t millivolts, const Axp2101VoltRange* ranges, size_t range_count, uint8_t* out_code) { + for (size_t i = 0; i < range_count; i++) { + const Axp2101VoltRange& range = ranges[i]; + if (millivolts >= range.min && millivolts <= range.max) { + if ((millivolts - range.min) % range.step != 0U) { + return ERROR_INVALID_ARGUMENT; + } + *out_code = static_cast(range.code_base + (millivolts - range.min) / range.step); + return ERROR_NONE; + } + } + return ERROR_INVALID_ARGUMENT; +} + +static error_t write_masked_register(Device* device, uint8_t reg, uint8_t preserve_mask, uint8_t code) { + auto* parent = device_get_parent(device); + auto address = GET_CONFIG(device)->address; + + if (preserve_mask == 0U) { + return i2c_controller_register8_set(parent, address, reg, code, TIMEOUT); + } + + uint8_t value; + error_t err = i2c_controller_register8_get(parent, address, reg, &value, TIMEOUT); + if (err != ERROR_NONE) { + return err; + } + value = static_cast((value & preserve_mask) | code); + return i2c_controller_register8_set(parent, address, reg, value, TIMEOUT); +} + +// endregion + +// region DCDC + +static error_t get_dcdc_enable_bit(Axp2101Dcdc dcdc, uint8_t* bit) { + if (dcdc < AXP2101_DCDC1 || dcdc > AXP2101_DCDC5) { + return ERROR_INVALID_ARGUMENT; + } + *bit = static_cast(1U << dcdc); + return ERROR_NONE; +} + +error_t axp2101_is_dcdc_enabled(Device* device, Axp2101Dcdc dcdc, bool* enabled) { + uint8_t bit; + error_t err = get_dcdc_enable_bit(dcdc, &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_DC_ONOFF_DVM_CTRL, &value, TIMEOUT); + if (err != ERROR_NONE) { + return err; + } + + *enabled = (value & bit) != 0U; + return ERROR_NONE; +} + +error_t axp2101_set_dcdc_enabled(Device* device, Axp2101Dcdc dcdc, bool enabled) { + uint8_t bit; + error_t err = get_dcdc_enable_bit(dcdc, &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_DC_ONOFF_DVM_CTRL, bit, TIMEOUT); + } else { + return i2c_controller_register8_reset_bits(parent, address, REG_DC_ONOFF_DVM_CTRL, bit, TIMEOUT); + } +} + +error_t axp2101_set_dcdc_voltage(Device* device, Axp2101Dcdc dcdc, uint16_t millivolts) { + uint8_t reg = static_cast(REG_DC_VOL0_CTRL + dcdc); + uint8_t code; + error_t err; + + switch (dcdc) { + case AXP2101_DCDC1: { + static constexpr Axp2101VoltRange ranges[] = { { 1500, 3400, 100, 0 } }; + err = encode_ranged_voltage(millivolts, ranges, 1, &code); + if (err != ERROR_NONE) { + return err; + } + return write_masked_register(device, reg, 0x00U, code); + } + case AXP2101_DCDC2: { + static constexpr Axp2101VoltRange ranges[] = { { 500, 1200, 10, 0 }, { 1220, 1540, 20, 71 } }; + err = encode_ranged_voltage(millivolts, ranges, 2, &code); + if (err != ERROR_NONE) { + return err; + } + return write_masked_register(device, reg, 0x80U, code); + } + case AXP2101_DCDC3: { + static constexpr Axp2101VoltRange ranges[] = { { 500, 1200, 10, 0 }, { 1220, 1540, 20, 71 }, { 1600, 3400, 100, 88 } }; + err = encode_ranged_voltage(millivolts, ranges, 3, &code); + if (err != ERROR_NONE) { + return err; + } + return write_masked_register(device, reg, 0x80U, code); + } + case AXP2101_DCDC4: { + static constexpr Axp2101VoltRange ranges[] = { { 500, 1200, 10, 0 }, { 1220, 1840, 20, 71 } }; + err = encode_ranged_voltage(millivolts, ranges, 2, &code); + if (err != ERROR_NONE) { + return err; + } + return write_masked_register(device, reg, 0x80U, code); + } + case AXP2101_DCDC5: { + // DCDC5 datasheet quirk: 1200mV maps to a fixed out-of-sequence code, distinct + // from the linear 1400-3700mV range (see XPowersLib's setDC5Voltage()). + if (millivolts == 1200U) { + return write_masked_register(device, reg, 0xE0U, 0x19U); + } + static constexpr Axp2101VoltRange ranges[] = { { 1400, 3700, 100, 0 } }; + err = encode_ranged_voltage(millivolts, ranges, 1, &code); + if (err != ERROR_NONE) { + return err; + } + return write_masked_register(device, reg, 0xE0U, code); + } + } + return ERROR_INVALID_ARGUMENT; +} + +// endregion + +// region LDO + +static error_t get_ldo_enable_location(Axp2101Ldo ldo, uint8_t* reg, uint8_t* bit) { + if (ldo < AXP2101_ALDO1 || ldo > AXP2101_DLDO2) { + return ERROR_INVALID_ARGUMENT; + } + if (ldo == AXP2101_DLDO2) { + *reg = REG_LDO_ONOFF_CTRL1; + *bit = 1U << 0U; + } else { + *reg = REG_LDO_ONOFF_CTRL0; + *bit = static_cast(1U << ldo); + } + return ERROR_NONE; +} + +error_t axp2101_is_ldo_enabled(Device* device, Axp2101Ldo ldo, bool* enabled) { + uint8_t reg, bit; + error_t err = get_ldo_enable_location(ldo, ®, &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 axp2101_set_ldo_enabled(Device* device, Axp2101Ldo ldo, bool enabled) { + uint8_t reg, bit; + error_t err = get_ldo_enable_location(ldo, ®, &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 axp2101_set_ldo_voltage(Device* device, Axp2101Ldo ldo, uint16_t millivolts) { + if (ldo < AXP2101_ALDO1 || ldo > AXP2101_DLDO2) { + return ERROR_INVALID_ARGUMENT; + } + + static constexpr Axp2101VoltRange LDO_RANGE[] = { + { 500, 3500, 100, 0 }, // ALDO1 + { 500, 3500, 100, 0 }, // ALDO2 + { 500, 3500, 100, 0 }, // ALDO3 + { 500, 3500, 100, 0 }, // ALDO4 + { 500, 3500, 100, 0 }, // BLDO1 + { 500, 3500, 100, 0 }, // BLDO2 + { 500, 1400, 50, 0 }, // CPUSLDO + { 500, 3400, 100, 0 }, // DLDO1 + { 500, 3400, 100, 0 }, // DLDO2 + }; + + uint8_t code; + error_t err = encode_ranged_voltage(millivolts, &LDO_RANGE[ldo], 1, &code); + if (err != ERROR_NONE) { + return err; + } + + uint8_t reg = static_cast(REG_LDO_VOL0_CTRL + ldo); + return write_masked_register(device, reg, 0xE0U, code); +} + +// endregion + +error_t axp2101_get_battery_voltage(Device* device, uint16_t* millivolts) { + bool connected; + error_t err = axp2101_is_battery_connected(device, &connected); + if (err != ERROR_NONE) { + return err; + } + if (!connected) { + *millivolts = 0; + return ERROR_NONE; + } + + auto* parent = device_get_parent(device); + auto address = GET_CONFIG(device)->address; + uint8_t high, low; + err = i2c_controller_register8_get(parent, address, REG_ADC_DATA_RESULT0, &high, TIMEOUT); + if (err != ERROR_NONE) { + return err; + } + err = i2c_controller_register8_get(parent, address, REG_ADC_DATA_RESULT1, &low, TIMEOUT); + if (err != ERROR_NONE) { + return err; + } + *millivolts = static_cast(((high & 0x1FU) << 8U) | low); + return ERROR_NONE; +} + +error_t axp2101_is_battery_connected(Device* device, bool* connected) { + 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_STATUS1, &value, TIMEOUT); + if (err != ERROR_NONE) { + return err; + } + *connected = (value & (1U << 3U)) != 0U; + return ERROR_NONE; +} + +error_t axp2101_is_vbus_present(Device* device, bool* present) { + 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_STATUS1, &value, TIMEOUT); + if (err != ERROR_NONE) { + return err; + } + *present = (value & BIT_VBUS_GOOD) != 0U; + return ERROR_NONE; +} + +error_t axp2101_get_vbus_voltage(Device* device, uint16_t* millivolts) { + bool present; + error_t err = axp2101_is_vbus_present(device, &present); + if (err != ERROR_NONE) { + return err; + } + if (!present) { + *millivolts = 0; + return ERROR_NONE; + } + + auto* parent = device_get_parent(device); + auto address = GET_CONFIG(device)->address; + uint8_t high, low; + err = i2c_controller_register8_get(parent, address, REG_ADC_DATA_RESULT4, &high, TIMEOUT); + if (err != ERROR_NONE) { + return err; + } + err = i2c_controller_register8_get(parent, address, REG_ADC_DATA_RESULT5, &low, TIMEOUT); + if (err != ERROR_NONE) { + return err; + } + *millivolts = static_cast(((high & 0x3FU) << 8U) | low); + return ERROR_NONE; +} + +error_t axp2101_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_STATUS2, &value, TIMEOUT); + if (err != ERROR_NONE) { + return err; + } + *charging = ((value >> 5U) & 0x03U) == 0x01U; + return ERROR_NONE; +} + +error_t axp2101_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_GAUGE_WDT_CTRL, &value, TIMEOUT); + if (err != ERROR_NONE) { + return err; + } + *enabled = (value & BIT_CHARGE_ENABLED) != 0U; + return ERROR_NONE; +} + +error_t axp2101_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_GAUGE_WDT_CTRL, BIT_CHARGE_ENABLED, TIMEOUT); + } else { + return i2c_controller_register8_reset_bits(parent, address, REG_CHARGE_GAUGE_WDT_CTRL, BIT_CHARGE_ENABLED, TIMEOUT); + } +} + +error_t axp2101_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_COMMON_CONFIG, BIT_SHUTDOWN, 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; +} + +static error_t ps_get_property(Device* device, PowerSupplyProperty property, PowerSupplyPropertyValue* out_value) { + // device_get_parent() here is the axp2101 device itself (this child's parent), not the I2C bus. + auto* axp2101_device = device_get_parent(device); + + switch (property) { + case POWER_SUPPLY_PROP_IS_CHARGING: { + bool charging; + error_t err = axp2101_is_charging(axp2101_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 = axp2101_get_battery_voltage(axp2101_device, &millivolts); + if (err != ERROR_NONE) { + return err; + } + out_value->int_value = millivolts; + 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; + axp2101_is_charge_enabled(device_get_parent(device), &enabled); + return enabled; +} + +static error_t ps_set_allowed_to_charge(Device* device, bool allowed) { + return axp2101_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 axp2101_power_off(device_get_parent(device)); } + +static constexpr PowerSupplyApi AXP2101_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: axp2101_driver wires it up directly by pointer. +Driver axp2101_power_supply_driver = { + .name = "axp2101-power-supply", + .compatible = (const char*[]) { "axp2101-power-supply", nullptr }, + .start_device = nullptr, + .stop_device = nullptr, + .api = &AXP2101_POWER_SUPPLY_API, + .device_type = &POWER_SUPPLY_TYPE, + .owner = &axp2101_module, + .internal = nullptr +}; + +struct Axp2101Internal { + 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 = "axp2101-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, &axp2101_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 address = GET_CONFIG(device)->address; + // Battery/VBUS voltage ADC channels are off by default; axp2101_get_battery_voltage() + // and axp2101_get_vbus_voltage() need them on to read anything but 0. + error_t error = i2c_controller_register8_set_bits(parent, address, REG_ADC_CHANNEL_CTRL, (1U << 0U) | (1U << 2U), TIMEOUT); + if (error != ERROR_NONE) { + LOG_W(TAG, "Failed to enable battery/VBUS ADC channels"); + return error; + } + + auto* internal = new(std::nothrow) Axp2101Internal(); + if (internal == nullptr) { + return ERROR_OUT_OF_MEMORY; + } + + 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(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 axp2101_driver = { + .name = "axp2101", + .compatible = (const char*[]) { "x-powers,axp2101", nullptr }, + .start_device = start, + .stop_device = stop, + .api = nullptr, + .device_type = nullptr, + .owner = &axp2101_module, + .internal = nullptr +}; + +} diff --git a/Drivers/axp2101-module/source/axp2101_backlight.cpp b/Drivers/axp2101-module/source/axp2101_backlight.cpp new file mode 100644 index 000000000..d3c5c756a --- /dev/null +++ b/Drivers/axp2101-module/source/axp2101_backlight.cpp @@ -0,0 +1,140 @@ +// SPDX-License-Identifier: Apache-2.0 +#include +#include + +#include +#include +#include +#include +#include + +#include + +#define TAG "Axp2101Backlight" +#define GET_CONFIG(device) (static_cast((device)->config)) +#define GET_INTERNAL(device) (static_cast(device_get_driver_data(device))) + +extern "C" { + +struct Axp2101BacklightInternal { + uint8_t brightness; +}; + +// region BacklightApi + +// Step size of axp2101_set_ldo_voltage()'s underlying register encoding (see axp2101.cpp's LDO_RANGE table). +static uint16_t get_ldo_voltage_step(Axp2101Ldo ldo) { + return ldo == AXP2101_CPUSLDO ? 50U : 100U; +} + +static error_t apply_brightness(Device* device, uint8_t brightness) { + const auto* config = GET_CONFIG(device); + auto* axp2101 = device_get_parent(device); + + if (brightness == 0) { + return axp2101_set_ldo_enabled(axp2101, config->ldo, false); + } + + uint16_t step = get_ldo_voltage_step(config->ldo); + uint16_t raw_millivolt = static_cast( + config->min_millivolt + + (static_cast(brightness) * (config->max_millivolt - config->min_millivolt)) / 255U + ); + // Round to the nearest valid step; the LDO's voltage range always starts at a multiple of every + // supported step, so rounding from zero keeps the result on a valid boundary. + uint16_t millivolt = static_cast(((raw_millivolt + step / 2U) / step) * step); + if (millivolt < config->min_millivolt) { + millivolt = config->min_millivolt; + } else if (millivolt > config->max_millivolt) { + millivolt = config->max_millivolt; + } + + error_t error = axp2101_set_ldo_voltage(axp2101, config->ldo, millivolt); + if (error != ERROR_NONE) { + LOG_E(TAG, "Failed to set LDO voltage"); + return error; + } + + return axp2101_set_ldo_enabled(axp2101, config->ldo, true); +} + +static error_t axp2101_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 axp2101_backlight_set_brightness_default(Device* device) { + return axp2101_backlight_set_brightness(device, GET_CONFIG(device)->brightness_default); +} + +static error_t axp2101_backlight_get_brightness(Device* device, uint8_t* out_brightness) { + *out_brightness = GET_INTERNAL(device)->brightness; + return ERROR_NONE; +} + +static uint8_t axp2101_backlight_get_min_brightness(Device*) { + return 0; +} + +static uint8_t axp2101_backlight_get_max_brightness(Device*) { + return 255; +} + +// endregion + +static constexpr BacklightApi AXP2101_BACKLIGHT_API = { + .set_brightness = axp2101_backlight_set_brightness, + .set_brightness_default = axp2101_backlight_set_brightness_default, + .get_brightness = axp2101_backlight_get_brightness, + .get_min_brightness = axp2101_backlight_get_min_brightness, + .get_max_brightness = axp2101_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) Axp2101BacklightInternal { .brightness = 0 }; + if (internal == nullptr) { + return ERROR_OUT_OF_MEMORY; + } + + device_set_driver_data(device, internal); + + axp2101_backlight_set_brightness_default(device); // Allowed to fail, we don't care about the result + + return ERROR_NONE; +} + +static error_t stop(Device* device) { + axp2101_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 axp2101_backlight_driver = { + .name = "axp2101_backlight", + .compatible = (const char*[]) { "axp2101-backlight", nullptr }, + .start_device = start, + .stop_device = stop, + .api = &AXP2101_BACKLIGHT_API, + .device_type = &BACKLIGHT_TYPE, + .owner = &axp2101_module, + .internal = nullptr +}; + +} diff --git a/Drivers/axp2101-module/source/module.cpp b/Drivers/axp2101-module/source/module.cpp new file mode 100644 index 000000000..daa0db564 --- /dev/null +++ b/Drivers/axp2101-module/source/module.cpp @@ -0,0 +1,57 @@ +// SPDX-License-Identifier: Apache-2.0 +#include +#include +#include +#include + +extern "C" { + +extern Driver axp2101_driver; +extern Driver axp2101_power_supply_driver; +extern Driver axp2101_backlight_driver; + +const struct ModuleSymbol axp2101_module_symbols[] = { + DEFINE_MODULE_SYMBOL(axp2101_is_dcdc_enabled), + DEFINE_MODULE_SYMBOL(axp2101_set_dcdc_enabled), + DEFINE_MODULE_SYMBOL(axp2101_set_dcdc_voltage), + DEFINE_MODULE_SYMBOL(axp2101_is_ldo_enabled), + DEFINE_MODULE_SYMBOL(axp2101_set_ldo_enabled), + DEFINE_MODULE_SYMBOL(axp2101_set_ldo_voltage), + DEFINE_MODULE_SYMBOL(axp2101_get_battery_voltage), + DEFINE_MODULE_SYMBOL(axp2101_is_battery_connected), + DEFINE_MODULE_SYMBOL(axp2101_is_vbus_present), + DEFINE_MODULE_SYMBOL(axp2101_get_vbus_voltage), + DEFINE_MODULE_SYMBOL(axp2101_is_charging), + DEFINE_MODULE_SYMBOL(axp2101_is_charge_enabled), + DEFINE_MODULE_SYMBOL(axp2101_set_charge_enabled), + DEFINE_MODULE_SYMBOL(axp2101_power_off), + 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(&axp2101_driver) == ERROR_NONE); + check(driver_construct_add(&axp2101_power_supply_driver) == ERROR_NONE); + check(driver_construct_add(&axp2101_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(&axp2101_backlight_driver) == ERROR_NONE); + check(driver_remove_destruct(&axp2101_power_supply_driver) == ERROR_NONE); + check(driver_remove_destruct(&axp2101_driver) == ERROR_NONE); + return ERROR_NONE; +} + +Module axp2101_module = { + .name = "axp2101", + .start = start, + .stop = stop, + .symbols = axp2101_module_symbols, + .internal = nullptr +}; + +} diff --git a/Drivers/axs15231b-module/bindings/axs,axs15231b-touch.yaml b/Drivers/axs15231b-module/bindings/axs,axs15231b-touch.yaml deleted file mode 100644 index 818eb6cc5..000000000 --- a/Drivers/axs15231b-module/bindings/axs,axs15231b-touch.yaml +++ /dev/null @@ -1,45 +0,0 @@ -description: AXS15231B touch controller (I2C side of the combined display+touch chip) - -include: ["i2c-device.yaml"] - -compatible: "axs,axs15231b-touch" - -bus: i2c - -properties: - x-max: - type: int - required: true - description: Maximum X coordinate reported by the controller (typically the panel's horizontal resolution) - y-max: - type: int - required: true - description: Maximum Y coordinate reported by the controller (typically the panel's vertical resolution) - swap-xy: - type: boolean - default: false - description: Swap the X and Y axes - mirror-x: - type: boolean - default: false - description: Mirror the X axis - mirror-y: - type: boolean - default: false - description: Mirror the Y axis - pin-reset: - type: phandles - default: GPIO_PIN_SPEC_NONE - description: Reset GPIO pin - pin-interrupt: - type: phandles - default: GPIO_PIN_SPEC_NONE - description: Interrupt GPIO pin - reset-active-high: - type: boolean - default: false - description: Whether the reset pin is active high - interrupt-active-high: - type: boolean - default: false - description: Whether the interrupt pin is active high diff --git a/Drivers/axs15231b-module/bindings/axs,axs15231b.yaml b/Drivers/axs15231b-module/bindings/axs,axs15231b.yaml deleted file mode 100644 index 1ea3cacc1..000000000 --- a/Drivers/axs15231b-module/bindings/axs,axs15231b.yaml +++ /dev/null @@ -1,75 +0,0 @@ -description: > - AXS15231B display panel (QSPI interface). Combined display+touch controller chip - see - axs,axs15231b-touch for the touch side, which sits on a separate I2C bus and is modeled as an - independent devicetree node. - -compatible: "axs,axs15231b" - -bus: spi - -properties: - horizontal-resolution: - type: int - required: true - description: Horizontal resolution in pixels - vertical-resolution: - type: int - required: true - description: Vertical resolution in pixels - mirror-x: - type: boolean - default: false - description: Mirror the X axis - mirror-y: - type: boolean - default: false - description: Mirror the Y axis - invert-color: - type: boolean - default: false - description: Invert the panel's color output - bgr-order: - type: boolean - default: false - description: Use BGR element order instead of RGB - pixel-clock-hz: - type: int - default: 40000000 - description: QSPI pixel clock frequency in Hz - transaction-queue-depth: - type: int - default: 10 - description: Size of the internal SPI transaction queue - pin-reset: - type: phandles - default: GPIO_PIN_SPEC_NONE - description: Reset GPIO pin - reset-active-high: - type: boolean - default: false - description: Whether the reset pin is active high - pin-te: - type: phandles - default: GPIO_PIN_SPEC_NONE - description: Optional Tearing-Effect GPIO pin. When set, draw_bitmap waits (best-effort, up - to 20ms) for a V-blank pulse on this pin before starting each transfer, to reduce visible - tearing. Omit to skip TE sync entirely. - init-sequence: - type: array - element-type: uint8_t - description: > - Custom vendor bring-up sequence, flattened into bytes as a run of - [cmd, data-length, delay-ms, data-length bytes of data...] entries. Omit to use the - AXS15231B component's own built-in default sequence. - requires-full-frame: - type: boolean - default: false - description: > - Whether this panel needs full-frame-only draws (DISPLAY_CAPABILITY_REQUIRES_FULL_FRAME) - - a sub-region draw_bitmap() call desyncs this chip's row auto-increment counter, since its - QSPI command set has no row-address command. - It's not certain that this is required for all driver implementations, so it's a config option for now. - backlight: - type: phandle - default: "NULL" - description: Optional reference to this display's backlight device diff --git a/Drivers/axs15231b-module/include/bindings/axs15231b_display.h b/Drivers/axs15231b-module/include/bindings/axs15231b_display.h deleted file mode 100644 index 5f2e73637..000000000 --- a/Drivers/axs15231b-module/include/bindings/axs15231b_display.h +++ /dev/null @@ -1,10 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -#pragma once - -#include -#include - -// The devicetree compiler derives the expected config typedef name from the compatible -// string's suffix (e.g. "axs,axs15231b" -> axs15231b_config_dt), not from the node name or -// driver name, so the tag here must match that exactly. -DEFINE_DEVICETREE(axs15231b, struct Axs15231bDisplayConfig) diff --git a/Drivers/axs15231b-module/include/bindings/axs15231b_touch.h b/Drivers/axs15231b-module/include/bindings/axs15231b_touch.h deleted file mode 100644 index 8455f5cba..000000000 --- a/Drivers/axs15231b-module/include/bindings/axs15231b_touch.h +++ /dev/null @@ -1,10 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -#pragma once - -#include -#include - -// The devicetree compiler derives the expected config typedef name from the compatible -// string's suffix (e.g. "axs,axs15231b-touch" -> axs15231b_touch_config_dt), not from the node -// name or driver name, so the tag here must match that exactly. -DEFINE_DEVICETREE(axs15231b_touch, struct Axs15231bTouchConfig) diff --git a/Drivers/axs15231b-module/include/drivers/axs15231b_display.h b/Drivers/axs15231b-module/include/drivers/axs15231b_display.h deleted file mode 100644 index 98489e3c9..000000000 --- a/Drivers/axs15231b-module/include/drivers/axs15231b_display.h +++ /dev/null @@ -1,48 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -#pragma once - -#ifdef __cplusplus -extern "C" { -#endif - -#include -#include - -#include -#include - -struct Axs15231bDisplayConfig { - uint16_t horizontal_resolution; - uint16_t vertical_resolution; - bool mirror_x; - bool mirror_y; - bool invert_color; - bool bgr_order; - uint32_t pixel_clock_hz; - uint32_t transaction_queue_depth; - - // Reset pin. GPIO_PIN_SPEC_NONE means no reset line is wired up (matches the original - // deprecated-HAL config for the boards using this chip so far). - struct GpioPinSpec pin_reset; - bool reset_active_high; - - // Optional Tearing-Effect GPIO pin. When set (not GPIO_PIN_SPEC_NONE), draw_bitmap() waits - // (best-effort, up to 20ms) for a V-blank pulse on this pin before starting each transfer, to - // reduce visible tearing. GPIO_PIN_SPEC_NONE skips TE sync entirely. - struct GpioPinSpec pin_te; - - // Custom vendor init sequence, flattened as bytes: a run of - // [cmd, data_len, delay_ms, data_len bytes of data...] entries. NULL/0 falls back to the - // AXS15231B component's own built-in default sequence. - const uint8_t* init_sequence; - uint32_t init_sequence_length; - - bool requires_full_frame; - - // Optional reference to this display's backlight device, NULL if none. - struct Device* backlight; -}; - -#ifdef __cplusplus -} -#endif diff --git a/Drivers/axs15231b-module/include/drivers/axs15231b_touch.h b/Drivers/axs15231b-module/include/drivers/axs15231b_touch.h deleted file mode 100644 index 47c041dba..000000000 --- a/Drivers/axs15231b-module/include/drivers/axs15231b_touch.h +++ /dev/null @@ -1,31 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -#pragma once - -#ifdef __cplusplus -extern "C" { -#endif - -#include -#include - -#include -#include - -struct Axs15231bTouchConfig { - // Devicetree address hint. Unused by the driver: the AXS15231B always sits at a fixed - // I2C address (see ESP_LCD_TOUCH_IO_I2C_AXS15231B_ADDRESS in esp_lcd_axs15231b.h). - uint8_t address; - uint16_t x_max; - uint16_t y_max; - bool swap_xy; - bool mirror_x; - bool mirror_y; - struct GpioPinSpec pin_reset; - struct GpioPinSpec pin_interrupt; - bool reset_active_high; - bool interrupt_active_high; -}; - -#ifdef __cplusplus -} -#endif diff --git a/Drivers/axs15231b-module/source/axs15231b_display.cpp b/Drivers/axs15231b-module/source/axs15231b_display.cpp deleted file mode 100644 index 732977ea5..000000000 --- a/Drivers/axs15231b-module/source/axs15231b_display.cpp +++ /dev/null @@ -1,501 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include - -#include -#include - -#include - -#define TAG "AXS15231B" -#define GET_CONFIG(device) (static_cast((device)->config)) - -struct Axs15231bDisplayInternal { - esp_lcd_panel_io_handle_t io_handle; - esp_lcd_panel_handle_t panel_handle; - // Given from ISR context by on_color_trans_done() once a queued transfer physically - // completes. draw_bitmap() blocks on this so it can honor DisplayApi's synchronous contract - // (see lvgl_display.c: the caller reuses/overwrites the color buffer as soon as draw_bitmap - // returns) - esp_lcd_panel_draw_bitmap() itself only queues the transfer and returns early. - SemaphoreHandle_t draw_done_semaphore; - // Non-null only when a TE (Tearing-Effect) pin is configured. Signaled by te_isr_handler() - // on each rising edge, so draw_bitmap() can wait for the next V-blank before transferring. - SemaphoreHandle_t te_semaphore; - bool te_isr_installed; - // Whether we're the one who called gpio_install_isr_service() - if so, we must be the one to - // uninstall it, but only if no other pin on the system is still relying on it. - bool te_isr_service_installed_by_us; - axs15231b_lcd_init_cmd_t* parsed_init_cmds; -}; - -static bool IRAM_ATTR on_color_trans_done(esp_lcd_panel_io_handle_t, esp_lcd_panel_io_event_data_t*, void* user_ctx) { - auto* internal = static_cast(user_ctx); - BaseType_t high_task_woken = pdFALSE; - xSemaphoreGiveFromISR(internal->draw_done_semaphore, &high_task_woken); - return high_task_woken == pdTRUE; -} - -static void IRAM_ATTR te_isr_handler(void* arg) { - auto* semaphore = static_cast(arg); - BaseType_t high_task_woken = pdFALSE; - xSemaphoreGiveFromISR(semaphore, &high_task_woken); - if (high_task_woken == pdTRUE) { - portYIELD_FROM_ISR(); - } -} - -static int pin_or_unused(const struct GpioPinSpec& pin) { - return pin.gpio_controller == nullptr ? -1 : static_cast(pin.pin); -} - -static gpio_num_t pin_or_nc(const struct GpioPinSpec& pin) { - return pin.gpio_controller == nullptr ? GPIO_NUM_NC : static_cast(pin.pin); -} - -// Unpacks the devicetree's flat [cmd, data_len, delay_ms, data_len bytes...] encoding (produced -// by the devicetree compiler's "array" property type - see init-sequence in -// bindings/axs,axs15231b.yaml) into a heap-allocated axs15231b_lcd_init_cmd_t array. -static bool parse_init_sequence(const uint8_t* bytes, uint32_t length, axs15231b_lcd_init_cmd_t** out_cmds, uint16_t* out_count) { - uint32_t count = 0; - for (uint32_t offset = 0; offset < length; count++) { - if (offset + 3 > length) { - LOG_E(TAG, "init-sequence truncated: entry header runs past the end of the array"); - return false; - } - offset += 3 + bytes[offset + 1]; - if (offset > length) { - LOG_E(TAG, "init-sequence truncated: entry data runs past the end of the array"); - return false; - } - } - - auto* cmds = static_cast(malloc(count * sizeof(axs15231b_lcd_init_cmd_t))); - if (cmds == nullptr) { - return false; - } - - uint32_t offset = 0; - for (uint32_t i = 0; i < count; i++) { - uint8_t data_len = bytes[offset + 1]; - cmds[i] = { - .cmd = bytes[offset], - .data = data_len > 0 ? &bytes[offset + 3] : nullptr, - .data_bytes = data_len, - .delay_ms = bytes[offset + 2], - }; - offset += 3 + data_len; - } - - *out_cmds = cmds; - *out_count = (uint16_t)count; - return true; -} - -// region Driver lifecycle - -// Best-effort: a TE pin is a hardware refinement, not a requirement, so failures here are logged -// and left for the caller to treat as non-fatal (matches the original deprecated-HAL driver, -// which continued without TE sync if setup failed). -static bool setup_te_sync(Axs15231bDisplayInternal* internal, gpio_num_t te_pin) { - if (te_pin == GPIO_NUM_NC) { - return true; - } - - internal->te_semaphore = xSemaphoreCreateBinary(); - if (internal->te_semaphore == nullptr) { - LOG_E(TAG, "Failed to create TE sync semaphore"); - return false; - } - - gpio_config_t io_conf = { - .pin_bit_mask = 1ULL << te_pin, - .mode = GPIO_MODE_INPUT, - .pull_up_en = GPIO_PULLUP_DISABLE, - .pull_down_en = GPIO_PULLDOWN_DISABLE, - .intr_type = GPIO_INTR_POSEDGE, - }; - if (gpio_config(&io_conf) != ESP_OK) { - LOG_E(TAG, "Failed to configure TE GPIO"); - vSemaphoreDelete(internal->te_semaphore); - internal->te_semaphore = nullptr; - return false; - } - - esp_err_t ret = gpio_install_isr_service(ESP_INTR_FLAG_IRAM); - if (ret == ESP_OK) { - internal->te_isr_service_installed_by_us = true; - } else if (ret != ESP_ERR_INVALID_STATE) { // ESP_ERR_INVALID_STATE means it's already installed elsewhere - LOG_E(TAG, "Failed to install GPIO ISR service"); - vSemaphoreDelete(internal->te_semaphore); - internal->te_semaphore = nullptr; - return false; - } - - if (gpio_isr_handler_add(te_pin, te_isr_handler, internal->te_semaphore) != ESP_OK) { - LOG_E(TAG, "Failed to add TE ISR handler"); - if (internal->te_isr_service_installed_by_us) { - gpio_uninstall_isr_service(); - internal->te_isr_service_installed_by_us = false; - } - vSemaphoreDelete(internal->te_semaphore); - internal->te_semaphore = nullptr; - return false; - } - - internal->te_isr_installed = true; - return true; -} - -static void teardown_te_sync(Axs15231bDisplayInternal* internal, gpio_num_t te_pin) { - if (internal->te_isr_installed) { - gpio_isr_handler_remove(te_pin); - gpio_intr_disable(te_pin); - internal->te_isr_installed = false; - } - if (internal->te_isr_service_installed_by_us) { - gpio_uninstall_isr_service(); - internal->te_isr_service_installed_by_us = false; - } - if (internal->te_semaphore != nullptr) { - vSemaphoreDelete(internal->te_semaphore); - internal->te_semaphore = nullptr; - } -} - -static error_t start(Device* device) { - auto* parent = device_get_parent(device); - check(device_get_type(parent) == &SPI_CONTROLLER_TYPE); - - const auto* spi_config = static_cast(parent->config); - const auto* config = GET_CONFIG(device); - - struct GpioPinSpec cs_pin; - if (esp32_spi_get_cs_pin(device, &cs_pin) != ERROR_NONE) { - LOG_E(TAG, "Failed to resolve CS pin"); - return ERROR_RESOURCE; - } - - auto* internal = static_cast(malloc(sizeof(Axs15231bDisplayInternal))); - if (internal == nullptr) { - return ERROR_OUT_OF_MEMORY; - } - internal->te_semaphore = nullptr; - internal->te_isr_installed = false; - internal->te_isr_service_installed_by_us = false; - internal->parsed_init_cmds = nullptr; - - const axs15231b_lcd_init_cmd_t* init_cmds = nullptr; - uint16_t init_cmds_size = 0; - if (config->init_sequence != nullptr && config->init_sequence_length > 0) { - if (!parse_init_sequence(config->init_sequence, config->init_sequence_length, &internal->parsed_init_cmds, &init_cmds_size)) { - LOG_E(TAG, "Failed to parse init-sequence property"); - free(internal); - return ERROR_INVALID_ARGUMENT; - } - init_cmds = internal->parsed_init_cmds; - } - - internal->draw_done_semaphore = xSemaphoreCreateBinary(); - if (internal->draw_done_semaphore == nullptr) { - free(internal->parsed_init_cmds); - free(internal); - return ERROR_OUT_OF_MEMORY; - } - - // AXS15231B is only ever driven over QSPI in this codebase (no plain-SPI/i80 board has shown - // up yet), so quad_mode/lcd_cmd_bits/lcd_param_bits are fixed rather than devicetree knobs - - // matches AXS15231B_PANEL_IO_QSPI_CONFIG() in esp_lcd_axs15231b.h. dc_gpio_num is unused in - // QSPI mode (command/data framing goes over the command byte instead of a DC line). - esp_lcd_panel_io_spi_config_t io_config = { - .cs_gpio_num = pin_or_unused(cs_pin), - .dc_gpio_num = -1, - .spi_mode = 3, - .pclk_hz = config->pixel_clock_hz, - .trans_queue_depth = config->transaction_queue_depth, - .on_color_trans_done = on_color_trans_done, - .user_ctx = internal, - .lcd_cmd_bits = 32, - .lcd_param_bits = 8, - .cs_ena_pretrans = 0, - .cs_ena_posttrans = 0, - .flags = { - .dc_high_on_cmd = 0, - .dc_low_on_data = 0, - .dc_low_on_param = 0, - .octal_mode = 0, - .quad_mode = 1, - .sio_mode = 0, - .lsb_first = 0, - .cs_high_active = 0, - }, - }; - - esp_err_t ret = esp_lcd_new_panel_io_spi((esp_lcd_spi_bus_handle_t)spi_config->host, &io_config, &internal->io_handle); - if (ret != ESP_OK) { - LOG_E(TAG, "Failed to create panel IO: %s", esp_err_to_name(ret)); - vSemaphoreDelete(internal->draw_done_semaphore); - free(internal->parsed_init_cmds); - free(internal); - return ERROR_RESOURCE; - } - - axs15231b_vendor_config_t vendor_config = { - .init_cmds = init_cmds, - .init_cmds_size = init_cmds_size, - .flags = { - .use_qspi_interface = 1, - }, - }; - - esp_lcd_panel_dev_config_t panel_config = { - .reset_gpio_num = pin_or_unused(config->pin_reset), - .rgb_ele_order = config->bgr_order ? LCD_RGB_ELEMENT_ORDER_BGR : LCD_RGB_ELEMENT_ORDER_RGB, - .data_endian = LCD_RGB_DATA_ENDIAN_LITTLE, - .bits_per_pixel = 16, - .flags = { .reset_active_high = config->reset_active_high }, - .vendor_config = &vendor_config, - }; - - ret = esp_lcd_new_panel_axs15231b(internal->io_handle, &panel_config, &internal->panel_handle); - if (ret != ESP_OK) { - LOG_E(TAG, "Failed to create panel: %s", esp_err_to_name(ret)); - esp_lcd_panel_io_del(internal->io_handle); - vSemaphoreDelete(internal->draw_done_semaphore); - free(internal->parsed_init_cmds); - free(internal); - return ERROR_RESOURCE; - } - - // Bring-up sequence. swap_xy is intentionally not called (and not exposed in DisplayApi below): - // It doesn't work on this chip/panel combination. - // - // esp_lcd_axs15231b's disp_on_off callback is wired up backwards: its body branches on a - // parameter it names "off" (true -> DISPOFF, false -> DISPON), but esp_lcd_panel_disp_on_off() - // forwards its "on_off" argument straight through with no inversion - so passing true here - // actually switches the panel OFF. Pass false to really turn it on (confirmed against the - // deleted deprecated-HAL driver, which called this same function with false for the same - // reason). See axs15231b_disp_on_off() below, which un-inverts this for DisplayApi callers. - // - // (note: all of this was tested on guition-jc3248w535c only) - bool ok = - esp_lcd_panel_reset(internal->panel_handle) == ESP_OK && - esp_lcd_panel_init(internal->panel_handle) == ESP_OK && - esp_lcd_panel_mirror(internal->panel_handle, config->mirror_x, config->mirror_y) == ESP_OK && - esp_lcd_panel_invert_color(internal->panel_handle, config->invert_color) == ESP_OK && - esp_lcd_panel_disp_on_off(internal->panel_handle, false) == ESP_OK; - - if (!ok) { - LOG_E(TAG, "Failed to bring up panel"); - esp_lcd_panel_del(internal->panel_handle); - esp_lcd_panel_io_del(internal->io_handle); - vSemaphoreDelete(internal->draw_done_semaphore); - free(internal->parsed_init_cmds); - free(internal); - return ERROR_RESOURCE; - } - - if (!setup_te_sync(internal, pin_or_nc(config->pin_te))) { - LOG_W(TAG, "TE sync setup failed, continuing without TE synchronization"); - } - - device_set_driver_data(device, internal); - return ERROR_NONE; -} - -static error_t stop(Device* device) { - const auto* config = GET_CONFIG(device); - auto* internal = static_cast(device_get_driver_data(device)); - - teardown_te_sync(internal, pin_or_nc(config->pin_te)); - - if (internal->panel_handle != nullptr) { - if (esp_lcd_panel_del(internal->panel_handle) != ESP_OK) { - LOG_E(TAG, "Failed to delete panel"); - return ERROR_RESOURCE; - } - internal->panel_handle = nullptr; - } - - if (internal->io_handle != nullptr) { - if (esp_lcd_panel_io_del(internal->io_handle) != ESP_OK) { - LOG_E(TAG, "Failed to delete panel IO"); - return ERROR_RESOURCE; - } - internal->io_handle = nullptr; - } - - vSemaphoreDelete(internal->draw_done_semaphore); - free(internal->parsed_init_cmds); - free(internal); - device_set_driver_data(device, nullptr); - return ERROR_NONE; -} - -// endregion - -// region DisplayApi - -static error_t axs15231b_reset(Device* device) { - auto* internal = static_cast(device_get_driver_data(device)); - return esp_lcd_panel_reset(internal->panel_handle) == ESP_OK ? ERROR_NONE : ERROR_RESOURCE; -} - -static error_t axs15231b_init(Device* device) { - auto* internal = static_cast(device_get_driver_data(device)); - return esp_lcd_panel_init(internal->panel_handle) == ESP_OK ? ERROR_NONE : ERROR_RESOURCE; -} - -static error_t axs15231b_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(device_get_driver_data(device)); - - // Best-effort wait for the next V-blank pulse before transferring, to reduce visible tearing. - // Non-fatal if it times out (matches the original deprecated-HAL driver) - draw_bitmap() - // still has to happen even if TE sync missed its window. - if (internal->te_semaphore != nullptr) { - xSemaphoreTake(internal->te_semaphore, 0); // drain any already-pending signal - xSemaphoreTake(internal->te_semaphore, pdMS_TO_TICKS(20)); - } - - // Drain any stale signal left over from a prior non-draw transaction (bring-up commands like - // reset/init also complete through on_color_trans_done), so the take() below can only be - // satisfied by this draw's own transfer completing. - xSemaphoreTake(internal->draw_done_semaphore, 0); - - if (esp_lcd_panel_draw_bitmap(internal->panel_handle, x_start, y_start, x_end, y_end, color_data) != ESP_OK) { - return ERROR_RESOURCE; - } - - xSemaphoreTake(internal->draw_done_semaphore, portMAX_DELAY); - return ERROR_NONE; -} - -static error_t axs15231b_mirror(Device* device, bool x_axis, bool y_axis) { - auto* internal = static_cast(device_get_driver_data(device)); - return esp_lcd_panel_mirror(internal->panel_handle, x_axis, y_axis) == ESP_OK ? ERROR_NONE : ERROR_RESOURCE; -} - -static bool axs15231b_get_mirror_x(Device* device) { - return GET_CONFIG(device)->mirror_x; -} - -static bool axs15231b_get_mirror_y(Device* device) { - return GET_CONFIG(device)->mirror_y; -} - -// swap_xy/set_gap/disp_sleep are not exposed: swap_xy is confirmed non-functional on this -// chip/panel combination on real hardware (see start()'s comment), and the AXS15231B component -// doesn't implement set_gap or disp_sleep at all. - -static error_t axs15231b_invert_color(Device* device, bool invert_color_data) { - auto* internal = static_cast(device_get_driver_data(device)); - return esp_lcd_panel_invert_color(internal->panel_handle, invert_color_data) == ESP_OK ? ERROR_NONE : ERROR_RESOURCE; -} - -static error_t axs15231b_disp_on_off(Device* device, bool on_off) { - auto* internal = static_cast(device_get_driver_data(device)); - // Inverted: see the comment on the disp_on_off call in start() above. - return esp_lcd_panel_disp_on_off(internal->panel_handle, !on_off) == ESP_OK ? ERROR_NONE : ERROR_RESOURCE; -} - -// _SWAPPED (not plain RGB565): the panel expects each 16-bit pixel high-byte-first over the QSPI -// bus, but this CPU is little-endian - the original deprecated-HAL driver did the equivalent -// byte-swap by hand in its custom flush callback (lv_draw_sw_rgb565_swap()); the generic -// lvgl-module bridge does it for us once we report this format (see lvgl_display_map_color_format()). -static enum DisplayColorFormat axs15231b_get_color_format(Device*) { - return DISPLAY_COLOR_FORMAT_RGB565_SWAPPED; -} - -static uint16_t axs15231b_get_resolution_x(Device* device) { - return GET_CONFIG(device)->horizontal_resolution; -} - -static uint16_t axs15231b_get_resolution_y(Device* device) { - return GET_CONFIG(device)->vertical_resolution; -} - -static void axs15231b_get_frame_buffer(Device*, uint8_t, void** out_buffer) { - *out_buffer = nullptr; -} - -static uint8_t axs15231b_get_frame_buffer_count(Device*) { - return 0; -} - -static error_t axs15231b_get_backlight(Device* device, Device** backlight) { - auto* configured_backlight = GET_CONFIG(device)->backlight; - if (configured_backlight == nullptr) { - return ERROR_NOT_SUPPORTED; - } - *backlight = configured_backlight; - return ERROR_NONE; -} - -// REQUIRES_FULL_FRAME is the only capability that varies per device instance (via the -// requires-full-frame devicetree property, see axs15231b_display.h) - a sub-region draw_bitmap() -// call desyncs this chip's row auto-increment counter, since its QSPI command set has no -// row-address command, but that's been confirmed needed on some boards' wiring/panel combination -// and not assumed true for every AXS15231B board (see start()'s notes on what's been tested where). -// Every other bit stays fixed for the driver, mirrored here from axs15231b_display_api.capabilities. -static bool axs15231b_has_capability(Device* device, uint32_t capability) { - uint32_t static_capabilities = DISPLAY_CAPABILITY_CAP_MIRROR | DISPLAY_CAPABILITY_INVERT_COLOR | - DISPLAY_CAPABILITY_ON_OFF | DISPLAY_CAPABILITY_BACKLIGHT; - if (GET_CONFIG(device)->requires_full_frame) { - static_capabilities |= DISPLAY_CAPABILITY_REQUIRES_FULL_FRAME; - } - return (static_capabilities & capability) == capability; -} - -// endregion - -static const DisplayApi axs15231b_display_api = { - // Mirrors axs15231b_has_capability()'s static_capabilities for callers that read this field - // directly instead of going through display_has_capability()/has_capability(). Excludes - // REQUIRES_FULL_FRAME - see axs15231b_has_capability() above, which is the source of truth. - .capabilities = DISPLAY_CAPABILITY_CAP_MIRROR | DISPLAY_CAPABILITY_INVERT_COLOR | - DISPLAY_CAPABILITY_ON_OFF | DISPLAY_CAPABILITY_BACKLIGHT, - .reset = axs15231b_reset, - .init = axs15231b_init, - .draw_bitmap = axs15231b_draw_bitmap, - .mirror = axs15231b_mirror, - .swap_xy = nullptr, - .get_swap_xy = nullptr, - .get_mirror_x = axs15231b_get_mirror_x, - .get_mirror_y = axs15231b_get_mirror_y, - .set_gap = nullptr, - .invert_color = axs15231b_invert_color, - .disp_on_off = axs15231b_disp_on_off, - .disp_sleep = nullptr, - .get_color_format = axs15231b_get_color_format, - .get_resolution_x = axs15231b_get_resolution_x, - .get_resolution_y = axs15231b_get_resolution_y, - .get_frame_buffer = axs15231b_get_frame_buffer, - .get_frame_buffer_count = axs15231b_get_frame_buffer_count, - .get_backlight = axs15231b_get_backlight, - .has_capability = axs15231b_has_capability, -}; - -Driver axs15231b_display_driver = { - .name = "axs15231b_display", - .compatible = (const char*[]) { "axs,axs15231b", nullptr }, - .start_device = start, - .stop_device = stop, - .api = &axs15231b_display_api, - .device_type = &DISPLAY_TYPE, - .owner = &axs15231b_module, - .internal = nullptr -}; diff --git a/Drivers/axs15231b-module/source/axs15231b_touch.cpp b/Drivers/axs15231b-module/source/axs15231b_touch.cpp deleted file mode 100644 index 5c6dbd413..000000000 --- a/Drivers/axs15231b-module/source/axs15231b_touch.cpp +++ /dev/null @@ -1,211 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include - -#include - -#define TAG "AXS15231BTouch" -#define GET_CONFIG(device) (static_cast((device)->config)) - -struct Axs15231bTouchInternal { - esp_lcd_panel_io_handle_t io_handle; - esp_lcd_touch_handle_t touch_handle; -}; - -static inline gpio_num_t pin_or_nc(const struct GpioPinSpec& pin) { - return pin.gpio_controller == nullptr ? GPIO_NUM_NC : static_cast(pin.pin); -} - -// region Driver lifecycle - -// AXS15231B always sits at a fixed I2C address (ESP_LCD_TOUCH_IO_I2C_AXS15231B_ADDRESS), unlike -// GT911's strapping-dependent address, so no bus probing is needed here. -static esp_err_t create_io_handle(Device* parent, esp_lcd_panel_io_handle_t* out_handle) { - esp_lcd_panel_io_i2c_config_t io_config = ESP_LCD_TOUCH_IO_I2C_AXS15231B_CONFIG(); - - auto* parent_driver = device_get_driver(parent); - if (driver_is_compatible(parent_driver, "espressif,esp32-i2c")) { - auto port = static_cast(parent->config)->port; - return esp_lcd_new_panel_io_i2c_v1(port, &io_config, out_handle); - } - if (driver_is_compatible(parent_driver, "espressif,esp32-i2c-master")) { - auto bus = esp32_i2c_master_get_bus_handle(parent); - io_config.scl_speed_hz = esp32_i2c_master_get_clock_frequency(parent); - return esp_lcd_new_panel_io_i2c_v2(bus, &io_config, out_handle); - } - - LOG_E(TAG, "Unsupported I2C driver"); - return ESP_ERR_NOT_SUPPORTED; -} - -static error_t start(Device* device) { - auto* parent = device_get_parent(device); - check(device_get_type(parent) == &I2C_CONTROLLER_TYPE); - - const auto* config = GET_CONFIG(device); - - auto* internal = static_cast(malloc(sizeof(Axs15231bTouchInternal))); - if (internal == nullptr) { - return ERROR_OUT_OF_MEMORY; - } - - esp_err_t ret = create_io_handle(parent, &internal->io_handle); - if (ret != ESP_OK) { - free(internal); - return ERROR_RESOURCE; - } - - esp_lcd_touch_config_t touch_config = { - .x_max = config->x_max, - .y_max = config->y_max, - .rst_gpio_num = pin_or_nc(config->pin_reset), - .int_gpio_num = pin_or_nc(config->pin_interrupt), - .levels = { - .reset = config->reset_active_high ? 1u : 0u, - .interrupt = config->interrupt_active_high ? 1u : 0u, - }, - .flags = { - .swap_xy = config->swap_xy ? 1u : 0u, - .mirror_x = config->mirror_x ? 1u : 0u, - .mirror_y = config->mirror_y ? 1u : 0u, - }, - .process_coordinates = nullptr, - .interrupt_callback = nullptr, - .user_data = nullptr, - .driver_data = nullptr, - }; - - ret = esp_lcd_touch_new_i2c_axs15231b(internal->io_handle, &touch_config, &internal->touch_handle); - if (ret != ESP_OK) { - LOG_E(TAG, "Failed to create touch handle: %s", esp_err_to_name(ret)); - esp_lcd_panel_io_del(internal->io_handle); - free(internal); - return ERROR_RESOURCE; - } - - device_set_driver_data(device, internal); - return ERROR_NONE; -} - -static error_t stop(Device* device) { - auto* internal = static_cast(device_get_driver_data(device)); - - // esp_lcd_touch_del() only releases the touch-side resources; the panel IO handle is owned - // separately and needs its own deletion. - if (internal->touch_handle != nullptr) { - if (esp_lcd_touch_del(internal->touch_handle) != ESP_OK) { - LOG_E(TAG, "Failed to delete touch handle"); - return ERROR_RESOURCE; - } - internal->touch_handle = nullptr; - } - - if (internal->io_handle != nullptr) { - if (esp_lcd_panel_io_del(internal->io_handle) != ESP_OK) { - LOG_E(TAG, "Failed to delete panel IO handle"); - return ERROR_RESOURCE; - } - internal->io_handle = nullptr; - } - - free(internal); - device_set_driver_data(device, nullptr); - return ERROR_NONE; -} - -// endregion - -// region PointerApi - -static error_t axs15231b_touch_enter_sleep(Device* device) { - auto* internal = static_cast(device_get_driver_data(device)); - return esp_lcd_touch_enter_sleep(internal->touch_handle) == ESP_OK ? ERROR_NONE : ERROR_RESOURCE; -} - -static error_t axs15231b_touch_exit_sleep(Device* device) { - auto* internal = static_cast(device_get_driver_data(device)); - return esp_lcd_touch_exit_sleep(internal->touch_handle) == ESP_OK ? ERROR_NONE : ERROR_RESOURCE; -} - -static error_t axs15231b_touch_read_data(Device* device, TickType_t timeout) { - (void)timeout; // esp_lcd_touch_read_data() has no timeout parameter - auto* internal = static_cast(device_get_driver_data(device)); - return esp_lcd_touch_read_data(internal->touch_handle) == ESP_OK ? ERROR_NONE : ERROR_RESOURCE; -} - -static bool axs15231b_touch_get_touched_points(Device* device, uint16_t* x, uint16_t* y, uint16_t* strength, uint8_t* point_count, uint8_t max_point_count) { - auto* internal = static_cast(device_get_driver_data(device)); - return esp_lcd_touch_get_coordinates(internal->touch_handle, x, y, strength, point_count, max_point_count); -} - -static error_t axs15231b_touch_set_swap_xy(Device* device, bool swap) { - auto* internal = static_cast(device_get_driver_data(device)); - return esp_lcd_touch_set_swap_xy(internal->touch_handle, swap) == ESP_OK ? ERROR_NONE : ERROR_RESOURCE; -} - -static error_t axs15231b_touch_get_swap_xy(Device* device, bool* swap) { - auto* internal = static_cast(device_get_driver_data(device)); - return esp_lcd_touch_get_swap_xy(internal->touch_handle, swap) == ESP_OK ? ERROR_NONE : ERROR_RESOURCE; -} - -static error_t axs15231b_touch_set_mirror_x(Device* device, bool mirror) { - auto* internal = static_cast(device_get_driver_data(device)); - return esp_lcd_touch_set_mirror_x(internal->touch_handle, mirror) == ESP_OK ? ERROR_NONE : ERROR_RESOURCE; -} - -static error_t axs15231b_touch_get_mirror_x(Device* device, bool* mirror) { - auto* internal = static_cast(device_get_driver_data(device)); - return esp_lcd_touch_get_mirror_x(internal->touch_handle, mirror) == ESP_OK ? ERROR_NONE : ERROR_RESOURCE; -} - -static error_t axs15231b_touch_set_mirror_y(Device* device, bool mirror) { - auto* internal = static_cast(device_get_driver_data(device)); - return esp_lcd_touch_set_mirror_y(internal->touch_handle, mirror) == ESP_OK ? ERROR_NONE : ERROR_RESOURCE; -} - -static error_t axs15231b_touch_get_mirror_y(Device* device, bool* mirror) { - auto* internal = static_cast(device_get_driver_data(device)); - return esp_lcd_touch_get_mirror_y(internal->touch_handle, mirror) == ESP_OK ? ERROR_NONE : ERROR_RESOURCE; -} - -// endregion - -static const PointerApi axs15231b_touch_pointer_api = { - .enter_sleep = axs15231b_touch_enter_sleep, - .exit_sleep = axs15231b_touch_exit_sleep, - .read_data = axs15231b_touch_read_data, - .get_touched_points = axs15231b_touch_get_touched_points, - .set_swap_xy = axs15231b_touch_set_swap_xy, - .get_swap_xy = axs15231b_touch_get_swap_xy, - .set_mirror_x = axs15231b_touch_set_mirror_x, - .get_mirror_x = axs15231b_touch_get_mirror_x, - .set_mirror_y = axs15231b_touch_set_mirror_y, - .get_mirror_y = axs15231b_touch_get_mirror_y, -}; - -Driver axs15231b_touch_driver = { - .name = "axs15231b_touch", - .compatible = (const char*[]) { "axs,axs15231b-touch", nullptr }, - .start_device = start, - .stop_device = stop, - .api = &axs15231b_touch_pointer_api, - .device_type = &POINTER_TYPE, - .owner = &axs15231b_module, - .internal = nullptr -}; diff --git a/Drivers/axs15231b-module/source/module.cpp b/Drivers/axs15231b-module/source/module.cpp deleted file mode 100644 index 600e96baa..000000000 --- a/Drivers/axs15231b-module/source/module.cpp +++ /dev/null @@ -1,35 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -#include -#include -#include - -extern "C" { - -extern Driver axs15231b_display_driver; -extern Driver axs15231b_touch_driver; - -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(&axs15231b_display_driver) == ERROR_NONE); - check(driver_construct_add(&axs15231b_touch_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(&axs15231b_touch_driver) == ERROR_NONE); - check(driver_remove_destruct(&axs15231b_display_driver) == ERROR_NONE); - return ERROR_NONE; -} - -Module axs15231b_module = { - .name = "axs15231b", - .start = start, - .stop = stop, - .symbols = nullptr, - .internal = nullptr -}; - -} // extern "C" diff --git a/Drivers/ft5x06-module/source/ft5x06.cpp b/Drivers/ft5x06-module/source/ft5x06.cpp index 72141596f..8725fc301 100644 --- a/Drivers/ft5x06-module/source/ft5x06.cpp +++ b/Drivers/ft5x06-module/source/ft5x06.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -17,6 +18,9 @@ #include #include +#include +#include + #include #define TAG "FT5x06" @@ -25,12 +29,44 @@ struct Ft5x06Internal { esp_lcd_panel_io_handle_t io_handle; esp_lcd_touch_handle_t touch_handle; + // Non-null when pin_reset is configured. Owned/pulsed by this driver instead of esp_lcd_touch + // (see pulse_reset() below) so the reset pin can live on any GPIO_CONTROLLER, not just native + // gpio0 - esp_lcd_touch's rst_gpio_num only accepts a native ESP32 GPIO number (it calls + // gpio_set_level() directly), and just reading a GpioPinSpec's raw pin index and handing it + // over as if it were one (the previous pin_or_nc() behavior below) silently toggles the wrong, + // unrelated physical pin whenever pin_reset actually points at an I2C expander. + GpioDescriptor* reset_descriptor; + bool reset_active_high; }; +// Only valid for pin_interrupt: esp_lcd_touch only ever reads this pin's level / attaches an ISR +// to it, both of which esp_lcd_touch performs via ESP-IDF's native gpio_* calls, so - like +// ili9341-module's cs/dc pins - it must be a real ESP32 GPIO. pin_reset has no such requirement and +// must never go through this helper; see pulse_reset() instead. static inline gpio_num_t pin_or_nc(const struct GpioPinSpec& pin) { return pin.gpio_controller == nullptr ? GPIO_NUM_NC : static_cast(pin.pin); } +// See esp_lcd_ili9341-module's pulse_reset() for the full rationale; same idea, same 10ms/10ms +// timing (matches esp_lcd_touch_ft5x06's own touch_ft5x06_reset()). esp_lcd_touch's rst_gpio_num is +// always left at GPIO_NUM_NC (see start()), under which it just skips its own reset step entirely. +static error_t pulse_reset(GpioDescriptor* descriptor, bool active_high) { + if (descriptor == nullptr) { + return ERROR_NONE; + } + error_t error = gpio_descriptor_set_level(descriptor, active_high); + if (error != ERROR_NONE) { + return error; + } + vTaskDelay(pdMS_TO_TICKS(10)); + error = gpio_descriptor_set_level(descriptor, !active_high); + if (error != ERROR_NONE) { + return error; + } + vTaskDelay(pdMS_TO_TICKS(10)); + return ERROR_NONE; +} + // region Driver lifecycle // FT5x06 always sits at a fixed I2C address, unlike GT911's strapping-dependent address, @@ -64,8 +100,38 @@ static error_t start(Device* device) { return ERROR_OUT_OF_MEMORY; } + internal->reset_descriptor = nullptr; + internal->reset_active_high = config->reset_active_high; + if (config->pin_reset.gpio_controller != nullptr) { + internal->reset_descriptor = gpio_descriptor_acquire(config->pin_reset.gpio_controller, config->pin_reset.pin, GPIO_OWNER_GPIO); + if (internal->reset_descriptor == nullptr) { + LOG_E(TAG, "Failed to acquire reset GPIO descriptor"); + free(internal); + return ERROR_RESOURCE; + } + if (gpio_descriptor_set_flags(internal->reset_descriptor, config->pin_reset.flags | GPIO_FLAG_DIRECTION_OUTPUT) != ERROR_NONE) { + LOG_E(TAG, "Failed to configure reset pin as output"); + gpio_descriptor_release(internal->reset_descriptor); + free(internal); + return ERROR_RESOURCE; + } + } + esp_err_t ret = create_io_handle(parent, &internal->io_handle); if (ret != ESP_OK) { + if (internal->reset_descriptor != nullptr) { + gpio_descriptor_release(internal->reset_descriptor); + } + free(internal); + return ERROR_RESOURCE; + } + + if (pulse_reset(internal->reset_descriptor, internal->reset_active_high) != ERROR_NONE) { + LOG_E(TAG, "Failed to pulse reset pin"); + esp_lcd_panel_io_del(internal->io_handle); + if (internal->reset_descriptor != nullptr) { + gpio_descriptor_release(internal->reset_descriptor); + } free(internal); return ERROR_RESOURCE; } @@ -73,7 +139,9 @@ static error_t start(Device* device) { esp_lcd_touch_config_t touch_config = { .x_max = config->x_max, .y_max = config->y_max, - .rst_gpio_num = pin_or_nc(config->pin_reset), + // Always NC: pulse_reset() above already handled the physical pin (see its comment for + // why); esp_lcd_touch just skips its own no-op reset step when this is NC. + .rst_gpio_num = GPIO_NUM_NC, .int_gpio_num = pin_or_nc(config->pin_interrupt), .levels = { .reset = config->reset_active_high ? 1u : 0u, @@ -94,6 +162,9 @@ static error_t start(Device* device) { if (ret != ESP_OK) { LOG_E(TAG, "Failed to create touch handle: %s", esp_err_to_name(ret)); esp_lcd_panel_io_del(internal->io_handle); + if (internal->reset_descriptor != nullptr) { + gpio_descriptor_release(internal->reset_descriptor); + } free(internal); return ERROR_RESOURCE; } @@ -123,6 +194,11 @@ static error_t stop(Device* device) { internal->io_handle = nullptr; } + if (internal->reset_descriptor != nullptr) { + gpio_descriptor_release(internal->reset_descriptor); + internal->reset_descriptor = nullptr; + } + free(internal); device_set_driver_data(device, nullptr); return ERROR_NONE; diff --git a/Drivers/ft6x36-module/source/ft6x36.cpp b/Drivers/ft6x36-module/source/ft6x36.cpp index ab998e2f7..1e7ab3162 100644 --- a/Drivers/ft6x36-module/source/ft6x36.cpp +++ b/Drivers/ft6x36-module/source/ft6x36.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -17,6 +18,9 @@ #include #include +#include +#include + #include #define TAG "FT6x36" @@ -25,12 +29,44 @@ struct Ft6x36Internal { esp_lcd_panel_io_handle_t io_handle; esp_lcd_touch_handle_t touch_handle; + // Non-null when pin_reset is configured. Owned/pulsed by this driver instead of esp_lcd_touch + // (see pulse_reset() below) so the reset pin can live on any GPIO_CONTROLLER, not just native + // gpio0 - esp_lcd_touch's rst_gpio_num only accepts a native ESP32 GPIO number (it calls + // gpio_set_level() directly), and just reading a GpioPinSpec's raw pin index and handing it + // over as if it were one (the previous pin_or_nc() behavior below) silently toggles the wrong, + // unrelated physical pin whenever pin_reset actually points at an I2C expander. + GpioDescriptor* reset_descriptor; + bool reset_active_high; }; +// Only valid for pin_interrupt: esp_lcd_touch only ever reads this pin's level / attaches an ISR +// to it via ESP-IDF's native gpio_* calls, so - like ili9341-module's cs/dc pins - it must be a +// real ESP32 GPIO. pin_reset has no such requirement and must never go through this helper; see +// pulse_reset() instead. static inline gpio_num_t pin_or_nc(const struct GpioPinSpec& pin) { return pin.gpio_controller == nullptr ? GPIO_NUM_NC : static_cast(pin.pin); } +// See ili9341-module's pulse_reset() for the full rationale; same idea, same 10ms/10ms timing. +// esp_lcd_touch's rst_gpio_num is always left at GPIO_NUM_NC (see start()), under which it just +// skips its own reset step entirely. +static error_t pulse_reset(GpioDescriptor* descriptor, bool active_high) { + if (descriptor == nullptr) { + return ERROR_NONE; + } + error_t error = gpio_descriptor_set_level(descriptor, active_high); + if (error != ERROR_NONE) { + return error; + } + vTaskDelay(pdMS_TO_TICKS(10)); + error = gpio_descriptor_set_level(descriptor, !active_high); + if (error != ERROR_NONE) { + return error; + } + vTaskDelay(pdMS_TO_TICKS(10)); + return ERROR_NONE; +} + // region Driver lifecycle // FT6x36 always sits at a fixed I2C address, unlike GT911's strapping-dependent address, @@ -64,8 +100,38 @@ static error_t start(Device* device) { return ERROR_OUT_OF_MEMORY; } + internal->reset_descriptor = nullptr; + internal->reset_active_high = config->reset_active_high; + if (config->pin_reset.gpio_controller != nullptr) { + internal->reset_descriptor = gpio_descriptor_acquire(config->pin_reset.gpio_controller, config->pin_reset.pin, GPIO_OWNER_GPIO); + if (internal->reset_descriptor == nullptr) { + LOG_E(TAG, "Failed to acquire reset GPIO descriptor"); + free(internal); + return ERROR_RESOURCE; + } + if (gpio_descriptor_set_flags(internal->reset_descriptor, config->pin_reset.flags | GPIO_FLAG_DIRECTION_OUTPUT) != ERROR_NONE) { + LOG_E(TAG, "Failed to configure reset pin as output"); + gpio_descriptor_release(internal->reset_descriptor); + free(internal); + return ERROR_RESOURCE; + } + } + esp_err_t ret = create_io_handle(parent, &internal->io_handle); if (ret != ESP_OK) { + if (internal->reset_descriptor != nullptr) { + gpio_descriptor_release(internal->reset_descriptor); + } + free(internal); + return ERROR_RESOURCE; + } + + if (pulse_reset(internal->reset_descriptor, internal->reset_active_high) != ERROR_NONE) { + LOG_E(TAG, "Failed to pulse reset pin"); + esp_lcd_panel_io_del(internal->io_handle); + if (internal->reset_descriptor != nullptr) { + gpio_descriptor_release(internal->reset_descriptor); + } free(internal); return ERROR_RESOURCE; } @@ -73,7 +139,9 @@ static error_t start(Device* device) { esp_lcd_touch_config_t touch_config = { .x_max = config->x_max, .y_max = config->y_max, - .rst_gpio_num = pin_or_nc(config->pin_reset), + // Always NC: pulse_reset() above already handled the physical pin (see its comment for + // why); esp_lcd_touch just skips its own no-op reset step when this is NC. + .rst_gpio_num = GPIO_NUM_NC, .int_gpio_num = pin_or_nc(config->pin_interrupt), .levels = { .reset = config->reset_active_high ? 1u : 0u, @@ -94,6 +162,9 @@ static error_t start(Device* device) { if (ret != ESP_OK) { LOG_E(TAG, "Failed to create touch handle: %s", esp_err_to_name(ret)); esp_lcd_panel_io_del(internal->io_handle); + if (internal->reset_descriptor != nullptr) { + gpio_descriptor_release(internal->reset_descriptor); + } free(internal); return ERROR_RESOURCE; } @@ -123,6 +194,11 @@ static error_t stop(Device* device) { internal->io_handle = nullptr; } + if (internal->reset_descriptor != nullptr) { + gpio_descriptor_release(internal->reset_descriptor); + internal->reset_descriptor = nullptr; + } + free(internal); device_set_driver_data(device, nullptr); return ERROR_NONE; diff --git a/Drivers/gt911-module/source/gt911.cpp b/Drivers/gt911-module/source/gt911.cpp index b340439fe..217e9b622 100644 --- a/Drivers/gt911-module/source/gt911.cpp +++ b/Drivers/gt911-module/source/gt911.cpp @@ -19,7 +19,7 @@ #include -#define TAG "GT911" +constexpr auto* TAG = "GT911"; #define GET_CONFIG(device) (static_cast((device)->config)) struct Gt911Internal { @@ -27,7 +27,7 @@ struct Gt911Internal { esp_lcd_touch_handle_t touch_handle; }; -static inline gpio_num_t pin_or_nc(const struct GpioPinSpec& pin) { +static gpio_num_t pin_or_nc(const GpioPinSpec& pin) { return pin.gpio_controller == nullptr ? GPIO_NUM_NC : static_cast(pin.pin); } diff --git a/Drivers/ili9341-module/source/ili9341.cpp b/Drivers/ili9341-module/source/ili9341.cpp index 70818577d..5f47dc426 100644 --- a/Drivers/ili9341-module/source/ili9341.cpp +++ b/Drivers/ili9341-module/source/ili9341.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -19,6 +20,7 @@ #include #include +#include #include @@ -39,6 +41,10 @@ struct Ili9341Internal { // (see lvgl_display.c: the caller reuses/overwrites the color buffer as soon as draw_bitmap // returns) - esp_lcd_panel_draw_bitmap() itself only queues the transfer and returns early. SemaphoreHandle_t draw_done_semaphore; + // Non-null when pin_reset is configured. Owned/pulsed by this driver instead of esp_lcd (see + // pulse_reset() below) so the reset pin can live on any GPIO_CONTROLLER, not just native gpio0. + GpioDescriptor* reset_descriptor; + bool reset_active_high; }; // Fires for every completed SPI transaction on this IO (not just draw_bitmap's color transfers - @@ -50,10 +56,38 @@ static bool IRAM_ATTR on_color_trans_done(esp_lcd_panel_io_handle_t, esp_lcd_pan return high_task_woken == pdTRUE; } +// Only valid for cs_gpio_num/dc_gpio_num: esp_lcd_panel_io_spi toggles DC synchronously with the +// SPI/DMA hardware itself (gpio_set_level() on the raw pin number), which is only possible for a +// native ESP32 GPIO - it cannot be routed through an I2C GPIO expander. pin_reset has no such +// constraint (see pulse_reset() below) and must never go through this helper. static int pin_or_unused(const struct GpioPinSpec& pin) { return pin.gpio_controller == nullptr ? -1 : static_cast(pin.pin); } +// esp_lcd's reset_gpio_num has the same native-GPIO-only limitation as cs/dc (see pin_or_unused), +// but unlike them it's just a plain static pulse, not something driven by SPI hardware timing - so +// it's pulsed here through the generic gpio_descriptor API, which works for any GPIO_CONTROLLER +// (native gpio0 or an I2C expander alike). esp_lcd's own reset_gpio_num is always left at -1 (see +// start()), which makes it fall back to sending a SWRESET command instead - itself a valid and +// commonly-recommended reset path, so this pulse plus that fallback is redundant-safe, not harmful. +// Timing (10ms low, 10ms recovery) matches esp_lcd_ili9341's own hardware-reset path exactly. +static error_t pulse_reset(GpioDescriptor* descriptor, bool active_high) { + if (descriptor == nullptr) { + return ERROR_NONE; + } + error_t error = gpio_descriptor_set_level(descriptor, active_high); + if (error != ERROR_NONE) { + return error; + } + vTaskDelay(pdMS_TO_TICKS(10)); + error = gpio_descriptor_set_level(descriptor, !active_high); + if (error != ERROR_NONE) { + return error; + } + vTaskDelay(pdMS_TO_TICKS(10)); + return ERROR_NONE; +} + // region Driver lifecycle static error_t start(Device* device) { @@ -80,6 +114,25 @@ static error_t start(Device* device) { return ERROR_OUT_OF_MEMORY; } + internal->reset_descriptor = nullptr; + internal->reset_active_high = config->reset_active_high; + if (config->pin_reset.gpio_controller != nullptr) { + internal->reset_descriptor = gpio_descriptor_acquire(config->pin_reset.gpio_controller, config->pin_reset.pin, GPIO_OWNER_GPIO); + if (internal->reset_descriptor == nullptr) { + LOG_E(TAG, "Failed to acquire reset GPIO descriptor"); + vSemaphoreDelete(internal->draw_done_semaphore); + free(internal); + return ERROR_RESOURCE; + } + if (gpio_descriptor_set_flags(internal->reset_descriptor, config->pin_reset.flags | GPIO_FLAG_DIRECTION_OUTPUT) != ERROR_NONE) { + LOG_E(TAG, "Failed to configure reset pin as output"); + gpio_descriptor_release(internal->reset_descriptor); + vSemaphoreDelete(internal->draw_done_semaphore); + free(internal); + return ERROR_RESOURCE; + } + } + esp_lcd_panel_io_spi_config_t io_config = { .cs_gpio_num = pin_or_unused(cs_pin), .dc_gpio_num = pin_or_unused(config->pin_dc), @@ -107,13 +160,18 @@ static error_t start(Device* device) { esp_err_t ret = esp_lcd_new_panel_io_spi((esp_lcd_spi_bus_handle_t)spi_config->host, &io_config, &internal->io_handle); if (ret != ESP_OK) { LOG_E(TAG, "Failed to create panel IO: %s", esp_err_to_name(ret)); + if (internal->reset_descriptor != nullptr) { + gpio_descriptor_release(internal->reset_descriptor); + } vSemaphoreDelete(internal->draw_done_semaphore); free(internal); return ERROR_RESOURCE; } esp_lcd_panel_dev_config_t panel_config = { - .reset_gpio_num = pin_or_unused(config->pin_reset), + // Always -1: pulse_reset() handles the physical pin itself (see its comment for why), and + // esp_lcd_panel_reset() below falls back to a SWRESET command when this is -1. + .reset_gpio_num = -1, .rgb_ele_order = config->bgr_order ? LCD_RGB_ELEMENT_ORDER_BGR : LCD_RGB_ELEMENT_ORDER_RGB, .data_endian = LCD_RGB_DATA_ENDIAN_LITTLE, .bits_per_pixel = config->bits_per_pixel, @@ -125,6 +183,9 @@ static error_t start(Device* device) { if (ret != ESP_OK) { LOG_E(TAG, "Failed to create panel: %s", esp_err_to_name(ret)); esp_lcd_panel_io_del(internal->io_handle); + if (internal->reset_descriptor != nullptr) { + gpio_descriptor_release(internal->reset_descriptor); + } vSemaphoreDelete(internal->draw_done_semaphore); free(internal); return ERROR_RESOURCE; @@ -134,6 +195,7 @@ static error_t start(Device* device) { // Every failure path below must clean up fully: unlike stop_device, this is never retried by the kernel // if start_device fails (see device_start() in TactilityKernel), so a partial failure here would leak. bool ok = + pulse_reset(internal->reset_descriptor, internal->reset_active_high) == ERROR_NONE && esp_lcd_panel_reset(internal->panel_handle) == ESP_OK && esp_lcd_panel_init(internal->panel_handle) == ESP_OK && (!config->invert_color || esp_lcd_panel_invert_color(internal->panel_handle, true) == ESP_OK); @@ -153,6 +215,9 @@ static error_t start(Device* device) { LOG_E(TAG, "Failed to bring up panel"); esp_lcd_panel_del(internal->panel_handle); esp_lcd_panel_io_del(internal->io_handle); + if (internal->reset_descriptor != nullptr) { + gpio_descriptor_release(internal->reset_descriptor); + } vSemaphoreDelete(internal->draw_done_semaphore); free(internal); return ERROR_RESOURCE; @@ -181,6 +246,11 @@ static error_t stop(Device* device) { internal->io_handle = nullptr; } + if (internal->reset_descriptor != nullptr) { + gpio_descriptor_release(internal->reset_descriptor); + internal->reset_descriptor = nullptr; + } + vSemaphoreDelete(internal->draw_done_semaphore); free(internal); device_set_driver_data(device, nullptr); @@ -193,6 +263,10 @@ static error_t stop(Device* device) { static error_t ili9341_reset(Device* device) { auto* internal = static_cast(device_get_driver_data(device)); + error_t error = pulse_reset(internal->reset_descriptor, internal->reset_active_high); + if (error != ERROR_NONE) { + return error; + } return esp_lcd_panel_reset(internal->panel_handle) == ESP_OK ? ERROR_NONE : ERROR_RESOURCE; } diff --git a/Modules/lvgl-module/source/arch/lvgl_esp32.c b/Modules/lvgl-module/source/arch/lvgl_esp32.c index f06d51066..45140b84d 100644 --- a/Modules/lvgl-module/source/arch/lvgl_esp32.c +++ b/Modules/lvgl-module/source/arch/lvgl_esp32.c @@ -18,9 +18,10 @@ bool lvgl_lock(void) { return lvgl_port_lock(portMAX_DELAY); } -bool lvgl_try_lock(uint32_t timeout) { +bool lvgl_try_lock(uint32_t timeoutTicks) { if (!initialized) return true; // We allow (fake) locking because it's safe to do so as LVGL is not running yet - return lvgl_port_lock(millis_to_ticks(timeout)); + // lvgl_port_lock expects milliseconds + return lvgl_port_lock(timeoutTicks * portTICK_PERIOD_MS); } void lvgl_unlock(void) { diff --git a/Modules/lvgl-module/source/lvgl_devices.c b/Modules/lvgl-module/source/lvgl_devices.c index 2d81beb8f..69150527c 100644 --- a/Modules/lvgl-module/source/lvgl_devices.c +++ b/Modules/lvgl-module/source/lvgl_devices.c @@ -1,6 +1,3 @@ -#include "tactility/lvgl_module.h" - - #include #include #include @@ -8,6 +5,7 @@ #include #include #include +#include #include #include @@ -18,6 +16,7 @@ void lvgl_devices_attach() { lvgl_lock(); lv_disp_t* lvgl_display = NULL; + bool display_updates_slowly = false; struct Device* kernel_display_device = NULL; device_get_first_by_type(&DISPLAY_TYPE, &kernel_display_device); @@ -29,24 +28,17 @@ void lvgl_devices_attach() { } if (kernel_display_device != NULL) { - // A full-frame buffer (buffer_height=0) needs one contiguous allocation of - // hres*vres*bpp bytes. Plain ESP32 boards without PSRAM only have ~100-150KB - // contiguous internal RAM, which a 240x320+ panel can already exceed - so - // lvgl_display_add() would fail with ERROR_OUT_OF_MEMORY and never register a - // display. Match the old deprecated-HAL drivers' convention of a partial buffer - // sized to 1/10th of the vertical resolution; it fits comfortably everywhere, - // including boards with PSRAM that could afford full-frame. uint16_t vres = display_get_resolution_y(kernel_display_device); - // _SWAPPED variants need it outright; plain BGR565 panels have also been found on real - // hardware to need it alongside their MADCTL BGR bit (see st7735-module/st7789-module). enum DisplayColorFormat color_format = display_get_color_format(kernel_display_device); bool swap_bytes = color_format == DISPLAY_COLOR_FORMAT_RGB565_SWAPPED || color_format == DISPLAY_COLOR_FORMAT_BGR565_SWAPPED || color_format == DISPLAY_COLOR_FORMAT_BGR565; + bool display_requires_full_frame = display_has_capability(kernel_display_device, DISPLAY_CAPABILITY_REQUIRES_FULL_FRAME); + display_updates_slowly = display_has_capability(kernel_display_device, DISPLAY_CAPABILITY_SLOW_REFRESH); struct LvglDisplayConfig lvgl_display_config = { .buffer_height = vres > 10 ? vres / 10 : vres, .swap_bytes = swap_bytes, - .force_full_frame = display_has_capability(kernel_display_device, DISPLAY_CAPABILITY_REQUIRES_FULL_FRAME) + .force_full_frame = display_requires_full_frame }; if (lvgl_display_add(kernel_display_device, &lvgl_display_config, &lvgl_display) == ERROR_NONE) { LOG_I(TAG, "Bound %s to LVGL", kernel_display_device->name); @@ -58,7 +50,6 @@ void lvgl_devices_attach() { struct Device* kernel_pointer_device = NULL; device_get_first_by_type(&POINTER_TYPE, &kernel_pointer_device); - // Same placeholder situation as the display above. if (kernel_pointer_device != NULL && device_get_driver(kernel_pointer_device)->api == NULL) { device_put(kernel_pointer_device); kernel_pointer_device = NULL; @@ -67,6 +58,10 @@ void lvgl_devices_attach() { lv_indev_t* lvgl_pointer_device; if (lvgl_pointer_add(kernel_pointer_device, lvgl_display, &lvgl_pointer_device) == ERROR_NONE) { LOG_I(TAG, "Bound %s to LVGL", kernel_pointer_device->name); + // Slow panels cause taps to be missed due to the long update time, prevent that + if (display_updates_slowly ) { + lv_indev_set_long_press_time(lvgl_pointer_device, 2000); + } } else { LOG_E(TAG, "Failed to bind %s to LVG", kernel_pointer_device->name); } diff --git a/Tactility/Source/app/poweroff/PowerOff.cpp b/Tactility/Source/app/poweroff/PowerOff.cpp index 1fbba45b5..61a70fa4f 100644 --- a/Tactility/Source/app/poweroff/PowerOff.cpp +++ b/Tactility/Source/app/poweroff/PowerOff.cpp @@ -1,3 +1,7 @@ +#include "Tactility/Tactility.h" +#include "tactility/drivers/display.h" + + #include #include #include @@ -16,13 +20,7 @@ extern const AppManifest manifest; class PowerOffApp final : public App { - /** Replaces the screen with a plain "powered off" message, forces it to draw - * synchronously, and waits for the display to confirm the draw physically - * finished. On e-paper this is what's left showing once power cuts, so it - * doubles as visual confirmation that the device shut down cleanly rather - * than crashed or hung. Called from an LVGL button click callback, so it's - * already running on the LVGL thread. */ - static void showPoweredOffScreenAndWait(hal::display::DisplayDevice* display) { + static void showPoweredOffScreen() { auto* screen = lv_obj_create(nullptr); lv_obj_set_style_bg_color(screen, lv_color_white(), 0); lv_obj_set_flex_flow(screen, LV_FLEX_FLOW_COLUMN); @@ -38,14 +36,6 @@ class PowerOffApp final : public App { lv_obj_set_style_text_color(subtitle, lv_color_black(), 0); lv_screen_load(screen); - - if (display != nullptr) { - auto* lvgl_display = display->getLvglDisplay(); - if (lvgl_display != nullptr) { - lv_refr_now(lvgl_display); - } - display->waitForFlushComplete(); - } } static bool anyDeviceSupportsPowerOff() { @@ -65,14 +55,40 @@ class PowerOffApp final : public App { return; } - auto display = hal::findFirstDevice(hal::Device::Type::Display); - showPoweredOffScreenAndWait(display.get()); + Device* display; + error_t error = device_get_first_by_type(&DISPLAY_TYPE, &display); + // TODO: remove this logic path when all displays have been migrated to kernel display drivers + if (error != ERROR_NONE) { + // No display, power off now + device_for_each_of_type(&POWER_SUPPLY_TYPE, nullptr, [](Device* device, void* /*context*/) { + if (device_is_ready(device) && power_supply_supports_power_off(device)) { + power_supply_power_off(device); + } + return true; + }); + return; + } + + bool is_slow_refresh = display_has_capability(display, DISPLAY_CAPABILITY_SLOW_REFRESH); + if (is_slow_refresh) { + auto* lvgl_display = lv_display_get_default(); + showPoweredOffScreen(); + if (lvgl_display != nullptr) { + lv_refr_now(lvgl_display); + } + } - device_for_each_of_type(&POWER_SUPPLY_TYPE, nullptr, [](Device* device, void* /*context*/) { - if (device_is_ready(device) && power_supply_supports_power_off(device)) { - power_supply_power_off(device); + getMainDispatcher().dispatch([is_slow_refresh] { + // Not necessary for LilyGO Paper S3, but other drivers with async rendering might need us to wait a bit. + if (is_slow_refresh) { + vTaskDelay(pdMS_TO_TICKS(2000)); } - return true; + device_for_each_of_type(&POWER_SUPPLY_TYPE, nullptr, [](Device* device, void* /*context*/) { + if (device_is_ready(device) && power_supply_supports_power_off(device)) { + power_supply_power_off(device); + } + return true; + }); }); } diff --git a/Tactility/Source/service/gui/GuiService.cpp b/Tactility/Source/service/gui/GuiService.cpp index 2f625f604..c2ec0fb61 100644 --- a/Tactility/Source/service/gui/GuiService.cpp +++ b/Tactility/Source/service/gui/GuiService.cpp @@ -12,6 +12,7 @@ #include #include +#include namespace tt::service::gui { @@ -98,7 +99,7 @@ void GuiService::onLoaderEvent(LoaderService::Event event) { int32_t GuiService::guiMain() { auto service = findServiceById(manifest.id); - if (!lvgl::lock(5000)) { + if (!lvgl_try_lock(5000)) { LOG_E(TAG, "LVGL guiMain start failed as LVGL couldn't be locked"); return 0; } @@ -109,7 +110,7 @@ int32_t GuiService::guiMain() { auto* screen_root = lv_screen_active(); if (screen_root == nullptr) { LOG_E(TAG, "No display found, exiting GUI task"); - lvgl::unlock(); + lvgl_unlock(); return 0; } @@ -137,7 +138,7 @@ int32_t GuiService::guiMain() { service->appRootWidget = app_container; - lvgl::unlock(); + lvgl_unlock(); while (!service->exitRequested) { dispatcher_consume(service->dispatcher); @@ -177,41 +178,40 @@ void GuiService::redraw() { return; } - if (lvgl::lock(1000)) { - lv_obj_clean(appRootWidget); - - if (appToRender != nullptr) { - - // Create a default group which adds all objects automatically, - // and assign all indevs to it. - // This enables navigation with limited input, such as encoder wheels. - lv_group_t* group = lv_group_create(); - auto* indev = lv_indev_get_next(nullptr); - while (indev) { - lv_indev_set_group(indev, group); - indev = lv_indev_get_next(indev); - } - lv_group_set_default(group); - - app::Flags flags = std::static_pointer_cast(appToRender)->getFlags(); - if (flags.hideStatusbar) { - lv_obj_add_flag(statusbarWidget, LV_OBJ_FLAG_HIDDEN); - } else { - lv_obj_remove_flag(statusbarWidget, LV_OBJ_FLAG_HIDDEN); - } - - lv_obj_t* container = createAppViews(appRootWidget); - appToRender->getApp()->onShow(*appToRender, container); + while (!lvgl_try_lock(1000)) { + LOG_W(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED_FMT, "GuiService LVGL"); + } + + lv_obj_clean(appRootWidget); + + if (appToRender != nullptr) { + + // Create a default group which adds all objects automatically, + // and assign all indevs to it. + // This enables navigation with limited input, such as encoder wheels. + lv_group_t* group = lv_group_create(); + auto* indev = lv_indev_get_next(nullptr); + while (indev) { + lv_indev_set_group(indev, group); + indev = lv_indev_get_next(indev); + } + lv_group_set_default(group); + + app::Flags flags = std::static_pointer_cast(appToRender)->getFlags(); + if (flags.hideStatusbar) { + lv_obj_add_flag(statusbarWidget, LV_OBJ_FLAG_HIDDEN); } else { - LOG_W(TAG, "Nothing to draw"); + lv_obj_remove_flag(statusbarWidget, LV_OBJ_FLAG_HIDDEN); } - // Unlock GUI and LVGL - lvgl::unlock(); + lv_obj_t* container = createAppViews(appRootWidget); + appToRender->getApp()->onShow(*appToRender, container); } else { - LOG_E(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED_FMT, "LVGL"); + LOG_W(TAG, "Nothing to draw"); } + lvgl_unlock(); + unlock(); } @@ -263,15 +263,12 @@ void GuiService::onStop(ServiceContext& service) { } thread->join(); - if (lvgl::lock()) { - if (keyboardGroup != nullptr) { - lv_group_delete(keyboardGroup); - keyboardGroup = nullptr; - } - lvgl::unlock(); - } else { - LOG_E(TAG, "Failed to lock LVGL during GUI stop"); + lvgl_lock(); + if (keyboardGroup != nullptr) { + lv_group_delete(keyboardGroup); + keyboardGroup = nullptr; } + lvgl_unlock(); delete thread; dispatcher_free(dispatcher); @@ -326,9 +323,9 @@ void GuiService::hideApp() { // We must lock the LVGL port, because the viewport hide callbacks // might call LVGL APIs (e.g. to remove the keyboard from the screen root) - lvgl::lock(portMAX_DELAY); + lvgl_lock(); appToRender->getApp()->onHide(*appToRender); - lvgl::unlock(); + lvgl_unlock(); appToRender = nullptr; } diff --git a/TactilityKernel/include/tactility/drivers/display.h b/TactilityKernel/include/tactility/drivers/display.h index 86aec4d64..02b90f151 100644 --- a/TactilityKernel/include/tactility/drivers/display.h +++ b/TactilityKernel/include/tactility/drivers/display.h @@ -14,20 +14,16 @@ extern "C" { * @brief Optional capabilities of the display. */ enum DisplayCapability { - DISPLAY_CAPABILITY_CAP_MIRROR = 1 << 0, - DISPLAY_CAPABILITY_CAP_SWAP_XY = 1 << 1, - DISPLAY_CAPABILITY_CAP_SET_GAP = 1 << 2, - DISPLAY_CAPABILITY_INVERT_COLOR = 1 << 3, - DISPLAY_CAPABILITY_ON_OFF = 1 << 4, - DISPLAY_CAPABILITY_BACKLIGHT = 1 << 5, - DISPLAY_CAPABILITY_SLEEP = 1 << 6, - /** - * Panel can only be written a full frame at a time (e.g. no row-address command, relying on - * an internal auto-increment counter reset by writing the whole frame from the top every - * time) - draw_bitmap() must always be called with the full resolution, never a sub-region. - * LVGL-facing callers (see lvgl_display.c) must use LV_DISPLAY_RENDER_MODE_FULL for such panels. - */ - DISPLAY_CAPABILITY_REQUIRES_FULL_FRAME = 1 << 7 + DISPLAY_CAPABILITY_CAP_MIRROR = 1 << 0, + DISPLAY_CAPABILITY_CAP_SWAP_XY = 1 << 1, + DISPLAY_CAPABILITY_CAP_SET_GAP = 1 << 2, + DISPLAY_CAPABILITY_INVERT_COLOR = 1 << 3, + DISPLAY_CAPABILITY_ON_OFF = 1 << 4, + DISPLAY_CAPABILITY_BACKLIGHT = 1 << 5, + DISPLAY_CAPABILITY_SLEEP = 1 << 6, + DISPLAY_CAPABILITY_REQUIRES_FULL_FRAME = 1 << 7, + /** Can be used by e-paper with pointer devices for long click timing changes. */ + DISPLAY_CAPABILITY_SLOW_REFRESH = 1 << 8 }; /**