2026-04-08 14:28:05 +08:00
|
|
|
#include <stdbool.h>
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
|
|
#include <app_event_manager.h>
|
|
|
|
|
|
|
|
|
|
#define MODULE mode_switch_module
|
|
|
|
|
#include <caf/events/module_state_event.h>
|
|
|
|
|
#include <caf/events/power_event.h>
|
|
|
|
|
|
|
|
|
|
#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-17 19:12:57 +08:00
|
|
|
#include "module_lifecycle.h"
|
2026-04-08 14:28:05 +08:00
|
|
|
#include "mode_switch_event.h"
|
|
|
|
|
|
|
|
|
|
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
|
|
|
|
|
|
|
|
|
#define MODE_SWITCH_ADC_NODE DT_NODELABEL(mode_switch_adc)
|
|
|
|
|
#define MODE_SWITCH_SAMPLE_INTERVAL K_MSEC(500)
|
|
|
|
|
#define MODE_SWITCH_USB_MAX_MV 825
|
|
|
|
|
#define MODE_SWITCH_24G_MAX_MV 2475
|
|
|
|
|
|
|
|
|
|
BUILD_ASSERT(DT_NODE_EXISTS(MODE_SWITCH_ADC_NODE),
|
|
|
|
|
"Missing mode_switch_adc node in devicetree");
|
|
|
|
|
|
2026-04-17 19:12:57 +08:00
|
|
|
struct mode_switch_module_ctx {
|
|
|
|
|
struct module_lifecycle_ctx lc;
|
|
|
|
|
const struct device *mode_switch_adc_dev;
|
|
|
|
|
struct k_work_delayable mode_switch_sample_work;
|
|
|
|
|
bool force_report;
|
|
|
|
|
bool mode_valid;
|
|
|
|
|
enum mode_switch_mode last_mode;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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 mode_switch_module_ctx ctx = {
|
|
|
|
|
.lc = {
|
|
|
|
|
.state = LC_UNINIT,
|
|
|
|
|
.cfg = &lifecycle_cfg,
|
|
|
|
|
.ops = &lifecycle_ops,
|
|
|
|
|
},
|
|
|
|
|
.mode_switch_adc_dev = DEVICE_DT_GET(MODE_SWITCH_ADC_NODE),
|
|
|
|
|
};
|
2026-04-08 14:28:05 +08:00
|
|
|
|
|
|
|
|
static enum mode_switch_mode detect_mode(uint16_t voltage_mv)
|
|
|
|
|
{
|
|
|
|
|
if (voltage_mv < MODE_SWITCH_USB_MAX_MV) {
|
|
|
|
|
return MODE_SWITCH_USB;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (voltage_mv < MODE_SWITCH_24G_MAX_MV) {
|
|
|
|
|
return MODE_SWITCH_24G;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return MODE_SWITCH_BLE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int mode_switch_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.mode_switch_adc_dev, action);
|
2026-04-08 14:28:05 +08:00
|
|
|
|
|
|
|
|
if (err && (err != -EALREADY) && (err != -ENOTSUP)) {
|
|
|
|
|
LOG_ERR("Cannot %s mode switch ADC (%d)",
|
|
|
|
|
enable ? "resume" : "suspend", err);
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void mode_switch_sample_fn(struct k_work *work)
|
|
|
|
|
{
|
|
|
|
|
struct sensor_value voltage;
|
|
|
|
|
int err;
|
|
|
|
|
|
|
|
|
|
ARG_UNUSED(work);
|
|
|
|
|
|
2026-04-17 19:12:57 +08:00
|
|
|
if (!module_lifecycle_is_running(&ctx.lc)) {
|
2026-04-08 14:28:05 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-17 19:12:57 +08:00
|
|
|
err = sensor_sample_fetch(ctx.mode_switch_adc_dev);
|
2026-04-08 14:28:05 +08:00
|
|
|
if (err) {
|
|
|
|
|
LOG_WRN("Mode switch ADC sample fetch failed (%d)", err);
|
|
|
|
|
goto reschedule;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-17 19:12:57 +08:00
|
|
|
err = sensor_channel_get(ctx.mode_switch_adc_dev, SENSOR_CHAN_VOLTAGE,
|
|
|
|
|
&voltage);
|
2026-04-08 14:28:05 +08:00
|
|
|
if (err) {
|
|
|
|
|
LOG_WRN("Mode switch ADC channel get failed (%d)", err);
|
|
|
|
|
goto reschedule;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint16_t sample_mv = (uint16_t)((voltage.val1 * 1000) + (voltage.val2 / 1000));
|
|
|
|
|
enum mode_switch_mode mode = detect_mode(sample_mv);
|
|
|
|
|
|
2026-04-17 19:12:57 +08:00
|
|
|
if (ctx.force_report || !ctx.mode_valid || (mode != ctx.last_mode)) {
|
2026-04-14 16:42:04 +08:00
|
|
|
submit_mode_switch_event(mode, sample_mv);
|
2026-04-08 14:28:05 +08:00
|
|
|
|
2026-04-17 19:12:57 +08:00
|
|
|
ctx.last_mode = mode;
|
|
|
|
|
ctx.mode_valid = true;
|
|
|
|
|
ctx.force_report = false;
|
2026-04-08 14:28:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
reschedule:
|
2026-04-17 19:12:57 +08:00
|
|
|
if (module_lifecycle_is_running(&ctx.lc)) {
|
|
|
|
|
k_work_reschedule(&ctx.mode_switch_sample_work,
|
|
|
|
|
MODE_SWITCH_SAMPLE_INTERVAL);
|
2026-04-08 14:28:05 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-17 19:12:57 +08:00
|
|
|
static int do_init(void)
|
2026-04-08 14:28:05 +08:00
|
|
|
{
|
2026-04-17 19:12:57 +08:00
|
|
|
if (!device_is_ready(ctx.mode_switch_adc_dev)) {
|
2026-04-08 14:28:05 +08:00
|
|
|
LOG_ERR("Mode switch ADC device not ready");
|
|
|
|
|
return -ENODEV;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-17 19:12:57 +08:00
|
|
|
k_work_init_delayable(&ctx.mode_switch_sample_work, mode_switch_sample_fn);
|
|
|
|
|
ctx.mode_valid = false;
|
|
|
|
|
ctx.force_report = false;
|
2026-04-08 14:28:05 +08:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-17 19:12:57 +08:00
|
|
|
static int do_start(void)
|
2026-04-08 14:28:05 +08:00
|
|
|
{
|
|
|
|
|
int err;
|
|
|
|
|
|
2026-04-17 19:12:57 +08:00
|
|
|
if (module_lifecycle_is_running(&ctx.lc)) {
|
2026-04-08 14:28:05 +08:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = mode_switch_enable(true);
|
|
|
|
|
if (err) {
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-17 19:12:57 +08:00
|
|
|
ctx.force_report = true;
|
|
|
|
|
k_work_reschedule(&ctx.mode_switch_sample_work, K_NO_WAIT);
|
2026-04-08 14:28:05 +08:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-17 19:12:57 +08:00
|
|
|
static int do_stop(void)
|
2026-04-08 14:28:05 +08:00
|
|
|
{
|
2026-04-17 19:12:57 +08:00
|
|
|
if (!module_lifecycle_is_running(&ctx.lc)) {
|
|
|
|
|
return 0;
|
2026-04-08 14:28:05 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-17 19:12:57 +08:00
|
|
|
(void)k_work_cancel_delayable(&ctx.mode_switch_sample_work);
|
2026-04-08 14:28:05 +08:00
|
|
|
(void)mode_switch_enable(false);
|
2026-04-17 19:12:57 +08:00
|
|
|
|
|
|
|
|
return 0;
|
2026-04-08 14:28:05 +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-08 14:28:05 +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-08 14:28:05 +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-08 14:28:05 +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);
|