M5Stack StickS3 - New Tab5 - driver modules (#516)

Font size set to 18 for 800x480 displays
Fix web server dashboard not rendering when sdcard isn't present

Added new driver modules
-  BM8563 RTC
- RX8130CE RTC
- MPU6886 IMU
- QMI8658 IMU
- M5PM1 Power Management Chip

Applied the above modules to applicable devicetrees.

Added new device: M5Stack StickS3

Added new M5Stack Tab5 St7123 variant.

ButtonControl changed to use interupts and xQueue, added AppClose action.

And some bonus symbols of course, the apps are hungry for symbols.
This commit is contained in:
Shadowtrance
2026-03-20 19:07:57 +10:00
committed by GitHub
parent e560cc7df2
commit e64f4ff16b
113 changed files with 3690 additions and 102 deletions
+11
View File
@@ -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(m5pm1-module
SRCS ${SOURCE_FILES}
INCLUDE_DIRS include/
REQUIRES TactilityKernel
)
+195
View File
@@ -0,0 +1,195 @@
Apache License
==============
_Version 2.0, January 2004_
_&lt;<http://www.apache.org/licenses/>&gt;_
### 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.
+8
View File
@@ -0,0 +1,8 @@
# M5PM1 I2C Driver
A driver for the `M5PM1` power management chip.
See https://m5stack-doc.oss-cn-shenzhen.aliyuncs.com/1207/M5PM1_Datasheet_EN.pdf
And https://github.com/m5stack/M5PM1
License: [Apache v2.0](LICENSE-Apache-2.0.md)
@@ -0,0 +1,5 @@
description: M5Stack M5PM1 Power Management IC
include: ["i2c-device.yaml"]
compatible: "m5stack,m5pm1"
+3
View File
@@ -0,0 +1,3 @@
dependencies:
- TactilityKernel
bindings: bindings
@@ -0,0 +1,15 @@
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include <tactility/bindings/bindings.h>
#include <drivers/m5pm1.h>
#ifdef __cplusplus
extern "C" {
#endif
DEFINE_DEVICETREE(m5pm1, struct M5pm1Config)
#ifdef __cplusplus
}
#endif
@@ -0,0 +1,88 @@
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include <stdbool.h>
#include <stdint.h>
#include <tactility/error.h>
struct Device;
#ifdef __cplusplus
extern "C" {
#endif
struct M5pm1Config {
uint8_t address;
};
// ---------------------------------------------------------------------------
// Power source (REG_PWR_SRC 0x04)
// ---------------------------------------------------------------------------
typedef enum {
M5PM1_PWR_SRC_5VIN = 0,
M5PM1_PWR_SRC_5VINOUT = 1,
M5PM1_PWR_SRC_BAT = 2,
M5PM1_PWR_SRC_UNKNOWN = 3,
} M5pm1PowerSource;
// ---------------------------------------------------------------------------
// Voltage readings
// ---------------------------------------------------------------------------
error_t m5pm1_get_battery_voltage(struct Device* device, uint16_t* mv);
error_t m5pm1_get_vin_voltage(struct Device* device, uint16_t* mv);
error_t m5pm1_get_5vout_voltage(struct Device* device, uint16_t* mv);
error_t m5pm1_get_power_source(struct Device* device, M5pm1PowerSource* source);
// ---------------------------------------------------------------------------
// Charging & power rails
// ---------------------------------------------------------------------------
/** PM1_G0 low = charging (connected to charge-status pin of the charge IC) */
error_t m5pm1_is_charging(struct Device* device, bool* charging);
error_t m5pm1_set_charge_enable(struct Device* device, bool enable);
error_t m5pm1_set_boost_enable(struct Device* device, bool enable); ///< 5V BOOST / Grove power
error_t m5pm1_set_ldo_enable(struct Device* device, bool enable); ///< 3.3V LDO
// ---------------------------------------------------------------------------
// Temperature (internal chip sensor)
// ---------------------------------------------------------------------------
/** Returns temperature in units of 0.1 °C */
error_t m5pm1_get_temperature(struct Device* device, uint16_t* decidegc);
// ---------------------------------------------------------------------------
// System commands
// ---------------------------------------------------------------------------
error_t m5pm1_shutdown(struct Device* device);
error_t m5pm1_reboot(struct Device* device);
// ---------------------------------------------------------------------------
// Power button (M5PM1 internal button, not ESP32 GPIO)
// ---------------------------------------------------------------------------
/** Current instantaneous state of the power button */
error_t m5pm1_btn_get_state(struct Device* device, bool* pressed);
/** Edge-triggered flag — auto-clears on read */
error_t m5pm1_btn_get_flag(struct Device* device, bool* was_pressed);
// ---------------------------------------------------------------------------
// Watchdog timer
// ---------------------------------------------------------------------------
/** timeout_sec: 0 = disabled, 1255 = timeout in seconds */
error_t m5pm1_wdt_set(struct Device* device, uint8_t timeout_sec);
error_t m5pm1_wdt_feed(struct Device* device);
// ---------------------------------------------------------------------------
// RTC RAM (32 bytes, retained across sleep / power-off)
// ---------------------------------------------------------------------------
error_t m5pm1_read_rtc_ram(struct Device* device, uint8_t offset, uint8_t* data, uint8_t len);
error_t m5pm1_write_rtc_ram(struct Device* device, uint8_t offset, const uint8_t* data, uint8_t len);
// ---------------------------------------------------------------------------
// NeoPixel LED (via M5PM1 LED controller, max 32 LEDs)
// ---------------------------------------------------------------------------
error_t m5pm1_set_led_count(struct Device* device, uint8_t count);
error_t m5pm1_set_led_color(struct Device* device, uint8_t index, uint8_t r, uint8_t g, uint8_t b);
error_t m5pm1_refresh_leds(struct Device* device);
error_t m5pm1_disable_leds(struct Device* device);
#ifdef __cplusplus
}
#endif
@@ -0,0 +1,14 @@
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include <tactility/module.h>
#ifdef __cplusplus
extern "C" {
#endif
extern struct Module m5pm1_module;
#ifdef __cplusplus
}
#endif
+288
View File
@@ -0,0 +1,288 @@
// SPDX-License-Identifier: Apache-2.0
#include <drivers/m5pm1.h>
#include <m5pm1_module.h>
#include <tactility/device.h>
#include <tactility/drivers/i2c_controller.h>
#include <tactility/log.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#define TAG "M5PM1"
// ---------------------------------------------------------------------------
// Register map
// ---------------------------------------------------------------------------
static constexpr uint8_t REG_DEVICE_ID = 0x00; ///< R - Device ID (0x50)
static constexpr uint8_t REG_PWR_SRC = 0x04; ///< R - Power source (0=5VIN, 1=5VINOUT, 2=BAT)
static constexpr uint8_t REG_PWR_CFG = 0x06; ///< RW - [3]=BOOST_EN [2]=LDO_EN [1]=DCDC_EN [0]=CHG_EN
static constexpr uint8_t REG_I2C_CFG = 0x09; ///< RW - [4]=SPD(400kHz) [3:0]=SLP_TO(0=off)
static constexpr uint8_t REG_WDT_CNT = 0x0A; ///< RW - Watchdog countdown (0=disabled, 1255=seconds)
static constexpr uint8_t REG_WDT_KEY = 0x0B; ///< W - Write 0xA5 to feed watchdog
static constexpr uint8_t REG_SYS_CMD = 0x0C; ///< W - High nibble=0xA; low: 1=shutdown 2=reboot 3=download
static constexpr uint8_t REG_GPIO_MODE = 0x10; ///< RW - GPIO direction [4:0] (1=output, 0=input)
static constexpr uint8_t REG_GPIO_OUT = 0x11; ///< RW - GPIO output level [4:0]
static constexpr uint8_t REG_GPIO_IN = 0x12; ///< R - GPIO input state [4:0]
static constexpr uint8_t REG_GPIO_DRV = 0x13; ///< RW - Drive mode [4:0] (0=push-pull, 1=open-drain)
static constexpr uint8_t REG_GPIO_FUNC0 = 0x16; ///< RW - GPIO03 function (2 bits each: 00=GPIO)
static constexpr uint8_t REG_VBAT_L = 0x22; ///< R - Battery voltage low byte (mV, 16-bit LE)
static constexpr uint8_t REG_VIN_L = 0x24; ///< R - VIN voltage low byte (mV, 16-bit LE)
static constexpr uint8_t REG_5VOUT_L = 0x26; ///< R - 5V output voltage low byte (mV, 16-bit LE)
static constexpr uint8_t REG_ADC_RES_L = 0x28; ///< R - ADC result low byte (mV, 16-bit LE)
static constexpr uint8_t REG_ADC_CTRL = 0x2A; ///< RW - [3:1]=channel [0]=START
static constexpr uint8_t REG_BTN_STATUS = 0x48; ///< R - [7]=BTN_FLAG(auto-clear) [0]=BTN_STATE
static constexpr uint8_t REG_NEO_CFG = 0x50; ///< RW - [6]=REFRESH [5:0]=LED_CNT
static constexpr uint8_t REG_NEO_DATA = 0x60; ///< RW - NeoPixel RGB565 data, 2 bytes per LED (max 32)
static constexpr uint8_t REG_RTC_RAM = 0xA0; ///< RW - 32 bytes of RTC RAM
// PWR_CFG bit masks
static constexpr uint8_t PWR_CFG_CHG_EN = (1U << 0U);
static constexpr uint8_t PWR_CFG_DCDC_EN = (1U << 1U);
static constexpr uint8_t PWR_CFG_LDO_EN = (1U << 2U);
static constexpr uint8_t PWR_CFG_BOOST_EN = (1U << 3U);
// System command values (high nibble must be 0xA)
static constexpr uint8_t SYS_CMD_SHUTDOWN = 0xA1;
static constexpr uint8_t SYS_CMD_REBOOT = 0xA2;
// ADC channel for temperature
static constexpr uint8_t ADC_CH_TEMP = 6;
// PM1_G2: LCD power enable on M5Stack StickS3
static constexpr uint8_t LCD_POWER_BIT = (1U << 2U);
static constexpr TickType_t TIMEOUT = pdMS_TO_TICKS(50);
#define GET_CONFIG(device) (static_cast<const M5pm1Config*>((device)->config))
// ---------------------------------------------------------------------------
// Driver lifecycle
// ---------------------------------------------------------------------------
static error_t start(Device* device) {
Device* i2c = device_get_parent(device);
if (device_get_type(i2c) != &I2C_CONTROLLER_TYPE) {
LOG_E(TAG, "Parent is not an I2C controller");
return ERROR_RESOURCE;
}
const uint8_t addr = GET_CONFIG(device)->address;
// M5PM1 enters I2C sleep after inactivity. The first transaction after sleep
// is ignored as the chip wakes up. Retry with increasing delays until ACK.
bool awake = false;
for (int attempt = 0; attempt < 5; attempt++) {
uint8_t chip_id = 0;
if (i2c_controller_register8_get(i2c, addr, REG_DEVICE_ID, &chip_id, TIMEOUT) == ERROR_NONE) {
LOG_I(TAG, "M5PM1 online (chip_id=0x%02X)", chip_id);
awake = true;
break;
}
vTaskDelay(pdMS_TO_TICKS(20 * (attempt + 1)));
}
if (!awake) {
LOG_E(TAG, "M5PM1 not responding — LCD power will not be enabled");
return ERROR_NONE; // non-fatal: don't crash the kernel
}
// Disable I2C idle sleep so the PMIC stays reachable on battery power
if (i2c_controller_register8_set(i2c, addr, REG_I2C_CFG, 0x00, TIMEOUT) != ERROR_NONE) {
LOG_W(TAG, "Failed to disable I2C sleep (non-fatal)");
}
// PM1_G2 → LCD power enable (L3B rail on StickS3)
// Sequence matches M5GFX: clear FUNC0 bit2, set MODE bit2 output, clear DRV bit2 push-pull, set OUT bit2 high
bool lcd_ok =
i2c_controller_register8_reset_bits(i2c, addr, REG_GPIO_FUNC0, LCD_POWER_BIT, TIMEOUT) == ERROR_NONE &&
i2c_controller_register8_set_bits (i2c, addr, REG_GPIO_MODE, LCD_POWER_BIT, TIMEOUT) == ERROR_NONE &&
i2c_controller_register8_reset_bits(i2c, addr, REG_GPIO_DRV, LCD_POWER_BIT, TIMEOUT) == ERROR_NONE &&
i2c_controller_register8_set_bits (i2c, addr, REG_GPIO_OUT, LCD_POWER_BIT, TIMEOUT) == ERROR_NONE;
if (lcd_ok) {
LOG_I(TAG, "LCD power enabled via PM1_G2");
} else {
LOG_E(TAG, "Failed to enable LCD power via PM1_G2");
}
return ERROR_NONE;
}
static error_t stop(Device* device) {
return ERROR_NONE;
}
// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------
extern "C" {
error_t m5pm1_get_battery_voltage(Device* device, uint16_t* mv) {
return i2c_controller_register16le_get(device_get_parent(device), GET_CONFIG(device)->address, REG_VBAT_L, mv, TIMEOUT);
}
error_t m5pm1_get_vin_voltage(Device* device, uint16_t* mv) {
return i2c_controller_register16le_get(device_get_parent(device), GET_CONFIG(device)->address, REG_VIN_L, mv, TIMEOUT);
}
error_t m5pm1_get_5vout_voltage(Device* device, uint16_t* mv) {
return i2c_controller_register16le_get(device_get_parent(device), GET_CONFIG(device)->address, REG_5VOUT_L, mv, TIMEOUT);
}
error_t m5pm1_get_power_source(Device* device, M5pm1PowerSource* source) {
uint8_t val = 0;
error_t err = i2c_controller_register8_get(device_get_parent(device), GET_CONFIG(device)->address, REG_PWR_SRC, &val, TIMEOUT);
if (err != ERROR_NONE) return err;
*source = static_cast<M5pm1PowerSource>(val & 0x03U);
return ERROR_NONE;
}
error_t m5pm1_is_charging(Device* device, bool* charging) {
// PM1_G0 is wired to the charge IC's charge-status output: LOW = charging
uint8_t gpio_in = 0;
error_t err = i2c_controller_register8_get(device_get_parent(device), GET_CONFIG(device)->address, REG_GPIO_IN, &gpio_in, TIMEOUT);
if (err != ERROR_NONE) return err;
*charging = (gpio_in & 0x01U) == 0;
return ERROR_NONE;
}
error_t m5pm1_set_charge_enable(Device* device, bool enable) {
if (enable) {
return i2c_controller_register8_set_bits(device_get_parent(device), GET_CONFIG(device)->address, REG_PWR_CFG, PWR_CFG_CHG_EN, TIMEOUT);
} else {
return i2c_controller_register8_reset_bits(device_get_parent(device), GET_CONFIG(device)->address, REG_PWR_CFG, PWR_CFG_CHG_EN, TIMEOUT);
}
}
error_t m5pm1_set_boost_enable(Device* device, bool enable) {
if (enable) {
return i2c_controller_register8_set_bits(device_get_parent(device), GET_CONFIG(device)->address, REG_PWR_CFG, PWR_CFG_BOOST_EN, TIMEOUT);
} else {
return i2c_controller_register8_reset_bits(device_get_parent(device), GET_CONFIG(device)->address, REG_PWR_CFG, PWR_CFG_BOOST_EN, TIMEOUT);
}
}
error_t m5pm1_set_ldo_enable(Device* device, bool enable) {
if (enable) {
return i2c_controller_register8_set_bits(device_get_parent(device), GET_CONFIG(device)->address, REG_PWR_CFG, PWR_CFG_LDO_EN, TIMEOUT);
} else {
return i2c_controller_register8_reset_bits(device_get_parent(device), GET_CONFIG(device)->address, REG_PWR_CFG, PWR_CFG_LDO_EN, TIMEOUT);
}
}
error_t m5pm1_get_temperature(Device* device, uint16_t* decidegc) {
Device* i2c = device_get_parent(device);
uint8_t addr = GET_CONFIG(device)->address;
// Select temperature channel and start conversion
uint8_t ctrl = static_cast<uint8_t>((ADC_CH_TEMP << 1U) | 0x01U);
error_t err = i2c_controller_register8_set(i2c, addr, REG_ADC_CTRL, ctrl, TIMEOUT);
if (err != ERROR_NONE) return err;
// Poll until conversion complete (START bit clears)
bool conversion_done = false;
for (int i = 0; i < 10; i++) {
vTaskDelay(pdMS_TO_TICKS(5));
uint8_t status = 0;
if (i2c_controller_register8_get(i2c, addr, REG_ADC_CTRL, &status, TIMEOUT) == ERROR_NONE) {
if ((status & 0x01U) == 0) {
conversion_done = true;
break;
}
}
}
if (!conversion_done) {
return ERROR_TIMEOUT;
}
return i2c_controller_register16le_get(i2c, addr, REG_ADC_RES_L, decidegc, TIMEOUT);
}
error_t m5pm1_shutdown(Device* device) {
uint8_t cmd = SYS_CMD_SHUTDOWN;
return i2c_controller_write_register(device_get_parent(device), GET_CONFIG(device)->address, REG_SYS_CMD, &cmd, 1, TIMEOUT);
}
error_t m5pm1_reboot(Device* device) {
uint8_t cmd = SYS_CMD_REBOOT;
return i2c_controller_write_register(device_get_parent(device), GET_CONFIG(device)->address, REG_SYS_CMD, &cmd, 1, TIMEOUT);
}
error_t m5pm1_btn_get_state(Device* device, bool* pressed) {
uint8_t val = 0;
error_t err = i2c_controller_register8_get(device_get_parent(device), GET_CONFIG(device)->address, REG_BTN_STATUS, &val, TIMEOUT);
if (err != ERROR_NONE) return err;
*pressed = (val & 0x01U) != 0;
return ERROR_NONE;
}
error_t m5pm1_btn_get_flag(Device* device, bool* was_pressed) {
uint8_t val = 0;
error_t err = i2c_controller_register8_get(device_get_parent(device), GET_CONFIG(device)->address, REG_BTN_STATUS, &val, TIMEOUT);
if (err != ERROR_NONE) return err;
*was_pressed = (val & 0x80U) != 0; // BTN_FLAG auto-clears on read
return ERROR_NONE;
}
error_t m5pm1_wdt_set(Device* device, uint8_t timeout_sec) {
return i2c_controller_register8_set(device_get_parent(device), GET_CONFIG(device)->address, REG_WDT_CNT, timeout_sec, TIMEOUT);
}
error_t m5pm1_wdt_feed(Device* device) {
return i2c_controller_register8_set(device_get_parent(device), GET_CONFIG(device)->address, REG_WDT_KEY, 0xA5, TIMEOUT);
}
error_t m5pm1_read_rtc_ram(Device* device, uint8_t offset, uint8_t* data, uint8_t len) {
if (offset + len > 32) return ERROR_INVALID_ARGUMENT;
return i2c_controller_read_register(device_get_parent(device), GET_CONFIG(device)->address, static_cast<uint8_t>(REG_RTC_RAM + offset), data, len, TIMEOUT);
}
error_t m5pm1_write_rtc_ram(Device* device, uint8_t offset, const uint8_t* data, uint8_t len) {
if (offset + len > 32) return ERROR_INVALID_ARGUMENT;
return i2c_controller_write_register(device_get_parent(device), GET_CONFIG(device)->address, static_cast<uint8_t>(REG_RTC_RAM + offset), data, len, TIMEOUT);
}
error_t m5pm1_set_led_count(Device* device, uint8_t count) {
if (count == 0 || count > 32) return ERROR_INVALID_ARGUMENT;
uint8_t val = count & 0x3FU;
return i2c_controller_register8_set(device_get_parent(device), GET_CONFIG(device)->address, REG_NEO_CFG, val, TIMEOUT);
}
error_t m5pm1_set_led_color(Device* device, uint8_t index, uint8_t r, uint8_t g, uint8_t b) {
if (index >= 32) return ERROR_INVALID_ARGUMENT;
Device* i2c = device_get_parent(device);
uint8_t addr = GET_CONFIG(device)->address;
// Store as RGB565: [15:11]=R5, [10:5]=G6, [4:0]=B5
uint16_t rgb565 = static_cast<uint16_t>(((r >> 3U) << 11U) | ((g >> 2U) << 5U) | (b >> 3U));
uint8_t buf[2] = { static_cast<uint8_t>(rgb565 & 0xFFU), static_cast<uint8_t>(rgb565 >> 8U) };
return i2c_controller_write_register(i2c, addr, static_cast<uint8_t>(REG_NEO_DATA + index * 2U), buf, 2, TIMEOUT);
}
error_t m5pm1_refresh_leds(Device* device) {
return i2c_controller_register8_set_bits(device_get_parent(device), GET_CONFIG(device)->address, REG_NEO_CFG, 0x40U, TIMEOUT);
}
error_t m5pm1_disable_leds(Device* device) {
// Set count to 1 and write black, then refresh
Device* i2c = device_get_parent(device);
uint8_t addr = GET_CONFIG(device)->address;
uint8_t black[2] = { 0, 0 };
error_t err = i2c_controller_write_register(i2c, addr, REG_NEO_DATA, black, 2, TIMEOUT);
if (err != ERROR_NONE) return err;
uint8_t cfg = 0x41U; // REFRESH | count=1
return i2c_controller_register8_set(i2c, addr, REG_NEO_CFG, cfg, TIMEOUT);
}
Driver m5pm1_driver = {
.name = "m5pm1",
.compatible = (const char*[]) { "m5stack,m5pm1", nullptr },
.start_device = start,
.stop_device = stop,
.api = nullptr,
.device_type = nullptr,
.owner = &m5pm1_module,
.internal = nullptr
};
} // extern "C"
+34
View File
@@ -0,0 +1,34 @@
// SPDX-License-Identifier: Apache-2.0
#include <tactility/check.h>
#include <tactility/driver.h>
#include <tactility/module.h>
extern "C" {
extern Driver m5pm1_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(&m5pm1_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(&m5pm1_driver) == ERROR_NONE);
return ERROR_NONE;
}
extern const ModuleSymbol m5pm1_module_symbols[];
Module m5pm1_module = {
.name = "m5pm1",
.start = start,
.stop = stop,
.symbols = m5pm1_module_symbols,
.internal = nullptr
};
} // extern "C"
+28
View File
@@ -0,0 +1,28 @@
// SPDX-License-Identifier: Apache-2.0
#include <drivers/m5pm1.h>
#include <tactility/module.h>
const struct ModuleSymbol m5pm1_module_symbols[] = {
DEFINE_MODULE_SYMBOL(m5pm1_get_battery_voltage),
DEFINE_MODULE_SYMBOL(m5pm1_get_vin_voltage),
DEFINE_MODULE_SYMBOL(m5pm1_get_5vout_voltage),
DEFINE_MODULE_SYMBOL(m5pm1_get_power_source),
DEFINE_MODULE_SYMBOL(m5pm1_is_charging),
DEFINE_MODULE_SYMBOL(m5pm1_set_charge_enable),
DEFINE_MODULE_SYMBOL(m5pm1_set_boost_enable),
DEFINE_MODULE_SYMBOL(m5pm1_set_ldo_enable),
DEFINE_MODULE_SYMBOL(m5pm1_get_temperature),
DEFINE_MODULE_SYMBOL(m5pm1_shutdown),
DEFINE_MODULE_SYMBOL(m5pm1_reboot),
DEFINE_MODULE_SYMBOL(m5pm1_btn_get_state),
DEFINE_MODULE_SYMBOL(m5pm1_btn_get_flag),
DEFINE_MODULE_SYMBOL(m5pm1_wdt_set),
DEFINE_MODULE_SYMBOL(m5pm1_wdt_feed),
DEFINE_MODULE_SYMBOL(m5pm1_read_rtc_ram),
DEFINE_MODULE_SYMBOL(m5pm1_write_rtc_ram),
DEFINE_MODULE_SYMBOL(m5pm1_set_led_count),
DEFINE_MODULE_SYMBOL(m5pm1_set_led_color),
DEFINE_MODULE_SYMBOL(m5pm1_refresh_leds),
DEFINE_MODULE_SYMBOL(m5pm1_disable_leds),
MODULE_SYMBOL_TERMINATOR
};