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-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
|
|
|
|
|
|
|
|
static const struct device *const vbatt_dev = DEVICE_DT_GET(VBATT_NODE);
|
2026-04-08 11:01:01 +08:00
|
|
|
static const struct device *const ip5306_dev = DEVICE_DT_GET(IP5306_NODE);
|
2026-04-07 16:58:10 +08:00
|
|
|
static struct k_work_delayable battery_sample_work;
|
2026-04-10 19:28:20 +08:00
|
|
|
static struct {
|
|
|
|
|
bool valid;
|
|
|
|
|
uint8_t soc;
|
|
|
|
|
bool charging;
|
|
|
|
|
bool full;
|
|
|
|
|
} last_bat_state;
|
2026-04-07 16:58:10 +08:00
|
|
|
static bool initialized;
|
|
|
|
|
static bool running;
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
int err = pm_device_action_run(vbatt_dev, action);
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void submit_bat_state_event(uint8_t soc, bool charging, bool full)
|
|
|
|
|
{
|
|
|
|
|
struct bat_state_event *event;
|
|
|
|
|
|
|
|
|
|
if (last_bat_state.valid &&
|
|
|
|
|
(last_bat_state.soc == soc) &&
|
|
|
|
|
(last_bat_state.charging == charging) &&
|
|
|
|
|
(last_bat_state.full == full)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
last_bat_state.valid = true;
|
|
|
|
|
last_bat_state.soc = soc;
|
|
|
|
|
last_bat_state.charging = charging;
|
|
|
|
|
last_bat_state.full = full;
|
|
|
|
|
|
|
|
|
|
event = new_bat_state_event();
|
|
|
|
|
event->soc = soc;
|
|
|
|
|
event->charging = charging;
|
|
|
|
|
event->full = full;
|
|
|
|
|
|
|
|
|
|
APP_EVENT_SUBMIT(event);
|
|
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
if (!running) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = sensor_sample_fetch(vbatt_dev);
|
|
|
|
|
if (err) {
|
|
|
|
|
LOG_WRN("Battery sample fetch failed (%d)", err);
|
|
|
|
|
goto reschedule;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = sensor_channel_get(vbatt_dev, SENSOR_CHAN_VOLTAGE, &voltage);
|
|
|
|
|
if (err) {
|
|
|
|
|
LOG_WRN("Battery channel get failed (%d)", err);
|
|
|
|
|
goto reschedule;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-08 11:01:01 +08:00
|
|
|
err = ip5306_get_status(ip5306_dev, &pmic_status);
|
|
|
|
|
if (err) {
|
|
|
|
|
LOG_WRN("IP5306 status read failed (%d)", err);
|
|
|
|
|
goto reschedule;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
voltage_mv = sensor_value_to_mv(&voltage);
|
2026-04-10 19:28:20 +08:00
|
|
|
submit_bat_state_event(battery_soc_from_mv(voltage_mv),
|
|
|
|
|
pmic_status.charging,
|
|
|
|
|
pmic_status.full);
|
2026-04-07 16:58:10 +08:00
|
|
|
|
|
|
|
|
reschedule:
|
|
|
|
|
if (running) {
|
|
|
|
|
k_work_reschedule(&battery_sample_work, BATTERY_SAMPLE_INTERVAL);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int module_init(void)
|
|
|
|
|
{
|
|
|
|
|
if (!device_is_ready(vbatt_dev)) {
|
|
|
|
|
LOG_ERR("vbatt device not ready");
|
|
|
|
|
return -ENODEV;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-08 11:01:01 +08:00
|
|
|
if (!device_is_ready(ip5306_dev)) {
|
|
|
|
|
LOG_ERR("ip5306 device not ready");
|
|
|
|
|
return -ENODEV;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int err = ip5306_init(ip5306_dev);
|
|
|
|
|
if (err) {
|
|
|
|
|
LOG_ERR("ip5306 init failed (%d)", err);
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-07 16:58:10 +08:00
|
|
|
k_work_init_delayable(&battery_sample_work, battery_sample_fn);
|
2026-04-10 19:28:20 +08:00
|
|
|
memset(&last_bat_state, 0, sizeof(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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int module_start(void)
|
|
|
|
|
{
|
|
|
|
|
int err;
|
|
|
|
|
|
|
|
|
|
if (running) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = measurement_enable(true);
|
|
|
|
|
if (err) {
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
running = true;
|
|
|
|
|
k_work_reschedule(&battery_sample_work, K_NO_WAIT);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void module_pause(void)
|
|
|
|
|
{
|
|
|
|
|
if (!running) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
(void)k_work_cancel_delayable(&battery_sample_work);
|
|
|
|
|
(void)measurement_enable(false);
|
|
|
|
|
running = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)) {
|
|
|
|
|
int err;
|
|
|
|
|
|
|
|
|
|
if (!initialized) {
|
|
|
|
|
err = module_init();
|
|
|
|
|
if (err) {
|
|
|
|
|
module_set_state(MODULE_STATE_ERROR);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
initialized = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = module_start();
|
|
|
|
|
if (err) {
|
|
|
|
|
module_set_state(MODULE_STATE_ERROR);
|
|
|
|
|
} else {
|
|
|
|
|
module_set_state(MODULE_STATE_READY);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (is_power_down_event(aeh)) {
|
|
|
|
|
if (initialized) {
|
|
|
|
|
module_pause();
|
|
|
|
|
module_set_state(MODULE_STATE_STANDBY);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (is_wake_up_event(aeh)) {
|
|
|
|
|
if (initialized) {
|
|
|
|
|
int err = module_start();
|
|
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
|
module_set_state(MODULE_STATE_ERROR);
|
|
|
|
|
} else {
|
|
|
|
|
module_set_state(MODULE_STATE_READY);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|