refactor(events): 移除未使用的usb_hid_event定义

移除src/events/usb_hid_event.c和usb_hid_event.h文件,
以及CMakeLists.txt中的相关源文件引用。

feat(modules): 电池模块增加充电状态电源管理限制策略

- 添加充电状态下的电源限制逻辑,充电时限制到ALIVE级别禁止自动休眠,
  非充电时恢复到SUSPENDED级别
- 默认非充电态允许进入SUSPENDED但禁止进入OFF

refactor(modules): 重构usb_hid_module减少模块间耦合

- 移除对power_manager_event.h和usb_hid_event.h的依赖
- 简化模块功能描述,去除对外发布usb_hid_event的职责
- 移除内部usb_hid_event相关的状态管理和发布逻辑
- 修改USB连接处理逻辑,在VBUS就绪时提交唤醒事件
This commit is contained in:
2026-03-16 17:09:49 +08:00
parent 4e8bb71f83
commit a9025d0f49
5 changed files with 29 additions and 164 deletions

View File

@@ -63,6 +63,7 @@ static int32_t battery_mv_window[BATTERY_MV_WINDOW_SIZE];
static int64_t battery_mv_sum;
static size_t battery_mv_count;
static size_t battery_mv_index;
static enum power_manager_level pm_restrict_level = POWER_MANAGER_LEVEL_MAX;
static void battery_module_resume(void);
@@ -191,6 +192,23 @@ static void publish_battery_status_event(const struct battery_status *status)
APP_EVENT_SUBMIT(event);
}
/*
* 电源限制策略:
* - 充电线插入charging=true时限制到 ALIVE禁止自动休眠
* - 非充电时恢复到 SUSPENDED允许系统进入挂起但不进入 OFF。
*/
static void update_power_restrict_by_charging(bool charging)
{
enum power_manager_level target = charging ?
POWER_MANAGER_LEVEL_ALIVE : POWER_MANAGER_LEVEL_SUSPENDED;
if (pm_restrict_level == target)
return;
pm_restrict_level = target;
power_manager_restrict(MODULE_IDX(MODULE), target);
}
static void battery_sample_fn(struct k_work *work)
{
ARG_UNUSED(work);
@@ -208,6 +226,8 @@ static void battery_sample_fn(struct k_work *work)
goto out_reschedule;
}
update_power_restrict_by_charging(sampled.charging);
/*
* 仅在状态发生变化时上报,避免重复事件淹没总线。
* 变化条件充电标志、满电标志、SOC 任意一个变化。
@@ -260,11 +280,8 @@ static int battery_module_init(void)
return err;
}
/*
* 按需求将最深电源级别限制在 SUSPENDED禁止进入 OFF。
* 这样即使 CPU 进入挂起IP5305 的硬件保活后端仍可持续输出保活脉冲。
*/
power_manager_restrict(MODULE_IDX(MODULE), POWER_MANAGER_LEVEL_SUSPENDED);
/* 默认非充电态允许进入 SUSPENDED但禁止进入 OFF。 */
update_power_restrict_by_charging(false);
k_work_init_delayable(&battery_sample_work, battery_sample_fn);
has_last_status = false;

View File

