Skip to content

Commit 6641550

Browse files
authored
Merge pull request #3333 from hathach/espressif-dual-mode
fix compile issue with enabled both device and host stack for dwc2
2 parents 58b4104 + 94c1e05 commit 6641550

File tree

15 files changed

+174
-77
lines changed

15 files changed

+174
-77
lines changed

.PVS-Studio/.pvsconfig

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
//V_EXCLUDE_PATH */iar/cxarm*
2-
//V_EXCLUDE_PATH */pico-sdk/*
2+
//V_EXCLUDE_PATH */pico-sdk/
3+
//V_EXCLUDE_PATH */esp-idf/
4+
//V_EXCLUDE_PATH */hw/bsp/espressif/components/
5+
//V_EXCLUDE_PATH */hw/mcu/
36

47
//-V::2506 MISRA. A function should have a single point of exit at the end.
58
//-V::2514 MISRA. Unions should not be used.
69
//-V:memcpy:2547 [MISRA-C-17.7] The return value of non-void function 'memcpy' should be used.
710
//-V:printf:2547 [MISRA-C-17.7] The return value of non-void function 'printf' should be used.
11+
//-V::2584::{gintsts} dwc2
812
//-V::2600 [MISRA-C-21.6] The function with the 'printf' name should not be used.
913
//+V2614 DISABLE_LENGHT_LIMIT_CHECK:YES
1014
//-V:memcpy:2628 Pointer arguments to the 'memcpy' function should be pointers to qualified or unqualified versions of compatible types.

examples/device/video_capture/src/main.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ void usb_device_task(void *param);
5353
void video_task(void* param);
5454

5555
#if CFG_TUSB_OS == OPT_OS_FREERTOS
56-
void freertos_init_task(void);
56+
void freertos_init(void);
5757
#endif
5858

5959

