Skip to content

Commit ac5ad03

Browse files
Some USB test code fixes (#775)
* Some USB test code fixes * Fix build warning * Fix tinyusb config issues The latest version of freertos adds the following definition to every tinyusb example CFG_TUSB_OS=${TINYUSB_OPT_OS} But due to cmake scoping rules and the inclusion of tinyusb in pico-sdk TINYUSB_OPT_OS is unset when the definition is added. Make sure it's defined to avoid lots of redefinition warnings. For freertos examples, add the tinyusb freertos config folder to the list of include directories. Different freertos examples might have different requirements. * Filter tinyusb examples Override the tinyusb cmake function family_add_subdirectory to ignore any examples that won't build * Make sure LINKERMAP_PY is defined This goes out of scope.
1 parent 74e724b commit ac5ad03

5 files changed

Lines changed: 86 additions & 9 deletions

File tree

usb/CMakeLists.txt

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,54 @@
1+
# family.cmake defines TINYUSB_OPT_OS and is included twice...
2+
# 1/ the first time by pico-sdk in src/rp2_common/tinyusb/CMakeLists.txt
3+
# 2/ before each tinyusb example via family_support.cmake
4+
# family.cmake has include_guard(GLOBAL) so the second include doesn't happen
5+
# But each example calls the cmake function family_configure_device_example
6+
# as the second include didn't happen TINYUSB_OPT_OS has gone out of scope
7+
# So make sure it's defined to avoid a redefinition with an invalid value
8+
# For freertos examples TINYUSB_OPT_OS is actually defined in FreeRTOSConfig.h
9+
set(TINYUSB_OPT_OS OPT_OS_PICO)
10+
11+
# Same issue as above - this variable goes out of scope
12+
set(LINKERMAP_PY ${PICO_TINYUSB_PATH}/tools/linkermap/linkermap.py)
13+
14+
# List of freertos examples
15+
set(tinyusb_freertos_examples
16+
audio_4_channel_mic_freertos
17+
audio_test_freertos
18+
cdc_msc_freertos
19+
hid_composite_freertos
20+
midi_test_freertos
21+
msc_file_explorer_freertos
22+
)
23+
24+
# This is used to filter the tinyusb examples
25+
# Ignore examples that don't build properly without extra code
26+
# Ignore freertos examples unless FREERTOS_KERNEL_PATH is defined
27+
function(family_add_subdirectory DIR)
28+
family_filter(SHOULD_ADD "${DIR}")
29+
if (SHOULD_ADD)
30+
if (NOT IS_DIRECTORY ${PICO_TINYUSB_PATH}/lib/fatfs)
31+
list(APPEND tinyusb_example_ignore_list
32+
msc_file_explorer msc_file_explorer_freertos # need fatfs repo
33+
)
34+
endif()
35+
if (NOT DEFINED FREERTOS_KERNEL_PATH)
36+
# skip freertos examples
37+
list(APPEND tinyusb_example_ignore_list
38+
${tinyusb_freertos_examples}
39+
)
40+
endif()
41+
list(FIND tinyusb_example_ignore_list ${DIR} skip_pos)
42+
if (skip_pos GREATER_EQUAL 0)
43+
set(SHOULD_ADD 0)
44+
endif()
45+
if (SHOULD_ADD)
46+
add_subdirectory(${DIR})
47+
endif()
48+
endif()
49+
endfunction()
50+
51+
152
if (TARGET tinyusb_device)
253
add_subdirectory(device)
354
else ()
@@ -13,3 +64,15 @@ if (TARGET tinyusb_pico_pio_usb)
1364
else ()
1465
message("Skipping TinyUSB dual examples, as TinyUSB hw/mcu/raspberry_pi/Pico-PIO-USB submodule unavailable")
1566
endif ()
67+
68+
# tinyusb freertos examples want to use FREERTOS_CONFIG_FILE_DIRECTORY to set the freertos config directory
69+
# we have other freertos examples and we want the config to be under the control of each example
70+
# We prefer to add the freertos config directory to the include directory list
71+
# Add the directory now for the tinyusb freertos examples
72+
foreach(tinyusb_freertos_example ${tinyusb_freertos_examples})
73+
if (TARGET ${tinyusb_freertos_example})
74+
target_include_directories(${tinyusb_freertos_example} PRIVATE
75+
${PICO_TINYUSB_PATH}/hw/bsp/rp2040/FreeRTOSConfig
76+
)
77+
endif()
78+
endforeach()

usb/device/dev_hid_composite/main.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,12 @@ int main(void)
5858
board_init();
5959

6060
// init device stack on configured roothub port
61-
tud_init(BOARD_TUD_RHPORT);
62-
63-
if (board_init_after_tusb) {
64-
board_init_after_tusb();
65-
}
61+
const tusb_rhport_init_t rh_init = {
62+
.role = TUSB_ROLE_DEVICE,
63+
.speed = TUD_OPT_HIGH_SPEED ? TUSB_SPEED_HIGH : TUSB_SPEED_FULL
64+
};
65+
TU_ASSERT(tud_rhport_init(BOARD_TUD_RHPORT, &rh_init));
66+
board_init_after_tusb();
6667

6768
while (1)
6869
{
@@ -198,6 +199,11 @@ static void send_hid_report(uint8_t report_id, uint32_t btn)
198199
}
199200
}
200201

