Skip to content

Commit c64452c

Browse files
authored
Merge pull request #3566 from Precidata/async-cleanup
Couple of random cleanup patches following async PR
2 parents bf8f0ec + 653e630 commit c64452c

File tree

4 files changed

+36
-14
lines changed

4 files changed

+36
-14
lines changed

src/device/usbd.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -803,10 +803,8 @@ void tud_task_ext(uint32_t timeout_ms, bool in_isr) {
803803
break;
804804
}
805805

806-
#if CFG_TUSB_OS != OPT_OS_NONE && CFG_TUSB_OS != OPT_OS_PICO
807-
// return if there is no more events, for application to run other background
808-
if (osal_queue_empty(_usbd_q)) { return; }
809-
#endif
806+
// allow to exit tud_task() if there is no event in the next run
807+
timeout_ms = 0;
810808
}
811809
}
812810

src/host/usbh.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,8 @@ bool usbh_defer_func_ms_async(uint32_t ms, tusb_defer_func_t func, uintptr_t par
374374
TU_LOG_USBH("USBH schedule function after %u ms\r\n", (unsigned int)ms);
375375
_usbh_data.call_after.func = func;
376376
_usbh_data.call_after.arg = param;
377-
_usbh_data.call_after.at_ms = tusb_time_millis_api() + ms;
377+
// add one to ensure we wait at least 'ms' milliseconds
378+
_usbh_data.call_after.at_ms = tusb_time_millis_api() + ms + 1;
378379
return true;
379380
}
380381

@@ -610,11 +611,19 @@ bool tuh_task_event_ready(void) {
610611
}
611612

612613
#if CFG_TUH_HUB
613-
if (!osal_queue_empty(_usbh_daq)) {
614+
if (_usbh_data.enumerating_daddr == TUSB_INDEX_INVALID_8 &&
615+
!osal_queue_empty(_usbh_daq)) {
614616
return true;
615617
}
616618
#endif
617619

620+
if (_usbh_data.call_after.func) {
621+
int32_t remain_ms = (int32_t)(_usbh_data.call_after.at_ms - tusb_time_millis_api());
622+
if (remain_ms <= 0) {
623+
return true;
624+
}
625+
}
626+
618627
return false;
619628
}
620629

src/osal/osal_mynewt.h

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,24 @@ TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_unlock(osal_mutex_t mutex_hd
123123
return os_mutex_release(mutex_hdl) == OS_OK;
124124
}
125125

126+
TU_ATTR_ALWAYS_INLINE static inline os_time_t _osal_ms2tick(uint32_t msec) {
127+
if (msec == OSAL_TIMEOUT_WAIT_FOREVER) {
128+
return OS_TIMEOUT_NEVER;
129+
}
130+
if (msec == 0) {
131+
return 0;
132+
}
133+
134+
os_time_t ticks = os_time_ms_to_ticks32(msec);
135+
136+
// If 1 tick > 1 ms, still wait at least 1 tick for non-zero timeout.
137+
if (ticks == 0) {
138+
ticks = 1;
139+
}
140+
141+
return ticks;
142+
}
143+
126144
//--------------------------------------------------------------------+
127145
// QUEUE API
128146
//--------------------------------------------------------------------+
@@ -161,10 +179,11 @@ TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_delete(osal_queue_t qhdl) {
161179
}
162180

163181
TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_receive(osal_queue_t qhdl, void* data, uint32_t msec) {
164-
(void) msec; // os_eventq_get() does not take timeout, always behave as msec = WAIT_FOREVER
165-
166-
struct os_event* ev;
167-
ev = os_eventq_get(&qhdl->evq);
182+
struct os_eventq* evq = &qhdl->evq;
183+
struct os_event* ev = os_eventq_poll(&evq, 1, _osal_ms2tick(msec));
184+
if (!ev) {
185+
return false;
186+
}
168187

169188
memcpy(data, ev->ev_arg, qhdl->item_sz); // copy message
170189
os_memblock_put(&qhdl->mpool, ev->ev_arg); // put back mem block

src/tusb_option.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -534,10 +534,6 @@
534534
#define CFG_TUSB_OS OPT_OS_NONE
535535
#endif
536536

537-
#ifndef CFG_TUSB_OS_HAS_SCHEDULER
538-
#define CFG_TUSB_OS_HAS_SCHEDULER (CFG_TUSB_OS != OPT_OS_NONE && CFG_TUSB_OS != OPT_OS_PICO)
539-
#endif
540-
541537
#ifndef CFG_TUSB_OS_INC_PATH
542538
#ifndef CFG_TUSB_OS_INC_PATH_DEFAULT
543539
#define CFG_TUSB_OS_INC_PATH_DEFAULT

0 commit comments

Comments
 (0)