1
0
Fork 0
mirror of https://github.com/betaflight/betaflight.git synced 2025-07-23 08:15:30 +03:00

PICO: Adding tinyUSB library

This commit is contained in:
blckmn 2025-01-12 07:56:01 +11:00
parent 1774693395
commit 0109f50909
1507 changed files with 299535 additions and 0 deletions

View file

@ -0,0 +1,97 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2021, Ha Thach (tinyusb.org)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* This file is part of the TinyUSB stack.
*/
#ifndef BOARD_H_
#define BOARD_H_
#ifdef __cplusplus
extern "C" {
#endif
// LED
#ifdef PICO_DEFAULT_LED_PIN
#define LED_PIN PICO_DEFAULT_LED_PIN
#define LED_STATE_ON (!(PICO_DEFAULT_LED_PIN_INVERTED))
#endif
// Button pin is BOOTSEL which is flash CS pin
#define BUTTON_BOOTSEL
#define BUTTON_STATE_ACTIVE 0
// UART
#if defined(PICO_DEFAULT_UART_TX_PIN) && defined(PICO_DEFAULT_UART_RX_PIN) && \
defined(PICO_DEFAULT_UART) && defined(LIB_PICO_STDIO_UART)
#define UART_DEV PICO_DEFAULT_UART
#define UART_TX_PIN PICO_DEFAULT_UART_TX_PIN
#define UART_RX_PIN PICO_DEFAULT_UART_RX_PIN
#endif
//--------------------------------------------------------------------+
// PIO_USB
// default to pin on Adafruit Feather rp2040 USB Host or Tester if defined
//--------------------------------------------------------------------+
// #define USE_ADAFRUIT_FEATHER_RP2040_USBHOST
#ifdef USE_ADAFRUIT_FEATHER_RP2040_USBHOST
#define PICO_DEFAULT_PIO_USB_DP_PIN 16
#define PICO_DEFAULT_PIO_USB_VBUSEN_PIN 18
#endif
#ifndef PICO_DEFAULT_PIO_USB_DP_PIN
#define PICO_DEFAULT_PIO_USB_DP_PIN 20
#endif
// VBUS enable pin and its active state
#ifndef PICO_DEFAULT_PIO_USB_VBUSEN_PIN
#define PICO_DEFAULT_PIO_USB_VBUSEN_PIN 22
#endif
// VBUS enable state
#ifndef PICO_DEFAULT_PIO_USB_VBUSEN_STATE
#define PICO_DEFAULT_PIO_USB_VBUSEN_STATE 1
#endif
//--------------------------------------------------------------------
// USB Host MAX3421E
//--------------------------------------------------------------------
#ifdef PICO_DEFAULT_SPI
#define MAX3421_SPI PICO_DEFAULT_SPI // sdk v2
#else
#define MAX3421_SPI PICO_DEFAULT_SPI_INSTANCE // sdk v1
#endif
#define MAX3421_SCK_PIN PICO_DEFAULT_SPI_SCK_PIN
#define MAX3421_MOSI_PIN PICO_DEFAULT_SPI_TX_PIN
#define MAX3421_MISO_PIN PICO_DEFAULT_SPI_RX_PIN
#define MAX3421_CS_PIN 10
#define MAX3421_INTR_PIN 9
#ifdef __cplusplus
}
#endif
#endif /* BOARD_H_ */

View file

@ -0,0 +1,5 @@
set(PICO_PLATFORM rp2040)
set(PICO_BOARD adafruit_feather_rp2040)
# Enable MAX3421E USB Host
set(MAX3421_HOST 1)

View file

@ -0,0 +1 @@
# This builds with settings based purely on the current PICO_BOARD set via the SDK

View file

@ -0,0 +1,2 @@
set(PICO_PLATFORM rp2040)
set(PICO_BOARD pico)

View file

@ -0,0 +1,2 @@
set(PICO_PLATFORM rp2350-arm-s)
set(PICO_BOARD pico2)

View file

