make host info to devcie cdc to work with freertos/noos

This commit is contained in:
hathach
2025-11-06 18:26:04 +07:00
parent 1f84dd595a
commit cd90d94e7c
8 changed files with 146 additions and 24 deletions

View File

@ -1,10 +1,14 @@
//V_EXCLUDE_PATH */iar/cxarm* //V_EXCLUDE_PATH */iar/cxarm*
//V_EXCLUDE_PATH */pico-sdk/* //V_EXCLUDE_PATH */pico-sdk/
//V_EXCLUDE_PATH */esp-idf/
//V_EXCLUDE_PATH */hw/bsp/espressif/components/
//V_EXCLUDE_PATH */hw/mcu/
//-V::2506 MISRA. A function should have a single point of exit at the end. //-V::2506 MISRA. A function should have a single point of exit at the end.
//-V::2514 MISRA. Unions should not be used. //-V::2514 MISRA. Unions should not be used.
//-V:memcpy:2547 [MISRA-C-17.7] The return value of non-void function 'memcpy' should be used. //-V:memcpy:2547 [MISRA-C-17.7] The return value of non-void function 'memcpy' should be used.
//-V:printf:2547 [MISRA-C-17.7] The return value of non-void function 'printf' should be used. //-V:printf:2547 [MISRA-C-17.7] The return value of non-void function 'printf' should be used.
//-V::2584::{gintsts} dwc2
//-V::2600 [MISRA-C-21.6] The function with the 'printf' name should not be used. //-V::2600 [MISRA-C-21.6] The function with the 'printf' name should not be used.
//+V2614 DISABLE_LENGHT_LIMIT_CHECK:YES //+V2614 DISABLE_LENGHT_LIMIT_CHECK:YES
//-V:memcpy:2628 Pointer arguments to the 'memcpy' function should be pointers to qualified or unqualified versions of compatible types. //-V:memcpy:2628 Pointer arguments to the 'memcpy' function should be pointers to qualified or unqualified versions of compatible types.

View File

