stm32f0_cherryusb: add multimedia and system keys

This commit is contained in:
wangyz1997 2024-05-18 12:54:47 +08:00
parent 6415b32d5a
commit 5336983b26
5 changed files with 195 additions and 177 deletions

View File

@ -158,6 +158,10 @@ int usbd_ep_open(uint8_t busid, const struct usb_endpoint_descriptor *ep)
g_fsdev_udc.out_ep[ep_idx].ep_type = USB_GET_ENDPOINT_TYPE(ep->bmAttributes);
g_fsdev_udc.out_ep[ep_idx].ep_enable = true;
if (g_fsdev_udc.out_ep[ep_idx].ep_mps > g_fsdev_udc.out_ep[ep_idx].ep_pma_buf_len) {
/* PMA address must be halfword-aligned */
if (g_fsdev_udc.pma_offset % 2) {
g_fsdev_udc.pma_offset ++;
}
if (g_fsdev_udc.pma_offset + g_fsdev_udc.out_ep[ep_idx].ep_mps > CONFIG_USB_FSDEV_RAM_SIZE) {
USB_LOG_ERR("Ep pma %02x overflow\r\n", ep->bEndpointAddress);
return -1;
@ -176,6 +180,10 @@ int usbd_ep_open(uint8_t busid, const struct usb_endpoint_descriptor *ep)
g_fsdev_udc.in_ep[ep_idx].ep_type = USB_GET_ENDPOINT_TYPE(ep->bmAttributes);
g_fsdev_udc.in_ep[ep_idx].ep_enable = true;
if (g_fsdev_udc.in_ep[ep_idx].ep_mps > g_fsdev_udc.in_ep[ep_idx].ep_pma_buf_len) {
/* PMA address must be halfword-aligned */
if (g_fsdev_udc.pma_offset % 2) {
g_fsdev_udc.pma_offset ++;
}
if (g_fsdev_udc.pma_offset + g_fsdev_udc.in_ep[ep_idx].ep_mps > CONFIG_USB_FSDEV_RAM_SIZE) {
USB_LOG_ERR("Ep pma %02x overflow\r\n", ep->bEndpointAddress);
return -1;

View File

@ -3,41 +3,34 @@
#include <stdint.h>
#define HID_KEYBOARD_LEFT_CTRL (1 << 0)
#define HID_KEYBOARD_LEFT_SHIFT (1 << 1)
#define HID_KEYBOARD_LEFT_ALT (1 << 2)
#define HID_KEYBOARD_LEFT_GUI (1 << 3)
#define HID_KEYBOARD_RIGHT_CTRL (1 << 4)
#define HID_KEYBOARD_RIGHT_SHIFT (1 << 5)
#define HID_KEYBOARD_RIGHT_ALT (1 << 6)
#define HID_KEYBOARD_RIGHT_GUI (1 << 7)
#define HID_MOUSE_LEFT_BUTTON (1 << 0)
#define HID_MOUSE_RIGHT_BUTTON (1 << 1)
#define HID_MOUSE_MIDDLE_BUTTON (1 << 2)
typedef struct {
uint8_t modifier_keys;
uint8_t __reserved;
uint8_t key1;
uint8_t key2;
uint8_t key3;
uint8_t key4;
uint8_t key5;
uint8_t key6;
} hid_keyboard_report_t;
typedef struct {
uint8_t buttons;
int8_t x;
int8_t y;
int8_t wheel;
} hid_mouse_report_t;
#define HID_CONSUMER_USAGE_NEXTTRACK 0x00B5
#define HID_CONSUMER_USAGE_PREVTRACK 0x00B6
#define HID_CONSUMER_USAGE_STOP 0x00B7
#define HID_CONSUMER_USAGE_PLAYPAUSE 0x00CD
#define HID_CONSUMER_USAGE_MUTE 0x00E2
#define HID_CONSUMER_USAGE_VOLUMEINC 0x00E9
#define HID_CONSUMER_USAGE_VOLUMEDEC 0x00EA
#define HID_CONSUMER_USAGE_MEDIASELECT 0x0181
#define HID_CONSUMER_USAGE_MAIL 0x018A
#define HID_CONSUMER_USAGE_CALCULATOR 0x0192
#define HID_CONSUMER_USAGE_MYCOMPUTER 0x0194
#define HID_CONSUMER_USAGE_WWWSEARCH 0x0221
#define HID_CONSUMER_USAGE_WEBHOME 0x0223
#define HID_CONSUMER_USAGE_WWWBACK 0x0224
#define HID_CONSUMER_USAGE_WWWFORWARD 0x0225
#define HID_CONSUMER_USAGE_WWWSTOP 0x0226
#define HID_CONSUMER_USAGE_WWWREFRESH 0x0227
#define HID_CONSUMER_USAGE_WWWFAVORITES 0x022A
#define HID_SYSTEM_USAGE_POWER 0x01
#define HID_SYSTEM_USAGE_SLEEP 0x02
#define HID_SYSTEM_USAGE_WAKEUP 0x04
void hid_keyboard_mouse_init(uint8_t busid, uint32_t reg_base);
void hid_keyboard_send(uint8_t busid, hid_keyboard_report_t *keyboard_report);
void hid_mouse_send(uint8_t busid, hid_mouse_report_t *mouse_report);
void hid_keyboard_key_send(uint8_t busid, uint8_t modifier_keys, uint8_t key);
void hid_consumer_control_key_send(uint8_t busid, uint16_t consumer_control_key);
void hid_system_control_key_send(uint8_t busid, uint16_t system_control_key);
void hid_mouse_send(uint8_t busid, uint8_t buttons, int8_t x, int8_t y, int8_t wheel);
#endif

View File

@ -1,4 +1,5 @@
#include "hid_keyboard_mouse.h"
#include <string.h>
#include "usbd_core.h"
#include "usbd_hid.h"
#include "main.h"
@ -6,18 +7,18 @@
#define HID_STATE_IDLE 0
#define HID_STATE_BUSY 1
#define USBD_VID 0xFFFF
#define USBD_PID 0xFFFF
#define USBD_VID 0x0483
#define USBD_PID 0x5750
#define USBD_MAX_POWER_MA 100
#define USBD_LANGID_STRING 1033
#define HID_KM_IN_EP 0x81
#define HID_KM_IN_EP_SIZE 16
#define HID_KM_IN_EP_INTERVAL 10
#define HID_KM_IN_EP_SIZE 9
#define HID_KM_IN_EP_INTERVAL 1
#define HID_KM_OUT_EP 0x02
#define HID_KM_OUT_EP_SIZE 4
#define HID_KM_OUT_EP_INTERVAL 10
#define HID_KM_OUT_EP_SIZE 2
#define HID_KM_OUT_EP_INTERVAL 1
#define HID_CONFIG_DESCRIPTOR_SIZE 41
#define HID_KM_REPORT_DESC_SIZE 187
@ -27,8 +28,8 @@ static const uint8_t hid_descriptor[] = {
USB_DEVICE_DESCRIPTOR_INIT(USB_1_1, 0x00, 0x00, 0x00, USBD_VID, USBD_PID, 0x0002, 0x01),
/* 配置(Configuration)描述符 所有设备至少有一个 */
USB_CONFIG_DESCRIPTOR_INIT(HID_CONFIG_DESCRIPTOR_SIZE, 0x02, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER_MA),
/* Configuration Descriptor Length = 09 */
USB_CONFIG_DESCRIPTOR_INIT(HID_CONFIG_DESCRIPTOR_SIZE, 0x01, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER_MA),
/* 截至此处的配置描述符长度 = 09 */
/* 接口(Interface)描述符 键盘 */
0x09, /* bLength: Descriptor size */
@ -49,8 +50,8 @@ static const uint8_t hid_descriptor[] = {
0x00, /* bCountryCode: Hardware target country */
0x01, /* bNumDescriptors: Number of HID class descriptors to follow */
0x22, /* bDescriptorType */
WBVAL(HID_KEYBOARD_REPORT_DESC_SIZE), /* wItemLength: Total length of Report descriptor */
/* 27 */
WBVAL(HID_KM_REPORT_DESC_SIZE), /* wItemLength: Total length of Report descriptor */
/* 截至此处的配置描述符长度 = 27 */
/* 端点(Endpoint)描述符 键盘IN */
0x07, /* bLength: Endpoint Descriptor size */
@ -107,7 +108,7 @@ static const uint8_t hid_descriptor[] = {
#ifdef CONFIG_USB_HS
/* 设备限定(Device Qualifier)描述符 同时支持全速与高速的设备必须有 */
0x0a,
0x0A,
USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
0x00,
0x02,
@ -118,91 +119,109 @@ static const uint8_t hid_descriptor[] = {
0x01,
0x00,
#endif
/* 空描述符 */
0x00
};
static const uint8_t hid_keyboard_report_desc[HID_KEYBOARD_REPORT_DESC_SIZE] = {
0x05, 0x01, // USAGE_PAGE (Generic Desktop)
0x09, 0x06, // USAGE (Keyboard)
0xa1, 0x01, // COLLECTION (Application)
0x05, 0x07, // USAGE_PAGE (Keyboard)
0x19, 0xe0, // USAGE_MINIMUM (Keyboard LeftControl)
0x29, 0xe7, // USAGE_MAXIMUM (Keyboard Right GUI)
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)
0x95, 0x01, // REPORT_COUNT (1)
0x75, 0x08, // REPORT_SIZE (8)
0x81, 0x03, // INPUT (Cnst,Var,Abs)
0x95, 0x05, // REPORT_COUNT (5)
0x75, 0x01, // REPORT_SIZE (1)
0x05, 0x08, // USAGE_PAGE (LEDs)
0x19, 0x01, // USAGE_MINIMUM (Num Lock)
0x29, 0x05, // USAGE_MAXIMUM (Kana)
0x91, 0x02, // OUTPUT (Data,Var,Abs)
0x95, 0x01, // REPORT_COUNT (1)
0x75, 0x03, // REPORT_SIZE (3)
0x91, 0x03, // OUTPUT (Cnst,Var,Abs)
0x95, 0x06, // REPORT_COUNT (6)
0x75, 0x08, // REPORT_SIZE (8)
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x25, 0xFF, // LOGICAL_MAXIMUM (255)
0x05, 0x07, // USAGE_PAGE (Keyboard)
0x19, 0x00, // USAGE_MINIMUM (Reserved (no event indicated))
0x29, 0x65, // USAGE_MAXIMUM (Keyboard Application)
0x81, 0x00, // INPUT (Data,Ary,Abs)
0xc0 // END_COLLECTION
};
static const uint8_t hid_km_report_desc[HID_KM_REPORT_DESC_SIZE] = {
0x05, 0x01, // Usage Page (Generic Desktop Ctrls)
0x09, 0x06, // Usage (Keyboard)
0xA1, 0x01, // Collection (Application)
0x85, 0x01, // Report ID (1)
0x05, 0x07, // Usage Page (Kbrd/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,No Wrap,Linear,Preferred State,No Null Position)
0x75, 0x08, // Report Size (8)
0x95, 0x01, // Report Count (1)
0x81, 0x01, // Input (Const,Array,Abs,No Wrap,Linear,Preferred State,No Null Position)
0x19, 0x00, // Usage Minimum (0x00)
0x29, 0x65, // Usage Maximum (0x65)
0x15, 0x00, // Logical Minimum (0)
0x25, 0x65, // Logical Maximum (101)
0x75, 0x08, // Report Size (8)
0x95, 0x06, // Report Count (6)
0x81, 0x00, // Input (Data,Array,Abs,No Wrap,Linear,Preferred State,No Null Position)
0x05, 0x08, // Usage Page (LEDs)
0x19, 0x01, // Usage Minimum (Num Lock)
0x29, 0x03, // Usage Maximum (Scroll Lock)
0x15, 0x00, // Logical Minimum (0)
0x25, 0x01, // Logical Maximum (1)
0x75, 0x01, // Report Size (1)
0x95, 0x03, // Report Count (3)
0x91, 0x02, // Output (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position,Non-volatile)
0x75, 0x05, // Report Size (5)
0x95, 0x01, // Report Count (1)
0x91, 0x01, // Output (Const,Array,Abs,No Wrap,Linear,Preferred State,No Null Position,Non-volatile)
0xC0, // End Collection
static const uint8_t hid_mouse_report_desc[HID_MOUSE_REPORT_DESC_SIZE] = {
0x05, 0x01, // USAGE_PAGE (Generic Desktop)
0x09, 0x02, // USAGE (Mouse)
0xA1, 0x01, // COLLECTION (Application)
0x09, 0x01, // USAGE (Pointer)
0x05, 0x0C, // Usage Page (Consumer)
0x09, 0x01, // Usage (Consumer Control)
0xA1, 0x01, // Collection (Application)
0x85, 0x02, // Report ID (2)
0x19, 0x00, // Usage Minimum (Unassigned)
0x2A, 0x3C, 0x02, // Usage Maximum (AC Format)
0x15, 0x00, // Logical Minimum (0)
0x26, 0x3C, 0x02, // Logical Maximum (572)
0x95, 0x01, // Report Count (1)
0x75, 0x10, // Report Size (16)
0x81, 0x00, // Input (Data,Array,Abs,No Wrap,Linear,Preferred State,No Null Position)
0x75, 0x08, // Report Size (8)
0x95, 0x06, // Report Count (6)
0x81, 0x01, // Input (Const,Array,Abs,No Wrap,Linear,Preferred State,No Null Position)
0xC0, // End Collection
0xA1, 0x00, // COLLECTION (Physical)
0x05, 0x09, // USAGE_PAGE (Button)
0x19, 0x01, // USAGE_MINIMUM (Button 1)
0x29, 0x03, // USAGE_MAXIMUM (Button 3)
0x05, 0x01, // Usage Page (Generic Desktop Ctrls)
0x09, 0x80, // Usage (Sys Control)
0xA1, 0x01, // Collection (Application)
0x85, 0x03, // Report ID (3)
0x19, 0x81, // Usage Minimum (Sys Power Down)
0x29, 0x83, // Usage Maximum (Sys Wake Up)
0x15, 0x00, // Logical Minimum (0)
0x25, 0x01, // Logical Maximum (1)
0x75, 0x01, // Report Size (1)
0x95, 0x03, // Report Count (3)
0x81, 0x02, // Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
0x95, 0x05, // Report Count (5)
0x81, 0x01, // Input (Const,Array,Abs,No Wrap,Linear,Preferred State,No Null Position)
0x75, 0x08, // Report Size (8)
0x95, 0x07, // Report Count (7)
0x81, 0x01, // Input (Const,Array,Abs,No Wrap,Linear,Preferred State,No Null Position)
0xC0, // End Collection
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x25, 0x01, // LOGICAL_MAXIMUM (1)
0x95, 0x03, // REPORT_COUNT (3)
0x75, 0x01, // REPORT_SIZE (1)
0x81, 0x02, // INPUT (Data,Var,Abs)
0x95, 0x01, // REPORT_COUNT (1)
0x75, 0x05, // REPORT_SIZE (5)
0x81, 0x01, // INPUT (Cnst,Var,Abs)
0x05, 0x01, // USAGE_PAGE (Generic Desktop)
0x09, 0x30, // USAGE (X)
0x09, 0x31, // USAGE (Y)
0x09, 0x38,
0x15, 0x81, // LOGICAL_MINIMUM (-127)
0x25, 0x7F, // LOGICAL_MAXIMUM (127)
0x75, 0x08, // REPORT_SIZE (8)
0x95, 0x03, // REPORT_COUNT (2)
0x81, 0x06, // INPUT (Data,Var,Rel)
0xC0, 0x09,
0x3c, 0x05,
0xff, 0x09,
0x01, 0x15,
0x00, 0x25,
0x01, 0x75,
0x01, 0x95,
0x02, 0xb1,
0x22, 0x75,
0x06, 0x95,
0x01, 0xb1,
0x01, 0xc0 // END_COLLECTION
0x05, 0x01, // Usage Page (Generic Desktop Ctrls)
0x05, 0x01, // Usage Page (Generic Desktop Ctrls)
0x09, 0x02, // Usage (Mouse)
0xA1, 0x01, // Collection (Application)
0x85, 0x04, // Report ID (4)
0x09, 0x01, // Usage (Pointer)
0xA1, 0x00, // Collection (Physical)
0x05, 0x09, // Usage Page (Button)
0x19, 0x01, // Usage Minimum (0x01)
0x29, 0x03, // Usage Maximum (0x03)
0x15, 0x00, // Logical Minimum (0)
0x25, 0x01, // Logical Maximum (1)
0x75, 0x01, // Report Size (1)
0x95, 0x03, // Report Count (3)
0x81, 0x02, // Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
0x75, 0x05, // Report Size (5)
0x95, 0x01, // Report Count (1)
0x81, 0x01, // Input (Const,Array,Abs,No Wrap,Linear,Preferred State,No Null Position)
0x05, 0x01, // Usage Page (Generic Desktop Ctrls)
0x09, 0x30, // Usage (X)
0x09, 0x31, // Usage (Y)
0x09, 0x38, // Usage (Wheel)
0x15, 0x81, // Logical Minimum (-127)
0x25, 0x7F, // Logical Maximum (127)
0x75, 0x08, // Report Size (8)
0x95, 0x03, // Report Count (3)
0x81, 0x06, // Input (Data,Var,Rel,No Wrap,Linear,Preferred State,No Null Position)
0xC0, // End Collection
0xC0, // End Collection
};
static volatile uint8_t hid_km_state = HID_STATE_BUSY;
@ -212,6 +231,7 @@ USB_MEM_ALIGNX uint8_t read_buffer[HID_KM_OUT_EP_SIZE];
static void usbd_hid_keyboard_in_callback(uint8_t busid, uint8_t ep, uint32_t nbytes)
{
hid_km_state = HID_STATE_IDLE;
HAL_GPIO_TogglePin(LED4_GPIO_Port, LED4_Pin);
}
static void usbd_hid_keyboard_out_callback(uint8_t busid, uint8_t ep, uint32_t nbytes)
@ -263,17 +283,16 @@ static struct usbd_endpoint hid_keyboard_out_ep = {
void hid_keyboard_mouse_init(uint8_t busid, uint32_t reg_base)
{
static struct usbd_interface intf0;
static struct usbd_interface intf1;
usbd_desc_register(busid, hid_descriptor);
usbd_add_interface(busid, usbd_hid_init_intf(busid, &intf0, hid_km_report_desc, HID_KM_REPORT_DESC_SIZE)); //添加键盘鼠标INT IN端点
usbd_add_endpoint(busid, &hid_keyboard_in_ep);
usbd_add_endpoint(busid, &hid_keyboard_out_ep);
usbd_add_interface(busid, usbd_hid_init_intf(busid, &intf0, hid_km_report_desc, HID_KM_REPORT_DESC_SIZE));
usbd_add_endpoint(busid, &hid_keyboard_in_ep); //添加键盘鼠标INT IN端点
usbd_add_endpoint(busid, &hid_keyboard_out_ep); //添加键盘鼠标INT OUT端点
usbd_initialize(busid, reg_base, usbd_event_handler);
}
void hid_keyboard_send(uint8_t busid, hid_keyboard_report_t *keyboard_report)
void hid_keyboard_key_send(uint8_t busid, uint8_t modifier_keys, uint8_t key)
{
static USB_MEM_ALIGNX uint8_t report[HID_KM_IN_EP_SIZE];
memset(report, 0, HID_KM_IN_EP_SIZE);
@ -320,7 +339,7 @@ void hid_consumer_control_key_send(uint8_t busid, uint16_t consumer_control_key)
}
}
void hid_mouse_send(uint8_t busid, hid_mouse_report_t *mouse_report)
void hid_system_control_key_send(uint8_t busid, uint16_t system_control_key)
{
static USB_MEM_ALIGNX uint8_t report[HID_KM_IN_EP_SIZE];
memset(report, 0, HID_KM_IN_EP_SIZE);

View File

@ -116,41 +116,39 @@ int main(void)
/* Infinite loop */
/* USER CODE BEGIN WHILE */
hid_keyboard_report_t keyboard_report;
hid_mouse_report_t mouse_report;
while (1)
{
bsp_key_number_e key;
bsp_key_event_type_e event;
bsp_key_get_event(&key, &event);
bsp_key_number_e key;
bsp_key_event_type_e event;
bsp_key_get_event(&key, &event);
if (key != bsp_key_none) { //有按键按下
if (key == bsp_key1) { //触发USB键盘按键
hid_mouse_send(0, 0, -20, 0, 0);
hid_mouse_send(0, 0, 0, -20, 0);
} else if (key == bsp_key2) {
hid_consumer_control_key_send(0, HID_CONSUMER_USAGE_MUTE); //静音
hid_consumer_control_key_send(0, 0); //抬起
} else if (key == bsp_key3) {
hid_keyboard_key_send(0, 0, HID_KBD_USAGE_A);
hid_keyboard_key_send(0, 0, 0);
} else if (key == bsp_key4) {
hid_mouse_send(0, 0, 0, 0, -1);
} else if (key == bsp_key5) {
hid_mouse_send(0, 0, 0, 20, 0);
} else if (key == bsp_key6) {
hid_consumer_control_key_send(0, HID_CONSUMER_USAGE_PLAYPAUSE); //静音
hid_consumer_control_key_send(0, 0); //抬起
} else if (key == bsp_key7) {
hid_keyboard_key_send(0, HID_MODIFER_LGUI, HID_KBD_USAGE_A + ('L' - 'A'));
hid_keyboard_key_send(0, 0, 0);
} else if (key == bsp_key8) {
hid_system_control_key_send(0, HID_SYSTEM_USAGE_POWER); //关闭屏幕
hid_system_control_key_send(0, 0); //抬起
} else if (key == bsp_key5) {
hid_mouse_send(0, 0, 20, 0, 0);
} else if (key == bsp_key6) {
} else if (key == bsp_key7) {
} else if (key == bsp_key8) {
}
} else {
__WFI();
}
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */

View File

@ -134,11 +134,11 @@
<RunIndependent>0</RunIndependent>
<UpdateFlashBeforeDebugging>1</UpdateFlashBeforeDebugging>
<Capability>1</Capability>
<DriverSelection>4101</DriverSelection>
<DriverSelection>4096</DriverSelection>
</Flash1>
<bUseTDR>1</bUseTDR>
<Flash2>BIN\UL2V8M.DLL</Flash2>
<Flash3></Flash3>
<Flash2>BIN\UL2CM3.DLL</Flash2>
<Flash3>"" ()</Flash3>
<Flash4></Flash4>
<pFcarmOut></pFcarmOut>
<pFcarmGrp></pFcarmGrp>