4141
4242 #define USBD_STACK_SIZE 4096
4343#else
44+
4445 #include "FreeRTOS.h"
4546 #include "semphr.h"
4647 #include "queue.h"
5455#define CDC_STACK_SZIE configMINIMAL_STACK_SIZE
5556
5657//--------------------------------------------------------------------+
57- // MACRO CONSTANT TYPEDEF PROTYPES
58+ // MACRO CONSTANT TYPEDEF PROTOTYPES
5859//--------------------------------------------------------------------+
5960
6061/* Blink pattern
6162 * - 250 ms : device not mounted
6263 * - 1000 ms : device mounted
6364 * - 2500 ms : device is suspended
6465 */
65- enum {
66+ enum {
6667 BLINK_NOT_MOUNTED = 250 ,
6768 BLINK_MOUNTED = 1000 ,
6869 BLINK_SUSPENDED = 2500 ,
@@ -81,16 +82,15 @@ StaticTask_t cdc_taskdef;
8182
8283TimerHandle_t blinky_tm ;
8384
84- void led_blinky_cb (TimerHandle_t xTimer );
85- void usb_device_task (void * param );
86- void cdc_task (void * params );
85+ static void led_blinky_cb (TimerHandle_t xTimer );
86+ static void usb_device_task (void * param );
87+ void cdc_task (void * params );
8788
8889//--------------------------------------------------------------------+
8990// Main
9091//--------------------------------------------------------------------+
9192
92- int main (void )
93- {
93+ int main (void ) {
9494 board_init ();
9595
9696#if configSUPPORT_STATIC_ALLOCATION
@@ -104,8 +104,8 @@ int main(void)
104104 xTaskCreateStatic (cdc_task , "cdc" , CDC_STACK_SZIE , NULL , configMAX_PRIORITIES - 2 , cdc_stack , & cdc_taskdef );
105105#else
106106 blinky_tm = xTimerCreate (NULL , pdMS_TO_TICKS (BLINK_NOT_MOUNTED ), true, NULL , led_blinky_cb );
107- xTaskCreate ( usb_device_task , "usbd" , USBD_STACK_SIZE , NULL , configMAX_PRIORITIES - 1 , NULL );
108- xTaskCreate ( cdc_task , "cdc" , CDC_STACK_SZIE , NULL , configMAX_PRIORITIES - 2 , NULL );
107+ xTaskCreate (usb_device_task , "usbd" , USBD_STACK_SIZE , NULL , configMAX_PRIORITIES - 1 , NULL );
108+ xTaskCreate (cdc_task , "cdc" , CDC_STACK_SZIE , NULL , configMAX_PRIORITIES - 2 , NULL );
109109#endif
110110
111111 xTimerStart (blinky_tm , 0 );
@@ -119,16 +119,14 @@ int main(void)
119119}
120120
121121#if TU_CHECK_MCU (OPT_MCU_ESP32S2 , OPT_MCU_ESP32S3 )
122- void app_main (void )
123- {
122+ void app_main (void ) {
124123 main ();
125124}
126125#endif
127126
128127// USB Device Driver task
129128// This top level thread process all usb events and invoke callbacks
130- void usb_device_task (void * param )
131- {
129+ static void usb_device_task (void * param ) {
132130 (void ) param ;
133131
134132 // init device stack on configured roothub port
@@ -141,8 +139,7 @@ void usb_device_task(void* param)
141139 }
142140
143141 // RTOS forever loop
144- while (1 )
145- {
142+ while (1 ) {
146143 // put this thread to waiting state until there is new events
147144 tud_task ();
148145
@@ -156,56 +153,46 @@ void usb_device_task(void* param)
156153//--------------------------------------------------------------------+
157154
158155// Invoked when device is mounted
159- void tud_mount_cb (void )
160- {
156+ void tud_mount_cb (void ) {
161157 xTimerChangePeriod (blinky_tm , pdMS_TO_TICKS (BLINK_MOUNTED ), 0 );
162158}
163159
164160// Invoked when device is unmounted
165- void tud_umount_cb (void )
166- {
161+ void tud_umount_cb (void ) {
167162 xTimerChangePeriod (blinky_tm , pdMS_TO_TICKS (BLINK_NOT_MOUNTED ), 0 );
168163}
169164
170165// Invoked when usb bus is suspended
171166// remote_wakeup_en : if host allow us to perform remote wakeup
172167// Within 7ms, device must draw an average of current less than 2.5 mA from bus
173- void tud_suspend_cb (bool remote_wakeup_en )
174- {
168+ void tud_suspend_cb (bool remote_wakeup_en ) {
175169 (void ) remote_wakeup_en ;
176170 xTimerChangePeriod (blinky_tm , pdMS_TO_TICKS (BLINK_SUSPENDED ), 0 );
177171}
178172
179173// Invoked when usb bus is resumed
180- void tud_resume_cb (void )
181- {
182- if (tud_mounted ())
183- {
174+ void tud_resume_cb (void ) {
175+ if (tud_mounted ()) {
184176 xTimerChangePeriod (blinky_tm , pdMS_TO_TICKS (BLINK_MOUNTED ), 0 );
185- }
186- else
187- {
177+ } else {
188178 xTimerChangePeriod (blinky_tm , pdMS_TO_TICKS (BLINK_NOT_MOUNTED ), 0 );
189179 }
190180}
191181
192182//--------------------------------------------------------------------+
193183// USB CDC
194184//--------------------------------------------------------------------+
195- void cdc_task (void * params )
196- {
185+ void cdc_task (void * params ) {
197186 (void ) params ;
198187
199188 // RTOS forever loop
200- while ( 1 )
201- {
189+ while (1 ) {
202190 // connected() check for DTR bit
203191 // Most but not all terminal client set this when making connection
204192 // if ( tud_cdc_connected() )
205193 {
206194 // There are data available
207- while ( tud_cdc_available () )
208- {
195+ while (tud_cdc_available ()) {
209196 uint8_t buf [64 ];
210197
211198 // read and echo back
@@ -228,32 +215,27 @@ void cdc_task(void* params)
228215}
229216
230217// Invoked when cdc when line state changed e.g connected/disconnected
231- void tud_cdc_line_state_cb (uint8_t itf , bool dtr , bool rts )
232- {
218+ void tud_cdc_line_state_cb (uint8_t itf , bool dtr , bool rts ) {
233219 (void ) itf ;
234220 (void ) rts ;
235221
236222 // TODO set some indicator
237- if ( dtr )
238- {
223+ if (dtr ) {
239224 // Terminal connected
240- }else
241- {
225+ } else {
242226 // Terminal disconnected
243227 }
244228}
245229
246230// Invoked when CDC interface received data from host
247- void tud_cdc_rx_cb (uint8_t itf )
248- {
231+ void tud_cdc_rx_cb (uint8_t itf ) {
249232 (void ) itf ;
250233}
251234
252235//--------------------------------------------------------------------+
253236// BLINKING TASK
254237//--------------------------------------------------------------------+
255- void led_blinky_cb (TimerHandle_t xTimer )
256- {
238+ static void led_blinky_cb (TimerHandle_t xTimer ) {
257239 (void ) xTimer ;
258240 static bool led_state = false;
259241
0 commit comments