feat(usb): 简化USB状态管理并引入模式策略模块
- 修改usb_state_event结构,将复杂的flag操作简化为单一的state枚举值 - 新增usb_function_hook机制用于USB功能预初始化 - 将ble_adv_ctrl_module重命名为mode_policy_module以更好地反映其功能 - 在mode_policy_module中添加USB设备启用/暂停控制逻辑 - 添加对电源事件的处理支持休眠/唤醒功能 - 更新CMakeLists.txt添加必要的链接器脚本和源文件 - 移除不再需要的ble_adv_ctrl_module并添加新的mode_policy_module
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
|
||||
#define MODULE usb_device_module
|
||||
#include <caf/events/module_state_event.h>
|
||||
#include <caf/events/module_suspend_event.h>
|
||||
#include <caf/events/power_event.h>
|
||||
|
||||
#include <zephyr/logging/log.h>
|
||||
@@ -13,6 +14,7 @@
|
||||
|
||||
#include <caf/events/power_manager_event.h>
|
||||
|
||||
#include "usb_function_hook.h"
|
||||
#include "usb_state_event.h"
|
||||
|
||||
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||
@@ -21,7 +23,6 @@ LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||
#define USB_DEVICE_PID 0x52F0
|
||||
#define USB_DEVICE_MANUFACTURER "Atguigu"
|
||||
#define USB_DEVICE_PRODUCT "WH Mini Keyboard"
|
||||
#define USB_REQUIRED_READY_MASK (USB_STATEF_HID_READY | USB_STATEF_CDC_READY)
|
||||
|
||||
USBD_DEVICE_DEFINE(blinky_usbd, DEVICE_DT_GET(DT_NODELABEL(usbd)),
|
||||
USB_DEVICE_VID, USB_DEVICE_PID);
|
||||
@@ -39,69 +40,65 @@ static const char *const class_blocklist[] = {
|
||||
|
||||
static bool initialized;
|
||||
static bool running;
|
||||
static bool usbd_initialized;
|
||||
static bool usb_enabled;
|
||||
static uint32_t usb_state_flags;
|
||||
|
||||
enum usb_runtime_state {
|
||||
USB_RUNTIME_STATE_DISCONNECTED,
|
||||
USB_RUNTIME_STATE_POWERED,
|
||||
USB_RUNTIME_STATE_ACTIVE,
|
||||
USB_RUNTIME_STATE_SUSPENDED,
|
||||
enum usb_stack_state {
|
||||
USB_STACK_UNINITIALIZED = 0,
|
||||
USB_STACK_DISABLED,
|
||||
USB_STACK_READY,
|
||||
USB_STACK_ENABLED,
|
||||
};
|
||||
|
||||
static enum usb_runtime_state device_state = USB_RUNTIME_STATE_DISCONNECTED;
|
||||
enum usb_bus_state {
|
||||
USB_BUS_DISCONNECTED = 0,
|
||||
USB_BUS_POWERED,
|
||||
USB_BUS_ACTIVE,
|
||||
USB_BUS_SUSPENDED,
|
||||
};
|
||||
|
||||
static void broadcast_usb_state(void)
|
||||
struct usb_owner_ctx {
|
||||
enum usb_stack_state stack;
|
||||
enum usb_bus_state bus;
|
||||
};
|
||||
|
||||
static struct usb_owner_ctx usb_ctx = {
|
||||
.stack = USB_STACK_UNINITIALIZED,
|
||||
.bus = USB_BUS_DISCONNECTED,
|
||||
};
|
||||
|
||||
static inline enum usb_state usb_public_state_get(void)
|
||||
{
|
||||
submit_usb_state_snapshot(MODULE_ID(MODULE), usb_state_flags);
|
||||
if ((usb_ctx.stack == USB_STACK_UNINITIALIZED) ||
|
||||
(usb_ctx.stack == USB_STACK_DISABLED)) {
|
||||
return USB_STATE_DISABLED;
|
||||
}
|
||||
|
||||
switch (usb_ctx.bus) {
|
||||
case USB_BUS_DISCONNECTED:
|
||||
return USB_STATE_DISCONNECTED;
|
||||
case USB_BUS_POWERED:
|
||||
return USB_STATE_POWERED;
|
||||
case USB_BUS_ACTIVE:
|
||||
return USB_STATE_ACTIVE;
|
||||
case USB_BUS_SUSPENDED:
|
||||
return USB_STATE_SUSPENDED;
|
||||
default:
|
||||
return USB_STATE_DISABLED;
|
||||
}
|
||||
}
|
||||
|
||||
static void set_usb_state_flags(uint32_t mask)
|
||||
static inline bool usb_stack_was_initialized(void)
|
||||
{
|
||||
usb_state_flags |= mask;
|
||||
return (usb_ctx.stack != USB_STACK_UNINITIALIZED);
|
||||
}
|
||||
|
||||
static void clear_usb_state_flags(uint32_t mask)
|
||||
static inline void usb_bus_set(enum usb_bus_state state)
|
||||
{
|
||||
usb_state_flags &= ~mask;
|
||||
}
|
||||
|
||||
static void update_usb_runtime_state(enum usb_runtime_state state)
|
||||
{
|
||||
if (device_state == state) {
|
||||
if (usb_ctx.bus == state) {
|
||||
return;
|
||||
}
|
||||
|
||||
device_state = state;
|
||||
|
||||
switch (state) {
|
||||
case USB_RUNTIME_STATE_DISCONNECTED:
|
||||
clear_usb_state_flags(USB_STATEF_POWERED |
|
||||
USB_STATEF_ACTIVE |
|
||||
USB_STATEF_SUSPENDED);
|
||||
break;
|
||||
|
||||
case USB_RUNTIME_STATE_POWERED:
|
||||
set_usb_state_flags(USB_STATEF_POWERED);
|
||||
clear_usb_state_flags(USB_STATEF_ACTIVE | USB_STATEF_SUSPENDED);
|
||||
break;
|
||||
|
||||
case USB_RUNTIME_STATE_ACTIVE:
|
||||
set_usb_state_flags(USB_STATEF_POWERED | USB_STATEF_ACTIVE);
|
||||
clear_usb_state_flags(USB_STATEF_SUSPENDED);
|
||||
break;
|
||||
|
||||
case USB_RUNTIME_STATE_SUSPENDED:
|
||||
set_usb_state_flags(USB_STATEF_POWERED | USB_STATEF_SUSPENDED);
|
||||
clear_usb_state_flags(USB_STATEF_ACTIVE);
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
broadcast_usb_state();
|
||||
usb_ctx.bus = state;
|
||||
submit_usb_state(usb_public_state_get());
|
||||
}
|
||||
|
||||
static void update_power_manager_restriction(bool vbus_present)
|
||||
@@ -153,21 +150,23 @@ static void usbd_msg_cb(struct usbd_context *const usbd_ctx,
|
||||
switch (msg->type) {
|
||||
case USBD_MSG_VBUS_READY:
|
||||
update_power_manager_restriction(true);
|
||||
if (!usb_enabled) {
|
||||
|
||||
if (usb_ctx.stack == USB_STACK_READY) {
|
||||
int err = usbd_enable(&blinky_usbd);
|
||||
|
||||
if (err) {
|
||||
LOG_ERR("usbd_enable failed (%d)", err);
|
||||
} else {
|
||||
usb_enabled = true;
|
||||
update_usb_runtime_state(USB_RUNTIME_STATE_POWERED);
|
||||
usb_ctx.stack = USB_STACK_ENABLED;
|
||||
usb_bus_set(USB_BUS_POWERED);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case USBD_MSG_VBUS_REMOVED:
|
||||
update_power_manager_restriction(false);
|
||||
if (usb_enabled) {
|
||||
|
||||
if (usb_ctx.stack == USB_STACK_ENABLED) {
|
||||
int err = usbd_disable(&blinky_usbd);
|
||||
|
||||
if (err) {
|
||||
@@ -175,27 +174,30 @@ static void usbd_msg_cb(struct usbd_context *const usbd_ctx,
|
||||
}
|
||||
}
|
||||
|
||||
usb_enabled = false;
|
||||
update_usb_runtime_state(USB_RUNTIME_STATE_DISCONNECTED);
|
||||
if (usb_ctx.stack != USB_STACK_DISABLED) {
|
||||
usb_ctx.stack = USB_STACK_READY;
|
||||
}
|
||||
|
||||
usb_bus_set(USB_BUS_DISCONNECTED);
|
||||
break;
|
||||
|
||||
case USBD_MSG_CONFIGURATION:
|
||||
if (msg->status) {
|
||||
update_usb_runtime_state(USB_RUNTIME_STATE_ACTIVE);
|
||||
} else if (usb_enabled) {
|
||||
update_usb_runtime_state(USB_RUNTIME_STATE_POWERED);
|
||||
usb_bus_set(USB_BUS_ACTIVE);
|
||||
} else if (usb_ctx.stack == USB_STACK_ENABLED) {
|
||||
usb_bus_set(USB_BUS_POWERED);
|
||||
}
|
||||
break;
|
||||
|
||||
case USBD_MSG_SUSPEND:
|
||||
if (usb_enabled) {
|
||||
update_usb_runtime_state(USB_RUNTIME_STATE_SUSPENDED);
|
||||
if (usb_ctx.stack == USB_STACK_ENABLED) {
|
||||
usb_bus_set(USB_BUS_SUSPENDED);
|
||||
}
|
||||
break;
|
||||
|
||||
case USBD_MSG_RESUME:
|
||||
if (usb_enabled) {
|
||||
update_usb_runtime_state(USB_RUNTIME_STATE_ACTIVE);
|
||||
if (usb_ctx.stack == USB_STACK_ENABLED) {
|
||||
usb_bus_set(USB_BUS_ACTIVE);
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -204,10 +206,29 @@ static void usbd_msg_cb(struct usbd_context *const usbd_ctx,
|
||||
}
|
||||
}
|
||||
|
||||
static int usb_stack_init(void)
|
||||
static int usb_stack_init_once(void)
|
||||
{
|
||||
int err;
|
||||
|
||||
if (usb_stack_was_initialized()) {
|
||||
if (usb_ctx.stack == USB_STACK_DISABLED) {
|
||||
usb_ctx.stack = USB_STACK_READY;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
STRUCT_SECTION_FOREACH(usb_function_hook, hook) {
|
||||
if (hook->pre_stack_init == NULL) {
|
||||
continue;
|
||||
}
|
||||
|
||||
err = hook->pre_stack_init();
|
||||
if (err) {
|
||||
LOG_ERR("USB function hook %s failed (%d)", hook->name, err);
|
||||
return err;
|
||||
}
|
||||
}
|
||||
|
||||
err = usbd_msg_register_cb(&blinky_usbd, usbd_msg_cb);
|
||||
if (err) {
|
||||
LOG_ERR("usbd_msg_register_cb failed (%d)", err);
|
||||
@@ -226,8 +247,7 @@ static int usb_stack_init(void)
|
||||
return err;
|
||||
}
|
||||
|
||||
usbd_initialized = true;
|
||||
set_usb_state_flags(USB_STATEF_STACK_READY);
|
||||
usb_ctx.stack = USB_STACK_READY;
|
||||
|
||||
if (!usbd_can_detect_vbus(&blinky_usbd)) {
|
||||
err = usbd_enable(&blinky_usbd);
|
||||
@@ -236,34 +256,67 @@ static int usb_stack_init(void)
|
||||
return err;
|
||||
}
|
||||
|
||||
usb_enabled = true;
|
||||
update_usb_runtime_state(USB_RUNTIME_STATE_POWERED);
|
||||
usb_ctx.stack = USB_STACK_ENABLED;
|
||||
usb_bus_set(USB_BUS_POWERED);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int try_start_usb_stack(void)
|
||||
static int usb_stack_enable_if_needed(void)
|
||||
{
|
||||
if (usbd_initialized) {
|
||||
int err;
|
||||
|
||||
err = usb_stack_init_once();
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
if ((usb_ctx.stack == USB_STACK_READY) &&
|
||||
(!usbd_can_detect_vbus(&blinky_usbd) ||
|
||||
(usb_ctx.bus != USB_BUS_DISCONNECTED))) {
|
||||
err = usbd_enable(&blinky_usbd);
|
||||
if (err) {
|
||||
LOG_ERR("usbd_enable failed (%d)", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
usb_ctx.stack = USB_STACK_ENABLED;
|
||||
|
||||
if (usb_ctx.bus == USB_BUS_DISCONNECTED) {
|
||||
usb_bus_set(USB_BUS_POWERED);
|
||||
} else {
|
||||
submit_usb_state(usb_public_state_get());
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
if ((usb_state_flags & USB_REQUIRED_READY_MASK) != USB_REQUIRED_READY_MASK) {
|
||||
set_usb_state_flags(USB_STATEF_PREPARE);
|
||||
return 0;
|
||||
submit_usb_state(usb_public_state_get());
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int usb_stack_disable_if_needed(void)
|
||||
{
|
||||
if (usb_ctx.stack == USB_STACK_ENABLED) {
|
||||
int err = usbd_disable(&blinky_usbd);
|
||||
|
||||
if (err) {
|
||||
LOG_ERR("usbd_disable failed (%d)", err);
|
||||
return err;
|
||||
}
|
||||
}
|
||||
|
||||
clear_usb_state_flags(USB_STATEF_PREPARE);
|
||||
return usb_stack_init();
|
||||
usb_ctx.stack = USB_STACK_DISABLED;
|
||||
update_power_manager_restriction(false);
|
||||
submit_usb_state(usb_public_state_get());
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int module_init(void)
|
||||
{
|
||||
device_state = USB_RUNTIME_STATE_DISCONNECTED;
|
||||
usb_enabled = false;
|
||||
usbd_initialized = false;
|
||||
usb_state_flags = 0U;
|
||||
usb_ctx.stack = USB_STACK_UNINITIALIZED;
|
||||
usb_ctx.bus = USB_BUS_DISCONNECTED;
|
||||
update_power_manager_restriction(false);
|
||||
|
||||
return 0;
|
||||
@@ -276,12 +329,8 @@ static int module_start(void)
|
||||
}
|
||||
|
||||
running = true;
|
||||
set_usb_state_flags(USB_STATEF_STACK_RUNNING);
|
||||
|
||||
int err = try_start_usb_stack();
|
||||
|
||||
broadcast_usb_state();
|
||||
return err;
|
||||
submit_usb_state(usb_public_state_get());
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void module_pause(void)
|
||||
@@ -291,8 +340,7 @@ static void module_pause(void)
|
||||
}
|
||||
|
||||
running = false;
|
||||
clear_usb_state_flags(USB_STATEF_STACK_RUNNING);
|
||||
broadcast_usb_state();
|
||||
submit_usb_state(usb_public_state_get());
|
||||
}
|
||||
|
||||
static bool handle_module_state_event(const struct module_state_event *event)
|
||||
@@ -317,7 +365,7 @@ static bool handle_module_state_event(const struct module_state_event *event)
|
||||
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
} else if (usbd_initialized) {
|
||||
} else if (usb_stack_was_initialized()) {
|
||||
module_set_state(MODULE_STATE_READY);
|
||||
}
|
||||
}
|
||||
@@ -325,32 +373,42 @@ static bool handle_module_state_event(const struct module_state_event *event)
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool handle_usb_state_event(const struct usb_state_event *event)
|
||||
static bool handle_module_suspend_req_event(
|
||||
const struct module_suspend_req_event *event)
|
||||
{
|
||||
if ((event->op == USB_STATE_EVENT_OP_SNAPSHOT) ||
|
||||
(event->sink_module_id != MODULE_ID(MODULE))) {
|
||||
if ((event->sink_module_id != MODULE_ID(MODULE)) || !running) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (event->op == USB_STATE_EVENT_OP_SET_BITS) {
|
||||
usb_state_flags |= event->flags;
|
||||
} else if (event->op == USB_STATE_EVENT_OP_CLEAR_BITS) {
|
||||
usb_state_flags &= ~event->flags;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
int err = usb_stack_disable_if_needed();
|
||||
|
||||
int err = running ? try_start_usb_stack() : 0;
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
return false;
|
||||
}
|
||||
|
||||
broadcast_usb_state();
|
||||
module_set_state(MODULE_STATE_READY);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (usbd_initialized) {
|
||||
static bool handle_module_resume_req_event(
|
||||
const struct module_resume_req_event *event)
|
||||
{
|
||||
if ((event->sink_module_id != MODULE_ID(MODULE)) || !running) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int err = usb_stack_enable_if_needed();
|
||||
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (usb_stack_was_initialized()) {
|
||||
module_set_state(MODULE_STATE_READY);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -360,8 +418,14 @@ static bool app_event_handler(const struct app_event_header *aeh)
|
||||
return handle_module_state_event(cast_module_state_event(aeh));
|
||||
}
|
||||
|
||||
if (is_usb_state_event(aeh)) {
|
||||
return handle_usb_state_event(cast_usb_state_event(aeh));
|
||||
if (is_module_suspend_req_event(aeh)) {
|
||||
return handle_module_suspend_req_event(
|
||||
cast_module_suspend_req_event(aeh));
|
||||
}
|
||||
|
||||
if (is_module_resume_req_event(aeh)) {
|
||||
return handle_module_resume_req_event(
|
||||
cast_module_resume_req_event(aeh));
|
||||
}
|
||||
|
||||
if (is_power_down_event(aeh)) {
|
||||
@@ -379,7 +443,7 @@ static bool app_event_handler(const struct app_event_header *aeh)
|
||||
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
} else if (usbd_initialized) {
|
||||
} else if (usb_stack_was_initialized()) {
|
||||
module_set_state(MODULE_STATE_READY);
|
||||
}
|
||||
}
|
||||
@@ -392,6 +456,7 @@ static bool app_event_handler(const struct app_event_header *aeh)
|
||||
|
||||
APP_EVENT_LISTENER(MODULE, app_event_handler);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, module_state_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, usb_state_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, module_suspend_req_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, module_resume_req_event);
|
||||
APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, wake_up_event);
|
||||
|
||||
Reference in New Issue
Block a user