From cc2fce31e5c2ba92f1a9f7874096e0e6e3db14cb Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 18 Apr 2019 00:36:17 +0700 Subject: [PATCH] refactor hid, rename hid_generic to simply hid_ --- examples/device/cdc_msc_hid/src/main.c | 14 +-- .../device/cdc_msc_hid/src/usb_descriptors.c | 7 +- .../device/cdc_msc_hid_freertos/src/main.c | 4 +- src/class/hid/hid_device.c | 108 ++++-------------- src/class/hid/hid_device.h | 93 +++++---------- src/device/usbd.h | 6 +- src/device/usbd_auto_desc.c | 4 + 7 files changed, 66 insertions(+), 170 deletions(-) diff --git a/examples/device/cdc_msc_hid/src/main.c b/examples/device/cdc_msc_hid/src/main.c index 78e07910d..1a431b765 100644 --- a/examples/device/cdc_msc_hid/src/main.c +++ b/examples/device/cdc_msc_hid/src/main.c @@ -144,18 +144,14 @@ void usb_hid_task(void) tud_remote_wakeup(); } -#if 0 +#if 1 /*------------- Keyboard -------------*/ - if ( tud_hid_keyboard_ready() ) + if ( tud_hid_ready() ) { if ( btn ) { uint8_t keycode[6] = { 0 }; - - for(uint8_t i=0; i < 6; i++) - { - if ( btn & (1 << i) ) keycode[i] = HID_KEY_A + i; - } + keycode[0] = HID_KEY_A; tud_hid_keyboard_keycode(0, keycode); }else @@ -180,7 +176,7 @@ void usb_hid_task(void) #endif } -uint16_t tud_hid_generic_get_report_cb(uint8_t report_id, hid_report_type_t report_type, uint8_t* buffer, uint16_t reqlen) +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; @@ -191,7 +187,7 @@ uint16_t tud_hid_generic_get_report_cb(uint8_t report_id, hid_report_type_t repo return 0; } -void tud_hid_generic_set_report_cb(uint8_t report_id, hid_report_type_t report_type, uint8_t const* buffer, uint16_t bufsize) +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; diff --git a/examples/device/cdc_msc_hid/src/usb_descriptors.c b/examples/device/cdc_msc_hid/src/usb_descriptors.c index 0892b9bf8..33fa62f86 100644 --- a/examples/device/cdc_msc_hid/src/usb_descriptors.c +++ b/examples/device/cdc_msc_hid/src/usb_descriptors.c @@ -120,10 +120,5 @@ tud_desc_set_t tud_desc_set = .string_arr = (uint8_t const **) string_desc_arr, .string_count = sizeof(string_desc_arr)/sizeof(string_desc_arr[0]), - .hid_report = - { - .generic = NULL, - .boot_keyboard = NULL, - .boot_mouse = NULL - } + .hid_report = NULL, }; diff --git a/examples/device/cdc_msc_hid_freertos/src/main.c b/examples/device/cdc_msc_hid_freertos/src/main.c index c4b970a81..626f7dc45 100644 --- a/examples/device/cdc_msc_hid_freertos/src/main.c +++ b/examples/device/cdc_msc_hid_freertos/src/main.c @@ -193,13 +193,13 @@ void usb_hid_task(void* params) } } -uint16_t tud_hid_generic_get_report_cb(uint8_t report_id, hid_report_type_t report_type, uint8_t* buffer, uint16_t reqlen) +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 return 0; } -void tud_hid_generic_set_report_cb(uint8_t report_id, hid_report_type_t report_type, uint8_t const* buffer, uint16_t bufsize) +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 } diff --git a/src/class/hid/hid_device.c b/src/class/hid/hid_device.c index 5c81a1268..342f2f1f9 100644 --- a/src/class/hid/hid_device.c +++ b/src/class/hid/hid_device.c @@ -43,57 +43,32 @@ #define REPORT_BUFSIZE 12 #define ITF_IDX_BOOT_KBD 0 -#define ITF_IDX_BOOT_MSE ( ITF_IDX_BOOT_KBD + (CFG_TUD_HID_KEYBOARD && CFG_TUD_HID_KEYBOARD_BOOT) ) -#define ITF_IDX_GENERIC ( ITF_IDX_BOOT_MSE + (CFG_TUD_HID_MOUSE && CFG_TUD_HID_MOUSE_BOOT) ) -#define ITF_COUNT ( ITF_IDX_GENERIC + 1 ) +#define ITF_IDX_BOOT_MSE 0 // ( ITF_IDX_BOOT_KBD + (CFG_TUD_HID_KEYBOARD && CFG_TUD_HID_KEYBOARD_BOOT) ) +#define ITF_IDX_GENERIC 0 // ( ITF_IDX_BOOT_MSE + (CFG_TUD_HID_MOUSE && CFG_TUD_HID_MOUSE_BOOT) ) typedef struct { uint8_t itf_num; uint8_t ep_in; - uint8_t idle_rate; // in unit of 4 ms TODO removed + 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). + // If idle time is less than interrupt polling then use the polling. + uint8_t boot_device; bool boot_protocol; uint16_t desc_len; uint8_t const * desc_report; CFG_TUSB_MEM_ALIGN uint8_t report_buf[REPORT_BUFSIZE]; - - // callbacks - uint16_t (*get_report_cb) (uint8_t report_id, hid_report_type_t type, uint8_t* buffer, uint16_t reqlen); - void (*set_report_cb) (uint8_t report_id, hid_report_type_t type, uint8_t const* buffer, uint16_t bufsize); - }hidd_interface_t; -typedef struct -{ - uint8_t usage; // HID_USAGE_* - 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). - // If idle time is less than interrupt polling then use the polling. - - uint8_t report_id; - uint8_t report_len; - - hidd_interface_t* itf; -} hidd_report_t ; - -CFG_TUSB_MEM_SECTION static hidd_interface_t _hidd_itf[ITF_COUNT] = { { 0 } }; - - -#if CFG_TUD_HID_KEYBOARD -static hidd_report_t _kbd_rpt; -#endif - -#if CFG_TUD_HID_MOUSE -static hidd_report_t _mse_rpt; -#endif +CFG_TUSB_MEM_SECTION static hidd_interface_t _hidd_itf[CFG_TUD_HID]; /*------------- Helpers -------------*/ static inline hidd_interface_t* get_interface_by_itfnum(uint8_t itf_num) { - for (uint8_t i=0; i < ITF_COUNT; i++ ) + for (uint8_t i=0; i < CFG_TUD_HID; i++ ) { if ( itf_num == _hidd_itf[i].itf_num ) return &_hidd_itf[i]; } @@ -101,18 +76,17 @@ static inline hidd_interface_t* get_interface_by_itfnum(uint8_t itf_num) return NULL; } - //--------------------------------------------------------------------+ // HID GENERIC API //--------------------------------------------------------------------+ -bool tud_hid_generic_ready(void) +bool tud_hid_ready(void) { return tud_ready() && (_hidd_itf[ITF_IDX_GENERIC].ep_in != 0) && !dcd_edpt_busy(TUD_OPT_RHPORT, _hidd_itf[ITF_IDX_GENERIC].ep_in); } -bool tud_hid_generic_report(uint8_t report_id, void const* report, uint8_t len) +bool tud_hid_report(uint8_t report_id, void const* report, uint8_t len) { - TU_VERIFY( tud_hid_generic_ready() && (len < REPORT_BUFSIZE) ); + TU_VERIFY( tud_hid_ready() && (len < REPORT_BUFSIZE) ); hidd_interface_t * p_hid = &_hidd_itf[ITF_IDX_GENERIC]; @@ -134,21 +108,10 @@ bool tud_hid_generic_report(uint8_t report_id, void const* report, uint8_t len) // KEYBOARD APPLICATION API //--------------------------------------------------------------------+ #if CFG_TUD_HID_KEYBOARD -bool tud_hid_keyboard_ready(void) -{ - return (_kbd_rpt.itf != NULL) && !dcd_edpt_busy(TUD_OPT_RHPORT, _kbd_rpt.itf->ep_in); -} - -bool tud_hid_keyboard_is_boot_protocol(void) -{ - return (_kbd_rpt.itf != NULL) && _kbd_rpt.itf->boot_protocol; -} - static bool hidd_kbd_report(hid_keyboard_report_t const *p_report) { - TU_VERIFY( tud_hid_keyboard_ready() ); - - hidd_interface_t * p_hid = _kbd_rpt.itf; + TU_VERIFY( tud_hid_ready() ); + hidd_interface_t * p_hid = &_hidd_itf[ITF_IDX_GENERIC]; // only send report if there is changes, i.e skip duplication // if ( _kbd_rpt.idle_rate == 0 ) @@ -198,11 +161,6 @@ bool tud_hid_keyboard_key_press(char ch) //--------------------------------------------------------------------+ #if CFG_TUD_HID_MOUSE -bool tud_hid_mouse_ready(void) -{ - return (_mse_rpt.itf != NULL) && !dcd_edpt_busy(TUD_OPT_RHPORT, _mse_rpt.itf->ep_in); -} - bool tud_hid_mouse_is_boot_protocol(void) { return (_mse_rpt.itf != NULL) && _mse_rpt.itf->boot_protocol; @@ -267,14 +225,6 @@ void hidd_reset(uint8_t rhport) { (void) rhport; tu_memclr(_hidd_itf, sizeof(_hidd_itf)); - - #if CFG_TUD_HID_KEYBOARD - tu_varclr(&_kbd_rpt); - #endif - - #if CFG_TUD_HID_MOUSE - tu_varclr(&_mse_rpt); - #endif } bool hidd_open(uint8_t rhport, tusb_desc_interface_t const * desc_itf, uint16_t *p_len) @@ -284,6 +234,11 @@ bool hidd_open(uint8_t rhport, tusb_desc_interface_t const * desc_itf, uint16_t // TODO support HID OUT Endpoint TU_ASSERT(desc_itf->bNumEndpoints == 1); + // TODO support multiple HID interface + hidd_interface_t * p_hid = &_hidd_itf[0]; + + if ( desc_itf->bInterfaceSubClass == HID_SUBCLASS_BOOT ) p_hid->boot_device = desc_itf->bInterfaceProtocol; + //------------- 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; @@ -294,9 +249,8 @@ bool hidd_open(uint8_t rhport, tusb_desc_interface_t const * desc_itf, uint16_t tusb_desc_endpoint_t const *desc_edpt = (tusb_desc_endpoint_t const *) p_desc; TU_ASSERT(TUSB_DESC_ENDPOINT == desc_edpt->bDescriptorType); - hidd_interface_t * p_hid = NULL; - /*------------- Boot protocol only keyboard & mouse -------------*/ +#if 0 if (desc_itf->bInterfaceSubClass == HID_SUBCLASS_BOOT) { TU_ASSERT(desc_itf->bInterfaceProtocol == HID_PROTOCOL_KEYBOARD || desc_itf->bInterfaceProtocol == HID_PROTOCOL_MOUSE); @@ -338,13 +292,12 @@ bool hidd_open(uint8_t rhport, tusb_desc_interface_t const * desc_itf, uint16_t } /*------------- Generic (multiple report) -------------*/ else +#endif { // TODO parse report ID for keyboard, mouse - p_hid = &_hidd_itf[ITF_IDX_GENERIC]; +// p_hid = &_hidd_itf[ITF_IDX_GENERIC]; - p_hid->desc_report = usbd_desc_set->hid_report.generic; - p_hid->get_report_cb = tud_hid_generic_get_report_cb; - p_hid->set_report_cb = tud_hid_generic_set_report_cb; + p_hid->desc_report = usbd_desc_set->hid_report; } TU_ASSERT(p_hid->desc_report); @@ -392,17 +345,9 @@ 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; - if ( p_hid->get_report_cb ) - { - xferlen = p_hid->get_report_cb(report_id, (hid_report_type_t) report_type, p_hid->report_buf, p_request->wLength); - }else - { - // For boot Interface only: re-use report_buf -> report has no change - xferlen = p_request->wLength; - } - + uint16_t xferlen = tud_hid_get_report_cb(report_id, (hid_report_type_t) report_type, p_hid->report_buf, p_request->wLength); TU_ASSERT( xferlen > 0 ); + usbd_control_xfer(rhport, p_request, p_hid->report_buf, xferlen); } break; @@ -459,10 +404,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); - if ( p_hid->set_report_cb ) - { - p_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->report_buf, p_request->wLength); } return true; diff --git a/src/class/hid/hid_device.h b/src/class/hid/hid_device.h index 096daad18..db1438bf5 100644 --- a/src/class/hid/hid_device.h +++ b/src/class/hid/hid_device.h @@ -54,12 +54,36 @@ //--------------------------------------------------------------------+ // HID GENERIC API //--------------------------------------------------------------------+ -bool tud_hid_generic_ready(void); -bool tud_hid_generic_report(uint8_t report_id, void const* report, uint8_t len); + +/** 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 + */ +bool tud_hid_ready(void); +bool tud_hid_report(uint8_t report_id, void const* report, uint8_t len); /*------------- Callbacks (Weak is optional) -------------*/ -uint16_t tud_hid_generic_get_report_cb(uint8_t report_id, hid_report_type_t report_type, uint8_t* buffer, uint16_t reqlen); -void tud_hid_generic_set_report_cb(uint8_t report_id, hid_report_type_t report_type, uint8_t const* buffer, uint16_t bufsize); +/** 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) + */ +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); //--------------------------------------------------------------------+ // KEYBOARD API @@ -70,13 +94,6 @@ void tud_hid_generic_set_report_cb(uint8_t report_id, hid_report_type_t repo /** \defgroup Keyboard_Device Device * @{ */ -/** 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 - */ -bool tud_hid_keyboard_ready(void); -bool tud_hid_keyboard_is_boot_protocol(void); - bool tud_hid_keyboard_keycode(uint8_t modifier, uint8_t keycode[6]); static inline bool tud_hid_keyboard_key_release(void) { return tud_hid_keyboard_keycode(0, NULL); } @@ -95,29 +112,6 @@ extern const hid_ascii_to_keycode_entry_t HID_ASCII_TO_KEYCODE[128]; /*------------- 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) - */ -ATTR_WEAK uint16_t tud_hid_keyboard_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. - */ -ATTR_WEAK void tud_hid_keyboard_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_keyboard_set_protocol_cb(bool boot_protocol); /** @} */ @@ -132,12 +126,6 @@ ATTR_WEAK void tud_hid_keyboard_set_report_cb(uint8_t report_id, hid_report_type /** \defgroup Mouse_Device Device * @{ */ -/** \brief Check if the interface is currently busy or not - * \retval true if the interface is busy meaning the stack is still transferring/waiting data from/to host - * \retval false if the interface is not busy meaning the stack successfully transferred data from/to host - * \note This function is primarily used for polling/waiting result after \ref tusbd_hid_mouse_send. - */ -bool tud_hid_mouse_ready(void); bool tud_hid_mouse_is_boot_protocol(void); bool tud_hid_mouse_data(uint8_t buttons, int8_t x, int8_t y, int8_t scroll, int8_t pan); @@ -155,31 +143,6 @@ static inline bool tud_hid_mouse_button_release(void) return tud_hid_mouse_data(0, 0, 0, 0, 0); } -/*------------- Callbacks (Weak is optional) -------------*/ - -/** - * Callback function that is 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 buffer 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 - */ -ATTR_WEAK uint16_t tud_hid_mouse_get_report_cb(uint8_t report_id, hid_report_type_t report_type, uint8_t* buffer, uint16_t reqlen); - -/** - * Callback function that is 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 buffer containing the report's data - * \param[in] bufsize number of bytes in the \a p_report_data - * \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. - */ -ATTR_WEAK void tud_hid_mouse_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_mouse_set_protocol_cb(bool boot_protocol); #endif diff --git a/src/device/usbd.h b/src/device/usbd.h index 13c282e91..f90c40aab 100644 --- a/src/device/usbd.h +++ b/src/device/usbd.h @@ -45,11 +45,7 @@ typedef struct { uint8_t const** string_arr; ///< a array of pointers to string descriptors uint16_t string_count; - struct { - uint8_t const* generic; - uint8_t const* boot_keyboard; - uint8_t const* boot_mouse; - } hid_report; + uint8_t const* hid_report; }tud_desc_set_t; diff --git a/src/device/usbd_auto_desc.c b/src/device/usbd_auto_desc.c index a980df1e0..6b4bbced5 100644 --- a/src/device/usbd_auto_desc.c +++ b/src/device/usbd_auto_desc.c @@ -562,6 +562,9 @@ tud_desc_set_t const _usbd_auto_desc_set = .device = NULL, // no auto device .config = &_desc_auto_config_struct, + .hid_report = _desc_auto_hid_boot_kbd_report + +#if 0 .hid_report = { #if AUTO_DESC_HID_GENERIC @@ -578,6 +581,7 @@ tud_desc_set_t const _usbd_auto_desc_set = .boot_mouse = _desc_auto_hid_boot_mse_report #endif } +#endif }; #endif