diff --git a/ports/nrf/Makefile b/ports/nrf/Makefile index a80a37e1fb464..78a17e09e9c19 100644 --- a/ports/nrf/Makefile +++ b/ports/nrf/Makefile @@ -242,7 +242,7 @@ SRC_SHARED_C += $(addprefix shared/,\ runtime/pyexec.c \ runtime/sys_stdio_mphal.c \ runtime/interrupt_char.c \ - runtime/tinyusb_helpers.c \ + tinyusb/mp_cdc_common.c \ timeutils/timeutils.c \ ) diff --git a/ports/rp2/CMakeLists.txt b/ports/rp2/CMakeLists.txt index 37ac78b7339f4..15264bf8ad614 100644 --- a/ports/rp2/CMakeLists.txt +++ b/ports/rp2/CMakeLists.txt @@ -94,8 +94,10 @@ set(MICROPY_SOURCE_LIB ${MICROPY_DIR}/shared/runtime/pyexec.c ${MICROPY_DIR}/shared/runtime/stdout_helpers.c ${MICROPY_DIR}/shared/runtime/sys_stdio_mphal.c - ${MICROPY_DIR}/shared/runtime/tinyusb_helpers.c ${MICROPY_DIR}/shared/timeutils/timeutils.c + ${MICROPY_DIR}/shared/tinyusb/mp_cdc_common.c + ${MICROPY_DIR}/shared/tinyusb/mp_usbd.c + ${MICROPY_DIR}/shared/tinyusb/mp_usbd_descriptor.c ) set(MICROPY_SOURCE_DRIVERS @@ -125,8 +127,8 @@ set(MICROPY_SOURCE_PORT pendsv.c rp2_flash.c rp2_pio.c - tusb_port.c uart.c + usbd.c msc_disk.c mbedtls/mbedtls_port.c ) @@ -233,6 +235,11 @@ if(MICROPY_BLUETOOTH_NIMBLE) list(APPEND MICROPY_INC_CORE ${NIMBLE_INCLUDE}) endif() +# tinyusb helper +target_include_directories(${MICROPY_TARGET} PRIVATE + ${MICROPY_DIR}/shared/tinyusb/ +) + if (MICROPY_PY_NETWORK_CYW43) string(CONCAT GIT_SUBMODULES "${GIT_SUBMODULES} " lib/cyw43-driver) if((NOT (${ECHO_SUBMODULES})) AND NOT EXISTS ${MICROPY_DIR}/lib/cyw43-driver/src/cyw43.h) diff --git a/ports/rp2/boards/PICO/mpconfigboard.h b/ports/rp2/boards/PICO/mpconfigboard.h index fb7f8054aaa1a..c39bc4d489bb6 100644 --- a/ports/rp2/boards/PICO/mpconfigboard.h +++ b/ports/rp2/boards/PICO/mpconfigboard.h @@ -1,6 +1,3 @@ // Board and hardware specific configuration #define MICROPY_HW_BOARD_NAME "Raspberry Pi Pico" #define MICROPY_HW_FLASH_STORAGE_BYTES (1408 * 1024) - -// Enable USB Mass Storage with FatFS filesystem. -// #define MICROPY_HW_USB_MSC (1) diff --git a/ports/rp2/main.c b/ports/rp2/main.c index 1062236a3916b..fdcdd90d4c303 100644 --- a/ports/rp2/main.c +++ b/ports/rp2/main.c @@ -43,6 +43,7 @@ #include "modrp2.h" #include "mpbthciport.h" #include "genhdr/mpversion.h" +#include "mp_usbd.h" #include "pico/stdlib.h" #include "pico/binary_info.h" @@ -85,7 +86,9 @@ int main(int argc, char **argv) { #endif #if MICROPY_HW_ENABLE_USBDEV + #if MICROPY_HW_USB_CDC bi_decl(bi_program_feature("USB REPL")) + #endif tusb_init(); #endif diff --git a/ports/rp2/mpconfigport.h b/ports/rp2/mpconfigport.h index a3725789f0848..e0bcd17c40e0b 100644 --- a/ports/rp2/mpconfigport.h +++ b/ports/rp2/mpconfigport.h @@ -27,19 +27,28 @@ // Options controlling how MicroPython is built, overriding defaults in py/mpconfig.h #include +#include "hardware/flash.h" #include "hardware/spi.h" #include "hardware/sync.h" #include "pico/binary_info.h" #include "pico/multicore.h" #include "mpconfigboard.h" -#if MICROPY_HW_USB_MSC -#include "hardware/flash.h" -#endif // Board and hardware specific configuration #define MICROPY_HW_MCU_NAME "RP2040" -#define MICROPY_HW_ENABLE_UART_REPL (0) // useful if there is no USB -#define MICROPY_HW_ENABLE_USBDEV (1) +#define MICROPY_HW_ENABLE_UART_REPL (1) // useful if there is no USB +#define MICROPY_HW_ENABLE_USBDEV (1) + +#if MICROPY_HW_ENABLE_USBDEV +// Enable USB-CDC serial port +#ifndef MICROPY_HW_USB_CDC +#define MICROPY_HW_USB_CDC (1) +#endif +// Enable USB Mass Storage with FatFS filesystem. +#ifndef MICROPY_HW_USB_MSC +#define MICROPY_HW_USB_MSC (0) +#endif +#endif #ifndef MICROPY_CONFIG_ROM_LEVEL #define MICROPY_CONFIG_ROM_LEVEL (MICROPY_CONFIG_ROM_LEVEL_EXTRA_FEATURES) @@ -198,6 +207,13 @@ extern const struct _mod_network_nic_type_t mod_network_nic_type_wiznet5k; // Miscellaneous settings +#ifndef MICROPY_HW_USB_VID +#define MICROPY_HW_USB_VID (0x2E8A) // Raspberry Pi +#endif +#ifndef MICROPY_HW_USB_PID +#define MICROPY_HW_USB_PID (0x0005) // RP2 MicroPython +#endif + // Entering a critical section. extern uint32_t mp_thread_begin_atomic_section(void); extern void mp_thread_end_atomic_section(uint32_t); @@ -210,7 +226,7 @@ extern void mp_thread_end_atomic_section(uint32_t); #define MICROPY_PY_LWIP_EXIT lwip_lock_release(); #if MICROPY_HW_ENABLE_USBDEV -#define MICROPY_HW_USBDEV_TASK_HOOK extern void tud_task_ext(uint32_t, bool); tud_task_ext(0, false); +#define MICROPY_HW_USBDEV_TASK_HOOK extern void usbd_task(void); usbd_task(); #define MICROPY_VM_HOOK_COUNT (10) #define MICROPY_VM_HOOK_INIT static uint vm_hook_divisor = MICROPY_VM_HOOK_COUNT; #define MICROPY_VM_HOOK_POLL if (get_core_num() == 0 && --vm_hook_divisor == 0) { \ diff --git a/ports/rp2/mphalport.c b/ports/rp2/mphalport.c index 6ba234c662e04..510d486d96a57 100644 --- a/ports/rp2/mphalport.c +++ b/ports/rp2/mphalport.c @@ -39,7 +39,7 @@ #include "lib/cyw43-driver/src/cyw43.h" #endif -#if MICROPY_HW_ENABLE_UART_REPL || MICROPY_HW_ENABLE_USBDEV +#if MICROPY_HW_ENABLE_UART_REPL || MICROPY_HW_USB_CDC #ifndef MICROPY_HW_STDIN_BUFFER_LEN #define MICROPY_HW_STDIN_BUFFER_LEN 512 @@ -50,7 +50,7 @@ ringbuf_t stdin_ringbuf = { stdin_ringbuf_array, sizeof(stdin_ringbuf_array) }; #endif -#if MICROPY_HW_ENABLE_USBDEV +#if MICROPY_HW_USB_CDC uint8_t cdc_itf_pending; // keep track of cdc interfaces which need attention to poll @@ -91,10 +91,10 @@ void tud_cdc_rx_cb(uint8_t itf) { uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) { uintptr_t ret = 0; - #if MICROPY_HW_ENABLE_USBDEV + #if MICROPY_HW_USB_CDC poll_cdc_interfaces(); #endif - #if MICROPY_HW_ENABLE_UART_REPL || MICROPY_HW_ENABLE_USBDEV + #if MICROPY_HW_ENABLE_UART_REPL || MICROPY_HW_USB_CDC if ((poll_flags & MP_STREAM_POLL_RD) && ringbuf_peek(&stdin_ringbuf) != -1) { ret |= MP_STREAM_POLL_RD; } @@ -108,7 +108,7 @@ uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) { // Receive single character int mp_hal_stdin_rx_chr(void) { for (;;) { - #if MICROPY_HW_ENABLE_USBDEV + #if MICROPY_HW_USB_CDC poll_cdc_interfaces(); #endif @@ -132,7 +132,7 @@ void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) { mp_uart_write_strn(str, len); #endif - #if MICROPY_HW_ENABLE_USBDEV + #if MICROPY_HW_USB_CDC if (tud_cdc_connected()) { for (size_t i = 0; i < len;) { uint32_t n = len - i; diff --git a/ports/rp2/tusb_port.c b/ports/rp2/tusb_port.c deleted file mode 100644 index 4aac08791e94e..0000000000000 --- a/ports/rp2/tusb_port.c +++ /dev/null @@ -1,151 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * 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" - -#ifndef MICROPY_HW_USB_VID -#define MICROPY_HW_USB_VID (0x2E8A) // Raspberry Pi -#endif -#ifndef MICROPY_HW_USB_PID -#define MICROPY_HW_USB_PID (0x0005) // RP2 MicroPython -#endif - -#if CFG_TUD_MSC -#define USBD_DESC_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN + TUD_MSC_DESC_LEN) -#else -#define USBD_DESC_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN) -#endif -#define USBD_MAX_POWER_MA (250) - -#define USBD_ITF_CDC (0) // needs 2 interfaces -#define USBD_ITF_MSC (2) -#if CFG_TUD_MSC -#define USBD_ITF_MAX (3) -#else -#define USBD_ITF_MAX (2) -#endif - -#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 EPNUM_MSC_OUT (0x03) -#define EPNUM_MSC_IN (0x83) - -#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_MSC (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 = MICROPY_HW_USB_VID, - .idProduct = MICROPY_HW_USB_PID, - .bcdDevice = 0x0100, - .iManufacturer = USBD_STR_MANUF, - .iProduct = USBD_STR_PRODUCT, - .iSerialNumber = USBD_STR_SERIAL, - .bNumConfigurations = 1, -}; - -static const uint8_t usbd_desc_cfg[USBD_DESC_LEN] = { - TUD_CONFIG_DESCRIPTOR(1, USBD_ITF_MAX, USBD_STR_0, USBD_DESC_LEN, - 0, 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), - #if CFG_TUD_MSC - TUD_MSC_DESCRIPTOR(USBD_ITF_MSC, 5, EPNUM_MSC_OUT, EPNUM_MSC_IN, 64), - #endif -}; - -static const char *const usbd_desc_str[] = { - [USBD_STR_MANUF] = "MicroPython", - [USBD_STR_PRODUCT] = "Board in FS mode", - [USBD_STR_SERIAL] = NULL, // generated dynamically - [USBD_STR_CDC] = "Board CDC", - #if CFG_TUD_MSC - [USBD_STR_MSC] = "Board MSC", - #endif -}; - -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) { - (void)index; - return usbd_desc_cfg; -} - -const uint16_t *tud_descriptor_string_cb(uint8_t index, uint16_t langid) { - #define DESC_STR_MAX (20) - static uint16_t desc_str[DESC_STR_MAX]; - - uint8_t len; - if (index == 0) { - desc_str[1] = 0x0409; // supported language is English - len = 1; - } else { - if (index >= sizeof(usbd_desc_str) / sizeof(usbd_desc_str[0])) { - return NULL; - } - // check, if serial is requested - if (index == USBD_STR_SERIAL) { - pico_unique_board_id_t id; - pico_get_unique_board_id(&id); - // byte by byte conversion - for (len = 0; len < 16; len += 2) { - const char *hexdig = "0123456789abcdef"; - desc_str[1 + len] = hexdig[id.id[len >> 1] >> 4]; - desc_str[1 + len + 1] = hexdig[id.id[len >> 1] & 0x0f]; - } - } else { - const char *str = usbd_desc_str[index]; - for (len = 0; len < 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] = (TUSB_DESC_STRING << 8) | (2 * len + 2); - - return desc_str; -} diff --git a/ports/rp2/usbd.c b/ports/rp2/usbd.c new file mode 100644 index 0000000000000..e17c546c6101d --- /dev/null +++ b/ports/rp2/usbd.c @@ -0,0 +1,44 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2022 Blake W. Felt & Angus Gratton + * + * 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 "mp_usbd.h" +#include "string.h" +#include "tusb.h" +#include "pico/unique_id.h" + +void mp_usbd_port_get_serial_number(char *serial_buf) { + pico_unique_board_id_t id; + pico_get_unique_board_id(&id); + // convert to hex + int hexlen = sizeof(id.id) * 2; + MP_STATIC_ASSERT(hexlen <= USBD_DESC_STR_MAX); + for (int i = 0; i < hexlen; i += 2) { + static const char *hexdig = "0123456789abcdef"; + serial_buf[i] = hexdig[id.id[i / 2] >> 4]; + serial_buf[i + 1] = hexdig[id.id[i / 2] & 0x0f]; + } + serial_buf[hexlen] = 0; +} diff --git a/shared/runtime/tinyusb_helpers.c b/shared/tinyusb/mp_cdc_common.c similarity index 100% rename from shared/runtime/tinyusb_helpers.c rename to shared/tinyusb/mp_cdc_common.c diff --git a/ports/rp2/tusb_config.h b/shared/tinyusb/mp_usbd.c similarity index 63% rename from ports/rp2/tusb_config.h rename to shared/tinyusb/mp_usbd.c index ce321616a6bc3..91d35a2b4a07b 100644 --- a/ports/rp2/tusb_config.h +++ b/shared/tinyusb/mp_usbd.c @@ -1,7 +1,9 @@ /* + * This file is part of the MicroPython project, http://micropython.org/ + * * The MIT License (MIT) * - * Copyright (c) 2020-2021 Damien P. George + * Copyright (c) 2022 Blake W. Felt & Angus Gratton * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,25 +22,16 @@ * 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. - * */ -#ifndef MICROPY_INCLUDED_RP2_TUSB_CONFIG_H -#define MICROPY_INCLUDED_RP2_TUSB_CONFIG_H - -#include "mpconfigport.h" - -#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE) -#define CFG_TUD_CDC (1) -#define CFG_TUD_CDC_EP_BUFSIZE (256) -#define CFG_TUD_CDC_RX_BUFSIZE (256) -#define CFG_TUD_CDC_TX_BUFSIZE (256) +#include -#if MICROPY_HW_USB_MSC -// Board and hardware specific configuration -#define CFG_TUD_MSC (1) -// Set MSC EP buffer size to FatFS block size to avoid partial read/writes (offset arg). -#define CFG_TUD_MSC_BUFSIZE (MICROPY_FATFS_MAX_SS) +#ifndef NO_QSTR +#include "tusb.h" // TinyUSB is not avaiable when running the string preprocessor +#include "device/usbd.h" +#include "device/usbd_pvt.h" #endif -#endif // MICROPY_INCLUDED_RP2_TUSB_CONFIG_H +void usbd_task(void) { + tud_task_ext(0, false); +} diff --git a/shared/tinyusb/mp_usbd.h b/shared/tinyusb/mp_usbd.h new file mode 100644 index 0000000000000..2329b08b53378 --- /dev/null +++ b/shared/tinyusb/mp_usbd.h @@ -0,0 +1,39 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2022 Blake W. Felt & Angus Gratton + * + * 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. + */ + +#ifndef MICROPY_INCLUDED_LIB_UTILS_MP_USBD_H +#define MICROPY_INCLUDED_LIB_UTILS_MP_USBD_H + +#include "py/obj.h" + +// Call instead of tud_task() +void mp_usbd_task(void); + +// Function to be implemented in port code. +// Can write a string up to USBD_DESC_STR_MAX characters long, plus terminating byte. +extern void mp_usbd_port_get_serial_number(char *buf); + +#endif // MICROPY_INCLUDED_LIB_UTILS_USBD_H diff --git a/shared/tinyusb/mp_usbd_descriptor.c b/shared/tinyusb/mp_usbd_descriptor.c new file mode 100644 index 0000000000000..3a21baf5bf289 --- /dev/null +++ b/shared/tinyusb/mp_usbd_descriptor.c @@ -0,0 +1,128 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2019 Damien P. George + * Copyright (c) 2022 Blake W. Felt & Angus Gratton + * + * 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 "mpconfigport.h" +#include "tusb.h" +#include "mp_usbd.h" +#include "mp_usbd_internal.h" + +#define USBD_CDC_CMD_MAX_SIZE (8) +#define USBD_CDC_IN_OUT_MAX_SIZE (64) + +const tusb_desc_device_t mp_usbd_desc_device_static = { + .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 = MICROPY_HW_USB_VID, + .idProduct = MICROPY_HW_USB_PID, + .bcdDevice = 0x0100, + .iManufacturer = USBD_STR_MANUF, + .iProduct = USBD_STR_PRODUCT, + .iSerialNumber = USBD_STR_SERIAL, + .bNumConfigurations = 1, +}; + +const uint8_t mp_usbd_desc_cfg_static[USBD_STATIC_DESC_LEN] = { + TUD_CONFIG_DESCRIPTOR(1, USBD_ITF_STATIC_MAX, USBD_STR_0, USBD_STATIC_DESC_LEN, + 0, USBD_MAX_POWER_MA), + + #if CFG_TUD_CDC + 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), + #endif + #if CFG_TUD_MSC + TUD_MSC_DESCRIPTOR(USBD_ITF_MSC, 5, EPNUM_MSC_OUT, EPNUM_MSC_IN, 64), + #endif +}; + +const uint16_t *tud_descriptor_string_cb(uint8_t index, uint16_t langid) { + char serial_buf[USBD_DESC_STR_MAX + 1]; // Includes terminating NUL byte + static uint16_t desc_wstr[USBD_DESC_STR_MAX + 1]; // Includes prefix uint16_t + const char *desc_str; + uint16_t desc_len; + + switch (index) { + case 0: + desc_wstr[1] = 0x0409; // supported language is English + desc_len = 4; + break; + case USBD_STR_SERIAL: + // TODO: make a port-specific serial number callback + mp_usbd_port_get_serial_number(serial_buf); + desc_str = serial_buf; + break; + case USBD_STR_MANUF: + desc_str = MICROPY_HW_USB_MANUFACTURER_STRING; + break; + case USBD_STR_PRODUCT: + desc_str = MICROPY_HW_USB_PRODUCT_FS_STRING; + break; + #if CFG_TUD_CDC + case USBD_STR_CDC: + desc_str = MICROPY_HW_USB_CDC_INTERFACE_STRING; + break; + #endif + #if CFG_TUD_MSC + case USBD_STR_MSC: + desc_str = MICROPY_HW_USB_MSC_INTERFACE_STRING; + break; + #endif + default: + desc_str = NULL; + } + + if (index != 0) { + if (desc_str == NULL) { + return NULL; // Will STALL the endpoint + } + + // Convert from narrow string to wide string + desc_len = 2; + for (int i = 0; i < USBD_DESC_STR_MAX && desc_str[i] != 0; i++) { + desc_wstr[1 + i] = desc_str[i]; + desc_len += 2; + } + } + // first byte is length (including header), second byte is string type + desc_wstr[0] = (TUSB_DESC_STRING << 8) | desc_len; + + return desc_wstr; +} + + +const uint8_t *tud_descriptor_device_cb(void) { + return (const void *)&mp_usbd_desc_device_static; +} + +const uint8_t *tud_descriptor_configuration_cb(uint8_t index) { + (void)index; + return mp_usbd_desc_cfg_static; +} diff --git a/shared/tinyusb/mp_usbd_internal.h b/shared/tinyusb/mp_usbd_internal.h new file mode 100644 index 0000000000000..0b16377529566 --- /dev/null +++ b/shared/tinyusb/mp_usbd_internal.h @@ -0,0 +1,34 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2022 Angus Gratton + * + * 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. + */ +#ifndef MICROPY_INCLUDED_LIB_UTILS_USBD_INTERNAL_H +#define MICROPY_INCLUDED_LIB_UTILS_USBD_INTERNAL_H +#include "tusb.h" + +// Static USB device descriptor values +extern const tusb_desc_device_t mp_usbd_desc_device_static; +extern const uint8_t mp_usbd_desc_cfg_static[USBD_STATIC_DESC_LEN]; + +#endif // MICROPY_INCLUDED_LIB_UTILS_USBD_INTERNAL_H diff --git a/shared/tinyusb/tusb_config.h b/shared/tinyusb/tusb_config.h new file mode 100644 index 0000000000000..bde52ce9162e8 --- /dev/null +++ b/shared/tinyusb/tusb_config.h @@ -0,0 +1,133 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2020-2021 Damien P. George + * Copyright (c) 2022 Blake W. Felt & Angus Gratton + * + * 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. + * + */ + +#ifndef MICROPY_INCLUDED_LIB_UTILS_TUSB_CONFIG_H +#define MICROPY_INCLUDED_LIB_UTILS_TUSB_CONFIG_H + +#include +#include "mpconfigport.h" + +#if MICROPY_HW_ENABLE_USBDEV + +#ifndef MICROPY_HW_USB_MANUFACTURER_STRING +#define MICROPY_HW_USB_MANUFACTURER_STRING "MicroPython" +#endif + +#ifndef MICROPY_HW_USB_PRODUCT_FS_STRING +#define MICROPY_HW_USB_PRODUCT_FS_STRING "Board in FS mode" +#endif + +#ifndef MICROPY_HW_USB_CDC_INTERFACE_STRING +#define MICROPY_HW_USB_CDC_INTERFACE_STRING "Board CDC" +#endif + +#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE) + +#if MICROPY_HW_USB_CDC +#define CFG_TUD_CDC (1) +#else +#define CFG_TUD_CDC (0) +#endif + +#if MICROPY_HW_USB_MSC +#define CFG_TUD_MSC (1) +#else +#define CFG_TUD_MSC (0) +#endif + +// CDC Configuration +#if CFG_TUD_CDC +#define CFG_TUD_CDC_EP_BUFSIZE (256) +#define CFG_TUD_CDC_RX_BUFSIZE (256) +#define CFG_TUD_CDC_TX_BUFSIZE (256) +#endif + +// MSC Configuration +#if CFG_TUD_MSC +#ifndef MICROPY_HW_USB_MSC_INTERFACE_STRING +#define MICROPY_HW_USB_MSC_INTERFACE_STRING "Board MSC" +#endif +// Set MSC EP buffer size to FatFS block size to avoid partial read/writes (offset arg). +#define CFG_TUD_MSC_BUFSIZE (MICROPY_FATFS_MAX_SS) +#endif + + +// Define static descriptor size and interface count based on the above config + +#define USBD_STATIC_DESC_LEN (TUD_CONFIG_DESC_LEN + \ + (CFG_TUD_CDC ? (TUD_CDC_DESC_LEN) : 0) + \ + (CFG_TUD_MSC ? (TUD_MSC_DESC_LEN) : 0) \ + ) + +#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_MSC (0x05) + +#define USBD_MAX_POWER_MA (250) + +#define USBD_DESC_STR_MAX (20) + +#if CFG_TUD_CDC +#define USBD_ITF_CDC (0) // needs 2 interfaces +#define USBD_CDC_EP_CMD (0x81) +#define USBD_CDC_EP_OUT (0x02) +#define USBD_CDC_EP_IN (0x82) +#endif // CFG_TUD_CDC + +#if CFG_TUD_MSC +// Interface & Endpoint numbers for MSC come after CDC, if it is enabled +#if CFG_TUD_CDC +#define USBD_ITF_MSC (2) +#define EPNUM_MSC_OUT (0x03) +#define EPNUM_MSC_IN (0x83) +#else +#define USBD_ITF_MSC (0) +#define EPNUM_MSC_OUT (0x01) +#define EPNUM_MSC_IN (0x81) +#endif // CFG_TUD_CDC +#endif // CFG_TUD_MSC + +/* Limits of statically defined USB interfaces, endpoints, strings */ +#if CFG_TUD_MSC +#define USBD_ITF_STATIC_MAX (USBD_ITF_MSC + 1) +#define USBD_STR_STATIC_MAX (USBD_STR_MSC + 1) +#define USBD_EP_STATIC_MAX (EPNUM_MSC_OUT + 1) +#elif CFG_TUD_CDC +#define USBD_ITF_STATIC_MAX (USBD_ITF_CDC + 1) +#define USBD_STR_STATIC_MAX (USBD_STR_CDC + 1) +#define USBD_EP_STATIC_MAX (((EPNUM_CDC_EP_IN)&~TUSB_DIR_IN_MASK) + 1) +#else // !CFG_TUD_MSC && !CFG_TUD_CDC +#define USBD_ITF_STATIC_MAX (0) +#define USBD_STR_STATIC_MAX (0) +#define USBD_EP_STATIC_MAX (0) +#endif + +#endif // MICROPY_HW_ENABLE_USBDEV + +#endif // MICROPY_INCLUDED_LIB_UTILS_TUSB_CONFIG_H diff --git a/tools/codeformat.py b/tools/codeformat.py index 13a699065e30c..4a2cfb3367d91 100755 --- a/tools/codeformat.py +++ b/tools/codeformat.py @@ -43,6 +43,7 @@ "shared/netutils/*.[ch]", "shared/timeutils/*.[ch]", "shared/runtime/*.[ch]", + "shared/tinyusb/*.[ch]", "mpy-cross/*.[ch]", "ports/**/*.[ch]", "py/*.[ch]", pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy