feat(usb): 引入统一的USB状态事件系统

重构USB事件管理,将原有的多个专用事件(usb_device_state_event、
usb_function_ready_event、usb_prepare_event)合并为统一的
usb_state_event。新的事件系统采用位标志方式管理USB状态,
提供更灵活的状态跟踪机制。

BREAKING CHANGE: 移除了旧的USB相关事件类型,需要更新依赖这些
事件的模块代码。
This commit is contained in:
2026-04-15 09:30:40 +08:00
parent 78a6dc212d
commit c4b205b8a1
13 changed files with 301 additions and 306 deletions

View File

@@ -18,10 +18,7 @@
#include "usb_cdc_rx_event.h"
#include "usb_cdc_tx_event.h"
#include "usb_function_ready_event.h"
#include "usb_prepare_event.h"
#include "usb_device_module.h"
#include "usb_device_state_event.h"
#include "usb_state_event.h"
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
@@ -46,6 +43,13 @@ static bool usb_function_prepared;
static bool dtr_ready;
static bool rx_enabled;
static bool is_usb_owner_snapshot(const struct usb_state_event *event)
{
return (event->op == USB_STATE_EVENT_OP_SNAPSHOT) &&
(event->src_module_id == MODULE_ID(usb_device_module)) &&
(event->sink_module_id == NULL);
}
static void reset_ring_buffers(void)
{
unsigned int key = irq_lock();
@@ -276,22 +280,21 @@ static void module_pause(void)
running = false;
}
static bool handle_usb_prepare_event(const struct usb_prepare_event *event)
static bool handle_usb_state_event(const struct usb_state_event *event)
{
ARG_UNUSED(event);
if (!running || usb_function_prepared) {
if (!is_usb_owner_snapshot(event)) {
return false;
}
usb_function_prepared = true;
submit_usb_function_ready_event(USB_FUNCTION_CDC_ACM);
return false;
}
if (running && !usb_function_prepared &&
((event->flags & USB_STATEF_PREPARE) != 0U)) {
usb_function_prepared = true;
submit_usb_state_set(MODULE_ID(MODULE),
MODULE_ID(usb_device_module),
USB_STATEF_CDC_READY);
}
static bool handle_usb_device_state_event(const struct usb_device_state_event *event)
{
bool new_usb_active = (event->state == USB_DEVICE_STATE_ACTIVE);
bool new_usb_active = (event->flags & USB_STATEF_ACTIVE) != 0U;
if (new_usb_active == usb_active) {
return false;
@@ -336,12 +339,8 @@ static bool handle_usb_cdc_tx_event(const struct usb_cdc_tx_event *event)
static bool app_event_handler(const struct app_event_header *aeh)
{
if (is_usb_device_state_event(aeh)) {
return handle_usb_device_state_event(cast_usb_device_state_event(aeh));
}
if (is_usb_prepare_event(aeh)) {
return handle_usb_prepare_event(cast_usb_prepare_event(aeh));
if (is_usb_state_event(aeh)) {
return handle_usb_state_event(cast_usb_state_event(aeh));
}
if (is_usb_cdc_tx_event(aeh)) {
@@ -403,8 +402,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_prepare_event);
APP_EVENT_SUBSCRIBE(MODULE, usb_device_state_event);
APP_EVENT_SUBSCRIBE(MODULE, usb_state_event);
APP_EVENT_SUBSCRIBE(MODULE, usb_cdc_tx_event);
APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event);
APP_EVENT_SUBSCRIBE(MODULE, wake_up_event);