diff --git a/src/platform/PICO/link/pico_rp2350.ld b/src/platform/PICO/link/pico_rp2350.ld index dd0cc76511..a645ded915 100644 --- a/src/platform/PICO/link/pico_rp2350.ld +++ b/src/platform/PICO/link/pico_rp2350.ld @@ -95,6 +95,15 @@ SECTIONS *(.data) /* .data sections */ *(.data*) /* .data* sections */ + . = ALIGN(4); + *(.after_data.*) + . = ALIGN(4); + /* preinit data */ + PROVIDE_HIDDEN (__mutex_array_start = .); + KEEP(*(SORT(.mutex_array.*))) + KEEP(*(.mutex_array)) + PROVIDE_HIDDEN (__mutex_array_end = .); + . = ALIGN(4); _edata = .; /* define a global symbol at data end */ } >RAM AT >FLASH diff --git a/src/platform/PICO/mk/PICO.mk b/src/platform/PICO/mk/PICO.mk index f8a5598983..43a8ed647f 100644 --- a/src/platform/PICO/mk/PICO.mk +++ b/src/platform/PICO/mk/PICO.mk @@ -12,32 +12,47 @@ SDK_DIR = $(LIB_MAIN_DIR)/pico-sdk CMSIS_DIR := $(SDK_DIR)/rp2_common/cmsis/stub/CMSIS #STDPERIPH -STDPERIPH_DIR := $(SDK_DIR)/rp2_common +STDPERIPH_DIR := $(SDK_DIR) STDPERIPH_SRC := \ - hardware_sync_spin_lock/sync_spin_lock.c \ - hardware_gpio/gpio.c \ - pico_stdio/stdio.c \ - hardware_uart/uart.c \ - hardware_irq/irq.c \ - hardware_timer/timer.c \ - hardware_clocks/clocks.c \ - hardware_pll/pll.c \ - hardware_spi/spi.c \ - hardware_i2c/i2c.c \ - hardware_adc/adc.c \ - hardware_pio/pio.c \ - hardware_watchdog/watchdog.c \ - hardware_flash/flash.c \ - pico_unique_id/unique_id.c + rp2_common/hardware_sync_spin_lock/sync_spin_lock.c \ + rp2_common/hardware_gpio/gpio.c \ + rp2_common/pico_stdio/stdio.c \ + rp2_common/hardware_uart/uart.c \ + rp2_common/hardware_irq/irq.c \ + rp2_common/hardware_irq/irq_handler_chain.S \ + rp2_common/hardware_timer/timer.c \ + rp2_common/hardware_clocks/clocks.c \ + rp2_common/hardware_pll/pll.c \ + rp2_common/hardware_spi/spi.c \ + rp2_common/hardware_i2c/i2c.c \ + rp2_common/hardware_adc/adc.c \ + rp2_common/hardware_pio/pio.c \ + rp2_common/hardware_watchdog/watchdog.c \ + rp2_common/hardware_flash/flash.c \ + rp2_common/pico_unique_id/unique_id.c \ + rp2_common/pico_platform_panic/panic.c \ + common/pico_sync/mutex.c \ + common/pico_time/time.c \ + common/pico_sync/lock_core.c \ + common/hardware_claim/claim.c \ + common/pico_sync/critical_section.c \ + rp2_common/hardware_sync/sync.c + +TINY_USB_SRC_DIR = tinyUSB/src +TINYUSB_SRC := \ + $(TINY_USB_SRC_DIR)/tusb.c \ + $(TINY_USB_SRC_DIR)/class/cdc/cdc_device.c \ + $(TINY_USB_SRC_DIR)/common/tusb_fifo.c \ + $(TINY_USB_SRC_DIR)/device/usbd.c \ + $(TINY_USB_SRC_DIR)/device/usbd_control.c \ + $(TINY_USB_SRC_DIR)/portable/raspberrypi/rp2040/dcd_rp2040.c \ + $(TINY_USB_SRC_DIR)/portable/raspberrypi/rp2040/rp2040_usb.c VPATH := $(VPATH):$(STDPERIPH_DIR) DEVICE_STDPERIPH_SRC := \ $(STDPERIPH_SRC) \ - $(USBCORE_SRC) \ - $(USBCDC_SRC) \ - $(USBHID_SRC) \ - $(USBMSC_SRC) + $(TINYUSB_SRC) ifeq ($(TARGET_MCU),RP2350B) TARGET_MCU_LIB_LOWER = rp2350 @@ -48,9 +63,9 @@ endif VPATH := $(VPATH):$(CMSIS_DIR)/Core/Include:$(CMSIS_DIR)/Device/$(TARGET_MCU_LIB_UPPER)/Include CMSIS_SRC := \ - INCLUDE_DIRS += \ $(TARGET_PLATFORM_DIR) \ + $(TARGET_PLATFORM_DIR)/usb \ $(TARGET_PLATFORM_DIR)/startup \ $(SDK_DIR)/common/pico_bit_ops_headers/include \ $(SDK_DIR)/common/pico_base_headers/include \ @@ -147,7 +162,8 @@ INCLUDE_DIRS += \ $(CMSIS_DIR)/Device/$(TARGET_MCU_LIB_UPPER)/Include \ $(SDK_DIR)/$(TARGET_MCU_LIB_LOWER)/pico_platform/include \ $(SDK_DIR)/$(TARGET_MCU_LIB_LOWER)/hardware_regs/include \ - $(SDK_DIR)/$(TARGET_MCU_LIB_LOWER)/hardware_structs/include + $(SDK_DIR)/$(TARGET_MCU_LIB_LOWER)/hardware_structs/include \ + $(LIB_MAIN_DIR)/tinyUSB/src #Flags ARCH_FLAGS = -mthumb -mcpu=cortex-m33 -march=armv8-m.main+fp+dsp -mfloat-abi=softfp -mcmse @@ -183,6 +199,7 @@ MCU_COMMON_SRC = \ drivers/bus_spi_config.c \ drivers/serial_pinconfig.c \ drivers/serial_uart_pinconfig.c \ + drivers/usb_io.c \ PICO/stdio_pico_stub.c \ PICO/debug_pico.c \ PICO/system.c \ @@ -190,6 +207,9 @@ MCU_COMMON_SRC = \ PICO/bus_spi_pico.c \ PICO/serial_uart_pico.c \ PICO/exti_pico.c \ - PICO/config_flash.c + PICO/config_flash.c \ + PICO/serial_usb_vcp_pico.c \ + PICO/usb/usb_cdc.c \ + PICO/usb/usb_descriptors.c DEVICE_FLAGS += diff --git a/src/platform/PICO/serial_usb_vcp_pico.c b/src/platform/PICO/serial_usb_vcp_pico.c new file mode 100644 index 0000000000..f1b7813efd --- /dev/null +++ b/src/platform/PICO/serial_usb_vcp_pico.c @@ -0,0 +1,208 @@ +/* + * This file is part of Betaflight. + * + * Betaflight is free software. You can redistribute this software + * and/or modify this software under the terms of the GNU General + * Public License as published by the Free Software Foundation, + * either version 3 of the License, or (at your option) any later + * version. + * + * Betaflight is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this software. + * + * If not, see . + */ + +#include +#include + +#include "platform.h" + +#ifdef USE_VCP + +#include "build/build_config.h" + +#include "common/utils.h" + +#include "drivers/io.h" + +#include "pg/usb.h" + +#include "usb/usb_cdc.h" + +#include "drivers/time.h" +#include "drivers/serial.h" +#include "drivers/serial_usb_vcp.h" + +static vcpPort_t vcpPort = { 0 }; + +static void usbVcpSetBaudRate(serialPort_t *instance, uint32_t baudRate) +{ + UNUSED(instance); + UNUSED(baudRate); +} + +static void usbVcpSetMode(serialPort_t *instance, portMode_e mode) +{ + UNUSED(instance); + UNUSED(mode); +} + +static void usbVcpSetCtrlLineStateCb(serialPort_t *instance, void (*cb)(void *context, uint16_t ctrlLineState), void *context) +{ + UNUSED(instance); + UNUSED(cb); + UNUSED(context); +} + +static void usbVcpSetBaudRateCb(serialPort_t *instance, void (*cb)(serialPort_t *context, uint32_t baud), serialPort_t *context) +{ + UNUSED(instance); + UNUSED(cb); + UNUSED(context); +} + +static bool isUsbVcpTransmitBufferEmpty(const serialPort_t *instance) +{ + UNUSED(instance); + return true; +} + +static uint32_t usbVcpRxBytesAvailable(const serialPort_t *instance) +{ + UNUSED(instance); + return cdc_usb_bytes_available(); +} + +static uint8_t usbVcpRead(serialPort_t *instance) +{ + UNUSED(instance); + + uint8_t buf[1]; + + while (true) { + if (cdc_usb_read(buf, 1)) { + return buf[0]; + } + } +} + +static void usbVcpWriteBuf(serialPort_t *instance, const void *data, int count) +{ + UNUSED(instance); + + if (!(cdc_usb_connected() && cdc_usb_configured())) { + return; + } + + const uint8_t *p = data; + while (count > 0) { + int txed = cdc_usb_write(p, count); + + if (txed <= 0) { + break; + } + count -= txed; + p += txed; + } +} + +static bool usbVcpFlush(vcpPort_t *port) +{ + uint32_t count = port->txAt; + port->txAt = 0; + + if (count == 0) { + return true; + } + + if (!cdc_usb_connected() || !cdc_usb_configured()) { + return false; + } + + const uint8_t *p = port->txBuf; + while (count > 0) { + int txed = cdc_usb_write(p, count); + + if (txed <= 0) { + break; + } + count -= txed; + p += txed; + } + return count == 0; +} + +static void usbVcpWrite(serialPort_t *instance, uint8_t c) +{ + vcpPort_t *port = container_of(instance, vcpPort_t, port); + + port->txBuf[port->txAt++] = c; + if (!port->buffering || port->txAt >= ARRAYLEN(port->txBuf)) { + usbVcpFlush(port); + } +} + +static void usbVcpBeginWrite(serialPort_t *instance) +{ + vcpPort_t *port = container_of(instance, vcpPort_t, port); + port->buffering = true; +} + +static uint32_t usbTxBytesFree(const serialPort_t *instance) +{ + UNUSED(instance); + return cdc_usb_tx_bytes_free(); +} + +static void usbVcpEndWrite(serialPort_t *instance) +{ + vcpPort_t *port = container_of(instance, vcpPort_t, port); + port->buffering = false; + usbVcpFlush(port); +} + +static const struct serialPortVTable usbVTable[] = { + { + .serialWrite = usbVcpWrite, + .serialTotalRxWaiting = usbVcpRxBytesAvailable, + .serialTotalTxFree = usbTxBytesFree, + .serialRead = usbVcpRead, + .serialSetBaudRate = usbVcpSetBaudRate, + .isSerialTransmitBufferEmpty = isUsbVcpTransmitBufferEmpty, + .setMode = usbVcpSetMode, + .setCtrlLineStateCb = usbVcpSetCtrlLineStateCb, + .setBaudRateCb = usbVcpSetBaudRateCb, + .writeBuf = usbVcpWriteBuf, + .beginWrite = usbVcpBeginWrite, + .endWrite = usbVcpEndWrite + } +}; + +serialPort_t *usbVcpOpen(void) +{ + cdc_usb_init(); + + vcpPort_t *s = &vcpPort; + s->port.vTable = usbVTable; + return &s->port; +} + +uint32_t usbVcpGetBaudRate(serialPort_t *instance) +{ + UNUSED(instance); + return cdc_usb_baud_rate(); +} + +uint8_t usbVcpIsConnected(void) +{ + return cdc_usb_connected(); +} + +#endif // USE_VCP diff --git a/src/platform/PICO/target/RP2350B/target.h b/src/platform/PICO/target/RP2350B/target.h index b549fa7d23..43efaa12ca 100644 --- a/src/platform/PICO/target/RP2350B/target.h +++ b/src/platform/PICO/target/RP2350B/target.h @@ -45,7 +45,7 @@ #undef USE_SOFTSERIAL1 #undef USE_SOFTSERIAL2 -#undef USE_VCP +#define USE_VCP #undef USE_TRANSPONDER #undef USE_DMA diff --git a/src/platform/PICO/usb/tusb_config.h b/src/platform/PICO/usb/tusb_config.h new file mode 100644 index 0000000000..f539c09cdd --- /dev/null +++ b/src/platform/PICO/usb/tusb_config.h @@ -0,0 +1,35 @@ +/* + * This file is part of Betaflight. + * + * Betaflight is free software. You can redistribute this software + * and/or modify this software under the terms of the GNU General + * Public License as published by the Free Software Foundation, + * either version 3 of the License, or (at your option) any later + * version. + * + * Betaflight is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this software. + * + * If not, see . + */ + +#pragma once + +#define CFG_TUD_ENABLED 1 +#define CFG_TUSB_RHPORT0_MODE OPT_MODE_DEVICE +#define CFG_TUD_CDC 1 +#define CFG_TUD_CDC_RX_BUFSIZE 256 +#define CFG_TUD_CDC_TX_BUFSIZE 256 + +#define TUP_DCD_EDPT_ISO_ALLOC +#define TUP_DCD_ENDPOINT_MAX 16 + +#define TU_ATTR_FAST_FUNC __attribute__((section(".time_critical.tinyusb"))) + +#define CFG_TUSB_MCU OPT_MCU_RP2040 diff --git a/src/platform/PICO/usb/usb_cdc.c b/src/platform/PICO/usb/usb_cdc.c new file mode 100644 index 0000000000..d0603a83f2 --- /dev/null +++ b/src/platform/PICO/usb/usb_cdc.c @@ -0,0 +1,276 @@ +/* + * This file is part of Betaflight. + * + * Betaflight is free software. You can redistribute this software + * and/or modify this software under the terms of the GNU General + * Public License as published by the Free Software Foundation, + * either version 3 of the License, or (at your option) any later + * version. + * + * Betaflight is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this software. + * + * If not, see . + */ + +#include "platform.h" + +#include "tusb_config.h" +#include "tusb.h" +#include "usb_cdc.h" + +#include "pico/binary_info.h" +#include "pico/time.h" +#include "pico/mutex.h" +#include "pico/critical_section.h" +#include "hardware/irq.h" + +#ifndef CDC_USB_TASK_INTERVAL_US +#define CDC_USB_TASK_INTERVAL_US 1000 +#endif + +#ifndef CDC_USB_WRITE_TIMEOUT_US +#define CDC_USB_WRITE_TIMEOUT_US 1000 +#endif + +#ifndef CDC_DEADLOCK_TIMEOUT_MS +#define CDC_DEADLOCK_TIMEOUT_MS 1000 +#endif + +#ifndef CDC_USB_BAUD_RATE +#define CDC_USD_BAUD_RATE 115200 +#endif + +static bool configured = false; +static mutex_t cdc_usb_mutex; + +// if this crit_sec is initialized, we are not in periodic timer mode, and must make sure +// we don't either create multiple one shot timers, or miss creating one. this crit_sec +// is used to protect the one_shot_timer_pending flag +static critical_section_t one_shot_timer_crit_sec; +static volatile bool one_shot_timer_pending; +static uint8_t low_priority_irq_num; + +static int64_t timer_task(alarm_id_t id, void *user_data) +{ + UNUSED(id); + UNUSED(user_data); + + int64_t repeat_time; + if (critical_section_is_initialized(&one_shot_timer_crit_sec)) { + critical_section_enter_blocking(&one_shot_timer_crit_sec); + one_shot_timer_pending = false; + critical_section_exit(&one_shot_timer_crit_sec); + repeat_time = 0; // don't repeat + } else { + repeat_time = CDC_USB_TASK_INTERVAL_US; + } + if (irq_is_enabled(low_priority_irq_num)) { + irq_set_pending(low_priority_irq_num); + return repeat_time; + } else { + return 0; // don't repeat + } +} + +static void low_priority_worker_irq(void) +{ + if (mutex_try_enter(&cdc_usb_mutex, NULL)) { + tud_task(); + mutex_exit(&cdc_usb_mutex); + } else { + // if the mutex is already owned, then we are in non IRQ code in this file. + // + // it would seem simplest to just let that code call tud_task() at the end, however this + // code might run during the call to tud_task() and we might miss a necessary tud_task() call + // + // if we are using a periodic timer (crit_sec is not initialized in this case), + // then we are happy just to wait until the next tick, however when we are not using a periodic timer, + // we must kick off a one-shot timer to make sure the tud_task() DOES run (this method + // will be called again as a result, and will try the mutex_try_enter again, and if that fails + // create another one shot timer again, and so on). + if (critical_section_is_initialized(&one_shot_timer_crit_sec)) { + bool need_timer; + critical_section_enter_blocking(&one_shot_timer_crit_sec); + need_timer = !one_shot_timer_pending; + one_shot_timer_pending = true; + critical_section_exit(&one_shot_timer_crit_sec); + if (need_timer) { + add_alarm_in_us(CDC_USB_TASK_INTERVAL_US, timer_task, NULL, true); + } + } + } +} + +static void usb_irq(void) +{ + irq_set_pending(low_priority_irq_num); +} + +int cdc_usb_write(const uint8_t *buf, unsigned length) +{ + static uint64_t last_avail_time; + int written = 0; + + if (!mutex_try_enter_block_until(&cdc_usb_mutex, make_timeout_time_ms(CDC_DEADLOCK_TIMEOUT_MS))) { + return -1; + } + + if (cdc_usb_connected()) { + for (unsigned i = 0; i < length;) { + unsigned n = length - i; + uint32_t avail = tud_cdc_write_available(); + if (n > avail) n = avail; + if (n) { + uint32_t n2 = tud_cdc_write(buf + i, n); + tud_task(); + tud_cdc_write_flush(); + i += n2; + written = i; + last_avail_time = time_us_64(); + } else { + tud_task(); + tud_cdc_write_flush(); + if (!cdc_usb_connected() || (!tud_cdc_write_available() && time_us_64() > last_avail_time + CDC_USB_WRITE_TIMEOUT_US)) { + break; + } + } + } + } else { + // reset our timeout + last_avail_time = 0; + } + mutex_exit(&cdc_usb_mutex); + return written; +} + +void cdc_usb_write_flush(void) +{ + if (!mutex_try_enter_block_until(&cdc_usb_mutex, make_timeout_time_ms(CDC_DEADLOCK_TIMEOUT_MS))) { + return; + } + do { + tud_task(); + } while (tud_cdc_write_flush()); + mutex_exit(&cdc_usb_mutex); +} + +int cdc_usb_read(uint8_t *buf, unsigned length) +{ + // note we perform this check outside the lock, to try and prevent possible deadlock conditions + // with printf in IRQs (which we will escape through timeouts elsewhere, but that would be less graceful). + // + // these are just checks of state, so we can call them while not holding the lock. + // they may be wrong, but only if we are in the middle of a tud_task call, in which case at worst + // we will mistakenly think we have data available when we do not (we will check again), or + // tud_task will complete running and we will check the right values the next time. + // + int rc = PICO_ERROR_NO_DATA; + if (cdc_usb_connected() && tud_cdc_available()) { + if (!mutex_try_enter_block_until(&cdc_usb_mutex, make_timeout_time_ms(CDC_DEADLOCK_TIMEOUT_MS))) { + return PICO_ERROR_NO_DATA; // would deadlock otherwise + } + if (cdc_usb_connected() && tud_cdc_available()) { + uint32_t count = tud_cdc_read(buf, length); + rc = count ? (int)count : PICO_ERROR_NO_DATA; + } else { + // because our mutex use may starve out the background task, run tud_task here (we own the mutex) + tud_task(); + } + mutex_exit(&cdc_usb_mutex); + } + return rc; +} + +bool cdc_usb_init(void) +{ + if (get_core_num() != alarm_pool_core_num(alarm_pool_get_default())) { + // included an assertion here rather than just returning false, as this is likely + // a coding bug, rather than anything else. + assert(false); + return false; + } + + // initialize TinyUSB, as user hasn't explicitly linked it + tusb_init(); + + if (!mutex_is_initialized(&cdc_usb_mutex)) { + mutex_init(&cdc_usb_mutex); + } + bool rc = true; + low_priority_irq_num = (uint8_t)user_irq_claim_unused(true); + + irq_set_exclusive_handler(low_priority_irq_num, low_priority_worker_irq); + irq_set_enabled(low_priority_irq_num, true); + + if (irq_has_shared_handler(USBCTRL_IRQ)) { + critical_section_init_with_lock_num(&one_shot_timer_crit_sec, spin_lock_claim_unused(true)); + // we can use a shared handler to notice when there may be work to do + irq_add_shared_handler(USBCTRL_IRQ, usb_irq, PICO_SHARED_IRQ_HANDLER_LOWEST_ORDER_PRIORITY); + } else { + // we use initialization state of the one_shot_timer_critsec as a flag + memset(&one_shot_timer_crit_sec, 0, sizeof(one_shot_timer_crit_sec)); + rc = add_alarm_in_us(CDC_USB_TASK_INTERVAL_US, timer_task, NULL, true) >= 0; + } + + configured = rc; + return rc; +} + +bool cdc_usb_deinit(void) +{ + if (get_core_num() != alarm_pool_core_num(alarm_pool_get_default())) { + // included an assertion here rather than just returning false, as this is likely + // a coding bug, rather than anything else. + assert(false); + return false; + } + + assert(tud_inited()); // we expect the caller to have initialized when calling sdio_usb_init + + if (irq_has_shared_handler(USBCTRL_IRQ)) { + spin_lock_unclaim(spin_lock_get_num(one_shot_timer_crit_sec.spin_lock)); + critical_section_deinit(&one_shot_timer_crit_sec); + // we can use a shared handler to notice when there may be work to do + irq_remove_handler(USBCTRL_IRQ, usb_irq); + } else { + // timer is disabled by disabling the irq + } + + irq_set_enabled(low_priority_irq_num, false); + user_irq_unclaim(low_priority_irq_num); + + configured = false; + return true; +} + +bool cdc_usb_configured(void) +{ + return configured; +} + +bool cdc_usb_connected(void) +{ + return tud_cdc_connected(); +} + +bool cdc_usb_bytes_available(void) +{ + return tud_cdc_available(); +} + +uint32_t cdc_usb_baud_rate(void) +{ + return CDC_USD_BAUD_RATE; +} + +uint32_t cdc_usb_tx_bytes_free(void) +{ + return tud_cdc_write_available(); +} diff --git a/src/platform/PICO/usb/usb_cdc.h b/src/platform/PICO/usb/usb_cdc.h new file mode 100644 index 0000000000..aadc9bb898 --- /dev/null +++ b/src/platform/PICO/usb/usb_cdc.h @@ -0,0 +1,38 @@ +/* + * This file is part of Betaflight. + * + * Betaflight is free software. You can redistribute this software + * and/or modify this software under the terms of the GNU General + * Public License as published by the Free Software Foundation, + * either version 3 of the License, or (at your option) any later + * version. + * + * Betaflight is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this software. + * + * If not, see . + */ + +#pragma once + +#include +#include + +#include "platform.h" + +void cdc_usb_write_flush(void); +int cdc_usb_write(const uint8_t *buf, unsigned length); +int cdc_usb_read(uint8_t *buf, unsigned length); +bool cdc_usb_init(void); +bool cdc_usb_deinit(void); +bool cdc_usb_configured(void); +bool cdc_usb_connected(void); +bool cdc_usb_bytes_available(void); +uint32_t cdc_usb_baud_rate(void); +uint32_t cdc_usb_tx_bytes_free(void); diff --git a/src/platform/PICO/usb/usb_descriptors.c b/src/platform/PICO/usb/usb_descriptors.c new file mode 100644 index 0000000000..0275371704 --- /dev/null +++ b/src/platform/PICO/usb/usb_descriptors.c @@ -0,0 +1,186 @@ +/* + * This file is part of Betaflight. + * + * Betaflight is free software. You can redistribute this software + * and/or modify this software under the terms of the GNU General + * Public License as published by the Free Software Foundation, + * either version 3 of the License, or (at your option) any later + * version. + * + * Betaflight is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this software. + * + * If not, see . + */ + +/* + * This file is based on a file originally part of the + * MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 Raspberry Pi (Trading) Ltd. + * Copyright (c) 2019 Damien P. George + * + * 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. + */ + +#include "tusb.h" +#include "pico/unique_id.h" +#include "common/utils.h" + +#ifndef USBD_VID + // Raspberry Pi +#define USBD_VID (0x2E8A) +#endif + +#ifndef USBD_PID +#if PICO_RP2040 + // Raspberry Pi Pico SDK CDC for RP2040 +#define USBD_PID (0x000a) +#else + // Raspberry Pi Pico SDK CDC +#define USBD_PID (0x0009) +#endif +#endif + +#ifndef USBD_MANUFACTURER +#define USBD_MANUFACTURER "Betaflight Pico" +#endif + +#ifndef USBD_PRODUCT +#define USBD_PRODUCT "Pico" +#endif + +#define TUD_RPI_RESET_DESC_LEN 9 +#define USBD_DESC_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN + TUD_RPI_RESET_DESC_LEN) + +#define USBD_CONFIGURATION_DESCRIPTOR_ATTRIBUTE 0 +#define USBD_MAX_POWER_MA 250 + +#define USBD_ITF_CDC 0 // needs 2 interfaces +#define USBD_ITF_MAX 2 + +#define USBD_CDC_EP_CMD 0x81 +#define USBD_CDC_EP_OUT 0x02 +#define USBD_CDC_EP_IN 0x82 +#define USBD_CDC_CMD_MAX_SIZE 8 +#define USBD_CDC_IN_OUT_MAX_SIZE 64 + +#define USBD_STR_0 0x00 +#define USBD_STR_MANUF 0x01 +#define USBD_STR_PRODUCT 0x02 +#define USBD_STR_SERIAL 0x03 +#define USBD_STR_CDC 0x04 +#define USBD_STR_RPI_RESET 0x05 + +// Note: descriptors returned from callbacks must exist long enough for transfer to complete + +static const tusb_desc_device_t usbd_desc_device = { + .bLength = sizeof(tusb_desc_device_t), + .bDescriptorType = TUSB_DESC_DEVICE, + .bcdUSB = 0x0200, + .bDeviceClass = TUSB_CLASS_MISC, + .bDeviceSubClass = MISC_SUBCLASS_COMMON, + .bDeviceProtocol = MISC_PROTOCOL_IAD, + .bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE, + .idVendor = USBD_VID, + .idProduct = USBD_PID, + .bcdDevice = 0x0100, + .iManufacturer = USBD_STR_MANUF, + .iProduct = USBD_STR_PRODUCT, + .iSerialNumber = USBD_STR_SERIAL, + .bNumConfigurations = 1, +}; + +#define TUD_RPI_RESET_DESCRIPTOR(_itfnum, _stridx) \ + /* Interface */\ + 9, TUSB_DESC_INTERFACE, _itfnum, 0, 0, TUSB_CLASS_VENDOR_SPECIFIC, RESET_INTERFACE_SUBCLASS, RESET_INTERFACE_PROTOCOL, _stridx, + +static const uint8_t usbd_desc_cfg[USBD_DESC_LEN] = { + TUD_CONFIG_DESCRIPTOR(1, USBD_ITF_MAX, USBD_STR_0, USBD_DESC_LEN, + USBD_CONFIGURATION_DESCRIPTOR_ATTRIBUTE, USBD_MAX_POWER_MA), + + TUD_CDC_DESCRIPTOR(USBD_ITF_CDC, USBD_STR_CDC, USBD_CDC_EP_CMD, + USBD_CDC_CMD_MAX_SIZE, USBD_CDC_EP_OUT, USBD_CDC_EP_IN, USBD_CDC_IN_OUT_MAX_SIZE), +}; + +static char usbd_serial_str[PICO_UNIQUE_BOARD_ID_SIZE_BYTES * 2 + 1]; + +static const char *const usbd_desc_str[] = { + [USBD_STR_MANUF] = USBD_MANUFACTURER, + [USBD_STR_PRODUCT] = USBD_PRODUCT, + [USBD_STR_SERIAL] = usbd_serial_str, + [USBD_STR_CDC] = "Board CDC", +}; + +const uint8_t *tud_descriptor_device_cb(void) +{ + return (const uint8_t *)&usbd_desc_device; +} + +const uint8_t *tud_descriptor_configuration_cb(uint8_t index) +{ + UNUSED(index); + return usbd_desc_cfg; +} + +const uint16_t *tud_descriptor_string_cb(uint8_t index, uint16_t langid) +{ + UNUSED(langid); + +#ifndef USBD_DESC_STR_MAX +#define USBD_DESC_STR_MAX (20) +#elif USBD_DESC_STR_MAX > 127 +#error USBD_DESC_STR_MAX too high (max is 127). +#elif USBD_DESC_STR_MAX < 17 +#error USBD_DESC_STR_MAX too low (min is 17). +#endif + static uint16_t desc_str[USBD_DESC_STR_MAX]; + + // Assign the SN using the unique flash id + if (!usbd_serial_str[0]) { + pico_get_unique_board_id_string(usbd_serial_str, sizeof(usbd_serial_str)); + } + + unsigned len; + if (index == 0) { + desc_str[1] = 0x0409; // supported language is English + len = 1; + } else { + if (index >= ARRAYLEN(usbd_desc_str)) { + return NULL; + } + const char *str = usbd_desc_str[index]; + for (len = 0; len < USBD_DESC_STR_MAX - 1 && str[len]; ++len) { + desc_str[1 + len] = str[len]; + } + } + + // first byte is length (including header), second byte is string type + desc_str[0] = (uint16_t)((TUSB_DESC_STRING << 8) | (2 * len + 2)); + + return desc_str; +}