2026-04-07 16:58:10 +08:00
|
|
|
#include <stdbool.h>
|
|
|
|
|
#include <stdint.h>
|
2026-04-10 19:28:20 +08:00
|
|
|
#include <string.h>
|
2026-04-07 16:58:10 +08:00
|
|
|
|
|
|
|
|
#include <app_event_manager.h>
|
|
|
|
|
|
|
|
|
|
#define MODULE battery_module
|
|
|
|
|
#include <caf/events/module_state_event.h>
|
2026-04-08 11:01:01 +08:00
|
|
|
#include <caf/events/power_manager_event.h>
|
2026-04-07 16:58:10 +08:00
|
|
|
#include <caf/events/power_event.h>
|
|
|
|
|
|
2026-04-08 11:01:01 +08:00
|
|
|
#include <drivers/pmic/ip5306.h>
|
2026-04-07 16:58:10 +08:00
|
|
|
#include <zephyr/device.h>
|
|
|
|
|
#include <zephyr/devicetree.h>
|
|
|
|
|
#include <zephyr/drivers/sensor.h>
|
|
|
|
|
#include <zephyr/kernel.h>
|
|
|
|
|
#include <zephyr/logging/log.h>
|
|
|
|
|
#include <zephyr/pm/device.h>
|
|
|
|
|
|
2026-04-10 19:28:20 +08:00
|
|
|
#include "bat_state_event.h"
|
2026-04-17 19:12:57 +08:00
|
|
|
#include "module_lifecycle.h"
|
2026-04-10 19:28:20 +08:00
|
|
|
|
2026-04-07 16:58:10 +08:00
|
|
|
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
|
|
|
|
|
|
|
|
|
#define VBATT_NODE DT_PATH(vbatt)
|
2026-04-08 11:01:01 +08:00
|
|
|
#define IP5306_NODE DT_NODELABEL(ip5306)
|
2026-04-07 16:58:10 +08:00
|
|
|
#define BATTERY_SAMPLE_INTERVAL K_SECONDS(1)
|
2026-04-10 19:28:20 +08:00
|
|
|
#define BATTERY_SOC_MIN_MV 3300
|
|
|
|
|
#define BATTERY_SOC_MAX_MV 4200
|
2026-04-07 16:58:10 +08:00
|
|
|
|
|
|
|
|
BUILD_ASSERT(DT_NODE_HAS_STATUS(VBATT_NODE, okay),
|
|
|
|
|
"Missing /vbatt voltage-divider node in devicetree");
|
2026-04-08 11:01:01 +08:00
|
|
|
BUILD_ASSERT(DT_NODE_HAS_STATUS(IP5306_NODE, okay),
|
|
|
|
|
"Missing ip5306 node in devicetree");
|
2026-04-07 16:58:10 +08:00
|
|
|
|
2026-04-17 19:12:57 +08:00
|
|
|
struct battery_module_ctx {
|
|
|
|
|
struct module_lifecycle_ctx lc;
|
|
|
|
|
const struct device *vbatt_dev;
|
|
|
|
|
const struct device *ip5306_dev;
|
|
|
|
|
struct k_work_delayable battery_sample_work;
|
|
|
|
|
struct {
|
|
|
|
|
bool valid;
|
|
|
|
|
uint8_t soc;
|
|
|
|
|
bool charging;
|
|
|
|
|
bool full;
|
|
|
|
|
} last_bat_state;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static int do_init(void);
|
|
|
|
|
static int do_start(void);
|
|
|
|
|
static int do_stop(void);
|
|
|
|
|
|
|
|
|
|
static const struct module_lifecycle_cfg lifecycle_cfg = {
|
|
|
|
|
.mode = ML_MODE_POWER,
|
|
|
|
|
.stopped_state = MODULE_STATE_STANDBY,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static const struct module_lifecycle_ops lifecycle_ops = {
|
|
|
|
|
.do_init = do_init,
|
|
|
|
|
.do_start = do_start,
|
|
|
|
|
.do_stop = do_stop,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static struct battery_module_ctx ctx = {
|
|
|
|
|
.lc = {
|
|
|
|
|
.state = LC_UNINIT,
|
|
|
|
|
.cfg = &lifecycle_cfg,
|
|
|
|
|
.ops = &lifecycle_ops,
|
|
|
|
|
},
|
|
|
|
|
.vbatt_dev = DEVICE_DT_GET(VBATT_NODE),
|
|
|
|
|
.ip5306_dev = DEVICE_DT_GET(IP5306_NODE),
|
|
|
|
|
};
|
2026-04-07 16:58:10 +08:00
|
|
|
|
|
|
|
|
static int sensor_value_to_mv(const struct sensor_value *value)
|
|
|
|
|
{
|
|
|
|
|
return (value->val1 * 1000) + (value->val2 / 1000);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int measurement_enable(bool enable)
|
|
|
|
|
{
|
|
|
|
|
enum pm_device_action action = enable ? PM_DEVICE_ACTION_RESUME
|
|
|
|
|
: PM_DEVICE_ACTION_SUSPEND;
|
2026-04-17 19:12:57 +08:00
|
|
|
int err = pm_device_action_run(ctx.vbatt_dev, action);
|
2026-04-07 16:58:10 +08:00
|
|
|
|
|
|
|
|
if (err && (err != -EALREADY) && (err != -ENOTSUP)) {
|
|
|
|
|
LOG_ERR("Cannot %s vbatt sensor (%d)", enable ? "resume" : "suspend", err);
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-10 19:28:20 +08:00
|
|
|
static uint8_t battery_soc_from_mv(int voltage_mv)
|
|
|
|
|
{
|
|
|
|
|
const int span_mv = BATTERY_SOC_MAX_MV - BATTERY_SOC_MIN_MV;
|
|
|
|
|
int bucket;
|
|
|
|
|
|
|
|
|
|
if (voltage_mv <= BATTERY_SOC_MIN_MV) {
|
|
|
|
|
return 0U;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (voltage_mv >= BATTERY_SOC_MAX_MV) {
|
|
|
|
|
return 100U;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bucket = ((voltage_mv - BATTERY_SOC_MIN_MV) * 10 + (span_mv / 2)) / span_mv;
|
|
|
|
|
|
|
|
|
|
return (uint8_t)(bucket * 10);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-07 16:58:10 +08:00
|
|
|
static void battery_sample_fn(struct k_work *work)
|
|
|
|
|
{
|
2026-04-08 11:01:01 +08:00
|
|
|
struct ip5306_status pmic_status;
|
2026-04-07 16:58:10 +08:00
|
|
|
struct sensor_value voltage;
|
2026-04-08 11:01:01 +08:00
|
|
|
int voltage_mv;
|
2026-04-07 16:58:10 +08:00
|
|
|
int err;
|
|
|
|
|
|
|
|
|
|
ARG_UNUSED(work);
|
|
|
|
|
|
2026-04-17 19:12:57 +08:00
|
|
|
if (!module_lifecycle_is_running(&ctx.lc)) {
|
2026-04-07 16:58:10 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-17 19:12:57 +08:00
|
|
|
err = sensor_sample_fetch(ctx.vbatt_dev);
|
2026-04-07 16:58:10 +08:00
|
|
|
if (err) {
|
|
|
|
|
LOG_WRN("Battery sample fetch failed (%d)", err);
|
|
|
|
|
goto reschedule;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-17 19:12:57 +08:00
|
|
|
err = sensor_channel_get(ctx.vbatt_dev, SENSOR_CHAN_VOLTAGE, &voltage);
|
2026-04-07 16:58:10 +08:00
|
|
|
if (err) {
|
|
|
|
|
LOG_WRN("Battery channel get failed (%d)", err);
|
|
|
|
|
goto reschedule;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-17 19:12:57 +08:00
|
|
|
err = ip5306_get_status(ctx.ip5306_dev, &pmic_status);
|
2026-04-08 11:01:01 +08:00
|
|
|
if (err) {
|
|
|
|
|
LOG_WRN("IP5306 status read failed (%d)", err);
|
|
|
|
|
goto reschedule;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
voltage_mv = sensor_value_to_mv(&voltage);
|
2026-04-14 16:42:04 +08:00
|
|
|
uint8_t soc = battery_soc_from_mv(voltage_mv);
|
|
|
|
|
|
2026-04-17 19:12:57 +08:00
|
|
|
if (!ctx.last_bat_state.valid ||
|
|
|
|
|
(ctx.last_bat_state.soc != soc) ||
|
|
|
|
|
(ctx.last_bat_state.charging != pmic_status.charging) ||
|
|
|
|
|
(ctx.last_bat_state.full != pmic_status.full)) {
|
|
|
|
|
ctx.last_bat_state.valid = true;
|
|
|
|
|
ctx.last_bat_state.soc = soc;
|
|
|
|
|
ctx.last_bat_state.charging = pmic_status.charging;
|
|
|
|
|
ctx.last_bat_state.full = pmic_status.full;
|
2026-04-14 16:42:04 +08:00
|
|
|
submit_bat_state_event(soc, pmic_status.charging, pmic_status.full);
|
|
|
|
|
}
|
2026-04-07 16:58:10 +08:00
|
|
|
|
|
|
|
|
reschedule:
|
2026-04-17 19:12:57 +08:00
|
|
|
if (module_lifecycle_is_running(&ctx.lc)) {
|
|
|
|
|
k_work_reschedule(&ctx.battery_sample_work, BATTERY_SAMPLE_INTERVAL);
|
2026-04-07 16:58:10 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-17 19:12:57 +08:00
|
|
|
static int do_init(void)
|
2026-04-07 16:58:10 +08:00
|
|
|
{
|
2026-04-17 19:12:57 +08:00
|
|
|
if (!device_is_ready(ctx.vbatt_dev)) {
|
2026-04-07 16:58:10 +08:00
|
|
|
LOG_ERR("vbatt device not ready");
|
|
|
|
|
return -ENODEV;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-17 19:12:57 +08:00
|
|
|
if (!device_is_ready(ctx.ip5306_dev)) {
|
2026-04-08 11:01:01 +08:00
|
|
|
LOG_ERR("ip5306 device not ready");
|
|
|
|
|
return -ENODEV;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-17 19:12:57 +08:00
|
|
|
int err = ip5306_init(ctx.ip5306_dev);
|
2026-04-08 11:01:01 +08:00
|
|
|
if (err) {
|
|
|
|
|
LOG_ERR("ip5306 init failed (%d)", err);
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-17 19:12:57 +08:00
|
|
|
k_work_init_delayable(&ctx.battery_sample_work, battery_sample_fn);
|
|
|
|
|
memset(&ctx.last_bat_state, 0, sizeof(ctx.last_bat_state));
|
2026-04-08 11:01:01 +08:00
|
|
|
power_manager_restrict(MODULE_IDX(MODULE), POWER_MANAGER_LEVEL_SUSPENDED);
|
2026-04-07 16:58:10 +08:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-17 19:12:57 +08:00
|
|
|
static int do_start(void)
|
2026-04-07 16:58:10 +08:00
|
|
|
{
|
|
|
|
|
int err;
|
|
|
|
|
|
2026-04-17 19:12:57 +08:00
|
|
|
if (module_lifecycle_is_running(&ctx.lc)) {
|
2026-04-07 16:58:10 +08:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = measurement_enable(true);
|
|
|
|
|
if (err) {
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-17 19:12:57 +08:00
|
|
|
k_work_reschedule(&ctx.battery_sample_work, K_NO_WAIT);
|
2026-04-07 16:58:10 +08:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-17 19:12:57 +08:00
|
|
|
static int do_stop(void)
|
2026-04-07 16:58:10 +08:00
|
|
|
{
|
2026-04-17 19:12:57 +08:00
|
|
|
if (!module_lifecycle_is_running(&ctx.lc)) {
|
|
|
|
|
return 0;
|
2026-04-07 16:58:10 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-17 19:12:57 +08:00
|
|
|
(void)k_work_cancel_delayable(&ctx.battery_sample_work);
|
2026-04-07 16:58:10 +08:00
|
|
|
(void)measurement_enable(false);
|
2026-04-17 19:12:57 +08:00
|
|
|
|
|
|
|
|
return 0;
|
2026-04-07 16:58:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool app_event_handler(const struct app_event_header *aeh)
|
|
|
|
|
{
|
|
|
|
|
if (is_module_state_event(aeh)) {
|
|
|
|
|
const struct module_state_event *event = cast_module_state_event(aeh);
|
|
|
|
|
|
|
|
|
|
if (check_state(event, MODULE_ID(main), MODULE_STATE_READY)) {
|
2026-04-17 19:12:57 +08:00
|
|
|
(void)module_set_lifecycle(&ctx.lc, LC_RUNNING);
|
2026-04-07 16:58:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (is_power_down_event(aeh)) {
|
2026-04-17 19:12:57 +08:00
|
|
|
if (module_lifecycle_is_initialized(&ctx.lc)) {
|
|
|
|
|
(void)module_set_lifecycle(&ctx.lc, LC_STOPPED);
|
2026-04-07 16:58:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (is_wake_up_event(aeh)) {
|
2026-04-17 19:12:57 +08:00
|
|
|
if (module_lifecycle_is_initialized(&ctx.lc)) {
|
|
|
|
|
(void)module_set_lifecycle(&ctx.lc, LC_RUNNING);
|
2026-04-07 16:58:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
__ASSERT_NO_MSG(false);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
APP_EVENT_LISTENER(MODULE, app_event_handler);
|
|
|
|
|
APP_EVENT_SUBSCRIBE(MODULE, module_state_event);
|
|
|
|
|
APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event);
|
|
|
|
|
APP_EVENT_SUBSCRIBE(MODULE, wake_up_event);
|