update hid inout demo

This commit is contained in:
sakimisu
2023-07-20 22:03:45 +08:00
parent fed6f26ebd
commit 64e2b8d480

View File

@@ -3,7 +3,7 @@
/*!< hidraw in endpoint */ /*!< hidraw in endpoint */
#define HIDRAW_IN_EP 0x81 #define HIDRAW_IN_EP 0x81
#define HIDRAW_IN_SIZE 64 #define HIDRAW_IN_EP_SIZE 64
#define HIDRAW_IN_INTERVAL 10 #define HIDRAW_IN_INTERVAL 10
/*!< hidraw out endpoint */ /*!< hidraw out endpoint */
@@ -20,7 +20,7 @@
#define USB_HID_CONFIG_DESC_SIZ (9 + 9 + 9 + 7 + 7) #define USB_HID_CONFIG_DESC_SIZ (9 + 9 + 9 + 7 + 7)
/*!< custom hid report descriptor size */ /*!< custom hid report descriptor size */
#define HID_CUSTOM_REPORT_DESC_SIZE 34 #define HID_CUSTOM_REPORT_DESC_SIZE 38
/*!< global descriptor */ /*!< global descriptor */
static const uint8_t hid_descriptor[] = { static const uint8_t hid_descriptor[] = {
@@ -51,7 +51,7 @@ static const uint8_t hid_descriptor[] = {
USB_DESCRIPTOR_TYPE_ENDPOINT, /* bDescriptorType: */ USB_DESCRIPTOR_TYPE_ENDPOINT, /* bDescriptorType: */
HIDRAW_IN_EP, /* bEndpointAddress: Endpoint Address (IN) */ HIDRAW_IN_EP, /* bEndpointAddress: Endpoint Address (IN) */
0x03, /* bmAttributes: Interrupt endpoint */ 0x03, /* bmAttributes: Interrupt endpoint */
WBVAL(HIDRAW_IN_SIZE), /* wMaxPacketSize: 4 Byte max */ WBVAL(HIDRAW_IN_EP_SIZE), /* wMaxPacketSize: 4 Byte max */
HIDRAW_IN_INTERVAL, /* bInterval: Polling Interval */ HIDRAW_IN_INTERVAL, /* bInterval: Polling Interval */
/******************** Descriptor of Custom out endpoint ********************/ /******************** Descriptor of Custom out endpoint ********************/
0x07, /* bLength: Endpoint Descriptor size */ 0x07, /* bLength: Endpoint Descriptor size */
@@ -61,13 +61,13 @@ static const uint8_t hid_descriptor[] = {
WBVAL(HIDRAW_OUT_EP_SIZE), /* wMaxPacketSize: 4 Byte max */ WBVAL(HIDRAW_OUT_EP_SIZE), /* wMaxPacketSize: 4 Byte max */
HIDRAW_OUT_EP_INTERVAL, /* bInterval: Polling Interval */ HIDRAW_OUT_EP_INTERVAL, /* bInterval: Polling Interval */
/* 73 */ /* 73 */
/////////////////////////////////////// /*
/// string0 descriptor * string0 descriptor
/////////////////////////////////////// */
USB_LANGID_INIT(USBD_LANGID_STRING), USB_LANGID_INIT(USBD_LANGID_STRING),
/////////////////////////////////////// /*
/// string1 descriptor * string1 descriptor
/////////////////////////////////////// */
0x14, /* bLength */ 0x14, /* bLength */
USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */ USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
'C', 0x00, /* wcChar0 */ 'C', 0x00, /* wcChar0 */
@@ -79,9 +79,9 @@ static const uint8_t hid_descriptor[] = {
'U', 0x00, /* wcChar6 */ 'U', 0x00, /* wcChar6 */
'S', 0x00, /* wcChar7 */ 'S', 0x00, /* wcChar7 */
'B', 0x00, /* wcChar8 */ 'B', 0x00, /* wcChar8 */
/////////////////////////////////////// /*
/// string2 descriptor * string2 descriptor
/////////////////////////////////////// */
0x26, /* bLength */ 0x26, /* bLength */
USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */ USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
'C', 0x00, /* wcChar0 */ 'C', 0x00, /* wcChar0 */
@@ -102,9 +102,9 @@ static const uint8_t hid_descriptor[] = {
'E', 0x00, /* wcChar15 */ 'E', 0x00, /* wcChar15 */
'M', 0x00, /* wcChar16 */ 'M', 0x00, /* wcChar16 */
'O', 0x00, /* wcChar17 */ 'O', 0x00, /* wcChar17 */
/////////////////////////////////////// /*
/// string3 descriptor * string3 descriptor
/////////////////////////////////////// */
0x16, /* bLength */ 0x16, /* bLength */
USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */ USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
'2', 0x00, /* wcChar0 */ '2', 0x00, /* wcChar0 */
@@ -118,9 +118,9 @@ static const uint8_t hid_descriptor[] = {
'5', 0x00, /* wcChar8 */ '5', 0x00, /* wcChar8 */
'6', 0x00, /* wcChar9 */ '6', 0x00, /* wcChar9 */
#ifdef CONFIG_USB_HS #ifdef CONFIG_USB_HS
/////////////////////////////////////// /*
/// device qualifier descriptor * device qualifier descriptor
/////////////////////////////////////// */
0x0a, 0x0a,
USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER, USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
0x00, 0x00,
@@ -138,34 +138,36 @@ static const uint8_t hid_descriptor[] = {
/*!< custom hid report descriptor */ /*!< custom hid report descriptor */
static const uint8_t hid_custom_report_desc[HID_CUSTOM_REPORT_DESC_SIZE] = { static const uint8_t hid_custom_report_desc[HID_CUSTOM_REPORT_DESC_SIZE] = {
/* USER CODE BEGIN 0 */ /* USER CODE BEGIN 0 */
0x06, 0x00, 0xff, // USAGE_PAGE (Vendor Defined Page 1) 0x06, 0x00, 0xff, /* USAGE_PAGE (Vendor Defined Page 1) */
0x09, 0x01, // USAGE (Vendor Usage 1) 0x09, 0x01, /* USAGE (Vendor Usage 1) */
0xa1, 0x01, // COLLECTION (Application) 0xa1, 0x01, /* COLLECTION (Application) */
0x09, 0x01, // USAGE (Vendor Usage 1) 0x85, 0x02, /* REPORT ID (0x02) */
0x15, 0x00, // LOGICAL_MINIMUM (0) 0x09, 0x01, /* USAGE (Vendor Usage 1) */
0x26, 0xff, 0x00, // LOGICAL_MAXIMUM (255) 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
0x95, 0x40, // REPORT_COUNT (64) 0x26, 0xff, 0x00, /* LOGICAL_MAXIMUM (255) */
0x75, 0x08, // REPORT_SIZE (8) 0x95, 0x40 - 1, /* REPORT_COUNT (63) */
0x81, 0x02, // INPUT (Data,Var,Abs) 0x75, 0x08, /* REPORT_SIZE (8) */
0x81, 0x02, /* INPUT (Data,Var,Abs) */
/* <___________________________________________________> */ /* <___________________________________________________> */
0x09, 0x01, // USAGE (Vendor Usage 1) 0x85, 0x01, /* REPORT ID (0x01) */
0x15, 0x00, // LOGICAL_MINIMUM (0) 0x09, 0x01, /* USAGE (Vendor Usage 1) */
0x26, 0xff, 0x00, // LOGICAL_MAXIMUM (255) 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
0x95, 0x40, // REPORT_COUNT (64) 0x26, 0xff, 0x00, /* LOGICAL_MAXIMUM (255) */
0x75, 0x08, // REPORT_SIZE (8) 0x95, 0x40 - 1, /* REPORT_COUNT (63) */
0x91, 0x02, // OUTPUT (Data,Var,Abs) 0x75, 0x08, /* REPORT_SIZE (8) */
0x91, 0x02, /* OUTPUT (Data,Var,Abs) */
/* USER CODE END 0 */ /* USER CODE END 0 */
0xC0 /* END_COLLECTION */ 0xC0 /* END_COLLECTION */
}; };
USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t read_buffer[64]; USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t read_buffer[HIDRAW_OUT_EP_SIZE];
USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t write_buffer[64]; USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t send_buffer[HIDRAW_IN_EP_SIZE];
#define HID_STATE_IDLE 0 #define HID_STATE_IDLE 0
#define HID_STATE_BUSY 1 #define HID_STATE_BUSY 1
/*!< hid state ! Data can be sent only when state is idle */ /*!< hid state ! Data can be sent only when state is idle */
static volatile uint8_t custom_state = HID_STATE_IDLE; static volatile uint8_t custom_state;
void usbd_event_handler(uint8_t event) void usbd_event_handler(uint8_t event)
{ {
@@ -182,7 +184,7 @@ void usbd_event_handler(uint8_t event)
break; break;
case USBD_EVENT_CONFIGURED: case USBD_EVENT_CONFIGURED:
/* setup first out ep read transfer */ /* setup first out ep read transfer */
usbd_ep_start_read(HIDRAW_OUT_EP, read_buffer, 64); usbd_ep_start_read(HIDRAW_OUT_EP, read_buffer, HIDRAW_OUT_EP_SIZE);
break; break;
case USBD_EVENT_SET_REMOTE_WAKEUP: case USBD_EVENT_SET_REMOTE_WAKEUP:
break; break;
@@ -204,6 +206,8 @@ static void usbd_hid_custom_out_callback(uint8_t ep, uint32_t nbytes)
{ {
USB_LOG_RAW("actual out len:%d\r\n", nbytes); USB_LOG_RAW("actual out len:%d\r\n", nbytes);
usbd_ep_start_read(HIDRAW_OUT_EP, read_buffer, 64); usbd_ep_start_read(HIDRAW_OUT_EP, read_buffer, 64);
read_buffer[0] = 0x02; /* IN: report id */
usbd_ep_start_write(HIDRAW_IN_EP, read_buffer, nbytes);
} }
static struct usbd_endpoint custom_in_ep = { static struct usbd_endpoint custom_in_ep = {
@@ -216,9 +220,16 @@ static struct usbd_endpoint custom_out_ep = {
.ep_addr = HIDRAW_OUT_EP .ep_addr = HIDRAW_OUT_EP
}; };
/* function ------------------------------------------------------------------*/
/**
* @brief hid custom init
* @pre none
* @param[in] none
* @retval none
*/
struct usbd_interface intf0; struct usbd_interface intf0;
void hid_custom_keyboard_init(void) void hid_custom_init(void)
{ {
usbd_desc_register(hid_descriptor); usbd_desc_register(hid_descriptor);
usbd_add_interface(usbd_hid_init_intf(&intf0, hid_custom_report_desc, HID_CUSTOM_REPORT_DESC_SIZE)); usbd_add_interface(usbd_hid_init_intf(&intf0, hid_custom_report_desc, HID_CUSTOM_REPORT_DESC_SIZE));
@@ -227,17 +238,3 @@ void hid_custom_keyboard_init(void)
usbd_initialize(); usbd_initialize();
} }
void hid_custom_test(void)
{
const uint8_t sendbuffer[8] = { 0x00, 0x00, HID_KBD_USAGE_A, 0x00, 0x00, 0x00, 0x00, 0x00 };
memcpy(write_buffer, sendbuffer, 8);
int ret = usbd_ep_start_write(HIDRAW_IN_EP, write_buffer, 8);
if (ret < 0) {
return;
}
custom_state = HID_STATE_BUSY;
while (custom_state == HID_STATE_BUSY) {
}
}