Skip to content

Commit

Permalink
Merge pull request #1467 from kilograham/rp2040_cleanup
Browse files Browse the repository at this point in the history
Minor cleanup of RP2040 code post addition of Pico-PIO-USB
  • Loading branch information
hathach committed May 26, 2022
2 parents c1ae13b + 26c4d4b commit 7b1344f
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 81 deletions.
2 changes: 2 additions & 0 deletions examples/device/net_lwip_webserver/src/lwipopts.h
Expand Up @@ -49,7 +49,9 @@

#define TCP_MSS (1500 /*mtu*/ - 20 /*iphdr*/ - 20 /*tcphhr*/)
#define TCP_SND_BUF (2 * TCP_MSS)
#ifndef TCP_WND
#define TCP_WND (TCP_MSS)
#endif

#define ETHARP_SUPPORT_STATIC_ENTRIES 1

Expand Down
12 changes: 12 additions & 0 deletions examples/dual/CMakeLists.txt
@@ -0,0 +1,12 @@
cmake_minimum_required(VERSION 3.5)

include(${CMAKE_CURRENT_SOURCE_DIR}/../../hw/bsp/family_support.cmake)

project(tinyusb_dual_examples)
family_initialize_project(tinyusb_dual_examples ${CMAKE_CURRENT_LIST_DIR})
if (FAMILY STREQUAL "rp2040" AND NOT TARGET tinyusb_pico_pio_usb)
message("Skipping dual host/device mode examples as Pico-PIO-USB is not available")
else()
# family_add_subdirectory will filter what to actually add based on selected FAMILY
family_add_subdirectory(host_hid_to_device_cdc)
endif()
4 changes: 1 addition & 3 deletions examples/dual/host_hid_to_device_cdc/CMakeLists.txt
Expand Up @@ -25,6 +25,4 @@ target_include_directories(${PROJECT} PUBLIC

# Configure compilation flags and libraries for the example... see the corresponding function
# in hw/bsp/FAMILY/family.cmake for details.
family_configure_device_example(${PROJECT})
family_configure_host_example(${PROJECT})
family_configure_pico_pio_usb_example(${PROJECT})
family_configure_dual_usb_example(${PROJECT})
2 changes: 1 addition & 1 deletion examples/host/bare_api/CMakeLists.txt
Expand Up @@ -27,4 +27,4 @@ target_include_directories(${PROJECT} PUBLIC
family_configure_host_example(${PROJECT})

# For rp2040, un-comment to enable pico-pio-usb
# family_configure_pico_pio_usb_example(${PROJECT})
# family_add_pico_pio_usb(${PROJECT})
2 changes: 1 addition & 1 deletion examples/host/cdc_msc_hid/CMakeLists.txt
Expand Up @@ -29,4 +29,4 @@ target_include_directories(${PROJECT} PUBLIC
family_configure_host_example(${PROJECT})

# For rp2040, un-comment to enable pico-pio-usb
# family_configure_pico_pio_usb_example(${PROJECT})
# family_add_pico_pio_usb(${PROJECT})
2 changes: 1 addition & 1 deletion examples/host/hid_controller/CMakeLists.txt
Expand Up @@ -28,4 +28,4 @@ target_include_directories(${PROJECT} PUBLIC
family_configure_host_example(${PROJECT})

# For rp2040, un-comment to enable pico-pio-usb
# family_configure_pico_pio_usb_example(${PROJECT})
# family_add_pico_pio_usb(${PROJECT})
2 changes: 2 additions & 0 deletions hw/bsp/rp2040/family.c
Expand Up @@ -127,8 +127,10 @@ void board_init(void)
#ifndef BUTTON_BOOTSEL
#endif

#if CFG_TUH_RPI_PIO_USB || CFG_TUD_RPI_PIO_USB
// Set the system clock to a multiple of 120mhz for bitbanging USB with pico-usb
set_sys_clock_khz(120000, true);
#endif

#if defined(UART_DEV) && defined(LIB_PICO_STDIO_UART)
bi_decl(bi_2pins_with_func(UART_TX_PIN, UART_TX_PIN, GPIO_FUNC_UART));
Expand Down
156 changes: 82 additions & 74 deletions hw/bsp/rp2040/family.cmake
Expand Up @@ -23,7 +23,7 @@ if (NOT TARGET _rp2040_family_inclusion_marker)
set(PICO_TINYUSB_PATH ${TOP})
endif()

#------------------------------------
#------------------------------------
# Base config for both device and host; wrapped by SDK's tinyusb_common
#------------------------------------
add_library(tinyusb_common_base INTERFACE)
Expand All @@ -50,52 +50,21 @@ if (NOT TARGET _rp2040_family_inclusion_marker)
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
message("Compiling TinyUSB with CFG_TUSB_DEBUG=1")
set(TINYUSB_DEBUG_LEVEL 1)
endif ()
endif()

target_compile_definitions(tinyusb_common_base INTERFACE
CFG_TUSB_MCU=OPT_MCU_RP2040
CFG_TUSB_OS=OPT_OS_PICO
CFG_TUSB_DEBUG=${TINYUSB_DEBUG_LEVEL}
)

#------------------------------------
# PIO USB for both host and device
#------------------------------------
add_library(pico_pio_usb INTERFACE)

if (NOT DEFINED PICO_PIO_USB_PATH)
set(PICO_PIO_USB_PATH "${TOP}/hw/mcu/raspberry_pi/Pico-PIO-USB")
endif()

target_sources(pico_pio_usb INTERFACE
${PICO_PIO_USB_PATH}/src/pio_usb.c
${PICO_PIO_USB_PATH}/src/pio_usb_host.c
${PICO_PIO_USB_PATH}/src/pio_usb_device.c
${PICO_PIO_USB_PATH}/src/usb_crc.c
)

target_include_directories(pico_pio_usb INTERFACE
${PICO_PIO_USB_PATH}/src
)

target_link_libraries(pico_pio_usb INTERFACE
hardware_dma
hardware_pio
pico_multicore
)

target_compile_definitions(pico_pio_usb INTERFACE
PIO_USB_USE_TINYUSB
)

#------------------------------------
#------------------------------------
# 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/portable/raspberrypi/pio_usb/dcd_pio_usb.c
${TOP}/src/device/usbd.c
${TOP}/src/device/usbd_control.c
${TOP}/src/class/audio/audio_device.c
Expand All @@ -119,7 +88,6 @@ if (NOT TARGET _rp2040_family_inclusion_marker)
target_sources(tinyusb_host_base INTERFACE
${TOP}/src/portable/raspberrypi/rp2040/hcd_rp2040.c
${TOP}/src/portable/raspberrypi/rp2040/rp2040_usb.c
${TOP}/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c
${TOP}/src/host/usbh.c
${TOP}/src/host/hub.c
${TOP}/src/class/cdc/cdc_host.c
Expand All @@ -140,8 +108,8 @@ if (NOT TARGET _rp2040_family_inclusion_marker)
target_sources(tinyusb_bsp INTERFACE
${TOP}/hw/bsp/rp2040/family.c
)
# target_include_directories(tinyusb_bsp INTERFACE
# ${TOP}/hw/bsp/rp2040)
# target_include_directories(tinyusb_bsp INTERFACE
# ${TOP}/hw/bsp/rp2040)

# tinyusb_additions will hold our extra settings for examples
add_library(tinyusb_additions INTERFACE)
Expand All @@ -151,22 +119,22 @@ if (NOT TARGET _rp2040_family_inclusion_marker)
)

if(DEFINED LOG)
target_compile_definitions(tinyusb_additions INTERFACE CFG_TUSB_DEBUG=${LOG} )
target_compile_definitions(tinyusb_additions INTERFACE CFG_TUSB_DEBUG=${LOG})
endif()

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
)
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
)
endif()

#------------------------------------
Expand All @@ -189,41 +157,81 @@ if (NOT TARGET _rp2040_family_inclusion_marker)
target_link_libraries(${TARGET} PUBLIC pico_stdlib tinyusb_host)
endfunction()

function(family_configure_pico_pio_usb_example TARGET)
function(family_add_pico_pio_usb TARGET)
target_link_libraries(${TARGET} PUBLIC tinyusb_pico_pio_usb)
endfunction()

function(family_configure_dual_usb_example TARGET)
family_configure_target(${TARGET})
target_link_libraries(${TARGET} PUBLIC pico_stdlib pico_pio_usb)
pico_generate_pio_header(tinyusb_common_base ${PICO_PIO_USB_PATH}/src/usb_tx.pio)
pico_generate_pio_header(tinyusb_common_base ${PICO_PIO_USB_PATH}/src/usb_rx.pio)
# require tinyusb_pico_pio_usb
target_link_libraries(${TARGET} PUBLIC pico_stdlib tinyusb_device tinyusb_host tinyusb_pico_pio_usb )
endfunction()

function(check_and_add_pico_pio_usb_support)
# check for pico_generate_pio_header (as depending on environment we may be called before SDK is
# initialized in which case it isn't available yet), and only do the initialization once
if (COMMAND pico_generate_pio_header AND NOT TARGET tinyusb_pico_pio_usb)
#------------------------------------
# PIO USB for both host and device
#------------------------------------

if (NOT DEFINED PICO_PIO_USB_PATH)
set(PICO_PIO_USB_PATH "${TOP}/hw/mcu/raspberry_pi/Pico-PIO-USB")
endif()

if (EXISTS ${PICO_PIO_USB_PATH}/src/pio_usb.c)
add_library(tinyusb_pico_pio_usb INTERFACE)
target_sources(tinyusb_device_base INTERFACE
${TOP}/src/portable/raspberrypi/pio_usb/dcd_pio_usb.c
)
target_sources(tinyusb_host_base INTERFACE
${TOP}/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c
)

target_sources(tinyusb_pico_pio_usb INTERFACE
${PICO_PIO_USB_PATH}/src/pio_usb.c
${PICO_PIO_USB_PATH}/src/pio_usb_host.c
${PICO_PIO_USB_PATH}/src/pio_usb_device.c
${PICO_PIO_USB_PATH}/src/usb_crc.c
)

target_include_directories(tinyusb_pico_pio_usb INTERFACE
${PICO_PIO_USB_PATH}/src
)

target_link_libraries(tinyusb_pico_pio_usb INTERFACE
hardware_dma
hardware_pio
pico_multicore
)

target_compile_definitions(tinyusb_pico_pio_usb INTERFACE
PIO_USB_USE_TINYUSB
)

pico_generate_pio_header(tinyusb_pico_pio_usb ${PICO_PIO_USB_PATH}/src/usb_tx.pio)
pico_generate_pio_header(tinyusb_pico_pio_usb ${PICO_PIO_USB_PATH}/src/usb_rx.pio)
endif()
endif()
endfunction()

# Try to add Pico-PIO_USB support now for the case where this file is included directly
# after Pico SDK initialization, but without using the family_ functions (as is the case
# when included by the SDK itself)
check_and_add_pico_pio_usb_support()

function(family_initialize_project PROJECT DIR)
# call the original version of this function from family_common.cmake
_family_initialize_project(${PROJECT} ${DIR})
enable_language(C CXX ASM)
pico_sdk_init()

# now re-check for adding Pico-PIO_USB support now SDK is definitely available
check_and_add_pico_pio_usb_support()
endfunction()

# This method must be called from the project scope to suppress known warnings in TinyUSB source files
function(suppress_tinyusb_warnings)
set_source_files_properties(
${PICO_TINYUSB_PATH}/src/tusb.c
PROPERTIES
COMPILE_FLAGS "-Wno-conversion")
set_source_files_properties(
${PICO_TINYUSB_PATH}/src/common/tusb_fifo.c
PROPERTIES
COMPILE_FLAGS "-Wno-conversion -Wno-cast-qual")
set_source_files_properties(
${PICO_TINYUSB_PATH}/src/device/usbd.c
PROPERTIES
COMPILE_FLAGS "-Wno-conversion -Wno-cast-qual -Wno-null-dereference")
set_source_files_properties(
${PICO_TINYUSB_PATH}/src/device/usbd_control.c
PROPERTIES
COMPILE_FLAGS "-Wno-conversion")
set_source_files_properties(
${PICO_TINYUSB_PATH}/src/class/cdc/cdc_device.c
PROPERTIES
COMPILE_FLAGS "-Wno-conversion")
# there are currently no warnings to suppress, however this function must still exist
endfunction()
endif()
4 changes: 4 additions & 0 deletions src/portable/raspberrypi/rp2040/hcd_rp2040.c
Expand Up @@ -537,7 +537,11 @@ bool hcd_setup_send(uint8_t rhport, uint8_t dev_addr, uint8_t const setup_packet
(void) rhport;

// Copy data into setup packet buffer
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Warray-bounds"
#pragma GCC diagnostic ignored "-Wstringop-overflow"
memcpy((void*)&usbh_dpram->setup_packet[0], setup_packet, 8);
#pragma GCC diagnostic pop

// Configure EP0 struct with setup info for the trans complete
struct hw_endpoint *ep = _hw_endpoint_allocate(0);
Expand Down
2 changes: 1 addition & 1 deletion src/portable/raspberrypi/rp2040/rp2040_usb.c
Expand Up @@ -58,9 +58,9 @@ void rp2040_usb_init(void)
unreset_block_wait(RESETS_RESET_USBCTRL_BITS);

// Clear any previous state just in case
// TODO Suppress warning array-bounds with gcc11
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Warray-bounds"
#pragma GCC diagnostic ignored "-Wstringop-overflow"
memset(usb_hw, 0, sizeof(*usb_hw));
memset(usb_dpram, 0, sizeof(*usb_dpram));
#pragma GCC diagnostic pop
Expand Down

0 comments on commit 7b1344f

Please sign in to comment.