@ -53,7 +53,7 @@ void usb_device_task(void *param);
void video_task(void* param); void video_task(void* param);
#if CFG_TUSB_OS == OPT_OS_FREERTOS #if CFG_TUSB_OS == OPT_OS_FREERTOS
void freertos_init_task(void); void freertos_init(void);
#endif #endif
@ -65,7 +65,7 @@ int main(void) {
// If using FreeRTOS: create blinky, tinyusb device, video task // If using FreeRTOS: create blinky, tinyusb device, video task
#if CFG_TUSB_OS == OPT_OS_FREERTOS #if CFG_TUSB_OS == OPT_OS_FREERTOS
freertos_init_task(); freertos_init();
#else #else
// init device stack on configured roothub port // init device stack on configured roothub port
tusb_rhport_init_t dev_init = { tusb_rhport_init_t dev_init = {
@ -211,8 +211,12 @@ static void video_send_frame(void) {
} }
unsigned cur = board_millis(); unsigned cur = board_millis();
if (cur - start_ms < interval_ms) return; // not enough time if (cur - start_ms < interval_ms) {
if (tx_busy) return; return; // not enough time
}
if (tx_busy) {
return;
}
start_ms += interval_ms; start_ms += interval_ms;
tx_busy = 1; tx_busy = 1;
@ -273,7 +277,9 @@ void led_blinking_task(void* param) {
#if CFG_TUSB_OS == OPT_OS_FREERTOS #if CFG_TUSB_OS == OPT_OS_FREERTOS
vTaskDelay(blink_interval_ms / portTICK_PERIOD_MS); vTaskDelay(blink_interval_ms / portTICK_PERIOD_MS);
#else #else
if (board_millis() - start_ms < blink_interval_ms) return; // not enough time if (board_millis() - start_ms < blink_interval_ms) {
return; // not enough time
}
#endif #endif
start_ms += blink_interval_ms; start_ms += blink_interval_ms;
@ -336,7 +342,7 @@ void usb_device_task(void *param) {
} }
} }
void freertos_init_task(void) { void freertos_init(void) {
#if configSUPPORT_STATIC_ALLOCATION #if configSUPPORT_STATIC_ALLOCATION
xTaskCreateStatic(led_blinking_task, "blinky", BLINKY_STACK_SIZE, NULL, 1, blinky_stack, &blinky_taskdef); xTaskCreateStatic(led_blinking_task, "blinky", BLINKY_STACK_SIZE, NULL, 1, blinky_stack, &blinky_taskdef);
xTaskCreateStatic(usb_device_task, "usbd", USBD_STACK_SIZE, NULL, configMAX_PRIORITIES-1, usb_device_stack, &usb_device_taskdef); xTaskCreateStatic(usb_device_task, "usbd", USBD_STACK_SIZE, NULL, configMAX_PRIORITIES-1, usb_device_stack, &usb_device_taskdef);

View File

@ -53,7 +53,7 @@ void usb_device_task(void *param);
void video_task(void* param); void video_task(void* param);
#if CFG_TUSB_OS == OPT_OS_FREERTOS #if CFG_TUSB_OS == OPT_OS_FREERTOS
void freertos_init_task(void); void freertos_init(void);
#endif #endif
@ -65,7 +65,7 @@ int main(void) {
// If using FreeRTOS: create blinky, tinyusb device, video task // If using FreeRTOS: create blinky, tinyusb device, video task
#if CFG_TUSB_OS == OPT_OS_FREERTOS #if CFG_TUSB_OS == OPT_OS_FREERTOS
freertos_init_task(); freertos_init();
#else #else
// init device stack on configured roothub port // init device stack on configured roothub port
tusb_rhport_init_t dev_init = { tusb_rhport_init_t dev_init = {
@ -343,7 +343,7 @@ void usb_device_task(void *param) {
} }
} }
void freertos_init_task(void) { void freertos_init(void) {
#if configSUPPORT_STATIC_ALLOCATION #if configSUPPORT_STATIC_ALLOCATION
xTaskCreateStatic(led_blinking_task, "blinky", BLINKY_STACK_SIZE, NULL, 1, blinky_stack, &blinky_taskdef); xTaskCreateStatic(led_blinking_task, "blinky", BLINKY_STACK_SIZE, NULL, 1, blinky_stack, &blinky_taskdef);
xTaskCreateStatic(usb_device_task, "usbd", USBD_STACK_SIZE, NULL, configMAX_PRIORITIES-1, usb_device_stack, &usb_device_taskdef); xTaskCreateStatic(usb_device_task, "usbd", USBD_STACK_SIZE, NULL, configMAX_PRIORITIES-1, usb_device_stack, &usb_device_taskdef);

View File

@ -10,6 +10,11 @@ project(${PROJECT} C CXX ASM)
# Checks this example is valid for the family and initializes the project # Checks this example is valid for the family and initializes the project
family_initialize_project(${PROJECT} ${CMAKE_CURRENT_LIST_DIR}) family_initialize_project(${PROJECT} ${CMAKE_CURRENT_LIST_DIR})
# Espressif has its own cmake build system
if(FAMILY STREQUAL "espressif")
return()
endif()
add_executable(${PROJECT}) add_executable(${PROJECT})
# Example source # Example source
@ -25,7 +30,7 @@ target_include_directories(${PROJECT} PUBLIC
# Configure compilation flags and libraries for the example without RTOS. # Configure compilation flags and libraries for the example without RTOS.
# See the corresponding function in hw/bsp/FAMILY/family.cmake for details. # See the corresponding function in hw/bsp/FAMILY/family.cmake for details.
family_configure_dual_usb_example(${PROJECT} noos) family_configure_dual_usb_example(${PROJECT} ${RTOS})
# due to warnings from Pico-PIO-USB # due to warnings from Pico-PIO-USB
if (FAMILY STREQUAL rp2040) if (FAMILY STREQUAL rp2040)

View File

@ -8,3 +8,4 @@ mcu:MAX3421
mcu:STM32F4 mcu:STM32F4
mcu:STM32F7 mcu:STM32F7
mcu:STM32H7 mcu:STM32H7
mcu:ESP32P4

View File

@ -0,0 +1,4 @@
# This file is for ESP-IDF only
idf_component_register(SRCS "main.c" "usb_descriptors.c"
INCLUDE_DIRS "."
REQUIRES boards tinyusb_src)

View File

@ -75,8 +75,12 @@ static tusb_desc_device_t descriptor_device[CFG_TUH_DEVICE_MAX+1];
static void print_utf16(uint16_t *temp_buf, size_t buf_len); static void print_utf16(uint16_t *temp_buf, size_t buf_len);
static void print_device_info(uint8_t daddr, const tusb_desc_device_t* desc_device); static void print_device_info(uint8_t daddr, const tusb_desc_device_t* desc_device);
void led_blinking_task(void); static void led_blinking_task(void);
void cdc_task(void); static void cdc_task(void);
#if CFG_TUSB_OS == OPT_OS_FREERTOS
static void freertos_init(void);
#endif
#define cdc_printf(...) \ #define cdc_printf(...) \
do { \ do { \
@ -94,37 +98,86 @@ void cdc_task(void);
} \ } \
} while(0) } while(0)
/*------------- MAIN -------------*/
int main(void) {
board_init();
printf("TinyUSB Host Information -> Device CDC Example\r\n");
static void usb_device_init(void) {
// init device and host stack on configured roothub port // init device and host stack on configured roothub port
tusb_rhport_init_t dev_init = { tusb_rhport_init_t dev_init = {
.role = TUSB_ROLE_DEVICE, .role = TUSB_ROLE_DEVICE,
.speed = TUSB_SPEED_AUTO .speed = TUSB_SPEED_AUTO
}; };
tusb_init(BOARD_TUD_RHPORT, &dev_init); tusb_init(BOARD_TUD_RHPORT, &dev_init);
board_init_after_tusb();
}
static void usb_host_init(void) {
// init host stack on configured roothub port
tusb_rhport_init_t host_init = { tusb_rhport_init_t host_init = {
.role = TUSB_ROLE_HOST, .role = TUSB_ROLE_HOST,
.speed = TUSB_SPEED_AUTO .speed = TUSB_SPEED_AUTO
}; };
tusb_init(BOARD_TUH_RHPORT, &host_init); tusb_init(BOARD_TUH_RHPORT, &host_init);
board_init_after_tusb(); board_init_after_tusb();
}
//--------------------------------------------------------------------+
// Main
//--------------------------------------------------------------------+
static void main_task(void* param) {
(void) param;
while (1) { while (1) {
tud_task(); // tinyusb device task
tuh_task(); // tinyusb host task
cdc_task(); cdc_task();
led_blinking_task(); led_blinking_task();
// preempted RTOS run device/host stack in its own task
#if CFG_TUSB_OS == OPT_OS_NONE || CFG_TUSB_OS == OPT_OS_PICO
tud_task(); // tinyusb device task
tuh_task(); // tinyusb host task
#endif
} }
}
int main(void) {
board_init();
#if CFG_TUSB_OS == OPT_OS_NONE || CFG_TUSB_OS == OPT_OS_PICO
printf("TinyUSB Host Information -> Device CDC Example\r\n");
usb_device_init();
usb_host_init();
main_task(NULL);
#elif CFG_TUSB_OS == OPT_OS_FREERTOS
freertos_init(); // create RTOS tasks for device, host stack and main_task()
#else
#error RTOS not supported
#endif
return 0; return 0;
} }
#if CFG_TUSB_OS != OPT_OS_NONE && CFG_TUSB_OS != OPT_OS_PICO
// USB Device Driver task for RTOS
static void usb_device_task(void *param) {
(void) param;
usb_device_init();
while (1) {
// put this thread to waiting state until there is new events
tud_task();
}
}
static void usb_host_task(void *param) {
(void) param;
usb_host_init();
while (1) {
// put this thread to waiting state until there is new events
tuh_task();
}
}
#endif
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// Device CDC // Device CDC
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
@ -156,7 +209,7 @@ void cdc_task(void) {
if (!tud_cdc_connected()) { if (!tud_cdc_connected()) {
// delay a bit otherwise we can outpace host's terminal. Linux will set LineState (DTR) then Line Coding. // delay a bit otherwise we can outpace host's terminal. Linux will set LineState (DTR) then Line Coding.
// If we send data before Linux's terminal set Line Coding, it can be ignored --> missing data with hardware test loop // If we send data before Linux's terminal set Line Coding, it can be ignored --> missing data with hardware test loop
board_delay(20); tusb_time_delay_ms_api(20);
return; return;
} }
@ -259,7 +312,6 @@ void led_blinking_task(void) {
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// String Descriptor Helper // String Descriptor Helper
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
static void _convert_utf16le_to_utf8(const uint16_t *utf16, size_t utf16_len, uint8_t *utf8, size_t utf8_len) { static void _convert_utf16le_to_utf8(const uint16_t *utf16, size_t utf16_len, uint8_t *utf8, size_t utf8_len) {
// TODO: Check for runover. // TODO: Check for runover.
(void)utf8_len; (void)utf8_len;
@ -310,3 +362,53 @@ static void print_utf16(uint16_t *temp_buf, size_t buf_len) {
cdc_printf("%s", (char*) temp_buf); cdc_printf("%s", (char*) temp_buf);
} }
//--------------------------------------------------------------------+
// FreeRTOS
//--------------------------------------------------------------------+
#if CFG_TUSB_OS == OPT_OS_FREERTOS
#ifdef ESP_PLATFORM
#define USBD_STACK_SIZE 4096
#define USBH_STACK_SIZE 4096
void app_main(void) {
main();
}
#else
// Increase stack size when debug log is enabled
#define USBD_STACK_SIZE (configMINIMAL_STACK_SIZE * (CFG_TUSB_DEBUG ? 4 : 2))
#define USBH_STACK_SIZE (configMINIMAL_STACK_SIZE * (CFG_TUSB_DEBUG ? 4 : 2))
#endif
#define MAIN_STACK_SIZE (configMINIMAL_STACK_SIZE*4)
// static task
#if configSUPPORT_STATIC_ALLOCATION
StackType_t main_stack[MAIN_STACK_SIZE];
StaticTask_t main_taskdef;
StackType_t usb_device_stack[USBD_STACK_SIZE];
StaticTask_t usb_device_taskdef;
StackType_t usb_host_stack[USBH_STACK_SIZE];
StaticTask_t usb_host_taskdef;
#endif
void freertos_init(void) {
#if configSUPPORT_STATIC_ALLOCATION
xTaskCreateStatic(usb_device_task, "usbd", USBD_STACK_SIZE, NULL, configMAX_PRIORITIES-1, usb_device_stack, &usb_device_taskdef);
xTaskCreateStatic(usb_host_task, "usbh", USBH_STACK_SIZE, NULL, configMAX_PRIORITIES-1, usb_host_stack, &usb_host_taskdef);
xTaskCreateStatic(main_task, "main", MAIN_STACK_SIZE, NULL, configMAX_PRIORITIES - 2, main_stack, &main_taskdef);
#else
xTaskCreate(usb_device_task, "usbd", USBD_STACK_SIZE, NULL, configMAX_PRIORITIES - 1, NULL);
xTaskCreate(usb_host_task, "usbh", USBH_STACK_SZE, NULL, configMAX_PRIORITIES - 1, NULL);
xTaskCreate(main_task, "main", MAIN_STACK_SZIE, NULL, configMAX_PRIORITIES - 2, NULL);
#endif
// only start scheduler for non-espressif mcu
#ifndef ESP_PLATFORM
vTaskStartScheduler();
#endif
}
#endif

View File

@ -14,7 +14,7 @@ set(compile_definitions
BOARD_TUH_RHPORT=${RHPORT_HOST} BOARD_TUH_RHPORT=${RHPORT_HOST}
BOARD_TUH_MAX_SPEED=${RHPORT_HOST_SPEED} BOARD_TUH_MAX_SPEED=${RHPORT_HOST_SPEED}
) )
cmake_print_variables(compile_definitions)
if (target STREQUAL esp32p4) if (target STREQUAL esp32p4)
# P4 change alignment to 64 (DCache line size) for possible DMA configuration # P4 change alignment to 64 (DCache line size) for possible DMA configuration
list(APPEND compile_definitions list(APPEND compile_definitions