@@ -8,7 +8,6 @@
#include <app_event_manager.h>
#include <caf/events/power_event.h>
#include <caf/events/power_manager_event.h>
#define MODULE usb_hid
#include <caf/events/module_state_event.h>
@@ -18,7 +17,6 @@
#include "hid_report_event.h"
#include "keyboard_led_state_event.h"
#include "mode_event.h"
#include "usb_hid_event.h"
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
@@ -28,8 +26,8 @@ LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
/*
* 模块目标:
* 1) 模块内聚控制 USB HID 栈生命周期(初始化/启用/禁用),不依赖外部 usb_hid_event 控制
* 2) 对外统一上报 usb_hid_event供 LED/上层状态机消费
* 1) 模块内聚控制 USB HID 栈生命周期(初始化/启用/禁用)。
* 2) 对外发布 HID 协议事件和 NumLock 指示事件
* 3) 仅响应 mode_eventUSB/BLE/2.4G)和 power_event休眠/唤醒)。
*
* 约束:
@@ -55,15 +53,11 @@ struct usb_hid_ctx {
bool boot_in_flight;
bool nkro_in_flight;
enum usb_hid_usbd_state usbd_state;
enum hid_protocol_type current_protocol;
enum power_manager_level pm_restrict_level;
};
static struct usb_hid_ctx g_usb_hid = {
.usbd_state = USB_HID_USBD_VBUS_DISCONNECTED,
.current_protocol = HID_PROTO_REPORT,
.pm_restrict_level = POWER_MANAGER_LEVEL_MAX,
};
USBD_DEVICE_DEFINE(new_kbd_usbd,
@@ -79,17 +73,6 @@ static const uint8_t boot_report_desc[] = HID_KEYBOARD_REPORT_DESC();
static const uint8_t nkro_report_desc[] = HID_DESC_KEYBOARD_NKRO_CONSUMER();
static const uint8_t raw_report_desc[] = HID_DESC_RAW_64();
static void publish_usb_hid_state(void)
{
struct usb_hid_event *event = new_usb_hid_event();
event->evt_type = USB_HID_EVT_STATE_REPORT;
event->stack_state = g_usb_hid.stack_enabled ?
USB_HID_STACK_ENABLED : USB_HID_STACK_DISABLED;
event->usbd_state = g_usb_hid.usbd_state;
APP_EVENT_SUBMIT(event);
}
/* 统一入口仅处理单字节 LED 报告并发布事件。 */
static void process_usb_led_input_report(uint8_t led_report)
{
@@ -136,40 +119,6 @@ static bool try_extract_led_mask(const struct device *dev,
return true;
}
static void recompute_hid_state(void)
{
/* 兼容现有调用点:对外仅发布 enable + usbd 状态。 */
publish_usb_hid_state();
}
/*
* USB 自动休眠策略:
* - 只要检测到 USB 线已连接USBD 状态非 DISCONNECTED限制系统保持 ALIVE
* - USB 线断开后,恢复为 MAX允许系统按原有超时策略自动休眠。
*/
static void refresh_usb_power_restrict(void)
{
enum power_manager_level target =
(g_usb_hid.usbd_state == USB_HID_USBD_VBUS_DISCONNECTED) ?
POWER_MANAGER_LEVEL_MAX : POWER_MANAGER_LEVEL_ALIVE;
if (g_usb_hid.pm_restrict_level == target) {
return;
}
g_usb_hid.pm_restrict_level = target;
power_manager_restrict(MODULE_IDX(MODULE), target);
}
static void set_usbd_state(enum usb_hid_usbd_state state)
{
if (g_usb_hid.usbd_state != state) {
g_usb_hid.usbd_state = state;
refresh_usb_power_restrict();
publish_usb_hid_state();
}
}
static int hid_stub_get_report(const struct device *dev,
uint8_t type, uint8_t id,
uint16_t len, uint8_t *buf)
@@ -308,7 +257,6 @@ static void hid_iface_ready_cb(const struct device *dev, bool ready)
APP_EVENT_SUBMIT(event);
}
recompute_hid_state();
}
static const struct hid_device_ops boot_hid_ops = {
@@ -348,7 +296,11 @@ static void usbd_msg_cb(struct usbd_context *const usbd_ctx,
{
switch (msg->type) {
case USBD_MSG_VBUS_READY:
set_usbd_state(USB_HID_USBD_VBUS_CONNECTED);
if (g_usb_hid.pm_suspended) {
LOG_INF("VBUS ready: submit wake_up_event");
APP_EVENT_SUBMIT(new_wake_up_event());
}
/*
* 只有在 USB 模式下才允许拉起 USB 栈。
* 这样即使插着线,只要用户切到 BLE/2.4G,也不会强制进入 USB HID。
@@ -359,26 +311,17 @@ static void usbd_msg_cb(struct usbd_context *const usbd_ctx,
break;
case USBD_MSG_VBUS_REMOVED:
set_usbd_state(USB_HID_USBD_VBUS_DISCONNECTED);
break;
case USBD_MSG_SUSPEND:
publish_usb_hid_state();
break;
case USBD_MSG_RESUME:
publish_usb_hid_state();
break;
case USBD_MSG_CONFIGURATION:
publish_usb_hid_state();
break;
case USBD_MSG_UDC_ERROR:
case USBD_MSG_STACK_ERROR:
LOG_ERR("USBD stack error message: %d", msg->type);
g_usb_hid.stack_error = true;
recompute_hid_state();
break;
default:
@@ -526,7 +469,6 @@ static int usb_hid_stack_init(void)
}
g_usb_hid.stack_initialized = true;
recompute_hid_state();
return 0;
}
@@ -556,17 +498,14 @@ static int usb_hid_set_enabled(bool enable)
g_usb_hid.raw_iface_ready = false;
g_usb_hid.boot_in_flight = false;
g_usb_hid.nkro_in_flight = false;
publish_usb_hid_state();
}
if (err && (err != -EALREADY)) {
LOG_ERR("usbd_%s failed: %d", enable ? "enable" : "disable", err);
g_usb_hid.stack_error = true;
recompute_hid_state();
return err;
}
recompute_hid_state();
return 0;
}
@@ -596,13 +535,10 @@ static bool handle_module_state_event(const struct module_state_event *event)
LOG_ERR("USB HID stack init failed: %d", err);
g_usb_hid.stack_error = true;
module_set_state(MODULE_STATE_ERROR);
recompute_hid_state();
return false;
}
refresh_usb_power_restrict();
module_set_state(MODULE_STATE_READY);
publish_usb_hid_state();
return false;
}