feat(usb): 添加USB CDC功能模块支持
- 在CMakeLists.txt中添加usb_cdc_module、usb_cdc_test_module和 usb_device_module源文件 - 添加usb_cdc_rx_event、usb_cdc_tx_event、usb_device_state_event、 usb_function_ready_event和usb_prepare_event事件定义 - 实现USB CDC串口通信功能,包括接收和发送数据处理 - 添加USB设备状态管理,支持连接、断开、激活等状态变化 - 配置设备树中的USB端点数量以支持CDC ACM功能 - 创建USB设备模块用于管理USB堆栈初始化和状态监控 - 添加USB功能就绪事件以协调不同USB功能的准备状态
This commit is contained in:
164
src/usb_cdc_test_module.c
Normal file
164
src/usb_cdc_test_module.c
Normal file
@@ -0,0 +1,164 @@
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <app_event_manager.h>
|
||||
|
||||
#define MODULE usb_cdc_test_module
|
||||
#include <caf/events/module_state_event.h>
|
||||
#include <caf/events/power_event.h>
|
||||
|
||||
#include <zephyr/kernel.h>
|
||||
#include <zephyr/logging/log.h>
|
||||
|
||||
#include "usb_cdc_tx_event.h"
|
||||
#include "usb_device_state_event.h"
|
||||
|
||||
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||
|
||||
#define USB_CDC_TEST_PERIOD K_SECONDS(1)
|
||||
|
||||
static const uint8_t hello_message[] = "hello\r\n";
|
||||
|
||||
static struct k_work_delayable hello_work;
|
||||
static bool initialized;
|
||||
static bool running;
|
||||
static bool usb_active;
|
||||
|
||||
static void submit_hello_message(void)
|
||||
{
|
||||
struct usb_cdc_tx_event *event =
|
||||
new_usb_cdc_tx_event(sizeof(hello_message) - 1U);
|
||||
|
||||
memcpy(event->dyndata.data, hello_message, sizeof(hello_message) - 1U);
|
||||
APP_EVENT_SUBMIT(event);
|
||||
}
|
||||
|
||||
static void hello_work_handler(struct k_work *work)
|
||||
{
|
||||
ARG_UNUSED(work);
|
||||
|
||||
if (!running || !usb_active) {
|
||||
return;
|
||||
}
|
||||
|
||||
submit_hello_message();
|
||||
k_work_reschedule(&hello_work, USB_CDC_TEST_PERIOD);
|
||||
}
|
||||
|
||||
static int module_init(void)
|
||||
{
|
||||
k_work_init_delayable(&hello_work, hello_work_handler);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int module_start(void)
|
||||
{
|
||||
if (running) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
running = true;
|
||||
if (usb_active) {
|
||||
k_work_reschedule(&hello_work, USB_CDC_TEST_PERIOD);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void module_pause(void)
|
||||
{
|
||||
if (!running) {
|
||||
return;
|
||||
}
|
||||
|
||||
k_work_cancel_delayable(&hello_work);
|
||||
running = false;
|
||||
}
|
||||
|
||||
static bool handle_usb_device_state_event(const struct usb_device_state_event *event)
|
||||
{
|
||||
bool new_usb_active = (event->state == USB_DEVICE_STATE_ACTIVE);
|
||||
|
||||
if (new_usb_active == usb_active) {
|
||||
return false;
|
||||
}
|
||||
|
||||
usb_active = new_usb_active;
|
||||
|
||||
if (!running) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (usb_active) {
|
||||
k_work_reschedule(&hello_work, USB_CDC_TEST_PERIOD);
|
||||
} else {
|
||||
k_work_cancel_delayable(&hello_work);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
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_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)) {
|
||||
int err;
|
||||
|
||||
if (!initialized) {
|
||||
err = module_init();
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
return false;
|
||||
}
|
||||
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
err = module_start();
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
} else {
|
||||
module_set_state(MODULE_STATE_READY);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
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 {
|
||||
module_set_state(MODULE_STATE_READY);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
APP_EVENT_LISTENER(MODULE, app_event_handler);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, module_state_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, usb_device_state_event);
|
||||
APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, wake_up_event);
|
||||
Reference in New Issue
Block a user