From 89ace295ba2b509dcb42d3df9501134df968c2f4 Mon Sep 17 00:00:00 2001 From: hathach Date: Sun, 28 Apr 2019 00:43:36 +0700 Subject: add TUD_ to HID_REPORT_DESC_* template --- src/class/hid/hid_device.h | 18 +++++++++--------- src/class/msc/msc_device.c | 7 +++++-- 2 files changed, 14 insertions(+), 11 deletions(-) (limited to 'src/class') diff --git a/src/class/hid/hid_device.h b/src/class/hid/hid_device.h index 13a213277..82986127c 100644 --- a/src/class/hid/hid_device.h +++ b/src/class/hid/hid_device.h @@ -64,7 +64,7 @@ uint16_t tud_hid_get_report_cb(uint8_t report_id, hid_report_type_t report_type, 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; +ATTR_WEAK void tud_hid_mode_changed_cb(uint8_t boot_mode); //--------------------------------------------------------------------+ // KEYBOARD API @@ -107,18 +107,18 @@ static inline bool tud_hid_mouse_button_release(uint8_t report_id) * empty if multiple reports is not used * * - Only 1 report: no parameter - * uint8_t const report_desc[] = { HID_REPORT_DESC_KEYBOARD() }; + * uint8_t const report_desc[] = { TUD_HID_REPORT_DESC_KEYBOARD() }; * * - Multiple Reports: "HID_REPORT_ID(ID)," must be passed to template * uint8_t const report_desc[] = * { - * HID_REPORT_DESC_KEYBOARD( HID_REPORT_ID(1), ) , - * HID_REPORT_DESC_MOUSE ( HID_REPORT_ID(2), ) + * TUD_HID_REPORT_DESC_KEYBOARD( HID_REPORT_ID(1), ) , + * TUD_HID_REPORT_DESC_MOUSE ( HID_REPORT_ID(2), ) * }; *--------------------------------------------------------------------*/ // Keyboard Report Descriptor Template -#define HID_REPORT_DESC_KEYBOARD(...) \ +#define TUD_HID_REPORT_DESC_KEYBOARD(...) \ HID_USAGE_PAGE ( HID_USAGE_PAGE_DESKTOP ) ,\ HID_USAGE ( HID_USAGE_DESKTOP_KEYBOARD ) ,\ HID_COLLECTION ( HID_COLLECTION_APPLICATION ) ,\ @@ -159,7 +159,7 @@ static inline bool tud_hid_mouse_button_release(uint8_t report_id) HID_COLLECTION_END \ // Mouse Report Descriptor Template -#define HID_REPORT_DESC_MOUSE(...) \ +#define TUD_HID_REPORT_DESC_MOUSE(...) \ HID_USAGE_PAGE ( HID_USAGE_PAGE_DESKTOP ) ,\ HID_USAGE ( HID_USAGE_DESKTOP_MOUSE ) ,\ HID_COLLECTION ( HID_COLLECTION_APPLICATION ) ,\ @@ -199,7 +199,7 @@ static inline bool tud_hid_mouse_button_release(uint8_t report_id) HID_COLLECTION_END \ // Consumer Control Report Descriptor Template -#define HID_REPORT_DESC_CONSUMER(...) \ +#define TUD_HID_REPORT_DESC_CONSUMER(...) \ HID_USAGE_PAGE ( HID_USAGE_PAGE_CONSUMER ) ,\ HID_USAGE ( HID_USAGE_CONSUMER_CONTROL ) ,\ HID_COLLECTION ( HID_COLLECTION_APPLICATION ) ,\ @@ -219,7 +219,7 @@ static inline bool tud_hid_mouse_button_release(uint8_t report_id) * 0x02 - Standby * 0x04 - Wake Host */ -#define HID_REPORT_DESC_SYSTEM_CONTROL(...) \ +#define TUD_HID_REPORT_DESC_SYSTEM_CONTROL(...) \ HID_USAGE_PAGE ( HID_USAGE_PAGE_DESKTOP ) ,\ HID_USAGE ( HID_USAGE_DESKTOP_SYSTEM_CONTROL ) ,\ HID_COLLECTION ( HID_COLLECTION_APPLICATION ) ,\ @@ -242,7 +242,7 @@ static inline bool tud_hid_mouse_button_release(uint8_t report_id) // 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(...) \ +#define TUD_HID_REPORT_DESC_GAMEPAD(...) \ HID_USAGE_PAGE ( HID_USAGE_PAGE_DESKTOP ) ,\ HID_USAGE ( HID_USAGE_DESKTOP_GAMEPAD ) ,\ HID_COLLECTION ( HID_COLLECTION_APPLICATION ) ,\ diff --git a/src/class/msc/msc_device.c b/src/class/msc/msc_device.c index e2cf92324..360503d9b 100644 --- a/src/class/msc/msc_device.c +++ b/src/class/msc/msc_device.c @@ -163,9 +163,12 @@ bool mscd_control_request(uint8_t rhport, tusb_control_request_t const * p_reque case MSC_REQ_GET_MAX_LUN: { + uint8_t maxlun = 1; + if (tud_msc_maxlun_cb) maxlun = tud_msc_maxlun_cb(); + TU_VERIFY(maxlun); + // MAX LUN is minus 1 by specs - uint8_t maxlun = 0; - if (tud_msc_maxlun_cb) maxlun = tud_msc_maxlun_cb() -1; + maxlun--; usbd_control_xfer(rhport, p_request, &maxlun, 1); } -- cgit v1.3.1 From 0b6999a28e5dde91e1a911523e1f633f8983b504 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 1 May 2019 16:53:35 +0700 Subject: add tud_hid_set_idle_cb - rename tud_hid_mode_changed_cb to tud_hid_mode_changed_cb - add mouse ac pan descriptor template support --- src/class/hid/hid.h | 2 +- src/class/hid/hid_device.c | 23 ++++++++++----- src/class/hid/hid_device.h | 73 ++++++++++++++++++++++++++-------------------- 3 files changed, 58 insertions(+), 40 deletions(-) (limited to 'src/class') diff --git a/src/class/hid/hid.h b/src/class/hid/hid.h index 637f13295..8cc8fc14f 100644 --- a/src/class/hid/hid.h +++ b/src/class/hid/hid.h @@ -155,7 +155,7 @@ typedef struct ATTR_PACKED int8_t x; /**< Current delta x movement of the mouse. */ int8_t y; /**< Current delta y movement on the mouse. */ int8_t wheel; /**< Current delta wheel movement on the mouse. */ -// int8_t pan; + int8_t pan; // using AC Pan } hid_mouse_report_t; /// Standard Mouse Buttons Bitmap diff --git a/src/class/hid/hid_device.c b/src/class/hid/hid_device.c index 78def53f0..688aa7dea 100644 --- a/src/class/hid/hid_device.c +++ b/src/class/hid/hid_device.c @@ -47,6 +47,9 @@ typedef struct { uint8_t itf_num; uint8_t ep_in; + uint8_t ep_out; // optional + + uint8_t boot_protocol; // Boot mouse or keyboard bool boot_mode; @@ -132,16 +135,15 @@ bool tud_hid_keyboard_report(uint8_t report_id, uint8_t modifier, uint8_t keycod //--------------------------------------------------------------------+ // MOUSE APPLICATION API //--------------------------------------------------------------------+ -bool tud_hid_mouse_report(uint8_t report_id, uint8_t buttons, int8_t x, int8_t y, int8_t scroll, int8_t pan) +bool tud_hid_mouse_report(uint8_t report_id, uint8_t buttons, int8_t x, int8_t y, int8_t vertical, int8_t horizontal) { - (void) pan; hid_mouse_report_t report = { .buttons = buttons, .x = x, .y = y, - .wheel = scroll, - //.pan = pan + .wheel = vertical, + .pan = horizontal }; uint8_t itf = 0; @@ -158,12 +160,12 @@ bool tud_hid_mouse_move(uint8_t report_id, int8_t x, int8_t y) return tud_hid_mouse_report(report_id, button, x, y, 0, 0); } -bool tud_hid_mouse_scroll(uint8_t report_id, int8_t scroll, int8_t pan) +bool tud_hid_mouse_scroll(uint8_t report_id, int8_t vertical, int8_t horizontal) { uint8_t itf = 0; uint8_t const button = _hidd_itf[itf].mouse_button; - return tud_hid_mouse_report(report_id, button, 0, 0, scroll, pan); + return tud_hid_mouse_report(report_id, button, 0, 0, vertical, horizontal); } //--------------------------------------------------------------------+ @@ -257,8 +259,13 @@ bool hidd_control_request(uint8_t rhport, tusb_control_request_t const * p_reque break; case HID_REQ_CONTROL_SET_IDLE: - // TODO idle rate of report p_hid->idle_rate = tu_u16_high(p_request->wValue); + if ( tud_hid_set_idle_cb ) + { + // stall request if callback return false + if ( !tud_hid_set_idle_cb(p_hid->idle_rate) ) return false; + } + usbd_control_status(rhport, p_request); break; @@ -277,7 +284,7 @@ bool hidd_control_request(uint8_t rhport, tusb_control_request_t const * p_reque case HID_REQ_CONTROL_SET_PROTOCOL: p_hid->boot_mode = 1 - p_request->wValue; // 0 is Boot, 1 is Report protocol - if (tud_hid_mode_changed_cb) tud_hid_mode_changed_cb(p_hid->boot_mode); + if (tud_hid_boot_mode_cb) tud_hid_boot_mode_cb(p_hid->boot_mode); usbd_control_status(rhport, p_request); break; diff --git a/src/class/hid/hid_device.h b/src/class/hid/hid_device.h index 82986127c..95ff9825c 100644 --- a/src/class/hid/hid_device.h +++ b/src/class/hid/hid_device.h @@ -39,7 +39,6 @@ // Class Driver Default Configure & Validation //--------------------------------------------------------------------+ - //--------------------------------------------------------------------+ // Application API //--------------------------------------------------------------------+ @@ -64,7 +63,11 @@ uint16_t tud_hid_get_report_cb(uint8_t report_id, hid_report_type_t report_type, 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 -ATTR_WEAK void tud_hid_mode_changed_cb(uint8_t boot_mode); +ATTR_WEAK void tud_hid_boot_mode_cb(uint8_t boot_mode); + +// Invoked when host send SET_IDLE request +// return false will stall the request +ATTR_WEAK bool tud_hid_set_idle_cb(uint8_t idle_rate); //--------------------------------------------------------------------+ // KEYBOARD API @@ -85,9 +88,9 @@ static inline bool tud_hid_keyboard_key_release(uint8_t report_id) // layout report as defined by hid_mouse_report_t //--------------------------------------------------------------------+ -bool tud_hid_mouse_report(uint8_t report_id, uint8_t buttons, int8_t x, int8_t y, int8_t scroll, int8_t pan); +bool tud_hid_mouse_report(uint8_t report_id, uint8_t buttons, int8_t x, int8_t y, int8_t vertical, int8_t horizontal); bool tud_hid_mouse_move(uint8_t report_id, int8_t x, int8_t y); -bool tud_hid_mouse_scroll(uint8_t report_id, int8_t scroll, int8_t pan); +bool tud_hid_mouse_scroll(uint8_t report_id, int8_t vertical, int8_t horizontal); static inline bool tud_hid_mouse_button_press(uint8_t report_id, uint8_t buttons) { @@ -160,42 +163,50 @@ static inline bool tud_hid_mouse_button_release(uint8_t report_id) // Mouse Report Descriptor Template #define TUD_HID_REPORT_DESC_MOUSE(...) \ - HID_USAGE_PAGE ( HID_USAGE_PAGE_DESKTOP ) ,\ - HID_USAGE ( HID_USAGE_DESKTOP_MOUSE ) ,\ - HID_COLLECTION ( HID_COLLECTION_APPLICATION ) ,\ + HID_USAGE_PAGE ( HID_USAGE_PAGE_DESKTOP ) ,\ + HID_USAGE ( HID_USAGE_DESKTOP_MOUSE ) ,\ + HID_COLLECTION ( HID_COLLECTION_APPLICATION ) ,\ __VA_ARGS__ \ - HID_USAGE ( HID_USAGE_DESKTOP_POINTER ) ,\ - HID_COLLECTION ( HID_COLLECTION_PHYSICAL ) ,\ - HID_USAGE_PAGE ( HID_USAGE_PAGE_BUTTON ) ,\ - HID_USAGE_MIN ( 1 ) ,\ - HID_USAGE_MAX ( 3 ) ,\ - HID_LOGICAL_MIN ( 0 ) ,\ - HID_LOGICAL_MAX ( 1 ) ,\ - /* Left, Right, Middle, Backward, Forward mouse buttons */ \ - HID_REPORT_COUNT ( 3 ) ,\ - HID_REPORT_SIZE ( 1 ) ,\ - HID_INPUT ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE ) ,\ + HID_USAGE ( HID_USAGE_DESKTOP_POINTER ) ,\ + HID_COLLECTION ( HID_COLLECTION_PHYSICAL ) ,\ + HID_USAGE_PAGE ( HID_USAGE_PAGE_BUTTON ) ,\ + HID_USAGE_MIN ( 1 ) ,\ + HID_USAGE_MAX ( 5 ) ,\ + HID_LOGICAL_MIN ( 0 ) ,\ + HID_LOGICAL_MAX ( 1 ) ,\ + /* Left, Right, Middle, Backward, Forward buttons */ \ + HID_REPORT_COUNT( 5 ) ,\ + HID_REPORT_SIZE ( 1 ) ,\ + HID_INPUT ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE ) ,\ /* 3 bit padding */ \ - HID_REPORT_COUNT ( 1 ) ,\ - HID_REPORT_SIZE ( 5 ) ,\ - HID_INPUT ( HID_CONSTANT ) ,\ - HID_USAGE_PAGE ( HID_USAGE_PAGE_DESKTOP ) ,\ + HID_REPORT_COUNT( 1 ) ,\ + HID_REPORT_SIZE ( 3 ) ,\ + HID_INPUT ( HID_CONSTANT ) ,\ + HID_USAGE_PAGE ( HID_USAGE_PAGE_DESKTOP ) ,\ /* X, Y position [-127, 127] */ \ - HID_USAGE ( HID_USAGE_DESKTOP_X ) ,\ - HID_USAGE ( HID_USAGE_DESKTOP_Y ) ,\ - HID_LOGICAL_MIN ( 0x81 ) ,\ - HID_LOGICAL_MAX ( 0x7f ) ,\ - HID_REPORT_COUNT ( 2 ) ,\ - HID_REPORT_SIZE ( 8 ) ,\ - HID_INPUT ( HID_DATA | HID_VARIABLE | HID_RELATIVE ) ,\ - /* Mouse scroll [-127, 127] */ \ + HID_USAGE ( HID_USAGE_DESKTOP_X ) ,\ + HID_USAGE ( HID_USAGE_DESKTOP_Y ) ,\ + HID_LOGICAL_MIN ( 0x81 ) ,\ + HID_LOGICAL_MAX ( 0x7f ) ,\ + HID_REPORT_COUNT( 2 ) ,\ + HID_REPORT_SIZE ( 8 ) ,\ + HID_INPUT ( HID_DATA | HID_VARIABLE | HID_RELATIVE ) ,\ + /* Verital wheel scroll [-127, 127] */ \ HID_USAGE ( HID_USAGE_DESKTOP_WHEEL ) ,\ HID_LOGICAL_MIN ( 0x81 ) ,\ HID_LOGICAL_MAX ( 0x7f ) ,\ HID_REPORT_COUNT( 1 ) ,\ HID_REPORT_SIZE ( 8 ) ,\ HID_INPUT ( HID_DATA | HID_VARIABLE | HID_RELATIVE ) ,\ - HID_COLLECTION_END ,\ + HID_USAGE_PAGE ( HID_USAGE_PAGE_CONSUMER ), \ + /* Horizontal wheel scroll [-127, 127] */ \ + HID_USAGE_N ( HID_USAGE_CONSUMER_AC_PAN, 2 ), \ + HID_LOGICAL_MIN ( 0x81 ), \ + HID_LOGICAL_MAX ( 0x7f ), \ + HID_REPORT_COUNT( 1 ), \ + HID_REPORT_SIZE ( 8 ), \ + HID_INPUT ( HID_DATA | HID_VARIABLE | HID_RELATIVE ), \ + HID_COLLECTION_END , \ HID_COLLECTION_END \ // Consumer Control Report Descriptor Template -- cgit v1.3.1 From 84f81f6b213305207fa6d4d01ce6911f5ad3cf66 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 1 May 2019 17:05:25 +0700 Subject: simplify hid keyboard & mouse report to one API each --- examples/device/cdc_msc_hid/src/main.c | 6 +++-- examples/device/cdc_msc_hid_freertos/src/main.c | 6 +++-- src/class/hid/hid_device.c | 32 +++---------------------- src/class/hid/hid_device.h | 32 ++++++------------------- 4 files changed, 18 insertions(+), 58 deletions(-) (limited to 'src/class') diff --git a/examples/device/cdc_msc_hid/src/main.c b/examples/device/cdc_msc_hid/src/main.c index 8087adbb9..1a954f21a 100644 --- a/examples/device/cdc_msc_hid/src/main.c +++ b/examples/device/cdc_msc_hid/src/main.c @@ -196,7 +196,9 @@ void hid_task(void) if ( btn ) { int8_t const delta = 5; - tud_hid_mouse_move(REPORT_ID_MOUSE, delta, delta); // right + down + + // no button, right + down, no scroll pan + tud_hid_mouse_report(REPORT_ID_MOUSE, 0x00, delta, delta, 0, 0); // delay a bit before attempt to send keyboard report board_delay(2); @@ -220,7 +222,7 @@ void hid_task(void) }else { // send empty key report if previously has key pressed - if (has_key) tud_hid_keyboard_key_release(REPORT_ID_KEYBOARD); + if (has_key) tud_hid_keyboard_report(REPORT_ID_KEYBOARD, 0, NULL); has_key = false; } } diff --git a/examples/device/cdc_msc_hid_freertos/src/main.c b/examples/device/cdc_msc_hid_freertos/src/main.c index 2cd34dddc..e4471242f 100644 --- a/examples/device/cdc_msc_hid_freertos/src/main.c +++ b/examples/device/cdc_msc_hid_freertos/src/main.c @@ -226,7 +226,9 @@ void hid_task(void* params) if ( btn ) { int8_t const delta = 5; - tud_hid_mouse_move(REPORT_ID_MOUSE, delta, delta); // right + down + + // no button, right + down, no scroll pan + tud_hid_mouse_report(REPORT_ID_MOUSE, 0x00, delta, delta, 0, 0); // delay a bit before attempt to send keyboard report vTaskDelay(pdMS_TO_TICKS(2)); @@ -250,7 +252,7 @@ void hid_task(void* params) }else { // send empty key report if previously has key pressed - if (has_key) tud_hid_keyboard_key_release(REPORT_ID_KEYBOARD); + if (has_key) tud_hid_keyboard_report(REPORT_ID_KEYBOARD, 0, NULL); has_key = false; } } diff --git a/src/class/hid/hid_device.c b/src/class/hid/hid_device.c index 688aa7dea..46e26bd10 100644 --- a/src/class/hid/hid_device.c +++ b/src/class/hid/hid_device.c @@ -47,16 +47,11 @@ typedef struct { uint8_t itf_num; uint8_t ep_in; - uint8_t ep_out; // optional - - + uint8_t ep_out; // optional uint8_t boot_protocol; // Boot mouse or keyboard - bool boot_mode; - + bool boot_mode; // default = false (Report) + uint8_t idle_rate; // up to application to handle idle rate uint16_t reprot_desc_len; - uint8_t idle_rate; // Idle Rate = 0 : only send report if there is changes, i.e skip duplication - // Idle Rate > 0 : skip duplication, but send at least 1 report every idle rate (in unit of 4 ms). - uint8_t mouse_button; // caching button for using with tud_hid_mouse_ API CFG_TUSB_MEM_ALIGN uint8_t report_buf[CFG_TUD_HID_BUFSIZE]; }hidd_interface_t; @@ -101,7 +96,6 @@ bool tud_hid_report(uint8_t report_id, void const* report, uint8_t len) memcpy(p_hid->report_buf, report, len); } - // TODO skip duplication ? and or idle rate return dcd_edpt_xfer(TUD_OPT_RHPORT, p_hid->ep_in, p_hid->report_buf, len + (report_id ? 1 : 0) ); } @@ -128,7 +122,6 @@ bool tud_hid_keyboard_report(uint8_t report_id, uint8_t modifier, uint8_t keycod tu_memclr(report.keycode, 6); } - // TODO skip duplication ? and or idle rate return tud_hid_report(report_id, &report, sizeof(report)); } @@ -146,28 +139,9 @@ bool tud_hid_mouse_report(uint8_t report_id, uint8_t buttons, int8_t x, int8_t y .pan = horizontal }; - uint8_t itf = 0; - _hidd_itf[itf].mouse_button = buttons; - return tud_hid_report(report_id, &report, sizeof(report)); } -bool tud_hid_mouse_move(uint8_t report_id, int8_t x, int8_t y) -{ - uint8_t itf = 0; - uint8_t const button = _hidd_itf[itf].mouse_button; - - return tud_hid_mouse_report(report_id, button, x, y, 0, 0); -} - -bool tud_hid_mouse_scroll(uint8_t report_id, int8_t vertical, int8_t horizontal) -{ - uint8_t itf = 0; - uint8_t const button = _hidd_itf[itf].mouse_button; - - return tud_hid_mouse_report(report_id, button, 0, 0, vertical, horizontal); -} - //--------------------------------------------------------------------+ // USBD-CLASS API //--------------------------------------------------------------------+ diff --git a/src/class/hid/hid_device.h b/src/class/hid/hid_device.h index 95ff9825c..c93055f06 100644 --- a/src/class/hid/hid_device.h +++ b/src/class/hid/hid_device.h @@ -65,42 +65,24 @@ void tud_hid_set_report_cb(uint8_t report_id, hid_report_type_t report_type, uin // Invoked when host switch mode Boot <-> Report via SET_PROTOCOL request ATTR_WEAK void tud_hid_boot_mode_cb(uint8_t boot_mode); -// Invoked when host send SET_IDLE request -// return false will stall the request +// Invoked when host send SET_IDLE request. return false will stall the request +// - Idle Rate = 0 : only send report if there is changes, i.e skip duplication +// - Idle Rate > 0 : skip duplication, but send at least 1 report every idle rate (in unit of 4 ms). ATTR_WEAK bool tud_hid_set_idle_cb(uint8_t idle_rate); //--------------------------------------------------------------------+ -// KEYBOARD API -// Convenient helper to send keyboard report if application use standard/boot -// layout report as defined by hid_keyboard_report_t +// KEYBOARD: convenient helper to send keyboard report if application +// use template layout report as defined by hid_keyboard_report_t //--------------------------------------------------------------------+ bool tud_hid_keyboard_report(uint8_t report_id, uint8_t modifier, uint8_t keycode[6]); -static inline bool tud_hid_keyboard_key_release(uint8_t report_id) -{ - return tud_hid_keyboard_report(report_id, 0, NULL); -} - //--------------------------------------------------------------------+ -// MOUSE API -// Convenient helper to send mouse report if application use standard/boot -// layout report as defined by hid_mouse_report_t +// MOUSE: convenient helper to send mouse report if application +// use template layout report as defined by hid_mouse_report_t //--------------------------------------------------------------------+ bool tud_hid_mouse_report(uint8_t report_id, uint8_t buttons, int8_t x, int8_t y, int8_t vertical, int8_t horizontal); -bool tud_hid_mouse_move(uint8_t report_id, int8_t x, int8_t y); -bool tud_hid_mouse_scroll(uint8_t report_id, int8_t vertical, int8_t horizontal); - -static inline bool tud_hid_mouse_button_press(uint8_t report_id, uint8_t buttons) -{ - return tud_hid_mouse_report(report_id, buttons, 0, 0, 0, 0); -} - -static inline bool tud_hid_mouse_button_release(uint8_t report_id) -{ - return tud_hid_mouse_report(report_id, 0, 0, 0, 0, 0); -} /* --------------------------------------------------------------------+ * HID Report Descriptor Template -- cgit v1.3.1 From 9fa5c8025faddfa68b506af3bf2b6383f70c8323 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 1 May 2019 17:07:33 +0700 Subject: clean up --- src/class/hid/hid_device.h | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) (limited to 'src/class') diff --git a/src/class/hid/hid_device.h b/src/class/hid/hid_device.h index c93055f06..d2eeebcf3 100644 --- a/src/class/hid/hid_device.h +++ b/src/class/hid/hid_device.h @@ -52,7 +52,17 @@ 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) -------------*/ +// KEYBOARD: convenient helper to send keyboard report if application +// use template layout report as defined by hid_keyboard_report_t +bool tud_hid_keyboard_report(uint8_t report_id, uint8_t modifier, uint8_t keycode[6]); + +// MOUSE: convenient helper to send mouse report if application +// use template layout report as defined by hid_mouse_report_t +bool tud_hid_mouse_report(uint8_t report_id, uint8_t buttons, int8_t x, int8_t y, int8_t vertical, int8_t horizontal); + +//--------------------------------------------------------------------+ +// Callbacks (Weak is optional) +//--------------------------------------------------------------------+ // Invoked when receiving GET_REPORT control request // Application must fill buffer report's content and return its length. @@ -70,20 +80,6 @@ ATTR_WEAK void tud_hid_boot_mode_cb(uint8_t boot_mode); // - Idle Rate > 0 : skip duplication, but send at least 1 report every idle rate (in unit of 4 ms). ATTR_WEAK bool tud_hid_set_idle_cb(uint8_t idle_rate); -//--------------------------------------------------------------------+ -// KEYBOARD: convenient helper to send keyboard report if application -// use template layout report as defined by hid_keyboard_report_t -//--------------------------------------------------------------------+ - -bool tud_hid_keyboard_report(uint8_t report_id, uint8_t modifier, uint8_t keycode[6]); - -//--------------------------------------------------------------------+ -// MOUSE: convenient helper to send mouse report if application -// use template layout report as defined by hid_mouse_report_t -//--------------------------------------------------------------------+ - -bool tud_hid_mouse_report(uint8_t report_id, uint8_t buttons, int8_t x, int8_t y, int8_t vertical, int8_t horizontal); - /* --------------------------------------------------------------------+ * HID Report Descriptor Template * -- cgit v1.3.1 From 90fdae94f8f0f86af5e7be3b14561baae1abb2fd Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 1 May 2019 18:21:08 +0700 Subject: clean up --- examples/device/cdc_msc_hid/src/tusb_config.h | 15 ++++++--------- examples/device/cdc_msc_hid_freertos/src/tusb_config.h | 15 ++++++--------- examples/device/msc_dual_lun/src/tusb_config.h | 4 +--- src/class/hid/hid_device.c | 2 +- 4 files changed, 14 insertions(+), 22 deletions(-) (limited to 'src/class') diff --git a/examples/device/cdc_msc_hid/src/tusb_config.h b/examples/device/cdc_msc_hid/src/tusb_config.h index 400afb620..c1210152e 100644 --- a/examples/device/cdc_msc_hid/src/tusb_config.h +++ b/examples/device/cdc_msc_hid/src/tusb_config.h @@ -77,17 +77,13 @@ #define CFG_TUD_MIDI 0 #define CFG_TUD_CUSTOM_CLASS 0 -//-------------------------------------------------------------------- -// CDC -//-------------------------------------------------------------------- +//------------- CDC -------------// // FIFO size of CDC TX and RX #define CFG_TUD_CDC_RX_BUFSIZE 64 #define CFG_TUD_CDC_TX_BUFSIZE 64 -//-------------------------------------------------------------------- -// MSC -//-------------------------------------------------------------------- +//------------- MSC -------------// // Buffer size of Device Mass storage #define CFG_TUD_MSC_BUFSIZE 512 @@ -101,9 +97,10 @@ // Product revision string included in Inquiry response, max 4 bytes #define CFG_TUD_MSC_PRODUCT_REV "1.0" -//-------------------------------------------------------------------- -// HID -//-------------------------------------------------------------------- +//------------- HID -------------// + +// Should be sufficient to hold ID (if any) + Data +#define CFG_TUD_HID_BUFSIZE 16 #ifdef __cplusplus } diff --git a/examples/device/cdc_msc_hid_freertos/src/tusb_config.h b/examples/device/cdc_msc_hid_freertos/src/tusb_config.h index 834d01216..04ffaafca 100644 --- a/examples/device/cdc_msc_hid_freertos/src/tusb_config.h +++ b/examples/device/cdc_msc_hid_freertos/src/tusb_config.h @@ -77,17 +77,13 @@ #define CFG_TUD_MIDI 0 #define CFG_TUD_CUSTOM_CLASS 0 -//-------------------------------------------------------------------- -// CDC -//-------------------------------------------------------------------- +//------------- CDC -------------// // FIFO size of CDC TX and RX #define CFG_TUD_CDC_RX_BUFSIZE 64 #define CFG_TUD_CDC_TX_BUFSIZE 64 -//-------------------------------------------------------------------- -// MSC -//-------------------------------------------------------------------- +//------------- MSC -------------// // Buffer size of Device Mass storage #define CFG_TUD_MSC_BUFSIZE 512 @@ -101,9 +97,10 @@ // Product revision string included in Inquiry response, max 4 bytes #define CFG_TUD_MSC_PRODUCT_REV "1.0" -//-------------------------------------------------------------------- -// HID -//-------------------------------------------------------------------- +//------------- HID -------------// + +// Should be sufficient to hold ID (if any) + Data +#define CFG_TUD_HID_BUFSIZE 16 #ifdef __cplusplus } diff --git a/examples/device/msc_dual_lun/src/tusb_config.h b/examples/device/msc_dual_lun/src/tusb_config.h index b25510f25..492eba5f1 100644 --- a/examples/device/msc_dual_lun/src/tusb_config.h +++ b/examples/device/msc_dual_lun/src/tusb_config.h @@ -76,9 +76,7 @@ #define CFG_TUD_MIDI 0 #define CFG_TUD_CUSTOM_CLASS 0 -//-------------------------------------------------------------------- -// MSC -//-------------------------------------------------------------------- +//------------- MSC -------------// // Buffer size of Device Mass storage #define CFG_TUD_MSC_BUFSIZE 512 diff --git a/src/class/hid/hid_device.c b/src/class/hid/hid_device.c index 46e26bd10..b1a4efe9a 100644 --- a/src/class/hid/hid_device.c +++ b/src/class/hid/hid_device.c @@ -47,7 +47,7 @@ typedef struct { uint8_t itf_num; uint8_t ep_in; - uint8_t ep_out; // optional + uint8_t ep_out; // optional Out endpoint uint8_t boot_protocol; // Boot mouse or keyboard bool boot_mode; // default = false (Report) uint8_t idle_rate; // up to application to handle idle rate -- cgit v1.3.1 From c006f3dbce7820bbdc2c1736ba0cb4a4e629f0b6 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 1 May 2019 19:29:56 +0700 Subject: adding optional ep out for hid refactor usbd_open_edpt_pair --- src/class/cdc/cdc_device.c | 8 +++----- src/class/hid/hid_device.c | 14 +++++--------- src/class/msc/msc_device.c | 5 ++--- src/device/usbd.c | 20 ++++++++++---------- src/device/usbd_pvt.h | 3 +-- 5 files changed, 21 insertions(+), 29 deletions(-) (limited to 'src/class') diff --git a/src/class/cdc/cdc_device.c b/src/class/cdc/cdc_device.c index c194aacd8..5e0f4e3f7 100644 --- a/src/class/cdc/cdc_device.c +++ b/src/class/cdc/cdc_device.c @@ -289,14 +289,12 @@ bool cdcd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t (TUSB_CLASS_CDC_DATA == ((tusb_desc_interface_t const *) p_desc)->bInterfaceClass) ) { // next to endpoint descriptor - (*p_length) += tu_desc_len(p_desc); p_desc = tu_desc_next(p_desc); - // Open endpoint pair with usbd helper - tusb_desc_endpoint_t const *p_desc_ep = (tusb_desc_endpoint_t const *) p_desc; - TU_ASSERT( usbd_open_edpt_pair(rhport, p_desc_ep, TUSB_XFER_BULK, &p_cdc->ep_out, &p_cdc->ep_in) ); + // Open endpoint pair + TU_ASSERT( usbd_open_edpt_pair(rhport, p_desc, 2, TUSB_XFER_BULK, &p_cdc->ep_out, &p_cdc->ep_in) ); - (*p_length) += 2*sizeof(tusb_desc_endpoint_t); + (*p_length) += sizeof(tusb_desc_interface_t) + 2*sizeof(tusb_desc_endpoint_t); } // Prepare for incoming data diff --git a/src/class/hid/hid_device.c b/src/class/hid/hid_device.c index b1a4efe9a..87b1b2c7b 100644 --- a/src/class/hid/hid_device.c +++ b/src/class/hid/hid_device.c @@ -160,6 +160,10 @@ bool hidd_open(uint8_t rhport, tusb_desc_interface_t const * desc_itf, uint16_t { uint8_t const *p_desc = (uint8_t const *) desc_itf; + // TODO support multiple HID interface + uint8_t itf = 0; + hidd_interface_t * p_hid = &_hidd_itf[itf]; + //------------- HID descriptor -------------// p_desc = tu_desc_next(p_desc); tusb_hid_descriptor_hid_t const *desc_hid = (tusb_hid_descriptor_hid_t const *) p_desc; @@ -167,20 +171,12 @@ bool hidd_open(uint8_t rhport, tusb_desc_interface_t const * desc_itf, uint16_t //------------- Endpoint Descriptor -------------// p_desc = tu_desc_next(p_desc); - tusb_desc_endpoint_t const *desc_edpt = (tusb_desc_endpoint_t const *) p_desc; - TU_ASSERT(TUSB_DESC_ENDPOINT == desc_edpt->bDescriptorType); - - TU_ASSERT(dcd_edpt_open(rhport, desc_edpt)); - - // TODO support multiple HID interface - uint8_t itf = 0; - hidd_interface_t * p_hid = &_hidd_itf[itf]; + TU_ASSERT(usbd_open_edpt_pair(rhport, p_desc, desc_itf->bNumEndpoints, TUSB_XFER_INTERRUPT, &p_hid->ep_out, &p_hid->ep_in)); if ( desc_itf->bInterfaceSubClass == HID_SUBCLASS_BOOT ) p_hid->boot_protocol = desc_itf->bInterfaceProtocol; p_hid->boot_mode = false; // default mode is REPORT p_hid->itf_num = desc_itf->bInterfaceNumber; - p_hid->ep_in = desc_edpt->bEndpointAddress; p_hid->reprot_desc_len = desc_hid->wReportLength; *p_len = sizeof(tusb_desc_interface_t) + sizeof(tusb_hid_descriptor_hid_t) + desc_itf->bNumEndpoints*sizeof(tusb_desc_endpoint_t); diff --git a/src/class/msc/msc_device.c b/src/class/msc/msc_device.c index 360503d9b..1d5a3fc26 100644 --- a/src/class/msc/msc_device.c +++ b/src/class/msc/msc_device.c @@ -135,9 +135,8 @@ bool mscd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t mscd_interface_t * p_msc = &_mscd_itf; - // Open endpoint pair with usbd helper - tusb_desc_endpoint_t const *p_desc_ep = (tusb_desc_endpoint_t const *) tu_desc_next( itf_desc ); - TU_ASSERT( usbd_open_edpt_pair(rhport, p_desc_ep, TUSB_XFER_BULK, &p_msc->ep_out, &p_msc->ep_in) ); + // Open endpoint pair + TU_ASSERT( usbd_open_edpt_pair(rhport, tu_desc_next(itf_desc), 2, TUSB_XFER_BULK, &p_msc->ep_out, &p_msc->ep_in) ); p_msc->itf_num = itf_desc->bInterfaceNumber; (*p_len) = sizeof(tusb_desc_interface_t) + 2*sizeof(tusb_desc_endpoint_t); diff --git a/src/device/usbd.c b/src/device/usbd.c index f02b21208..d811c471a 100644 --- a/src/device/usbd.c +++ b/src/device/usbd.c @@ -702,25 +702,25 @@ void dcd_event_xfer_complete (uint8_t rhport, uint8_t ep_addr, uint32_t xferred_ // Helper //--------------------------------------------------------------------+ -// Helper to parse an pair of endpoint descriptors (IN & OUT) -bool usbd_open_edpt_pair(uint8_t rhport, tusb_desc_endpoint_t const* ep_desc, uint8_t xfer_type, uint8_t* ep_out, uint8_t* ep_in) +// Parse consecutive endpoint descriptors (IN & OUT) +bool usbd_open_edpt_pair(uint8_t rhport, uint8_t const* p_desc, uint8_t ep_count, uint8_t xfer_type, uint8_t* ep_out, uint8_t* ep_in) { - for(int i=0; i<2; i++) + for(int i=0; ibDescriptorType && - xfer_type == ep_desc->bmAttributes.xfer ); + tusb_desc_endpoint_t const * desc_ep = (tusb_desc_endpoint_t const *) p_desc; - TU_ASSERT(dcd_edpt_open(rhport, ep_desc)); + TU_VERIFY(TUSB_DESC_ENDPOINT == desc_ep->bDescriptorType && xfer_type == desc_ep->bmAttributes.xfer); + TU_ASSERT(dcd_edpt_open(rhport, desc_ep)); - if ( tu_edpt_dir(ep_desc->bEndpointAddress) == TUSB_DIR_IN ) + if ( tu_edpt_dir(desc_ep->bEndpointAddress) == TUSB_DIR_IN ) { - (*ep_in) = ep_desc->bEndpointAddress; + (*ep_in) = desc_ep->bEndpointAddress; }else { - (*ep_out) = ep_desc->bEndpointAddress; + (*ep_out) = desc_ep->bEndpointAddress; } - ep_desc = (tusb_desc_endpoint_t const *) tu_desc_next(ep_desc); + p_desc = tu_desc_next(p_desc); } return true; diff --git a/src/device/usbd_pvt.h b/src/device/usbd_pvt.h index 8518ee209..fb3672359 100644 --- a/src/device/usbd_pvt.h +++ b/src/device/usbd_pvt.h @@ -53,9 +53,8 @@ bool usbd_edpt_stalled(uint8_t rhport, uint8_t ep_addr); /*------------------------------------------------------------------*/ /* Helper *------------------------------------------------------------------*/ -// helper to parse an pair of In and Out endpoint descriptors. They must be consecutive -bool usbd_open_edpt_pair(uint8_t rhport, tusb_desc_endpoint_t const* p_desc_ep, uint8_t xfer_type, uint8_t* ep_out, uint8_t* ep_in); +bool usbd_open_edpt_pair(uint8_t rhport, uint8_t const* p_desc, uint8_t ep_count, uint8_t xfer_type, uint8_t* ep_out, uint8_t* ep_in); void usbd_defer_func( osal_task_func_t func, void* param, bool in_isr ); -- cgit v1.3.1 From 205077876380cc63f34c99c559c41ed3a2eaaa81 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 1 May 2019 20:41:26 +0700 Subject: clean up --- src/class/cdc/cdc_device.c | 2 +- src/class/msc/msc_device.c | 12 ++++-------- src/portable/nordic/nrf5x/dcd_nrf5x.c | 4 ++-- 3 files changed, 7 insertions(+), 11 deletions(-) (limited to 'src/class') diff --git a/src/class/cdc/cdc_device.c b/src/class/cdc/cdc_device.c index 5e0f4e3f7..7a0380e6b 100644 --- a/src/class/cdc/cdc_device.c +++ b/src/class/cdc/cdc_device.c @@ -71,7 +71,7 @@ typedef struct //--------------------------------------------------------------------+ // INTERNAL OBJECT & FUNCTION DECLARATION //--------------------------------------------------------------------+ -CFG_TUSB_MEM_SECTION static cdcd_interface_t _cdcd_itf[CFG_TUD_CDC] = { { 0 } }; +CFG_TUSB_MEM_SECTION static cdcd_interface_t _cdcd_itf[CFG_TUD_CDC]; // TODO will be replaced by dcd_edpt_busy() bool pending_read_from_host; diff --git a/src/class/msc/msc_device.c b/src/class/msc/msc_device.c index 1d5a3fc26..f980e941e 100644 --- a/src/class/msc/msc_device.c +++ b/src/class/msc/msc_device.c @@ -42,13 +42,9 @@ enum MSC_STAGE_STATUS }; -typedef struct { - CFG_TUSB_MEM_ALIGN msc_cbw_t cbw; - -//#if defined (__ICCARM__) && (CFG_TUSB_MCU == OPT_MCU_LPC11UXX || CFG_TUSB_MCU == OPT_MCU_LPC13XX) -// uint8_t padding1[64-sizeof(msc_cbw_t)]; // IAR cannot align struct's member -//#endif - +typedef struct +{ + CFG_TUSB_MEM_ALIGN msc_cbw_t cbw; CFG_TUSB_MEM_ALIGN msc_csw_t csw; uint8_t itf_num; @@ -66,7 +62,7 @@ typedef struct { uint8_t add_sense_qualifier; }mscd_interface_t; -CFG_TUSB_MEM_SECTION CFG_TUSB_MEM_ALIGN static mscd_interface_t _mscd_itf = { 0 }; +CFG_TUSB_MEM_SECTION CFG_TUSB_MEM_ALIGN static mscd_interface_t _mscd_itf; CFG_TUSB_MEM_SECTION CFG_TUSB_MEM_ALIGN static uint8_t _mscd_buf[CFG_TUD_MSC_BUFSIZE]; //--------------------------------------------------------------------+ diff --git a/src/portable/nordic/nrf5x/dcd_nrf5x.c b/src/portable/nordic/nrf5x/dcd_nrf5x.c index 1f8e94420..3e5f69f79 100644 --- a/src/portable/nordic/nrf5x/dcd_nrf5x.c +++ b/src/portable/nordic/nrf5x/dcd_nrf5x.c @@ -489,7 +489,7 @@ void USBD_IRQHandler(void) { xfer->total_len = xfer->actual_len; - // BULK/INT OUT complete + // CBI OUT complete dcd_event_xfer_complete(0, epnum, xfer->actual_len, XFER_RESULT_SUCCESS, true); } } @@ -524,7 +524,7 @@ void USBD_IRQHandler(void) xact_in_prepare(epnum); } else { - // Bulk/Int IN complete + // CBI IN complete dcd_event_xfer_complete(0, epnum | TUSB_DIR_IN_MASK, xfer->actual_len, XFER_RESULT_SUCCESS, true); } } -- cgit v1.3.1 From c8b9293d68900a2df8cacccd0d5287f114e41a4a Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 1 May 2019 20:45:05 +0700 Subject: rename hid report_buf to epin_buf add epout_buf and use it for SET_REPORT request buffer --- src/class/hid/hid_device.c | 26 ++++++++++++-------------- src/class/hid/hid_device.h | 4 ++++ 2 files changed, 16 insertions(+), 14 deletions(-) (limited to 'src/class') diff --git a/src/class/hid/hid_device.c b/src/class/hid/hid_device.c index 87b1b2c7b..5a59413e7 100644 --- a/src/class/hid/hid_device.c +++ b/src/class/hid/hid_device.c @@ -38,11 +38,6 @@ //--------------------------------------------------------------------+ // MACRO CONSTANT TYPEDEF //--------------------------------------------------------------------+ - -#ifndef CFG_TUD_HID_BUFSIZE -#define CFG_TUD_HID_BUFSIZE 16 -#endif - typedef struct { uint8_t itf_num; @@ -53,7 +48,9 @@ typedef struct uint8_t idle_rate; // up to application to handle idle rate uint16_t reprot_desc_len; - CFG_TUSB_MEM_ALIGN uint8_t report_buf[CFG_TUD_HID_BUFSIZE]; + CFG_TUSB_MEM_ALIGN uint8_t epin_buf[CFG_TUD_HID_BUFSIZE]; + CFG_TUSB_MEM_ALIGN uint8_t epout_buf[CFG_TUD_HID_BUFSIZE]; + }hidd_interface_t; CFG_TUSB_MEM_SECTION static hidd_interface_t _hidd_itf[CFG_TUD_HID]; @@ -89,14 +86,15 @@ bool tud_hid_report(uint8_t report_id, void const* report, uint8_t len) // If report id = 0, skip ID field if (report_id) { - p_hid->report_buf[0] = report_id; - memcpy(p_hid->report_buf+1, report, len); + p_hid->epin_buf[0] = report_id; + memcpy(p_hid->epin_buf+1, report, len); + len++; }else { - memcpy(p_hid->report_buf, report, len); + memcpy(p_hid->epin_buf, report, len); } - return dcd_edpt_xfer(TUD_OPT_RHPORT, p_hid->ep_in, p_hid->report_buf, len + (report_id ? 1 : 0) ); + return dcd_edpt_xfer(TUD_OPT_RHPORT, p_hid->ep_in, p_hid->epin_buf, len); } bool tud_hid_boot_mode(void) @@ -217,15 +215,15 @@ bool hidd_control_request(uint8_t rhport, tusb_control_request_t const * p_reque uint8_t const report_type = tu_u16_high(p_request->wValue); uint8_t const report_id = tu_u16_low(p_request->wValue); - uint16_t xferlen = tud_hid_get_report_cb(report_id, (hid_report_type_t) report_type, p_hid->report_buf, p_request->wLength); + uint16_t xferlen = tud_hid_get_report_cb(report_id, (hid_report_type_t) report_type, p_hid->epin_buf, p_request->wLength); TU_ASSERT( xferlen > 0 ); - usbd_control_xfer(rhport, p_request, p_hid->report_buf, xferlen); + usbd_control_xfer(rhport, p_request, p_hid->epin_buf, xferlen); } break; case HID_REQ_CONTROL_SET_REPORT: - usbd_control_xfer(rhport, p_request, p_hid->report_buf, p_request->wLength); + usbd_control_xfer(rhport, p_request, p_hid->epout_buf, p_request->wLength); break; case HID_REQ_CONTROL_SET_IDLE: @@ -284,7 +282,7 @@ bool hidd_control_request_complete(uint8_t rhport, tusb_control_request_t const uint8_t const report_type = tu_u16_high(p_request->wValue); uint8_t const report_id = tu_u16_low(p_request->wValue); - tud_hid_set_report_cb(report_id, (hid_report_type_t) report_type, p_hid->report_buf, p_request->wLength); + tud_hid_set_report_cb(report_id, (hid_report_type_t) report_type, p_hid->epout_buf, p_request->wLength); } return true; diff --git a/src/class/hid/hid_device.h b/src/class/hid/hid_device.h index d2eeebcf3..e349186e3 100644 --- a/src/class/hid/hid_device.h +++ b/src/class/hid/hid_device.h @@ -39,6 +39,10 @@ // Class Driver Default Configure & Validation //--------------------------------------------------------------------+ +#ifndef CFG_TUD_HID_BUFSIZE +#define CFG_TUD_HID_BUFSIZE 16 +#endif + //--------------------------------------------------------------------+ // Application API //--------------------------------------------------------------------+ -- cgit v1.3.1 From a25ca701cda3e1989bab3add052e82b7a71e5820 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 1 May 2019 23:26:52 +0700 Subject: text clean up --- src/class/cdc/cdc_device.h | 8 ++++++++ src/class/msc/msc_device.h | 14 ++++++++------ 2 files changed, 16 insertions(+), 6 deletions(-) (limited to 'src/class') diff --git a/src/class/cdc/cdc_device.h b/src/class/cdc/cdc_device.h index 6c59bac7a..18d6f842f 100644 --- a/src/class/cdc/cdc_device.h +++ b/src/class/cdc/cdc_device.h @@ -89,9 +89,17 @@ static inline bool tud_cdc_write_flush (void) //--------------------------------------------------------------------+ // APPLICATION CALLBACK API (WEAK is optional) //--------------------------------------------------------------------+ + +// Invoked when received new data ATTR_WEAK void tud_cdc_rx_cb(uint8_t itf); + +// Invoked when received `wanted_char` ATTR_WEAK void tud_cdc_rx_wanted_cb(uint8_t itf, char wanted_char); + +// Invoked when line state DTR & RTS are changed via SET_CONTROL_LINE_STATE ATTR_WEAK void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts); + +// Invoked when line coding is change via SET_LINE_CODING ATTR_WEAK void tud_cdc_line_coding_cb(uint8_t itf, cdc_line_coding_t const* p_line_coding); /** @} */ diff --git a/src/class/msc/msc_device.h b/src/class/msc/msc_device.h index 6e3aef8bb..b1e7e8da1 100644 --- a/src/class/msc/msc_device.h +++ b/src/class/msc/msc_device.h @@ -64,10 +64,11 @@ TU_VERIFY_STATIC(CFG_TUD_MSC_BUFSIZE < UINT16_MAX, "Size is not correct"); bool tud_msc_set_sense(uint8_t lun, uint8_t sense_key, uint8_t add_sense_code, uint8_t add_sense_qualifier); //--------------------------------------------------------------------+ -// APPLICATION CALLBACK (WEAK is optional) +// Application Callbacks (WEAK is optional) //--------------------------------------------------------------------+ + /** - * Callback invoked when received \ref SCSI_CMD_READ_10 command + * Invoked when received \ref SCSI_CMD_READ_10 command * \param[in] lun Logical unit number * \param[in] lba Logical Block Address to be read * \param[in] offset Byte offset from LBA @@ -86,7 +87,7 @@ bool tud_msc_set_sense(uint8_t lun, uint8_t sense_key, uint8_t add_sense_code, u int32_t tud_msc_read10_cb (uint8_t lun, uint32_t lba, uint32_t offset, void* buffer, uint32_t bufsize); /** - * Callback invoked when received \ref SCSI_CMD_WRITE_10 command + * Invoked when received \ref SCSI_CMD_WRITE_10 command * \param[in] lun Logical unit number * \param[in] lba Logical Block Address to be write * \param[in] offset Byte offset from LBA @@ -104,7 +105,8 @@ int32_t tud_msc_read10_cb (uint8_t lun, uint32_t lba, uint32_t offset, void* buf */ int32_t tud_msc_write10_cb (uint8_t lun, uint32_t lba, uint32_t offset, uint8_t* buffer, uint32_t bufsize); -// Invoked to determine the disk size +// Invoked when received SCSI_CMD_READ_CAPACITY_10 and SCSI_CMD_READ_FORMAT_CAPACITY to determine the disk size +// Application update block count and block size void tud_msc_capacity_cb(uint8_t lun, uint32_t* block_count, uint16_t* block_size); /** @@ -128,13 +130,13 @@ int32_t tud_msc_scsi_cb (uint8_t lun, uint8_t const scsi_cmd[16], void* buffer, /*------------- Optional callbacks -------------*/ -// Invoked to determine max LUN +// Invoked when received GET_MAX_LUN request ATTR_WEAK uint8_t tud_msc_maxlun_cb(void); // Invoked when Read10 command is complete ATTR_WEAK void tud_msc_read10_complete_cb(uint8_t lun); -// Invoke when Write10 command is complete +// Invoke when Write10 command is complete, can be used to flush flash caching ATTR_WEAK void tud_msc_write10_complete_cb(uint8_t lun); // Invoked when command in tud_msc_scsi_cb is complete -- cgit v1.3.1 From 1c0ec61aa1f7ea87dabe330036c0f4d59b629a5b Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 2 May 2019 00:08:39 +0700 Subject: add tud_hid_out_report_cb() for hid epout --- src/class/hid/hid_device.c | 24 +++++++++++++++++------- src/class/hid/hid_device.h | 11 +++++++---- 2 files changed, 24 insertions(+), 11 deletions(-) (limited to 'src/class') diff --git a/src/class/hid/hid_device.c b/src/class/hid/hid_device.c index 5a59413e7..2ce44a9b2 100644 --- a/src/class/hid/hid_device.c +++ b/src/class/hid/hid_device.c @@ -159,7 +159,7 @@ bool hidd_open(uint8_t rhport, tusb_desc_interface_t const * desc_itf, uint16_t uint8_t const *p_desc = (uint8_t const *) desc_itf; // TODO support multiple HID interface - uint8_t itf = 0; + uint8_t const itf = 0; hidd_interface_t * p_hid = &_hidd_itf[itf]; //------------- HID descriptor -------------// @@ -179,6 +179,9 @@ bool hidd_open(uint8_t rhport, tusb_desc_interface_t const * desc_itf, uint16_t *p_len = sizeof(tusb_desc_interface_t) + sizeof(tusb_hid_descriptor_hid_t) + desc_itf->bNumEndpoints*sizeof(tusb_desc_endpoint_t); + // Prepare for output endpoint + if (p_hid->ep_out) TU_ASSERT(dcd_edpt_xfer(rhport, p_hid->ep_out, p_hid->epout_buf, sizeof(p_hid->epout_buf))); + return true; } @@ -288,13 +291,20 @@ bool hidd_control_request_complete(uint8_t rhport, tusb_control_request_t const return true; } -bool hidd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes) +bool hidd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes) { - // nothing to do - (void) rhport; - (void) ep_addr; - (void) event; - (void) xferred_bytes; + (void) result; + + // TODO support multiple HID interface + uint8_t const itf = 0; + hidd_interface_t * p_hid = &_hidd_itf[itf]; + + if (ep_addr == p_hid->ep_out) + { + if (tud_hid_out_report_cb) tud_hid_out_report_cb(p_hid->epout_buf, xferred_bytes); + + TU_ASSERT(dcd_edpt_xfer(rhport, p_hid->ep_out, p_hid->epout_buf, sizeof(p_hid->epout_buf))); + } return true; } diff --git a/src/class/hid/hid_device.h b/src/class/hid/hid_device.h index e349186e3..503b4b430 100644 --- a/src/class/hid/hid_device.h +++ b/src/class/hid/hid_device.h @@ -68,18 +68,21 @@ bool tud_hid_mouse_report(uint8_t report_id, uint8_t buttons, int8_t x, int8_t y // Callbacks (Weak is optional) //--------------------------------------------------------------------+ -// Invoked when receiving GET_REPORT control request +// Invoked when received 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); -// Invoked when receiving SET_REPORT control request +// Invoked when received 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 +// Invoked when received data on OUT endpoint +ATTR_WEAK void tud_hid_out_report_cb(uint8_t const* buffer, uint16_t bufsize); + +// Invoked when received SET_PROTOCOL request ( mode switch Boot <-> Report ) ATTR_WEAK void tud_hid_boot_mode_cb(uint8_t boot_mode); -// Invoked when host send SET_IDLE request. return false will stall the request +// Invoked when received SET_IDLE request. return false will stall the request // - Idle Rate = 0 : only send report if there is changes, i.e skip duplication // - Idle Rate > 0 : skip duplication, but send at least 1 report every idle rate (in unit of 4 ms). ATTR_WEAK bool tud_hid_set_idle_cb(uint8_t idle_rate); -- cgit v1.3.1 From 353d415d3fb569c85bcaf5d3658c40e1a3784a68 Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 2 May 2019 13:27:00 +0700 Subject: clean up --- examples/device/cdc_msc_hid/src/usb_descriptors.c | 10 +- examples/device/hid_generic/Makefile | 15 ++ examples/device/hid_generic/src/main.c | 211 +++++++++++++++++++++ examples/device/hid_generic/src/tusb_config.h | 88 +++++++++ examples/device/hid_generic/src/usb_descriptors.c | 116 +++++++++++ examples/device/msc_dual_lun/src/usb_descriptors.c | 3 +- src/class/hid/hid.h | 107 +++++------ tests/support/descriptor_test.c | 4 +- 8 files changed, 492 insertions(+), 62 deletions(-) create mode 100644 examples/device/hid_generic/Makefile create mode 100644 examples/device/hid_generic/src/main.c create mode 100644 examples/device/hid_generic/src/tusb_config.h create mode 100644 examples/device/hid_generic/src/usb_descriptors.c (limited to 'src/class') diff --git a/examples/device/cdc_msc_hid/src/usb_descriptors.c b/examples/device/cdc_msc_hid/src/usb_descriptors.c index 38f60a561..c8fca571f 100644 --- a/examples/device/cdc_msc_hid/src/usb_descriptors.c +++ b/examples/device/cdc_msc_hid/src/usb_descriptors.c @@ -162,13 +162,13 @@ uint16_t const * const string_desc_arr [] = // tud_desc_set is required by tinyusb stack tud_desc_set_t tud_desc_set = { - .device = &desc_device, - .config = desc_configuration, + .device = &desc_device, + .config = desc_configuration, - .string_arr = (uint8_t const **) string_desc_arr, - .string_count = sizeof(string_desc_arr)/sizeof(string_desc_arr[0]), + .string_arr = (uint8_t const **) string_desc_arr, + .string_count = sizeof(string_desc_arr)/sizeof(string_desc_arr[0]), #if CFG_TUD_HID - .hid_report = desc_hid_report, + .hid_report = desc_hid_report, #endif }; diff --git a/examples/device/hid_generic/Makefile b/examples/device/hid_generic/Makefile new file mode 100644 index 000000000..4ff3371dc --- /dev/null +++ b/examples/device/hid_generic/Makefile @@ -0,0 +1,15 @@ +include ../../../tools/top.mk +include ../../make.mk + +INC += \ + src \ + $(TOP)/hw \ + +# Example source +EXAMPLE_SOURCE += $(wildcard src/*.c) +SRC_C += $(addprefix $(CURRENT_PATH)/, $(EXAMPLE_SOURCE)) + +# Board source +SRC_C += hw/bsp/$(BOARD)/board_$(BOARD).c + +include ../../rules.mk diff --git a/examples/device/hid_generic/src/main.c b/examples/device/hid_generic/src/main.c new file mode 100644 index 000000000..c8926fc16 --- /dev/null +++ b/examples/device/hid_generic/src/main.c @@ -0,0 +1,211 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2018, hathach (tinyusb.org) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + */ + +#include +#include +#include + +#include "bsp/board.h" +#include "tusb.h" + +//--------------------------------------------------------------------+ +// MACRO CONSTANT TYPEDEF PROTYPES +//--------------------------------------------------------------------+ + +/* Blink pattern + * - 250 ms : device not mounted + * - 1000 ms : device mounted + * - 2500 ms : device is suspended + */ +enum { + BLINK_NOT_MOUNTED = 250, + BLINK_MOUNTED = 1000, + BLINK_SUSPENDED = 2500, +}; + +static uint32_t blink_interval_ms = BLINK_NOT_MOUNTED; + +void led_blinking_task(void); +void hid_task(void); + +/*------------- MAIN -------------*/ +int main(void) +{ + board_init(); + + tusb_init(); + + while (1) + { + // tinyusb device task + tud_task(); + + led_blinking_task(); + +#if CFG_TUD_HID + hid_task(); +#endif + } + + return 0; +} + +//--------------------------------------------------------------------+ +// Device callbacks +//--------------------------------------------------------------------+ + +// Invoked when device is mounted +void tud_mount_cb(void) +{ + blink_interval_ms = BLINK_MOUNTED; +} + +// Invoked when device is unmounted +void tud_umount_cb(void) +{ + blink_interval_ms = BLINK_NOT_MOUNTED; +} + +// Invoked when usb bus is suspended +// remote_wakeup_en : if host allow us to perform remote wakeup +// Within 7ms, device must draw an average of current less than 2.5 mA from bus +void tud_suspend_cb(bool remote_wakeup_en) +{ + (void) remote_wakeup_en; + blink_interval_ms = BLINK_SUSPENDED; +} + +// Invoked when usb bus is resumed +void tud_resume_cb(void) +{ + blink_interval_ms = BLINK_MOUNTED; +} + +//--------------------------------------------------------------------+ +// 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 hid_task(void) +{ + // Poll every 10ms + const uint32_t interval_ms = 10; + static uint32_t start_ms = 0; + + if ( board_millis() < start_ms + interval_ms) return; // not enough time + start_ms += interval_ms; + + uint32_t const btn = board_button_read(); + + // Remote wakeup + if ( tud_suspended() && btn ) + { + // Wake up host if we are in suspend mode + // and REMOTE_WAKEUP feature is enabled by host + tud_remote_wakeup(); + } + + /*------------- Mouse -------------*/ + if ( tud_hid_ready() ) + { + if ( btn ) + { + int8_t const delta = 5; + + // no button, right + down, no scroll pan + tud_hid_mouse_report(REPORT_ID_MOUSE, 0x00, delta, delta, 0, 0); + + // 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(REPORT_ID_KEYBOARD, 0, keycode); + + has_key = true; + }else + { + // send empty key report if previously has key pressed + if (has_key) tud_hid_keyboard_report(REPORT_ID_KEYBOARD, 0, NULL); + has_key = false; + } + } +} + +uint16_t tud_hid_get_report_cb(uint8_t report_id, hid_report_type_t report_type, uint8_t* buffer, uint16_t reqlen) +{ + // TODO not Implemented + (void) report_id; + (void) report_type; + (void) buffer; + (void) reqlen; + + return 0; +} + +void tud_hid_set_report_cb(uint8_t report_id, hid_report_type_t report_type, uint8_t const* buffer, uint16_t bufsize) +{ + // TODO not Implemented + (void) report_id; + (void) report_type; + (void) buffer; + (void) bufsize; +} + +#endif + +//--------------------------------------------------------------------+ +// BLINKING TASK +//--------------------------------------------------------------------+ +void led_blinking_task(void) +{ + static uint32_t start_ms = 0; + static bool led_state = false; + + // Blink every 1000 ms + if ( board_millis() < start_ms + blink_interval_ms) return; // not enough time + start_ms += blink_interval_ms; + + board_led_write(led_state); + led_state = 1 - led_state; // toggle +} diff --git a/examples/device/hid_generic/src/tusb_config.h b/examples/device/hid_generic/src/tusb_config.h new file mode 100644 index 000000000..baf418d94 --- /dev/null +++ b/examples/device/hid_generic/src/tusb_config.h @@ -0,0 +1,88 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2018, hathach (tinyusb.org) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + */ + +#ifndef _TUSB_CONFIG_H_ +#define _TUSB_CONFIG_H_ + +#ifdef __cplusplus + extern "C" { +#endif + +//-------------------------------------------------------------------- +// COMMON CONFIGURATION +//-------------------------------------------------------------------- + +// defined by compiler flags for flexibility +#ifndef CFG_TUSB_MCU + #error CFG_TUSB_MCU must be defined +#endif + +#if CFG_TUSB_MCU == OPT_MCU_LPC43XX || CFG_TUSB_MCU == OPT_MCU_LPC18XX +#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE | OPT_MODE_HIGH_SPEED) +#else +#define CFG_TUSB_RHPORT0_MODE OPT_MODE_DEVICE +#endif + +#define CFG_TUSB_OS OPT_OS_NONE +#define CFG_TUSB_DEBUG 2 + +/* USB DMA on some MCUs can only access a specific SRAM region with restriction on alignment. + * Tinyusb use follows macros to declare transferring memory so that they can be put + * into those specific section. + * e.g + * - CFG_TUSB_MEM SECTION : __attribute__ (( section(".usb_ram") )) + * - CFG_TUSB_MEM_ALIGN : __attribute__ ((aligned(4))) + */ +#ifndef CFG_TUSB_MEM_SECTION +#define CFG_TUSB_MEM_SECTION +#endif + +#ifndef CFG_TUSB_MEM_ALIGN +#define CFG_TUSB_MEM_ALIGN ATTR_ALIGNED(4) +#endif + +//-------------------------------------------------------------------- +// DEVICE CONFIGURATION +//-------------------------------------------------------------------- + +#define CFG_TUD_ENDOINT0_SIZE 64 + +//------------- CLASS -------------// +#define CFG_TUD_CDC 0 +#define CFG_TUD_MSC 0 +#define CFG_TUD_HID 1 +#define CFG_TUD_MIDI 0 +#define CFG_TUD_CUSTOM_CLASS 0 + +//------------- HID -------------// + +// Should be sufficient to hold ID (if any) + Data +#define CFG_TUD_HID_BUFSIZE 64 + +#ifdef __cplusplus + } +#endif + +#endif /* _TUSB_CONFIG_H_ */ diff --git a/examples/device/hid_generic/src/usb_descriptors.c b/examples/device/hid_generic/src/usb_descriptors.c new file mode 100644 index 000000000..68909583b --- /dev/null +++ b/examples/device/hid_generic/src/usb_descriptors.c @@ -0,0 +1,116 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2018, hathach (tinyusb.org) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + */ + +#include "tusb.h" + +/* 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 | 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, 2) ) + +//------------- Device Descriptors -------------// +tusb_desc_device_t const desc_device = +{ + .bLength = sizeof(tusb_desc_device_t), + .bDescriptorType = TUSB_DESC_DEVICE, + .bcdUSB = 0x0200, + .bDeviceClass = 0x00, + .bDeviceSubClass = 0x00, + .bDeviceProtocol = 0x00, + .bMaxPacketSize0 = CFG_TUD_ENDOINT0_SIZE, + + .idVendor = 0xCafe, + .idProduct = USB_PID, + .bcdDevice = 0x0100, + + .iManufacturer = 0x01, + .iProduct = 0x02, + .iSerialNumber = 0x03, + + .bNumConfigurations = 0x01 +}; + +//------------- HID Report Descriptor -------------// +uint8_t const desc_hid_report[] = +{ + TUD_HID_REPORT_DESC_KEYBOARD( HID_REPORT_ID(REPORT_ID_KEYBOARD), ), + TUD_HID_REPORT_DESC_MOUSE ( HID_REPORT_ID(REPORT_ID_MOUSE), ) +}; + +//------------- Configuration Descriptor -------------// +enum +{ + ITF_NUM_HID, + ITF_NUM_TOTAL +}; + +enum +{ + CONFIG_TOTAL_LEN = TUD_CONFIG_DESC_LEN + TUD_HID_DESC_LEN +}; + +// Use Endpoint 2 instead of 1 due to NXP MCU +// LPC 17xx and 40xx endpoint type (bulk/interrupt/iso) are fixed by its number +// 0 control, 1 In, 2 Bulk, 3 Iso, 4 In etc ... +#define EPNUM_HID 0x01 + +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_TOTAL_LEN, TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, 100), + TUD_HID_DESCRIPTOR(ITF_NUM_HID, 0, HID_PROTOCOL_KEYBOARD, sizeof(desc_hid_report), 0x84, 16, 10) +}; + +//------------- 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'), +}; + +// tud_desc_set is required by tinyusb stack +tud_desc_set_t tud_desc_set = +{ + .device = &desc_device, + .config = desc_configuration, + + .string_arr = (uint8_t const **) string_desc_arr, + .string_count = sizeof(string_desc_arr)/sizeof(string_desc_arr[0]), + .hid_report = desc_hid_report, +}; diff --git a/examples/device/msc_dual_lun/src/usb_descriptors.c b/examples/device/msc_dual_lun/src/usb_descriptors.c index ede516eb1..b91ccb04a 100644 --- a/examples/device/msc_dual_lun/src/usb_descriptors.c +++ b/examples/device/msc_dual_lun/src/usb_descriptors.c @@ -43,7 +43,6 @@ tusb_desc_device_t const desc_device = .bDeviceClass = 0x00, .bDeviceSubClass = 0x00, .bDeviceProtocol = 0x00, - .bMaxPacketSize0 = CFG_TUD_ENDOINT0_SIZE, .idVendor = 0xCafe, @@ -80,7 +79,7 @@ uint8_t const desc_configuration[] = TUD_CONFIG_DESCRIPTOR(ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, 100), #if CFG_TUD_MSC - TUD_MSC_DESCRIPTOR(ITF_NUM_MSC, 5, EPNUM_MSC, 0x80 | EPNUM_MSC, 64), // highspeed 512 + TUD_MSC_DESCRIPTOR(ITF_NUM_MSC, 0, EPNUM_MSC, 0x80 | EPNUM_MSC, 64), // highspeed 512 #endif }; diff --git a/src/class/hid/hid.h b/src/class/hid/hid.h index 8cc8fc14f..a72fe9c86 100644 --- a/src/class/hid/hid.h +++ b/src/class/hid/hid.h @@ -43,6 +43,20 @@ /** \defgroup ClassDriver_HID_Common Common Definitions * @{ */ + /// USB HID Descriptor +typedef struct ATTR_PACKED +{ + uint8_t bLength; /**< Numeric expression that is the total size of the HID descriptor */ + uint8_t bDescriptorType; /**< Constant name specifying type of HID descriptor. */ + + uint16_t bcdHID; /**< Numeric expression identifying the HID Class Specification release */ + uint8_t bCountryCode; /**< Numeric expression identifying country code of the localized hardware. */ + uint8_t bNumDescriptors; /**< Numeric expression specifying the number of class descriptors */ + + uint8_t bReportType; /**< Type of HID class report. */ + uint16_t wReportLength; /**< the total size of the Report descriptor. */ +} tusb_hid_descriptor_hid_t; + /// HID Subclass typedef enum { @@ -69,9 +83,10 @@ typedef enum /// HID Request Report Type typedef enum { - HID_REPORT_TYPE_INPUT = 1, ///< Input - HID_REPORT_TYPE_OUTPUT, ///< Output - HID_REPORT_TYPE_FEATURE ///< Feature + HID_REPORT_TYPE_INVALID = 0, + HID_REPORT_TYPE_INPUT, ///< Input + HID_REPORT_TYPE_OUTPUT, ///< Output + HID_REPORT_TYPE_FEATURE ///< Feature }hid_report_type_t; /// HID Class Specific Control Request @@ -85,59 +100,45 @@ typedef enum HID_REQ_CONTROL_SET_PROTOCOL = 0x0b ///< Set Protocol }hid_request_type_t; -/// USB HID Descriptor -typedef struct ATTR_PACKED -{ - uint8_t bLength; /**< Numeric expression that is the total size of the HID descriptor */ - uint8_t bDescriptorType; /**< Constant name specifying type of HID descriptor. */ - - uint16_t bcdHID; /**< Numeric expression identifying the HID Class Specification release */ - uint8_t bCountryCode; /**< Numeric expression identifying country code of the localized hardware. */ - uint8_t bNumDescriptors; /**< Numeric expression specifying the number of class descriptors */ - - uint8_t bReportType; /**< Type of HID class report. */ - uint16_t wReportLength; /**< the total size of the Report descriptor. */ -} tusb_hid_descriptor_hid_t; - /// HID Country Code typedef enum { - HID_Local_NotSupported = 0 , ///< NotSupported - HID_Local_Arabic , ///< Arabic - HID_Local_Belgian , ///< Belgian - HID_Local_Canadian_Bilingual , ///< Canadian_Bilingual - HID_Local_Canadian_French , ///< Canadian_French - HID_Local_Czech_Republic , ///< Czech_Republic - HID_Local_Danish , ///< Danish - HID_Local_Finnish , ///< Finnish - HID_Local_French , ///< French - HID_Local_German , ///< German - HID_Local_Greek , ///< Greek - HID_Local_Hebrew , ///< Hebrew - HID_Local_Hungary , ///< Hungary - HID_Local_International , ///< International - HID_Local_Italian , ///< Italian - HID_Local_Japan_Katakana , ///< Japan_Katakana - HID_Local_Korean , ///< Korean - HID_Local_Latin_American , ///< Latin_American - HID_Local_Netherlands_Dutch , ///< Netherlands/Dutch - HID_Local_Norwegian , ///< Norwegian - HID_Local_Persian_Farsi , ///< Persian (Farsi) - HID_Local_Poland , ///< Poland - HID_Local_Portuguese , ///< Portuguese - HID_Local_Russia , ///< Russia - HID_Local_Slovakia , ///< Slovakia - HID_Local_Spanish , ///< Spanish - HID_Local_Swedish , ///< Swedish - HID_Local_Swiss_French , ///< Swiss/French - HID_Local_Swiss_German , ///< Swiss/German - HID_Local_Switzerland , ///< Switzerland - HID_Local_Taiwan , ///< Taiwan - HID_Local_Turkish_Q , ///< Turkish-Q - HID_Local_UK , ///< UK - HID_Local_US , ///< US - HID_Local_Yugoslavia , ///< Yugoslavia - HID_Local_Turkish_F ///< Turkish-F + HID_LOCAL_NotSupported = 0 , ///< NotSupported + HID_LOCAL_Arabic , ///< Arabic + HID_LOCAL_Belgian , ///< Belgian + HID_LOCAL_Canadian_Bilingual , ///< Canadian_Bilingual + HID_LOCAL_Canadian_French , ///< Canadian_French + HID_LOCAL_Czech_Republic , ///< Czech_Republic + HID_LOCAL_Danish , ///< Danish + HID_LOCAL_Finnish , ///< Finnish + HID_LOCAL_French , ///< French + HID_LOCAL_German , ///< German + HID_LOCAL_Greek , ///< Greek + HID_LOCAL_Hebrew , ///< Hebrew + HID_LOCAL_Hungary , ///< Hungary + HID_LOCAL_International , ///< International + HID_LOCAL_Italian , ///< Italian + HID_LOCAL_Japan_Katakana , ///< Japan_Katakana + HID_LOCAL_Korean , ///< Korean + HID_LOCAL_Latin_American , ///< Latin_American + HID_LOCAL_Netherlands_Dutch , ///< Netherlands/Dutch + HID_LOCAL_Norwegian , ///< Norwegian + HID_LOCAL_Persian_Farsi , ///< Persian (Farsi) + HID_LOCAL_Poland , ///< Poland + HID_LOCAL_Portuguese , ///< Portuguese + HID_LOCAL_Russia , ///< Russia + HID_LOCAL_Slovakia , ///< Slovakia + HID_LOCAL_Spanish , ///< Spanish + HID_LOCAL_Swedish , ///< Swedish + HID_LOCAL_Swiss_French , ///< Swiss/French + HID_LOCAL_Swiss_German , ///< Swiss/German + HID_LOCAL_Switzerland , ///< Switzerland + HID_LOCAL_Taiwan , ///< Taiwan + HID_LOCAL_Turkish_Q , ///< Turkish-Q + HID_LOCAL_UK , ///< UK + HID_LOCAL_US , ///< US + HID_LOCAL_Yugoslavia , ///< Yugoslavia + HID_LOCAL_Turkish_F ///< Turkish-F } hid_country_code_t; /** @} */ diff --git a/tests/support/descriptor_test.c b/tests/support/descriptor_test.c index 20e10af56..f4b1a6d17 100644 --- a/tests/support/descriptor_test.c +++ b/tests/support/descriptor_test.c @@ -166,7 +166,7 @@ const app_configuration_desc_t desc_configuration = .bLength = sizeof(tusb_hid_descriptor_hid_t), .bDescriptorType = HID_DESC_TYPE_HID, .bcdHID = 0x0111, - .bCountryCode = HID_Local_NotSupported, + .bCountryCode = HID_LOCAL_NotSupported, .bNumDescriptors = 1, .bReportType = HID_DESC_TYPE_REPORT, .wReportLength = sizeof(keyboard_report_descriptor) @@ -201,7 +201,7 @@ const app_configuration_desc_t desc_configuration = .bLength = sizeof(tusb_hid_descriptor_hid_t), .bDescriptorType = HID_DESC_TYPE_HID, .bcdHID = 0x0111, - .bCountryCode = HID_Local_NotSupported, + .bCountryCode = HID_LOCAL_NotSupported, .bNumDescriptors = 1, .bReportType = HID_DESC_TYPE_REPORT, .wReportLength = sizeof(mouse_report_descriptor) -- cgit v1.3.1 From 3387b9fca55f434867c449d4e2cdd7687721e993 Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 2 May 2019 13:27:32 +0700 Subject: use tud_hid_set_report_cb for hid epout --- src/class/hid/hid_device.c | 3 +-- src/class/hid/hid_device.h | 6 ++---- 2 files changed, 3 insertions(+), 6 deletions(-) (limited to 'src/class') diff --git a/src/class/hid/hid_device.c b/src/class/hid/hid_device.c index 2ce44a9b2..e82932e1f 100644 --- a/src/class/hid/hid_device.c +++ b/src/class/hid/hid_device.c @@ -301,8 +301,7 @@ bool hidd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_ if (ep_addr == p_hid->ep_out) { - if (tud_hid_out_report_cb) tud_hid_out_report_cb(p_hid->epout_buf, xferred_bytes); - + tud_hid_set_report_cb(0, HID_REPORT_TYPE_INVALID, p_hid->epout_buf, xferred_bytes); TU_ASSERT(dcd_edpt_xfer(rhport, p_hid->ep_out, p_hid->epout_buf, sizeof(p_hid->epout_buf))); } diff --git a/src/class/hid/hid_device.h b/src/class/hid/hid_device.h index 503b4b430..1120e8e45 100644 --- a/src/class/hid/hid_device.h +++ b/src/class/hid/hid_device.h @@ -73,12 +73,10 @@ bool tud_hid_mouse_report(uint8_t report_id, uint8_t buttons, int8_t x, int8_t y // 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); -// Invoked when received SET_REPORT control request +// Invoked when received SET_REPORT control request or +// received data on OUT endpoint ( Report ID = 0, Type = 0 ) 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 received data on OUT endpoint -ATTR_WEAK void tud_hid_out_report_cb(uint8_t const* buffer, uint16_t bufsize); - // Invoked when received SET_PROTOCOL request ( mode switch Boot <-> Report ) ATTR_WEAK void tud_hid_boot_mode_cb(uint8_t boot_mode); -- cgit v1.3.1 From f9d9eebfe1dbf462a69e7d3a967b2ad103ae2277 Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 2 May 2019 14:30:49 +0700 Subject: support hid generic inout #58 - add report descriptor template TUD_HID_REPORT_DESC_GENERIC_INOUT - add TUD_HID_INOUT_DESCRIPTOR & TUD_HID_INOUT_DESC_LEN - add hid_generic_inout example --- README.md | 2 +- examples/device/cdc_msc_hid/src/usb_descriptors.c | 7 +++-- .../cdc_msc_hid_freertos/src/usb_descriptors.c | 7 +++-- .../device/hid_generic_inout/src/usb_descriptors.c | 11 ++++---- examples/device/msc_dual_lun/src/usb_descriptors.c | 5 ++-- src/class/hid/hid.h | 2 +- src/class/hid/hid_device.h | 32 +++++++++++++++++++++- src/common/tusb_types.h | 4 +-- src/device/usbd.h | 20 ++++++++++++-- tools/build_all.py | 2 +- 10 files changed, 72 insertions(+), 20 deletions(-) (limited to 'src/class') diff --git a/README.md b/README.md index d512d1e28..919251243 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ TinyUSB is an open-source cross-platform USB Host/Device stack for embedded syst Support multiple device configurations by dynamically changing usb descriptors. Low power functions such as suspend, resume and remote wakeup. Following device classes are supported: - Communication Class (CDC) -- Human Interface Device (HID): Keyboard, Mouse, Gamepad etc ... +- Human Interface Device (HID): Generic (In & Out), Keyboard, Mouse, Gamepad etc ... - Mass Storage Class (MSC): with multiple LUNs - Musical Instrument Digital Interface (MIDI) diff --git a/examples/device/cdc_msc_hid/src/usb_descriptors.c b/examples/device/cdc_msc_hid/src/usb_descriptors.c index c8fca571f..72d280c2f 100644 --- a/examples/device/cdc_msc_hid/src/usb_descriptors.c +++ b/examples/device/cdc_msc_hid/src/usb_descriptors.c @@ -117,19 +117,22 @@ enum uint8_t const desc_configuration[] = { - // Config: self-powered with remote wakeup support, max power up to 100 mA + // Inteface count, string index, total length, attribute, power in mA TUD_CONFIG_DESCRIPTOR(ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, 100), #if CFG_TUD_CDC + // Interface number, string index, EP notification address and size, EP data address (out, in) and size. TUD_CDC_DESCRIPTOR(ITF_NUM_CDC, 4, 0x81, 8, 0x02, 0x82, 64), #endif #if CFG_TUD_MSC + // Interface number, string index, EP Out & EP In address, EP size TUD_MSC_DESCRIPTOR(ITF_NUM_MSC, 5, EPNUM_MSC, 0x80 | EPNUM_MSC, 64), // highspeed 512 #endif #if CFG_TUD_HID - TUD_HID_DESCRIPTOR(ITF_NUM_HID, 6, HID_PROTOCOL_KEYBOARD, sizeof(desc_hid_report), 0x84, 16, 10) + // Interface number, string index, protocol, report descriptor len, EP In address, size & polling interval + TUD_HID_DESCRIPTOR(ITF_NUM_HID, 6, HID_PROTOCOL_NONE, sizeof(desc_hid_report), 0x84, 16, 10) #endif }; diff --git a/examples/device/cdc_msc_hid_freertos/src/usb_descriptors.c b/examples/device/cdc_msc_hid_freertos/src/usb_descriptors.c index 38f60a561..1faaaed66 100644 --- a/examples/device/cdc_msc_hid_freertos/src/usb_descriptors.c +++ b/examples/device/cdc_msc_hid_freertos/src/usb_descriptors.c @@ -117,19 +117,22 @@ enum uint8_t const desc_configuration[] = { - // Config: self-powered with remote wakeup support, max power up to 100 mA + // Inteface count, string index, total length, attribute, power in mA TUD_CONFIG_DESCRIPTOR(ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, 100), #if CFG_TUD_CDC + // Interface number, string index, EP notification address and size, EP data address (out, in) and size. TUD_CDC_DESCRIPTOR(ITF_NUM_CDC, 4, 0x81, 8, 0x02, 0x82, 64), #endif #if CFG_TUD_MSC + // Interface number, string index, EP Out & EP In address, EP size TUD_MSC_DESCRIPTOR(ITF_NUM_MSC, 5, EPNUM_MSC, 0x80 | EPNUM_MSC, 64), // highspeed 512 #endif #if CFG_TUD_HID - TUD_HID_DESCRIPTOR(ITF_NUM_HID, 6, HID_PROTOCOL_KEYBOARD, sizeof(desc_hid_report), 0x84, 16, 10) + // Interface number, string index, protocol, report descriptor len, EP In address, size & polling interval + TUD_HID_DESCRIPTOR(ITF_NUM_HID, 6, HID_PROTOCOL_NONE, sizeof(desc_hid_report), 0x84, 16, 10) #endif }; diff --git a/examples/device/hid_generic_inout/src/usb_descriptors.c b/examples/device/hid_generic_inout/src/usb_descriptors.c index 68909583b..231eb4fa6 100644 --- a/examples/device/hid_generic_inout/src/usb_descriptors.c +++ b/examples/device/hid_generic_inout/src/usb_descriptors.c @@ -59,8 +59,7 @@ tusb_desc_device_t const desc_device = //------------- HID Report Descriptor -------------// uint8_t const desc_hid_report[] = { - TUD_HID_REPORT_DESC_KEYBOARD( HID_REPORT_ID(REPORT_ID_KEYBOARD), ), - TUD_HID_REPORT_DESC_MOUSE ( HID_REPORT_ID(REPORT_ID_MOUSE), ) + TUD_HID_REPORT_DESC_GENERIC_INOUT(CFG_TUD_HID_BUFSIZE) }; //------------- Configuration Descriptor -------------// @@ -72,7 +71,7 @@ enum enum { - CONFIG_TOTAL_LEN = TUD_CONFIG_DESC_LEN + TUD_HID_DESC_LEN + CONFIG_TOTAL_LEN = TUD_CONFIG_DESC_LEN + TUD_HID_INOUT_DESC_LEN }; // Use Endpoint 2 instead of 1 due to NXP MCU @@ -82,9 +81,11 @@ enum uint8_t const desc_configuration[] = { - // Config: self-powered with remote wakeup support, max power up to 100 mA + // Inteface count, string index, total length, attribute, power in mA TUD_CONFIG_DESCRIPTOR(ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, 100), - TUD_HID_DESCRIPTOR(ITF_NUM_HID, 0, HID_PROTOCOL_KEYBOARD, sizeof(desc_hid_report), 0x84, 16, 10) + + // Interface number, string index, protocol, report descriptor len, EP In & Out address, size & polling interval + TUD_HID_INOUT_DESCRIPTOR(ITF_NUM_HID, 0, HID_PROTOCOL_NONE, sizeof(desc_hid_report), 0x80 | EPNUM_HID, EPNUM_HID, 16, 10) }; //------------- String Descriptors -------------// diff --git a/examples/device/msc_dual_lun/src/usb_descriptors.c b/examples/device/msc_dual_lun/src/usb_descriptors.c index b91ccb04a..af2b1a091 100644 --- a/examples/device/msc_dual_lun/src/usb_descriptors.c +++ b/examples/device/msc_dual_lun/src/usb_descriptors.c @@ -75,12 +75,11 @@ enum uint8_t const desc_configuration[] = { - // Config: self-powered with remote wakeup support, max power up to 100 mA + // Inteface count, string index, total length, attribute, power in mA TUD_CONFIG_DESCRIPTOR(ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, 100), -#if CFG_TUD_MSC + // Interface number, string index, EP Out & EP In address, EP size TUD_MSC_DESCRIPTOR(ITF_NUM_MSC, 0, EPNUM_MSC, 0x80 | EPNUM_MSC, 64), // highspeed 512 -#endif }; //------------- String Descriptors -------------// diff --git a/src/class/hid/hid.h b/src/class/hid/hid.h index a72fe9c86..7c702f4de 100644 --- a/src/class/hid/hid.h +++ b/src/class/hid/hid.h @@ -462,7 +462,7 @@ enum { HID_USAGE_PAGE_MSR = 0x8e, HID_USAGE_PAGE_CAMERA = 0x90, HID_USAGE_PAGE_ARCADE = 0x91, - HID_USAGE_PAGE_VENDOR = 0xFFFF // 0xFF00 - 0xFFFF + HID_USAGE_PAGE_VENDOR = 0xFF00 // 0xFF00 - 0xFFFF }; /// HID Usage Table - Table 6: Generic Desktop Page diff --git a/src/class/hid/hid_device.h b/src/class/hid/hid_device.h index 1120e8e45..825be2181 100644 --- a/src/class/hid/hid_device.h +++ b/src/class/hid/hid_device.h @@ -108,8 +108,9 @@ ATTR_WEAK bool tud_hid_set_idle_cb(uint8_t idle_rate); HID_USAGE_PAGE ( HID_USAGE_PAGE_DESKTOP ) ,\ HID_USAGE ( HID_USAGE_DESKTOP_KEYBOARD ) ,\ HID_COLLECTION ( HID_COLLECTION_APPLICATION ) ,\ - /* 8 bits Modifier Keys (Shfit, Control, Alt) */ \ + /* Report ID if any */\ __VA_ARGS__ \ + /* 8 bits Modifier Keys (Shfit, Control, Alt) */ \ HID_USAGE_PAGE ( HID_USAGE_PAGE_KEYBOARD ) ,\ HID_USAGE_MIN ( 224 ) ,\ HID_USAGE_MAX ( 231 ) ,\ @@ -149,6 +150,7 @@ ATTR_WEAK bool tud_hid_set_idle_cb(uint8_t idle_rate); HID_USAGE_PAGE ( HID_USAGE_PAGE_DESKTOP ) ,\ HID_USAGE ( HID_USAGE_DESKTOP_MOUSE ) ,\ HID_COLLECTION ( HID_COLLECTION_APPLICATION ) ,\ + /* Report ID if any */\ __VA_ARGS__ \ HID_USAGE ( HID_USAGE_DESKTOP_POINTER ) ,\ HID_COLLECTION ( HID_COLLECTION_PHYSICAL ) ,\ @@ -197,6 +199,7 @@ ATTR_WEAK bool tud_hid_set_idle_cb(uint8_t idle_rate); HID_USAGE_PAGE ( HID_USAGE_PAGE_CONSUMER ) ,\ HID_USAGE ( HID_USAGE_CONSUMER_CONTROL ) ,\ HID_COLLECTION ( HID_COLLECTION_APPLICATION ) ,\ + /* Report ID if any */\ __VA_ARGS__ \ HID_LOGICAL_MIN ( 0x00 ) ,\ HID_LOGICAL_MAX_N( 0x03FF, 2 ) ,\ @@ -217,6 +220,7 @@ ATTR_WEAK bool tud_hid_set_idle_cb(uint8_t idle_rate); HID_USAGE_PAGE ( HID_USAGE_PAGE_DESKTOP ) ,\ HID_USAGE ( HID_USAGE_DESKTOP_SYSTEM_CONTROL ) ,\ HID_COLLECTION ( HID_COLLECTION_APPLICATION ) ,\ + /* Report ID if any */\ __VA_ARGS__ \ /* 2 bit system power control */ \ HID_LOGICAL_MIN ( 1 ) ,\ @@ -240,6 +244,7 @@ ATTR_WEAK bool tud_hid_set_idle_cb(uint8_t idle_rate); HID_USAGE_PAGE ( HID_USAGE_PAGE_DESKTOP ) ,\ HID_USAGE ( HID_USAGE_DESKTOP_GAMEPAD ) ,\ HID_COLLECTION ( HID_COLLECTION_APPLICATION ) ,\ + /* Report ID if any */\ __VA_ARGS__ \ /* 16 bit Button Map */ \ HID_USAGE_PAGE ( HID_USAGE_PAGE_BUTTON ) ,\ @@ -263,6 +268,31 @@ ATTR_WEAK bool tud_hid_set_idle_cb(uint8_t idle_rate); HID_INPUT ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE ) ,\ HID_COLLECTION_END \ +// HID Generic Input & Output +// - 1st parameter is report size (mandatory) +// - 2nd parameter is report id HID_REPORT_ID(n) (optional) +#define TUD_HID_REPORT_DESC_GENERIC_INOUT(report_size, ...) \ + HID_USAGE_PAGE_N ( HID_USAGE_PAGE_VENDOR, 2 ),\ + HID_USAGE ( 0x01 ),\ + HID_COLLECTION ( HID_COLLECTION_APPLICATION ),\ + /* Report ID if any */\ + __VA_ARGS__ \ + /* Input */ \ + HID_USAGE ( 0x02 ),\ + HID_LOGICAL_MIN ( 0x00 ),\ + HID_LOGICAL_MAX ( 0xff ),\ + HID_REPORT_SIZE ( 8 ),\ + HID_REPORT_COUNT( report_size ),\ + HID_INPUT ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE ),\ + /* Output */ \ + HID_USAGE ( 0x03 ),\ + HID_LOGICAL_MIN ( 0x00 ),\ + HID_LOGICAL_MAX ( 0xff ),\ + HID_REPORT_SIZE ( 8 ),\ + HID_REPORT_COUNT( report_size ),\ + HID_OUTPUT ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE ),\ + HID_COLLECTION_END \ + /*-------------------------------------------------------------------- * ASCII to KEYCODE Conversion * Expand to array of [128][2] (shift, keycode) diff --git a/src/common/tusb_types.h b/src/common/tusb_types.h index 7d57a7950..909deffa8 100644 --- a/src/common/tusb_types.h +++ b/src/common/tusb_types.h @@ -169,7 +169,7 @@ enum { #define TUSB_DESC_CONFIG_POWER_MA(x) ((x)/2) -/// Device State +/// Device State TODO remove typedef enum { TUSB_DEVICE_STATE_UNPLUG = 0 , @@ -184,7 +184,7 @@ typedef enum XFER_RESULT_STALLED, }xfer_result_t; -enum +enum // TODO remove { DESC_OFFSET_LEN = 0, DESC_OFFSET_TYPE = 1 diff --git a/src/device/usbd.h b/src/device/usbd.h index de59731a2..8debbf535 100644 --- a/src/device/usbd.h +++ b/src/device/usbd.h @@ -107,7 +107,7 @@ ATTR_WEAK void tud_resume_cb(void); #define TUD_CDC_DESC_LEN (8+9+5+5+4+5+7+9+7+7) // CDC Descriptor Template -// interface number, string index, EP notification address and size, EP data address (out,in) and size. +// 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 */\ 8, TUSB_DESC_INTERFACE_ASSOCIATION, _itfnum, 2, TUSB_CLASS_CDC, CDC_COMM_SUBCLASS_ABSTRACT_CONTROL_MODEL, CDC_COMM_PROTOCOL_ATCOMMAND, 0,\ @@ -149,15 +149,31 @@ ATTR_WEAK void tud_resume_cb(void); // Length of template descriptor: 25 bytes #define TUD_HID_DESC_LEN (9 + 9 + 7) +// HID Input only descriptor // Interface number, string index, protocol, report descriptor len, EP In address, size & polling interval #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 */\ + /* Endpoint In */\ 7, TUSB_DESC_ENDPOINT, _epin, TUSB_XFER_INTERRUPT, U16_TO_U8S_LE(_epsize), _ep_interval +// Length of template descriptor: 32 bytes +#define TUD_HID_INOUT_DESC_LEN (9 + 9 + 7 + 7) + +// HID Input & Output descriptor +// Interface number, string index, protocol, report descriptor len, EP In & Out address, size & polling interval +#define TUD_HID_INOUT_DESCRIPTOR(_itfnum, _stridx, _boot_protocol, _report_desc_len, _epin, _epout, _epsize, _ep_interval) \ + /* Interface */\ + 9, TUSB_DESC_INTERFACE, _itfnum, 0, 2, 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 In */\ + 7, TUSB_DESC_ENDPOINT, _epin, TUSB_XFER_INTERRUPT, U16_TO_U8S_LE(_epsize), _ep_interval,\ + /* Endpoint Out */\ + 7, TUSB_DESC_ENDPOINT, _epout, TUSB_XFER_INTERRUPT, U16_TO_U8S_LE(_epsize), _ep_interval + #ifdef __cplusplus } #endif diff --git a/tools/build_all.py b/tools/build_all.py index 8a945712e..4f1bc2636 100644 --- a/tools/build_all.py +++ b/tools/build_all.py @@ -4,7 +4,7 @@ import sys import subprocess import time -all_device_example = ["cdc_msc_hid", "msc_dual_lun"] +all_device_example = ["cdc_msc_hid", "msc_dual_lun", "hid_generic_inout"] all_boards = ["metro_m0_express", "metro_m4_express", "pca10056", "stm32f407g_disc1"] for example in all_device_example: -- cgit v1.3.1