5252 #define USBD_STACK_SIZE (3*configMINIMAL_STACK_SIZE/2) * (CFG_TUSB_DEBUG ? 2 : 1)
5353#endif
5454
55- #define CDC_STACK_SZIE configMINIMAL_STACK_SIZE
55+ #define CDC_STACK_SIZE configMINIMAL_STACK_SIZE
56+ #define BLINKY_STACK_SIZE configMINIMAL_STACK_SIZE
5657
5758//--------------------------------------------------------------------+
5859// MACRO CONSTANT TYPEDEF PROTOTYPES
@@ -69,21 +70,22 @@ enum {
6970 BLINK_SUSPENDED = 2500 ,
7071};
7172
72- // static timer & task
73+ // static task
7374#if configSUPPORT_STATIC_ALLOCATION
74- StaticTimer_t blinky_tmdef ;
75+ StackType_t blinky_stack [BLINKY_STACK_SIZE ];
76+ StaticTask_t blinky_taskdef ;
7577
7678StackType_t usb_device_stack [USBD_STACK_SIZE ];
7779StaticTask_t usb_device_taskdef ;
7880
79- StackType_t cdc_stack [CDC_STACK_SZIE ];
81+ StackType_t cdc_stack [CDC_STACK_SIZE ];
8082StaticTask_t cdc_taskdef ;
8183#endif
8284
83- TimerHandle_t blinky_tm ;
85+ static uint32_t blink_interval_ms = BLINK_NOT_MOUNTED ;
8486
85- static void led_blinky_cb (TimerHandle_t xTimer );
8687static void usb_device_task (void * param );
88+ void led_blinking_task (void * param );
8789void cdc_task (void * params );
8890
8991//--------------------------------------------------------------------+
@@ -94,22 +96,20 @@ int main(void) {
9496 board_init ();
9597
9698#if configSUPPORT_STATIC_ALLOCATION
97- // soft timer for blinky
98- blinky_tm = xTimerCreateStatic ( NULL , pdMS_TO_TICKS ( BLINK_NOT_MOUNTED ), true , NULL , led_blinky_cb , & blinky_tmdef );
99+ // blinky task
100+ xTaskCreateStatic ( led_blinking_task , "blinky" , BLINKY_STACK_SIZE , NULL , 1 , blinky_stack , & blinky_taskdef );
99101
100102 // Create a task for tinyusb device stack
101103 xTaskCreateStatic (usb_device_task , "usbd" , USBD_STACK_SIZE , NULL , configMAX_PRIORITIES - 1 , usb_device_stack , & usb_device_taskdef );
102104
103105 // Create CDC task
104- xTaskCreateStatic (cdc_task , "cdc" , CDC_STACK_SZIE , NULL , configMAX_PRIORITIES - 2 , cdc_stack , & cdc_taskdef );
106+ xTaskCreateStatic (cdc_task , "cdc" , CDC_STACK_SIZE , NULL , configMAX_PRIORITIES - 2 , cdc_stack , & cdc_taskdef );
105107#else
106- blinky_tm = xTimerCreate ( NULL , pdMS_TO_TICKS ( BLINK_NOT_MOUNTED ), true , NULL , led_blinky_cb );
108+ xTaskCreate ( led_blinking_task , "blinky" , BLINKY_STACK_SIZE , NULL , 1 , NULL );
107109 xTaskCreate (usb_device_task , "usbd" , USBD_STACK_SIZE , NULL , configMAX_PRIORITIES - 1 , NULL );
108110 xTaskCreate (cdc_task , "cdc" , CDC_STACK_SZIE , NULL , configMAX_PRIORITIES - 2 , NULL );
109111#endif
110112
111- xTimerStart (blinky_tm , 0 );
112-
113113 // skip starting scheduler (and return) for ESP32-S2 or ESP32-S3
114114#if !TU_CHECK_MCU (OPT_MCU_ESP32S2 , OPT_MCU_ESP32S3 )
115115 vTaskStartScheduler ();
@@ -154,29 +154,25 @@ static void usb_device_task(void *param) {
154154
155155// Invoked when device is mounted
156156void tud_mount_cb (void ) {
157- xTimerChangePeriod ( blinky_tm , pdMS_TO_TICKS ( BLINK_MOUNTED ), 0 ) ;
157+ blink_interval_ms = BLINK_MOUNTED ;
158158}
159159
160160// Invoked when device is unmounted
161161void tud_umount_cb (void ) {
162- xTimerChangePeriod ( blinky_tm , pdMS_TO_TICKS ( BLINK_NOT_MOUNTED ), 0 ) ;
162+ blink_interval_ms = BLINK_NOT_MOUNTED ;
163163}
164164
165165// Invoked when usb bus is suspended
166166// remote_wakeup_en : if host allow us to perform remote wakeup
167167// Within 7ms, device must draw an average of current less than 2.5 mA from bus
168168void tud_suspend_cb (bool remote_wakeup_en ) {
169169 (void ) remote_wakeup_en ;
170- xTimerChangePeriod ( blinky_tm , pdMS_TO_TICKS ( BLINK_SUSPENDED ), 0 ) ;
170+ blink_interval_ms = BLINK_SUSPENDED ;
171171}
172172
173173// Invoked when usb bus is resumed
174174void tud_resume_cb (void ) {
175- if (tud_mounted ()) {
176- xTimerChangePeriod (blinky_tm , pdMS_TO_TICKS (BLINK_MOUNTED ), 0 );
177- } else {
178- xTimerChangePeriod (blinky_tm , pdMS_TO_TICKS (BLINK_NOT_MOUNTED ), 0 );
179- }
175+ blink_interval_ms = tud_mounted () ? BLINK_MOUNTED : BLINK_NOT_MOUNTED ;
180176}
181177
182178//--------------------------------------------------------------------+
@@ -235,10 +231,17 @@ void tud_cdc_rx_cb(uint8_t itf) {
235231//--------------------------------------------------------------------+
236232// BLINKING TASK
237233//--------------------------------------------------------------------+
238- static void led_blinky_cb (TimerHandle_t xTimer ) {
239- (void ) xTimer ;
234+ void led_blinking_task (void * param ) {
235+ (void ) param ;
236+ static uint32_t start_ms = 0 ;
240237 static bool led_state = false;
241238
242- board_led_write (led_state );
243- led_state = 1 - led_state ; // toggle
239+ while (1 ) {
240+ // Blink every interval ms
241+ vTaskDelay (blink_interval_ms / portTICK_PERIOD_MS );
242+ start_ms += blink_interval_ms ;
243+
244+ board_led_write (led_state );
245+ led_state = 1 - led_state ; // toggle
246+ }
244247}
0 commit comments