2026-04-11 17:15:11 +08:00
|
|
|
#include <errno.h>
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
|
|
#include <app_event_manager.h>
|
|
|
|
|
|
|
|
|
|
#define MODULE usb_device_module
|
|
|
|
|
#include <caf/events/module_state_event.h>
|
|
|
|
|
#include <caf/events/power_event.h>
|
|
|
|
|
|
|
|
|
|
#include <zephyr/logging/log.h>
|
|
|
|
|
#include <zephyr/usb/usbd.h>
|
|
|
|
|
|
2026-04-11 17:57:00 +08:00
|
|
|
#include <caf/events/power_manager_event.h>
|
|
|
|
|
|
2026-04-15 09:30:40 +08:00
|
|
|
#include "usb_state_event.h"
|
2026-04-11 17:15:11 +08:00
|
|
|
|
|
|
|
|
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
|
|
|
|
|
|
|
|
|
#define USB_DEVICE_VID 0x1915
|
|
|
|
|
#define USB_DEVICE_PID 0x52F0
|
|
|
|
|
#define USB_DEVICE_MANUFACTURER "Atguigu"
|
|
|
|
|
#define USB_DEVICE_PRODUCT "WH Mini Keyboard"
|
2026-04-15 09:30:40 +08:00
|
|
|
#define USB_REQUIRED_READY_MASK (USB_STATEF_HID_READY | USB_STATEF_CDC_READY)
|
2026-04-11 17:15:11 +08:00
|
|
|
|
|
|
|
|
USBD_DEVICE_DEFINE(blinky_usbd, DEVICE_DT_GET(DT_NODELABEL(usbd)),
|
|
|
|
|
USB_DEVICE_VID, USB_DEVICE_PID);
|
|
|
|
|
|
|
|
|
|
USBD_DESC_LANG_DEFINE(blinky_lang);
|
|
|
|
|
USBD_DESC_MANUFACTURER_DEFINE(blinky_mfr, USB_DEVICE_MANUFACTURER);
|
|
|
|
|
USBD_DESC_PRODUCT_DEFINE(blinky_product, USB_DEVICE_PRODUCT);
|
|
|
|
|
|
|
|
|
|
USBD_DESC_CONFIG_DEFINE(blinky_fs_cfg_desc, "FS Configuration");
|
|
|
|
|
USBD_CONFIGURATION_DEFINE(blinky_fs_config, 0, 250, &blinky_fs_cfg_desc);
|
|
|
|
|
|
|
|
|
|
static const char *const class_blocklist[] = {
|
|
|
|
|
NULL,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static bool initialized;
|
|
|
|
|
static bool running;
|
|
|
|
|
static bool usbd_initialized;
|
|
|
|
|
static bool usb_enabled;
|
2026-04-15 09:30:40 +08:00
|
|
|
static uint32_t usb_state_flags;
|
2026-04-11 17:15:11 +08:00
|
|
|
|
2026-04-15 09:30:40 +08:00
|
|
|
enum usb_runtime_state {
|
|
|
|
|
USB_RUNTIME_STATE_DISCONNECTED,
|
|
|
|
|
USB_RUNTIME_STATE_POWERED,
|
|
|
|
|
USB_RUNTIME_STATE_ACTIVE,
|
|
|
|
|
USB_RUNTIME_STATE_SUSPENDED,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static enum usb_runtime_state device_state = USB_RUNTIME_STATE_DISCONNECTED;
|
|
|
|
|
|
|
|
|
|
static void broadcast_usb_state(void)
|
|
|
|
|
{
|
|
|
|
|
submit_usb_state_snapshot(MODULE_ID(MODULE), usb_state_flags);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void set_usb_state_flags(uint32_t mask)
|
|
|
|
|
{
|
|
|
|
|
usb_state_flags |= mask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void clear_usb_state_flags(uint32_t mask)
|
|
|
|
|
{
|
|
|
|
|
usb_state_flags &= ~mask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void update_usb_runtime_state(enum usb_runtime_state state)
|
2026-04-11 17:15:11 +08:00
|
|
|
{
|
|
|
|
|
if (device_state == state) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
device_state = state;
|
2026-04-15 09:30:40 +08:00
|
|
|
|
|
|
|
|
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();
|
2026-04-11 17:15:11 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-11 17:57:00 +08:00
|
|
|
static void update_power_manager_restriction(bool vbus_present)
|
|
|
|
|
{
|
|
|
|
|
power_manager_restrict(MODULE_IDX(MODULE),
|
|
|
|
|
vbus_present ? POWER_MANAGER_LEVEL_ALIVE :
|
|
|
|
|
POWER_MANAGER_LEVEL_SUSPENDED);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-11 17:15:11 +08:00
|
|
|
static int usb_descriptors_init(void)
|
|
|
|
|
{
|
|
|
|
|
int err;
|
|
|
|
|
|
|
|
|
|
err = usbd_add_descriptor(&blinky_usbd, &blinky_lang);
|
|
|
|
|
if (err) {
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = usbd_add_descriptor(&blinky_usbd, &blinky_mfr);
|
|
|
|
|
if (err) {
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = usbd_add_descriptor(&blinky_usbd, &blinky_product);
|
|
|
|
|
if (err) {
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = usbd_add_configuration(&blinky_usbd, USBD_SPEED_FS, &blinky_fs_config);
|
|
|
|
|
if (err) {
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = usbd_register_all_classes(&blinky_usbd, USBD_SPEED_FS, 1, class_blocklist);
|
|
|
|
|
if (err) {
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
usbd_device_set_code_triple(&blinky_usbd, USBD_SPEED_FS, 0, 0, 0);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void usbd_msg_cb(struct usbd_context *const usbd_ctx,
|
|
|
|
|
const struct usbd_msg *const msg)
|
|
|
|
|
{
|
|
|
|
|
ARG_UNUSED(usbd_ctx);
|
|
|
|
|
|
|
|
|
|
switch (msg->type) {
|
|
|
|
|
case USBD_MSG_VBUS_READY:
|
2026-04-11 17:57:00 +08:00
|
|
|
update_power_manager_restriction(true);
|
2026-04-11 17:15:11 +08:00
|
|
|
if (!usb_enabled) {
|
|
|
|
|
int err = usbd_enable(&blinky_usbd);
|
|
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
|
LOG_ERR("usbd_enable failed (%d)", err);
|
|
|
|
|
} else {
|
|
|
|
|
usb_enabled = true;
|
2026-04-15 09:30:40 +08:00
|
|
|
update_usb_runtime_state(USB_RUNTIME_STATE_POWERED);
|
2026-04-11 17:15:11 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case USBD_MSG_VBUS_REMOVED:
|
2026-04-11 17:57:00 +08:00
|
|
|
update_power_manager_restriction(false);
|
2026-04-11 17:15:11 +08:00
|
|
|
if (usb_enabled) {
|
|
|
|
|
int err = usbd_disable(&blinky_usbd);
|
|
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
|
LOG_ERR("usbd_disable failed (%d)", err);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
usb_enabled = false;
|
2026-04-15 09:30:40 +08:00
|
|
|
update_usb_runtime_state(USB_RUNTIME_STATE_DISCONNECTED);
|
2026-04-11 17:15:11 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case USBD_MSG_CONFIGURATION:
|
|
|
|
|
if (msg->status) {
|
2026-04-15 09:30:40 +08:00
|
|
|
update_usb_runtime_state(USB_RUNTIME_STATE_ACTIVE);
|
2026-04-11 17:15:11 +08:00
|
|
|
} else if (usb_enabled) {
|
2026-04-15 09:30:40 +08:00
|
|
|
update_usb_runtime_state(USB_RUNTIME_STATE_POWERED);
|
2026-04-11 17:15:11 +08:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case USBD_MSG_SUSPEND:
|
|
|
|
|
if (usb_enabled) {
|
2026-04-15 09:30:40 +08:00
|
|
|
update_usb_runtime_state(USB_RUNTIME_STATE_SUSPENDED);
|
2026-04-11 17:15:11 +08:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case USBD_MSG_RESUME:
|
|
|
|
|
if (usb_enabled) {
|
2026-04-15 09:30:40 +08:00
|
|
|
update_usb_runtime_state(USB_RUNTIME_STATE_ACTIVE);
|
2026-04-11 17:15:11 +08:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int usb_stack_init(void)
|
|
|
|
|
{
|
|
|
|
|
int err;
|
|
|
|
|
|
|
|
|
|
err = usbd_msg_register_cb(&blinky_usbd, usbd_msg_cb);
|
|
|
|
|
if (err) {
|
|
|
|
|
LOG_ERR("usbd_msg_register_cb failed (%d)", err);
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = usb_descriptors_init();
|
|
|
|
|
if (err) {
|
|
|
|
|
LOG_ERR("usb descriptor init failed (%d)", err);
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = usbd_init(&blinky_usbd);
|
|
|
|
|
if (err) {
|
|
|
|
|
LOG_ERR("usbd_init failed (%d)", err);
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
usbd_initialized = true;
|
2026-04-15 09:30:40 +08:00
|
|
|
set_usb_state_flags(USB_STATEF_STACK_READY);
|
2026-04-11 17:15:11 +08:00
|
|
|
|
|
|
|
|
if (!usbd_can_detect_vbus(&blinky_usbd)) {
|
|
|
|
|
err = usbd_enable(&blinky_usbd);
|
|
|
|
|
if (err) {
|
|
|
|
|
LOG_ERR("usbd_enable failed (%d)", err);
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
usb_enabled = true;
|
2026-04-15 09:30:40 +08:00
|
|
|
update_usb_runtime_state(USB_RUNTIME_STATE_POWERED);
|
2026-04-11 17:15:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-15 09:30:40 +08:00
|
|
|
static int try_start_usb_stack(void)
|
|
|
|
|
{
|
|
|
|
|
if (usbd_initialized) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((usb_state_flags & USB_REQUIRED_READY_MASK) != USB_REQUIRED_READY_MASK) {
|
|
|
|
|
set_usb_state_flags(USB_STATEF_PREPARE);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
clear_usb_state_flags(USB_STATEF_PREPARE);
|
|
|
|
|
return usb_stack_init();
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-11 17:15:11 +08:00
|
|
|
static int module_init(void)
|
|
|
|
|
{
|
2026-04-15 09:30:40 +08:00
|
|
|
device_state = USB_RUNTIME_STATE_DISCONNECTED;
|
2026-04-11 17:15:11 +08:00
|
|
|
usb_enabled = false;
|
|
|
|
|
usbd_initialized = false;
|
2026-04-15 09:30:40 +08:00
|
|
|
usb_state_flags = 0U;
|
2026-04-11 17:57:00 +08:00
|
|
|
update_power_manager_restriction(false);
|
2026-04-11 17:15:11 +08:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int module_start(void)
|
|
|
|
|
{
|
|
|
|
|
if (running) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
running = true;
|
2026-04-15 09:30:40 +08:00
|
|
|
set_usb_state_flags(USB_STATEF_STACK_RUNNING);
|
2026-04-11 17:15:11 +08:00
|
|
|
|
2026-04-15 09:30:40 +08:00
|
|
|
int err = try_start_usb_stack();
|
2026-04-11 17:15:11 +08:00
|
|
|
|
2026-04-15 09:30:40 +08:00
|
|
|
broadcast_usb_state();
|
|
|
|
|
return err;
|
2026-04-11 17:15:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void module_pause(void)
|
|
|
|
|
{
|
|
|
|
|
if (!running) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
running = false;
|
2026-04-15 09:30:40 +08:00
|
|
|
clear_usb_state_flags(USB_STATEF_STACK_RUNNING);
|
|
|
|
|
broadcast_usb_state();
|
2026-04-11 17:15:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool handle_module_state_event(const struct module_state_event *event)
|
|
|
|
|
{
|
|
|
|
|
if (!check_state(event, MODULE_ID(main), MODULE_STATE_READY)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!initialized) {
|
|
|
|
|
int err = module_init();
|
|
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
|
module_set_state(MODULE_STATE_ERROR);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
initialized = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!running) {
|
|
|
|
|
int err = module_start();
|
|
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
|
module_set_state(MODULE_STATE_ERROR);
|
|
|
|
|
} else if (usbd_initialized) {
|
|
|
|
|
module_set_state(MODULE_STATE_READY);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-15 09:30:40 +08:00
|
|
|
static bool handle_usb_state_event(const struct usb_state_event *event)
|
2026-04-11 17:15:11 +08:00
|
|
|
{
|
2026-04-15 09:30:40 +08:00
|
|
|
if ((event->op == USB_STATE_EVENT_OP_SNAPSHOT) ||
|
|
|
|
|
(event->sink_module_id != MODULE_ID(MODULE))) {
|
2026-04-11 17:15:11 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-15 09:30:40 +08:00
|
|
|
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 {
|
2026-04-11 17:15:11 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-15 09:30:40 +08:00
|
|
|
int err = running ? try_start_usb_stack() : 0;
|
2026-04-11 17:15:11 +08:00
|
|
|
if (err) {
|
|
|
|
|
module_set_state(MODULE_STATE_ERROR);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-15 09:30:40 +08:00
|
|
|
broadcast_usb_state();
|
|
|
|
|
|
|
|
|
|
if (usbd_initialized) {
|
|
|
|
|
module_set_state(MODULE_STATE_READY);
|
|
|
|
|
}
|
2026-04-11 17:15:11 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool app_event_handler(const struct app_event_header *aeh)
|
|
|
|
|
{
|
|
|
|
|
if (is_module_state_event(aeh)) {
|
|
|
|
|
return handle_module_state_event(cast_module_state_event(aeh));
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-15 09:30:40 +08:00
|
|
|
if (is_usb_state_event(aeh)) {
|
|
|
|
|
return handle_usb_state_event(cast_usb_state_event(aeh));
|
2026-04-11 17:15:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 if (usbd_initialized) {
|
|
|
|
|
module_set_state(MODULE_STATE_READY);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
APP_EVENT_LISTENER(MODULE, app_event_handler);
|
|
|
|
|
APP_EVENT_SUBSCRIBE(MODULE, module_state_event);
|
2026-04-15 09:30:40 +08:00
|
|
|
APP_EVENT_SUBSCRIBE(MODULE, usb_state_event);
|
2026-04-11 17:15:11 +08:00
|
|
|
APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event);
|
|
|
|
|
APP_EVENT_SUBSCRIBE(MODULE, wake_up_event);
|