Просмотр исходного кода

finish descriptor refractor for hid keyboard & mouse

hathach 12 лет назад
Родитель
Сommit
cf82b13a0e

+ 1 - 1
demos/device/src/tusb_config.h

@@ -63,7 +63,7 @@
 #define TUSB_CFG_DEVICE_HID_MOUSE               1
 #define TUSB_CFG_DEVICE_HID_GENERIC             0 // not supported yet
 #define TUSB_CFG_DEVICE_MSC                     1
-#define TUSB_CFG_DEVICE_CDC                     0
+#define TUSB_CFG_DEVICE_CDC                     1
 
 //--------------------------------------------------------------------+
 // COMMON CONFIGURATION

+ 6 - 6
demos/device/src/tusb_descriptors.c

@@ -43,7 +43,7 @@
 //--------------------------------------------------------------------+
 #if TUSB_CFG_DEVICE_HID_KEYBOARD
 ATTR_USB_MIN_ALIGNMENT TUSB_CFG_ATTR_USBRAM
-uint8_t tusbd_hid_keyboard_descriptor_report[] = {
+uint8_t desc_keyboard_report[] = {
   HID_USAGE_PAGE ( HID_USAGE_PAGE_DESKTOP     ),
   HID_USAGE      ( HID_USAGE_DESKTOP_KEYBOARD ),
   HID_COLLECTION ( HID_COLLECTION_APPLICATION ),
@@ -90,7 +90,7 @@ uint8_t tusbd_hid_keyboard_descriptor_report[] = {
 //--------------------------------------------------------------------+
 #if TUSB_CFG_DEVICE_HID_MOUSE
 ATTR_USB_MIN_ALIGNMENT TUSB_CFG_ATTR_USBRAM
-uint8_t tusbd_hid_mouse_descriptor_report[] = {
+uint8_t desc_mouse_report[] = {
   HID_USAGE_PAGE ( HID_USAGE_PAGE_DESKTOP     ),
   HID_USAGE      ( HID_USAGE_DESKTOP_MOUSE    ),
   HID_COLLECTION ( HID_COLLECTION_APPLICATION ),
@@ -316,7 +316,7 @@ app_descriptor_configuration_t desc_configuration =
         .bCountryCode      = HID_Local_NotSupported,
         .bNumDescriptors   = 1,
         .bReportType       = HID_DESC_TYPE_REPORT,
-        .wReportLength     = sizeof(tusbd_hid_keyboard_descriptor_report)
+        .wReportLength     = sizeof(desc_keyboard_report)
     },
 
     .keyboard_endpoint =
@@ -353,7 +353,7 @@ app_descriptor_configuration_t desc_configuration =
         .bCountryCode      = HID_Local_NotSupported,
         .bNumDescriptors   = 1,
         .bReportType       = HID_DESC_TYPE_REPORT,
-        .wReportLength     = sizeof(tusbd_hid_mouse_descriptor_report)
+        .wReportLength     = sizeof(desc_mouse_report)
     },
 
     .mouse_endpoint =
@@ -454,10 +454,10 @@ tusbd_descriptor_pointer_t tusbd_descriptor_pointers =
     },
 
     #if TUSB_CFG_DEVICE_HID_KEYBOARD
-    .p_hid_keyboard_report = tusbd_hid_keyboard_descriptor_report,
+    .p_hid_keyboard_report = desc_keyboard_report,
     #endif
 
     #if TUSB_CFG_DEVICE_HID_KEYBOARD
-    .p_hid_mouse_report    = tusbd_hid_mouse_descriptor_report,
+    .p_hid_mouse_report    = desc_mouse_report,
     #endif
 };

+ 0 - 6
demos/device/src/tusb_descriptors.h

@@ -176,10 +176,4 @@ typedef ATTR_PACKED_STRUCT(struct)
 
 } app_descriptor_configuration_t;
 
-//--------------------------------------------------------------------+
-// Export descriptors
-//--------------------------------------------------------------------+
-extern uint8_t tusbd_hid_keyboard_descriptor_report[];
-extern uint8_t tusbd_hid_mouse_descriptor_report[];
-
 #endif

+ 9 - 13
tinyusb/class/hid_device.c

@@ -46,17 +46,14 @@
 //--------------------------------------------------------------------+
 #include "common/common.h"
 #include "hid_device.h"
-#include "tusb_descriptors.h"
 
 //--------------------------------------------------------------------+
 // MACRO CONSTANT TYPEDEF
 //--------------------------------------------------------------------+
