@@ -75,8 +75,12 @@ static tusb_desc_device_t descriptor_device[CFG_TUH_DEVICE_MAX+1];
7575static void print_utf16 (uint16_t * temp_buf , size_t buf_len );
7676static void print_device_info (uint8_t daddr , const tusb_desc_device_t * desc_device );
7777
78- void led_blinking_task (void );
79- void cdc_task (void );
78+ static void led_blinking_task (void );
79+ static void cdc_task (void );
80+
81+ #if CFG_TUSB_OS == OPT_OS_FREERTOS
82+ static void freertos_init (void );
83+ #endif
8084
8185#define cdc_printf (...) \
8286 do { \
@@ -94,37 +98,86 @@ void cdc_task(void);
9498 } \
9599 } while(0)
96100
97- /*------------- MAIN -------------*/
98- int main (void ) {
99- board_init ();
100-
101- printf ("TinyUSB Host Information -> Device CDC Example\r\n" );
102101
102+ static void usb_device_init (void ) {
103103 // init device and host stack on configured roothub port
104104 tusb_rhport_init_t dev_init = {
105105 .role = TUSB_ROLE_DEVICE ,
106106 .speed = TUSB_SPEED_AUTO
107107 };
108108 tusb_init (BOARD_TUD_RHPORT , & dev_init );
109+ board_init_after_tusb ();
110+ }
109111
112+ static void usb_host_init (void ) {
113+ // init host stack on configured roothub port
110114 tusb_rhport_init_t host_init = {
111115 .role = TUSB_ROLE_HOST ,
112116 .speed = TUSB_SPEED_AUTO
113117 };
114118 tusb_init (BOARD_TUH_RHPORT , & host_init );
115-
116119 board_init_after_tusb ();
120+ }
121+
122+ //--------------------------------------------------------------------+
123+ // Main
124+ //--------------------------------------------------------------------+
125+ static void main_task (void * param ) {
126+ (void ) param ;
117127
118128 while (1 ) {
119- tud_task (); // tinyusb device task
120- tuh_task (); // tinyusb host task
121129 cdc_task ();
122130 led_blinking_task ();
131+
132+ // preempted RTOS run device/host stack in its own task
133+ #if CFG_TUSB_OS == OPT_OS_NONE || CFG_TUSB_OS == OPT_OS_PICO
134+ tud_task (); // tinyusb device task
135+ tuh_task (); // tinyusb host task
136+ #endif
123137 }
138+ }
139+
140+ int main (void ) {
141+ board_init ();
142+
143+ #if CFG_TUSB_OS == OPT_OS_NONE || CFG_TUSB_OS == OPT_OS_PICO
144+ printf ("TinyUSB Host Information -> Device CDC Example\r\n" );
145+
146+ usb_device_init ();
147+ usb_host_init ();
148+
149+ main_task (NULL );
150+ #elif CFG_TUSB_OS == OPT_OS_FREERTOS
151+ freertos_init (); // create RTOS tasks for device, host stack and main_task()
152+ #else
153+ #error RTOS not supported
154+ #endif
124155
125156 return 0 ;
126157}
127158
159+ #if CFG_TUSB_OS != OPT_OS_NONE && CFG_TUSB_OS != OPT_OS_PICO
160+ // USB Device Driver task for RTOS
161+ static void usb_device_task (void * param ) {
162+ (void ) param ;
163+ usb_device_init ();
164+ while (1 ) {
165+ // put this thread to waiting state until there is new events
166+ tud_task ();
167+ }
168+ }
169+
170+ static void usb_host_task (void * param ) {
171+ (void ) param ;
172+ usb_host_init ();
173+ while (1 ) {
174+ // put this thread to waiting state until there is new events
175+ tuh_task ();
176+ }
177+ }
178+ #endif
179+
180+
128181//--------------------------------------------------------------------+
129182// Device CDC
130183//--------------------------------------------------------------------+
@@ -156,7 +209,7 @@ void cdc_task(void) {
156209 if (!tud_cdc_connected ()) {
157210 // delay a bit otherwise we can outpace host's terminal. Linux will set LineState (DTR) then Line Coding.
158211 // If we send data before Linux's terminal set Line Coding, it can be ignored --> missing data with hardware test loop
159- board_delay (20 );
212+ tusb_time_delay_ms_api (20 );
160213 return ;
161214 }
162215
@@ -259,7 +312,6 @@ void led_blinking_task(void) {
259312//--------------------------------------------------------------------+
260313// String Descriptor Helper
261314//--------------------------------------------------------------------+
262-
263315static void _convert_utf16le_to_utf8 (const uint16_t * utf16 , size_t utf16_len , uint8_t * utf8 , size_t utf8_len ) {
264316 // TODO: Check for runover.
265317 (void )utf8_len ;
@@ -310,3 +362,53 @@ static void print_utf16(uint16_t *temp_buf, size_t buf_len) {
310362
311363 cdc_printf ("%s" , (char * ) temp_buf );
312364}
365+
366+ //--------------------------------------------------------------------+
367+ // FreeRTOS
368+ //--------------------------------------------------------------------+
369+ #if CFG_TUSB_OS == OPT_OS_FREERTOS
370+
371+ #ifdef ESP_PLATFORM
372+ #define USBD_STACK_SIZE 4096
373+ #define USBH_STACK_SIZE 4096
374+ void app_main (void ) {
375+ main ();
376+ }
377+ #else
378+ // Increase stack size when debug log is enabled
379+ #define USBD_STACK_SIZE (configMINIMAL_STACK_SIZE * (CFG_TUSB_DEBUG ? 4 : 2))
380+ #define USBH_STACK_SIZE (configMINIMAL_STACK_SIZE * (CFG_TUSB_DEBUG ? 4 : 2))
381+ #endif
382+
383+ #define MAIN_STACK_SIZE (configMINIMAL_STACK_SIZE*4)
384+
385+ // static task
386+ #if configSUPPORT_STATIC_ALLOCATION
387+ StackType_t main_stack [MAIN_STACK_SIZE ];
388+ StaticTask_t main_taskdef ;
389+
390+ StackType_t usb_device_stack [USBD_STACK_SIZE ];
391+ StaticTask_t usb_device_taskdef ;
392+
393+ StackType_t usb_host_stack [USBH_STACK_SIZE ];
394+ StaticTask_t usb_host_taskdef ;
395+ #endif
396+
397+ void freertos_init (void ) {
398+ #if configSUPPORT_STATIC_ALLOCATION
399+ xTaskCreateStatic (usb_device_task , "usbd" , USBD_STACK_SIZE , NULL , configMAX_PRIORITIES - 1 , usb_device_stack , & usb_device_taskdef );
400+ xTaskCreateStatic (usb_host_task , "usbh" , USBH_STACK_SIZE , NULL , configMAX_PRIORITIES - 1 , usb_host_stack , & usb_host_taskdef );
401+ xTaskCreateStatic (main_task , "main" , MAIN_STACK_SIZE , NULL , configMAX_PRIORITIES - 2 , main_stack , & main_taskdef );
402+ #else
403+ xTaskCreate (usb_device_task , "usbd" , USBD_STACK_SIZE , NULL , configMAX_PRIORITIES - 1 , NULL );
404+ xTaskCreate (usb_host_task , "usbh" , USBH_STACK_SIZE , NULL , configMAX_PRIORITIES - 1 , NULL );
405+ xTaskCreate (main_task , "main" , MAIN_STACK_SIZE , NULL , configMAX_PRIORITIES - 2 , NULL );
406+ #endif
407+
408+ // only start scheduler for non-espressif mcu
409+ #ifndef ESP_PLATFORM
410+ vTaskStartScheduler ();
411+ #endif
412+ }
413+
414+ #endif
0 commit comments