Skip to content

Commit f8a5cde

Browse files
committed
add tuh_task_event_ready(), better implement blocking control transfer for rtos
1 parent 0921eda commit f8a5cde

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

src/host/usbh.c

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,14 @@ bool tuh_init(uint8_t controller_id)
360360
return true;
361361
}
362362

363+
bool tuh_task_event_ready(void)
364+
{
365+
// Skip if stack is not initialized
366+
if ( !tuh_inited() ) return false;
367+
368+
return !osal_queue_empty(_usbh_q);
369+
}
370+
363371
/* USB Host Driver task
364372
* This top level thread manages all host controller event and delegates events to class-specific drivers.
365373
* This should be called periodically within the mainloop or rtos thread.
@@ -383,7 +391,7 @@ void tuh_task_ext(uint32_t timeout_ms, bool in_isr)
383391
(void) in_isr; // not implemented yet
384392

385393
// Skip if stack is not initialized
386-
if ( !tusb_inited() ) return;
394+
if ( !tuh_inited() ) return;
387395

388396
// Loop until there is no more events in the queue
389397
while (1)
@@ -565,12 +573,12 @@ bool tuh_control_xfer (tuh_xfer_t* xfer)
565573

566574
while (result == XFER_RESULT_INVALID)
567575
{
568-
// only need to call task if not preempted RTOS
569-
#if CFG_TUSB_OS == OPT_OS_NONE || CFG_TUSB_OS == OPT_OS_PICO
570-
tuh_task();
571-
#else
572-
osal_task_delay(1); // TODO maybe yield()
573-
#endif
576+
// Note: this can be called within an callback ie. part of tuh_task()
577+
// therefore event with RTOS tuh_task() still need to be invoked
578+
if (tuh_task_event_ready())
579+
{
580+
tuh_task();
581+
}
574582

575583
// TODO probably some timeout to prevent hanged
576584
}

src/host/usbh.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@ struct tuh_xfer_s
6969
// uint32_t timeout_ms; // place holder, not supported yet
7070
};
7171

72+
// Subject to change
73+
typedef struct
74+
{
75+
uint8_t daddr;
76+
tusb_desc_interface_t desc;
77+
} tuh_itf_info_t;
78+
7279
// ConfigID for tuh_config()
7380
enum
7481
{
@@ -118,6 +125,9 @@ void tuh_task(void)
118125
tuh_task_ext(UINT32_MAX, false);
119126
}
120127

128+
// Check if there is pending events need processing by tuh_task()
129+
bool tuh_task_event_ready(void);
130+
121131
#ifndef _TUSB_HCD_H_
122132
extern void hcd_int_handler(uint8_t rhport);
123133
#endif

0 commit comments

Comments
 (0)