@ -0,0 +1,331 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
* Copyright (c) 2021, Ha Thach (tinyusb.org)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* This file is part of the TinyUSB stack.
*/
#include "pico/stdlib.h"
#include "pico/binary_info.h"
#include "pico/unique_id.h"
#include "hardware/gpio.h"
#include "hardware/sync.h"
#include "hardware/resets.h"
#include "hardware/clocks.h"
#include "hardware/structs/ioqspi.h"
#include "hardware/structs/sio.h"
#include "bsp/board_api.h"
#include "board.h"
#ifdef UART_DEV
static uart_inst_t *uart_inst;
#endif
#if (CFG_TUH_ENABLED && CFG_TUH_RPI_PIO_USB) || (CFG_TUD_ENABLED && CFG_TUD_RPI_PIO_USB)
#include "pio_usb.h"
#endif
#if CFG_TUH_ENABLED && CFG_TUH_MAX3421
#include "hardware/spi.h"
static void max3421_init(void);
#endif
#ifdef BUTTON_BOOTSEL
// This example blinks the Picoboard LED when the BOOTSEL button is pressed.
//
// Picoboard has a button attached to the flash CS pin, which the bootrom
// checks, and jumps straight to the USB bootcode if the button is pressed
// (pulling flash CS low). We can check this pin in by jumping to some code in
// SRAM (so that the XIP interface is not required), floating the flash CS
// pin, and observing whether it is pulled low.
//
// This doesn't work if others are trying to access flash at the same time,
// e.g. XIP streamer, or the other core.
bool __no_inline_not_in_flash_func(get_bootsel_button)(void) {
const uint CS_PIN_INDEX = 1;
// Must disable interrupts, as interrupt handlers may be in flash, and we
// are about to temporarily disable flash access!
uint32_t flags = save_and_disable_interrupts();
// Set chip select to Hi-Z
hw_write_masked(&ioqspi_hw->io[CS_PIN_INDEX].ctrl,
GPIO_OVERRIDE_LOW << IO_QSPI_GPIO_QSPI_SS_CTRL_OEOVER_LSB,
IO_QSPI_GPIO_QSPI_SS_CTRL_OEOVER_BITS);
// Note we can't call into any sleep functions in flash right now
for (volatile int i = 0; i < 1000; ++i);
// The HI GPIO registers in SIO can observe and control the 6 QSPI pins.
// Note the button pulls the pin *low* when pressed.
#ifdef __ARM_ARCH_6M__ // CM0 for rp2040
#define CS_BIT (1u << 1)
#else // rp2350 (cm33/risv)
#define CS_BIT SIO_GPIO_HI_IN_QSPI_CSN_BITS
#endif
bool button_state = (sio_hw->gpio_hi_in & CS_BIT);
// Need to restore the state of chip select, else we are going to have a
// bad time when we return to code in flash!
hw_write_masked(&ioqspi_hw->io[CS_PIN_INDEX].ctrl,
GPIO_OVERRIDE_NORMAL << IO_QSPI_GPIO_QSPI_SS_CTRL_OEOVER_LSB,
IO_QSPI_GPIO_QSPI_SS_CTRL_OEOVER_BITS);
restore_interrupts(flags);
return button_state;
}
#endif
//------------- Segger RTT retarget -------------//
#if defined(LOGGER_RTT)
// Logging with RTT
// - If RTT Control Block is not found by 'Auto Detection` try to use 'Search Range` with '0x20000000 0x10000'
// - SWD speed is rather slow around 1000Khz
#include "pico/stdio/driver.h"
#include "SEGGER_RTT.h"
static void stdio_rtt_write (const char *buf, int length) {
SEGGER_RTT_Write(0, buf, (unsigned) length);
}
static int stdio_rtt_read (char *buf, int len) {
return (int) SEGGER_RTT_Read(0, buf, (unsigned) len);
}
static stdio_driver_t stdio_rtt = {
.out_chars = stdio_rtt_write,
.out_flush = NULL,
.in_chars = stdio_rtt_read
};
void stdio_rtt_init(void) {
stdio_set_driver_enabled(&stdio_rtt, true);
}
#endif
//--------------------------------------------------------------------+
//
//--------------------------------------------------------------------+
void board_init(void)
{
#if (CFG_TUH_ENABLED && CFG_TUH_RPI_PIO_USB) || (CFG_TUD_ENABLED && 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);
#ifdef PICO_DEFAULT_PIO_USB_VBUSEN_PIN
gpio_init(PICO_DEFAULT_PIO_USB_VBUSEN_PIN);
gpio_set_dir(PICO_DEFAULT_PIO_USB_VBUSEN_PIN, GPIO_OUT);
gpio_put(PICO_DEFAULT_PIO_USB_VBUSEN_PIN, PICO_DEFAULT_PIO_USB_VBUSEN_STATE);
#endif
// rp2040 use pico-pio-usb for host tuh_configure() can be used to passed pio configuration to the host stack
// Note: tuh_configure() must be called before tuh_init()
pio_usb_configuration_t pio_cfg = PIO_USB_DEFAULT_CONFIG;
pio_cfg.pin_dp = PICO_DEFAULT_PIO_USB_DP_PIN;
tuh_configure(BOARD_TUH_RHPORT, TUH_CFGID_RPI_PIO_USB_CONFIGURATION, &pio_cfg);
#endif
#ifdef LED_PIN
bi_decl(bi_1pin_with_name(LED_PIN, "LED"));
gpio_init(LED_PIN);
gpio_set_dir(LED_PIN, GPIO_OUT);
#endif
// Button
#ifndef BUTTON_BOOTSEL
#endif
#ifdef UART_DEV
bi_decl(bi_2pins_with_func(UART_TX_PIN, UART_RX_PIN, GPIO_FUNC_UART));
uart_inst = uart_get_instance(UART_DEV);
stdio_uart_init_full(uart_inst, CFG_BOARD_UART_BAUDRATE, UART_TX_PIN, UART_RX_PIN);
#endif
#if defined(LOGGER_RTT)
stdio_rtt_init();
#endif
#if CFG_TUD_ENABLED
// TODO probably set up device mode?
#endif
#if CFG_TUH_ENABLED
#if CFG_TUH_MAX3421
max3421_init();
#endif
#endif
#if !CFG_TUD_ENABLED && !CFG_TUH_ENABLED
// board test exxample, reset usb controller
reset_block(RESETS_RESET_USBCTRL_BITS);
unreset_block_wait(RESETS_RESET_USBCTRL_BITS);
#endif
}
//--------------------------------------------------------------------+
// Board porting API
//--------------------------------------------------------------------+
void board_led_write(bool state) {
(void) state;
#ifdef LED_PIN
gpio_put(LED_PIN, state ? LED_STATE_ON : (1 - LED_STATE_ON));
#endif
}
uint32_t board_button_read(void) {
#ifdef BUTTON_BOOTSEL
return BUTTON_STATE_ACTIVE == get_bootsel_button();
#else
return 0;
#endif
}
size_t board_get_unique_id(uint8_t id[], size_t max_len) {
pico_unique_board_id_t pico_id;
pico_get_unique_board_id(&pico_id);
size_t len = PICO_UNIQUE_BOARD_ID_SIZE_BYTES;
if (len > max_len) {
len = max_len;
}
memcpy(id, pico_id.id, len);
return len;
}
int board_uart_read(uint8_t *buf, int len) {
#ifdef UART_DEV
int count = 0;
while ( (count < len) && uart_is_readable(uart_inst) ) {
buf[count] = uart_getc(uart_inst);
count++;
}
return count;
#else
(void) buf; (void) len;
return 0;
#endif
}
int board_uart_write(void const *buf, int len) {
#ifdef UART_DEV
char const *bufch = (char const *) buf;
for ( int i = 0; i < len; i++ ) {
uart_putc(uart_inst, bufch[i]);
}
return len;
#else
(void) buf; (void) len;
return 0;
#endif
}
int board_getchar(void) {
return getchar_timeout_us(0);
}
//--------------------------------------------------------------------+
// USB Interrupt Handler
// rp2040 implementation will install appropriate handler when initializing
// tinyusb. There is no need to forward IRQ from application
//--------------------------------------------------------------------+
//--------------------------------------------------------------------+
// API: SPI transfer with MAX3421E, must be implemented by application
//--------------------------------------------------------------------+
#if CFG_TUH_ENABLED && defined(CFG_TUH_MAX3421) && CFG_TUH_MAX3421
void max3421_int_handler(uint gpio, uint32_t event_mask) {
if (!(gpio == MAX3421_INTR_PIN && event_mask & GPIO_IRQ_EDGE_FALL)) return;
tuh_int_handler(BOARD_TUH_RHPORT, true);
}
static void max3421_init(void) {
// CS pin
gpio_init(MAX3421_CS_PIN);
gpio_set_dir(MAX3421_CS_PIN, GPIO_OUT);
gpio_put(MAX3421_CS_PIN, true);
// Interrupt pin
gpio_init(MAX3421_INTR_PIN);
gpio_set_dir(MAX3421_INTR_PIN, GPIO_IN);
gpio_pull_up(MAX3421_INTR_PIN);
gpio_set_irq_enabled_with_callback(MAX3421_INTR_PIN, GPIO_IRQ_EDGE_FALL, true, max3421_int_handler);
// SPI init
spi_init(MAX3421_SPI, 4*1000000ul);
gpio_set_function(MAX3421_SCK_PIN, GPIO_FUNC_SPI);
gpio_set_function(MAX3421_MOSI_PIN, GPIO_FUNC_SPI);
gpio_set_function(MAX3421_MISO_PIN, GPIO_FUNC_SPI);
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wnull-dereference"
#endif
spi_set_format(MAX3421_SPI, 8, SPI_CPOL_0, SPI_CPHA_0, SPI_MSB_FIRST);
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
}
//// API to enable/disable MAX3421 INTR pin interrupt
void tuh_max3421_int_api(uint8_t rhport, bool enabled) {
(void) rhport;
irq_set_enabled(IO_IRQ_BANK0, enabled);
}
// API to control MAX3421 SPI CS
void tuh_max3421_spi_cs_api(uint8_t rhport, bool active) {
(void) rhport;
gpio_put(MAX3421_CS_PIN, !active);
}
// API to transfer data with MAX3421 SPI
// Either tx_buf or rx_buf can be NULL, which means transfer is write or read only
bool tuh_max3421_spi_xfer_api(uint8_t rhport, uint8_t const* tx_buf, uint8_t* rx_buf, size_t xfer_bytes) {
(void) rhport;
if (tx_buf == NULL && rx_buf == NULL) {
return false;
}
int ret;
if (tx_buf == NULL) {
ret = spi_read_blocking(MAX3421_SPI, 0, rx_buf, xfer_bytes);
}else if (rx_buf == NULL) {
ret = spi_write_blocking(MAX3421_SPI, tx_buf, xfer_bytes);
}else {
ret = spi_write_read_blocking(spi0, tx_buf, rx_buf, xfer_bytes);
}
return ret == (int) xfer_bytes;
}
#endif

