summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorhathach <[email protected]>2019-04-18 22:31:35 +0700
committerhathach <[email protected]>2019-04-18 22:31:35 +0700
commit307ba23046ff4c0795009c1426708facb37bdf82 (patch)
tree9f0e80d00ee3c44d3937353c860cb84064c20c35
parenta30461b078445ab2d11319fade47789ecc866b7d (diff)
wrap up hid device refactor
-rw-r--r--examples/device/cdc_msc_hid/src/main.c48
-rw-r--r--examples/device/cdc_msc_hid/src/tusb_config.h14
-rw-r--r--examples/device/cdc_msc_hid/src/usb_descriptors.c100
-rw-r--r--hw/bsp/board.h6
-rw-r--r--src/class/cdc/cdc_device.h48
-rw-r--r--src/class/hid/hid_device.c1
-rw-r--r--src/class/hid/hid_device.h94
-rw-r--r--src/class/msc/msc_device.h37
-rw-r--r--src/device/usbd.h7
-rw-r--r--src/device/usbd_auto_desc.c41
-rw-r--r--src/tusb_option.h16
11 files changed, 202 insertions, 210 deletions
diff --git a/examples/device/cdc_msc_hid/src/main.c b/examples/device/cdc_msc_hid/src/main.c
index 4b82dfd86..99e9ff690 100644
--- a/examples/device/cdc_msc_hid/src/main.c
+++ b/examples/device/cdc_msc_hid/src/main.c
@@ -126,6 +126,14 @@ void tud_cdc_rx_cb(uint8_t itf)
// USB HID
//--------------------------------------------------------------------+
#if CFG_TUD_HID
+
+// Must match with ID declared by HID Report Descriptor, better to be in header file
+enum
+{
+ REPORT_ID_KEYBOARD = 1,
+ REPORT_ID_MOUSE
+};
+
void usb_hid_task(void)
{
// Poll every 10ms
@@ -137,6 +145,7 @@ void usb_hid_task(void)
uint32_t const btn = board_button_read();
+ // Remote wakeup
if ( tud_suspended() && btn )
{
// Wake up host if we are in suspend mode
@@ -144,35 +153,40 @@ void usb_hid_task(void)
tud_remote_wakeup();
}
-#if 1
+ /*------------- Mouse -------------*/
+ if ( tud_hid_ready() )
+ {
+ if ( btn )
+ {
+ int8_t const delta = 5;
+ tud_hid_mouse_move(REPORT_ID_MOUSE, delta, delta); // right + down
+
+ // delay a bit before attempt to send keyboard report
+ board_delay(2);
+ }
+ }
+
/*------------- Keyboard -------------*/
if ( tud_hid_ready() )
{
+ // use to avoid send multiple consecutive zero report for keyboard
+ static bool has_key = false;
+
if ( btn )
{
uint8_t keycode[6] = { 0 };
keycode[0] = HID_KEY_A;
- tud_hid_keyboard_report(0, 0, keycode);
+ tud_hid_keyboard_report(REPORT_ID_KEYBOARD, 0, keycode);
+
+ has_key = true;
}else
{
- tud_hid_keyboard_key_release(0);
+ // send empty key report if previously has key pressed
+ if (has_key) tud_hid_keyboard_key_release(REPORT_ID_KEYBOARD);
+ has_key = false;
}
}
-#endif
-
-#if 0
- /*------------- Mouse -------------*/
- if ( tud_hid_mouse_ready() )
- {
- enum { DELTA = 5 };
-
- if ( btn & 0x01 ) tud_hid_mouse_move(-DELTA, 0); // left
- if ( btn & 0x02 ) tud_hid_mouse_move( DELTA, 0); // right
- if ( btn & 0x04 ) tud_hid_mouse_move( 0 , -DELTA); // up
- if ( btn & 0x08 ) tud_hid_mouse_move( 0 , DELTA); // down
- }
-#endif
}
uint16_t tud_hid_get_report_cb(uint8_t report_id, hid_report_type_t report_type, uint8_t* buffer, uint16_t reqlen)
diff --git a/examples/device/cdc_msc_hid/src/tusb_config.h b/examples/device/cdc_msc_hid/src/tusb_config.h
index e815cfef7..00d152af6 100644
--- a/examples/device/cdc_msc_hid/src/tusb_config.h
+++ b/examples/device/cdc_msc_hid/src/tusb_config.h
@@ -76,7 +76,7 @@
*
* Note: All CFG_TUD_DESC_* are relevant only if CFG_TUD_DESC_AUTO is enabled
*/
-#define CFG_TUD_DESC_AUTO 1
+#define CFG_TUD_DESC_AUTO 0
// LPC 17xx and 40xx endpoint type (bulk/interrupt/iso) are fixed by its number
// Therefore we need to force endpoint number to correct type on lpc17xx
@@ -91,18 +91,10 @@
//------------- CLASS -------------//
#define CFG_TUD_CDC 1
#define CFG_TUD_MSC 1
-#define CFG_TUD_MIDI 0
-#define CFG_TUD_CUSTOM_CLASS 0
-
#define CFG_TUD_HID 1
-#define CFG_TUD_HID_KEYBOARD 1
-#define CFG_TUD_HID_MOUSE 0
-/* Use Boot Protocol for Keyboard, Mouse. Enable this will create separated HID interface
- * require more IN endpoints. If disabled, they they are all packed into a single
- * multiple report interface called "Generic". */
-#define CFG_TUD_HID_KEYBOARD_BOOT 1
-#define CFG_TUD_HID_MOUSE_BOOT 0
+#define CFG_TUD_MIDI 0
+#define CFG_TUD_CUSTOM_CLASS 0
//--------------------------------------------------------------------
// CDC
diff --git a/examples/device/cdc_msc_hid/src/usb_descriptors.c b/examples/device/cdc_msc_hid/src/usb_descriptors.c
index 33fa62f86..047edbecc 100644
--- a/examples/device/cdc_msc_hid/src/usb_descriptors.c
+++ b/examples/device/cdc_msc_hid/src/usb_descriptors.c
@@ -26,19 +26,14 @@
#include "tusb.h"
-// If HID Generic interface is generated
-#define AUTO_DESC_HID_GENERIC (CFG_TUD_HID && ((CFG_TUD_HID_KEYBOARD && !CFG_TUD_HID_KEYBOARD_BOOT) || \
- (CFG_TUD_HID_MOUSE && !CFG_TUD_HID_MOUSE_BOOT)) )
-
/* A combination of interfaces must have a unique product id, since PC will save device driver after the first plug.
* Same VID/PID with different interface e.g MSC (first), then CDC (later) will possibly cause system error on PC.
*
* Auto ProductID layout's Bitmap:
- * [MSB] HID Generic | Boot Mouse | Boot Keyboard | MSC | CDC [LSB]
+ * [MSB] HID | MSC | CDC [LSB]
*/
-#define _PID_MAP(itf, n) ( (CFG_TUD_##itf) << (n) )
-#define USB_PID (0x4000 | _PID_MAP(CDC, 0) | _PID_MAP(MSC, 1) | \
- _PID_MAP(HID_KEYBOARD, 2) | _PID_MAP(HID_MOUSE, 3) | (AUTO_DESC_HID_GENERIC << 4) )
+#define _PID_MAP(itf, n) ( (CFG_TUD_##itf) << (n) )
+#define USB_PID (0x4000 | _PID_MAP(CDC, 0) | _PID_MAP(MSC, 1) | _PID_MAP(HID, 2) )
//------------- Device Descriptors -------------//
tusb_desc_device_t const desc_device =
@@ -72,42 +67,83 @@ tusb_desc_device_t const desc_device =
.bNumConfigurations = 0x01
};
-//------------- String Descriptors -------------//
-// array of pointer to string descriptors
-uint16_t const * const string_desc_arr [] =
+//------------- HID Report Descriptor -------------//
+enum
{
- // 0: is supported language = English
- TUD_DESC_STRCONV(0x0409),
+ REPORT_ID_KEYBOARD = 1,
+ REPORT_ID_MOUSE
+};
+
+uint8_t const desc_hid_report[] =
+{
+ HID_REPORT_DESC_KEYBOARD( HID_REPORT_ID(REPORT_ID_KEYBOARD), ),
+ HID_REPORT_DESC_MOUSE ( HID_REPORT_ID(REPORT_ID_MOUSE), )
+};
+
+//------------- Configuration Descriptor -------------//
+enum {
+#if CFG_TUD_CDC
+ ITF_NUM_CDC = 0,
+ ITF_NUM_CDC_DATA,
+#endif
+
+#if CFG_TUD_MSC
+ ITF_NUM_MSC,
+#endif
+
+#if CFG_TUD_HID
+ ITF_NUM_HID,
+#endif
- // 1: Manufacturer
- TUD_DESC_STRCONV('t', 'i', 'n', 'y', 'u', 's', 'b', '.', 'o', 'r', 'g'),
+ ITF_NUM_TOTAL
+};
- // 2: Product
- TUD_DESC_STRCONV('t', 'i', 'n', 'y', 'u', 's', 'b', ' ', 'd', 'e', 'v', 'i', 'c', 'e'),
+enum {
+ CONFIG_DESC_LEN = sizeof(tusb_desc_configuration_t) + CFG_TUD_CDC*TUD_CDC_DESC_LEN + CFG_TUD_MSC*TUD_MSC_DESC_LEN + CFG_TUD_HID*TUD_HID_DESC_LEN
+};
- // 3: Serials, should use chip ID
- TUD_DESC_STRCONV('1', '2', '3', '4', '5', '6'),
+uint8_t const desc_configuration[] =
+{
+ // Config: self-powered with remote wakeup support, max power up to 100 mA
+ TUD_CONFIG_DESCRIPTOR(ITF_NUM_TOTAL, 0, CONFIG_DESC_LEN, TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, 100),
#if CFG_TUD_CDC
- // 4: CDC Interface
- TUD_DESC_STRCONV('t','u','s','b',' ','c','d','c'),
+ TUD_CDC_DESCRIPTOR(ITF_NUM_CDC, 4, 0x81, 8, 0x02, 0x82, 64),
#endif
#if CFG_TUD_MSC
- // 5: MSC Interface
- TUD_DESC_STRCONV('t','u','s','b',' ','m','s','c'),
+ TUD_MSC_DESCRIPTOR(ITF_NUM_MSC, 5, 0x03, 0x83, 64), // highspeed 512
#endif
-#if CFG_TUD_HID_KEYBOARD
- // 6: Keyboard
- TUD_DESC_STRCONV('t','u','s','b',' ','k','e','y','b','o','a','r','d'),
+#if CFG_TUD_HID
+ TUD_HID_DESCRIPTOR(ITF_NUM_HID, 6, HID_PROTOCOL_KEYBOARD, sizeof(desc_hid_report), 0x84, 16, 10)
#endif
+};
-#if CFG_TUD_HID_MOUSE
- // 7: Mouse
- TUD_DESC_STRCONV('t','u','s','b',' ','m', 'o','u','s','e'),
-#endif
+//------------- String Descriptors -------------//
+// array of pointer to string descriptors
+uint16_t const * const string_desc_arr [] =
+{
+ // 0: is supported language = English
+ TUD_DESC_STRCONV(0x0409),
+
+ // 1: Manufacturer
+ TUD_DESC_STRCONV('t', 'i', 'n', 'y', 'u', 's', 'b', '.', 'o', 'r', 'g'),
+
+ // 2: Product
+ TUD_DESC_STRCONV('t', 'i', 'n', 'y', 'u', 's', 'b', ' ', 'd', 'e', 'v', 'i', 'c', 'e'),
+
+ // 3: Serials, should use chip ID
+ TUD_DESC_STRCONV('1', '2', '3', '4', '5', '6'),
+
+ // 4: CDC Interface
+ TUD_DESC_STRCONV('t','u','s','b',' ','c','d','c'),
+
+ // 5: MSC Interface
+ TUD_DESC_STRCONV('t','u','s','b',' ','m','s','c'),
+ // 6: HID
+ TUD_DESC_STRCONV('t','u','s','b',' ','h','i','d')
};
// tud_desc_set is required by tinyusb stack
@@ -115,10 +151,10 @@ uint16_t const * const string_desc_arr [] =
tud_desc_set_t tud_desc_set =
{
.device = &desc_device,
- .config = NULL,
+ .config = desc_configuration,
.string_arr = (uint8_t const **) string_desc_arr,
.string_count = sizeof(string_desc_arr)/sizeof(string_desc_arr[0]),
- .hid_report = NULL,
+ .hid_report = desc_hid_report,
};
diff --git a/hw/bsp/board.h b/hw/bsp/board.h
index 896a14c0c..c78f3708b 100644
--- a/hw/bsp/board.h
+++ b/hw/bsp/board.h
@@ -95,6 +95,12 @@ static inline void board_led_off(void)
board_led_write(false);
}
+static inline void board_delay(uint32_t ms)
+{
+ uint32_t start_ms = board_millis();
+ while( board_millis() < start_ms + ms) {}
+}
+
static inline int8_t board_uart_getchar(void)
{
uint8_t c;
diff --git a/src/class/cdc/cdc_device.h b/src/class/cdc/cdc_device.h
index 23f4f609a..d7f89081c 100644
--- a/src/class/cdc/cdc_device.h
+++ b/src/class/cdc/cdc_device.h
@@ -95,17 +95,6 @@ ATTR_WEAK void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts);
ATTR_WEAK void tud_cdc_line_coding_cb(uint8_t itf, cdc_line_coding_t const* p_line_coding);
//--------------------------------------------------------------------+
-// INTERNAL USBD-CLASS DRIVER API
-//--------------------------------------------------------------------+
-void cdcd_init (void);
-bool cdcd_open (uint8_t rhport, tusb_desc_interface_t const * p_interface_desc, uint16_t *p_length);
-bool cdcd_control_request (uint8_t rhport, tusb_control_request_t const * p_request);
-bool cdcd_control_request_complete (uint8_t rhport, tusb_control_request_t const * p_request);
-bool cdcd_xfer_cb (uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes);
-void cdcd_reset (uint8_t rhport);
-
-
-//--------------------------------------------------------------------+
// Interface Descriptor Template
//--------------------------------------------------------------------+
@@ -116,31 +105,42 @@ void cdcd_reset (uint8_t rhport);
// interface number, string index, EP notification address and size, EP data address (out,in) and size.
#define TUD_CDC_DESCRIPTOR(_itfnum, _stridx, _ep_notif, _ep_notif_size, _epout, _epin, _epsize) \
/* Interface Associate */\
- 0x08, TUSB_DESC_INTERFACE_ASSOCIATION, _itfnum, 0x02, TUSB_CLASS_CDC, CDC_COMM_SUBCLASS_ABSTRACT_CONTROL_MODEL, CDC_COMM_PROTOCOL_ATCOMMAND, 0x00,\
+ 8, TUSB_DESC_INTERFACE_ASSOCIATION, _itfnum, 2, TUSB_CLASS_CDC, CDC_COMM_SUBCLASS_ABSTRACT_CONTROL_MODEL, CDC_COMM_PROTOCOL_ATCOMMAND, 0,\
/* CDC Control Interface */\
- 0x09, TUSB_DESC_INTERFACE, _itfnum, 0x00, 0x01, TUSB_CLASS_CDC, CDC_COMM_SUBCLASS_ABSTRACT_CONTROL_MODEL, CDC_COMM_PROTOCOL_ATCOMMAND, _stridx,\
+ 9, TUSB_DESC_INTERFACE, _itfnum, 0, 1, TUSB_CLASS_CDC, CDC_COMM_SUBCLASS_ABSTRACT_CONTROL_MODEL, CDC_COMM_PROTOCOL_ATCOMMAND, _stridx,\
/* CDC Header */\
- 0x05, TUSB_DESC_CLASS_SPECIFIC, CDC_FUNC_DESC_HEADER, U16_TO_U8S_LE(0x0120),\
+ 5, TUSB_DESC_CLASS_SPECIFIC, CDC_FUNC_DESC_HEADER, U16_TO_U8S_LE(0x0120),\
/* CDC Call */\
- 0x05, TUSB_DESC_CLASS_SPECIFIC, CDC_FUNC_DESC_CALL_MANAGEMENT, 0x00, (_itfnum) + 1,\
+ 5, TUSB_DESC_CLASS_SPECIFIC, CDC_FUNC_DESC_CALL_MANAGEMENT, 0, (_itfnum) + 1,\
/* CDC ACM: support line request */\
- 0x04, TUSB_DESC_CLASS_SPECIFIC, CDC_FUNC_DESC_ABSTRACT_CONTROL_MANAGEMENT, 0x02,\
+ 4, TUSB_DESC_CLASS_SPECIFIC, CDC_FUNC_DESC_ABSTRACT_CONTROL_MANAGEMENT, 2,\
/* CDC Union */\
- 0x05, TUSB_DESC_CLASS_SPECIFIC, CDC_FUNC_DESC_UNION, _itfnum, (_itfnum) + 1,\
+ 5, TUSB_DESC_CLASS_SPECIFIC, CDC_FUNC_DESC_UNION, _itfnum, (_itfnum) + 1,\
/* Endpoint Notification */\
- 0x07, TUSB_DESC_ENDPOINT, _ep_notif, TUSB_XFER_INTERRUPT, U16_TO_U8S_LE(_ep_notif_size), 0x10,\
+ 7, TUSB_DESC_ENDPOINT, _ep_notif, TUSB_XFER_INTERRUPT, U16_TO_U8S_LE(_ep_notif_size), 16,\
/* CDC Data Interface */\
- 0x09, TUSB_DESC_INTERFACE, (_itfnum)+1, 0x00, 0x02, TUSB_CLASS_CDC_DATA, 0x00, 0x00, 0x00,\
+ 9, TUSB_DESC_INTERFACE, (_itfnum)+1, 0, 2, TUSB_CLASS_CDC_DATA, 0, 0, 0,\
/* Endpoint Out */\
- 0x07, TUSB_DESC_ENDPOINT, _epout, TUSB_XFER_BULK, U16_TO_U8S_LE(_epsize), 0x00,\
+ 7, TUSB_DESC_ENDPOINT, _epout, TUSB_XFER_BULK, U16_TO_U8S_LE(_epsize), 0,\
/* Endpoint In */\
- 0x07, TUSB_DESC_ENDPOINT, _epin, TUSB_XFER_BULK, U16_TO_U8S_LE(_epsize), 0x00
+ 7, TUSB_DESC_ENDPOINT, _epin, TUSB_XFER_BULK, U16_TO_U8S_LE(_epsize), 0
+
+
+/** @} */
+/** @} */
+
+//--------------------------------------------------------------------+
+// INTERNAL USBD-CLASS DRIVER API
+//--------------------------------------------------------------------+
+void cdcd_init (void);
+bool cdcd_open (uint8_t rhport, tusb_desc_interface_t const * p_interface_desc, uint16_t *p_length);
+bool cdcd_control_request (uint8_t rhport, tusb_control_request_t const * p_request);
+bool cdcd_control_request_complete (uint8_t rhport, tusb_control_request_t const * p_request);
+bool cdcd_xfer_cb (uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes);
+void cdcd_reset (uint8_t rhport);
#ifdef __cplusplus
}
#endif
#endif /* _TUSB_CDC_DEVICE_H_ */
-
-/** @} */
-/** @} */
diff --git a/src/class/hid/hid_device.c b/src/class/hid/hid_device.c
index 1a9e16105..ff572a0d2 100644
--- a/src/class/hid/hid_device.c
+++ b/src/class/hid/hid_device.c
@@ -147,6 +147,7 @@ bool tud_hid_keyboard_key_press(uint8_t report_id, char ch)
//--------------------------------------------------------------------+
bool tud_hid_mouse_report(uint8_t report_id, uint8_t buttons, int8_t x, int8_t y, int8_t scroll, int8_t pan)
{
+ (void) pan;
hid_mouse_report_t report =
{
.buttons = buttons,
diff --git a/src/class/hid/hid_device.h b/src/class/hid/hid_device.h
index 07121ab17..c017a8227 100644
--- a/src/class/hid/hid_device.h
+++ b/src/class/hid/hid_device.h
@@ -42,55 +42,31 @@
#define CFG_TUD_HID_ASCII_TO_KEYCODE_LOOKUP 0
#endif
-#if !CFG_TUD_HID_KEYBOARD && CFG_TUD_HID_KEYBOARD_BOOT
-#error CFG_TUD_HID_KEYBOARD must be enabled
-#endif
-
-#if !CFG_TUD_HID_MOUSE && CFG_TUD_HID_MOUSE_BOOT
-#error CFG_TUD_HID_MOUSE must be enabled
-#endif
-
-
//--------------------------------------------------------------------+
// Application API
//--------------------------------------------------------------------+
-/** Check if the interface is ready to use
- * \returns true if ready, otherwise interface may not be mounted or still busy transferring data
- * \note Application must not perform any action if the interface is not ready
- */
+// Check if the interface is ready to use
bool tud_hid_ready(void);
-bool tud_hid_report(uint8_t report_id, void const* report, uint8_t len);
// Check if current mode is Boot (true) or Report (false)
bool tud_hid_boot_mode(void);
+// Send report to host
+bool tud_hid_report(uint8_t report_id, void const* report, uint8_t len);
+
/*------------- Callbacks (Weak is optional) -------------*/
-/** Callback invoked when USB host request \ref HID_REQ_CONTROL_GET_REPORT.
- * \param[in] report_type specify which report (INPUT, OUTPUT, FEATURE) that host requests
- * \param[out] buffer data that application need to update, value must be accessible by USB controller (see \ref CFG_TUSB_MEM_SECTION)
- * \param[in] reqlen number of bytes that host requested
- * \retval non-zero Actual number of bytes in the response's buffer.
- * \retval zero indicates the current request is not supported. Tinyusb device stack will reject the request by
- * sending STALL in the data phase.
- * \note After this callback, the request is silently executed by the tinyusb stack, thus
- * the completion of this control request will not be reported to application.
- * For Keyboard, USB host often uses this to turn on/off the LED for CAPLOCKS, NUMLOCK (\ref hid_keyboard_led_bm_t)
- */
+// Invoked when receiving GET_REPORT control request
+// Application must fill buffer report's content and return its length.
+// Return zero will cause the stack to STALL request
uint16_t tud_hid_get_report_cb(uint8_t report_id, hid_report_type_t report_type, uint8_t* buffer, uint16_t reqlen);
-/** Callback invoked when USB host request \ref HID_REQ_CONTROL_SET_REPORT.
- * \param[in] report_type specify which report (INPUT, OUTPUT, FEATURE) that host requests
- * \param[in] buffer containing the report's data
- * \param[in] bufsize number of bytes in the \a buffer
- * \note By the time this callback is invoked, the USB control transfer is already completed in the hardware side.
- * Application are free to handle data at its own will.
- */
-void tud_hid_set_report_cb(uint8_t report_id, hid_report_type_t report_type, uint8_t const* buffer, uint16_t bufsize);
-
-ATTR_WEAK void tud_hid_mode_changed_cb(uint8_t boot_mode);
+// Invoked when receiving SET_REPORT control request
+void tud_hid_set_report_cb(uint8_t report_id, hid_report_type_t report_type, uint8_t const* buffer, uint16_t bufsize);
+// Invoked when host switch mode Boot <-> Report via SET_PROTOCOL request
+void tud_hid_mode_changed_cb(uint8_t boot_mode) ATTR_WEAK;
//--------------------------------------------------------------------+
// KEYBOARD API
@@ -136,20 +112,38 @@ static inline bool tud_hid_mouse_button_release(uint8_t report_id)
}
//--------------------------------------------------------------------+
-// HID Report Descriptor Template
+// Interface Descriptor Template
//--------------------------------------------------------------------+
-/* These template should be used as follow
- * - Only 1 report : no parameter
- * uint8_t report_desc[] = { ID_REPORT_DESC_KEYBOARD() };
+
+#define TUD_HID_DESC_LEN (9 + 9 + 7)
+
+#define TUD_HID_DESCRIPTOR(_itfnum, _stridx, _boot_protocol, _report_desc_len, _epin, _epsize, _ep_interval) \
+ /* Interface */\
+ 9, TUSB_DESC_INTERFACE, _itfnum, 0, 1, TUSB_CLASS_HID, (_boot_protocol) ? HID_SUBCLASS_BOOT : 0, _boot_protocol, _stridx,\
+ /* HID descriptor */\
+ 9, HID_DESC_TYPE_HID, U16_TO_U8S_LE(0x0111), 0, 1, HID_DESC_TYPE_REPORT, U16_TO_U8S_LE(_report_desc_len),\
+ /* Endpoint descriptor */\
+ 7, TUSB_DESC_ENDPOINT, _epin, TUSB_XFER_INTERRUPT, U16_TO_U8S_LE(_epsize), _ep_interval
+
+/* --------------------------------------------------------------------+
+ * HID Report Descriptor Template
+ *
+ * Convenient for declaring popular HID device (keyboard, mouse, consumer,
+ * gamepad etc...). Templates take "HID_REPORT_ID(n)," as input, leave
+ * empty if multiple reports is not used
+ *
+ * - Only 1 report: no parameter
+ * uint8_t const report_desc[] = { HID_REPORT_DESC_KEYBOARD() };
*
* - Multiple Reports: "HID_REPORT_ID(ID)," must be passed to template
- * uint8_t report_desc[] = {
- * ID_REPORT_DESC_KEYBOARD( HID_REPORT_ID(1) ,) ,
- * HID_REPORT_DESC_MOUSE ( HID_REPORT_ID(2) ,)
+ * uint8_t const report_desc[] =
+ * {
+ * HID_REPORT_DESC_KEYBOARD( HID_REPORT_ID(1), ) ,
+ * HID_REPORT_DESC_MOUSE ( HID_REPORT_ID(2), )
* };
- */
+ *--------------------------------------------------------------------*/
-/*------------- Keyboard Descriptor Template -------------*/
+// Keyboard Report Descriptor Template
#define HID_REPORT_DESC_KEYBOARD(...) \
HID_USAGE_PAGE ( HID_USAGE_PAGE_DESKTOP ) ,\
HID_USAGE ( HID_USAGE_DESKTOP_KEYBOARD ) ,\
@@ -190,7 +184,7 @@ static inline bool tud_hid_mouse_button_release(uint8_t report_id)
HID_OUTPUT ( HID_CONSTANT ) ,\
HID_COLLECTION_END \
-/*------------- Mouse Descriptor Template -------------*/
+// Mouse Report Descriptor Template
#define HID_REPORT_DESC_MOUSE(...) \
HID_USAGE_PAGE ( HID_USAGE_PAGE_DESKTOP ) ,\
HID_USAGE ( HID_USAGE_DESKTOP_MOUSE ) ,\
@@ -230,7 +224,7 @@ static inline bool tud_hid_mouse_button_release(uint8_t report_id)
HID_COLLECTION_END ,\
HID_COLLECTION_END \
-//------------- Consumer Control Report Template -------------//
+// Consumer Control Report Descriptor Template
#define HID_REPORT_DESC_CONSUMER(...) \
HID_USAGE_PAGE ( HID_USAGE_PAGE_CONSUMER ) ,\
HID_USAGE ( HID_USAGE_CONSUMER_CONTROL ) ,\
@@ -245,8 +239,8 @@ static inline bool tud_hid_mouse_button_release(uint8_t report_id)
HID_INPUT ( HID_DATA | HID_ARRAY | HID_ABSOLUTE ) ,\
HID_COLLECTION_END \
-//------------- System Control Report Template -------------//
-/* 0x00 - do nothing
+/* System Control Report Descriptor Template
+ * 0x00 - do nothing
* 0x01 - Power Off
* 0x02 - Standby
* 0x04 - Wake Host
@@ -271,8 +265,8 @@ static inline bool tud_hid_mouse_button_release(uint8_t report_id)
HID_INPUT ( HID_CONSTANT ) ,\
HID_COLLECTION_END \
-//------------- Gamepad Report Template -------------//
-// Gamepad with 16 buttons and 2 joysticks
+// Gamepad Report Descriptor Template
+// with 16 buttons and 2 joysticks with following layout
// | Button Map (2 bytes) | X | Y | Z | Rz
#define HID_REPORT_DESC_GAMEPAD(...) \
HID_USAGE_PAGE ( HID_USAGE_PAGE_DESKTOP ) ,\
diff --git a/src/class/msc/msc_device.h b/src/class/msc/msc_device.h
index e7571ba60..b6cef78b6 100644
--- a/src/class/msc/msc_device.h
+++ b/src/class/msc/msc_device.h
@@ -62,24 +62,6 @@ TU_VERIFY_STATIC(CFG_TUD_MSC_BUFSIZE < UINT16_MAX, "Size is not correct");
#error CFG_TUD_MSC_PRODUCT_REV 4-byte string must be defined
#endif
-
-//--------------------------------------------------------------------+
-// Interface Descriptor Template
-//--------------------------------------------------------------------+
-
-// Length of template descriptor: 23 bytes
-#define TUD_MSC_DESC_LEN (9 + 7 + 7)
-
-// Interface Number, EP Out & EP In address
-#define TUD_MSC_DESCRIPTOR(_itfnum, _stridx, _epout, _epin, _epsize) \
- /* Interface */\
- 0x09, TUSB_DESC_INTERFACE, _itfnum, 0x00, 0x02, TUSB_CLASS_MSC, MSC_SUBCLASS_SCSI, MSC_PROTOCOL_BOT, _stridx,\
- /* Endpoint Out */\
- 0x07, TUSB_DESC_ENDPOINT, _epout, TUSB_XFER_BULK, U16_TO_U8S_LE(_epsize), 0x00,\
- /* Endpoint In */\
- 0x07, TUSB_DESC_ENDPOINT, _epin, TUSB_XFER_BULK, U16_TO_U8S_LE(_epsize), 0x00
-
-
/** \addtogroup ClassDriver_MSC
* @{
* \defgroup MSC_Device Device
@@ -150,7 +132,7 @@ void tud_msc_capacity_cb(uint8_t lun, uint32_t* block_count, uint16_t* block_siz
*/
int32_t tud_msc_scsi_cb (uint8_t lun, uint8_t const scsi_cmd[16], void* buffer, uint16_t bufsize);
-/*------------- Optional callbacks : Could be used by application to free up resources -------------*/
+/*------------- Optional callbacks -------------*/
// Invoked when Read10 command is complete
ATTR_WEAK void tud_msc_read10_complete_cb(uint8_t lun);
@@ -164,6 +146,23 @@ ATTR_WEAK void tud_msc_scsi_complete_cb(uint8_t lun, uint8_t const scsi_cmd[16])
// Hook to make a mass storage device read-only. TODO remove
ATTR_WEAK bool tud_msc_is_writable_cb(uint8_t lun);
+//--------------------------------------------------------------------+
+// Interface Descriptor Template
+//--------------------------------------------------------------------+
+
+// Length of template descriptor: 23 bytes
+#define TUD_MSC_DESC_LEN (9 + 7 + 7)
+
+// Interface Number, EP Out & EP In address
+#define TUD_MSC_DESCRIPTOR(_itfnum, _stridx, _epout, _epin, _epsize) \
+ /* Interface */\
+ 9, TUSB_DESC_INTERFACE, _itfnum, 0, 2, TUSB_CLASS_MSC, MSC_SUBCLASS_SCSI, MSC_PROTOCOL_BOT, _stridx,\
+ /* Endpoint Out */\
+ 7, TUSB_DESC_ENDPOINT, _epout, TUSB_XFER_BULK, U16_TO_U8S_LE(_epsize), 0,\
+ /* Endpoint In */\
+ 7, TUSB_DESC_ENDPOINT, _epin, TUSB_XFER_BULK, U16_TO_U8S_LE(_epsize), 0
+
+
/** @} */
/** @} */
diff --git a/src/device/usbd.h b/src/device/usbd.h
index f90c40aab..b3b66768c 100644
--- a/src/device/usbd.h
+++ b/src/device/usbd.h
@@ -91,6 +91,13 @@ ATTR_WEAK void tud_suspend_cb(bool remote_wakeup_en);
// Invoked when usb bus is resumed
ATTR_WEAK void tud_resume_cb(void);
+//--------------------------------------------------------------------+
+// Interface Descriptor Template
+//--------------------------------------------------------------------+
+
+#define TUD_CONFIG_DESCRIPTOR(_itfcount, _stridx, _total_len, _attribute, _power_ma) \
+ 9, TUSB_DESC_CONFIGURATION, U16_TO_U8S_LE(_total_len), _itfcount, 1, _stridx, TU_BIT(7) | _attribute, (_power_ma)/2
+
#ifdef __cplusplus
}
#endif
diff --git a/src/device/usbd_auto_desc.c b/src/device/usbd_auto_desc.c
index ad90f7d70..b5a0ce4b2 100644
--- a/src/device/usbd_auto_desc.c
+++ b/src/device/usbd_auto_desc.c
@@ -473,47 +473,6 @@ desc_auto_cfg_t const _desc_auto_config_struct =
},
#endif // boot keyboard
- //------------- HID Mouse -------------//
-#if CFG_TUD_HID_MOUSE && CFG_TUD_HID_MOUSE_BOOT
- .hid_mse_boot =
- {
- .itf =
- {
- .bLength = sizeof(tusb_desc_interface_t),
- .bDescriptorType = TUSB_DESC_INTERFACE,
- .bInterfaceNumber = ITF_NUM_HID_BOOT_MSE,
- .bAlternateSetting = 0x00,
- .bNumEndpoints = 1,
- .bInterfaceClass = TUSB_CLASS_HID,
- .bInterfaceSubClass = HID_SUBCLASS_BOOT,
- .bInterfaceProtocol = HID_PROTOCOL_MOUSE,
- .iInterface = 0 // 4 + CFG_TUD_CDC + CFG_TUD_MSC + CFG_TUD_HID_KEYBOARD
- },
-
- .hid_desc =
- {
- .bLength = sizeof(tusb_hid_descriptor_hid_t),
- .bDescriptorType = HID_DESC_TYPE_HID,
- .bcdHID = 0x0111,
- .bCountryCode = HID_Local_NotSupported,
- .bNumDescriptors = 1,
- .bReportType = HID_DESC_TYPE_REPORT,
- .wReportLength = sizeof(_desc_auto_hid_boot_mse_report)
- },
-
- .ep_in =
- {
- .bLength = sizeof(tusb_desc_endpoint_t),
- .bDescriptorType = TUSB_DESC_ENDPOINT,
- .bEndpointAddress = EP_HID_MSE_BOOT,
- .bmAttributes = { .xfer = TUSB_XFER_INTERRUPT },
- .wMaxPacketSize = { .size = EP_HID_MSE_BOOT_SZ },
- .bInterval = 0x0A
- },
- },
-
-#endif // boot mouse
-
#if AUTO_DESC_HID_GENERIC
//------------- HID Generic Multiple report -------------//
.hid_generic =
diff --git a/src/tusb_option.h b/src/tusb_option.h
index 46d2186a0..f365e9c31 100644
--- a/src/tusb_option.h
+++ b/src/tusb_option.h
@@ -169,22 +169,6 @@
#define CFG_TUD_MSC 0
#endif
- #ifndef CFG_TUD_HID_KEYBOARD
- #define CFG_TUD_HID_KEYBOARD 0
- #endif
-
- #ifndef CFG_TUD_HID_MOUSE
- #define CFG_TUD_HID_MOUSE 0
- #endif
-
- #ifndef CFG_TUD_HID_KEYBOARD_BOOT
- #define CFG_TUD_HID_KEYBOARD_BOOT 0
- #endif
-
- #ifndef CFG_TUD_HID_MOUSE_BOOT
- #define CFG_TUD_HID_MOUSE_BOOT 0
- #endif
-
#endif // TUSB_OPT_DEVICE_ENABLED
//--------------------------------------------------------------------