202+
#if TUSB_VERSION_NUMBER > 1800
203+
// board_millis has been removed from tinyusb. Use tusb_time_millis_api instead
204+
#define board_millis tusb_time_millis_api
205+
#endif
206+
201207
// Every 10ms, we will sent 1 report for each HID profile (keyboard, mouse etc ..)
202208
// tud_hid_report_complete_cb() is used to send the next report after previous one is complete
203209
void hid_task(void)

usb/device/dev_multi_cdc/usb_descriptors.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include <tusb.h>
88
#include <bsp/board_api.h>
9+
#include <assert.h>
910

1011
#include "pico/usb_reset.h"
1112

usb/host/host_cdc_msc_hid/main.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,13 @@ int main(void) {
5050
printf("TinyUSB Host CDC MSC HID Example\r\n");
5151

5252
// init host stack on configured roothub port
53-
tuh_init(BOARD_TUH_RHPORT);
53+
const tusb_rhport_init_t rh_init = {
54+
.role = TUSB_ROLE_HOST,
55+
.speed = TUH_OPT_HIGH_SPEED ? TUSB_SPEED_HIGH : TUSB_SPEED_FULL,
56+
};
57+
TU_ASSERT(tuh_rhport_init(BOARD_TUH_RHPORT, &rh_init));
5458

55-
if (board_init_after_tusb) {
56-
board_init_after_tusb();
57-
}
59+
board_init_after_tusb();
5860

5961
#if CFG_TUH_ENABLED && CFG_TUH_MAX3421
6062
// FeatherWing MAX3421E use MAX3421E's GPIO0 for VBUS enable
@@ -86,6 +88,10 @@ void tuh_umount_cb(uint8_t dev_addr) {
8688
printf("A device with address %d is unmounted \r\n", dev_addr);
8789
}
8890

91+
#if TUSB_VERSION_NUMBER > 1800
92+
// board_millis has been removed from tinyusb. Use tusb_time_millis_api instead
93+
#define board_millis tusb_time_millis_api
94+
#endif
8995

9096
//--------------------------------------------------------------------+
9197
// Blinking Task

usb/host/host_cdc_msc_hid/msc_app.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "tusb.h"
2727

2828
#include <inttypes.h>
29+
#include <stdio.h>
2930

3031
//--------------------------------------------------------------------+
3132
// MACRO TYPEDEF CONSTANT ENUM DECLARATION

0 commit comments

Comments
 (0)