View file

@ -0,0 +1,407 @@
cmake_minimum_required(VERSION 3.13)
include_guard(GLOBAL)
if (NOT BOARD)
message("BOARD not specified, defaulting to pico_sdk")
set(BOARD pico_sdk)
endif()
include(${CMAKE_CURRENT_LIST_DIR}/boards/${BOARD}/board.cmake)
#if (TOOLCHAIN STREQUAL "clang")
# set(PICO_COMPILER "pico_arm_clang")
#else()
# set(PICO_COMPILER "pico_arm_gcc")
#endif()
# add the SDK in case we are standalone tinyusb example (noop if already present)
include(${CMAKE_CURRENT_LIST_DIR}/pico_sdk_import.cmake)
# include basic family CMake functionality
set(FAMILY_MCUS RP2040)
if (PICO_PLATFORM STREQUAL "rp2040")
set(JLINK_DEVICE rp2040_m0_0)
set(OPENOCD_TARGET rp2040)
elseif (PICO_PLATFORM STREQUAL "rp2350-arm-s" OR PICO_PLATFORM STREQUAL "rp2350")
set(JLINK_DEVICE rp2350_m33_0)
set(OPENOCD_TARGET rp2350)
elseif (PICO_PLATFORM STREQUAL "rp2350-riscv")
set(JLINK_DEVICE rp2350_riscv_0)
set(OPENOCD_TARGET rp2350-riscv)
endif()
set(OPENOCD_OPTION "-f interface/cmsis-dap.cfg -f target/${OPENOCD_TARGET}.cfg -c \"adapter speed 5000\"")
if (NOT PICO_TINYUSB_PATH)
set(PICO_TINYUSB_PATH ${TOP})
endif()
if (NOT TINYUSB_OPT_OS)
set(TINYUSB_OPT_OS OPT_OS_PICO)
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
)
if(DEFINED LOG)
set(TINYUSB_DEBUG_LEVEL ${LOG})
elseif (CMAKE_BUILD_TYPE STREQUAL "Debug")
message("Compiling TinyUSB with CFG_TUSB_DEBUG=1")
set(TINYUSB_DEBUG_LEVEL 1)
else ()
set(TINYUSB_DEBUG_LEVEL 0)
endif()
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}
)
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
#------------------------------------
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
)
#------------------------------------
# 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
)
# Sometimes have to do host specific actions in mostly common functions
target_compile_definitions(tinyusb_host_base INTERFACE
RP2040_USB_HOST_MODE=1
)
#------------------------------------
# Host MAX3421
#------------------------------------
add_library(tinyusb_host_max3421 INTERFACE)
target_sources(tinyusb_host_max3421 INTERFACE
${TOP}/src/portable/analog/max3421/hcd_max3421.c
)
target_compile_definitions(tinyusb_host_max3421 INTERFACE
CFG_TUH_MAX3421=1
)
target_link_libraries(tinyusb_host_max3421 INTERFACE
hardware_spi
)
#------------------------------------
# BSP & Additions
#------------------------------------
add_library(tinyusb_bsp INTERFACE)
target_sources(tinyusb_bsp INTERFACE
${TOP}/hw/bsp/rp2040/family.c
)
target_include_directories(tinyusb_bsp INTERFACE
${TOP}/hw
)
target_link_libraries(tinyusb_bsp INTERFACE
pico_unique_id
hardware_clocks
)
# tinyusb_additions will hold our extra settings for examples
add_library(tinyusb_additions INTERFACE)
target_compile_definitions(tinyusb_additions INTERFACE
PICO_RP2040_USB_DEVICE_ENUMERATION_FIX=1
PICO_RP2040_USB_DEVICE_UFRAME_FIX=1
)
if(LOGGER STREQUAL "RTT" OR 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
)
set_source_files_properties(${TOP}/lib/SEGGER_RTT/RTT/SEGGER_RTT.c
PROPERTIES
COMPILE_FLAGS "-Wno-cast-qual -Wno-cast-align -Wno-sign-conversion")
target_include_directories(tinyusb_additions INTERFACE
${TOP}/lib/SEGGER_RTT/RTT
)
endif()
#------------------------------------
# Functions
#------------------------------------
function(family_configure_target TARGET RTOS)
if (RTOS STREQUAL noos OR RTOS STREQUAL "")
set(RTOS_SUFFIX "")
else()
set(RTOS_SUFFIX _${RTOS})
endif()
# export RTOS_SUFFIX to parent scope
set(RTOS_SUFFIX ${RTOS_SUFFIX} PARENT_SCOPE)
# compile define from command line
if(DEFINED CFLAGS_CLI)
separate_arguments(CFLAGS_CLI)
target_compile_options(${TARGET} PUBLIC ${CFLAGS_CLI})
endif()
pico_add_extra_outputs(${TARGET})
pico_enable_stdio_uart(${TARGET} 1)
target_link_libraries(${TARGET} PUBLIC pico_stdlib tinyusb_board${RTOS_SUFFIX} tinyusb_additions)
family_flash_openocd(${TARGET})
family_flash_jlink(${TARGET})
endfunction()
function(rp2040_family_configure_example_warnings TARGET)
if (NOT PICO_TINYUSB_NO_EXAMPLE_WARNINGS)
family_add_default_example_warnings(${TARGET})
endif()
if(CMAKE_C_COMPILER_ID STREQUAL "Clang")
target_compile_options(${TARGET} PRIVATE -Wno-unreachable-code)
endif()
suppress_tinyusb_warnings()
endfunction()
function(family_configure_device_example TARGET RTOS)
family_configure_target(${TARGET} ${RTOS})
target_link_libraries(${TARGET} PUBLIC pico_stdlib tinyusb_device${RTOS_SUFFIX})
rp2040_family_configure_example_warnings(${TARGET})
endfunction()
function(family_add_pico_pio_usb TARGET)
target_link_libraries(${TARGET} PUBLIC tinyusb_pico_pio_usb)
endfunction()
# since Pico-PIO_USB compiler support may lag, and change from version to version, add a function that pico-sdk/pico-examples
# can check (if present) in case the user has updated their TinyUSB
function(is_compiler_supported_by_pico_pio_usb OUTVAR)
if ((NOT CMAKE_C_COMPILER_ID STREQUAL "GNU"))
SET(${OUTVAR} 0 PARENT_SCOPE)
else()
set(${OUTVAR} 1 PARENT_SCOPE)
endif()
endfunction()
function(family_configure_host_example TARGET RTOS)
family_configure_target(${TARGET} ${RTOS})
target_link_libraries(${TARGET} PUBLIC pico_stdlib tinyusb_host${RTOS_SUFFIX})
rp2040_family_configure_example_warnings(${TARGET})
# For rp2040 enable pico-pio-usb
if (TARGET tinyusb_pico_pio_usb)
# Pico-PIO-USB does not compile with all pico-sdk supported compilers, so check before enabling it
is_compiler_supported_by_pico_pio_usb(PICO_PIO_USB_COMPILER_SUPPORTED)
if (PICO_PIO_USB_COMPILER_SUPPORTED)
family_add_pico_pio_usb(${PROJECT})
endif()
endif()
# for max3421 host
if (MAX3421_HOST STREQUAL "1")
target_link_libraries(${TARGET} PUBLIC tinyusb_host_max3421)
endif()
endfunction()
function(family_configure_dual_usb_example TARGET RTOS)
family_configure_target(${TARGET} ${RTOS})
# 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})
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)
# some of these are pretty silly warnings only occurring in some older GCC versions 9 or prior
if (CMAKE_C_COMPILER_ID STREQUAL "GNU")
if (CMAKE_C_COMPILER_VERSION VERSION_LESS 10.0)
set(CONVERSION_WARNING_FILES
${PICO_TINYUSB_PATH}/src/tusb.c
${PICO_TINYUSB_PATH}/src/common/tusb_fifo.c
${PICO_TINYUSB_PATH}/src/device/usbd.c
${PICO_TINYUSB_PATH}/src/device/usbd_control.c
${PICO_TINYUSB_PATH}/src/host/usbh.c
${PICO_TINYUSB_PATH}/src/class/cdc/cdc_device.c
${PICO_TINYUSB_PATH}/src/class/cdc/cdc_host.c
${PICO_TINYUSB_PATH}/src/class/hid/hid_device.c
${PICO_TINYUSB_PATH}/src/class/hid/hid_host.c
${PICO_TINYUSB_PATH}/src/class/audio/audio_device.c
${PICO_TINYUSB_PATH}/src/class/dfu/dfu_device.c
${PICO_TINYUSB_PATH}/src/class/dfu/dfu_rt_device.c
${PICO_TINYUSB_PATH}/src/class/midi/midi_device.c
${PICO_TINYUSB_PATH}/src/class/usbtmc/usbtmc_device.c
${PICO_TINYUSB_PATH}/src/portable/raspberrypi/rp2040/hcd_rp2040.c
)
foreach(SOURCE_FILE IN LISTS CONVERSION_WARNING_FILES)
set_source_files_properties(
${SOURCE_FILE}
PROPERTIES
COMPILE_FLAGS "-Wno-conversion")
endforeach()
endif()
if (CMAKE_C_COMPILER_ID STREQUAL "GNU" AND CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 11.0)
set_source_files_properties(
${PICO_TINYUSB_PATH}/lib/fatfs/source/ff.c
COMPILE_FLAGS "-Wno-stringop-overflow -Wno-array-bounds")
endif()
set_source_files_properties(
${PICO_TINYUSB_PATH}/lib/fatfs/source/ff.c
PROPERTIES
COMPILE_FLAGS "-Wno-conversion -Wno-cast-qual")
set_source_files_properties(
${PICO_TINYUSB_PATH}/lib/lwip/src/core/tcp_in.c
${PICO_TINYUSB_PATH}/lib/lwip/src/core/tcp_out.c
PROPERTIES
COMPILE_FLAGS "-Wno-conversion")
set_source_files_properties(
${PICO_TINYUSB_PATH}/lib/networking/dnserver.c
${PICO_TINYUSB_PATH}/lib/networking/dhserver.c
${PICO_TINYUSB_PATH}/lib/networking/rndis_reports.c
PROPERTIES
COMPILE_FLAGS "-Wno-conversion -Wno-sign-conversion")
if (TARGET tinyusb_pico_pio_usb)
set_source_files_properties(
${PICO_TINYUSB_PATH}/hw/mcu/raspberry_pi/Pico-PIO-USB/src/pio_usb_device.c
${PICO_TINYUSB_PATH}/hw/mcu/raspberry_pi/Pico-PIO-USB/src/pio_usb.c
${PICO_TINYUSB_PATH}/hw/mcu/raspberry_pi/Pico-PIO-USB/src/pio_usb_host.c
${PICO_TINYUSB_PATH}/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c
PROPERTIES
COMPILE_FLAGS "-Wno-conversion -Wno-cast-qual -Wno-attributes")
endif()
elseif(CMAKE_C_COMPILER_ID STREQUAL "Clang")
set_source_files_properties(
${PICO_TINYUSB_PATH}/src/class/cdc/cdc_device.c
COMPILE_FLAGS "-Wno-unreachable-code")
set_source_files_properties(
${PICO_TINYUSB_PATH}/src/class/cdc/cdc_host.c
COMPILE_FLAGS "-Wno-unreachable-code-fallthrough")
set_source_files_properties(
${PICO_TINYUSB_PATH}/lib/fatfs/source/ff.c
PROPERTIES
COMPILE_FLAGS "-Wno-cast-qual")
endif()
endfunction()

