Skip to content

Commit b034c18

Browse files
committed
add tud_task_ext(), tuh_task_ext() as exteneded version that take timeout and in_isr
also allow exit tud_task,tuh_task after processing all events for running other background task for user
1 parent ccafb42 commit b034c18

File tree

6 files changed

+50
-15
lines changed

6 files changed

+50
-15
lines changed

examples/device/cdc_msc_freertos/src/main.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,9 @@ void cdc_task(void* params)
198198
tud_cdc_write(buf, count);
199199
}
200200
}
201+
202+
// For ESP32-Sx this delay is essential to allow idle how to run and reset watchdog
203+
vTaskDelay(1);
201204
}
202205
}
203206

src/common/tusb_verify.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@
9999
*------------------------------------------------------------------*/
100100

101101
// Helper to implement optional parameter for TU_VERIFY Macro family
102-
#define GET_3RD_ARG(arg1, arg2, arg3, ...) arg3
103-
#define GET_4TH_ARG(arg1, arg2, arg3, arg4, ...) arg4
102+
#define _GET_3RD_ARG(arg1, arg2, arg3, ...) arg3
103+
#define _GET_4TH_ARG(arg1, arg2, arg3, arg4, ...) arg4
104104

105105
/*------------- Generator for TU_VERIFY and TU_VERIFY_HDLR -------------*/
106106
#define TU_VERIFY_DEFINE(_cond, _handler, _ret) do \
@@ -116,7 +116,7 @@
116116
#define TU_VERIFY_1ARGS(_cond) TU_VERIFY_DEFINE(_cond, , false)
117117
#define TU_VERIFY_2ARGS(_cond, _ret) TU_VERIFY_DEFINE(_cond, , _ret)
118118

119-
#define TU_VERIFY(...) GET_3RD_ARG(__VA_ARGS__, TU_VERIFY_2ARGS, TU_VERIFY_1ARGS, UNUSED)(__VA_ARGS__)
119+
#define TU_VERIFY(...) _GET_3RD_ARG(__VA_ARGS__, TU_VERIFY_2ARGS, TU_VERIFY_1ARGS, UNUSED)(__VA_ARGS__)
120120

121121

122122
/*------------------------------------------------------------------*/
@@ -127,7 +127,7 @@
127127
#define TU_VERIFY_HDLR_2ARGS(_cond, _handler) TU_VERIFY_DEFINE(_cond, _handler, false)
128128
#define TU_VERIFY_HDLR_3ARGS(_cond, _handler, _ret) TU_VERIFY_DEFINE(_cond, _handler, _ret)
129129

130-
#define TU_VERIFY_HDLR(...) GET_4TH_ARG(__VA_ARGS__, TU_VERIFY_HDLR_3ARGS, TU_VERIFY_HDLR_2ARGS,UNUSED)(__VA_ARGS__)
130+
#define TU_VERIFY_HDLR(...) _GET_4TH_ARG(__VA_ARGS__, TU_VERIFY_HDLR_3ARGS, TU_VERIFY_HDLR_2ARGS,UNUSED)(__VA_ARGS__)
131131

132132
/*------------------------------------------------------------------*/
133133
/* ASSERT
@@ -139,7 +139,7 @@
139139
#define ASSERT_2ARGS(_cond, _ret) TU_VERIFY_DEFINE(_cond, _MESS_FAILED(); TU_BREAKPOINT(), _ret)
140140

141141
#ifndef TU_ASSERT
142-
#define TU_ASSERT(...) GET_3RD_ARG(__VA_ARGS__, ASSERT_2ARGS, ASSERT_1ARGS,UNUSED)(__VA_ARGS__)
142+
#define TU_ASSERT(...) _GET_3RD_ARG(__VA_ARGS__, ASSERT_2ARGS, ASSERT_1ARGS,UNUSED)(__VA_ARGS__)
143143
#endif
144144

145145
/*------------------------------------------------------------------*/

src/device/usbd.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -466,16 +466,18 @@ bool tud_task_event_ready(void)
466466
}
467467
@endcode
468468
*/
469-
void tud_task (void)
469+
void tud_task_ext(uint32_t timeout_ms, bool in_isr)
470470
{
471+
(void) in_isr; // not implemented yet
472+
471473
// Skip if stack is not initialized
472474
if ( !tusb_inited() ) return;
473475

474476
// Loop until there is no more events in the queue
475477
while (1)
476478
{
477479
dcd_event_t event;
478-
if ( !osal_queue_receive(_usbd_q, &event, 1) ) return;
480+
if ( !osal_queue_receive(_usbd_q, &event, timeout_ms) ) return;
479481

480482
#if CFG_TUSB_DEBUG >= 2
481483
if (event.event_id == DCD_EVENT_SETUP_RECEIVED) TU_LOG2("\r\n"); // extra line for setup
@@ -592,6 +594,11 @@ void tud_task (void)
592594
TU_BREAKPOINT();
593595
break;
594596
}
597+
598+
#if CFG_TUSB_OS != OPT_OS_NONE && CFG_TUSB_OS != OPT_OS_PICO
599+
// return if there is no more events, for application to run other background
600+
if (osal_queue_empty(_usbd_q)) return;
601+
#endif
595602
}
596603
}
597604