@@ -65,7 +65,7 @@ int main(void) {
6565

6666
// If using FreeRTOS: create blinky, tinyusb device, video task
6767
#if CFG_TUSB_OS == OPT_OS_FREERTOS
68-
freertos_init_task();
68+
freertos_init();
6969
#else
7070
// init device stack on configured roothub port
7171
tusb_rhport_init_t dev_init = {
@@ -211,8 +211,12 @@ static void video_send_frame(void) {
211211
}
212212

213213
unsigned cur = board_millis();
214-
if (cur - start_ms < interval_ms) return; // not enough time
215-
if (tx_busy) return;
214+
if (cur - start_ms < interval_ms) {
215+
return; // not enough time
216+
}
217+
if (tx_busy) {
218+
return;
219+
}
216220
start_ms += interval_ms;
217221
tx_busy = 1;
218222

@@ -273,7 +277,9 @@ void led_blinking_task(void* param) {
273277
#if CFG_TUSB_OS == OPT_OS_FREERTOS
274278
vTaskDelay(blink_interval_ms / portTICK_PERIOD_MS);
275279
#else
276-
if (board_millis() - start_ms < blink_interval_ms) return; // not enough time
280+
if (board_millis() - start_ms < blink_interval_ms) {
281+
return; // not enough time
282+
}
277283
#endif
278284

279285
start_ms += blink_interval_ms;
@@ -336,7 +342,7 @@ void usb_device_task(void *param) {
336342
}
337343
}
338344

339-
void freertos_init_task(void) {
345+
void freertos_init(void) {
340346
#if configSUPPORT_STATIC_ALLOCATION
341347
xTaskCreateStatic(led_blinking_task, "blinky", BLINKY_STACK_SIZE, NULL, 1, blinky_stack, &blinky_taskdef);
342348
xTaskCreateStatic(usb_device_task, "usbd", USBD_STACK_SIZE, NULL, configMAX_PRIORITIES-1, usb_device_stack, &usb_device_taskdef);

examples/device/video_capture_2ch/src/main.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ void usb_device_task(void *param);
5353
void video_task(void* param);
5454

5555
#if CFG_TUSB_OS == OPT_OS_FREERTOS
56-
void freertos_init_task(void);
56+
void freertos_init(void);
5757
#endif
5858

5959

@@ -65,7 +65,7 @@ int main(void) {
6565

6666
// If using FreeRTOS: create blinky, tinyusb device, video task
6767
#if CFG_TUSB_OS == OPT_OS_FREERTOS
68-
freertos_init_task();
68+
freertos_init();
6969
#else
7070
// init device stack on configured roothub port
7171
tusb_rhport_init_t dev_init = {
@@ -343,7 +343,7 @@ void usb_device_task(void *param) {
343343
}
344344
}
345345

346-
void freertos_init_task(void) {
346+
void freertos_init(void) {
347347
#if configSUPPORT_STATIC_ALLOCATION
348348
xTaskCreateStatic(led_blinking_task, "blinky", BLINKY_STACK_SIZE, NULL, 1, blinky_stack, &blinky_taskdef);
349349
xTaskCreateStatic(usb_device_task, "usbd", USBD_STACK_SIZE, NULL, configMAX_PRIORITIES-1, usb_device_stack, &usb_device_taskdef);

examples/dual/host_info_to_device_cdc/CMakeLists.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ project(${PROJECT} C CXX ASM)
1010
# Checks this example is valid for the family and initializes the project
1111
family_initialize_project(${PROJECT} ${CMAKE_CURRENT_LIST_DIR})
1212

13+
# Espressif has its own cmake build system
14+
if(FAMILY STREQUAL "espressif")
15+
return()
16+
endif()
17+
1318
add_executable(${PROJECT})
1419

1520
# Example source
@@ -25,7 +30,7 @@ target_include_directories(${PROJECT} PUBLIC
2530

2631
# Configure compilation flags and libraries for the example without RTOS.
2732
# See the corresponding function in hw/bsp/FAMILY/family.cmake for details.
28-
family_configure_dual_usb_example(${PROJECT} noos)
33+
family_configure_dual_usb_example(${PROJECT} ${RTOS})
2934

3035
# due to warnings from Pico-PIO-USB
3136
if (FAMILY STREQUAL rp2040)

examples/dual/host_info_to_device_cdc/only.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ mcu:MAX3421
88
mcu:STM32F4
99
mcu:STM32F7
1010
mcu:STM32H7
11+
mcu:ESP32P4
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# This file is for ESP-IDF only
2+
idf_component_register(SRCS "main.c" "usb_descriptors.c"
3+
INCLUDE_DIRS "."
4+
REQUIRES boards tinyusb_src)

examples/dual/host_info_to_device_cdc/src/main.c

Lines changed: 114 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,12 @@ static tusb_desc_device_t descriptor_device[CFG_TUH_DEVICE_MAX+1];
7575
static void print_utf16(uint16_t *temp_buf, size_t buf_len);
7676
static 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-
263315
static 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

examples/host/cdc_msc_hid_freertos/only.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
mcu:ESP32P4
21
mcu:LPC175X_6X
32
mcu:LPC177X_8X
43
mcu:LPC18XX
@@ -15,5 +14,6 @@ mcu:STM32F7
1514
mcu:STM32H7
1615
mcu:STM32H7RS
1716
mcu:STM32N6
17+
family:espressif
1818
family:samd21
1919
family:samd5x_e5x

examples/host/device_info/only.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
mcu:CH32V20X
2-
mcu:ESP32S2
3-
mcu:ESP32S3
4-
mcu:ESP32P4
52
mcu:KINETIS_KL
63
mcu:LPC175X_6X
74
mcu:LPC177X_8X
@@ -21,5 +18,6 @@ mcu:STM32F7
2118
mcu:STM32H7
2219
mcu:STM32H7RS
2320
mcu:STM32N6
21+
family:espressif
2422
family:samd21
2523
family:samd5x_e5x

0 commit comments

Comments
 (0)