-
-enum {
-  HIDD_NUMBER_OF_SUBCLASS = 3
-};
+enum { HIDD_NUMBER_OF_SUBCLASS = 3 };
 
 typedef struct {
+  uint8_t const * p_report_desc;
   uint16_t report_length;
 
   endpoint_handle_t ept_handle;
@@ -64,7 +61,6 @@ typedef struct {
 }hidd_interface_t;
 
 typedef struct {
-  uint8_t const * const p_report_desc;
   hidd_interface_t * const p_interface;
   void (* const mounted_cb) (uint8_t coreid);
   void (* const unmounted_cb) (uint8_t coreid);
@@ -83,7 +79,6 @@ static hidd_class_driver_t const hidd_class_driver[HIDD_NUMBER_OF_SUBCLASS] =
 #if TUSB_CFG_DEVICE_HID_KEYBOARD
     [HID_PROTOCOL_KEYBOARD] =
     {
-        .p_report_desc = tusbd_hid_keyboard_descriptor_report,
         .p_interface   = &keyboardd_data,
         .mounted_cb    = tusbd_hid_keyboard_mounted_cb,
         .unmounted_cb  = tusbd_hid_keyboard_unmounted_cb,
@@ -96,7 +91,6 @@ static hidd_class_driver_t const hidd_class_driver[HIDD_NUMBER_OF_SUBCLASS] =
 #if TUSB_CFG_DEVICE_HID_MOUSE
     [HID_PROTOCOL_MOUSE] =
     {
-        .p_report_desc = tusbd_hid_mouse_descriptor_report,
         .p_interface   = &moused_data,
         .mounted_cb    = tusbd_hid_mouse_mounted_cb,
         .unmounted_cb  = tusbd_hid_mouse_unmounted_cb,
@@ -107,16 +101,16 @@ static hidd_class_driver_t const hidd_class_driver[HIDD_NUMBER_OF_SUBCLASS] =
 #endif
 };
 
-#if TUSB_CFG_DEVICE_HID_KEYBOARD || TUSB_CFG_DEVICE_HID_MOUSE
+
+// TODO [HID] generic
 TUSB_CFG_ATTR_USBRAM ATTR_USB_MIN_ALIGNMENT
 uint8_t m_control_data[ MAX_OF(sizeof(hid_keyboard_report_t), sizeof(hid_mouse_report_t)) ];
-#endif
 
 //--------------------------------------------------------------------+
 // KEYBOARD APPLICATION API
 //--------------------------------------------------------------------+
 #if TUSB_CFG_DEVICE_HID_KEYBOARD
-TUSB_CFG_ATTR_USBRAM STATIC_VAR hidd_interface_t keyboardd_data;
+STATIC_VAR hidd_interface_t keyboardd_data;
 
 bool tusbd_hid_keyboard_is_busy(uint8_t coreid)
 {
@@ -139,7 +133,7 @@ tusb_error_t tusbd_hid_keyboard_send(uint8_t coreid, hid_keyboard_report_t const
 // MOUSE APPLICATION API
 //--------------------------------------------------------------------+
 #if TUSB_CFG_DEVICE_HID_MOUSE
-TUSB_CFG_ATTR_USBRAM STATIC_VAR hidd_interface_t moused_data;
+STATIC_VAR hidd_interface_t moused_data;
 
 bool tusbd_hid_mouse_is_busy(uint8_t coreid)
 {
@@ -212,7 +206,7 @@ tusb_error_t hidd_control_request_subtask(uint8_t coreid, tusb_control_request_t
     ASSERT ( p_request->bRequest == TUSB_REQUEST_GET_DESCRIPTOR && desc_type == HID_DESC_TYPE_REPORT,
              TUSB_ERROR_DCD_CONTROL_REQUEST_NOT_SUPPORT);
 
-    dcd_pipe_control_xfer(coreid, TUSB_DIR_DEV_TO_HOST, (uint8_t*) p_driver->p_report_desc, p_hid->report_length, false);
+    dcd_pipe_control_xfer(coreid, TUSB_DIR_DEV_TO_HOST, (uint8_t*) p_hid->p_report_desc, p_hid->report_length, false);
   }
   //------------- Class Specific Request -------------//
   else if (p_request->bmRequestType_bit.type == TUSB_REQUEST_TYPE_CLASS)
@@ -294,8 +288,10 @@ tusb_error_t hidd_open(uint8_t coreid, tusb_descriptor_interface_t const * p_int
         ASSERT( endpointhandle_is_valid(p_hid->ept_handle), TUSB_ERROR_DCD_FAILED);
 
         p_hid->interface_number = p_interface_desc->bInterfaceNumber;
+        p_hid->p_report_desc    = (p_interface_desc->bInterfaceProtocol == HID_PROTOCOL_KEYBOARD) ? tusbd_descriptor_pointers.p_hid_keyboard_report : tusbd_descriptor_pointers.p_hid_mouse_report;
         p_hid->report_length    = p_desc_hid->wReportLength;
 
+        ASSERT_PTR(p_hid->p_report_desc, TUSB_ERROR_DESCRIPTOR_CORRUPTED);
         p_driver->mounted_cb(coreid);
       }
       break;

+ 2 - 2
tinyusb/device/usbd.c

@@ -303,7 +303,7 @@ static tusb_error_t usbd_set_configure_received(uint8_t coreid, uint8_t config_n
   {
     if ( TUSB_DESC_TYPE_INTERFACE_ASSOCIATION == p_desc[DESCRIPTOR_OFFSET_TYPE])
     {
-      p_desc += p_desc[DESCRIPTOR_OFFSET_LENGTH]; // ignore IAD
+      p_desc += p_desc[DESCRIPTOR_OFFSET_LENGTH]; // ignore Interface Association
     }else
     {
       ASSERT( TUSB_DESC_TYPE_INTERFACE == p_desc[DESCRIPTOR_OFFSET_TYPE], TUSB_ERROR_NOT_SUPPORTED_YET );
@@ -346,7 +346,7 @@ static tusb_error_t get_descriptor(uint8_t coreid, tusb_control_request_t const
   }
   else if ( TUSB_DESC_TYPE_STRING == desc_type )
   {
-    if ( ! (desc_index < TUSB_CFG_DEVICE_STRING_DESCRIPTOR_COUNT) ) return TUSB_ERROR_DCD_CONTROL_REQUEST_NOT_SUPPORT;
+    if ( !(desc_index < TUSB_CFG_DEVICE_STRING_DESCRIPTOR_COUNT && tusbd_descriptor_pointers.p_string_arr[desc_index] != NULL) ) return TUSB_ERROR_DCD_CONTROL_REQUEST_NOT_SUPPORT;
 
     (*pp_buffer) = tusbd_descriptor_pointers.p_string_arr[desc_index];
     (*p_length)  = **pp_buffer;