src/device/usbd.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,17 @@ bool tud_init (uint8_t rhport);
4343
// Check if device stack is already initialized
4444
bool tud_inited(void);
4545

46+
// Task function should be called in main/rtos loop, extended version of tud_task()
47+
// - timeout_ms: millisecond to wait, zero = no wait, 0xFFFFFFFF = wait forever
48+
// - in_isr: if function is called in ISR
49+
void tud_task_ext(uint32_t timeout_ms, bool in_isr);
50+
4651
// Task function should be called in main/rtos loop
47-
void tud_task (void);
52+
TU_ATTR_ALWAYS_INLINE static inline
53+
void tud_task (void)
54+
{
55+
tud_task_ext(OSAL_TIMEOUT_WAIT_FOREVER, false);
56+
}
4857

4958
// Check if there is pending events need proccessing by tud_task()
5059
bool tud_task_event_ready(void);

src/host/usbh.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,16 +392,18 @@ bool tuh_init(uint8_t rhport)
392392
}
393393
@endcode
394394
*/
395-
void tuh_task(void)
395+
void tuh_task_ext(uint32_t timeout_ms, bool in_isr)
396396
{
397+
(void) in_isr; // not implemented yet
398+
397399
// Skip if stack is not initialized
398400
if ( !tusb_inited() ) return;
399401

400402
// Loop until there is no more events in the queue
401403
while (1)
402404
{
403405
hcd_event_t event;
404-
if ( !osal_queue_receive(_usbh_q, &event, 1) ) return;
406+
if ( !osal_queue_receive(_usbh_q, &event, timeout_ms) ) return;
405407

406408
switch (event.event_id)
407409
{
@@ -497,6 +499,11 @@ void tuh_task(void)
497499

498500
default: break;
499501
}
502+
503+
#if CFG_TUSB_OS != OPT_OS_NONE && CFG_TUSB_OS != OPT_OS_PICO
504+
// return if there is no more events, for application to run other background
505+
if (osal_queue_empty(_usbh_q)) return;
506+
#endif
500507
}
501508
}
502509

src/host/usbh.h

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,17 @@ bool tuh_init(uint8_t rhport);
9191
// Check if host stack is already initialized
9292
bool tuh_inited(void);
9393

94+
// Task function should be called in main/rtos loop, extended version of tuh_task()
95+
// - timeout_ms: millisecond to wait, zero = no wait, 0xFFFFFFFF = wait forever
96+
// - in_isr: if function is called in ISR
97+
void tuh_task_ext(uint32_t timeout_ms, bool in_isr);
98+
9499
// Task function should be called in main/rtos loop
95-
void tuh_task(void);
100+
TU_ATTR_ALWAYS_INLINE static inline
101+
void tuh_task(void)
102+
{
103+
tuh_task_ext(OSAL_TIMEOUT_WAIT_FOREVER, false);
104+
}
96105

97106
// Interrupt handler, name alias to HCD
98107
extern void hcd_int_handler(uint8_t rhport);
@@ -106,17 +115,17 @@ tusb_speed_t tuh_speed_get(uint8_t daddr);
106115
bool tuh_mounted(uint8_t daddr);
107116

108117
// Check if device is suspended
109-
TU_ATTR_ALWAYS_INLINE
110-
static inline bool tuh_suspended(uint8_t daddr)
118+
TU_ATTR_ALWAYS_INLINE static inline
119+
bool tuh_suspended(uint8_t daddr)
111120
{
112121
// TODO implement suspend & resume on host
113122
(void) daddr;
114123
return false;
115124
}
116125

117126
// Check if device is ready to communicate with
118-
TU_ATTR_ALWAYS_INLINE
119-
static inline bool tuh_ready(uint8_t daddr)
127+
TU_ATTR_ALWAYS_INLINE static inline
128+
bool tuh_ready(uint8_t daddr)
120129
{
121130
return tuh_mounted(daddr) && !tuh_suspended(daddr);
122131
}

0 commit comments

Comments
 (0)