@@ -268,22 +268,17 @@ tu_static uint8_t _app_driver_count = 0;
268
268
269
269
// virtually joins built-in and application drivers together.
270
270
// 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 ) {
273
272
usbd_class_driver_t const * driver = NULL ;
274
-
275
273
if ( drvid < _app_driver_count ) {
276
274
// Application drivers
277
275
driver = & _app_driver [drvid ];
278
276
} else if ( drvid < TOTAL_DRIVER_COUNT && BUILTIN_DRIVER_COUNT > 0 ){
279
277
driver = & _usbd_driver [drvid - _app_driver_count ];
280
278
}
281
-
282
279
return driver ;
283
280
}
284
281
285
-
286
-
287
282
//--------------------------------------------------------------------+
288
283
// DCD Event
289
284
//--------------------------------------------------------------------+
@@ -304,6 +299,11 @@ tu_static osal_queue_t _usbd_q;
304
299
#define _usbd_mutex NULL
305
300
#endif
306
301
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
+ }
307
307
308
308
//--------------------------------------------------------------------+
309
309
// Prototypes
@@ -400,8 +400,7 @@ bool tud_connect(void)
400
400
//--------------------------------------------------------------------+
401
401
// USBD Task
402
402
//--------------------------------------------------------------------+
403
- bool tud_inited (void )
404
- {
403
+ bool tud_inited (void ) {
405
404
return _usbd_rhport != RHPORT_INVALID ;
406
405
}
407
406
@@ -1103,66 +1102,64 @@ static bool process_get_descriptor(uint8_t rhport, tusb_control_request_t const
1103
1102
//--------------------------------------------------------------------+
1104
1103
// DCD Event Handler
1105
1104
//--------------------------------------------------------------------+
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 ) {
1110
1108
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 ;
1117
1115
1118
1116
case DCD_EVENT_SUSPEND :
1119
1117
// NOTE: When plugging/unplugging device, the D+/D- state are unstable and
1120
1118
// can accidentally meet the SUSPEND condition ( Bus Idle for 3ms ).
1121
1119
// In addition, some MCUs such as SAMD or boards that haven no VBUS detection cannot distinguish
1122
1120
// 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 ) {
1125
1122
_usbd_dev .suspended = 1 ;
1126
- osal_queue_send ( _usbd_q , event , in_isr ) ;
1123
+ send = true ;
1127
1124
}
1128
- break ;
1125
+ break ;
1129
1126
1130
1127
case DCD_EVENT_RESUME :
1131
1128
// skip event if not connected (especially required for SAMD)
1132
- if ( _usbd_dev .connected )
1133
- {
1129
+ if (_usbd_dev .connected ) {
1134
1130
_usbd_dev .suspended = 0 ;
1135
- osal_queue_send ( _usbd_q , event , in_isr ) ;
1131
+ send = true ;
1136
1132
}
1137
- break ;
1133
+ break ;
1138
1134
1139
1135
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
-
1150
1136
// Some MCUs after running dcd_remote_wakeup() does not have way to detect the end of remote wakeup
1151
1137
// 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 ) {
1154
1139
_usbd_dev .suspended = 0 ;
1155
1140
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
+ }
1158
1151
}
1159
1152
1160
1153
// skip osal queue for SOF in usbd task
1161
- break ;
1154
+ break ;
1162
1155
1163
1156
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 );
1166
1163
}
1167
1164
}
1168
1165
@@ -1206,18 +1203,15 @@ bool usbd_open_edpt_pair(uint8_t rhport, uint8_t const* p_desc, uint8_t ep_count
1206
1203
}
1207
1204
1208
1205
// 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 = {
1213
1208
.rhport = 0 ,
1214
1209
.event_id = USBD_EVENT_FUNC_CALL ,
1215
1210
};
1216
-
1217
1211
event .func_call .func = func ;
1218
1212
event .func_call .param = param ;
1219
1213
1220
- dcd_event_handler (& event , in_isr );
1214
+ queue_event (& event , in_isr );
1221
1215
}
1222
1216
1223
1217
//--------------------------------------------------------------------+
0 commit comments