From d5f8da44d13d3410f6c710432062807b52ebf407 Mon Sep 17 00:00:00 2001 From: Marcelo Bezerra <23555060+mmosca@users.noreply.github.com> Date: Mon, 7 Jun 2021 19:04:21 +0200 Subject: Add optional support for 32 gamepad buttons --- src/class/hid/hid.h | 40 ++++++++++++++++++++++++++++++++++++++-- src/class/hid/hid_device.h | 9 ++++----- 2 files changed, 42 insertions(+), 7 deletions(-) (limited to 'src/class') diff --git a/src/class/hid/hid.h b/src/class/hid/hid.h index 5cc9233b5..3c45dc6c1 100644 --- a/src/class/hid/hid.h +++ b/src/class/hid/hid.h @@ -33,10 +33,12 @@ #include "common/tusb_common.h" + #ifdef __cplusplus extern "C" { #endif + //--------------------------------------------------------------------+ // Common Definitions //--------------------------------------------------------------------+ @@ -150,6 +152,22 @@ typedef enum /** @} */ +#ifndef CFG_TUD_HID_EP_BUFSIZE + #define CFG_TUD_HID_EP_BUFSIZE 64 +#endif + +#ifndef CFG_TUSB_MAX_BUTTONS + #define CFG_TUSB_MAX_BUTTONS 16 +#endif + +#if (CFG_TUSB_MAX_BUTTONS == 16) + typedef uint16_t hid_gamepad_buttons_t; +#elif (CFG_TUSB_MAX_BUTTONS == 32) + typedef uint32_t hid_gamepad_buttons_t; +#else + #error "Invalid CFG_TUSB_MAX_BUTTONS value." +#endif + //--------------------------------------------------------------------+ // GAMEPAD //--------------------------------------------------------------------+ @@ -201,7 +219,7 @@ typedef struct TU_ATTR_PACKED int8_t rx; ///< Delta Rx movement of analog left trigger int8_t ry; ///< Delta Ry movement of analog right trigger uint8_t hat; ///< Buttons mask for currently pressed buttons in the DPad/hat - uint16_t buttons; ///< Buttons mask for currently pressed buttons + hid_gamepad_buttons_t buttons; ///< Buttons mask for currently pressed buttons }hid_gamepad_report_t; /// Standard Gamepad Buttons Bitmap (from Linux input event codes) @@ -222,7 +240,25 @@ typedef enum GAMEPAD_BUTTON_MODE = TU_BIT(12), ///< Mode button GAMEPAD_BUTTON_THUMBL = TU_BIT(13), ///< L3 button GAMEPAD_BUTTON_THUMBR = TU_BIT(14), ///< R3 button -//GAMEPAD_BUTTON_ = TU_BIT(15), ///< Undefined button + GAMEPAD_BUTTON_15 = TU_BIT(15), ///< Button 15 +#ifdef CFG_TUSB_MAX_BUTTONS_32 + GAMEPAD_BUTTON_17 = TU_BIT(16), ///< Button 17 + GAMEPAD_BUTTON_18 = TU_BIT(17), ///< Button 16 + GAMEPAD_BUTTON_19 = TU_BIT(18), ///< Button 16 + GAMEPAD_BUTTON_20 = TU_BIT(19), ///< Button 16 + GAMEPAD_BUTTON_21 = TU_BIT(20), ///< Button 16 + GAMEPAD_BUTTON_22 = TU_BIT(21), ///< Button 16 + GAMEPAD_BUTTON_23 = TU_BIT(22), ///< Button 16 + GAMEPAD_BUTTON_24 = TU_BIT(23), ///< Button 16 + GAMEPAD_BUTTON_25 = TU_BIT(24), ///< Button 16 + GAMEPAD_BUTTON_26 = TU_BIT(25), ///< Button 16 + GAMEPAD_BUTTON_27 = TU_BIT(26), ///< Button 16 + GAMEPAD_BUTTON_28 = TU_BIT(27), ///< Button 16 + GAMEPAD_BUTTON_29 = TU_BIT(28), ///< Button 16 + GAMEPAD_BUTTON_30 = TU_BIT(29), ///< Button 16 + GAMEPAD_BUTTON_31 = TU_BIT(30), ///< Button 16 + GAMEPAD_BUTTON_32 = TU_BIT(31), ///< Button 16 +#endif }hid_gamepad_button_bm_t; /// Standard Gamepad HAT/DPAD Buttons (from Linux input event codes) diff --git a/src/class/hid/hid_device.h b/src/class/hid/hid_device.h index beb755888..0ff5cab3f 100644 --- a/src/class/hid/hid_device.h +++ b/src/class/hid/hid_device.h @@ -43,9 +43,8 @@ #define CFG_TUD_HID_EP_BUFSIZE CFG_TUD_HID_BUFSIZE #endif -#ifndef CFG_TUD_HID_EP_BUFSIZE - #define CFG_TUD_HID_EP_BUFSIZE 64 -#endif + + //--------------------------------------------------------------------+ // Application API (Multiple Instances) @@ -344,10 +343,10 @@ static inline bool tud_hid_gamepad_report(uint8_t report_id, int8_t x, int8_t y /* 16 bit Button Map */ \ HID_USAGE_PAGE ( HID_USAGE_PAGE_BUTTON ) ,\ HID_USAGE_MIN ( 1 ) ,\ - HID_USAGE_MAX ( 16 ) ,\ + HID_USAGE_MAX ( CFG_TUSB_MAX_BUTTONS ) ,\ HID_LOGICAL_MIN ( 0 ) ,\ HID_LOGICAL_MAX ( 1 ) ,\ - HID_REPORT_COUNT ( 16 ) ,\ + HID_REPORT_COUNT ( CFG_TUSB_MAX_BUTTONS ) ,\ HID_REPORT_SIZE ( 1 ) ,\ HID_INPUT ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE ) ,\ HID_COLLECTION_END \ -- cgit v1.3.1 From 3842c806a6bd90a3be8431c4fa3fb8c46220a6bf Mon Sep 17 00:00:00 2001 From: Marcelo Bezerra <23555060+mmosca@users.noreply.github.com> Date: Mon, 7 Jun 2021 20:16:05 +0200 Subject: clean up --- src/class/hid/hid.h | 16 ++++++---------- src/class/hid/hid_device.h | 4 ++-- src/tusb_option.h | 17 +++++++++++++---- 3 files changed, 21 insertions(+), 16 deletions(-) (limited to 'src/class') diff --git a/src/class/hid/hid.h b/src/class/hid/hid.h index 3c45dc6c1..eac30c62e 100644 --- a/src/class/hid/hid.h +++ b/src/class/hid/hid.h @@ -152,20 +152,16 @@ typedef enum /** @} */ -#ifndef CFG_TUD_HID_EP_BUFSIZE - #define CFG_TUD_HID_EP_BUFSIZE 64 +#ifndef CFG_TUD_MAX_BUTTONS + #define CFG_TUD_MAX_BUTTONS 16 #endif -#ifndef CFG_TUSB_MAX_BUTTONS - #define CFG_TUSB_MAX_BUTTONS 16 -#endif - -#if (CFG_TUSB_MAX_BUTTONS == 16) +#if (CFG_TUD_MAX_BUTTONS == 16) typedef uint16_t hid_gamepad_buttons_t; -#elif (CFG_TUSB_MAX_BUTTONS == 32) +#elif (CFG_TUD_MAX_BUTTONS == 32) typedef uint32_t hid_gamepad_buttons_t; #else - #error "Invalid CFG_TUSB_MAX_BUTTONS value." + #error "Invalid CFG_TUD_MAX_BUTTONS value." #endif //--------------------------------------------------------------------+ @@ -241,7 +237,7 @@ typedef enum GAMEPAD_BUTTON_THUMBL = TU_BIT(13), ///< L3 button GAMEPAD_BUTTON_THUMBR = TU_BIT(14), ///< R3 button GAMEPAD_BUTTON_15 = TU_BIT(15), ///< Button 15 -#ifdef CFG_TUSB_MAX_BUTTONS_32 +#if (CFG_TUD_MAX_BUTTONS > 16) GAMEPAD_BUTTON_17 = TU_BIT(16), ///< Button 17 GAMEPAD_BUTTON_18 = TU_BIT(17), ///< Button 16 GAMEPAD_BUTTON_19 = TU_BIT(18), ///< Button 16 diff --git a/src/class/hid/hid_device.h b/src/class/hid/hid_device.h index 0ff5cab3f..39ec6be76 100644 --- a/src/class/hid/hid_device.h +++ b/src/class/hid/hid_device.h @@ -343,10 +343,10 @@ static inline bool tud_hid_gamepad_report(uint8_t report_id, int8_t x, int8_t y /* 16 bit Button Map */ \ HID_USAGE_PAGE ( HID_USAGE_PAGE_BUTTON ) ,\ HID_USAGE_MIN ( 1 ) ,\ - HID_USAGE_MAX ( CFG_TUSB_MAX_BUTTONS ) ,\ + HID_USAGE_MAX ( CFG_TUD_MAX_BUTTONS ) ,\ HID_LOGICAL_MIN ( 0 ) ,\ HID_LOGICAL_MAX ( 1 ) ,\ - HID_REPORT_COUNT ( CFG_TUSB_MAX_BUTTONS ) ,\ + HID_REPORT_COUNT ( CFG_TUD_MAX_BUTTONS ) ,\ HID_REPORT_SIZE ( 1 ) ,\ HID_INPUT ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE ) ,\ HID_COLLECTION_END \ diff --git a/src/tusb_option.h b/src/tusb_option.h index a7939d9c7..3adb4fcf9 100644 --- a/src/tusb_option.h +++ b/src/tusb_option.h @@ -309,6 +309,16 @@ #define TUP_MCU_STRICT_ALIGN 0 #endif +//--------------------------------------------------------------------+ +// HID Gamepad options +//--------------------------------------------------------------------+ + +// CFG_TUD_MAX_BUTTONS lets you choose if you want 16 or 32 buttons on you HID gamepad +#ifndef CFG_TUD_MAX_BUTTONS + #define CFG_TUD_MAX_BUTTONS 16 +#endif + + //------------------------------------------------------------------ // Configuration Validation //------------------------------------------------------------------ @@ -316,10 +326,9 @@ #error Control Endpoint Max Packet Size cannot be larger than 64 #endif -#ifndef TUD_OPT_GAMEPAD_32_BUTTONS - #define CFG_TUSB_MAX_BUTTONS 16 -#else - #define CFG_TUSB_MAX_BUTTONS 32 + +#if (CFG_TUD_MAX_BUTTONS != 16 && CFG_TUD_MAX_BUTTONS != 32) + #error "Unsupported CFG_TUD_MAX_BUTTONS" #endif #endif /* _TUSB_OPTION_H_ */ -- cgit v1.3.1 From ce634f226e59cccfb236371f85a62cb610ef80f6 Mon Sep 17 00:00:00 2001 From: Marcelo Bezerra <23555060+mmosca@users.noreply.github.com> Date: Mon, 7 Jun 2021 20:44:53 +0200 Subject: remove excess empty lines --- src/class/hid/hid.h | 2 -- src/tusb_option.h | 1 - 2 files changed, 3 deletions(-) (limited to 'src/class') diff --git a/src/class/hid/hid.h b/src/class/hid/hid.h index eac30c62e..0764bc228 100644 --- a/src/class/hid/hid.h +++ b/src/class/hid/hid.h @@ -33,12 +33,10 @@ #include "common/tusb_common.h" - #ifdef __cplusplus extern "C" { #endif - //--------------------------------------------------------------------+ // Common Definitions //--------------------------------------------------------------------+ diff --git a/src/tusb_option.h b/src/tusb_option.h index 3adb4fcf9..6a6e31ee9 100644 --- a/src/tusb_option.h +++ b/src/tusb_option.h @@ -318,7 +318,6 @@ #define CFG_TUD_MAX_BUTTONS 16 #endif - //------------------------------------------------------------------ // Configuration Validation //------------------------------------------------------------------ -- cgit v1.3.1 From e12195705c2b849379145618691c2e8564762aa7 Mon Sep 17 00:00:00 2001 From: Marcelo Bezerra <23555060+mmosca@users.noreply.github.com> Date: Tue, 8 Jun 2021 09:24:50 +0200 Subject: Pull request changes Remove configuration options and just bump number of buttons to 32 Fix button numbereing and comments in --- examples/device/hid_composite/src/tusb_config.h | 4 --- src/class/hid/hid.h | 48 +++++++++---------------- src/class/hid/hid_device.h | 4 +-- src/tusb_option.h | 14 -------- 4 files changed, 19 insertions(+), 51 deletions(-) (limited to 'src/class') diff --git a/examples/device/hid_composite/src/tusb_config.h b/examples/device/hid_composite/src/tusb_config.h index bc9192d13..3e608ed37 100644 --- a/examples/device/hid_composite/src/tusb_config.h +++ b/examples/device/hid_composite/src/tusb_config.h @@ -104,10 +104,6 @@ // HID buffer size Should be sufficient to hold ID (if any) + Data #define CFG_TUD_HID_EP_BUFSIZE 16 -// Number of button on the gamepad 16 or 32 -#define CFG_TUD_MAX_BUTTONS 16 - - #ifdef __cplusplus } #endif diff --git a/src/class/hid/hid.h b/src/class/hid/hid.h index 0764bc228..a344c9100 100644 --- a/src/class/hid/hid.h +++ b/src/class/hid/hid.h @@ -150,18 +150,6 @@ typedef enum /** @} */ -#ifndef CFG_TUD_MAX_BUTTONS - #define CFG_TUD_MAX_BUTTONS 16 -#endif - -#if (CFG_TUD_MAX_BUTTONS == 16) - typedef uint16_t hid_gamepad_buttons_t; -#elif (CFG_TUD_MAX_BUTTONS == 32) - typedef uint32_t hid_gamepad_buttons_t; -#else - #error "Invalid CFG_TUD_MAX_BUTTONS value." -#endif - //--------------------------------------------------------------------+ // GAMEPAD //--------------------------------------------------------------------+ @@ -213,7 +201,7 @@ typedef struct TU_ATTR_PACKED int8_t rx; ///< Delta Rx movement of analog left trigger int8_t ry; ///< Delta Ry movement of analog right trigger uint8_t hat; ///< Buttons mask for currently pressed buttons in the DPad/hat - hid_gamepad_buttons_t buttons; ///< Buttons mask for currently pressed buttons + uint32_t buttons; ///< Buttons mask for currently pressed buttons }hid_gamepad_report_t; /// Standard Gamepad Buttons Bitmap (from Linux input event codes) @@ -234,25 +222,23 @@ typedef enum GAMEPAD_BUTTON_MODE = TU_BIT(12), ///< Mode button GAMEPAD_BUTTON_THUMBL = TU_BIT(13), ///< L3 button GAMEPAD_BUTTON_THUMBR = TU_BIT(14), ///< R3 button - GAMEPAD_BUTTON_15 = TU_BIT(15), ///< Button 15 -#if (CFG_TUD_MAX_BUTTONS > 16) + GAMEPAD_BUTTON_16 = TU_BIT(15), ///< Button 15 GAMEPAD_BUTTON_17 = TU_BIT(16), ///< Button 17 - GAMEPAD_BUTTON_18 = TU_BIT(17), ///< Button 16 - GAMEPAD_BUTTON_19 = TU_BIT(18), ///< Button 16 - GAMEPAD_BUTTON_20 = TU_BIT(19), ///< Button 16 - GAMEPAD_BUTTON_21 = TU_BIT(20), ///< Button 16 - GAMEPAD_BUTTON_22 = TU_BIT(21), ///< Button 16 - GAMEPAD_BUTTON_23 = TU_BIT(22), ///< Button 16 - GAMEPAD_BUTTON_24 = TU_BIT(23), ///< Button 16 - GAMEPAD_BUTTON_25 = TU_BIT(24), ///< Button 16 - GAMEPAD_BUTTON_26 = TU_BIT(25), ///< Button 16 - GAMEPAD_BUTTON_27 = TU_BIT(26), ///< Button 16 - GAMEPAD_BUTTON_28 = TU_BIT(27), ///< Button 16 - GAMEPAD_BUTTON_29 = TU_BIT(28), ///< Button 16 - GAMEPAD_BUTTON_30 = TU_BIT(29), ///< Button 16 - GAMEPAD_BUTTON_31 = TU_BIT(30), ///< Button 16 - GAMEPAD_BUTTON_32 = TU_BIT(31), ///< Button 16 -#endif + GAMEPAD_BUTTON_18 = TU_BIT(17), ///< Button 18 + GAMEPAD_BUTTON_19 = TU_BIT(18), ///< Button 19 + GAMEPAD_BUTTON_20 = TU_BIT(19), ///< Button 20 + GAMEPAD_BUTTON_21 = TU_BIT(20), ///< Button 21 + GAMEPAD_BUTTON_22 = TU_BIT(21), ///< Button 22 + GAMEPAD_BUTTON_23 = TU_BIT(22), ///< Button 23 + GAMEPAD_BUTTON_24 = TU_BIT(23), ///< Button 24 + GAMEPAD_BUTTON_25 = TU_BIT(24), ///< Button 25 + GAMEPAD_BUTTON_26 = TU_BIT(25), ///< Button 26 + GAMEPAD_BUTTON_27 = TU_BIT(26), ///< Button 27 + GAMEPAD_BUTTON_28 = TU_BIT(27), ///< Button 28 + GAMEPAD_BUTTON_29 = TU_BIT(28), ///< Button 29 + GAMEPAD_BUTTON_30 = TU_BIT(29), ///< Button 30 + GAMEPAD_BUTTON_31 = TU_BIT(30), ///< Button 31 + GAMEPAD_BUTTON_32 = TU_BIT(31), ///< Button 32 }hid_gamepad_button_bm_t; /// Standard Gamepad HAT/DPAD Buttons (from Linux input event codes) diff --git a/src/class/hid/hid_device.h b/src/class/hid/hid_device.h index 39ec6be76..543bc3b3b 100644 --- a/src/class/hid/hid_device.h +++ b/src/class/hid/hid_device.h @@ -343,10 +343,10 @@ static inline bool tud_hid_gamepad_report(uint8_t report_id, int8_t x, int8_t y /* 16 bit Button Map */ \ HID_USAGE_PAGE ( HID_USAGE_PAGE_BUTTON ) ,\ HID_USAGE_MIN ( 1 ) ,\ - HID_USAGE_MAX ( CFG_TUD_MAX_BUTTONS ) ,\ + HID_USAGE_MAX ( 32 ) ,\ HID_LOGICAL_MIN ( 0 ) ,\ HID_LOGICAL_MAX ( 1 ) ,\ - HID_REPORT_COUNT ( CFG_TUD_MAX_BUTTONS ) ,\ + HID_REPORT_COUNT ( 32 ) ,\ HID_REPORT_SIZE ( 1 ) ,\ HID_INPUT ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE ) ,\ HID_COLLECTION_END \ diff --git a/src/tusb_option.h b/src/tusb_option.h index 6a6e31ee9..5cfcc08e2 100644 --- a/src/tusb_option.h +++ b/src/tusb_option.h @@ -309,15 +309,6 @@ #define TUP_MCU_STRICT_ALIGN 0 #endif -//--------------------------------------------------------------------+ -// HID Gamepad options -//--------------------------------------------------------------------+ - -// CFG_TUD_MAX_BUTTONS lets you choose if you want 16 or 32 buttons on you HID gamepad -#ifndef CFG_TUD_MAX_BUTTONS - #define CFG_TUD_MAX_BUTTONS 16 -#endif - //------------------------------------------------------------------ // Configuration Validation //------------------------------------------------------------------ @@ -325,11 +316,6 @@ #error Control Endpoint Max Packet Size cannot be larger than 64 #endif - -#if (CFG_TUD_MAX_BUTTONS != 16 && CFG_TUD_MAX_BUTTONS != 32) - #error "Unsupported CFG_TUD_MAX_BUTTONS" -#endif - #endif /* _TUSB_OPTION_H_ */ /** @} */ -- cgit v1.3.1 From de71e72e31ca1d534e5e0413f9da0a0e84a9aa69 Mon Sep 17 00:00:00 2001 From: Marcelo Bezerra <23555060+mmosca@users.noreply.github.com> Date: Tue, 8 Jun 2021 09:26:17 +0200 Subject: Fix comment in hid.h --- src/class/hid/hid.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/class') diff --git a/src/class/hid/hid.h b/src/class/hid/hid.h index a344c9100..8ab24ab3e 100644 --- a/src/class/hid/hid.h +++ b/src/class/hid/hid.h @@ -217,12 +217,12 @@ typedef enum GAMEPAD_BUTTON_TR = TU_BIT(7), ///< R1 button GAMEPAD_BUTTON_TL2 = TU_BIT(8), ///< L2 button GAMEPAD_BUTTON_TR2 = TU_BIT(9), ///< R2 button - GAMEPAD_BUTTON_SELECT = TU_BIT(10), ///< Select button + GAMEPAD_BUTTON_SELECT = TU_BIT(10), ///< Select button GAMEPAD_BUTTON_START = TU_BIT(11), ///< Start button GAMEPAD_BUTTON_MODE = TU_BIT(12), ///< Mode button GAMEPAD_BUTTON_THUMBL = TU_BIT(13), ///< L3 button GAMEPAD_BUTTON_THUMBR = TU_BIT(14), ///< R3 button - GAMEPAD_BUTTON_16 = TU_BIT(15), ///< Button 15 + GAMEPAD_BUTTON_16 = TU_BIT(15), ///< Button 16 GAMEPAD_BUTTON_17 = TU_BIT(16), ///< Button 17 GAMEPAD_BUTTON_18 = TU_BIT(17), ///< Button 18 GAMEPAD_BUTTON_19 = TU_BIT(18), ///< Button 19 -- cgit v1.3.1 From d3b6e28387c63a1519cd1958ee7e21bd2980d099 Mon Sep 17 00:00:00 2001 From: Marcelo Bezerra <23555060+mmosca@users.noreply.github.com> Date: Tue, 8 Jun 2021 09:31:40 +0200 Subject: indent fix --- src/class/hid/hid.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/class') diff --git a/src/class/hid/hid.h b/src/class/hid/hid.h index 8ab24ab3e..782a1a552 100644 --- a/src/class/hid/hid.h +++ b/src/class/hid/hid.h @@ -217,7 +217,7 @@ typedef enum GAMEPAD_BUTTON_TR = TU_BIT(7), ///< R1 button GAMEPAD_BUTTON_TL2 = TU_BIT(8), ///< L2 button GAMEPAD_BUTTON_TR2 = TU_BIT(9), ///< R2 button - GAMEPAD_BUTTON_SELECT = TU_BIT(10), ///< Select button + GAMEPAD_BUTTON_SELECT = TU_BIT(10), ///< Select button GAMEPAD_BUTTON_START = TU_BIT(11), ///< Start button GAMEPAD_BUTTON_MODE = TU_BIT(12), ///< Mode button GAMEPAD_BUTTON_THUMBL = TU_BIT(13), ///< L3 button -- cgit v1.3.1 From e393fb32a0830e9d371f3e335cc0cca92b75cbbb Mon Sep 17 00:00:00 2001 From: Marcelo Bezerra <23555060+mmosca@users.noreply.github.com> Date: Tue, 8 Jun 2021 09:39:53 +0200 Subject: re-adding ifdef removed accidentally --- src/class/hid/hid_device.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'src/class') diff --git a/src/class/hid/hid_device.h b/src/class/hid/hid_device.h index 543bc3b3b..1d1213e3f 100644 --- a/src/class/hid/hid_device.h +++ b/src/class/hid/hid_device.h @@ -43,8 +43,9 @@ #define CFG_TUD_HID_EP_BUFSIZE CFG_TUD_HID_BUFSIZE #endif - - +#ifndef CFG_TUD_HID_EP_BUFSIZE + #define CFG_TUD_HID_EP_BUFSIZE 64 +#endif //--------------------------------------------------------------------+ // Application API (Multiple Instances) -- cgit v1.3.1 From 2c0947ebb621e3e53933300cbfd1dce7e0f31338 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 9 Jun 2021 10:30:13 +0700 Subject: update gamepad helper --- src/class/hid/hid.h | 1 + src/class/hid/hid_device.c | 2 +- src/class/hid/hid_device.h | 6 +++--- 3 files changed, 5 insertions(+), 4 deletions(-) (limited to 'src/class') diff --git a/src/class/hid/hid.h b/src/class/hid/hid.h index 782a1a552..b4e8a9c4c 100644 --- a/src/class/hid/hid.h +++ b/src/class/hid/hid.h @@ -222,6 +222,7 @@ typedef enum GAMEPAD_BUTTON_MODE = TU_BIT(12), ///< Mode button GAMEPAD_BUTTON_THUMBL = TU_BIT(13), ///< L3 button GAMEPAD_BUTTON_THUMBR = TU_BIT(14), ///< R3 button + // Note: Button number start from 1 on host OS GAMEPAD_BUTTON_16 = TU_BIT(15), ///< Button 16 GAMEPAD_BUTTON_17 = TU_BIT(16), ///< Button 17 GAMEPAD_BUTTON_18 = TU_BIT(17), ///< Button 18 diff --git a/src/class/hid/hid_device.c b/src/class/hid/hid_device.c index 151a36225..e44f282c4 100644 --- a/src/class/hid/hid_device.c +++ b/src/class/hid/hid_device.c @@ -149,7 +149,7 @@ bool tud_hid_n_mouse_report(uint8_t instance, uint8_t report_id, } bool tud_hid_n_gamepad_report(uint8_t instance, uint8_t report_id, - int8_t x, int8_t y, int8_t z, int8_t rz, int8_t rx, int8_t ry, uint8_t hat, uint16_t buttons) + int8_t x, int8_t y, int8_t z, int8_t rz, int8_t rx, int8_t ry, uint8_t hat, uint32_t buttons) { hid_gamepad_report_t report = { diff --git a/src/class/hid/hid_device.h b/src/class/hid/hid_device.h index 1d1213e3f..19e9315f4 100644 --- a/src/class/hid/hid_device.h +++ b/src/class/hid/hid_device.h @@ -72,9 +72,9 @@ bool tud_hid_n_keyboard_report(uint8_t instance, uint8_t report_id, uint8_t modi // use template layout report as defined by hid_mouse_report_t bool tud_hid_n_mouse_report(uint8_t instance, uint8_t report_id, uint8_t buttons, int8_t x, int8_t y, int8_t vertical, int8_t horizontal); -// Gamepad: convenient helper to send mouse report if application +// Gamepad: convenient helper to send gamepad report if application // use template layout report TUD_HID_REPORT_DESC_GAMEPAD -bool tud_hid_n_gamepad_report(uint8_t instance, uint8_t report_id, int8_t x, int8_t y, int8_t z, int8_t rz, int8_t rx, int8_t ry, uint8_t hat, uint16_t buttons); +bool tud_hid_n_gamepad_report(uint8_t instance, uint8_t report_id, int8_t x, int8_t y, int8_t z, int8_t rz, int8_t rx, int8_t ry, uint8_t hat, uint32_t buttons); //--------------------------------------------------------------------+ // Application API (Single Port) @@ -85,7 +85,7 @@ static inline uint8_t tud_hid_get_protocol(void); static inline bool tud_hid_report(uint8_t report_id, void const* report, uint8_t len); static inline bool tud_hid_keyboard_report(uint8_t report_id, uint8_t modifier, uint8_t keycode[6]); static inline bool tud_hid_mouse_report(uint8_t report_id, uint8_t buttons, int8_t x, int8_t y, int8_t vertical, int8_t horizontal); -static inline bool tud_hid_gamepad_report(uint8_t report_id, int8_t x, int8_t y, int8_t z, int8_t rz, int8_t rx, int8_t ry, uint8_t hat, uint16_t buttons); +static inline bool tud_hid_gamepad_report(uint8_t report_id, int8_t x, int8_t y, int8_t z, int8_t rz, int8_t rx, int8_t ry, uint8_t hat, uint32_t buttons); //--------------------------------------------------------------------+ // Callbacks (Weak is optional) -- cgit v1.3.1 From dca9bc97d66fe2912d2643e4f36cb839d08a31da Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 9 Jun 2021 10:45:37 +0700 Subject: miss a helper --- src/class/hid/hid_device.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/class') diff --git a/src/class/hid/hid_device.h b/src/class/hid/hid_device.h index 19e9315f4..e2c950dd1 100644 --- a/src/class/hid/hid_device.h +++ b/src/class/hid/hid_device.h @@ -152,7 +152,7 @@ static inline bool tud_hid_mouse_report(uint8_t report_id, uint8_t buttons, int8 return tud_hid_n_mouse_report(0, report_id, buttons, x, y, vertical, horizontal); } -static inline bool tud_hid_gamepad_report(uint8_t report_id, int8_t x, int8_t y, int8_t z, int8_t rz, int8_t rx, int8_t ry, uint8_t hat, uint16_t buttons) +static inline bool tud_hid_gamepad_report(uint8_t report_id, int8_t x, int8_t y, int8_t z, int8_t rz, int8_t rx, int8_t ry, uint8_t hat, uint32_t buttons) { return tud_hid_n_gamepad_report(0, report_id, x, y, z, rz, rx, ry, hat, buttons); } -- cgit v1.3.1 From a8abbcc34df72ff5a63d5567d26276ce84e6d162 Mon Sep 17 00:00:00 2001 From: Marcelo Bezerra <23555060+mmosca@users.noreply.github.com> Date: Wed, 9 Jun 2021 12:14:04 +0200 Subject: Change buttons to start from 0 --- src/class/hid/hid.h | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) (limited to 'src/class') diff --git a/src/class/hid/hid.h b/src/class/hid/hid.h index b4e8a9c4c..3c616d691 100644 --- a/src/class/hid/hid.h +++ b/src/class/hid/hid.h @@ -222,24 +222,23 @@ typedef enum GAMEPAD_BUTTON_MODE = TU_BIT(12), ///< Mode button GAMEPAD_BUTTON_THUMBL = TU_BIT(13), ///< L3 button GAMEPAD_BUTTON_THUMBR = TU_BIT(14), ///< R3 button - // Note: Button number start from 1 on host OS - GAMEPAD_BUTTON_16 = TU_BIT(15), ///< Button 16 - GAMEPAD_BUTTON_17 = TU_BIT(16), ///< Button 17 - GAMEPAD_BUTTON_18 = TU_BIT(17), ///< Button 18 - GAMEPAD_BUTTON_19 = TU_BIT(18), ///< Button 19 - GAMEPAD_BUTTON_20 = TU_BIT(19), ///< Button 20 - GAMEPAD_BUTTON_21 = TU_BIT(20), ///< Button 21 - GAMEPAD_BUTTON_22 = TU_BIT(21), ///< Button 22 - GAMEPAD_BUTTON_23 = TU_BIT(22), ///< Button 23 - GAMEPAD_BUTTON_24 = TU_BIT(23), ///< Button 24 - GAMEPAD_BUTTON_25 = TU_BIT(24), ///< Button 25 - GAMEPAD_BUTTON_26 = TU_BIT(25), ///< Button 26 - GAMEPAD_BUTTON_27 = TU_BIT(26), ///< Button 27 - GAMEPAD_BUTTON_28 = TU_BIT(27), ///< Button 28 - GAMEPAD_BUTTON_29 = TU_BIT(28), ///< Button 29 - GAMEPAD_BUTTON_30 = TU_BIT(29), ///< Button 30 - GAMEPAD_BUTTON_31 = TU_BIT(30), ///< Button 31 - GAMEPAD_BUTTON_32 = TU_BIT(31), ///< Button 32 + GAMEPAD_BUTTON_15 = TU_BIT(15), ///< Button 15 + GAMEPAD_BUTTON_16 = TU_BIT(16), ///< Button 16 + GAMEPAD_BUTTON_17 = TU_BIT(17), ///< Button 17 + GAMEPAD_BUTTON_18 = TU_BIT(18), ///< Button 18 + GAMEPAD_BUTTON_19 = TU_BIT(19), ///< Button 19 + GAMEPAD_BUTTON_20 = TU_BIT(20), ///< Button 20 + GAMEPAD_BUTTON_21 = TU_BIT(21), ///< Button 21 + GAMEPAD_BUTTON_22 = TU_BIT(22), ///< Button 22 + GAMEPAD_BUTTON_23 = TU_BIT(23), ///< Button 23 + GAMEPAD_BUTTON_24 = TU_BIT(24), ///< Button 24 + GAMEPAD_BUTTON_25 = TU_BIT(25), ///< Button 25 + GAMEPAD_BUTTON_26 = TU_BIT(26), ///< Button 26 + GAMEPAD_BUTTON_27 = TU_BIT(27), ///< Button 27 + GAMEPAD_BUTTON_28 = TU_BIT(28), ///< Button 28 + GAMEPAD_BUTTON_29 = TU_BIT(29), ///< Button 29 + GAMEPAD_BUTTON_30 = TU_BIT(30), ///< Button 30 + GAMEPAD_BUTTON_31 = TU_BIT(31), ///< Button 31 }hid_gamepad_button_bm_t; /// Standard Gamepad HAT/DPAD Buttons (from Linux input event codes) -- cgit v1.3.1 From cffa666bd1fd8f0ab45336b36eb79ab1a8f4663d Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 9 Jun 2021 17:45:14 +0700 Subject: use alias for button naming --- src/class/hid/hid.h | 93 ++++++++++++++++++++++++++++++++++------------------- 1 file changed, 60 insertions(+), 33 deletions(-) (limited to 'src/class') diff --git a/src/class/hid/hid.h b/src/class/hid/hid.h index 3c616d691..ec14c9c7c 100644 --- a/src/class/hid/hid.h +++ b/src/class/hid/hid.h @@ -204,43 +204,70 @@ typedef struct TU_ATTR_PACKED uint32_t buttons; ///< Buttons mask for currently pressed buttons }hid_gamepad_report_t; -/// Standard Gamepad Buttons Bitmap (from Linux input event codes) +/// Standard Gamepad Buttons Bitmap typedef enum { - GAMEPAD_BUTTON_A = TU_BIT(0), ///< A/South button - GAMEPAD_BUTTON_B = TU_BIT(1), ///< B/East button - GAMEPAD_BUTTON_C = TU_BIT(2), ///< C button - GAMEPAD_BUTTON_X = TU_BIT(3), ///< X/North button - GAMEPAD_BUTTON_Y = TU_BIT(4), ///< Y/West button - GAMEPAD_BUTTON_Z = TU_BIT(5), ///< Z button - GAMEPAD_BUTTON_TL = TU_BIT(6), ///< L1 button - GAMEPAD_BUTTON_TR = TU_BIT(7), ///< R1 button - GAMEPAD_BUTTON_TL2 = TU_BIT(8), ///< L2 button - GAMEPAD_BUTTON_TR2 = TU_BIT(9), ///< R2 button - GAMEPAD_BUTTON_SELECT = TU_BIT(10), ///< Select button - GAMEPAD_BUTTON_START = TU_BIT(11), ///< Start button - GAMEPAD_BUTTON_MODE = TU_BIT(12), ///< Mode button - GAMEPAD_BUTTON_THUMBL = TU_BIT(13), ///< L3 button - GAMEPAD_BUTTON_THUMBR = TU_BIT(14), ///< R3 button - GAMEPAD_BUTTON_15 = TU_BIT(15), ///< Button 15 - GAMEPAD_BUTTON_16 = TU_BIT(16), ///< Button 16 - GAMEPAD_BUTTON_17 = TU_BIT(17), ///< Button 17 - GAMEPAD_BUTTON_18 = TU_BIT(18), ///< Button 18 - GAMEPAD_BUTTON_19 = TU_BIT(19), ///< Button 19 - GAMEPAD_BUTTON_20 = TU_BIT(20), ///< Button 20 - GAMEPAD_BUTTON_21 = TU_BIT(21), ///< Button 21 - GAMEPAD_BUTTON_22 = TU_BIT(22), ///< Button 22 - GAMEPAD_BUTTON_23 = TU_BIT(23), ///< Button 23 - GAMEPAD_BUTTON_24 = TU_BIT(24), ///< Button 24 - GAMEPAD_BUTTON_25 = TU_BIT(25), ///< Button 25 - GAMEPAD_BUTTON_26 = TU_BIT(26), ///< Button 26 - GAMEPAD_BUTTON_27 = TU_BIT(27), ///< Button 27 - GAMEPAD_BUTTON_28 = TU_BIT(28), ///< Button 28 - GAMEPAD_BUTTON_29 = TU_BIT(29), ///< Button 29 - GAMEPAD_BUTTON_30 = TU_BIT(30), ///< Button 30 - GAMEPAD_BUTTON_31 = TU_BIT(31), ///< Button 31 + GAMEPAD_BUTTON_0 = TU_BIT(0), + GAMEPAD_BUTTON_1 = TU_BIT(1), + GAMEPAD_BUTTON_2 = TU_BIT(2), + GAMEPAD_BUTTON_3 = TU_BIT(3), + GAMEPAD_BUTTON_4 = TU_BIT(4), + GAMEPAD_BUTTON_5 = TU_BIT(5), + GAMEPAD_BUTTON_6 = TU_BIT(6), + GAMEPAD_BUTTON_7 = TU_BIT(7), + GAMEPAD_BUTTON_8 = TU_BIT(8), + GAMEPAD_BUTTON_9 = TU_BIT(9), + GAMEPAD_BUTTON_10 = TU_BIT(10), + GAMEPAD_BUTTON_11 = TU_BIT(11), + GAMEPAD_BUTTON_12 = TU_BIT(12), + GAMEPAD_BUTTON_13 = TU_BIT(13), + GAMEPAD_BUTTON_14 = TU_BIT(14), + GAMEPAD_BUTTON_15 = TU_BIT(15), + GAMEPAD_BUTTON_16 = TU_BIT(16), + GAMEPAD_BUTTON_17 = TU_BIT(17), + GAMEPAD_BUTTON_18 = TU_BIT(18), + GAMEPAD_BUTTON_19 = TU_BIT(19), + GAMEPAD_BUTTON_20 = TU_BIT(20), + GAMEPAD_BUTTON_21 = TU_BIT(21), + GAMEPAD_BUTTON_22 = TU_BIT(22), + GAMEPAD_BUTTON_23 = TU_BIT(23), + GAMEPAD_BUTTON_24 = TU_BIT(24), + GAMEPAD_BUTTON_25 = TU_BIT(25), + GAMEPAD_BUTTON_26 = TU_BIT(26), + GAMEPAD_BUTTON_27 = TU_BIT(27), + GAMEPAD_BUTTON_28 = TU_BIT(28), + GAMEPAD_BUTTON_29 = TU_BIT(29), + GAMEPAD_BUTTON_30 = TU_BIT(30), + GAMEPAD_BUTTON_31 = TU_BIT(31), }hid_gamepad_button_bm_t; +/// Standard Gamepad Buttons Naming from Linux input event codes +/// https://github.com/torvalds/linux/blob/master/include/uapi/linux/input-event-codes.h +#define GAMEPAD_BUTTON_A GAMEPAD_BUTTON_0 +#define GAMEPAD_BUTTON_SOUTH GAMEPAD_BUTTON_0 + +#define GAMEPAD_BUTTON_B GAMEPAD_BUTTON_1 +#define GAMEPAD_BUTTON_EAST GAMEPAD_BUTTON_1 + +#define GAMEPAD_BUTTON_C GAMEPAD_BUTTON_2 + +#define GAMEPAD_BUTTON_X GAMEPAD_BUTTON_3 +#define GAMEPAD_BUTTON_NORTH GAMEPAD_BUTTON_3 + +#define GAMEPAD_BUTTON_Y GAMEPAD_BUTTON_4 +#define GAMEPAD_BUTTON_WEST GAMEPAD_BUTTON_4 + +#define GAMEPAD_BUTTON_Z GAMEPAD_BUTTON_5 +#define GAMEPAD_BUTTON_TL GAMEPAD_BUTTON_6 +#define GAMEPAD_BUTTON_TR GAMEPAD_BUTTON_7 +#define GAMEPAD_BUTTON_TL2 GAMEPAD_BUTTON_8 +#define GAMEPAD_BUTTON_TR2 GAMEPAD_BUTTON_9 +#define GAMEPAD_BUTTON_SELECT GAMEPAD_BUTTON_10 +#define GAMEPAD_BUTTON_START GAMEPAD_BUTTON_11 +#define GAMEPAD_BUTTON_MODE GAMEPAD_BUTTON_12 +#define GAMEPAD_BUTTON_THUMBL GAMEPAD_BUTTON_13 +#define GAMEPAD_BUTTON_THUMBR GAMEPAD_BUTTON_14 + /// Standard Gamepad HAT/DPAD Buttons (from Linux input event codes) typedef enum { -- cgit v1.3.1 From 13cb0160421a6a66429e99ed103afba4f1c345ce Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 10 Jun 2021 16:48:20 +0700 Subject: add usbh_classdriver.h --- src/class/cdc/cdc_host.c | 2 ++ src/class/hid/hid_host.c | 2 ++ src/class/msc/msc_host.c | 2 ++ src/device/usbd_pvt.h | 2 +- src/host/hub.c | 1 + src/host/usbh.c | 7 ++--- src/host/usbh.h | 43 ++----------------------- src/host/usbh_classdriver.h | 76 +++++++++++++++++++++++++++++++++++++++++++++ src/host/usbh_hcd.h | 6 ---- 9 files changed, 89 insertions(+), 52 deletions(-) create mode 100644 src/host/usbh_classdriver.h (limited to 'src/class') diff --git a/src/class/cdc/cdc_host.c b/src/class/cdc/cdc_host.c index facf86d56..dbe5341e9 100644 --- a/src/class/cdc/cdc_host.c +++ b/src/class/cdc/cdc_host.c @@ -29,6 +29,8 @@ #if (TUSB_OPT_HOST_ENABLED && CFG_TUH_CDC) #include "host/usbh.h" +#include "host/usbh_classdriver.h" + #include "cdc_host.h" //--------------------------------------------------------------------+ diff --git a/src/class/hid/hid_host.c b/src/class/hid/hid_host.c index 83f3f3039..ec50f55f9 100644 --- a/src/class/hid/hid_host.c +++ b/src/class/hid/hid_host.c @@ -29,6 +29,8 @@ #if (TUSB_OPT_HOST_ENABLED && CFG_TUH_HID) #include "host/usbh.h" +#include "host/usbh_classdriver.h" + #include "hid_host.h" //--------------------------------------------------------------------+ diff --git a/src/class/msc/msc_host.c b/src/class/msc/msc_host.c index 205f0fd2d..176403e86 100644 --- a/src/class/msc/msc_host.c +++ b/src/class/msc/msc_host.c @@ -29,6 +29,8 @@ #if TUSB_OPT_HOST_ENABLED & CFG_TUH_MSC #include "host/usbh.h" +#include "host/usbh_classdriver.h" + #include "msc_host.h" //--------------------------------------------------------------------+ diff --git a/src/device/usbd_pvt.h b/src/device/usbd_pvt.h index 65fdadf51..b8d34d7b6 100644 --- a/src/device/usbd_pvt.h +++ b/src/device/usbd_pvt.h @@ -34,7 +34,7 @@ #endif //--------------------------------------------------------------------+ -// Class Drivers +// Class Driver API //--------------------------------------------------------------------+ typedef struct diff --git a/src/host/hub.c b/src/host/hub.c index f921027c7..b2761184d 100644 --- a/src/host/hub.c +++ b/src/host/hub.c @@ -29,6 +29,7 @@ #if (TUSB_OPT_HOST_ENABLED && CFG_TUH_HUB) #include "usbh.h" +#include "usbh_classdriver.h" #include "hub.h" //--------------------------------------------------------------------+ diff --git a/src/host/usbh.c b/src/host/usbh.c index ca0653409..ff66d5afd 100644 --- a/src/host/usbh.c +++ b/src/host/usbh.c @@ -24,7 +24,7 @@ * This file is part of the TinyUSB stack. */ -#include "common/tusb_common.h" +#include "tusb_option.h" #if TUSB_OPT_HOST_ENABLED @@ -32,10 +32,9 @@ #define CFG_TUH_TASK_QUEUE_SZ 16 #endif -//--------------------------------------------------------------------+ -// INCLUDE -//--------------------------------------------------------------------+ #include "tusb.h" +#include "host/usbh.h" +#include "host/usbh_classdriver.h" #include "hub.h" #include "usbh_hcd.h" diff --git a/src/host/usbh.h b/src/host/usbh.h index a9790b484..edc9e0808 100644 --- a/src/host/usbh.h +++ b/src/host/usbh.h @@ -40,27 +40,6 @@ //--------------------------------------------------------------------+ // MACRO CONSTANT TYPEDEF //--------------------------------------------------------------------+ -typedef enum tusb_interface_status_{ - TUSB_INTERFACE_STATUS_READY = 0, - TUSB_INTERFACE_STATUS_BUSY, - TUSB_INTERFACE_STATUS_COMPLETE, - TUSB_INTERFACE_STATUS_ERROR, - TUSB_INTERFACE_STATUS_INVALID_PARA -} tusb_interface_status_t; - -typedef struct { - #if CFG_TUSB_DEBUG >= 2 - char const* name; - #endif - - uint8_t class_code; - - void (* const init )(void); - bool (* const open )(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const * itf_desc, uint16_t* outlen); - bool (* const set_config )(uint8_t dev_addr, uint8_t itf_num); - bool (* const xfer_cb )(uint8_t dev_addr, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes); - void (* const close )(uint8_t dev_addr); -} usbh_class_driver_t; typedef bool (*tuh_control_complete_cb_t)(uint8_t dev_addr, tusb_control_request_t const * request, xfer_result_t result); @@ -101,30 +80,12 @@ bool tuh_control_xfer (uint8_t dev_addr, tusb_control_request_t const* request, //--------------------------------------------------------------------+ //TU_ATTR_WEAK uint8_t tuh_attach_cb (tusb_desc_device_t const *desc_device); -/** Callback invoked when device is mounted (configured) */ +// Invoked when device is mounted (configured) TU_ATTR_WEAK void tuh_mount_cb (uint8_t dev_addr); -/** Callback invoked when device is unmounted (bus reset/unplugged) */ +/// Invoked when device is unmounted (bus reset/unplugged) TU_ATTR_WEAK void tuh_umount_cb(uint8_t dev_addr); -//--------------------------------------------------------------------+ -// CLASS-USBH & INTERNAL API -// TODO move to usbh_pvt.h -//--------------------------------------------------------------------+ - -bool usbh_edpt_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_endpoint_t const * ep_desc); -bool usbh_edpt_xfer(uint8_t dev_addr, uint8_t ep_addr, uint8_t * buffer, uint16_t total_bytes); - -// Claim an endpoint before submitting a transfer. -// If caller does not make any transfer, it must release endpoint for others. -bool usbh_edpt_claim(uint8_t dev_addr, uint8_t ep_addr); - -void usbh_driver_set_config_complete(uint8_t dev_addr, uint8_t itf_num); - -uint8_t usbh_get_rhport(uint8_t dev_addr); - -uint8_t* usbh_get_enum_buf(void); - #ifdef __cplusplus } #endif diff --git a/src/host/usbh_classdriver.h b/src/host/usbh_classdriver.h new file mode 100644 index 000000000..2b1523855 --- /dev/null +++ b/src/host/usbh_classdriver.h @@ -0,0 +1,76 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2021, Ha Thach (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. + * + * This file is part of the TinyUSB stack. + */ + +#ifndef _TUSB_USBH_CLASSDRIVER_H_ +#define _TUSB_USBH_CLASSDRIVER_H_ + +#include "osal/osal.h" +#include "common/tusb_fifo.h" + +#ifdef __cplusplus + extern "C" { +#endif + +//--------------------------------------------------------------------+ +// Class Driver API +//--------------------------------------------------------------------+ + +typedef struct { + #if CFG_TUSB_DEBUG >= 2 + char const* name; + #endif + + uint8_t class_code; + + void (* const init )(void); + bool (* const open )(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const * itf_desc, uint16_t* outlen); + bool (* const set_config )(uint8_t dev_addr, uint8_t itf_num); + bool (* const xfer_cb )(uint8_t dev_addr, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes); + void (* const close )(uint8_t dev_addr); +} usbh_class_driver_t; + +//--------------------------------------------------------------------+ +// USBH Endpoint API +//--------------------------------------------------------------------+ + +bool usbh_edpt_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_endpoint_t const * ep_desc); +bool usbh_edpt_xfer(uint8_t dev_addr, uint8_t ep_addr, uint8_t * buffer, uint16_t total_bytes); + +// Claim an endpoint before submitting a transfer. +// If caller does not make any transfer, it must release endpoint for others. +bool usbh_edpt_claim(uint8_t dev_addr, uint8_t ep_addr); + +void usbh_driver_set_config_complete(uint8_t dev_addr, uint8_t itf_num); + +uint8_t usbh_get_rhport(uint8_t dev_addr); + +uint8_t* usbh_get_enum_buf(void); + +#ifdef __cplusplus + } +#endif + +#endif diff --git a/src/host/usbh_hcd.h b/src/host/usbh_hcd.h index abc7fd250..b3856d6b7 100644 --- a/src/host/usbh_hcd.h +++ b/src/host/usbh_hcd.h @@ -97,12 +97,6 @@ typedef struct { extern usbh_device_t _usbh_devices[CFG_TUSB_HOST_DEVICE_MAX+1]; // including zero-address -//--------------------------------------------------------------------+ -// callback from HCD ISR -//--------------------------------------------------------------------+ - - - #ifdef __cplusplus } #endif -- cgit v1.3.1 From c7f51cde40afbefe3f1d57b9dbdaaca0abed424e Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 10 Jun 2021 17:19:21 +0700 Subject: implement usbh_edpt_busy (WIP), remove hcd_edpt_busy --- src/class/cdc/cdc_host.c | 10 +++--- src/class/hid/hid_host.c | 2 +- src/class/msc/msc_host.c | 2 +- src/device/dcd.h | 2 +- src/device/usbd.c | 4 +-- src/host/hcd.h | 2 -- src/host/usbh.c | 46 ++++++++++++++++++++++++++-- src/host/usbh_classdriver.h | 19 ++++++++---- src/portable/ehci/ehci.c | 6 ---- src/portable/ohci/ohci.c | 6 ---- src/portable/raspberrypi/rp2040/hcd_rp2040.c | 26 ++++++++-------- 11 files changed, 79 insertions(+), 46 deletions(-) (limited to 'src/class') diff --git a/src/class/cdc/cdc_host.c b/src/class/cdc/cdc_host.c index dbe5341e9..33f1a8ad3 100644 --- a/src/class/cdc/cdc_host.c +++ b/src/class/cdc/cdc_host.c @@ -73,13 +73,13 @@ bool tuh_cdc_is_busy(uint8_t dev_addr, cdc_pipeid_t pipeid) switch (pipeid) { case CDC_PIPE_NOTIFICATION: - return hcd_edpt_busy(dev_addr, p_cdc->ep_notif ); + return usbh_edpt_busy(dev_addr, p_cdc->ep_notif ); case CDC_PIPE_DATA_IN: - return hcd_edpt_busy(dev_addr, p_cdc->ep_in ); + return usbh_edpt_busy(dev_addr, p_cdc->ep_in ); case CDC_PIPE_DATA_OUT: - return hcd_edpt_busy(dev_addr, p_cdc->ep_out ); + return usbh_edpt_busy(dev_addr, p_cdc->ep_out ); default: return false; @@ -103,7 +103,7 @@ bool tuh_cdc_send(uint8_t dev_addr, void const * p_data, uint32_t length, bool i TU_VERIFY( p_data != NULL && length, TUSB_ERROR_INVALID_PARA); uint8_t const ep_out = cdch_data[dev_addr-1].ep_out; - if ( hcd_edpt_busy(dev_addr, ep_out) ) return false; + if ( usbh_edpt_busy(dev_addr, ep_out) ) return false; return usbh_edpt_xfer(dev_addr, ep_out, (void *) p_data, length); } @@ -115,7 +115,7 @@ bool tuh_cdc_receive(uint8_t dev_addr, void * p_buffer, uint32_t length, bool is TU_VERIFY( p_buffer != NULL && length, TUSB_ERROR_INVALID_PARA); uint8_t const ep_in = cdch_data[dev_addr-1].ep_in; - if ( hcd_edpt_busy(dev_addr, ep_in) ) return false; + if ( usbh_edpt_busy(dev_addr, ep_in) ) return false; return usbh_edpt_xfer(dev_addr, ep_in, p_buffer, length); } diff --git a/src/class/hid/hid_host.c b/src/class/hid/hid_host.c index ec50f55f9..cde5e23a5 100644 --- a/src/class/hid/hid_host.c +++ b/src/class/hid/hid_host.c @@ -201,7 +201,7 @@ bool tuh_hid_set_report(uint8_t dev_addr, uint8_t instance, uint8_t report_id, u // TU_VERIFY(tuh_n_hid_n_mounted(dev_addr, instance)); // // hidh_interface_t* hid_itf = get_instance(dev_addr, instance); -// return !hcd_edpt_busy(dev_addr, hid_itf->ep_in); +// return !usbh_edpt_busy(dev_addr, hid_itf->ep_in); //} //void tuh_hid_send_report(uint8_t dev_addr, uint8_t instance, uint8_t report_id, uint8_t const* report, uint16_t len); diff --git a/src/class/msc/msc_host.c b/src/class/msc/msc_host.c index 176403e86..7f98ef9b1 100644 --- a/src/class/msc/msc_host.c +++ b/src/class/msc/msc_host.c @@ -111,7 +111,7 @@ bool tuh_msc_mounted(uint8_t dev_addr) bool tuh_msc_ready(uint8_t dev_addr) { msch_interface_t* p_msc = get_itf(dev_addr); - return p_msc->mounted && !hcd_edpt_busy(dev_addr, p_msc->ep_in); + return p_msc->mounted && !usbh_edpt_busy(dev_addr, p_msc->ep_in); } //--------------------------------------------------------------------+ diff --git a/src/device/dcd.h b/src/device/dcd.h index e17c62d4e..63e97df96 100644 --- a/src/device/dcd.h +++ b/src/device/dcd.h @@ -122,7 +122,7 @@ void dcd_disconnect(uint8_t rhport) TU_ATTR_WEAK; void dcd_edpt0_status_complete(uint8_t rhport, tusb_control_request_t const * request) TU_ATTR_WEAK; // Configure endpoint's registers according to descriptor -bool dcd_edpt_open (uint8_t rhport, tusb_desc_endpoint_t const * p_endpoint_desc); +bool dcd_edpt_open (uint8_t rhport, tusb_desc_endpoint_t const * desc_ep); // Close an endpoint. // Since it is weak, caller must TU_ASSERT this function's existence before calling it. diff --git a/src/device/usbd.c b/src/device/usbd.c index 71c5d4e6c..2935defd0 100644 --- a/src/device/usbd.c +++ b/src/device/usbd.c @@ -1248,8 +1248,8 @@ bool usbd_edpt_xfer(uint8_t rhport, uint8_t ep_addr, uint8_t * buffer, uint16_t // Attempt to transfer on a busy endpoint, sound like an race condition ! TU_ASSERT(_usbd_dev.ep_status[epnum][dir].busy == 0); - // Set busy first since the actual transfer can be complete before dcd_edpt_xfer() could return - // and usbd task can preempt and clear the busy + // Set busy first since the actual transfer can be complete before dcd_edpt_xfer() + // could return and USBD task can preempt and clear the busy _usbd_dev.ep_status[epnum][dir].busy = true; if ( dcd_edpt_xfer(rhport, ep_addr, buffer, total_bytes) ) diff --git a/src/host/hcd.h b/src/host/hcd.h index 8cb5b3772..7a9aadc73 100644 --- a/src/host/hcd.h +++ b/src/host/hcd.h @@ -137,8 +137,6 @@ void hcd_device_close(uint8_t rhport, uint8_t dev_addr); bool hcd_setup_send(uint8_t rhport, uint8_t dev_addr, uint8_t const setup_packet[8]); bool hcd_edpt_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_endpoint_t const * ep_desc); bool hcd_edpt_xfer(uint8_t rhport, uint8_t dev_addr, uint8_t ep_addr, uint8_t * buffer, uint16_t buflen); - -bool hcd_edpt_busy(uint8_t dev_addr, uint8_t ep_addr); bool hcd_edpt_clear_stall(uint8_t dev_addr, uint8_t ep_addr); //--------------------------------------------------------------------+ diff --git a/src/host/usbh.c b/src/host/usbh.c index ff66d5afd..59246a663 100644 --- a/src/host/usbh.c +++ b/src/host/usbh.c @@ -314,8 +314,11 @@ uint8_t* usbh_get_enum_buf(void) return _usbh_ctrl_buf; } -//------------- Endpoint API -------------// +//--------------------------------------------------------------------+ +// Endpoint API +//--------------------------------------------------------------------+ +// TODO has some duplication code with device, refactor later bool usbh_edpt_claim(uint8_t dev_addr, uint8_t ep_addr) { uint8_t const epnum = tu_edpt_number(ep_addr); @@ -343,6 +346,7 @@ bool usbh_edpt_claim(uint8_t dev_addr, uint8_t ep_addr) return ret; } +// TODO has some duplication code with device, refactor later bool usbh_edpt_release(uint8_t dev_addr, uint8_t ep_addr) { uint8_t const epnum = tu_edpt_number(ep_addr); @@ -368,11 +372,36 @@ bool usbh_edpt_release(uint8_t dev_addr, uint8_t ep_addr) return ret; } +// TODO has some duplication code with device, refactor later bool usbh_edpt_xfer(uint8_t dev_addr, uint8_t ep_addr, uint8_t * buffer, uint16_t total_bytes) { + uint8_t const epnum = tu_edpt_number(ep_addr); + uint8_t const dir = tu_edpt_dir(ep_addr); + usbh_device_t* dev = &_usbh_devices[dev_addr]; - TU_LOG2(" Queue EP %02X with %u bytes ... OK\r\n", ep_addr, total_bytes); - return hcd_edpt_xfer(dev->rhport, dev_addr, ep_addr, buffer, total_bytes); + + TU_LOG2(" Queue EP %02X with %u bytes ... ", ep_addr, total_bytes); + + // Attempt to transfer on a busy endpoint, sound like an race condition ! + TU_ASSERT(dev->ep_status[epnum][dir].busy == 0); + + // Set busy first since the actual transfer can be complete before hcd_edpt_xfer() + // could return and USBH task can preempt and clear the busy + dev->ep_status[epnum][dir].busy = true; + + if ( hcd_edpt_xfer(dev->rhport, dev_addr, ep_addr, buffer, total_bytes) ) + { + TU_LOG2("OK\r\n"); + return true; + }else + { + // HCD error, mark endpoint as ready to allow next transfer + dev->ep_status[epnum][dir].busy = false; + dev->ep_status[epnum][dir].claimed = 0; + TU_LOG2("failed\r\n"); + TU_BREAKPOINT(); + return false; + } } bool usbh_edpt_control_open(uint8_t dev_addr, uint8_t max_packet_size) @@ -419,6 +448,17 @@ bool usbh_edpt_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_endpoint_t const return ret; } +bool usbh_edpt_busy(uint8_t dev_addr, uint8_t ep_addr) +{ + uint8_t const epnum = tu_edpt_number(ep_addr); + uint8_t const dir = tu_edpt_dir(ep_addr); + + usbh_device_t* dev = &_usbh_devices[dev_addr]; + + return dev->ep_status[epnum][dir].busy; +} + + //--------------------------------------------------------------------+ // HCD Event Handler //--------------------------------------------------------------------+ diff --git a/src/host/usbh_classdriver.h b/src/host/usbh_classdriver.h index 2b1523855..07480fe8e 100644 --- a/src/host/usbh_classdriver.h +++ b/src/host/usbh_classdriver.h @@ -52,22 +52,29 @@ typedef struct { void (* const close )(uint8_t dev_addr); } usbh_class_driver_t; +// Call by class driver to tell USBH that it has complete the enumeration +void usbh_driver_set_config_complete(uint8_t dev_addr, uint8_t itf_num); + +uint8_t usbh_get_rhport(uint8_t dev_addr); + +uint8_t* usbh_get_enum_buf(void); + //--------------------------------------------------------------------+ // USBH Endpoint API //--------------------------------------------------------------------+ -bool usbh_edpt_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_endpoint_t const * ep_desc); +// Open an endpoint +bool usbh_edpt_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_endpoint_t const * desc_ep); + +// Submit a usb transfer bool usbh_edpt_xfer(uint8_t dev_addr, uint8_t ep_addr, uint8_t * buffer, uint16_t total_bytes); // Claim an endpoint before submitting a transfer. // If caller does not make any transfer, it must release endpoint for others. bool usbh_edpt_claim(uint8_t dev_addr, uint8_t ep_addr); -void usbh_driver_set_config_complete(uint8_t dev_addr, uint8_t itf_num); - -uint8_t usbh_get_rhport(uint8_t dev_addr); - -uint8_t* usbh_get_enum_buf(void); +// Check if endpoint transferring is complete +bool usbh_edpt_busy(uint8_t dev_addr, uint8_t ep_addr); #ifdef __cplusplus } diff --git a/src/portable/ehci/ehci.c b/src/portable/ehci/ehci.c index be3030676..48788ba06 100644 --- a/src/portable/ehci/ehci.c +++ b/src/portable/ehci/ehci.c @@ -440,12 +440,6 @@ bool hcd_edpt_xfer(uint8_t rhport, uint8_t dev_addr, uint8_t ep_addr, uint8_t * return true; } -bool hcd_edpt_busy(uint8_t dev_addr, uint8_t ep_addr) -{ - ehci_qhd_t *p_qhd = qhd_get_from_addr(dev_addr, ep_addr); - return !p_qhd->qtd_overlay.halted && (p_qhd->p_qtd_list_head != NULL); -} - bool hcd_edpt_clear_stall(uint8_t dev_addr, uint8_t ep_addr) { ehci_qhd_t *p_qhd = qhd_get_from_addr(dev_addr, ep_addr); diff --git a/src/portable/ohci/ohci.c b/src/portable/ohci/ohci.c index cbb72c726..73489c764 100644 --- a/src/portable/ohci/ohci.c +++ b/src/portable/ohci/ohci.c @@ -489,12 +489,6 @@ bool hcd_edpt_xfer(uint8_t rhport, uint8_t dev_addr, uint8_t ep_addr, uint8_t * return true; } -bool hcd_edpt_busy(uint8_t dev_addr, uint8_t ep_addr) -{ - ohci_ed_t const * const p_ed = ed_from_addr(dev_addr, ep_addr); - return tu_align16(p_ed->td_head.address) != tu_align16(p_ed->td_tail); -} - bool hcd_edpt_clear_stall(uint8_t dev_addr, uint8_t ep_addr) { ohci_ed_t * const p_ed = ed_from_addr(dev_addr, ep_addr); diff --git a/src/portable/raspberrypi/rp2040/hcd_rp2040.c b/src/portable/raspberrypi/rp2040/hcd_rp2040.c index aeee93324..b575c6afc 100644 --- a/src/portable/raspberrypi/rp2040/hcd_rp2040.c +++ b/src/portable/raspberrypi/rp2040/hcd_rp2040.c @@ -515,19 +515,19 @@ bool hcd_edpt_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_endpoint_t const return true; } -bool hcd_edpt_busy(uint8_t dev_addr, uint8_t ep_addr) -{ - // EPX is shared, so multiple device addresses and endpoint addresses share that - // so if any transfer is active on epx, we are busy. Interrupt endpoints have their own - // EPX so ep->active will only be busy if there is a pending transfer on that interrupt endpoint - // on that device - pico_trace("hcd_edpt_busy dev addr %d ep_addr 0x%x\n", dev_addr, ep_addr); - struct hw_endpoint *ep = get_dev_ep(dev_addr, ep_addr); - assert(ep); - bool busy = ep->active; - pico_trace("busy == %d\n", busy); - return busy; -} +//bool hcd_edpt_busy(uint8_t dev_addr, uint8_t ep_addr) +//{ +// // EPX is shared, so multiple device addresses and endpoint addresses share that +// // so if any transfer is active on epx, we are busy. Interrupt endpoints have their own +// // EPX so ep->active will only be busy if there is a pending transfer on that interrupt endpoint +// // on that device +// pico_trace("hcd_edpt_busy dev addr %d ep_addr 0x%x\n", dev_addr, ep_addr); +// struct hw_endpoint *ep = get_dev_ep(dev_addr, ep_addr); +// assert(ep); +// bool busy = ep->active; +// pico_trace("busy == %d\n", busy); +// return busy; +//} bool hcd_edpt_clear_stall(uint8_t dev_addr, uint8_t ep_addr) { -- cgit v1.3.1