From c0a4abc03c2657daadb394771e47ad1fb73332e9 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Thu, 19 Oct 2023 16:02:42 -0400 Subject: [PATCH] Fix merge bugs; remove shared/tinyusb/* --- extmod/modbinascii.c | 4 +- extmod/modzlib.c | 6 +- ports/raspberrypi/bindings/cyw43/__init__.c | 3 +- py/builtinimport.c | 3 +- py/compile.c | 2 +- py/gc.c | 17 +-- py/mpconfig.h | 46 +++++- py/obj.h | 2 +- py/objgenerator.c | 7 +- py/parse.c | 11 +- py/runtime.c | 3 +- py/runtime.h | 3 + shared-bindings/_bleio/Connection.c | 3 +- shared-bindings/microcontroller/Pin.c | 7 +- shared-bindings/socketpool/Socket.c | 4 +- shared-bindings/ssl/SSLSocket.c | 7 +- shared-bindings/wifi/Packet.c | 3 +- shared/runtime/pyexec.c | 5 +- shared/tinyusb/mp_cdc_common.c | 60 -------- shared/tinyusb/mp_usbd_descriptor.c | 133 ------------------ shared/tinyusb/mp_usbd_internal.h | 34 ----- shared/tinyusb/tusb_config.h | 146 -------------------- 22 files changed, 82 insertions(+), 427 deletions(-) delete mode 100644 shared/tinyusb/mp_cdc_common.c delete mode 100644 shared/tinyusb/mp_usbd_descriptor.c delete mode 100644 shared/tinyusb/mp_usbd_internal.h delete mode 100644 shared/tinyusb/tusb_config.h diff --git a/extmod/modbinascii.c b/extmod/modbinascii.c index 6d08ba1ad3..86183cd96b 100644 --- a/extmod/modbinascii.c +++ b/extmod/modbinascii.c @@ -32,7 +32,7 @@ #include "py/binary.h" #include "py/objstr.h" -#include "lib/uzlib/tinf.h" +//#include "lib/uzlib/tinf.h" #if MICROPY_PY_BINASCII @@ -219,7 +219,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_KW(mod_binascii_b2a_base64_obj, 1, mod_binascii_b */ #if MICROPY_PY_UBINASCII_CRC32 -#include "lib/uzlib/tinf.h" +//#include "lib/uzlib/uzlib.h" STATIC mp_obj_t mod_binascii_crc32(size_t n_args, const mp_obj_t *args) { mp_buffer_info_t bufinfo; diff --git a/extmod/modzlib.c b/extmod/modzlib.c index c2046fd93d..3984647a5d 100644 --- a/extmod/modzlib.c +++ b/extmod/modzlib.c @@ -31,10 +31,10 @@ #include "py/stream.h" #include "py/mperrno.h" -#if MICROPY_PY_UZLIB +#if MICROPY_PY_ZLIB #define UZLIB_CONF_PARANOID_CHECKS (1) -#include "lib/uzlib/tinf.h" +#include "lib/uzlib/uzlib.h" #if 0 // print debugging info #define DEBUG_printf DEBUG_printf @@ -243,4 +243,4 @@ MP_REGISTER_MODULE(MP_QSTR_zlib, mp_module_uzlib); #include "lib/uzlib/adler32.c" #include "lib/uzlib/crc32.c" -#endif // MICROPY_PY_UZLIB +#endif // MICROPY_PY_ZLIB diff --git a/ports/raspberrypi/bindings/cyw43/__init__.c b/ports/raspberrypi/bindings/cyw43/__init__.c index 7704c08790..2a7ec217f4 100644 --- a/ports/raspberrypi/bindings/cyw43/__init__.c +++ b/ports/raspberrypi/bindings/cyw43/__init__.c @@ -59,8 +59,7 @@ MP_DEFINE_CONST_OBJ_TYPE( cyw43_pin_type, MP_QSTR_CywPin, MP_TYPE_FLAG_NONE, - print, shared_bindings_microcontroller_pin_print, - unary_op, mp_generic_unary_op + print, shared_bindings_microcontroller_pin_print ); //| PM_STANDARD: int diff --git a/py/builtinimport.c b/py/builtinimport.c index a8847b7376..e2dcc3a0b1 100644 --- a/py/builtinimport.c +++ b/py/builtinimport.c @@ -31,6 +31,8 @@ #include #include "py/compile.h" +// CIRCUITPY: for gc_collect() after each import +#include "py/gc.h" #include "py/objmodule.h" #include "py/persistentcode.h" #include "py/runtime.h" @@ -136,7 +138,6 @@ STATIC mp_import_stat_t stat_top_level(qstr mod_name, vstr_t *dest) { return stat; } } - #endif // sys.path was empty or no matches, do not search the filesystem or // frozen code. diff --git a/py/compile.c b/py/compile.c index 4f91ca49b9..01df374a8d 100644 --- a/py/compile.c +++ b/py/compile.c @@ -255,7 +255,7 @@ STATIC void compile_error_set_line(compiler_t *comp, mp_parse_node_t pn) { } } -STATIC void compile_syntax_error(compiler_t *comp, mp_parse_node_t pn, mp_rom_error_text_t msg) { +STATIC void compile_syntax_error(compiler_t *comp, mp_parse_node_t pn, const compressed_string_t *msg) { // only register the error if there has been no other error if (comp->compile_error == MP_OBJ_NULL) { comp->compile_error = mp_obj_new_exception_msg(&mp_type_SyntaxError, msg); diff --git a/py/gc.c b/py/gc.c index ddc236b2e4..1fb0dd2ec7 100644 --- a/py/gc.c +++ b/py/gc.c @@ -260,14 +260,6 @@ void gc_add(void *start, void *end) { prev_area->next = area; } -// CIRCUITPY -// TODO FOR MERGE: fix this for multiple areas?? -void gc_deinit(void) { - // Run any finalisers before we stop using the heap. - gc_sweep_all(); - MP_STATIC_ASSERT(!MICROPY_GC_SPLIT_HEAP); - memset(&MP_STATE_MEM(area), 0, sizeof(MP_STATE_MEM(area))); - #if MICROPY_GC_SPLIT_HEAP_AUTO // Try to automatically add a heap area large enough to fulfill 'failed_alloc'. STATIC bool gc_try_add_heap(size_t failed_alloc) { @@ -346,6 +338,15 @@ STATIC bool gc_try_add_heap(size_t failed_alloc) { #endif +// CIRCUITPY +// TODO FOR MERGE: fix this for multiple areas?? +void gc_deinit(void) { + // Run any finalisers before we stop using the heap. + gc_sweep_all(); + MP_STATIC_ASSERT(!MICROPY_GC_SPLIT_HEAP); + memset(&MP_STATE_MEM(area), 0, sizeof(MP_STATE_MEM(area))); +} + void gc_lock(void) { // This does not need to be atomic or have the GC mutex because: // - each thread has its own gc_lock_depth so there are no races between threads; diff --git a/py/mpconfig.h b/py/mpconfig.h index 222878558d..3ae363345b 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -26,6 +26,11 @@ #ifndef MICROPY_INCLUDED_PY_MPCONFIG_H #define MICROPY_INCLUDED_PY_MPCONFIG_H +// Is this a CircuitPython build? +#ifndef CIRCUITPY +#define CIRCUITPY 0 +#endif + // In CircuitPython, version info is in genhdr/mpversion.h. #if CIRCUITPY #include "genhdr/mpversion.h" @@ -53,11 +58,6 @@ // You can override any of the options below using mpconfigport.h file // located in a directory of your port. -// Is this a CircuitPython build? -#ifndef CIRCUITPY -#define CIRCUITPY 0 -#endif - // mpconfigport.h is a file containing configuration settings for a // particular port. mpconfigport.h is actually a default name for // such config, and it can be overridden using MP_CONFIGFILE preprocessor @@ -540,6 +540,15 @@ #define MICROPY_OPT_COMPUTED_GOTO (0) #endif +// CIRCUITPY +// Whether to save trade flash space for speed in MICROPY_OPT_COMPUTED_GOTO. +// Costs about 3% speed, saves about 1500 bytes space. In addition to the assumptions +// of MICROPY_OPT_COMPUTED_GOTO, also assumes that mp_execute_bytecode is less than +// 32kB in size. +#ifndef MICROPY_OPT_COMPUTED_GOTO_SAVE_SPACE +#define MICROPY_OPT_COMPUTED_GOTO_SAVE_SPACE (0) +#endif + // Optimise the fast path for loading attributes from instance types. Increases // Thumb2 code size by about 48 bytes. #ifndef MICROPY_OPT_LOAD_ATTR_FAST_PATH @@ -597,6 +606,12 @@ #define MICROPY_HAS_FILE_READER (MICROPY_READER_POSIX || MICROPY_READER_VFS) #endif +// CIRCUITPY +// Number of VFS mounts to persist across soft-reset. +#ifndef MICROPY_FATFS_NUM_PERSISTENT +#define MICROPY_FATFS_NUM_PERSISTENT (0) +#endif + // Hook for the VM at the start of the opcode loop (can contain variable // definitions usable by the other hook functions) #ifndef MICROPY_VM_HOOK_INIT @@ -1595,6 +1610,7 @@ typedef double mp_float_t; // TODO? CIRCUITPY_ZLIB instead #ifndef MICROPY_PY_ZLIB #define MICROPY_PY_ZLIB (MICROPY_CONFIG_ROM_LEVEL_AT_LEAST_EXTRA_FEATURES) +#endif // Whether to provide "deflate" module (decompression-only by default) #ifndef MICROPY_PY_DEFLATE @@ -1977,6 +1993,19 @@ typedef double mp_float_t; #define MP_WEAK __attribute__((weak)) #endif +// CIRCUITPY +// Modifier for functions which should not be instrumented when tracing with +// -finstrument-functions +#ifndef MP_NO_INSTRUMENT +#define MP_NO_INSTRUMENT __attribute__((no_instrument_function)) +#endif + +// CIRCUITPY +// Modifier for functions which should ideally inlined +#ifndef MP_INLINE +#define MP_INLINE inline MP_NO_INSTRUMENT +#endif + // Modifier for functions which should be never inlined #ifndef MP_NOINLINE #define MP_NOINLINE __attribute__((noinline)) @@ -1997,6 +2026,13 @@ typedef double mp_float_t; #define MP_UNLIKELY(x) __builtin_expect((x), 0) #endif +// CIRCUITPY +// Modifier for functions which aren't often used. Calls will also be considered +// unlikely. Section names are `.text.unlikely` for use in linker scripts. +#ifndef MP_COLD +#define MP_COLD __attribute__((cold)) +#endif + // To annotate that code is unreachable #ifndef MP_UNREACHABLE #if defined(__GNUC__) diff --git a/py/obj.h b/py/obj.h index cf65c6a777..0ce974144a 100644 --- a/py/obj.h +++ b/py/obj.h @@ -1057,7 +1057,7 @@ mp_obj_t mp_obj_new_memoryview(byte typecode, size_t nitems, void *items); const mp_obj_type_t *mp_obj_get_type(mp_const_obj_t o_in); const char *mp_obj_get_type_str(mp_const_obj_t o_in); -CIRCUITPY +// CIRCUITPY #define mp_obj_get_type_qstr(o_in) (mp_obj_get_type((o_in))->name) bool mp_obj_is_subclass_fast(mp_const_obj_t object, mp_const_obj_t classinfo); // arguments should be type objects mp_obj_t mp_obj_cast_to_native_base(mp_obj_t self_in, mp_const_obj_t native_type); diff --git a/py/objgenerator.c b/py/objgenerator.c index ebb4a1a8ac..144a9ae627 100644 --- a/py/objgenerator.c +++ b/py/objgenerator.c @@ -103,8 +103,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_QSTR_coroutine, MP_TYPE_FLAG_BINDS_SELF, GEN_WRAP_TYPE_ATTR - call, gen_wrap_call, - unary_op, mp_generic_unary_op + call, gen_wrap_call ); #endif @@ -191,8 +190,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_QSTR_coroutine, MP_TYPE_FLAG_BINDS_SELF, GEN_WRAP_TYPE_ATTR - call, native_gen_wrap_call, - unary_op, mp_generic_unary_op + call, native_gen_wrap_call ); #endif @@ -475,7 +473,6 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_QSTR_coroutine, MP_TYPE_FLAG_ITER_IS_ITERNEXT, print, coro_instance_print, - unary_op, mp_generic_unary_op, iter, gen_instance_iternext, locals_dict, &coro_instance_locals_dict ); diff --git a/py/parse.c b/py/parse.c index 369ab9957b..72ca7c4f38 100644 --- a/py/parse.c +++ b/py/parse.c @@ -653,20 +653,11 @@ STATIC const mp_rom_map_elem_t mp_constants_table[] = { STATIC MP_DEFINE_CONST_MAP(mp_constants_map, mp_constants_table); #endif -STATIC void push_result_rule(parser_t *parser, size_t src_line, uint8_t rule_id, size_t num_args); - -#if MICROPY_COMP_CONST_FOLDING -// CIRCUITPY: The compilers mentioned below are esp-2020r3. We are using minimum esp-2021r3 (ESP-IDF v4.4). -// See https://github.com/micropython/micropython/commit/f63b4f85aae1e0ade7a7c9f908debb5905cc144d -// and https://github.com/espressif/esp-idf/issues/9130 -// So disable this for CircuitPython. -/* -#if MICROPY_COMP_CONST_FOLDING_COMPILER_WORKAROUND +#if defined(MICROPY_COMP_CONST_FOLDING_COMPILER_WORKAROUND) && MICROPY_COMP_CONST_FOLDING_COMPILER_WORKAROUND // Some versions of the xtensa-esp32-elf-gcc compiler generate wrong code if this // function is static, so provide a hook for them to work around this problem. MP_NOINLINE #endif -*/ STATIC bool fold_logical_constants(parser_t *parser, uint8_t rule_id, size_t *num_args) { if (rule_id == RULE_or_test || rule_id == RULE_and_test) { diff --git a/py/runtime.c b/py/runtime.c index 677920122e..da1f13bb6a 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -1393,7 +1393,8 @@ mp_obj_t mp_getiter(mp_obj_t o_in, mp_obj_iter_buf_t *iter_buf) { STATIC mp_fun_1_t type_get_iternext(const mp_obj_type_t *type) { if ((type->flags & MP_TYPE_FLAG_ITER_IS_STREAM) == MP_TYPE_FLAG_ITER_IS_STREAM) { - mp_obj_t mp_stream_unbuffered_iter(mp_obj_t self); + // CIRCUITPY: unneeded declaration + // mp_obj_t mp_stream_unbuffered_iter(mp_obj_t self); return mp_stream_unbuffered_iter; } else if (type->flags & MP_TYPE_FLAG_ITER_IS_ITERNEXT) { return (mp_fun_1_t)MP_OBJ_TYPE_GET_SLOT(type, iter); diff --git a/py/runtime.h b/py/runtime.h index 332acf7d94..9587022041 100644 --- a/py/runtime.h +++ b/py/runtime.h @@ -163,6 +163,9 @@ static inline void mp_globals_set(mp_obj_dict_t *d) { MP_STATE_THREAD(dict_globals) = d; } +void mp_globals_locals_set_from_nlr_jump_callback(void *ctx_in); +void mp_call_function_1_from_nlr_jump_callback(void *ctx_in); + mp_obj_t mp_load_name(qstr qst); mp_obj_t mp_load_global(qstr qst); mp_obj_t mp_load_build_class(void); diff --git a/shared-bindings/_bleio/Connection.c b/shared-bindings/_bleio/Connection.c index cec1c4a851..6f67996103 100644 --- a/shared-bindings/_bleio/Connection.c +++ b/shared-bindings/_bleio/Connection.c @@ -248,6 +248,5 @@ MP_DEFINE_CONST_OBJ_TYPE( bleio_connection_type, MP_QSTR_Connection, MP_TYPE_FLAG_NONE, - locals_dict, &bleio_connection_locals_dict, - unary_op, mp_generic_unary_op + locals_dict, &bleio_connection_locals_dict ); diff --git a/shared-bindings/microcontroller/Pin.c b/shared-bindings/microcontroller/Pin.c index 5d33b6a590..8472414550 100644 --- a/shared-bindings/microcontroller/Pin.c +++ b/shared-bindings/microcontroller/Pin.c @@ -45,7 +45,9 @@ //| """Returns a hash for the Pin.""" //| ... //| -// Provided by mp_generic_unary_op(). +// Provided inherently. +// See https://github.com/micropython/micropython/pull/10348. + static void get_pin_name(const mcu_pin_obj_t *self, qstr *package, qstr *module, qstr *name) { const mp_map_t *board_map = &board_module_globals.map; @@ -86,8 +88,7 @@ MP_DEFINE_CONST_OBJ_TYPE( mcu_pin_type, MP_QSTR_Pin, MP_TYPE_FLAG_NONE, - print, shared_bindings_microcontroller_pin_print, - unary_op, mp_generic_unary_op + print, shared_bindings_microcontroller_pin_print ); const mcu_pin_obj_t *validate_obj_is_pin(mp_obj_t obj, qstr arg_name) { diff --git a/shared-bindings/socketpool/Socket.c b/shared-bindings/socketpool/Socket.c index f4a811913a..b404c38662 100644 --- a/shared-bindings/socketpool/Socket.c +++ b/shared-bindings/socketpool/Socket.c @@ -51,7 +51,8 @@ //| def __hash__(self) -> int: //| """Returns a hash for the Socket.""" //| ... -// Provided by mp_generic_unary_op(). +// Provided inherently. +// See https://github.com/micropython/micropython/pull/10348. //| def __enter__(self) -> Socket: //| """No-op used by Context Managers.""" @@ -452,6 +453,5 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_QSTR_Socket, MP_TYPE_FLAG_NONE, locals_dict, &socketpool_socket_locals_dict, - unary_op, mp_generic_unary_op, protocol, &socket_stream_p ); diff --git a/shared-bindings/ssl/SSLSocket.c b/shared-bindings/ssl/SSLSocket.c index f2a8d8ade6..d9ffe196eb 100644 --- a/shared-bindings/ssl/SSLSocket.c +++ b/shared-bindings/ssl/SSLSocket.c @@ -48,7 +48,8 @@ //| def __hash__(self) -> int: //| """Returns a hash for the Socket.""" //| ... -// Provided by mp_generic_unary_op(). +// Provided by automatic inclusion of hash() +// https://github.com/micropython/micropython/pull/10348 //| def __enter__(self) -> SSLSocket: //| """No-op used by Context Managers.""" @@ -301,7 +302,5 @@ MP_DEFINE_CONST_OBJ_TYPE( ssl_sslsocket_type, MP_QSTR_SSLSocket, MP_TYPE_FLAG_NONE, - locals_dict, &ssl_sslsocket_locals_dict, - unary_op, mp_generic_unary_op - + locals_dict, &ssl_sslsocket_locals_dict ); diff --git a/shared-bindings/wifi/Packet.c b/shared-bindings/wifi/Packet.c index 1a5056aaf6..ef9d23b7a9 100644 --- a/shared-bindings/wifi/Packet.c +++ b/shared-bindings/wifi/Packet.c @@ -63,6 +63,5 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_QSTR_Packet, MP_TYPE_FLAG_NONE, print, wifi_packet_print, - locals_dict, &wifi_packet_locals_dict, - unary_op, mp_generic_unary_op + locals_dict, &wifi_packet_locals_dict ); diff --git a/shared/runtime/pyexec.c b/shared/runtime/pyexec.c index 1e74ccf1d8..ed35241bda 100644 --- a/shared/runtime/pyexec.c +++ b/shared/runtime/pyexec.c @@ -29,13 +29,14 @@ #include #include +#include "py/mphal.h" #include "py/compile.h" #include "py/runtime.h" #include "py/repl.h" #include "py/gc.h" #include "py/frozenmod.h" #include "py/mphal.h" -#if MICROPY_HW_ENABLE_USB +#if defined(MICROPY_HW_ENABLE_USB) && MICROPY_HW_ENABLE_USB #include "irq.h" #include "usb.h" #endif @@ -638,7 +639,7 @@ friendly_repl_reset: for (;;) { input_restart: - #if MICROPY_HW_ENABLE_USB + #if defined(MICROPY_HW_ENABLE_USB) && MICROPY_HW_ENABLE_USB if (usb_vcp_is_enabled()) { // If the user gets to here and interrupts are disabled then // they'll never see the prompt, traceback etc. The USB REPL needs diff --git a/shared/tinyusb/mp_cdc_common.c b/shared/tinyusb/mp_cdc_common.c deleted file mode 100644 index 4ccae4bac1..0000000000 --- a/shared/tinyusb/mp_cdc_common.c +++ /dev/null @@ -1,60 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2022 Ibrahim Abdelkader - * - * 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 "py/mphal.h" -#include "modmachine.h" - -#if MICROPY_HW_USB_CDC_1200BPS_TOUCH && MICROPY_HW_ENABLE_USBDEV - -#include "tusb.h" - -static mp_sched_node_t mp_bootloader_sched_node; - -STATIC void usbd_cdc_run_bootloader_task(mp_sched_node_t *node) { - mp_hal_delay_ms(250); - machine_bootloader(0, NULL); -} - -void -#if MICROPY_HW_USB_EXTERNAL_TINYUSB -mp_usbd_line_state_cb -#else -tud_cdc_line_state_cb -#endif - (uint8_t itf, bool dtr, bool rts) { - if (dtr == false && rts == false) { - // Device is disconnected. - cdc_line_coding_t line_coding; - tud_cdc_n_get_line_coding(itf, &line_coding); - if (line_coding.bit_rate == 1200) { - // Delay bootloader jump to allow the USB stack to service endpoints. - mp_sched_schedule_node(&mp_bootloader_sched_node, usbd_cdc_run_bootloader_task); - } - } -} - -#endif diff --git a/shared/tinyusb/mp_usbd_descriptor.c b/shared/tinyusb/mp_usbd_descriptor.c deleted file mode 100644 index 72a2652179..0000000000 --- a/shared/tinyusb/mp_usbd_descriptor.c +++ /dev/null @@ -1,133 +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 & 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 "py/mpconfig.h" - -#if MICROPY_HW_ENABLE_USBDEV - -#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 ((CFG_TUD_MAX_SPEED == OPT_MODE_HIGH_SPEED) ? 512 : 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[MICROPY_HW_USB_DESC_STR_MAX + 1]; // Includes terminating NUL byte - static uint16_t desc_wstr[MICROPY_HW_USB_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 < MICROPY_HW_USB_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; -} - -#endif diff --git a/shared/tinyusb/mp_usbd_internal.h b/shared/tinyusb/mp_usbd_internal.h deleted file mode 100644 index 8b8f50bae8..0000000000 --- a/shared/tinyusb/mp_usbd_internal.h +++ /dev/null @@ -1,34 +0,0 @@ -/* - * 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_SHARED_TINYUSB_MP_USBD_INTERNAL_H -#define MICROPY_INCLUDED_SHARED_TINYUSB_MP_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_SHARED_TINYUSB_MP_USBD_INTERNAL_H diff --git a/shared/tinyusb/tusb_config.h b/shared/tinyusb/tusb_config.h deleted file mode 100644 index 266cb88cc2..0000000000 --- a/shared/tinyusb/tusb_config.h +++ /dev/null @@ -1,146 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2020-2021 Damien P. George - * 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_SHARED_TINYUSB_TUSB_CONFIG_H -#define MICROPY_INCLUDED_SHARED_TINYUSB_TUSB_CONFIG_H - -#include "py/mpconfig.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 - -#ifndef MICROPY_HW_USB_MSC_INQUIRY_VENDOR_STRING -#define MICROPY_HW_USB_MSC_INQUIRY_VENDOR_STRING "MicroPy" -#endif - -#ifndef MICROPY_HW_USB_MSC_INQUIRY_PRODUCT_STRING -#define MICROPY_HW_USB_MSC_INQUIRY_PRODUCT_STRING "Mass Storage" -#endif - -#ifndef MICROPY_HW_USB_MSC_INQUIRY_REVISION_STRING -#define MICROPY_HW_USB_MSC_INQUIRY_REVISION_STRING "1.00" -#endif - -#ifndef CFG_TUSB_RHPORT0_MODE -#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE) -#endif - -#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_RX_BUFSIZE ((CFG_TUD_MAX_SPEED == OPT_MODE_HIGH_SPEED) ? 512 : 256) -#define CFG_TUD_CDC_TX_BUFSIZE ((CFG_TUD_MAX_SPEED == OPT_MODE_HIGH_SPEED) ? 512 : 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) - -#ifndef MICROPY_HW_USB_DESC_STR_MAX -#define MICROPY_HW_USB_DESC_STR_MAX (20) -#endif - -#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 + 2) -#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_SHARED_TINYUSB_TUSB_CONFIG_H