diff --git a/examples/device/hid_composite_freertos/CMakeLists.txt b/examples/device/hid_composite_freertos/CMakeLists.txt index ed734b9543..9dec196421 100644 --- a/examples/device/hid_composite_freertos/CMakeLists.txt +++ b/examples/device/hid_composite_freertos/CMakeLists.txt @@ -1,17 +1,77 @@ cmake_minimum_required(VERSION 3.5) -# use BOARD-Directory name for project id -get_filename_component(PROJECT ${CMAKE_CURRENT_SOURCE_DIR} NAME) -set(PROJECT ${BOARD}-${PROJECT}) - # TOP is absolute path to root directory of TinyUSB git repo set(TOP "../../..") get_filename_component(TOP "${TOP}" REALPATH) # Check for -DFAMILY= if(FAMILY MATCHES "^esp32s[2-3]") + # use BOARD-Directory name for project id + get_filename_component(PROJECT ${CMAKE_CURRENT_SOURCE_DIR} NAME) + set(PROJECT ${BOARD}-${PROJECT}) include(${TOP}/hw/bsp/${FAMILY}/family.cmake) project(${PROJECT}) +elseif(FAMILY MATCHES "rp2040") + # gets PROJECT name for the example (e.g. -) + family_get_project_name(PROJECT ${CMAKE_CURRENT_LIST_DIR}) + if (DEFINED ENV{FREERTOS_KERNEL_PATH}) + # Pull in FreeRTOS + include(FreeRTOS_Kernel_import.cmake) + project(${PROJECT}) + include(${TOP}/hw/bsp/${FAMILY}/family.cmake) + # Checks this example is valid for the family and initializes the project + family_initialize_project(${PROJECT} ${CMAKE_CURRENT_LIST_DIR}) + + add_executable(${PROJECT}) + # Example source + target_sources(${PROJECT} PUBLIC + ${CMAKE_CURRENT_SOURCE_DIR}/src/main.c + ${CMAKE_CURRENT_SOURCE_DIR}/src/usb_descriptors.c + ${CMAKE_CURRENT_SOURCE_DIR}/src/freertos_hook.c + ) + + # Example include + target_include_directories(${PROJECT} PUBLIC + ${CMAKE_CURRENT_SOURCE_DIR}/src + ${CMAKE_CURRENT_SOURCE_DIR}/src/FreeRTOSConfig + ) + if (PICO_C_COMPILER_IS_GNU) + # TODO: watch for update to the FreeRTOS-Kernel code + # There are a disturbing number of warnings in the FreeRTOS-Kernel smp branch + # The need for -Wno-shadow -Wno-cast-qual -Wno-cast-align indicate likely bugs + set_source_files_properties( + ${FREERTOS_KERNEL_PATH}/portable/ThirdParty/GCC/RP2040/port.c + ${FREERTOS_KERNEL_PATH}/portable/MemMang/heap_4.c + ${FREERTOS_KERNEL_PATH}/event_groups.c + ${FREERTOS_KERNEL_PATH}/croutine.c + ${FREERTOS_KERNEL_PATH}/timers.c + ${FREERTOS_KERNEL_PATH}/queue.c + ${FREERTOS_KERNEL_PATH}/tasks.c + ${FREERTOS_KERNEL_PATH}/list.c + ${FREERTOS_KERNEL_PATH}/stream_buffer.c + PROPERTIES + COMPILE_FLAGS "-Wno-type-limits -Wno-conversion -Wno-sign-compare -Wno-shadow -Wno-unused-parameter -Wno-cast-qual -Wno-cast-align") + # Small issues in vPortRecursiveLock() in portmacro.h cause the following warnings in our added code + set_source_files_properties( + ${PICO_TINYUSB_PATH}/hw/bsp/rp2040/family.c + ${PICO_TINYUSB_PATH}/src/tusb.c + ${PICO_TINYUSB_PATH}/src/portable/raspberrypi/rp2040/dcd_rp2040.c + ${PICO_TINYUSB_PATH}/src/class/hid/hid_device.c + ${PICO_TINYUSB_PATH}/src/common/tusb_fifo.c + ${PICO_TINYUSB_PATH}/src/device/usbd.c + ${PICO_TINYUSB_PATH}/src/device/usbd_control.c + ${CMAKE_CURRENT_SOURCE_DIR}/src/main.c + ${CMAKE_CURRENT_SOURCE_DIR}/src/usb_descriptors.c + ${CMAKE_CURRENT_SOURCE_DIR}/src/freertos_hook.c + PROPERTIES + COMPILE_FLAGS "-Wno-type-limits -Wno-conversion") + endif() + # Configure compilation flags and libraries for the example... see the corresponding function + # in hw/bsp/FAMILY/family.cmake for details. + family_configure_device_freertos_example(${PROJECT}) + else() + message(STATUS "skipping ${PROJECT}: FREERTOS_KERNEL_PATH is not defined") + endif() else() message(FATAL_ERROR "Invalid FAMILY specified: ${FAMILY}") endif() diff --git a/examples/device/hid_composite_freertos/FreeRTOS_Kernel_import.cmake b/examples/device/hid_composite_freertos/FreeRTOS_Kernel_import.cmake new file mode 100644 index 0000000000..dc68ed0387 --- /dev/null +++ b/examples/device/hid_composite_freertos/FreeRTOS_Kernel_import.cmake @@ -0,0 +1,62 @@ +# This is a copy of /portable/ThirdParty/GCC/RP2040/FREERTOS_KERNEL_import.cmake + +# This can be dropped into an external project to help locate the FreeRTOS kernel +# It should be include()ed prior to project(). Alternatively this file may +# or the CMakeLists.txt in this directory may be included or added via add_subdirectory +# respectively. + +if (DEFINED ENV{FREERTOS_KERNEL_PATH} AND (NOT FREERTOS_KERNEL_PATH)) + set(FREERTOS_KERNEL_PATH $ENV{FREERTOS_KERNEL_PATH}) + message("Using FREERTOS_KERNEL_PATH from environment ('${FREERTOS_KERNEL_PATH}')") +endif () + +set(FREERTOS_KERNEL_RP2040_RELATIVE_PATH "portable/ThirdParty/GCC/RP2040") +# undo the above +set(FREERTOS_KERNEL_RP2040_BACK_PATH "../../../..") + +if (NOT FREERTOS_KERNEL_PATH) + # check if we are inside the FreeRTOS kernel tree (i.e. this file has been included directly) + get_filename_component(_ACTUAL_PATH ${CMAKE_CURRENT_LIST_DIR} REALPATH) + get_filename_component(_POSSIBLE_PATH ${CMAKE_CURRENT_LIST_DIR}/${FREERTOS_KERNEL_RP2040_BACK_PATH}/${FREERTOS_KERNEL_RP2040_RELATIVE_PATH} REALPATH) + if (_ACTUAL_PATH STREQUAL _POSSIBLE_PATH) + get_filename_component(FREERTOS_KERNEL_PATH ${CMAKE_CURRENT_LIST_DIR}/${FREERTOS_KERNEL_RP2040_BACK_PATH} REALPATH) + endif() + if (_ACTUAL_PATH STREQUAL _POSSIBLE_PATH) + get_filename_component(FREERTOS_KERNEL_PATH ${CMAKE_CURRENT_LIST_DIR}/${FREERTOS_KERNEL_RP2040_BACK_PATH} REALPATH) + message("Setting FREERTOS_KERNEL_PATH to ${FREERTOS_KERNEL_PATH} based on location of FreeRTOS-Kernel-import.cmake") + elseif (PICO_SDK_PATH AND EXISTS "${PICO_SDK_PATH}/../FreeRTOS-Kernel") + set(FREERTOS_KERNEL_PATH ${PICO_SDK_PATH}/../FreeRTOS-Kernel) + message("Defaulting FREERTOS_KERNEL_PATH as sibling of PICO_SDK_PATH: ${FREERTOS_KERNEL_PATH}") + endif() +endif () + +if (NOT FREERTOS_KERNEL_PATH) + foreach(POSSIBLE_SUFFIX Source FreeRTOS-Kernel FreeRTOS/Source) + # check if FreeRTOS-Kernel exists under directory that included us + set(SEARCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}}) + set(SEARCH_ROOT ../../../..) + get_filename_component(_POSSIBLE_PATH ${SEARCH_ROOT}/${POSSIBLE_SUFFIX} REALPATH) + if (EXISTS ${_POSSIBLE_PATH}/${FREERTOS_KERNEL_RP2040_RELATIVE_PATH}/CMakeLists.txt) + get_filename_component(FREERTOS_KERNEL_PATH ${_POSSIBLE_PATH} REALPATH) + message("Setting FREERTOS_KERNEL_PATH to '${FREERTOS_KERNEL_PATH}' found relative to enclosing project") + break() + endif() + endforeach() +endif() + +if (NOT FREERTOS_KERNEL_PATH) + message(FATAL_ERROR "FreeRTOS location was not specified. Please set FREERTOS_KERNEL_PATH.") +endif() + +set(FREERTOS_KERNEL_PATH "${FREERTOS_KERNEL_PATH}" CACHE PATH "Path to the FreeRTOS Kernel") + +get_filename_component(FREERTOS_KERNEL_PATH "${FREERTOS_KERNEL_PATH}" REALPATH BASE_DIR "${CMAKE_BINARY_DIR}") +if (NOT EXISTS ${FREERTOS_KERNEL_PATH}) + message(FATAL_ERROR "Directory '${FREERTOS_KERNEL_PATH}' not found") +endif() +if (NOT EXISTS ${FREERTOS_KERNEL_PATH}/${FREERTOS_KERNEL_RP2040_RELATIVE_PATH}/CMakeLists.txt) + message(FATAL_ERROR "Directory '${FREERTOS_KERNEL_PATH}' does not contain an RP2040 port here: ${FREERTOS_KERNEL_RP2040_RELATIVE_PATH}") +endif() +set(FREERTOS_KERNEL_PATH ${FREERTOS_KERNEL_PATH} CACHE PATH "Path to the FreeRTOS_KERNEL" FORCE) + +add_subdirectory(${FREERTOS_KERNEL_PATH}/${FREERTOS_KERNEL_RP2040_RELATIVE_PATH} FREERTOS_KERNEL) \ No newline at end of file diff --git a/examples/device/hid_composite_freertos/skip.txt b/examples/device/hid_composite_freertos/skip.txt index 49b8ee57b8..97ed7ac788 100644 --- a/examples/device/hid_composite_freertos/skip.txt +++ b/examples/device/hid_composite_freertos/skip.txt @@ -4,7 +4,6 @@ mcu:F1C100S mcu:GD32VF103 mcu:MKL25ZXX mcu:MSP430x5xx -mcu:RP2040 mcu:SAMD11 mcu:SAMX7X mcu:VALENTYUSB_EPTRI diff --git a/examples/device/hid_composite_freertos/src/FreeRTOSConfig/FreeRTOSConfig.h b/examples/device/hid_composite_freertos/src/FreeRTOSConfig/FreeRTOSConfig.h index 9bef9bbbf6..70924bca9a 100644 --- a/examples/device/hid_composite_freertos/src/FreeRTOSConfig/FreeRTOSConfig.h +++ b/examples/device/hid_composite_freertos/src/FreeRTOSConfig/FreeRTOSConfig.h @@ -62,6 +62,103 @@ #endif +#if CFG_TUSB_MCU == OPT_MCU_RP2040 + +/* Scheduler Related */ +#define configUSE_PREEMPTION 1 +#define configUSE_TICKLESS_IDLE 0 +#define configUSE_IDLE_HOOK 0 +#define configUSE_TICK_HOOK 1 +#define configTICK_RATE_HZ ( ( TickType_t ) 1000 ) +#define configMAX_PRIORITIES 32 +#define configMINIMAL_STACK_SIZE ( configSTACK_DEPTH_TYPE ) 256 +#define configUSE_16_BIT_TICKS 0 + +#define configIDLE_SHOULD_YIELD 1 + +/* Synchronization Related */ +#define configUSE_MUTEXES 1 +#define configUSE_RECURSIVE_MUTEXES 1 +#define configUSE_APPLICATION_TASK_TAG 0 +#define configUSE_COUNTING_SEMAPHORES 1 +#define configQUEUE_REGISTRY_SIZE 8 +#define configUSE_QUEUE_SETS 1 +#define configUSE_TIME_SLICING 1 +#define configUSE_NEWLIB_REENTRANT 0 +#define configENABLE_BACKWARD_COMPATIBILITY 0 +#define configNUM_THREAD_LOCAL_STORAGE_POINTERS 5 + +/* System */ +#define configSTACK_DEPTH_TYPE uint32_t +#define configMESSAGE_BUFFER_LENGTH_TYPE size_t + +/* Memory allocation related definitions. */ +#define configSUPPORT_STATIC_ALLOCATION 0 +#define configSUPPORT_DYNAMIC_ALLOCATION 1 +#define configTOTAL_HEAP_SIZE (128*1024) +#define configAPPLICATION_ALLOCATED_HEAP 0 + +/* Hook function related definitions. */ +#define configCHECK_FOR_STACK_OVERFLOW 2 +#define configUSE_MALLOC_FAILED_HOOK 1 +#define configUSE_DAEMON_TASK_STARTUP_HOOK 0 + +/* Run time and task stats gathering related definitions. */ +#define configGENERATE_RUN_TIME_STATS 0 +#define configUSE_TRACE_FACILITY 1 +#define configUSE_STATS_FORMATTING_FUNCTIONS 0 + +/* Co-routine related definitions. */ +#define configUSE_CO_ROUTINES 0 +#define configMAX_CO_ROUTINE_PRIORITIES 1 + +/* Software timer related definitions. */ +#define configUSE_TIMERS 1 +#define configTIMER_TASK_PRIORITY ( configMAX_PRIORITIES - 1 ) +#define configTIMER_QUEUE_LENGTH 10 +#define configTIMER_TASK_STACK_DEPTH 1024 + +/* Interrupt nesting behaviour configuration. */ +/* +#define configKERNEL_INTERRUPT_PRIORITY [dependent of processor] +#define configMAX_SYSCALL_INTERRUPT_PRIORITY [dependent on processor and application] +#define configMAX_API_CALL_INTERRUPT_PRIORITY [dependent on processor and application] +*/ + +/* SMP port only */ +#define configNUM_CORES 2 +#define configTICK_CORE 0 +#define configRUN_MULTIPLE_PRIORITIES 0 + +/* RP2040 specific */ +#define configSUPPORT_PICO_SYNC_INTEROP 1 +#define configSUPPORT_PICO_TIME_INTEROP 1 + +#include +/* Define to trap errors during development. */ +#define configASSERT(x) assert(x) + +/* Set the following definitions to 1 to include the API function, or zero +to exclude the API function. */ +#define INCLUDE_vTaskPrioritySet 1 +#define INCLUDE_uxTaskPriorityGet 1 +#define INCLUDE_vTaskDelete 1 +#define INCLUDE_vTaskSuspend 1 +#define INCLUDE_vTaskDelayUntil 1 +#define INCLUDE_vTaskDelay 1 +#define INCLUDE_xTaskGetSchedulerState 1 +#define INCLUDE_xTaskGetCurrentTaskHandle 1 +#define INCLUDE_uxTaskGetStackHighWaterMark 1 +#define INCLUDE_xTaskGetIdleTaskHandle 1 +#define INCLUDE_eTaskGetState 1 +#define INCLUDE_xTimerPendFunctionCall 1 +#define INCLUDE_xTaskAbortDelay 1 +#define INCLUDE_xTaskGetHandle 1 +#define INCLUDE_xTaskResumeFromISR 1 +#define INCLUDE_xQueueGetMutexHolder 1 + +#else + /* Cortex M23/M33 port configuration. */ #define configENABLE_MPU 0 #define configENABLE_FPU 1 @@ -202,5 +299,5 @@ See http://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html. */ #define configMAX_SYSCALL_INTERRUPT_PRIORITY ( configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY << (8 - configPRIO_BITS) ) #endif - +#endif #endif /* __FREERTOS_CONFIG__H */ diff --git a/examples/device/hid_composite_freertos/src/freertos_hook.c b/examples/device/hid_composite_freertos/src/freertos_hook.c index ab885947c6..0a806d9d28 100644 --- a/examples/device/hid_composite_freertos/src/freertos_hook.c +++ b/examples/device/hid_composite_freertos/src/freertos_hook.c @@ -37,7 +37,7 @@ void vApplicationMallocFailedHook(void) TU_ASSERT(false, ); } -void vApplicationStackOverflowHook(xTaskHandle pxTask, char *pcTaskName) +void vApplicationStackOverflowHook(TaskHandle_t pxTask, char *pcTaskName) { (void) pxTask; (void) pcTaskName; @@ -46,6 +46,11 @@ void vApplicationStackOverflowHook(xTaskHandle pxTask, char *pcTaskName) TU_ASSERT(false, ); } +void vApplicationTickHook( void ) +{ +} + +#if configSUPPORT_STATIC_ALLOCATION /* configSUPPORT_STATIC_ALLOCATION is set to 1, so the application must provide an * implementation of vApplicationGetIdleTaskMemory() to provide the memory that is * used by the Idle task. */ @@ -93,7 +98,7 @@ void vApplicationGetTimerTaskMemory( StaticTask_t **ppxTimerTaskTCBBuffer, Stack configTIMER_TASK_STACK_DEPTH is specified in words, not bytes. */ *pulTimerTaskStackSize = configTIMER_TASK_STACK_DEPTH; } - +#endif #if CFG_TUSB_MCU == OPT_MCU_RX63X | CFG_TUSB_MCU == OPT_MCU_RX65X #include "iodefine.h" void vApplicationSetupTimerInterrupt(void) diff --git a/examples/device/hid_composite_freertos/src/main.c b/examples/device/hid_composite_freertos/src/main.c index ca02af1005..201f4f1d05 100644 --- a/examples/device/hid_composite_freertos/src/main.c +++ b/examples/device/hid_composite_freertos/src/main.c @@ -41,7 +41,6 @@ #include "freertos/timers.h" #define USBD_STACK_SIZE 4096 - #else #include "FreeRTOS.h" #include "semphr.h" diff --git a/hw/bsp/board.h b/hw/bsp/board.h index 0365567316..8441dec7a0 100644 --- a/hw/bsp/board.h +++ b/hw/bsp/board.h @@ -80,7 +80,7 @@ int board_uart_write(void const * buf, int len); #elif CFG_TUSB_OS == OPT_OS_FREERTOS static inline uint32_t board_millis(void) { - return ( ( ((uint64_t) xTaskGetTickCount()) * 1000) / configTICK_RATE_HZ ); + return (uint32_t)( ( ((uint64_t) xTaskGetTickCount()) * 1000) / configTICK_RATE_HZ ); } #elif CFG_TUSB_OS == OPT_OS_MYNEWT diff --git a/hw/bsp/rp2040/family.cmake b/hw/bsp/rp2040/family.cmake index 5d80ab66e2..063060dfae 100644 --- a/hw/bsp/rp2040/family.cmake +++ b/hw/bsp/rp2040/family.cmake @@ -23,133 +23,218 @@ if (NOT TARGET _rp2040_family_inclusion_marker) set(PICO_TINYUSB_PATH ${TOP}) endif() - if (NOT TINYUSB_OPT_OS) - set(TINYUSB_OPT_OS OPT_OS_PICO) + unset(SUPPORTED_OS_LIST) + if (DEFINED TINYUSB_OPT_OS) + # for backward compatibility, let the user force the OS option + list(APPEND SUPPORTED_OS_LIST ${TINYUSB_OPT_OS}) + else() + # otherwise, build both the pico-sdk and FreeRTOS versions of the library + list(APPEND SUPPORTED_OS_LIST OPT_OS_PICO OPT_OS_FREERTOS) endif() + set(PROJ_SUFFIX "") + foreach(SUPPORTED_OS IN LISTS SUPPORTED_OS_LIST) + if (${SUPPORTED_OS} MATCHES OPT_OS_FREERTOS) + # see if FreeRTOS kernel source is available; based on /portable/ThirdParty/GCC/RP2040/FreeRTOS_Kernel_import.cmake + if (DEFINED ENV{FREERTOS_KERNEL_PATH} AND (NOT FREERTOS_KERNEL_PATH)) + set(FREERTOS_KERNEL_PATH $ENV{FREERTOS_KERNEL_PATH}) + message("Using FREERTOS_KERNEL_PATH from environment ('${FREERTOS_KERNEL_PATH}')") + endif () + + set(FREERTOS_KERNEL_RP2040_RELATIVE_PATH "portable/ThirdParty/GCC/RP2040") + # undo the above + set(FREERTOS_KERNEL_RP2040_BACK_PATH "../../../..") + + if (NOT FREERTOS_KERNEL_PATH) + # check if we are inside the FreeRTOS kernel tree (i.e. this file has been included directly) + get_filename_component(_ACTUAL_PATH ${CMAKE_CURRENT_LIST_DIR} REALPATH) + get_filename_component(_POSSIBLE_PATH ${CMAKE_CURRENT_LIST_DIR}/${FREERTOS_KERNEL_RP2040_BACK_PATH}/${FREERTOS_KERNEL_RP2040_RELATIVE_PATH} REALPATH) + if (_ACTUAL_PATH STREQUAL _POSSIBLE_PATH) + get_filename_component(FREERTOS_KERNEL_PATH ${CMAKE_CURRENT_LIST_DIR}/${FREERTOS_KERNEL_RP2040_BACK_PATH} REALPATH) + endif() + if (_ACTUAL_PATH STREQUAL _POSSIBLE_PATH) + get_filename_component(FREERTOS_KERNEL_PATH ${CMAKE_CURRENT_LIST_DIR}/${FREERTOS_KERNEL_RP2040_BACK_PATH} REALPATH) + message("Setting FREERTOS_KERNEL_PATH to ${FREERTOS_KERNEL_PATH} based on location of family.cmake") + elseif (PICO_SDK_PATH AND EXISTS "${PICO_SDK_PATH}/../FreeRTOS-Kernel") + set(FREERTOS_KERNEL_PATH ${PICO_SDK_PATH}/../FreeRTOS-Kernel) + message("Defaulting FREERTOS_KERNEL_PATH as sibling of PICO_SDK_PATH: ${FREERTOS_KERNEL_PATH}") + endif() + endif () + + if (NOT FREERTOS_KERNEL_PATH) + foreach(POSSIBLE_SUFFIX Source FreeRTOS-Kernel FreeRTOS/Source) + # check if FreeRTOS-Kernel exists under directory that included us + set(SEARCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}) + set(SEARCH_ROOT ../../../..) + get_filename_component(_POSSIBLE_PATH ${SEARCH_ROOT}/${POSSIBLE_SUFFIX} REALPATH) + if (EXISTS ${_POSSIBLE_PATH}/${FREERTOS_KERNEL_RP2040_RELATIVE_PATH}/CMakeLists.txt) + get_filename_component(FREERTOS_KERNEL_PATH ${_POSSIBLE_PATH} REALPATH) + message("Setting FREERTOS_KERNEL_PATH to '${FREERTOS_KERNEL_PATH}' found relative to enclosing project") + break() + endif() + endforeach() + endif() + if (NOT FREERTOS_KERNEL_PATH) + message(STATUS "FREERTOS_KERNEL_PATH not defined. Skipping tinyusb OPT_OS_FREERTOS") + continue() + else() + set(PROJ_SUFFIX "_freertos") + if (NOT DEFINED ENV{FREERTOS_KERNEL_PATH}) + set(ENV{FREERTOS_KERNEL_PATH} ${FREERTOS_KERNEL_PATH}) + endif() + endif() + endif() + set(TINYUSB_OPT_OS ${SUPPORTED_OS}) + message(STATUS "Configuring for TINYUSB_OPT_OS=${TINYUSB_OPT_OS}") + #------------------------------------ + # Base config for both device and host; wrapped by SDK's tinyusb_common + #------------------------------------ + set(TUSB_COMMON_BASE tinyusb_common_base${PROJ_SUFFIX}) + add_library(${TUSB_COMMON_BASE} INTERFACE) + + target_sources(${TUSB_COMMON_BASE} INTERFACE + ${TOP}/src/tusb.c + ${TOP}/src/common/tusb_fifo.c + ) + + target_include_directories(${TUSB_COMMON_BASE} INTERFACE + ${TOP}/src + ${TOP}/src/common + ${TOP}/hw + ) + + target_link_libraries(${TUSB_COMMON_BASE} INTERFACE + hardware_structs + hardware_irq + hardware_resets + pico_sync + ) + + set(TINYUSB_DEBUG_LEVEL 0) + if (CMAKE_BUILD_TYPE STREQUAL "Debug") + message("Compiling TinyUSB with CFG_TUSB_DEBUG=1") + set(TINYUSB_DEBUG_LEVEL 1) + endif() - #------------------------------------ - # Base config for both device and host; wrapped by SDK's tinyusb_common - #------------------------------------ - add_library(tinyusb_common_base INTERFACE) - - target_sources(tinyusb_common_base INTERFACE - ${TOP}/src/tusb.c - ${TOP}/src/common/tusb_fifo.c - ) - - target_include_directories(tinyusb_common_base INTERFACE - ${TOP}/src - ${TOP}/src/common - ${TOP}/hw - ) + target_compile_definitions(${TUSB_COMMON_BASE} INTERFACE + CFG_TUSB_MCU=OPT_MCU_RP2040 + CFG_TUSB_OS=${TINYUSB_OPT_OS} + #CFG_TUSB_DEBUG=${TINYUSB_DEBUG_LEVEL} + ) - target_link_libraries(tinyusb_common_base INTERFACE - hardware_structs - hardware_irq - hardware_resets - pico_sync - ) + #------------------------------------ + # Base config for device mode; wrapped by SDK's tinyusb_device + #------------------------------------ + set(TUSB_DEV_BASE tinyusb_device_base${PROJ_SUFFIX}) + add_library(${TUSB_DEV_BASE} INTERFACE) + target_sources(${TUSB_DEV_BASE} INTERFACE + ${TOP}/src/portable/raspberrypi/rp2040/dcd_rp2040.c + ${TOP}/src/portable/raspberrypi/rp2040/rp2040_usb.c + ${TOP}/src/device/usbd.c + ${TOP}/src/device/usbd_control.c + ${TOP}/src/class/audio/audio_device.c + ${TOP}/src/class/cdc/cdc_device.c + ${TOP}/src/class/dfu/dfu_device.c + ${TOP}/src/class/dfu/dfu_rt_device.c + ${TOP}/src/class/hid/hid_device.c + ${TOP}/src/class/midi/midi_device.c + ${TOP}/src/class/msc/msc_device.c + ${TOP}/src/class/net/ecm_rndis_device.c + ${TOP}/src/class/net/ncm_device.c + ${TOP}/src/class/usbtmc/usbtmc_device.c + ${TOP}/src/class/vendor/vendor_device.c + ${TOP}/src/class/video/video_device.c + ) + + #------------------------------------ + # Base config for host mode; wrapped by SDK's tinyusb_host + #------------------------------------ + set(TUSB_HOST_BASE tinyusb_host_base${PROJ_SUFFIX}) + add_library(${TUSB_HOST_BASE} INTERFACE) + target_sources(${TUSB_HOST_BASE} INTERFACE + ${TOP}/src/portable/raspberrypi/rp2040/hcd_rp2040.c + ${TOP}/src/portable/raspberrypi/rp2040/rp2040_usb.c + ${TOP}/src/host/usbh.c + ${TOP}/src/host/hub.c + ${TOP}/src/class/cdc/cdc_host.c + ${TOP}/src/class/hid/hid_host.c + ${TOP}/src/class/msc/msc_host.c + ${TOP}/src/class/vendor/vendor_host.c + ) + + # Sometimes have to do host specific actions in mostly common functions + target_compile_definitions(${TUSB_HOST_BASE} INTERFACE + RP2040_USB_HOST_MODE=1 + ) - set(TINYUSB_DEBUG_LEVEL 0) - if (CMAKE_BUILD_TYPE STREQUAL "Debug") - message("Compiling TinyUSB with CFG_TUSB_DEBUG=1") - set(TINYUSB_DEBUG_LEVEL 1) - endif() + #------------------------------------ + # BSP & Additions + #------------------------------------ + add_library(tinyusb_bsp${PROJ_SUFFIX} INTERFACE) + target_sources(tinyusb_bsp${PROJ_SUFFIX} INTERFACE + ${TOP}/hw/bsp/rp2040/family.c + ) + # target_include_directories(tinyusb_bsp INTERFACE + # ${TOP}/hw/bsp/rp2040) + + # tinyusb_additions will hold our extra settings for examples + add_library(tinyusb_additions${PROJ_SUFFIX} INTERFACE) + + target_compile_definitions(tinyusb_additions${PROJ_SUFFIX} INTERFACE + PICO_RP2040_USB_DEVICE_ENUMERATION_FIX=1 + PICO_RP2040_USB_DEVICE_UFRAME_FIX=1 + ) - target_compile_definitions(tinyusb_common_base INTERFACE - CFG_TUSB_MCU=OPT_MCU_RP2040 - CFG_TUSB_OS=${TINYUSB_OPT_OS} - #CFG_TUSB_DEBUG=${TINYUSB_DEBUG_LEVEL} - ) + if(DEFINED LOG) + target_compile_definitions(tinyusb_additions${PROJ_SUFFIX} INTERFACE CFG_TUSB_DEBUG=${LOG}) + endif() - #------------------------------------ - # Base config for device mode; wrapped by SDK's tinyusb_device - #------------------------------------ - add_library(tinyusb_device_base INTERFACE) - target_sources(tinyusb_device_base INTERFACE - ${TOP}/src/portable/raspberrypi/rp2040/dcd_rp2040.c - ${TOP}/src/portable/raspberrypi/rp2040/rp2040_usb.c - ${TOP}/src/device/usbd.c - ${TOP}/src/device/usbd_control.c - ${TOP}/src/class/audio/audio_device.c - ${TOP}/src/class/cdc/cdc_device.c - ${TOP}/src/class/dfu/dfu_device.c - ${TOP}/src/class/dfu/dfu_rt_device.c - ${TOP}/src/class/hid/hid_device.c - ${TOP}/src/class/midi/midi_device.c - ${TOP}/src/class/msc/msc_device.c - ${TOP}/src/class/net/ecm_rndis_device.c - ${TOP}/src/class/net/ncm_device.c - ${TOP}/src/class/usbtmc/usbtmc_device.c - ${TOP}/src/class/vendor/vendor_device.c - ${TOP}/src/class/video/video_device.c + if(LOGGER STREQUAL "rtt") + target_compile_definitions(tinyusb_additions${PROJ_SUFFIX} INTERFACE + LOGGER_RTT + SEGGER_RTT_MODE_DEFAULT=SEGGER_RTT_MODE_BLOCK_IF_FIFO_FULL ) - #------------------------------------ - # Base config for host mode; wrapped by SDK's tinyusb_host - #------------------------------------ - add_library(tinyusb_host_base INTERFACE) - target_sources(tinyusb_host_base INTERFACE - ${TOP}/src/portable/raspberrypi/rp2040/hcd_rp2040.c - ${TOP}/src/portable/raspberrypi/rp2040/rp2040_usb.c - ${TOP}/src/host/usbh.c - ${TOP}/src/host/hub.c - ${TOP}/src/class/cdc/cdc_host.c - ${TOP}/src/class/hid/hid_host.c - ${TOP}/src/class/msc/msc_host.c - ${TOP}/src/class/vendor/vendor_host.c + target_sources(tinyusb_additions${PROJ_SUFFIX} INTERFACE + ${TOP}/lib/SEGGER_RTT/RTT/SEGGER_RTT.c ) - # Sometimes have to do host specific actions in mostly common functions - target_compile_definitions(tinyusb_host_base INTERFACE - RP2040_USB_HOST_MODE=1 - ) - - #------------------------------------ - # BSP & Additions - #------------------------------------ - add_library(tinyusb_bsp INTERFACE) - target_sources(tinyusb_bsp INTERFACE - ${TOP}/hw/bsp/rp2040/family.c + target_include_directories(tinyusb_additions${PROJ_SUFFIX} INTERFACE + ${TOP}/lib/SEGGER_RTT/RTT ) - # target_include_directories(tinyusb_bsp INTERFACE - # ${TOP}/hw/bsp/rp2040) + endif() + endforeach() - # tinyusb_additions will hold our extra settings for examples - add_library(tinyusb_additions INTERFACE) + # TODO: For FreeRTOS support, add wrapper libraries. This should happen + # in ${PICO_SDK_PATH}/src/rp2common/tinyusb/CMakeLists.txt + # but it is done here for testing purposes + if (DEFINED FREERTOS_KERNEL_PATH) + add_library(tinyusb_common_freertos INTERFACE) + target_link_libraries(tinyusb_common_freertos INTERFACE tinyusb_common_base_freertos) - target_compile_definitions(tinyusb_additions INTERFACE - PICO_RP2040_USB_DEVICE_ENUMERATION_FIX=1 - PICO_RP2040_USB_DEVICE_UFRAME_FIX=1 - ) + pico_add_library(tinyusb_device_freertos) + target_link_libraries(tinyusb_device_freertos INTERFACE tinyusb_common_freertos pico_fix_rp2040_usb_device_enumeration tinyusb_device_base_freertos) - if(DEFINED LOG) - target_compile_definitions(tinyusb_additions INTERFACE CFG_TUSB_DEBUG=${LOG}) - endif() + pico_add_library(tinyusb_host_freertos) + target_link_libraries(tinyusb_host_freertos INTERFACE tinyusb_host_base_freertos tinyusb_common_freertos) - if(LOGGER STREQUAL "rtt") - target_compile_definitions(tinyusb_additions INTERFACE - LOGGER_RTT - SEGGER_RTT_MODE_DEFAULT=SEGGER_RTT_MODE_BLOCK_IF_FIFO_FULL - ) - - target_sources(tinyusb_additions INTERFACE - ${TOP}/lib/SEGGER_RTT/RTT/SEGGER_RTT.c - ) - - target_include_directories(tinyusb_additions INTERFACE - ${TOP}/lib/SEGGER_RTT/RTT - ) + pico_add_library(tinyusb_board_freertos) + target_link_libraries(tinyusb_board_freertos INTERFACE tinyusb_bsp_freertos) + else() + message(STATUS "FREERTOS_KERNEL_PATH not defined") endif() #------------------------------------ # Functions #------------------------------------ - function(family_configure_target TARGET) + function(family_configure_target TARGET PR_SUFFIX) pico_add_extra_outputs(${TARGET}) pico_enable_stdio_uart(${TARGET} 1) - target_link_libraries(${TARGET} PUBLIC pico_stdlib pico_bootsel_via_double_reset tinyusb_board tinyusb_additions) + target_link_libraries(${TARGET} PUBLIC pico_stdlib pico_bootsel_via_double_reset tinyusb_board${PR_SUFFIX} tinyusb_additions${PR_SUFFIX}) + if (${PR_SUFFIX} MATCHES "_freertos") + message(STATUS "${TARGET}: Adding FreeRTOS-Kernel and FreeRTOS-Kernel-Heap4 libraries") + target_link_libraries(${TARGET} PUBLIC FreeRTOS-Kernel FreeRTOS-Kernel-Heap4) + endif() endfunction() function(rp2040_family_configure_example_warnings TARGET) @@ -163,17 +248,24 @@ if (NOT TARGET _rp2040_family_inclusion_marker) endfunction() function(family_configure_device_example TARGET) - family_configure_target(${TARGET}) + family_configure_target(${TARGET} "") target_link_libraries(${TARGET} PUBLIC pico_stdlib tinyusb_device) rp2040_family_configure_example_warnings(${TARGET}) endfunction() + function(family_configure_device_freertos_example TARGET) + message(STATUS "configuring freertos example ${TARGET}") + family_configure_target(${TARGET} "_freertos") + target_link_libraries(${TARGET} PUBLIC pico_stdlib tinyusb_device_freertos) + rp2040_family_configure_example_warnings(${TARGET}) + endfunction() + function(family_add_pico_pio_usb TARGET) target_link_libraries(${TARGET} PUBLIC tinyusb_pico_pio_usb) endfunction() function(family_configure_host_example TARGET) - family_configure_target(${TARGET}) + family_configure_target(${TARGET} "") target_link_libraries(${TARGET} PUBLIC pico_stdlib tinyusb_host) rp2040_family_configure_example_warnings(${TARGET}) @@ -187,7 +279,7 @@ if (NOT TARGET _rp2040_family_inclusion_marker) endfunction() function(family_configure_dual_usb_example TARGET) - family_configure_target(${TARGET}) + family_configure_target(${TARGET} "") # require tinyusb_pico_pio_usb target_link_libraries(${TARGET} PUBLIC pico_stdlib tinyusb_device tinyusb_host tinyusb_pico_pio_usb ) rp2040_family_configure_example_warnings(${TARGET})