Przeglądaj źródła

update hid inout demo

sakimisu 2 lat temu
rodzic
commit
64e2b8d480
1 zmienionych plików z 49 dodań i 52 usunięć
  1. 49 52
      demo/hid_custom_inout_template.c

+ 49 - 52
demo/hid_custom_inout_template.c

@@ -3,7 +3,7 @@
 
 /*!< hidraw in endpoint */
 #define HIDRAW_IN_EP       0x81
-#define HIDRAW_IN_SIZE     64
+#define HIDRAW_IN_EP_SIZE  64
 #define HIDRAW_IN_INTERVAL 10
 
 /*!< hidraw out endpoint */
@@ -20,7 +20,7 @@
 #define USB_HID_CONFIG_DESC_SIZ (9 + 9 + 9 + 7 + 7)
 
 /*!< custom hid report descriptor size */
-#define HID_CUSTOM_REPORT_DESC_SIZE 34
+#define HID_CUSTOM_REPORT_DESC_SIZE 38
 
 /*!< global descriptor */
 static const uint8_t hid_descriptor[] = {
@@ -51,7 +51,7 @@ static const uint8_t hid_descriptor[] = {
     USB_DESCRIPTOR_TYPE_ENDPOINT, /* bDescriptorType: */
     HIDRAW_IN_EP,                 /* bEndpointAddress: Endpoint Address (IN) */
     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 */
     /******************** Descriptor of Custom out endpoint ********************/
     0x07,                         /* bLength: Endpoint Descriptor size */
@@ -61,13 +61,13 @@ static const uint8_t hid_descriptor[] = {
     WBVAL(HIDRAW_OUT_EP_SIZE),    /* wMaxPacketSize: 4 Byte max */
     HIDRAW_OUT_EP_INTERVAL,       /* bInterval: Polling Interval */
     /* 73 */
-    ///////////////////////////////////////
-    /// string0 descriptor
-    ///////////////////////////////////////
+    /*
+     * string0 descriptor
+     */
     USB_LANGID_INIT(USBD_LANGID_STRING),
-    ///////////////////////////////////////
-    /// string1 descriptor
-    ///////////////////////////////////////
+    /*
+     * string1 descriptor
+     */
     0x14,                       /* bLength */
     USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
     'C', 0x00,                  /* wcChar0 */
@@ -79,9 +79,9 @@ static const uint8_t hid_descriptor[] = {
     'U', 0x00,                  /* wcChar6 */
     'S', 0x00,                  /* wcChar7 */
     'B', 0x00,                  /* wcChar8 */
-    ///////////////////////////////////////
-    /// string2 descriptor
-    ///////////////////////////////////////
+    /*
+     * string2 descriptor
+     */
     0x26,                       /* bLength */
     USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
     'C', 0x00,                  /* wcChar0 */
@@ -102,9 +102,9 @@ static const uint8_t hid_descriptor[] = {
     'E', 0x00,                  /* wcChar15 */
     'M', 0x00,                  /* wcChar16 */
     'O', 0x00,                  /* wcChar17 */
-    ///////////////////////////////////////
-    /// string3 descriptor
-    ///////////////////////////////////////
+    /*
+     * string3 descriptor
+     */
     0x16,                       /* bLength */
     USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
     '2', 0x00,                  /* wcChar0 */
@@ -118,9 +118,9 @@ static const uint8_t hid_descriptor[] = {
     '5', 0x00,                  /* wcChar8 */
     '6', 0x00,                  /* wcChar9 */
 #ifdef CONFIG_USB_HS
-    ///////////////////////////////////////
-    /// device qualifier descriptor
-    ///////////////////////////////////////
+    /*
+     * device qualifier descriptor
+     */
     0x0a,
     USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
     0x00,
@@ -138,34 +138,36 @@ static const uint8_t hid_descriptor[] = {
 /*!< custom hid report descriptor */
 static const uint8_t hid_custom_report_desc[HID_CUSTOM_REPORT_DESC_SIZE] = {
     /* USER CODE BEGIN 0 */
-    0x06, 0x00, 0xff, // USAGE_PAGE (Vendor Defined Page 1)
-    0x09, 0x01,       // USAGE (Vendor Usage 1)
-    0xa1, 0x01,       // COLLECTION (Application)
-    0x09, 0x01,       //   USAGE (Vendor Usage 1)
-    0x15, 0x00,       //   LOGICAL_MINIMUM (0)
-    0x26, 0xff, 0x00, //   LOGICAL_MAXIMUM (255)
-    0x95, 0x40,       //   REPORT_COUNT (64)
-    0x75, 0x08,       //   REPORT_SIZE (8)
-    0x81, 0x02,       //   INPUT (Data,Var,Abs)
+    0x06, 0x00, 0xff, /* USAGE_PAGE (Vendor Defined Page 1) */
+    0x09, 0x01,       /* USAGE (Vendor Usage 1) */
+    0xa1, 0x01,       /* COLLECTION (Application) */
+    0x85, 0x02,       /*   REPORT ID (0x02) */
+    0x09, 0x01,       /*   USAGE (Vendor Usage 1) */
+    0x15, 0x00,       /*   LOGICAL_MINIMUM (0) */
+    0x26, 0xff, 0x00, /*   LOGICAL_MAXIMUM (255) */
+    0x95, 0x40 - 1,   /*   REPORT_COUNT (63) */
+    0x75, 0x08,       /*   REPORT_SIZE (8) */
+    0x81, 0x02,       /*   INPUT (Data,Var,Abs) */
     /* <___________________________________________________> */
-    0x09, 0x01,       //   USAGE (Vendor Usage 1)
-    0x15, 0x00,       //   LOGICAL_MINIMUM (0)
-    0x26, 0xff, 0x00, //   LOGICAL_MAXIMUM (255)
-    0x95, 0x40,       //   REPORT_COUNT (64)
-    0x75, 0x08,       //   REPORT_SIZE (8)
-    0x91, 0x02,       //   OUTPUT (Data,Var,Abs)
+    0x85, 0x01,       /*   REPORT ID (0x01) */
+    0x09, 0x01,       /*   USAGE (Vendor Usage 1) */
+    0x15, 0x00,       /*   LOGICAL_MINIMUM (0) */
+    0x26, 0xff, 0x00, /*   LOGICAL_MAXIMUM (255) */
+    0x95, 0x40 - 1,   /*   REPORT_COUNT (63) */
+    0x75, 0x08,       /*   REPORT_SIZE (8) */
+    0x91, 0x02,       /*   OUTPUT (Data,Var,Abs) */
     /* USER CODE END 0 */
     0xC0 /*     END_COLLECTION	             */
 };
 
-USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t read_buffer[64];
-USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t write_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 send_buffer[HIDRAW_IN_EP_SIZE];
 
 #define HID_STATE_IDLE 0
 #define HID_STATE_BUSY 1
 
 /*!< 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)
 {
@@ -182,7 +184,7 @@ void usbd_event_handler(uint8_t event)
             break;
         case USBD_EVENT_CONFIGURED:
             /* 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;
         case USBD_EVENT_SET_REMOTE_WAKEUP:
             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);
     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 = {
@@ -216,9 +220,16 @@ static struct usbd_endpoint custom_out_ep = {
     .ep_addr = HIDRAW_OUT_EP
 };
 
+/* function ------------------------------------------------------------------*/
+/**
+ * @brief            hid custom init
+ * @pre              none
+ * @param[in]        none
+ * @retval           none
+ */
 struct usbd_interface intf0;
 
-void hid_custom_keyboard_init(void)
+void hid_custom_init(void)
 {
     usbd_desc_register(hid_descriptor);
     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();
 }
-
-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) {
-    }
-}