summaryrefslogtreecommitdiff
path: root/src/tusb.c
diff options
context:
space:
mode:
authorhathach <[email protected]>2024-11-28 16:11:30 +0700
committerhathach <[email protected]>2024-11-28 16:11:30 +0700
commit79373afaafffe64dd2bb33d725b88137c3d4ee6f (patch)
tree272da237079fd812ec088f16fa0cabb3494891c7 /src/tusb.c
parent047ba0a62db8a078aa2f7d68d00cbf41406c0fb2 (diff)
parent5bb90efd5ff59d3b1d67f3c9269ce1aa57a925a6 (diff)
Merge branch 'master' into fork/HiFiPhile/lwip_fix
Diffstat (limited to 'src/tusb.c')
-rw-r--r--src/tusb.c79
1 files changed, 72 insertions, 7 deletions
diff --git a/src/tusb.c b/src/tusb.c
index 11690b203..65a850930 100644
--- a/src/tusb.c
+++ b/src/tusb.c
@@ -39,19 +39,67 @@
#include "host/usbh_pvt.h"
#endif
+tusb_role_t _tusb_rhport_role[TUP_USBIP_CONTROLLER_NUM] = { TUSB_ROLE_INVALID };
+
+//--------------------------------------------------------------------
+// Weak/Default API, can be overwritten by Application
+//--------------------------------------------------------------------
+
+TU_ATTR_WEAK void tusb_time_delay_ms_api(uint32_t ms) {
+#if CFG_TUSB_OS != OPT_OS_NONE
+ osal_task_delay(ms);
+#else
+ // delay using millis() (if implemented) and/or frame number if possible
+ const uint32_t time_ms = tusb_time_millis_api();
+ while ((tusb_time_millis_api() - time_ms) < ms) {}
+#endif
+}
+
//--------------------------------------------------------------------+
// Public API
//--------------------------------------------------------------------+
+bool tusb_rhport_init(uint8_t rhport, const tusb_rhport_init_t* rh_init) {
+ // backward compatible called with tusb_init(void)
+ #if defined(TUD_OPT_RHPORT) || defined(TUH_OPT_RHPORT)
+ if (rh_init == NULL) {
+ #if CFG_TUD_ENABLED && defined(TUD_OPT_RHPORT)
+ // init device stack CFG_TUSB_RHPORTx_MODE must be defined
+ const tusb_rhport_init_t dev_init = {
+ .role = TUSB_ROLE_DEVICE,
+ .speed = TUD_OPT_HIGH_SPEED ? TUSB_SPEED_HIGH : TUSB_SPEED_FULL
+ };
+ TU_ASSERT ( tud_rhport_init(TUD_OPT_RHPORT, &dev_init) );
+ _tusb_rhport_role[TUD_OPT_RHPORT] = TUSB_ROLE_DEVICE;
+ #endif
-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) );
+ #if CFG_TUH_ENABLED && defined(TUH_OPT_RHPORT)
+ // init host stack CFG_TUSB_RHPORTx_MODE must be defined
+ const tusb_rhport_init_t host_init = {
+ .role = TUSB_ROLE_HOST,
+ .speed = TUH_OPT_HIGH_SPEED ? TUSB_SPEED_HIGH : TUSB_SPEED_FULL
+ };
+ TU_ASSERT( tuh_rhport_init(TUH_OPT_RHPORT, &host_init) );
+ _tusb_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 && rh_init->role != TUSB_ROLE_INVALID);
+ _tusb_rhport_role[rhport] = rh_init->role;
+
+ #if CFG_TUD_ENABLED
+ if (rh_init->role == TUSB_ROLE_DEVICE) {
+ TU_ASSERT(tud_rhport_init(rhport, rh_init));
+ }
#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 (rh_init->role == TUSB_ROLE_HOST) {
+ TU_ASSERT(tuh_rhport_init(rhport, rh_init));
+ }
#endif
return true;
@@ -71,6 +119,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 (_tusb_rhport_role[rhport] == TUSB_ROLE_DEVICE) {
+ (void) in_isr;
+ dcd_int_handler(rhport);
+ }
+ #endif
+
+ #if CFG_TUH_ENABLED
+ if (_tusb_rhport_role[rhport] == TUSB_ROLE_HOST) {
+ hcd_int_handler(rhport, in_isr);
+ }
+ #endif
+}
+
//--------------------------------------------------------------------+
// Descriptor helper
//--------------------------------------------------------------------+