summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorhathach <[email protected]>2023-11-24 18:21:24 +0700
committerhathach <[email protected]>2023-11-24 18:21:24 +0700
commit188fbd8ed9b5e0f5cc3ddb5e89f7afd421fc7c06 (patch)
treed1bb915fbc21a6ec2ef59ac083e4db1a388d733d
parent111b21e76703ee0d2d0190c349b1aa3d797a72e5 (diff)
add optional event hook callback tud_event_hook_cb() and tuh_event_hook_cb()
-rw-r--r--src/device/usbd.c83
-rw-r--r--src/device/usbd.h3
-rw-r--r--src/host/usbh.c18
-rw-r--r--src/host/usbh.h5
-rw-r--r--src/tusb_option.h12
5 files changed, 52 insertions, 69 deletions
diff --git a/src/device/usbd.c b/src/device/usbd.c
index 2b6d07a96..59466b42e 100644
--- a/src/device/usbd.c
+++ b/src/device/usbd.c
@@ -242,22 +242,17 @@ tu_static uint8_t _app_driver_count = 0;
// virtually joins built-in and application drivers together.
// Application is positioned first to allow overwriting built-in ones.
-static inline usbd_class_driver_t const * get_driver(uint8_t drvid)
-{
+TU_ATTR_ALWAYS_INLINE static inline usbd_class_driver_t const * get_driver(uint8_t drvid) {
usbd_class_driver_t const * driver = NULL;
-
if ( drvid < _app_driver_count ) {
// Application drivers
driver = &_app_driver[drvid];
} else if ( drvid < TOTAL_DRIVER_COUNT && BUILTIN_DRIVER_COUNT > 0 ){
driver = &_usbd_driver[drvid - _app_driver_count];
}
-
return driver;
}
-
-
//--------------------------------------------------------------------+
// DCD Event
//--------------------------------------------------------------------+
@@ -278,6 +273,11 @@ tu_static osal_queue_t _usbd_q;
#define _usbd_mutex NULL
#endif
+TU_ATTR_ALWAYS_INLINE static inline bool queue_event(dcd_event_t const * event, bool in_isr) {
+ bool ret = osal_queue_send(_usbd_q, event, in_isr);
+ if (tud_event_hook_cb) tud_event_hook_cb(event->rhport, event->event_id, in_isr);
+ return ret;
+}
//--------------------------------------------------------------------+
// Prototypes
@@ -374,8 +374,7 @@ bool tud_connect(void)
//--------------------------------------------------------------------+
// USBD Task
//--------------------------------------------------------------------+
-bool tud_inited(void)
-{
+bool tud_inited(void) {
return _usbd_rhport != RHPORT_INVALID;
}
@@ -1077,73 +1076,64 @@ static bool process_get_descriptor(uint8_t rhport, tusb_control_request_t const
//--------------------------------------------------------------------+
// DCD Event Handler
//--------------------------------------------------------------------+
-TU_ATTR_FAST_FUNC void dcd_event_handler(dcd_event_t const * event, bool in_isr)
-{
+TU_ATTR_FAST_FUNC void dcd_event_handler(dcd_event_t const* event, bool in_isr) {
bool send = false;
- switch (event->event_id)
- {
+ switch (event->event_id) {
case DCD_EVENT_UNPLUGGED:
- _usbd_dev.connected = 0;
- _usbd_dev.addressed = 0;
- _usbd_dev.cfg_num = 0;
- _usbd_dev.suspended = 0;
+ _usbd_dev.connected = 0;
+ _usbd_dev.addressed = 0;
+ _usbd_dev.cfg_num = 0;
+ _usbd_dev.suspended = 0;
send = true;
- break;
+ break;
case DCD_EVENT_SUSPEND:
// NOTE: When plugging/unplugging device, the D+/D- state are unstable and
// can accidentally meet the SUSPEND condition ( Bus Idle for 3ms ).
// In addition, some MCUs such as SAMD or boards that haven no VBUS detection cannot distinguish
// suspended vs disconnected. We will skip handling SUSPEND/RESUME event if not currently connected
- if ( _usbd_dev.connected )
- {
+ if (_usbd_dev.connected) {
_usbd_dev.suspended = 1;
send = true;
}
- break;
+ break;
case DCD_EVENT_RESUME:
// skip event if not connected (especially required for SAMD)
- if ( _usbd_dev.connected )
- {
+ if (_usbd_dev.connected) {
_usbd_dev.suspended = 0;
send = true;
}
- break;
+ break;
case DCD_EVENT_SOF:
- // SOF driver handler in ISR context
- for (uint8_t i = 0; i < TOTAL_DRIVER_COUNT; i++)
- {
- usbd_class_driver_t const * driver = get_driver(i);
- if (driver && driver->sof)
- {
- driver->sof(event->rhport, event->sof.frame_count);
- }
- }
-
// Some MCUs after running dcd_remote_wakeup() does not have way to detect the end of remote wakeup
// which last 1-15 ms. DCD can use SOF as a clear indicator that bus is back to operational
- if ( _usbd_dev.suspended )
- {
+ if (_usbd_dev.suspended) {
_usbd_dev.suspended = 0;
- dcd_event_t const event_resume = { .rhport = event->rhport, .event_id = DCD_EVENT_RESUME };
- osal_queue_send(_usbd_q, &event_resume, in_isr);
- CFG_TUD_EVENT_HOOK(&event_resume, in_isr);
+ dcd_event_t const event_resume = {.rhport = event->rhport, .event_id = DCD_EVENT_RESUME};
+ queue_event(&event_resume, in_isr);
+ }
+
+ // SOF driver handler in ISR context
+ for (uint8_t i = 0; i < TOTAL_DRIVER_COUNT; i++) {
+ usbd_class_driver_t const* driver = get_driver(i);
+ if (driver && driver->sof) {
+ driver->sof(event->rhport, event->sof.frame_count);
+ }
}
// skip osal queue for SOF in usbd task
- break;
+ break;
default:
send = true;
- break;
+ break;
}
if (send) {
- osal_queue_send(_usbd_q, event, in_isr);
- CFG_TUD_EVENT_HOOK(event, in_isr);
+ queue_event(event, in_isr);
}
}
@@ -1187,18 +1177,15 @@ bool usbd_open_edpt_pair(uint8_t rhport, uint8_t const* p_desc, uint8_t ep_count
}
// Helper to defer an isr function
-void usbd_defer_func(osal_task_func_t func, void* param, bool in_isr)
-{
- dcd_event_t event =
- {
+void usbd_defer_func(osal_task_func_t func, void* param, bool in_isr) {
+ dcd_event_t event = {
.rhport = 0,
.event_id = USBD_EVENT_FUNC_CALL,
};
-
event.func_call.func = func;
event.func_call.param = param;
- dcd_event_handler(&event, in_isr);
+ queue_event(&event, in_isr);
}
//--------------------------------------------------------------------+
diff --git a/src/device/usbd.h b/src/device/usbd.h
index 93a2033c4..1e2f31b06 100644
--- a/src/device/usbd.h
+++ b/src/device/usbd.h
@@ -146,6 +146,9 @@ TU_ATTR_WEAK void tud_suspend_cb(bool remote_wakeup_en);
// Invoked when usb bus is resumed
TU_ATTR_WEAK void tud_resume_cb(void);
+// Invoked when there is a new usb event, which need to be processed by tud_task()/tud_task_ext()
+TU_ATTR_WEAK void tud_event_hook_cb(uint8_t rhport, uint32_t eventid, bool in_isr);
+
// Invoked when received control request with VENDOR TYPE
TU_ATTR_WEAK bool tud_vendor_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const * request);
diff --git a/src/host/usbh.c b/src/host/usbh.c
index 2fdc8216d..bbda99ea2 100644
--- a/src/host/usbh.c
+++ b/src/host/usbh.c
@@ -247,9 +247,7 @@ CFG_TUH_MEM_SECTION struct
//------------- Helper Function -------------//
-TU_ATTR_ALWAYS_INLINE
-static inline usbh_device_t* get_device(uint8_t dev_addr)
-{
+TU_ATTR_ALWAYS_INLINE static inline usbh_device_t* get_device(uint8_t dev_addr) {
TU_VERIFY(dev_addr > 0 && dev_addr <= TOTAL_DEVICES, NULL);
return &_usbh_devices[dev_addr-1];
}
@@ -268,6 +266,12 @@ TU_ATTR_WEAK void osal_task_delay(uint32_t msec) {
}
#endif
+TU_ATTR_ALWAYS_INLINE static inline bool queue_event(hcd_event_t const * event, bool in_isr) {
+ bool ret = osal_queue_send(_usbh_q, event, in_isr);
+ if (tuh_event_hook_cb) tuh_event_hook_cb(event->rhport, event->event_id, in_isr);
+ return ret;
+}
+
//--------------------------------------------------------------------+
// Device API
//--------------------------------------------------------------------+
@@ -433,7 +437,7 @@ void tuh_task_ext(uint32_t timeout_ms, bool in_isr) {
TU_LOG_USBH("[%u:] USBH Defer Attach until current enumeration complete\r\n", event.rhport);
bool is_empty = osal_queue_empty(_usbh_q);
- osal_queue_send(_usbh_q, &event, in_isr);
+ queue_event(&event, in_isr);
if (is_empty) {
// Exit if this is the only event in the queue, otherwise we may loop forever
@@ -779,8 +783,7 @@ void usbh_defer_func(osal_task_func_t func, void *param, bool in_isr) {
event.func_call.func = func;
event.func_call.param = param;
- osal_queue_send(_usbh_q, &event, in_isr);
- CFG_TUH_EVENT_HOOK(event, in_isr);
+ queue_event(&event, in_isr);
}
//--------------------------------------------------------------------+
@@ -936,8 +939,7 @@ TU_ATTR_FAST_FUNC void hcd_event_handler(hcd_event_t const* event, bool in_isr)
default: break;
}
- osal_queue_send(_usbh_q, event, in_isr);
- CFG_TUH_EVENT_HOOK(event, in_isr);
+ queue_event(event, in_isr);
}
//--------------------------------------------------------------------+
diff --git a/src/host/usbh.h b/src/host/usbh.h
index 770d70dfe..7591c7672 100644
--- a/src/host/usbh.h
+++ b/src/host/usbh.h
@@ -90,9 +90,12 @@ TU_ATTR_WEAK void tuh_mount_cb (uint8_t daddr);
// Invoked when a device failed to mount during enumeration process
// TU_ATTR_WEAK void tuh_mount_failed_cb (uint8_t daddr);
-/// Invoked when a device is unmounted (detached)
+// Invoked when a device is unmounted (detached)
TU_ATTR_WEAK void tuh_umount_cb(uint8_t daddr);
+// Invoked when there is a new usb event, which need to be processed by tuh_task()/tuh_task_ext()
+TU_ATTR_WEAK void tuh_event_hook_cb(uint8_t rhport, uint32_t eventid, bool in_isr);
+
//--------------------------------------------------------------------+
// APPLICATION API
//--------------------------------------------------------------------+
diff --git a/src/tusb_option.h b/src/tusb_option.h
index cf774a41c..b8509a069 100644
--- a/src/tusb_option.h
+++ b/src/tusb_option.h
@@ -486,18 +486,6 @@
#define tuc_int_handler(_p)
#endif
-//--------------------------------------------------------------------+
-// Optional event hook functions for platform integration (defaults)
-//--------------------------------------------------------------------+
-
-#ifndef CFG_TUD_EVENT_HOOK
-#define CFG_TUD_EVENT_HOOK(EVENT, IN_ISR)
-#endif
-
-#ifndef CFG_TUH_EVENT_HOOK
-#define CFG_TUH_EVENT_HOOK(EVENT, IN_ISR)
-#endif
-
//------------------------------------------------------------------
// Configuration Validation
//------------------------------------------------------------------