From 099d802d8cab175f3c3c619c39f728e54b8579b4 Mon Sep 17 00:00:00 2001 From: Blake Felt Date: Sat, 17 Sep 2022 20:34:09 -0600 Subject: [PATCH 1/3] shared/tinyusb: Create for reuse, rp2 only for now. --- ports/rp2/CMakeLists.txt | 9 +++- ports/rp2/main.c | 5 +++ ports/rp2/mpconfigport.h | 7 ++++ ports/rp2/usbd.c | 39 ++++++++++++++++++ {ports/rp2 => shared/tinyusb}/tusb_config.h | 9 ++-- shared/tinyusb/usbd.c | 32 +++++++++++++++ shared/tinyusb/usbd.h | 41 +++++++++++++++++++ .../tinyusb/usbd_descriptor.c | 40 ++++++++++-------- 8 files changed, 161 insertions(+), 21 deletions(-) create mode 100644 ports/rp2/usbd.c rename {ports/rp2 => shared/tinyusb}/tusb_config.h (88%) create mode 100644 shared/tinyusb/usbd.c create mode 100644 shared/tinyusb/usbd.h rename ports/rp2/tusb_port.c => shared/tinyusb/usbd_descriptor.c (85%) diff --git a/ports/rp2/CMakeLists.txt b/ports/rp2/CMakeLists.txt index 37ac78b7339f4..6037eafbe862e 100644 --- a/ports/rp2/CMakeLists.txt +++ b/ports/rp2/CMakeLists.txt @@ -96,6 +96,8 @@ set(MICROPY_SOURCE_LIB ${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/usbd.c + ${MICROPY_DIR}/shared/tinyusb/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/main.c b/ports/rp2/main.c index 1062236a3916b..572e9e42c18f2 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 "usbd.h" #include "pico/stdlib.h" #include "pico/binary_info.h" @@ -87,6 +88,7 @@ int main(int argc, char **argv) { #if MICROPY_HW_ENABLE_USBDEV bi_decl(bi_program_feature("USB REPL")) tusb_init(); + usbd_reset_all(); // run now just in case usb initialization occurs early #endif #if MICROPY_PY_THREAD @@ -159,6 +161,9 @@ int main(int argc, char **argv) { machine_pin_init(); rp2_pio_init(); machine_i2s_init0(); + #if MICROPY_HW_ENABLE_USBDEV + usbd_reset_all(); + #endif #if MICROPY_PY_BLUETOOTH mp_bluetooth_hci_init(); diff --git a/ports/rp2/mpconfigport.h b/ports/rp2/mpconfigport.h index a3725789f0848..2518a275b0bbc 100644 --- a/ports/rp2/mpconfigport.h +++ b/ports/rp2/mpconfigport.h @@ -198,6 +198,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); diff --git a/ports/rp2/usbd.c b/ports/rp2/usbd.c new file mode 100644 index 0000000000000..38c6dcdd42d4c --- /dev/null +++ b/ports/rp2/usbd.c @@ -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 + * + * 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 "usbd.h" +#include "string.h" +#include "pico/unique_id.h" + +int usbd_serialnumber(uint8_t *buf) { + pico_unique_board_id_t id; + const int len = 8; + + pico_get_unique_board_id(&id); + memcpy(buf, id.id, len); + + return len; +} diff --git a/ports/rp2/tusb_config.h b/shared/tinyusb/tusb_config.h similarity index 88% rename from ports/rp2/tusb_config.h rename to shared/tinyusb/tusb_config.h index ce321616a6bc3..c331800ef1baa 100644 --- a/ports/rp2/tusb_config.h +++ b/shared/tinyusb/tusb_config.h @@ -2,6 +2,7 @@ * The MIT License (MIT) * * Copyright (c) 2020-2021 Damien P. George + * Copyright (c) 2022 Blake W. Felt * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -22,9 +23,11 @@ * THE SOFTWARE. * */ -#ifndef MICROPY_INCLUDED_RP2_TUSB_CONFIG_H -#define MICROPY_INCLUDED_RP2_TUSB_CONFIG_H +#ifndef MICROPY_INCLUDED_LIB_UTILS_TUSB_CONFIG_H +#define MICROPY_INCLUDED_LIB_UTILS_TUSB_CONFIG_H + +#include #include "mpconfigport.h" #define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE) @@ -41,4 +44,4 @@ #define CFG_TUD_MSC_BUFSIZE (MICROPY_FATFS_MAX_SS) #endif -#endif // MICROPY_INCLUDED_RP2_TUSB_CONFIG_H +#endif // MICROPY_INCLUDED_LIB_UTILS_TUSB_CONFIG_H diff --git a/shared/tinyusb/usbd.c b/shared/tinyusb/usbd.c new file mode 100644 index 0000000000000..01e04b9462e20 --- /dev/null +++ b/shared/tinyusb/usbd.c @@ -0,0 +1,32 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2022 Blake W. Felt + * + * 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 "py/runtime.h" +#include "usbd.h" + +void usbd_reset_all(void) { + usbd_reset_descriptor(); +} diff --git a/shared/tinyusb/usbd.h b/shared/tinyusb/usbd.h new file mode 100644 index 0000000000000..7d939513333dd --- /dev/null +++ b/shared/tinyusb/usbd.h @@ -0,0 +1,41 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2022 Blake W. Felt + * + * 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_H +#define MICROPY_INCLUDED_LIB_UTILS_USBD_H + +#include "py/obj.h" + +// defined externally (needed per port) + +int usbd_serialnumber(uint8_t *buf); + +// external use + +void usbd_reset_all(void); +void usbd_reset_descriptor(void); + +#endif // MICROPY_INCLUDED_LIB_UTILS_USBD_H diff --git a/ports/rp2/tusb_port.c b/shared/tinyusb/usbd_descriptor.c similarity index 85% rename from ports/rp2/tusb_port.c rename to shared/tinyusb/usbd_descriptor.c index 4aac08791e94e..d3c65aee8aa37 100644 --- a/ports/rp2/tusb_port.c +++ b/shared/tinyusb/usbd_descriptor.c @@ -4,6 +4,7 @@ * The MIT License (MIT) * * Copyright (c) 2019 Damien P. George + * Copyright (c) 2022 Blake W. Felt * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -24,15 +25,9 @@ * THE SOFTWARE. */ +#include "mpconfigport.h" #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 +#include "usbd.h" #if CFG_TUD_MSC #define USBD_DESC_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN + TUD_MSC_DESC_LEN) @@ -65,6 +60,9 @@ #define USBD_STR_CDC (0x04) #define USBD_STR_MSC (0x05) +#define USBD_DESC_STR_MAX (20) +#define USBD_DESC_SERIAL_MAX (32) + // Note: descriptors returned from callbacks must exist long enough for transfer to complete static const tusb_desc_device_t usbd_desc_device = { @@ -115,8 +113,7 @@ const uint8_t *tud_descriptor_configuration_cb(uint8_t index) { } 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]; + static uint16_t desc_str[USBD_DESC_STR_MAX]; uint8_t len; if (index == 0) { @@ -128,17 +125,22 @@ const uint16_t *tud_descriptor_string_cb(uint8_t index, uint16_t langid) { } // check, if serial is requested if (index == USBD_STR_SERIAL) { - pico_unique_board_id_t id; - pico_get_unique_board_id(&id); + uint8_t buffer[USBD_DESC_SERIAL_MAX] = {0}; + int buflen; + const char *hexdig = "0123456789abcdef"; + + buflen = usbd_serialnumber(buffer); // 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]; + len = 0; + for (int i=0; i> 4]; + desc_str[2 + len] = hexdig[val & 0x0F]; + len += 2; } } else { const char *str = usbd_desc_str[index]; - for (len = 0; len < DESC_STR_MAX - 1 && str[len]; ++len) { + for (len = 0; len < USBD_DESC_STR_MAX - 1 && str[len]; ++len) { desc_str[1 + len] = str[len]; } } @@ -149,3 +151,7 @@ const uint16_t *tud_descriptor_string_cb(uint8_t index, uint16_t langid) { return desc_str; } + +void usbd_reset_descriptor(void) { + // not used yet +} From fc357440662257b560710eb5573b9766eb7265ed Mon Sep 17 00:00:00 2001 From: Angus Gratton Date: Tue, 4 Oct 2022 10:25:56 +1100 Subject: [PATCH 2/3] tinyusb: More refactoring of static USB device implementation. App the mp_ prefix to usbd_ symbols and files which are defined here and not in TinyUSB. rp2 only for now. This includes some groundwork for dynamic USB devices (defined in Python). This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton --- ports/nrf/Makefile | 2 +- ports/rp2/CMakeLists.txt | 6 +- ports/rp2/main.c | 6 +- ports/rp2/mpconfigport.h | 2 +- ports/rp2/usbd.c | 21 ++- .../mp_cdc_common.c} | 0 shared/tinyusb/{usbd.c => mp_usbd.c} | 15 +- shared/tinyusb/{usbd.h => mp_usbd.h} | 18 +- shared/tinyusb/mp_usbd_descriptor.c | 124 ++++++++++++++ shared/tinyusb/mp_usbd_internal.h | 34 ++++ shared/tinyusb/tusb_config.h | 65 +++++++- shared/tinyusb/usbd_descriptor.c | 157 ------------------ tools/codeformat.py | 1 + 13 files changed, 258 insertions(+), 193 deletions(-) rename shared/{runtime/tinyusb_helpers.c => tinyusb/mp_cdc_common.c} (100%) rename shared/tinyusb/{usbd.c => mp_usbd.c} (80%) rename shared/tinyusb/{usbd.h => mp_usbd.h} (76%) create mode 100644 shared/tinyusb/mp_usbd_descriptor.c create mode 100644 shared/tinyusb/mp_usbd_internal.h delete mode 100644 shared/tinyusb/usbd_descriptor.c 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 6037eafbe862e..15264bf8ad614 100644 --- a/ports/rp2/CMakeLists.txt +++ b/ports/rp2/CMakeLists.txt @@ -94,10 +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/usbd.c - ${MICROPY_DIR}/shared/tinyusb/usbd_descriptor.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 diff --git a/ports/rp2/main.c b/ports/rp2/main.c index 572e9e42c18f2..8983f0781650e 100644 --- a/ports/rp2/main.c +++ b/ports/rp2/main.c @@ -43,7 +43,7 @@ #include "modrp2.h" #include "mpbthciport.h" #include "genhdr/mpversion.h" -#include "usbd.h" +#include "mp_usbd.h" #include "pico/stdlib.h" #include "pico/binary_info.h" @@ -88,7 +88,6 @@ int main(int argc, char **argv) { #if MICROPY_HW_ENABLE_USBDEV bi_decl(bi_program_feature("USB REPL")) tusb_init(); - usbd_reset_all(); // run now just in case usb initialization occurs early #endif #if MICROPY_PY_THREAD @@ -161,9 +160,6 @@ int main(int argc, char **argv) { machine_pin_init(); rp2_pio_init(); machine_i2s_init0(); - #if MICROPY_HW_ENABLE_USBDEV - usbd_reset_all(); - #endif #if MICROPY_PY_BLUETOOTH mp_bluetooth_hci_init(); diff --git a/ports/rp2/mpconfigport.h b/ports/rp2/mpconfigport.h index 2518a275b0bbc..b58d7bed0d844 100644 --- a/ports/rp2/mpconfigport.h +++ b/ports/rp2/mpconfigport.h @@ -217,7 +217,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/usbd.c b/ports/rp2/usbd.c index 38c6dcdd42d4c..e17c546c6101d 100644 --- a/ports/rp2/usbd.c +++ b/ports/rp2/usbd.c @@ -3,7 +3,7 @@ * * The MIT License (MIT) * - * Copyright (c) 2022 Blake W. Felt + * 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 @@ -24,16 +24,21 @@ * THE SOFTWARE. */ -#include "usbd.h" +#include "mp_usbd.h" #include "string.h" +#include "tusb.h" #include "pico/unique_id.h" -int usbd_serialnumber(uint8_t *buf) { +void mp_usbd_port_get_serial_number(char *serial_buf) { pico_unique_board_id_t id; - const int len = 8; - pico_get_unique_board_id(&id); - memcpy(buf, id.id, len); - - return len; + // 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/shared/tinyusb/usbd.c b/shared/tinyusb/mp_usbd.c similarity index 80% rename from shared/tinyusb/usbd.c rename to shared/tinyusb/mp_usbd.c index 01e04b9462e20..91d35a2b4a07b 100644 --- a/shared/tinyusb/usbd.c +++ b/shared/tinyusb/mp_usbd.c @@ -3,7 +3,7 @@ * * The MIT License (MIT) * - * Copyright (c) 2022 Blake W. Felt + * 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 @@ -24,9 +24,14 @@ * THE SOFTWARE. */ -#include "py/runtime.h" -#include "usbd.h" +#include -void usbd_reset_all(void) { - usbd_reset_descriptor(); +#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 + +void usbd_task(void) { + tud_task_ext(0, false); } diff --git a/shared/tinyusb/usbd.h b/shared/tinyusb/mp_usbd.h similarity index 76% rename from shared/tinyusb/usbd.h rename to shared/tinyusb/mp_usbd.h index 7d939513333dd..2329b08b53378 100644 --- a/shared/tinyusb/usbd.h +++ b/shared/tinyusb/mp_usbd.h @@ -3,7 +3,7 @@ * * The MIT License (MIT) * - * Copyright (c) 2022 Blake W. Felt + * 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 @@ -24,18 +24,16 @@ * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_LIB_UTILS_USBD_H -#define MICROPY_INCLUDED_LIB_UTILS_USBD_H +#ifndef MICROPY_INCLUDED_LIB_UTILS_MP_USBD_H +#define MICROPY_INCLUDED_LIB_UTILS_MP_USBD_H #include "py/obj.h" -// defined externally (needed per port) +// Call instead of tud_task() +void mp_usbd_task(void); -int usbd_serialnumber(uint8_t *buf); - -// external use - -void usbd_reset_all(void); -void usbd_reset_descriptor(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..1e9cc9876ef3a --- /dev/null +++ b/shared/tinyusb/mp_usbd_descriptor.c @@ -0,0 +1,124 @@ +/* + * 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), + + 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 +}; + +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; + case USBD_STR_CDC: + desc_str = MICROPY_HW_USB_CDC_INTERFACE_STRING; + break; + #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 index c331800ef1baa..e135ca6631574 100644 --- a/shared/tinyusb/tusb_config.h +++ b/shared/tinyusb/tusb_config.h @@ -2,7 +2,7 @@ * The MIT License (MIT) * * Copyright (c) 2020-2021 Damien P. George - * Copyright (c) 2022 Blake W. Felt + * 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 @@ -30,6 +30,20 @@ #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) #define CFG_TUD_CDC (1) @@ -37,11 +51,56 @@ #define CFG_TUD_CDC_RX_BUFSIZE (256) #define CFG_TUD_CDC_TX_BUFSIZE (256) -#if MICROPY_HW_USB_MSC -// Board and hardware specific configuration +// MSC Configuration +#if CFG_TUD_MSC +#ifndef MICROPY_HW_USB_MSC_INTERFACE_STRING +#define MICROPY_HW_USB_MSC_INTERFACE_STRING "Board MSC" +#endif #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) #endif +// Define static descriptor size and interface count based on the above config + +#if CFG_TUD_MSC +#define USBD_STATIC_DESC_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN + TUD_MSC_DESC_LEN) +#else +#define USBD_STATIC_DESC_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN) +#endif + +#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) + +#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) + +#if CFG_TUD_MSC +#define USBD_ITF_MSC (2) +#define EPNUM_MSC_OUT (0x03) +#define EPNUM_MSC_IN (0x83) +#endif + +/* Limits of statically defined USB interfaces, endpoints, strings */ +#if CFG_TUD_MSC +#define USBD_ITF_STATIC_MAX (3) +#define USBD_STR_STATIC_MAX (USBD_STR_MSC + 1) +#define USBD_EP_STATIC_MAX (0x04) // ignoring the IN EP flag 0x80 +#else +#define USBD_ITF_STATIC_MAX (2) +#define USBD_STR_STATIC_MAX (USBD_STR_CDC + 1) +#define USBD_EP_STATIC_MAX (0x03) // ignoring the IN EP flag 0x80 +#endif + #endif // MICROPY_INCLUDED_LIB_UTILS_TUSB_CONFIG_H diff --git a/shared/tinyusb/usbd_descriptor.c b/shared/tinyusb/usbd_descriptor.c deleted file mode 100644 index d3c65aee8aa37..0000000000000 --- a/shared/tinyusb/usbd_descriptor.c +++ /dev/null @@ -1,157 +0,0 @@ -/* - * 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 - * - * 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 "usbd.h" - -#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) - -#define USBD_DESC_STR_MAX (20) -#define USBD_DESC_SERIAL_MAX (32) - -// 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) { - static uint16_t desc_str[USBD_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) { - uint8_t buffer[USBD_DESC_SERIAL_MAX] = {0}; - int buflen; - const char *hexdig = "0123456789abcdef"; - - buflen = usbd_serialnumber(buffer); - // byte by byte conversion - len = 0; - for (int i=0; i> 4]; - desc_str[2 + len] = hexdig[val & 0x0F]; - len += 2; - } - } else { - 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] = (TUSB_DESC_STRING << 8) | (2 * len + 2); - - return desc_str; -} - -void usbd_reset_descriptor(void) { - // not used yet -} 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]", From 4578b04a05b8603eba2e33bb4217b561cd4fd309 Mon Sep 17 00:00:00 2001 From: Angus Gratton Date: Wed, 2 Nov 2022 15:21:21 +1100 Subject: [PATCH 3/3] rp2: Allow enabling USB device without enabling USB-CDC. Requires changing the USB-CDC stdin/stdout guards from MICROPY_HW_ENABLE_USBDEV to the new (in this port) MICROPY_HW_USB_CDC. --- ports/rp2/boards/PICO/mpconfigboard.h | 3 -- ports/rp2/main.c | 2 + ports/rp2/mpconfigport.h | 19 ++++++--- ports/rp2/mphalport.c | 12 +++--- shared/tinyusb/mp_usbd_descriptor.c | 4 ++ shared/tinyusb/tusb_config.h | 59 +++++++++++++++++++-------- 6 files changed, 69 insertions(+), 30 deletions(-) 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 8983f0781650e..fdcdd90d4c303 100644 --- a/ports/rp2/main.c +++ b/ports/rp2/main.c @@ -86,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 b58d7bed0d844..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) 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/shared/tinyusb/mp_usbd_descriptor.c b/shared/tinyusb/mp_usbd_descriptor.c index 1e9cc9876ef3a..3a21baf5bf289 100644 --- a/shared/tinyusb/mp_usbd_descriptor.c +++ b/shared/tinyusb/mp_usbd_descriptor.c @@ -54,8 +54,10 @@ 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 @@ -83,9 +85,11 @@ const uint16_t *tud_descriptor_string_cb(uint8_t index, uint16_t langid) { 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; diff --git a/shared/tinyusb/tusb_config.h b/shared/tinyusb/tusb_config.h index e135ca6631574..bde52ce9162e8 100644 --- a/shared/tinyusb/tusb_config.h +++ b/shared/tinyusb/tusb_config.h @@ -46,28 +46,41 @@ #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 -#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) +#define CFG_TUD_MSC_BUFSIZE (MICROPY_FATFS_MAX_SS) #endif + // Define static descriptor size and interface count based on the above config -#if CFG_TUD_MSC -#define USBD_STATIC_DESC_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN + TUD_MSC_DESC_LEN) -#else -#define USBD_STATIC_DESC_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN) -#endif +#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) @@ -80,27 +93,41 @@ #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) -#endif +#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 (3) +#define USBD_ITF_STATIC_MAX (USBD_ITF_MSC + 1) #define USBD_STR_STATIC_MAX (USBD_STR_MSC + 1) -#define USBD_EP_STATIC_MAX (0x04) // ignoring the IN EP flag 0x80 -#else -#define USBD_ITF_STATIC_MAX (2) +#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 (0x03) // ignoring the IN EP flag 0x80 +#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