feat(usb): 添加USB HID模块支持键盘和消费设备
- 添加USB HID模块实现键盘和消费控制设备功能 - 在CMakeLists.txt中添加usb_hid_module.c和相关事件文件 - 添加HID LED事件和设置协议事件定义及实现 - 配置设备树添加HID键盘和消费者设备节点 - 启用USB设备堆栈配置选项 - 修改键盘核心模块以处理协议切换事件 - 修复键映射中keypad enter的位置错误 - 注释掉电池模块中的调试日志输出
This commit is contained in:
@@ -14,7 +14,10 @@ target_sources(app PRIVATE
|
|||||||
src/main.c
|
src/main.c
|
||||||
src/battery_module.c
|
src/battery_module.c
|
||||||
src/keyboard_core_module.c
|
src/keyboard_core_module.c
|
||||||
|
src/usb_hid_module.c
|
||||||
|
src/events/hid_led_event.c
|
||||||
src/mode_switch_module.c
|
src/mode_switch_module.c
|
||||||
src/events/keyboard_hid_report_event.c
|
src/events/keyboard_hid_report_event.c
|
||||||
src/events/mode_switch_event.c
|
src/events/mode_switch_event.c
|
||||||
|
src/events/set_protocol_event.c
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -17,6 +17,24 @@
|
|||||||
led0 = &myled0;
|
led0 = &myled0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
hid_kbd: hid_kbd {
|
||||||
|
compatible = "zephyr,hid-device";
|
||||||
|
label = "HID_KBD";
|
||||||
|
protocol-code = "keyboard";
|
||||||
|
in-report-size = <29>;
|
||||||
|
out-report-size = <1>;
|
||||||
|
in-polling-period-us = <1000>;
|
||||||
|
out-polling-period-us = <1000>;
|
||||||
|
};
|
||||||
|
|
||||||
|
hid_consumer: hid_consumer {
|
||||||
|
compatible = "zephyr,hid-device";
|
||||||
|
label = "HID_CONSUMER";
|
||||||
|
protocol-code = "none";
|
||||||
|
in-report-size = <2>;
|
||||||
|
in-polling-period-us = <1000>;
|
||||||
|
};
|
||||||
|
|
||||||
leds {
|
leds {
|
||||||
compatible = "gpio-leds";
|
compatible = "gpio-leds";
|
||||||
myled0: led_0 {
|
myled0: led_0 {
|
||||||
@@ -128,3 +146,12 @@
|
|||||||
&gpiote {
|
&gpiote {
|
||||||
status = "okay";
|
status = "okay";
|
||||||
};
|
};
|
||||||
|
|
||||||
|
&usbd {
|
||||||
|
status = "okay";
|
||||||
|
num-bidir-endpoints = <0>;
|
||||||
|
num-in-endpoints = <2>;
|
||||||
|
num-out-endpoints = <1>;
|
||||||
|
num-isoin-endpoints = <0>;
|
||||||
|
num-isoout-endpoints = <0>;
|
||||||
|
};
|
||||||
|
|||||||
22
inc/events/hid_led_event.h
Normal file
22
inc/events/hid_led_event.h
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
#ifndef BLINKY_HID_LED_EVENT_H_
|
||||||
|
#define BLINKY_HID_LED_EVENT_H_
|
||||||
|
|
||||||
|
#include <app_event_manager.h>
|
||||||
|
#include <app_event_manager_profiler_tracer.h>
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
struct hid_led_event {
|
||||||
|
struct app_event_header header;
|
||||||
|
uint8_t led_bm;
|
||||||
|
};
|
||||||
|
|
||||||
|
APP_EVENT_TYPE_DECLARE(hid_led_event);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /* BLINKY_HID_LED_EVENT_H_ */
|
||||||
24
inc/events/set_protocol_event.h
Normal file
24
inc/events/set_protocol_event.h
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
#ifndef BLINKY_SET_PROTOCOL_EVENT_H_
|
||||||
|
#define BLINKY_SET_PROTOCOL_EVENT_H_
|
||||||
|
|
||||||
|
#include <app_event_manager.h>
|
||||||
|
#include <app_event_manager_profiler_tracer.h>
|
||||||
|
|
||||||
|
#include "keyboard_core.h"
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
struct set_protocol_event {
|
||||||
|
struct app_event_header header;
|
||||||
|
enum keyboard_protocol_mode protocol_mode;
|
||||||
|
};
|
||||||
|
|
||||||
|
APP_EVENT_TYPE_DECLARE(set_protocol_event);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /* BLINKY_SET_PROTOCOL_EVENT_H_ */
|
||||||
4
prj.conf
4
prj.conf
@@ -12,6 +12,10 @@ CONFIG_HEAP_MEM_POOL_SIZE=2048
|
|||||||
CONFIG_LOG=y
|
CONFIG_LOG=y
|
||||||
CONFIG_ASSERT=y
|
CONFIG_ASSERT=y
|
||||||
|
|
||||||
|
# USB HID next stack
|
||||||
|
CONFIG_USB_DEVICE_STACK_NEXT=y
|
||||||
|
CONFIG_USBD_HID_SUPPORT=y
|
||||||
|
|
||||||
# Power manager
|
# Power manager
|
||||||
CONFIG_CAF_POWER_MANAGER=y
|
CONFIG_CAF_POWER_MANAGER=y
|
||||||
CONFIG_CAF_POWER_MANAGER_TIMEOUT=120
|
CONFIG_CAF_POWER_MANAGER_TIMEOUT=120
|
||||||
|
|||||||
@@ -84,8 +84,8 @@ static void battery_sample_fn(struct k_work *work)
|
|||||||
}
|
}
|
||||||
|
|
||||||
voltage_mv = sensor_value_to_mv(&voltage);
|
voltage_mv = sensor_value_to_mv(&voltage);
|
||||||
LOG_INF("Battery: %d mV, charging=%d, full=%d",
|
// LOG_INF("Battery: %d mV, charging=%d, full=%d",
|
||||||
voltage_mv, pmic_status.charging, pmic_status.full);
|
// voltage_mv, pmic_status.charging, pmic_status.full);
|
||||||
|
|
||||||
reschedule:
|
reschedule:
|
||||||
if (running) {
|
if (running) {
|
||||||
|
|||||||
27
src/events/hid_led_event.c
Normal file
27
src/events/hid_led_event.c
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
#include "hid_led_event.h"
|
||||||
|
|
||||||
|
static void log_hid_led_event(const struct app_event_header *aeh)
|
||||||
|
{
|
||||||
|
const struct hid_led_event *event = cast_hid_led_event(aeh);
|
||||||
|
|
||||||
|
APP_EVENT_MANAGER_LOG(aeh, "led_bm:0x%02x", event->led_bm);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void profile_hid_led_event(struct log_event_buf *buf,
|
||||||
|
const struct app_event_header *aeh)
|
||||||
|
{
|
||||||
|
const struct hid_led_event *event = cast_hid_led_event(aeh);
|
||||||
|
|
||||||
|
nrf_profiler_log_encode_uint8(buf, event->led_bm);
|
||||||
|
}
|
||||||
|
|
||||||
|
APP_EVENT_INFO_DEFINE(hid_led_event,
|
||||||
|
ENCODE(NRF_PROFILER_ARG_U8),
|
||||||
|
ENCODE("led_bm"),
|
||||||
|
profile_hid_led_event);
|
||||||
|
|
||||||
|
APP_EVENT_TYPE_DEFINE(hid_led_event,
|
||||||
|
log_hid_led_event,
|
||||||
|
&hid_led_event_info,
|
||||||
|
APP_EVENT_FLAGS_CREATE(
|
||||||
|
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));
|
||||||
40
src/events/set_protocol_event.c
Normal file
40
src/events/set_protocol_event.c
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
#include "set_protocol_event.h"
|
||||||
|
|
||||||
|
static const char *protocol_mode_name(enum keyboard_protocol_mode protocol_mode)
|
||||||
|
{
|
||||||
|
switch (protocol_mode) {
|
||||||
|
case KEYBOARD_PROTOCOL_MODE_BOOT:
|
||||||
|
return "boot";
|
||||||
|
case KEYBOARD_PROTOCOL_MODE_REPORT:
|
||||||
|
return "report";
|
||||||
|
default:
|
||||||
|
return "?";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void log_set_protocol_event(const struct app_event_header *aeh)
|
||||||
|
{
|
||||||
|
const struct set_protocol_event *event = cast_set_protocol_event(aeh);
|
||||||
|
|
||||||
|
APP_EVENT_MANAGER_LOG(aeh, "protocol:%s",
|
||||||
|
protocol_mode_name(event->protocol_mode));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void profile_set_protocol_event(struct log_event_buf *buf,
|
||||||
|
const struct app_event_header *aeh)
|
||||||
|
{
|
||||||
|
const struct set_protocol_event *event = cast_set_protocol_event(aeh);
|
||||||
|
|
||||||
|
nrf_profiler_log_encode_uint8(buf, event->protocol_mode);
|
||||||
|
}
|
||||||
|
|
||||||
|
APP_EVENT_INFO_DEFINE(set_protocol_event,
|
||||||
|
ENCODE(NRF_PROFILER_ARG_U8),
|
||||||
|
ENCODE("protocol_mode"),
|
||||||
|
profile_set_protocol_event);
|
||||||
|
|
||||||
|
APP_EVENT_TYPE_DEFINE(set_protocol_event,
|
||||||
|
log_set_protocol_event,
|
||||||
|
&set_protocol_event_info,
|
||||||
|
APP_EVENT_FLAGS_CREATE(
|
||||||
|
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));
|
||||||
@@ -17,6 +17,7 @@
|
|||||||
#include "keyboard_core.h"
|
#include "keyboard_core.h"
|
||||||
#include "keyboard_hid_report_event.h"
|
#include "keyboard_hid_report_event.h"
|
||||||
#include "mode_switch_event.h"
|
#include "mode_switch_event.h"
|
||||||
|
#include "set_protocol_event.h"
|
||||||
|
|
||||||
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||||
|
|
||||||
@@ -66,7 +67,7 @@ static const struct keymap_entry keymap[] = {
|
|||||||
{ KEY_ID(2, 2), KEY_USAGE_TYPE_KEYBOARD, 0x0061 }, /* keypad 9 */
|
{ KEY_ID(2, 2), KEY_USAGE_TYPE_KEYBOARD, 0x0061 }, /* keypad 9 */
|
||||||
{ KEY_ID(2, 3), KEY_USAGE_TYPE_KEYBOARD, 0x005E }, /* keypad 6 */
|
{ KEY_ID(2, 3), KEY_USAGE_TYPE_KEYBOARD, 0x005E }, /* keypad 6 */
|
||||||
{ KEY_ID(2, 4), KEY_USAGE_TYPE_KEYBOARD, 0x005B }, /* keypad 3 */
|
{ KEY_ID(2, 4), KEY_USAGE_TYPE_KEYBOARD, 0x005B }, /* keypad 3 */
|
||||||
{ KEY_ID(2, 5), KEY_USAGE_TYPE_KEYBOARD, 0x0058 }, /* keypad enter */
|
{ KEY_ID(3, 5), KEY_USAGE_TYPE_KEYBOARD, 0x0058 }, /* keypad enter */
|
||||||
{ KEY_ID(3, 0), KEY_USAGE_TYPE_CONSUMER, KEYBOARD_CONSUMER_CTRL_MUTE },
|
{ KEY_ID(3, 0), KEY_USAGE_TYPE_CONSUMER, KEYBOARD_CONSUMER_CTRL_MUTE },
|
||||||
{ KEY_ID(3, 1), KEY_USAGE_TYPE_KEYBOARD, 0x0056 }, /* keypad - */
|
{ KEY_ID(3, 1), KEY_USAGE_TYPE_KEYBOARD, 0x0056 }, /* keypad - */
|
||||||
{ KEY_ID(3, 3), KEY_USAGE_TYPE_KEYBOARD, 0x0057 }, /* keypad + */
|
{ KEY_ID(3, 3), KEY_USAGE_TYPE_KEYBOARD, 0x0057 }, /* keypad + */
|
||||||
@@ -421,6 +422,20 @@ static bool app_event_handler(const struct app_event_header *aeh)
|
|||||||
return handle_button_event(cast_button_event(aeh));
|
return handle_button_event(cast_button_event(aeh));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (is_set_protocol_event(aeh)) {
|
||||||
|
const struct set_protocol_event *event = cast_set_protocol_event(aeh);
|
||||||
|
|
||||||
|
if (protocol_mode != event->protocol_mode) {
|
||||||
|
protocol_mode = event->protocol_mode;
|
||||||
|
|
||||||
|
if (running && mode_valid && (current_mode == MODE_SWITCH_USB)) {
|
||||||
|
emit_keys_report(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
if (is_mode_switch_event(aeh)) {
|
if (is_mode_switch_event(aeh)) {
|
||||||
return handle_mode_switch_event(cast_mode_switch_event(aeh));
|
return handle_mode_switch_event(cast_mode_switch_event(aeh));
|
||||||
}
|
}
|
||||||
@@ -481,6 +496,7 @@ static bool app_event_handler(const struct app_event_header *aeh)
|
|||||||
|
|
||||||
APP_EVENT_LISTENER(MODULE, app_event_handler);
|
APP_EVENT_LISTENER(MODULE, app_event_handler);
|
||||||
APP_EVENT_SUBSCRIBE(MODULE, button_event);
|
APP_EVENT_SUBSCRIBE(MODULE, button_event);
|
||||||
|
APP_EVENT_SUBSCRIBE(MODULE, set_protocol_event);
|
||||||
APP_EVENT_SUBSCRIBE(MODULE, mode_switch_event);
|
APP_EVENT_SUBSCRIBE(MODULE, mode_switch_event);
|
||||||
APP_EVENT_SUBSCRIBE(MODULE, module_state_event);
|
APP_EVENT_SUBSCRIBE(MODULE, module_state_event);
|
||||||
APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event);
|
APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event);
|
||||||
|
|||||||
652
src/usb_hid_module.c
Normal file
652
src/usb_hid_module.c
Normal file
@@ -0,0 +1,652 @@
|
|||||||
|
#include <errno.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include <app_event_manager.h>
|
||||||
|
|
||||||
|
#define MODULE usb_hid_module
|
||||||
|
#include <caf/events/module_state_event.h>
|
||||||
|
#include <caf/events/power_event.h>
|
||||||
|
|
||||||
|
#include <zephyr/device.h>
|
||||||
|
#include <zephyr/drivers/usb/udc_buf.h>
|
||||||
|
#include <zephyr/logging/log.h>
|
||||||
|
#include <zephyr/sys/byteorder.h>
|
||||||
|
#include <zephyr/sys/util.h>
|
||||||
|
#include <zephyr/usb/class/usbd_hid.h>
|
||||||
|
#include <zephyr/usb/usbd.h>
|
||||||
|
|
||||||
|
#include "hid_led_event.h"
|
||||||
|
#include "keyboard_core.h"
|
||||||
|
#include "keyboard_hid_report_event.h"
|
||||||
|
#include "set_protocol_event.h"
|
||||||
|
|
||||||
|
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||||
|
|
||||||
|
#define USB_HID_VID 0x1915
|
||||||
|
#define USB_HID_PID 0x52F0
|
||||||
|
#define USB_HID_MANUFACTURER "blinky"
|
||||||
|
#define USB_HID_PRODUCT "Mini Keyboard"
|
||||||
|
#define USB_HID_POLLING_US 1000U
|
||||||
|
|
||||||
|
#define KBD_LED_REPORT_SIZE 1U
|
||||||
|
|
||||||
|
enum usb_hid_interface {
|
||||||
|
USB_HID_INTERFACE_KEYBOARD,
|
||||||
|
USB_HID_INTERFACE_CONSUMER,
|
||||||
|
USB_HID_INTERFACE_COUNT,
|
||||||
|
};
|
||||||
|
|
||||||
|
struct usb_hid_interface_state {
|
||||||
|
const struct device *dev;
|
||||||
|
bool ready;
|
||||||
|
};
|
||||||
|
|
||||||
|
static const uint8_t keyboard_report_desc[] = {
|
||||||
|
0x05, 0x01, /* Usage Page (Generic Desktop) */
|
||||||
|
0x09, 0x06, /* Usage (Keyboard) */
|
||||||
|
0xA1, 0x01, /* Collection (Application) */
|
||||||
|
0x05, 0x07, /* Usage Page (Keyboard/Keypad) */
|
||||||
|
0x19, 0xE0, /* Usage Minimum (0xE0) */
|
||||||
|
0x29, 0xE7, /* Usage Maximum (0xE7) */
|
||||||
|
0x15, 0x00, /* Logical Minimum (0) */
|
||||||
|
0x25, 0x01, /* Logical Maximum (1) */
|
||||||
|
0x75, 0x01, /* Report Size (1) */
|
||||||
|
0x95, 0x08, /* Report Count (8) */
|
||||||
|
0x81, 0x02, /* Input (Data,Var,Abs) */
|
||||||
|
0x05, 0x07, /* Usage Page (Keyboard/Keypad) */
|
||||||
|
0x19, 0x00, /* Usage Minimum (0x00) */
|
||||||
|
0x2A, 0xDF, 0x00, /* Usage Maximum (0x00DF) */
|
||||||
|
0x15, 0x00, /* Logical Minimum (0) */
|
||||||
|
0x25, 0x01, /* Logical Maximum (1) */
|
||||||
|
0x75, 0x01, /* Report Size (1) */
|
||||||
|
0x96, 0xE0, 0x00, /* Report Count (224) */
|
||||||
|
0x81, 0x02, /* Input (Data,Var,Abs) */
|
||||||
|
0x05, 0x08, /* Usage Page (LEDs) */
|
||||||
|
0x19, 0x01, /* Usage Minimum (1) */
|
||||||
|
0x29, 0x05, /* Usage Maximum (5) */
|
||||||
|
0x15, 0x00, /* Logical Minimum (0) */
|
||||||
|
0x25, 0x01, /* Logical Maximum (1) */
|
||||||
|
0x75, 0x01, /* Report Size (1) */
|
||||||
|
0x95, 0x05, /* Report Count (5) */
|
||||||
|
0x91, 0x02, /* Output (Data,Var,Abs) */
|
||||||
|
0x75, 0x03, /* Report Size (3) */
|
||||||
|
0x95, 0x01, /* Report Count (1) */
|
||||||
|
0x91, 0x01, /* Output (Const,Array,Abs) */
|
||||||
|
0xC0 /* End Collection */
|
||||||
|
};
|
||||||
|
|
||||||
|
static const uint8_t consumer_report_desc[] = {
|
||||||
|
0x05, 0x0C, /* Usage Page (Consumer) */
|
||||||
|
0x09, 0x01, /* Usage (Consumer Control) */
|
||||||
|
0xA1, 0x01, /* Collection (Application) */
|
||||||
|
0x15, 0x00, /* Logical Minimum (0) */
|
||||||
|
0x26, 0xFF, 0x03, /* Logical Maximum (1023) */
|
||||||
|
0x19, 0x00, /* Usage Minimum (0) */
|
||||||
|
0x2A, 0xFF, 0x03, /* Usage Maximum (1023) */
|
||||||
|
0x75, 0x10, /* Report Size (16) */
|
||||||
|
0x95, 0x01, /* Report Count (1) */
|
||||||
|
0x81, 0x00, /* Input (Data,Array,Abs) */
|
||||||
|
0xC0 /* End Collection */
|
||||||
|
};
|
||||||
|
|
||||||
|
static struct usb_hid_interface_state hid_ifaces[USB_HID_INTERFACE_COUNT] = {
|
||||||
|
[USB_HID_INTERFACE_KEYBOARD] = {
|
||||||
|
.dev = DEVICE_DT_GET(DT_NODELABEL(hid_kbd)),
|
||||||
|
},
|
||||||
|
[USB_HID_INTERFACE_CONSUMER] = {
|
||||||
|
.dev = DEVICE_DT_GET(DT_NODELABEL(hid_consumer)),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static enum keyboard_protocol_mode keyboard_protocol_mode =
|
||||||
|
KEYBOARD_PROTOCOL_MODE_REPORT;
|
||||||
|
static bool initialized;
|
||||||
|
static bool running;
|
||||||
|
static bool usb_enabled;
|
||||||
|
static bool keyboard_report_in_flight;
|
||||||
|
static bool consumer_report_in_flight;
|
||||||
|
|
||||||
|
UDC_STATIC_BUF_DEFINE(keyboard_tx_buf, KEYBOARD_NKRO_REPORT_SIZE);
|
||||||
|
UDC_STATIC_BUF_DEFINE(consumer_tx_buf, KEYBOARD_CONSUMER_REPORT_SIZE);
|
||||||
|
|
||||||
|
USBD_DEVICE_DEFINE(blinky_usbd, DEVICE_DT_GET(DT_NODELABEL(usbd)),
|
||||||
|
USB_HID_VID, USB_HID_PID);
|
||||||
|
|
||||||
|
USBD_DESC_LANG_DEFINE(blinky_lang);
|
||||||
|
USBD_DESC_MANUFACTURER_DEFINE(blinky_mfr, USB_HID_MANUFACTURER);
|
||||||
|
USBD_DESC_PRODUCT_DEFINE(blinky_product, USB_HID_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 struct usb_hid_interface_state *iface_from_dev(const struct device *dev)
|
||||||
|
{
|
||||||
|
for (size_t i = 0; i < ARRAY_SIZE(hid_ifaces); i++) {
|
||||||
|
if (hid_ifaces[i].dev == dev) {
|
||||||
|
return &hid_ifaces[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
static enum keyboard_protocol_mode usb_proto_to_keyboard_proto(uint8_t proto)
|
||||||
|
{
|
||||||
|
return (proto == HID_PROTOCOL_BOOT) ? KEYBOARD_PROTOCOL_MODE_BOOT
|
||||||
|
: KEYBOARD_PROTOCOL_MODE_REPORT;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void submit_set_protocol_event(enum keyboard_protocol_mode protocol_mode)
|
||||||
|
{
|
||||||
|
struct set_protocol_event *event = new_set_protocol_event();
|
||||||
|
|
||||||
|
event->protocol_mode = protocol_mode;
|
||||||
|
APP_EVENT_SUBMIT(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void submit_hid_led_event(uint8_t led_bm)
|
||||||
|
{
|
||||||
|
struct hid_led_event *event = new_hid_led_event();
|
||||||
|
|
||||||
|
event->led_bm = led_bm;
|
||||||
|
APP_EVENT_SUBMIT(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void keyboard_iface_ready(const struct device *dev, const bool ready)
|
||||||
|
{
|
||||||
|
struct usb_hid_interface_state *iface = iface_from_dev(dev);
|
||||||
|
|
||||||
|
if (!iface) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
iface->ready = ready;
|
||||||
|
if (!ready) {
|
||||||
|
keyboard_report_in_flight = false;
|
||||||
|
}
|
||||||
|
LOG_INF("%s interface %s",
|
||||||
|
dev->name, ready ? "ready" : "not ready");
|
||||||
|
}
|
||||||
|
|
||||||
|
static int keyboard_get_report(const struct device *dev,
|
||||||
|
const uint8_t type, const uint8_t id,
|
||||||
|
const uint16_t len, uint8_t *const buf)
|
||||||
|
{
|
||||||
|
ARG_UNUSED(dev);
|
||||||
|
ARG_UNUSED(type);
|
||||||
|
ARG_UNUSED(id);
|
||||||
|
ARG_UNUSED(len);
|
||||||
|
ARG_UNUSED(buf);
|
||||||
|
|
||||||
|
return -ENOTSUP;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int keyboard_set_report(const struct device *dev,
|
||||||
|
const uint8_t type, const uint8_t id,
|
||||||
|
const uint16_t len, const uint8_t *const buf)
|
||||||
|
{
|
||||||
|
ARG_UNUSED(dev);
|
||||||
|
ARG_UNUSED(type);
|
||||||
|
ARG_UNUSED(id);
|
||||||
|
ARG_UNUSED(len);
|
||||||
|
ARG_UNUSED(buf);
|
||||||
|
|
||||||
|
return -ENOTSUP;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void keyboard_set_idle(const struct device *dev,
|
||||||
|
const uint8_t id, const uint32_t duration)
|
||||||
|
{
|
||||||
|
ARG_UNUSED(dev);
|
||||||
|
ARG_UNUSED(id);
|
||||||
|
ARG_UNUSED(duration);
|
||||||
|
}
|
||||||
|
|
||||||
|
static uint32_t keyboard_get_idle(const struct device *dev, const uint8_t id)
|
||||||
|
{
|
||||||
|
ARG_UNUSED(dev);
|
||||||
|
ARG_UNUSED(id);
|
||||||
|
|
||||||
|
return 0U;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void keyboard_set_protocol(const struct device *dev, const uint8_t proto)
|
||||||
|
{
|
||||||
|
ARG_UNUSED(dev);
|
||||||
|
|
||||||
|
enum keyboard_protocol_mode new_mode = usb_proto_to_keyboard_proto(proto);
|
||||||
|
|
||||||
|
if (keyboard_protocol_mode == new_mode) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
keyboard_protocol_mode = new_mode;
|
||||||
|
LOG_INF("USB keyboard protocol -> %s",
|
||||||
|
(new_mode == KEYBOARD_PROTOCOL_MODE_BOOT) ? "boot" : "report");
|
||||||
|
submit_set_protocol_event(new_mode);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void keyboard_input_report_done(const struct device *dev,
|
||||||
|
const uint8_t *const report)
|
||||||
|
{
|
||||||
|
ARG_UNUSED(report);
|
||||||
|
|
||||||
|
keyboard_report_in_flight = false;
|
||||||
|
LOG_DBG("USB keyboard report sent by %s", dev->name);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void keyboard_output_report(const struct device *dev,
|
||||||
|
const uint16_t len,
|
||||||
|
const uint8_t *const buf)
|
||||||
|
{
|
||||||
|
ARG_UNUSED(dev);
|
||||||
|
|
||||||
|
if ((len < KBD_LED_REPORT_SIZE) || (buf == NULL)) {
|
||||||
|
LOG_WRN("Invalid keyboard output report");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
submit_hid_led_event(buf[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
static const struct hid_device_ops keyboard_ops = {
|
||||||
|
.iface_ready = keyboard_iface_ready,
|
||||||
|
.get_report = keyboard_get_report,
|
||||||
|
.set_report = keyboard_set_report,
|
||||||
|
.set_idle = keyboard_set_idle,
|
||||||
|
.get_idle = keyboard_get_idle,
|
||||||
|
.set_protocol = keyboard_set_protocol,
|
||||||
|
.input_report_done = keyboard_input_report_done,
|
||||||
|
.output_report = keyboard_output_report,
|
||||||
|
};
|
||||||
|
|
||||||
|
static void consumer_iface_ready(const struct device *dev, const bool ready)
|
||||||
|
{
|
||||||
|
struct usb_hid_interface_state *iface = iface_from_dev(dev);
|
||||||
|
|
||||||
|
if (!iface) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
iface->ready = ready;
|
||||||
|
if (!ready) {
|
||||||
|
consumer_report_in_flight = false;
|
||||||
|
}
|
||||||
|
LOG_INF("%s interface %s",
|
||||||
|
dev->name, ready ? "ready" : "not ready");
|
||||||
|
}
|
||||||
|
|
||||||
|
static int consumer_get_report(const struct device *dev,
|
||||||
|
const uint8_t type, const uint8_t id,
|
||||||
|
const uint16_t len, uint8_t *const buf)
|
||||||
|
{
|
||||||
|
ARG_UNUSED(dev);
|
||||||
|
ARG_UNUSED(type);
|
||||||
|
ARG_UNUSED(id);
|
||||||
|
ARG_UNUSED(len);
|
||||||
|
ARG_UNUSED(buf);
|
||||||
|
|
||||||
|
return -ENOTSUP;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int consumer_set_report(const struct device *dev,
|
||||||
|
const uint8_t type, const uint8_t id,
|
||||||
|
const uint16_t len, const uint8_t *const buf)
|
||||||
|
{
|
||||||
|
ARG_UNUSED(dev);
|
||||||
|
ARG_UNUSED(type);
|
||||||
|
ARG_UNUSED(id);
|
||||||
|
ARG_UNUSED(len);
|
||||||
|
ARG_UNUSED(buf);
|
||||||
|
|
||||||
|
return -ENOTSUP;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void consumer_set_idle(const struct device *dev,
|
||||||
|
const uint8_t id, const uint32_t duration)
|
||||||
|
{
|
||||||
|
ARG_UNUSED(dev);
|
||||||
|
ARG_UNUSED(id);
|
||||||
|
ARG_UNUSED(duration);
|
||||||
|
}
|
||||||
|
|
||||||
|
static uint32_t consumer_get_idle(const struct device *dev, const uint8_t id)
|
||||||
|
{
|
||||||
|
ARG_UNUSED(dev);
|
||||||
|
ARG_UNUSED(id);
|
||||||
|
|
||||||
|
return 0U;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void consumer_set_protocol(const struct device *dev, const uint8_t proto)
|
||||||
|
{
|
||||||
|
ARG_UNUSED(dev);
|
||||||
|
ARG_UNUSED(proto);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void consumer_input_report_done(const struct device *dev,
|
||||||
|
const uint8_t *const report)
|
||||||
|
{
|
||||||
|
ARG_UNUSED(report);
|
||||||
|
|
||||||
|
consumer_report_in_flight = false;
|
||||||
|
LOG_DBG("USB consumer report sent by %s", dev->name);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void consumer_output_report(const struct device *dev,
|
||||||
|
const uint16_t len,
|
||||||
|
const uint8_t *const buf)
|
||||||
|
{
|
||||||
|
ARG_UNUSED(dev);
|
||||||
|
ARG_UNUSED(len);
|
||||||
|
ARG_UNUSED(buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
static const struct hid_device_ops consumer_ops = {
|
||||||
|
.iface_ready = consumer_iface_ready,
|
||||||
|
.get_report = consumer_get_report,
|
||||||
|
.set_report = consumer_set_report,
|
||||||
|
.set_idle = consumer_set_idle,
|
||||||
|
.get_idle = consumer_get_idle,
|
||||||
|
.set_protocol = consumer_set_protocol,
|
||||||
|
.input_report_done = consumer_input_report_done,
|
||||||
|
.output_report = consumer_output_report,
|
||||||
|
};
|
||||||
|
|
||||||
|
static void usbd_msg_cb(struct usbd_context *const usbd_ctx,
|
||||||
|
const struct usbd_msg *const msg)
|
||||||
|
{
|
||||||
|
ARG_UNUSED(usbd_ctx);
|
||||||
|
|
||||||
|
if (msg->type == USBD_MSG_VBUS_READY) {
|
||||||
|
if (!usb_enabled) {
|
||||||
|
int err = usbd_enable(&blinky_usbd);
|
||||||
|
|
||||||
|
if (err) {
|
||||||
|
LOG_ERR("usbd_enable failed (%d)", err);
|
||||||
|
} else {
|
||||||
|
usb_enabled = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (msg->type == USBD_MSG_VBUS_REMOVED) {
|
||||||
|
if (usb_enabled) {
|
||||||
|
int err = usbd_disable(&blinky_usbd);
|
||||||
|
|
||||||
|
if (err) {
|
||||||
|
LOG_ERR("usbd_disable failed (%d)", err);
|
||||||
|
} else {
|
||||||
|
usb_enabled = false;
|
||||||
|
for (size_t i = 0; i < ARRAY_SIZE(hid_ifaces); i++) {
|
||||||
|
hid_ifaces[i].ready = false;
|
||||||
|
}
|
||||||
|
keyboard_report_in_flight = false;
|
||||||
|
consumer_report_in_flight = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 int usb_hid_register_devices(void)
|
||||||
|
{
|
||||||
|
int err;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < ARRAY_SIZE(hid_ifaces); i++) {
|
||||||
|
if (!device_is_ready(hid_ifaces[i].dev)) {
|
||||||
|
LOG_ERR("HID device %s not ready", hid_ifaces[i].dev->name);
|
||||||
|
return -ENODEV;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
err = hid_device_register(hid_ifaces[USB_HID_INTERFACE_KEYBOARD].dev,
|
||||||
|
keyboard_report_desc,
|
||||||
|
sizeof(keyboard_report_desc),
|
||||||
|
&keyboard_ops);
|
||||||
|
if (err) {
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
|
err = hid_device_register(hid_ifaces[USB_HID_INTERFACE_CONSUMER].dev,
|
||||||
|
consumer_report_desc,
|
||||||
|
sizeof(consumer_report_desc),
|
||||||
|
&consumer_ops);
|
||||||
|
if (err) {
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int module_init(void)
|
||||||
|
{
|
||||||
|
int err;
|
||||||
|
|
||||||
|
err = usb_hid_register_devices();
|
||||||
|
if (err) {
|
||||||
|
LOG_ERR("hid_device_register failed (%d)", err);
|
||||||
|
return 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int module_start(void)
|
||||||
|
{
|
||||||
|
if (running) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
running = true;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void module_pause(void)
|
||||||
|
{
|
||||||
|
running = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool handle_keyboard_hid_report_event(const struct keyboard_hid_report_event *event)
|
||||||
|
{
|
||||||
|
int err;
|
||||||
|
|
||||||
|
if (!running || (event->mode != MODE_SWITCH_USB)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (event->report_type == KEYBOARD_REPORT_TYPE_KEYS) {
|
||||||
|
if (event->protocol_mode != keyboard_protocol_mode) {
|
||||||
|
LOG_WRN("Drop USB keys report due to protocol mismatch");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!hid_ifaces[USB_HID_INTERFACE_KEYBOARD].ready) {
|
||||||
|
LOG_DBG("USB keyboard interface not ready");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (keyboard_report_in_flight) {
|
||||||
|
LOG_WRN("Drop USB keyboard report while previous report is in flight");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
memcpy(keyboard_tx_buf, event->dyndata.data, event->dyndata.size);
|
||||||
|
|
||||||
|
err = hid_device_submit_report(
|
||||||
|
hid_ifaces[USB_HID_INTERFACE_KEYBOARD].dev,
|
||||||
|
(uint16_t)event->dyndata.size,
|
||||||
|
keyboard_tx_buf);
|
||||||
|
if (err) {
|
||||||
|
LOG_WRN("USB keyboard report submit failed (%d)", err);
|
||||||
|
} else {
|
||||||
|
keyboard_report_in_flight = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (event->report_type == KEYBOARD_REPORT_TYPE_CONSUMER) {
|
||||||
|
if (!hid_ifaces[USB_HID_INTERFACE_CONSUMER].ready) {
|
||||||
|
LOG_DBG("USB consumer interface not ready");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (consumer_report_in_flight) {
|
||||||
|
LOG_WRN("Drop USB consumer report while previous report is in flight");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
memcpy(consumer_tx_buf, event->dyndata.data, event->dyndata.size);
|
||||||
|
|
||||||
|
err = hid_device_submit_report(
|
||||||
|
hid_ifaces[USB_HID_INTERFACE_CONSUMER].dev,
|
||||||
|
(uint16_t)event->dyndata.size,
|
||||||
|
consumer_tx_buf);
|
||||||
|
if (err) {
|
||||||
|
LOG_WRN("USB consumer report submit failed (%d)", err);
|
||||||
|
} else {
|
||||||
|
consumer_report_in_flight = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool app_event_handler(const struct app_event_header *aeh)
|
||||||
|
{
|
||||||
|
if (is_keyboard_hid_report_event(aeh)) {
|
||||||
|
return handle_keyboard_hid_report_event(
|
||||||
|
cast_keyboard_hid_report_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;
|
||||||
|
}
|
||||||
|
|
||||||
|
__ASSERT_NO_MSG(false);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
APP_EVENT_LISTENER(MODULE, app_event_handler);
|
||||||
|
APP_EVENT_SUBSCRIBE(MODULE, module_state_event);
|
||||||
|
APP_EVENT_SUBSCRIBE(MODULE, keyboard_hid_report_event);
|
||||||
|
APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event);
|
||||||
|
APP_EVENT_SUBSCRIBE(MODULE, wake_up_event);
|
||||||
Reference in New Issue
Block a user