@@ -268,22 +268,17 @@ tu_static uint8_t _app_driver_count = 0;
268268
269269// virtually joins built-in and application drivers together.
270270// Application is positioned first to allow overwriting built-in ones.
271- static inline usbd_class_driver_t const * get_driver (uint8_t drvid )
272- {
271+ TU_ATTR_ALWAYS_INLINE static inline usbd_class_driver_t const * get_driver (uint8_t drvid ) {
273272 usbd_class_driver_t const * driver = NULL ;
274-
275273 if ( drvid < _app_driver_count ) {
276274 // Application drivers
277275 driver = & _app_driver [drvid ];
278276 } else if ( drvid < TOTAL_DRIVER_COUNT && BUILTIN_DRIVER_COUNT > 0 ){
279277 driver = & _usbd_driver [drvid - _app_driver_count ];
280278 }
281-
282279 return driver ;
283280}
284281
285-
286-
287282//--------------------------------------------------------------------+
288283// DCD Event
289284//--------------------------------------------------------------------+
@@ -304,6 +299,11 @@ tu_static osal_queue_t _usbd_q;
304299 #define _usbd_mutex NULL
305300#endif
306301
302+ TU_ATTR_ALWAYS_INLINE static inline bool queue_event (dcd_event_t const * event , bool in_isr ) {
303+ bool ret = osal_queue_send (_usbd_q , event , in_isr );
304+ if (tud_event_hook_cb ) tud_event_hook_cb (event -> rhport , event -> event_id , in_isr );
305+ return ret ;
306+ }
307307
308308//--------------------------------------------------------------------+
309309// Prototypes
@@ -400,8 +400,7 @@ bool tud_connect(void)
400400//--------------------------------------------------------------------+
401401// USBD Task
402402//--------------------------------------------------------------------+
403- bool tud_inited (void )
404- {
403+ bool tud_inited (void ) {
405404 return _usbd_rhport != RHPORT_INVALID ;
406405}
407406
@@ -1103,66 +1102,64 @@ static bool process_get_descriptor(uint8_t rhport, tusb_control_request_t const
11031102//--------------------------------------------------------------------+
11041103// DCD Event Handler
11051104//--------------------------------------------------------------------+
1106- TU_ATTR_FAST_FUNC void dcd_event_handler (dcd_event_t const * event , bool in_isr )
1107- {
1108- switch (event -> event_id )
1109- {
1105+ TU_ATTR_FAST_FUNC void dcd_event_handler (dcd_event_t const * event , bool in_isr ) {
1106+ bool send = false;
1107+ switch (event -> event_id ) {
11101108 case DCD_EVENT_UNPLUGGED :
1111- _usbd_dev .connected = 0 ;
1112- _usbd_dev .addressed = 0 ;
1113- _usbd_dev .cfg_num = 0 ;
1114- _usbd_dev .suspended = 0 ;
1115- osal_queue_send ( _usbd_q , event , in_isr ) ;
1116- break ;
1109+ _usbd_dev .connected = 0 ;
1110+ _usbd_dev .addressed = 0 ;
1111+ _usbd_dev .cfg_num = 0 ;
1112+ _usbd_dev .suspended = 0 ;
1113+ send = true ;
1114+ break ;
11171115
11181116 case DCD_EVENT_SUSPEND :
11191117 // NOTE: When plugging/unplugging device, the D+/D- state are unstable and
11201118 // can accidentally meet the SUSPEND condition ( Bus Idle for 3ms ).
11211119 // In addition, some MCUs such as SAMD or boards that haven no VBUS detection cannot distinguish
11221120 // suspended vs disconnected. We will skip handling SUSPEND/RESUME event if not currently connected
1123- if ( _usbd_dev .connected )
1124- {
1121+ if (_usbd_dev .connected ) {
11251122 _usbd_dev .suspended = 1 ;
1126- osal_queue_send ( _usbd_q , event , in_isr ) ;
1123+ send = true ;
11271124 }
1128- break ;
1125+ break ;
11291126
11301127 case DCD_EVENT_RESUME :
11311128 // skip event if not connected (especially required for SAMD)
1132- if ( _usbd_dev .connected )
1133- {
1129+ if (_usbd_dev .connected ) {
11341130 _usbd_dev .suspended = 0 ;
1135- osal_queue_send ( _usbd_q , event , in_isr ) ;
1131+ send = true ;
11361132 }
1137- break ;
1133+ break ;
11381134
11391135 case DCD_EVENT_SOF :
1140- // SOF driver handler in ISR context
1141- for (uint8_t i = 0 ; i < TOTAL_DRIVER_COUNT ; i ++ )
1142- {
1143- usbd_class_driver_t const * driver = get_driver (i );
1144- if (driver && driver -> sof )
1145- {
1146- driver -> sof (event -> rhport , event -> sof .frame_count );
1147- }
1148- }
1149-
11501136 // Some MCUs after running dcd_remote_wakeup() does not have way to detect the end of remote wakeup
11511137 // which last 1-15 ms. DCD can use SOF as a clear indicator that bus is back to operational
1152- if ( _usbd_dev .suspended )
1153- {
1138+ if (_usbd_dev .suspended ) {
11541139 _usbd_dev .suspended = 0 ;
11551140
1156- dcd_event_t const event_resume = { .rhport = event -> rhport , .event_id = DCD_EVENT_RESUME };
1157- osal_queue_send (_usbd_q , & event_resume , in_isr );
1141+ dcd_event_t const event_resume = {.rhport = event -> rhport , .event_id = DCD_EVENT_RESUME };
1142+ queue_event (& event_resume , in_isr );
1143+ }
1144+
1145+ // SOF driver handler in ISR context
1146+ for (uint8_t i = 0 ; i < TOTAL_DRIVER_COUNT ; i ++ ) {
1147+ usbd_class_driver_t const * driver = get_driver (i );
1148+ if (driver && driver -> sof ) {
1149+ driver -> sof (event -> rhport , event -> sof .frame_count );
1150+ }
11581151 }
11591152
11601153 // skip osal queue for SOF in usbd task
1161- break ;
1154+ break ;
11621155
11631156 default :
1164- osal_queue_send (_usbd_q , event , in_isr );
1165- break ;
1157+ send = true;
1158+ break ;
1159+ }
1160+
1161+ if (send ) {
1162+ queue_event (event , in_isr );
11661163 }
11671164}
11681165
@@ -1206,18 +1203,15 @@ bool usbd_open_edpt_pair(uint8_t rhport, uint8_t const* p_desc, uint8_t ep_count
12061203}
12071204
12081205// Helper to defer an isr function
1209- void usbd_defer_func (osal_task_func_t func , void * param , bool in_isr )
1210- {
1211- dcd_event_t event =
1212- {
1206+ void usbd_defer_func (osal_task_func_t func , void * param , bool in_isr ) {
1207+ dcd_event_t event = {
12131208 .rhport = 0 ,
12141209 .event_id = USBD_EVENT_FUNC_CALL ,
12151210 };
1216-
12171211 event .func_call .func = func ;
12181212 event .func_call .param = param ;
12191213
1220- dcd_event_handler (& event , in_isr );
1214+ queue_event (& event , in_isr );
12211215}
12221216
12231217//--------------------------------------------------------------------+
0 commit comments