diff options
| author | Ha Thach <[email protected]> | 2024-10-10 17:00:40 +0700 |
|---|---|---|
| committer | GitHub <[email protected]> | 2024-10-10 17:00:40 +0700 |
| commit | a4fb8354e41b2604f66e7da22afa292b1a44f0a2 (patch) | |
| tree | 1e980f0c18f558e3041dbef3021afa346297a85d /src | |
| parent | ffdf81f53a84111a2536defa86e4a637c3c46854 (diff) | |
| parent | 57aac432b52df6d284c98daf591e661d1c55aa05 (diff) | |
Merge pull request #2834 from hathach/add-tusb_int_handler-update-tinyusb_init
add new tusb_int_handler(rhport, in_isr) and update tusb_init(rhport, role)
Diffstat (limited to 'src')
| -rw-r--r-- | src/common/tusb_types.h | 6 | ||||
| -rw-r--r-- | src/common/tusb_verify.h | 6 | ||||
| -rw-r--r-- | src/device/usbd.c | 2 | ||||
| -rw-r--r-- | src/host/usbh.c | 2 | ||||
| -rw-r--r-- | src/host/usbh.h | 5 | ||||
| -rw-r--r-- | src/tusb.c | 60 | ||||
| -rw-r--r-- | src/tusb.h | 26 |
7 files changed, 88 insertions, 19 deletions
diff --git a/src/common/tusb_types.h b/src/common/tusb_types.h index 1501a5af6..adf4b5720 100644 --- a/src/common/tusb_types.h +++ b/src/common/tusb_types.h @@ -39,6 +39,12 @@ /* CONSTANTS *------------------------------------------------------------------*/ +typedef enum { + TUSB_ROLE_INVALID = 0, + TUSB_ROLE_DEVICE, + TUSB_ROLE_HOST, +} tusb_role_t; + /// defined base on EHCI specs value for Endpoint Speed typedef enum { TUSB_SPEED_FULL = 0, diff --git a/src/common/tusb_verify.h b/src/common/tusb_verify.h index 4344575b7..8ad218b03 100644 --- a/src/common/tusb_verify.h +++ b/src/common/tusb_verify.h @@ -94,7 +94,7 @@ #endif // Helper to implement optional parameter for TU_VERIFY Macro family -#define _GET_3RD_ARG(arg1, arg2, arg3, ...) arg3 +#define TU_GET_3RD_ARG(arg1, arg2, arg3, ...) arg3 /*------------------------------------------------------------------*/ /* TU_VERIFY @@ -109,7 +109,7 @@ #define TU_VERIFY_1ARGS(_cond) TU_VERIFY_DEFINE(_cond, false) #define TU_VERIFY_2ARGS(_cond, _ret) TU_VERIFY_DEFINE(_cond, _ret) -#define TU_VERIFY(...) _GET_3RD_ARG(__VA_ARGS__, TU_VERIFY_2ARGS, TU_VERIFY_1ARGS, _dummy)(__VA_ARGS__) +#define TU_VERIFY(...) TU_GET_3RD_ARG(__VA_ARGS__, TU_VERIFY_2ARGS, TU_VERIFY_1ARGS, _dummy)(__VA_ARGS__) /*------------------------------------------------------------------*/ /* ASSERT @@ -126,7 +126,7 @@ #define TU_ASSERT_2ARGS(_cond, _ret) TU_ASSERT_DEFINE(_cond, _ret) #ifndef TU_ASSERT -#define TU_ASSERT(...) _GET_3RD_ARG(__VA_ARGS__, TU_ASSERT_2ARGS, TU_ASSERT_1ARGS, _dummy)(__VA_ARGS__) +#define TU_ASSERT(...) TU_GET_3RD_ARG(__VA_ARGS__, TU_ASSERT_2ARGS, TU_ASSERT_1ARGS, _dummy)(__VA_ARGS__) #endif #ifdef __cplusplus diff --git a/src/device/usbd.c b/src/device/usbd.c index 67faf0da7..6fe7e416e 100644 --- a/src/device/usbd.c +++ b/src/device/usbd.c @@ -557,7 +557,7 @@ bool tud_task_event_ready(void) { * int main(void) { application_init(); - tusb_init(); + tusb_init(0, TUSB_ROLE_DEVICE); while(1) { // the mainloop application_code(); diff --git a/src/host/usbh.c b/src/host/usbh.c index 8c488aaa2..ed253ffcc 100644 --- a/src/host/usbh.c +++ b/src/host/usbh.c @@ -459,7 +459,7 @@ bool tuh_task_event_ready(void) { int main(void) { application_init(); - tusb_init(); + tusb_init(0, TUSB_ROLE_HOST); while(1) // the mainloop { diff --git a/src/host/usbh.h b/src/host/usbh.h index d0a5732e5..217cb1a9c 100644 --- a/src/host/usbh.h +++ b/src/host/usbh.h @@ -149,12 +149,9 @@ extern void hcd_int_handler(uint8_t rhport, bool in_isr); #endif // Interrupt handler alias to HCD with in_isr as optional parameter -// - tuh_int_handler(rhport) --> hcd_int_handler(rhport, true) -// - tuh_int_handler(rhport, in_isr) --> hcd_int_handler(rhport, in_isr) -// Note: this is similar to TU_VERIFY(), _GET_3RD_ARG() is defined in tusb_verify.h #define _tuh_int_handler_1arg(_rhport) hcd_int_handler(_rhport, true) #define _tuh_int_hanlder_2arg(_rhport, _in_isr) hcd_int_handler(_rhport, _in_isr) -#define tuh_int_handler(...) _GET_3RD_ARG(__VA_ARGS__, _tuh_int_hanlder_2arg, _tuh_int_handler_1arg, _dummy)(__VA_ARGS__) +#define tuh_int_handler(...) TU_GET_3RD_ARG(__VA_ARGS__, _tuh_int_hanlder_2arg, _tuh_int_handler_1arg, _dummy)(__VA_ARGS__) // Check if roothub port is initialized and active as a host bool tuh_rhport_is_active(uint8_t rhport); diff --git a/src/tusb.c b/src/tusb.c index 11690b203..9f0e45fef 100644 --- a/src/tusb.c +++ b/src/tusb.c @@ -39,21 +39,50 @@ #include "host/usbh_pvt.h" #endif +#define TUP_USBIP_CONTROLLER_NUM 2 + +static tusb_role_t _rhport_role[TUP_USBIP_CONTROLLER_NUM] = { 0 }; + //--------------------------------------------------------------------+ // Public API //--------------------------------------------------------------------+ -bool tusb_init(void) { - #if CFG_TUD_ENABLED && defined(TUD_OPT_RHPORT) - // init device stack CFG_TUSB_RHPORTx_MODE must be defined - TU_ASSERT ( tud_init(TUD_OPT_RHPORT) ); +bool _tusb_rhport_init(uint8_t rhport, tusb_role_t role) { + // backward compatible called with tusb_init(void) + #if defined(TUD_OPT_RHPORT) || defined(TUH_OPT_RHPORT) + if (rhport == 0xff || role == TUSB_ROLE_INVALID) { + #if CFG_TUD_ENABLED && defined(TUD_OPT_RHPORT) + // init device stack CFG_TUSB_RHPORTx_MODE must be defined + TU_ASSERT ( tud_init(TUD_OPT_RHPORT) ); + _rhport_role[TUD_OPT_RHPORT] = TUSB_ROLE_DEVICE; + #endif + + #if CFG_TUH_ENABLED && defined(TUH_OPT_RHPORT) + // init host stack CFG_TUSB_RHPORTx_MODE must be defined + TU_ASSERT( tuh_init(TUH_OPT_RHPORT) ); + _rhport_role[TUH_OPT_RHPORT] = TUSB_ROLE_HOST; + #endif + + return true; + } + #endif + + // new API with explicit rhport and role + TU_ASSERT(rhport < TUP_USBIP_CONTROLLER_NUM && role != TUSB_ROLE_INVALID); + + #if CFG_TUD_ENABLED + if (role == TUSB_ROLE_DEVICE) { + TU_ASSERT( tud_init(rhport) ); + } #endif - #if CFG_TUH_ENABLED && defined(TUH_OPT_RHPORT) - // init host stack CFG_TUSB_RHPORTx_MODE must be defined - TU_ASSERT( tuh_init(TUH_OPT_RHPORT) ); + #if CFG_TUH_ENABLED + if (role == TUSB_ROLE_HOST) { + TU_ASSERT( tuh_init(rhport) ); + } #endif + _rhport_role[rhport] = role; return true; } @@ -71,6 +100,23 @@ bool tusb_inited(void) { return ret; } +void tusb_int_handler(uint8_t rhport, bool in_isr) { + TU_VERIFY(rhport < TUP_USBIP_CONTROLLER_NUM,); + + #if CFG_TUD_ENABLED + if (_rhport_role[rhport] == TUSB_ROLE_DEVICE) { + (void) in_isr; + tud_int_handler(rhport); + } + #endif + + #if CFG_TUH_ENABLED + if (_rhport_role[rhport] == TUSB_ROLE_HOST) { + tuh_int_handler(rhport, in_isr); + } + #endif +} + //--------------------------------------------------------------------+ // Descriptor helper //--------------------------------------------------------------------+ diff --git a/src/tusb.h b/src/tusb.h index 4f69a1414..911d1bae0 100644 --- a/src/tusb.h +++ b/src/tusb.h @@ -129,18 +129,38 @@ //--------------------------------------------------------------------+ // APPLICATION API //--------------------------------------------------------------------+ +#if CFG_TUH_ENABLED || CFG_TUD_ENABLED -// Initialize device/host stack +// Internal helper for backward compatible with tusb_init(void) +bool _tusb_rhport_init(uint8_t rhport, tusb_role_t role); + +// Initialize roothub port with device/host role // Note: when using with RTOS, this should be called after scheduler/kernel is started. -// Otherwise it could cause kernel issue since USB IRQ handler does use RTOS queue API. -bool tusb_init(void); +// Otherwise, it could cause kernel issue since USB IRQ handler does use RTOS queue API. +// Note2: defined as macro for backward compatible with tusb_init(void), can be changed to function in the future. +#define _tusb_init_0arg() _tusb_rhport_init(0xff, TUSB_ROLE_INVALID) +#define _tusb_init_1arg(_rhport) _tusb_rhport_init(_rhport, TUSB_ROLE_INVALID) +#define _tusb_init_2arg(_rhport, _role) _tusb_rhport_init(_rhport, _role) +#define tusb_init(...) TU_GET_3RD_ARG(__VA_ARGS__, _tusb_init_2arg, _tusb_init_1arg, _tusb_init_0arg)(__VA_ARGS__) // Check if stack is initialized bool tusb_inited(void); +// Called to handle usb interrupt/event. tusb_init(rhport, role) must be called before +void tusb_int_handler(uint8_t rhport, bool in_isr); + // TODO // bool tusb_teardown(void); +#else + +#define tusb_init(...) (false) +#define tusb_int_handler(...) do {}while(0) +#define tusb_inited() (false) + +#endif + + #ifdef __cplusplus } #endif |