View file

@ -0,0 +1,18 @@
JLINK_DEVICE = rp2040_m0_0
PYOCD_TARGET = rp2040
DEPS_SUBMODULES += hw/mcu/raspberry_pi/Pico-PIO-USB
ifeq ($(DEBUG), 1)
CMAKE_DEFSYM += -DCMAKE_BUILD_TYPE=Debug
endif
$(BUILD):
cmake -S . -B $(BUILD) -DFAMILY=$(FAMILY) -DBOARD=$(BOARD) -DPICO_BUILD_DOCS=0 $(CMAKE_DEFSYM)
all: $(BUILD)
$(MAKE) -C $(BUILD)
flash: flash-pyocd
flash-uf2:
@$(CP) $(BUILD)/$(PROJECT).uf2 /media/$(USER)/RPI-RP2

View file

@ -0,0 +1,62 @@
# This is a copy of <PICO_SDK_PATH>/external/pico_sdk_import.cmake
# This can be dropped into an external project to help locate this SDK
# It should be include()ed prior to project()
if (DEFINED ENV{PICO_SDK_PATH} AND (NOT PICO_SDK_PATH))
set(PICO_SDK_PATH $ENV{PICO_SDK_PATH})
message("Using PICO_SDK_PATH from environment ('${PICO_SDK_PATH}')")
endif ()
if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT} AND (NOT PICO_SDK_FETCH_FROM_GIT))
set(PICO_SDK_FETCH_FROM_GIT $ENV{PICO_SDK_FETCH_FROM_GIT})
message("Using PICO_SDK_FETCH_FROM_GIT from environment ('${PICO_SDK_FETCH_FROM_GIT}')")
endif ()
if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT_PATH} AND (NOT PICO_SDK_FETCH_FROM_GIT_PATH))
set(PICO_SDK_FETCH_FROM_GIT_PATH $ENV{PICO_SDK_FETCH_FROM_GIT_PATH})
message("Using PICO_SDK_FETCH_FROM_GIT_PATH from environment ('${PICO_SDK_FETCH_FROM_GIT_PATH}')")
endif ()
set(PICO_SDK_PATH "${PICO_SDK_PATH}" CACHE PATH "Path to the Raspberry Pi Pico SDK")
set(PICO_SDK_FETCH_FROM_GIT "${PICO_SDK_FETCH_FROM_GIT}" CACHE BOOL "Set to ON to fetch copy of SDK from git if not otherwise locatable")
set(PICO_SDK_FETCH_FROM_GIT_PATH "${PICO_SDK_FETCH_FROM_GIT_PATH}" CACHE FILEPATH "location to download SDK")
if (NOT PICO_SDK_PATH)
if (PICO_SDK_FETCH_FROM_GIT)
include(FetchContent)
set(FETCHCONTENT_BASE_DIR_SAVE ${FETCHCONTENT_BASE_DIR})
if (PICO_SDK_FETCH_FROM_GIT_PATH)
get_filename_component(FETCHCONTENT_BASE_DIR "${PICO_SDK_FETCH_FROM_GIT_PATH}" REALPATH BASE_DIR "${CMAKE_SOURCE_DIR}")
endif ()
FetchContent_Declare(
pico_sdk
GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk
GIT_TAG master
)
if (NOT pico_sdk)
message("Downloading Raspberry Pi Pico SDK")
FetchContent_Populate(pico_sdk)
set(PICO_SDK_PATH ${pico_sdk_SOURCE_DIR})
endif ()
set(FETCHCONTENT_BASE_DIR ${FETCHCONTENT_BASE_DIR_SAVE})
else ()
message(FATAL_ERROR
"SDK location was not specified. Please set PICO_SDK_PATH or set PICO_SDK_FETCH_FROM_GIT to on to fetch from git."
)
endif ()
endif ()
get_filename_component(PICO_SDK_PATH "${PICO_SDK_PATH}" REALPATH BASE_DIR "${CMAKE_BINARY_DIR}")
if (NOT EXISTS ${PICO_SDK_PATH})
message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' not found")
endif ()
set(PICO_SDK_INIT_CMAKE_FILE ${PICO_SDK_PATH}/pico_sdk_init.cmake)
if (NOT EXISTS ${PICO_SDK_INIT_CMAKE_FILE})
message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' does not appear to contain the Raspberry Pi Pico SDK")
endif ()
set(PICO_SDK_PATH ${PICO_SDK_PATH} CACHE PATH "Path to the Raspberry Pi Pico SDK" FORCE)
include(${PICO_SDK_INIT_CMAKE_FILE})

View file

@ -0,0 +1,3 @@
source [find interface/cmsis-dap.cfg]
adapter speed 5000
source [find target/rp2040.cfg]