diff --git a/lib/tinyusb b/lib/tinyusb index f1e006d09b..6c7c9f2ef5 160000 --- a/lib/tinyusb +++ b/lib/tinyusb @@ -1 +1 @@ -Subproject commit f1e006d09bd32088ab421d0b519eb89c531eda4e +Subproject commit 6c7c9f2ef5a80d5a6879e9c3558162188c6cf889 diff --git a/locale/de_DE.po b/locale/de_DE.po index 41d873e7ff..e706cb81c5 100644 --- a/locale/de_DE.po +++ b/locale/de_DE.po @@ -6,7 +6,7 @@ msgstr "" "Project-Id-Version: \n" "Report-Msgid-Bugs-To: \n" "POT-Creation-Date: 2021-01-04 12:55-0600\n" -"PO-Revision-Date: 2023-07-10 14:50+0000\n" +"PO-Revision-Date: 2023-07-19 21:06+0000\n" "Last-Translator: Luc \n" "Language: de_DE\n" "MIME-Version: 1.0\n" @@ -487,7 +487,7 @@ msgstr "Alle Kanäle werden verwendet" #: ports/raspberrypi/common-hal/usb_host/Port.c msgid "All dma channels in use" -msgstr "" +msgstr "All DMA-Kanäle in Verwendung" #: ports/atmel-samd/common-hal/audioio/AudioOut.c msgid "All event channels in use" diff --git a/locale/en_GB.po b/locale/en_GB.po index c4d1039de7..02d8748cf8 100644 --- a/locale/en_GB.po +++ b/locale/en_GB.po @@ -7,7 +7,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"PO-Revision-Date: 2023-05-23 22:55+0000\n" +"PO-Revision-Date: 2023-07-19 21:06+0000\n" "Last-Translator: Andi Chandler \n" "Language-Team: none\n" "Language: en_GB\n" @@ -15,7 +15,7 @@ msgstr "" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" "Plural-Forms: nplurals=2; plural=n != 1;\n" -"X-Generator: Weblate 4.18-dev\n" +"X-Generator: Weblate 5.0-dev\n" #: main.c msgid "" @@ -486,7 +486,7 @@ msgstr "All channels in use" #: ports/raspberrypi/common-hal/usb_host/Port.c msgid "All dma channels in use" -msgstr "" +msgstr "All DMA channels in use" #: ports/atmel-samd/common-hal/audioio/AudioOut.c msgid "All event channels in use" @@ -1378,7 +1378,7 @@ msgstr "Mismatched swap flag" #: ports/mimxrt10xx/common-hal/busio/SPI.c msgid "Missing MISO or MOSI Pin" -msgstr "Missing MISO or MOSI pin" +msgstr "Missing MISO or MOSI Pin" #: ports/stm/common-hal/busio/SPI.c msgid "Missing MISO or MOSI pin" @@ -1493,7 +1493,7 @@ msgstr "No IP" #: ports/espressif/common-hal/busio/SPI.c #: ports/mimxrt10xx/common-hal/busio/SPI.c msgid "No MISO Pin" -msgstr "No MISO pin" +msgstr "No MISO Pin" #: ports/stm/common-hal/busio/SPI.c shared-module/bitbangio/SPI.c msgid "No MISO pin" @@ -1502,7 +1502,7 @@ msgstr "No MISO pin" #: ports/espressif/common-hal/busio/SPI.c #: ports/mimxrt10xx/common-hal/busio/SPI.c msgid "No MOSI Pin" -msgstr "No MOSI pin" +msgstr "No MOSI Pin" #: ports/stm/common-hal/busio/SPI.c shared-module/bitbangio/SPI.c msgid "No MOSI pin" diff --git a/locale/zh_Latn_pinyin.po b/locale/zh_Latn_pinyin.po index 4f56b14dfd..8ff874492e 100644 --- a/locale/zh_Latn_pinyin.po +++ b/locale/zh_Latn_pinyin.po @@ -7,7 +7,7 @@ msgstr "" "Project-Id-Version: circuitpython-cn\n" "Report-Msgid-Bugs-To: \n" "POT-Creation-Date: 2021-01-04 12:55-0600\n" -"PO-Revision-Date: 2023-07-16 04:32+0000\n" +"PO-Revision-Date: 2023-07-21 00:10+0000\n" "Last-Translator: hexthat \n" "Language-Team: Chinese Hanyu Pinyin\n" "Language: zh_Latn_pinyin\n" @@ -1641,7 +1641,7 @@ msgstr "Bù zhīchí jīshù" #: supervisor/shared/bluetooth/bluetooth.c msgid "Off" -msgstr "guānbì" +msgstr "Guānbì" #: supervisor/shared/bluetooth/bluetooth.c msgid "Ok" @@ -3286,7 +3286,7 @@ msgstr "bù zhèngquè de tiánchōng" #: extmod/ulab/code/ndarray.c extmod/ulab/code/numpy/transform.c msgid "index is out of bounds" -msgstr "suǒyǐn chāochū fànwéi" +msgstr "suǒyǐn yuèjiè" #: shared-bindings/_pixelmap/PixelMap.c msgid "index must be tuple or int" diff --git a/ports/atmel-samd/boards/mini_sam_m4/mpconfigboard.mk b/ports/atmel-samd/boards/mini_sam_m4/mpconfigboard.mk index c3e21a4c36..fcbbbd5bf9 100644 --- a/ports/atmel-samd/boards/mini_sam_m4/mpconfigboard.mk +++ b/ports/atmel-samd/boards/mini_sam_m4/mpconfigboard.mk @@ -12,6 +12,7 @@ LONGINT_IMPL = MPZ # No I2S on SAMD51G CIRCUITPY_AUDIOBUSIO = 0 +CIRCUITPY_SYNTHIO = 0 CIRCUITPY_BITBANG_APA102 = 1 diff --git a/ports/espressif/boards/unexpectedmaker_feathers3/mpconfigboard.h b/ports/espressif/boards/unexpectedmaker_feathers3/mpconfigboard.h index 67054b8664..1d51cb77d9 100644 --- a/ports/espressif/boards/unexpectedmaker_feathers3/mpconfigboard.h +++ b/ports/espressif/boards/unexpectedmaker_feathers3/mpconfigboard.h @@ -34,8 +34,9 @@ #define MICROPY_HW_LED_STATUS (&pin_GPIO13) -#define DEFAULT_I2C_BUS_SCL (&pin_GPIO9) -#define DEFAULT_I2C_BUS_SDA (&pin_GPIO8) +#define CIRCUITPY_BOARD_I2C (2) +#define CIRCUITPY_BOARD_I2C_PIN {{.scl = &pin_GPIO9, .sda = &pin_GPIO8}, \ + {.scl = &pin_GPIO15, .sda = &pin_GPIO16}} #define DEFAULT_SPI_BUS_SCK (&pin_GPIO36) #define DEFAULT_SPI_BUS_MOSI (&pin_GPIO35) diff --git a/ports/espressif/boards/unexpectedmaker_feathers3/pins.c b/ports/espressif/boards/unexpectedmaker_feathers3/pins.c index 9a2fde5a0c..5cdfc69ed2 100644 --- a/ports/espressif/boards/unexpectedmaker_feathers3/pins.c +++ b/ports/espressif/boards/unexpectedmaker_feathers3/pins.c @@ -1,5 +1,7 @@ #include "shared-bindings/board/__init__.h" +CIRCUITPY_BOARD_BUS_SINGLETON(stemma_i2c, i2c, 1) + STATIC const mp_rom_map_elem_t board_module_globals_table[] = { CIRCUITPYTHON_BOARD_DICT_STANDARD_ITEMS @@ -89,11 +91,16 @@ STATIC const mp_rom_map_elem_t board_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_D13), MP_ROM_PTR(&pin_GPIO11) }, { MP_ROM_QSTR(MP_QSTR_A10), MP_ROM_PTR(&pin_GPIO11) }, - { MP_ROM_QSTR(MP_QSTR_LED), MP_ROM_PTR(&pin_GPIO13) }, // Blue LED + // Blue LED + { MP_ROM_QSTR(MP_QSTR_LED), MP_ROM_PTR(&pin_GPIO13) }, + + // STEMMA QT Vertical Connector I2C IO + { MP_ROM_QSTR(MP_QSTR_IO15), MP_ROM_PTR(&pin_GPIO15) }, + { MP_ROM_QSTR(MP_QSTR_SCL2), MP_ROM_PTR(&pin_GPIO15) }, + { MP_ROM_QSTR(MP_QSTR_IO16), MP_ROM_PTR(&pin_GPIO16) }, + { MP_ROM_QSTR(MP_QSTR_SDA2), MP_ROM_PTR(&pin_GPIO16) }, // Battery voltage sense pin - // I really don't know what name to use here. Adafruit use BATTERY & VOLTAGE_MONITOR - // I prefer VBAT or VBAT_SENSE { MP_ROM_QSTR(MP_QSTR_BATTERY), MP_ROM_PTR(&pin_GPIO2) }, { MP_ROM_QSTR(MP_QSTR_VBAT), MP_ROM_PTR(&pin_GPIO2) }, { MP_ROM_QSTR(MP_QSTR_VBAT_SENSE), MP_ROM_PTR(&pin_GPIO2) }, @@ -103,18 +110,29 @@ STATIC const mp_rom_map_elem_t board_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_VBUS), MP_ROM_PTR(&pin_GPIO34) }, { MP_ROM_QSTR(MP_QSTR_VBUS_SENSE), MP_ROM_PTR(&pin_GPIO34) }, + // Neopixel pins { MP_ROM_QSTR(MP_QSTR_NEOPIXEL_POWER), MP_ROM_PTR(&pin_GPIO39) }, { MP_ROM_QSTR(MP_QSTR_NEOPIXEL), MP_ROM_PTR(&pin_GPIO40) }, - { MP_ROM_QSTR(MP_QSTR_IO4), MP_ROM_PTR(&pin_GPIO4) }, // Ambient Light Sensor - { MP_ROM_QSTR(MP_QSTR_AMB), MP_ROM_PTR(&pin_GPIO4) }, // Ambient Light Sensor + // Ambient Light Sensor + { MP_ROM_QSTR(MP_QSTR_IO4), MP_ROM_PTR(&pin_GPIO4) }, + { MP_ROM_QSTR(MP_QSTR_AMB), MP_ROM_PTR(&pin_GPIO4) }, - { MP_ROM_QSTR(MP_QSTR_LDO2), MP_ROM_PTR(&pin_GPIO39) }, // Second LDO Enable control - { MP_ROM_QSTR(MP_QSTR_IO39), MP_ROM_PTR(&pin_GPIO39) }, // Second LDO Enable control + // Second LDO Enable control + { MP_ROM_QSTR(MP_QSTR_LDO2), MP_ROM_PTR(&pin_GPIO39) }, + { MP_ROM_QSTR(MP_QSTR_IO39), MP_ROM_PTR(&pin_GPIO39) }, + // I2C { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, { MP_ROM_QSTR(MP_QSTR_STEMMA_I2C), MP_ROM_PTR(&board_i2c_obj) }, + { MP_ROM_QSTR(MP_QSTR_STEMMA_VERTICAL_I2C), MP_ROM_PTR(&board_stemma_i2c_obj) }, + { MP_ROM_QSTR(MP_QSTR_STEMMA_I2C2), MP_ROM_PTR(&board_stemma_i2c_obj) }, + { MP_ROM_QSTR(MP_QSTR_I2C2), MP_ROM_PTR(&board_stemma_i2c_obj) }, + + // SPI { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&board_spi_obj) }, + + // UART { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&board_uart_obj) }, }; MP_DEFINE_CONST_DICT(board_module_globals, board_module_globals_table); diff --git a/ports/mimxrt10xx/boards/imxrt1060_evk/board.c b/ports/mimxrt10xx/boards/imxrt1060_evk/board.c index 4a2e6e0913..12278acabb 100644 --- a/ports/mimxrt10xx/boards/imxrt1060_evk/board.c +++ b/ports/mimxrt10xx/boards/imxrt1060_evk/board.c @@ -28,6 +28,8 @@ #include "supervisor/board.h" #include "shared-bindings/microcontroller/Pin.h" +#include "shared-bindings/usb_host/Port.h" + // These pins should never ever be reset; doing so could interfere with basic operation. // Used in common-hal/microcontroller/Pin.c const mcu_pin_obj_t *mimxrt10xx_reset_forbidden_pins[] = { @@ -55,4 +57,8 @@ const mcu_pin_obj_t *mimxrt10xx_reset_forbidden_pins[] = { NULL, // Must end in NULL. }; +void board_init(void) { + common_hal_usb_host_port_construct(&pin_USB_OTG2_DP, &pin_USB_OTG2_DN); +} + // Use the MP_WEAK supervisor/shared/board.c versions of routines not defined here. diff --git a/ports/mimxrt10xx/boards/imxrt1060_evkb/board.c b/ports/mimxrt10xx/boards/imxrt1060_evkb/board.c index 4a2e6e0913..26dd28cbb4 100644 --- a/ports/mimxrt10xx/boards/imxrt1060_evkb/board.c +++ b/ports/mimxrt10xx/boards/imxrt1060_evkb/board.c @@ -28,6 +28,8 @@ #include "supervisor/board.h" #include "shared-bindings/microcontroller/Pin.h" +#include "shared-bindings/usb_host/Port.h" + // These pins should never ever be reset; doing so could interfere with basic operation. // Used in common-hal/microcontroller/Pin.c const mcu_pin_obj_t *mimxrt10xx_reset_forbidden_pins[] = { @@ -52,7 +54,11 @@ const mcu_pin_obj_t *mimxrt10xx_reset_forbidden_pins[] = { // USB Pins &pin_GPIO_AD_B0_01, // ID Pin &pin_GPIO_AD_B0_03, // OC/Fault Pin - NULL, // Must end in NULL. + NULL, // Must end in NULL. }; +void board_init(void) { + common_hal_usb_host_port_construct(&pin_USB_OTG2_DP, &pin_USB_OTG2_DN); +} + // Use the MP_WEAK supervisor/shared/board.c versions of routines not defined here. diff --git a/ports/mimxrt10xx/boards/teensy41/board.c b/ports/mimxrt10xx/boards/teensy41/board.c index 8ece1546d7..3418cff43f 100644 --- a/ports/mimxrt10xx/boards/teensy41/board.c +++ b/ports/mimxrt10xx/boards/teensy41/board.c @@ -28,6 +28,8 @@ #include "supervisor/board.h" #include "shared-bindings/microcontroller/Pin.h" +#include "shared-bindings/usb_host/Port.h" + // These pins should never ever be reset; doing so could interfere with basic operation. // Used in common-hal/microcontroller/Pin.c const mcu_pin_obj_t *mimxrt10xx_reset_forbidden_pins[] = { @@ -54,4 +56,8 @@ const mcu_pin_obj_t *mimxrt10xx_reset_forbidden_pins[] = { NULL, // Must end in NULL. }; +void board_init(void) { + common_hal_usb_host_port_construct(&pin_USB_OTG2_DP, &pin_USB_OTG2_DN); +} + // Use the MP_WEAK supervisor/shared/board.c versions of routines not defined here. diff --git a/ports/mimxrt10xx/common-hal/usb_host/Port.c b/ports/mimxrt10xx/common-hal/usb_host/Port.c index 126160c69c..444c410729 100644 --- a/ports/mimxrt10xx/common-hal/usb_host/Port.c +++ b/ports/mimxrt10xx/common-hal/usb_host/Port.c @@ -29,9 +29,13 @@ #include "py/runtime.h" -bool usb_host_init; +#include "tusb.h" -void common_hal_usb_host_port_construct(usb_host_port_obj_t *self, const mcu_pin_obj_t *dp, const mcu_pin_obj_t *dm) { +#include "imx_usb.h" + +usb_host_port_obj_t usb_host_instance; + +usb_host_port_obj_t *common_hal_usb_host_port_construct(const mcu_pin_obj_t *dp, const mcu_pin_obj_t *dm) { const mcu_pin_obj_t *supported_dp; const mcu_pin_obj_t *supported_dm; if (CIRCUITPY_USB_HOST_INSTANCE == 0) { @@ -41,18 +45,27 @@ void common_hal_usb_host_port_construct(usb_host_port_obj_t *self, const mcu_pin supported_dp = &pin_USB_OTG2_DP; supported_dm = &pin_USB_OTG2_DN; } + // Return the singleton if given the same pins. + usb_host_port_obj_t *self = &usb_host_instance; + if (self->dp != NULL) { + if (self->dp != dp || self->dm != dm) { + mp_raise_msg_varg(&mp_type_RuntimeError, translate("%q in use"), MP_QSTR_usb_host); + } + return self; + } if (dp != supported_dp || dm != supported_dm) { raise_ValueError_invalid_pins(); } - self->init = true; - usb_host_init = true; -} -void common_hal_usb_host_port_deinit(usb_host_port_obj_t *self) { - self->init = false; - usb_host_init = false; -} + assert_pin_free(dp); + assert_pin_free(dm); -bool common_hal_usb_host_port_deinited(usb_host_port_obj_t *self) { - return !self->init; + init_usb_instance(CIRCUITPY_USB_HOST_INSTANCE); + tuh_init(TUH_OPT_RHPORT); + + self->base.type = &usb_host_port_type; + self->dp = dp; + self->dm = dm; + + return self; } diff --git a/ports/mimxrt10xx/common-hal/usb_host/Port.h b/ports/mimxrt10xx/common-hal/usb_host/Port.h index dad1adf3a2..59f7735439 100644 --- a/ports/mimxrt10xx/common-hal/usb_host/Port.h +++ b/ports/mimxrt10xx/common-hal/usb_host/Port.h @@ -31,11 +31,8 @@ typedef struct { mp_obj_base_t base; - bool init; + const mcu_pin_obj_t *dp; + const mcu_pin_obj_t *dm; } usb_host_port_obj_t; -// Cheater state so that the usb module knows if it should return the TinyUSB -// state. -extern bool usb_host_init; - #endif // MICROPY_INCLUDED_MIMXRT10XX_COMMON_HAL_USB_HOST_PORT_H diff --git a/ports/mimxrt10xx/imx_usb.h b/ports/mimxrt10xx/imx_usb.h new file mode 100644 index 0000000000..7ae7df8461 --- /dev/null +++ b/ports/mimxrt10xx/imx_usb.h @@ -0,0 +1,31 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2022 Scott Shawcroft for Adafruit Industries + * + * 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. + */ + +#pragma once + +// Provided by supervisor/usb.c that has a shared, non-port-specific header. So, +// just define it here. +void init_usb_instance(mp_int_t instance); diff --git a/ports/mimxrt10xx/linking/common.ld b/ports/mimxrt10xx/linking/common.ld index 71e2c457c3..6e674a49f2 100644 --- a/ports/mimxrt10xx/linking/common.ld +++ b/ports/mimxrt10xx/linking/common.ld @@ -71,7 +71,7 @@ SECTIONS . = ALIGN(4); *(EXCLUDE_FILE( *fsl_flexspi.o - *dcd_ci_hs.o + *cd_ci_hs.o *ehci.o *tusb_fifo.o *usbd.o @@ -88,6 +88,9 @@ SECTIONS /* Keep USB processing functions out of RAM because we don't know which will be used. We try to only keep USB interrupt related functions. */ *dcd_ci_hs.o(.text.process_*_request .text.dcd_edpt* .text.dcd_init .text.dcd_set_address) + /* Move hcd_dcache* routines to RAM so that we don't cross execution from + the cache during cache maintenance. Weird things happen when we do. */ + *hcd_ci_hs.o(.text.hcd_i*) *usbd.o(.text.process_*_request .text.process_[gs]et* .text.tud_* .text.usbd_* .text.configuration_reset .text.invoke_*) *ehci.o(.text.hcd_edpt* .text.hcd_setup* .text.ehci_init* .text.hcd_port* .text.hcd_device* .text.qtd_init* .text.list_remove*) diff --git a/ports/mimxrt10xx/supervisor/usb.c b/ports/mimxrt10xx/supervisor/usb.c index 64a2639c16..ab0bd15b51 100644 --- a/ports/mimxrt10xx/supervisor/usb.c +++ b/ports/mimxrt10xx/supervisor/usb.c @@ -31,7 +31,9 @@ #include "supervisor/linker.h" #include "supervisor/usb.h" -STATIC void init_usb_instance(mp_int_t instance) { +#include "imx_usb.h" + +void init_usb_instance(mp_int_t instance) { if (instance < 0) { return; } @@ -72,9 +74,6 @@ STATIC void init_usb_instance(mp_int_t instance) { void init_usb_hardware(void) { init_usb_instance(CIRCUITPY_USB_DEVICE_INSTANCE); - // We can't dynamically start the USB Host port at the moment, so do it - // up front. - init_usb_instance(CIRCUITPY_USB_HOST_INSTANCE); } // Provide the prototypes for the interrupt handlers. The iMX RT SDK doesn't. diff --git a/ports/raspberrypi/boards/adafruit_feather_rp2040_usb_host/board.c b/ports/raspberrypi/boards/adafruit_feather_rp2040_usb_host/board.c index 724fde1d27..1d054a03ac 100644 --- a/ports/raspberrypi/boards/adafruit_feather_rp2040_usb_host/board.c +++ b/ports/raspberrypi/boards/adafruit_feather_rp2040_usb_host/board.c @@ -31,7 +31,6 @@ // Use the MP_WEAK supervisor/shared/board.c versions of routines not defined here. -usb_host_port_obj_t _host_port; digitalio_digitalinout_obj_t _host_power; void board_init(void) { @@ -39,5 +38,5 @@ void board_init(void) { common_hal_digitalio_digitalinout_never_reset(&_host_power); common_hal_digitalio_digitalinout_switch_to_output(&_host_power, true, DRIVE_MODE_PUSH_PULL); - common_hal_usb_host_port_construct(&_host_port, &pin_GPIO16, &pin_GPIO17); + common_hal_usb_host_port_construct(&pin_GPIO16, &pin_GPIO17); } diff --git a/ports/raspberrypi/common-hal/usb_host/Port.c b/ports/raspberrypi/common-hal/usb_host/Port.c index 9ad28c73ea..93d19acd69 100644 --- a/ports/raspberrypi/common-hal/usb_host/Port.c +++ b/ports/raspberrypi/common-hal/usb_host/Port.c @@ -45,7 +45,7 @@ #include "supervisor/serial.h" -bool usb_host_init; +usb_host_port_obj_t usb_host_instance; STATIC PIO pio_instances[2] = {pio0, pio1}; volatile bool _core1_ready = false; @@ -102,10 +102,23 @@ STATIC bool _has_program_room(uint8_t pio_index, uint8_t program_size) { return pio_can_add_program(pio, &program_struct); } -void common_hal_usb_host_port_construct(usb_host_port_obj_t *self, const mcu_pin_obj_t *dp, const mcu_pin_obj_t *dm) { +usb_host_port_obj_t *common_hal_usb_host_port_construct(const mcu_pin_obj_t *dp, const mcu_pin_obj_t *dm) { if (dp->number + 1 != dm->number) { raise_ValueError_invalid_pins(); } + usb_host_port_obj_t *self = &usb_host_instance; + + // Return the singleton if given the same pins. + if (self->dp != NULL) { + if (self->dp != dp || self->dm != dm) { + mp_raise_msg_varg(&mp_type_RuntimeError, translate("%q in use"), MP_QSTR_usb_host); + } + return self; + } + + assert_pin_free(dp); + assert_pin_free(dm); + pio_usb_configuration_t pio_cfg = PIO_USB_DEFAULT_CONFIG; pio_cfg.skip_alarm_pool = true; pio_cfg.pin_dp = dp->number; @@ -122,6 +135,10 @@ void common_hal_usb_host_port_construct(usb_host_port_obj_t *self, const mcu_pin mp_raise_RuntimeError(translate("All dma channels in use")); } + self->base.type = &usb_host_port_type; + self->dp = dp; + self->dm = dm; + PIO tx_pio = pio_instances[pio_cfg.pio_tx_num]; pio_cfg.sm_tx = pio_claim_unused_sm(tx_pio, false); PIO rx_pio = pio_instances[pio_cfg.pio_rx_num]; @@ -151,15 +168,5 @@ void common_hal_usb_host_port_construct(usb_host_port_obj_t *self, const mcu_pin tuh_configure(TUH_OPT_RHPORT, TUH_CFGID_RPI_PIO_USB_CONFIGURATION, &pio_cfg); tuh_init(TUH_OPT_RHPORT); - self->init = true; - usb_host_init = true; -} - -void common_hal_usb_host_port_deinit(usb_host_port_obj_t *self) { - self->init = false; - usb_host_init = false; -} - -bool common_hal_usb_host_port_deinited(usb_host_port_obj_t *self) { - return !self->init; + return self; } diff --git a/ports/raspberrypi/common-hal/usb_host/Port.h b/ports/raspberrypi/common-hal/usb_host/Port.h index c9294debb2..69a7f8deaf 100644 --- a/ports/raspberrypi/common-hal/usb_host/Port.h +++ b/ports/raspberrypi/common-hal/usb_host/Port.h @@ -30,9 +30,6 @@ typedef struct { mp_obj_base_t base; - bool init; + const mcu_pin_obj_t *dp; + const mcu_pin_obj_t *dm; } usb_host_port_obj_t; - -// Cheater state so that the usb module knows if it should return the TinyUSB -// state. -extern bool usb_host_init; diff --git a/shared-bindings/synthio/Synthesizer.c b/shared-bindings/synthio/Synthesizer.c index 8e186573cd..25d26fc661 100644 --- a/shared-bindings/synthio/Synthesizer.c +++ b/shared-bindings/synthio/Synthesizer.c @@ -31,6 +31,7 @@ #include "py/binary.h" #include "py/objproperty.h" #include "py/runtime.h" +#include "py/enum.h" #include "shared-bindings/util.h" #include "shared-bindings/synthio/Biquad.h" #include "shared-bindings/synthio/Synthesizer.h" @@ -256,7 +257,9 @@ MP_PROPERTY_GETTER(synthio_synthesizer_sample_rate_obj, (mp_obj_t)&synthio_synthesizer_get_sample_rate_obj); //| pressed: NoteSequence -//| """A sequence of the currently pressed notes (read-only property)""" +//| """A sequence of the currently pressed notes (read-only property). +//| +//| This does not include notes in the release phase of the envelope.""" //| STATIC mp_obj_t synthio_synthesizer_obj_get_pressed(mp_obj_t self_in) { synthio_synthesizer_obj_t *self = MP_OBJ_TO_PTR(self_in); @@ -268,6 +271,23 @@ MP_DEFINE_CONST_FUN_OBJ_1(synthio_synthesizer_get_pressed_obj, synthio_synthesiz MP_PROPERTY_GETTER(synthio_synthesizer_pressed_obj, (mp_obj_t)&synthio_synthesizer_get_pressed_obj); +//| def note_info(self, note: Note) -> Tuple[Optional[EnvelopeState], float]: +//| """Get info about a note's current envelope state +//| +//| If the note is currently playing (including in the release phase), the returned value gives the current envelope state and the current envelope value. +//| +//| If the note is not playing on this synthesizer, returns the tuple ``(None, 0.0)``.""" +STATIC mp_obj_t synthio_synthesizer_obj_note_info(mp_obj_t self_in, mp_obj_t note) { + synthio_synthesizer_obj_t *self = MP_OBJ_TO_PTR(self_in); + check_for_deinit(self); + mp_float_t vol = MICROPY_FLOAT_CONST(0.0); + envelope_state_e state = common_hal_synthio_synthesizer_note_info(self, note, &vol); + return MP_OBJ_NEW_TUPLE( + cp_enum_find(&synthio_note_state_type, state), + mp_obj_new_float(vol)); +} +MP_DEFINE_CONST_FUN_OBJ_2(synthio_synthesizer_note_info_obj, synthio_synthesizer_obj_note_info); + //| blocks: List[BlockInput] //| """A list of blocks to advance whether or not they are associated with a playing note. //| @@ -417,6 +437,7 @@ STATIC const mp_rom_map_elem_t synthio_synthesizer_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_sample_rate), MP_ROM_PTR(&synthio_synthesizer_sample_rate_obj) }, { MP_ROM_QSTR(MP_QSTR_max_polyphony), MP_ROM_INT(CIRCUITPY_SYNTHIO_MAX_CHANNELS) }, { MP_ROM_QSTR(MP_QSTR_pressed), MP_ROM_PTR(&synthio_synthesizer_pressed_obj) }, + { MP_ROM_QSTR(MP_QSTR_note_info), MP_ROM_PTR(&synthio_synthesizer_note_info_obj) }, { MP_ROM_QSTR(MP_QSTR_blocks), MP_ROM_PTR(&synthio_synthesizer_blocks_obj) }, }; STATIC MP_DEFINE_CONST_DICT(synthio_synthesizer_locals_dict, synthio_synthesizer_locals_dict_table); diff --git a/shared-bindings/synthio/Synthesizer.h b/shared-bindings/synthio/Synthesizer.h index 16543ce2c3..8ae0af3bf3 100644 --- a/shared-bindings/synthio/Synthesizer.h +++ b/shared-bindings/synthio/Synthesizer.h @@ -45,3 +45,4 @@ void common_hal_synthio_synthesizer_retrigger(synthio_synthesizer_obj_t *self, m void common_hal_synthio_synthesizer_release_all(synthio_synthesizer_obj_t *self); mp_obj_t common_hal_synthio_synthesizer_get_pressed_notes(synthio_synthesizer_obj_t *self); mp_obj_t common_hal_synthio_synthesizer_get_blocks(synthio_synthesizer_obj_t *self); +envelope_state_e common_hal_synthio_synthesizer_note_info(synthio_synthesizer_obj_t *self, mp_obj_t note, mp_float_t *vol_out); diff --git a/shared-bindings/synthio/__init__.c b/shared-bindings/synthio/__init__.c index b295430b09..52d30cd2c4 100644 --- a/shared-bindings/synthio/__init__.c +++ b/shared-bindings/synthio/__init__.c @@ -45,6 +45,32 @@ #include "shared-module/synthio/LFO.h" +//| class EnvelopeState: +//| ATTACK: EnvelopeState +//| """The note is in its attack phase""" +//| DECAY: EnvelopeState +//| """The note is in its decay phase""" +//| SUSTAIN: EnvelopeState +//| """The note is in its sustain phase""" +//| RELEASE: EnvelopeState +//| """The note is in its release phase""" +//| +MAKE_ENUM_VALUE(synthio_note_state_type, note_state, ATTACK, SYNTHIO_ENVELOPE_STATE_ATTACK); +MAKE_ENUM_VALUE(synthio_note_state_type, note_state, DECAY, SYNTHIO_ENVELOPE_STATE_DECAY); +MAKE_ENUM_VALUE(synthio_note_state_type, note_state, SUSTAIN, SYNTHIO_ENVELOPE_STATE_SUSTAIN); +MAKE_ENUM_VALUE(synthio_note_state_type, note_state, RELEASE, SYNTHIO_ENVELOPE_STATE_RELEASE); + +MAKE_ENUM_MAP(synthio_note_state) { + MAKE_ENUM_MAP_ENTRY(note_state, ATTACK), + MAKE_ENUM_MAP_ENTRY(note_state, DECAY), + MAKE_ENUM_MAP_ENTRY(note_state, SUSTAIN), + MAKE_ENUM_MAP_ENTRY(note_state, RELEASE), +}; + +STATIC MP_DEFINE_CONST_DICT(synthio_note_state_locals_dict, synthio_note_state_locals_table); +MAKE_PRINTER(synthio, synthio_note_state); +MAKE_ENUM_TYPE(synthio, EnvelopeState, synthio_note_state); + #define default_attack_time (MICROPY_FLOAT_CONST(0.1)) #define default_decay_time (MICROPY_FLOAT_CONST(0.05)) #define default_release_time (MICROPY_FLOAT_CONST(0.2)) @@ -316,6 +342,7 @@ STATIC const mp_rom_map_elem_t synthio_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_MathOperation), MP_ROM_PTR(&synthio_math_operation_type) }, { MP_ROM_QSTR(MP_QSTR_MidiTrack), MP_ROM_PTR(&synthio_miditrack_type) }, { MP_ROM_QSTR(MP_QSTR_Note), MP_ROM_PTR(&synthio_note_type) }, + { MP_ROM_QSTR(MP_QSTR_EnvelopeState), MP_ROM_PTR(&synthio_note_state_type) }, { MP_ROM_QSTR(MP_QSTR_LFO), MP_ROM_PTR(&synthio_lfo_type) }, { MP_ROM_QSTR(MP_QSTR_Synthesizer), MP_ROM_PTR(&synthio_synthesizer_type) }, { MP_ROM_QSTR(MP_QSTR_from_file), MP_ROM_PTR(&synthio_from_file_obj) }, diff --git a/shared-bindings/synthio/__init__.h b/shared-bindings/synthio/__init__.h index 2315d92e58..4a44277e85 100644 --- a/shared-bindings/synthio/__init__.h +++ b/shared-bindings/synthio/__init__.h @@ -29,10 +29,16 @@ #include "py/objnamedtuple.h" #include "py/enum.h" +typedef enum { + SYNTHIO_ENVELOPE_STATE_ATTACK, SYNTHIO_ENVELOPE_STATE_DECAY, + SYNTHIO_ENVELOPE_STATE_SUSTAIN, SYNTHIO_ENVELOPE_STATE_RELEASE +} envelope_state_e; + typedef enum synthio_bend_mode_e { SYNTHIO_BEND_MODE_STATIC, SYNTHIO_BEND_MODE_VIBRATO, SYNTHIO_BEND_MODE_SWEEP, SYNTHIO_BEND_MODE_SWEEP_IN } synthio_bend_mode_t; +extern const mp_obj_type_t synthio_note_state_type; extern const cp_enum_obj_t bend_mode_VIBRATO_obj; extern const mp_obj_type_t synthio_bend_mode_type; typedef struct synthio_synth synthio_synth_t; diff --git a/shared-bindings/usb_host/Port.c b/shared-bindings/usb_host/Port.c index 53b1fec54f..9be6da9df3 100644 --- a/shared-bindings/usb_host/Port.c +++ b/shared-bindings/usb_host/Port.c @@ -35,59 +35,36 @@ //| //| def __init__(self, dp: microcontroller.Pin, dm: microcontroller.Pin) -> None: //| """Create a USB host port on the given pins. Access attached devices -//| through the `usb` module. Keep this object referenced while -//| interacting with devices, otherwise they will be disconnected. +//| through the `usb` module. +//| +//| The resulting object lives longer than the CircuitPython VM so that +//| USB devices such as keyboards can continue to be used. Subsequent +//| calls to this constructor will return the same object and *not* +//| reinitialize the USB host port. It will raise an exception when +//| given different arguments from the first successful call. //| //| :param ~microcontroller.Pin dp: The data plus pin //| :param ~microcontroller.Pin dm: The data minus pin //| """ //| ... +//| STATIC mp_obj_t usb_host_port_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { // check number of arguments mp_arg_check_num(n_args, n_kw, 2, 2, false); - const mcu_pin_obj_t *dp = validate_obj_is_free_pin(args[0], MP_QSTR_dp); - const mcu_pin_obj_t *dm = validate_obj_is_free_pin(args[1], MP_QSTR_dm); + const mcu_pin_obj_t *dp = validate_obj_is_pin(args[0], MP_QSTR_dp); + const mcu_pin_obj_t *dm = validate_obj_is_pin(args[1], MP_QSTR_dm); - usb_host_port_obj_t *self = m_new_obj(usb_host_port_obj_t); - self->base.type = &usb_host_port_type; - common_hal_usb_host_port_construct(self, dp, dm); + // Pin in use checks happen in the implementation so they can be ignored + // when returning the singleton. + + usb_host_port_obj_t *self = common_hal_usb_host_port_construct(dp, dm); return (mp_obj_t)self; } -//| def deinit(self) -> None: -//| """Turn off the USB host port and release the pins for other use.""" -//| ... -STATIC mp_obj_t usb_host_port_obj_deinit(mp_obj_t self_in) { - usb_host_port_obj_t *self = MP_OBJ_TO_PTR(self_in); - common_hal_usb_host_port_deinit(self); - return mp_const_none; -} -MP_DEFINE_CONST_FUN_OBJ_1(usb_host_port_deinit_obj, usb_host_port_obj_deinit); - -//| def __enter__(self) -> Port: -//| """No-op used by Context Managers.""" -//| ... -// Provided by context manager helper. - -//| def __exit__(self) -> None: -//| """Automatically deinitializes the hardware when exiting a context. See -//| :ref:`lifetime-and-contextmanagers` for more info.""" -//| ... -//| -STATIC mp_obj_t usb_host_port_obj___exit__(size_t n_args, const mp_obj_t *args) { - (void)n_args; - common_hal_usb_host_port_deinit(MP_OBJ_TO_PTR(args[0])); - return mp_const_none; -} -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(usb_host_port_obj___exit___obj, 4, 4, usb_host_port_obj___exit__); - STATIC const mp_rom_map_elem_t usb_host_port_locals_dict_table[] = { - { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&usb_host_port_deinit_obj) }, - { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, - { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&usb_host_port_obj___exit___obj) }, }; STATIC MP_DEFINE_CONST_DICT(usb_host_port_locals_dict, usb_host_port_locals_dict_table); diff --git a/shared-bindings/usb_host/Port.h b/shared-bindings/usb_host/Port.h index 68645d1146..ba8d298517 100644 --- a/shared-bindings/usb_host/Port.h +++ b/shared-bindings/usb_host/Port.h @@ -35,8 +35,10 @@ extern const mp_obj_type_t usb_host_port_type; -void common_hal_usb_host_port_construct(usb_host_port_obj_t *self, const mcu_pin_obj_t *dp, const mcu_pin_obj_t *dm); -void common_hal_usb_host_port_deinit(usb_host_port_obj_t *self); -bool common_hal_usb_host_port_deinited(usb_host_port_obj_t *self); +// This is unique to common_hal constructs because it returns a globally stored +// object instead of taking on in that may be on the heap. This allows the +// method to check the internals of the global object against the given arguments +// to determine whether to return the singleton or raise an exception. +usb_host_port_obj_t *common_hal_usb_host_port_construct(const mcu_pin_obj_t *dp, const mcu_pin_obj_t *dm); #endif // MICROPY_INCLUDED_SHARED_BINDINGS_USB_HOST_PORT_H diff --git a/shared-module/synthio/Synthesizer.c b/shared-module/synthio/Synthesizer.c index 7dc888dfd0..672ac5b9e6 100644 --- a/shared-module/synthio/Synthesizer.c +++ b/shared-module/synthio/Synthesizer.c @@ -185,6 +185,17 @@ mp_obj_t common_hal_synthio_synthesizer_get_pressed_notes(synthio_synthesizer_ob return MP_OBJ_FROM_PTR(result); } +envelope_state_e common_hal_synthio_synthesizer_note_info(synthio_synthesizer_obj_t *self, mp_obj_t note, mp_float_t *vol_out) { + for (int chan = 0; chan < CIRCUITPY_SYNTHIO_MAX_CHANNELS; chan++) { + if (self->synth.span.note_obj[chan] == note) { + *vol_out = self->synth.envelope_state[chan].level / 32767.; + return self->synth.envelope_state[chan].state; + } + } + return (envelope_state_e) - 1; +} + + mp_obj_t common_hal_synthio_synthesizer_get_blocks(synthio_synthesizer_obj_t *self) { return self->blocks; } diff --git a/shared-module/synthio/__init__.h b/shared-module/synthio/__init__.h index 9e4db96d50..178e4fb866 100644 --- a/shared-module/synthio/__init__.h +++ b/shared-module/synthio/__init__.h @@ -35,6 +35,7 @@ #define SYNTHIO_FREQUENCY_SHIFT (16) #include "shared-module/audiocore/__init__.h" +#include "shared-bindings/synthio/__init__.h" typedef struct { uint16_t dur; @@ -49,11 +50,6 @@ typedef struct { uint16_t attack_level, sustain_level; } synthio_envelope_definition_t; -typedef enum { - SYNTHIO_ENVELOPE_STATE_ATTACK, SYNTHIO_ENVELOPE_STATE_DECAY, - SYNTHIO_ENVELOPE_STATE_SUSTAIN, SYNTHIO_ENVELOPE_STATE_RELEASE -} envelope_state_e; - typedef struct { int16_t level; uint16_t substep; diff --git a/supervisor/shared/usb/tusb_config.h b/supervisor/shared/usb/tusb_config.h index 19f5c0a0ce..d097005aac 100644 --- a/supervisor/shared/usb/tusb_config.h +++ b/supervisor/shared/usb/tusb_config.h @@ -152,7 +152,8 @@ extern "C" { #endif #define CFG_TUH_HID 2 -#define CFG_TUH_HUB 1 +// 2 hubs so we can support "7 port" hubs which have two internal hubs. +#define CFG_TUH_HUB 2 #define CFG_TUH_CDC 0 #define CFG_TUH_MSC 0 #define CFG_TUH_VENDOR 0 diff --git a/tests/circuitpython/synthio_note_info.py b/tests/circuitpython/synthio_note_info.py new file mode 100644 index 0000000000..4d4e7a59ae --- /dev/null +++ b/tests/circuitpython/synthio_note_info.py @@ -0,0 +1,17 @@ +from synthio import Synthesizer, Note, Envelope +from audiocore import get_buffer + +s = Synthesizer() +n = Note(440, envelope=Envelope()) +print("{} {:.2f}".format(*s.note_info(n))) +s.press(n) +print("press") +for _ in range(9): + print("{} {:.2f}".format(*s.note_info(n))) + get_buffer(s) + +s.release(n) +print("release") +for _ in range(11): + print("{} {:.2f}".format(*s.note_info(n))) + get_buffer(s) diff --git a/tests/circuitpython/synthio_note_info.py.exp b/tests/circuitpython/synthio_note_info.py.exp new file mode 100644 index 0000000000..cd75174a11 --- /dev/null +++ b/tests/circuitpython/synthio_note_info.py.exp @@ -0,0 +1,23 @@ +None 0.00 +press +synthio.EnvelopeState.ATTACK 0.23 +synthio.EnvelopeState.ATTACK 0.46 +synthio.EnvelopeState.ATTACK 0.70 +synthio.EnvelopeState.ATTACK 0.93 +synthio.EnvelopeState.DECAY 1.00 +synthio.EnvelopeState.DECAY 0.91 +synthio.EnvelopeState.DECAY 0.81 +synthio.EnvelopeState.SUSTAIN 0.80 +synthio.EnvelopeState.SUSTAIN 0.80 +release +synthio.EnvelopeState.RELEASE 0.80 +synthio.EnvelopeState.RELEASE 0.71 +synthio.EnvelopeState.RELEASE 0.61 +synthio.EnvelopeState.RELEASE 0.52 +synthio.EnvelopeState.RELEASE 0.43 +synthio.EnvelopeState.RELEASE 0.34 +synthio.EnvelopeState.RELEASE 0.24 +synthio.EnvelopeState.RELEASE 0.15 +synthio.EnvelopeState.RELEASE 0.06 +synthio.EnvelopeState.RELEASE 0.00 +None 0.00 diff --git a/tests/cmdline/cmd_showbc.py.exp b/tests/cmdline/cmd_showbc.py.exp index 22712b79ee..c3dbfd74f8 100644 --- a/tests/cmdline/cmd_showbc.py.exp +++ b/tests/cmdline/cmd_showbc.py.exp @@ -1,7 +1,6 @@ File cmdline/cmd_showbc.py, code block '' (descriptor: \.\+, bytecode @\.\+ bytes) Raw bytecode (code_info_size=\\d\+, bytecode_size=\\d\+): ######## -\.\+63 arg names: (N_STATE 3) (N_EXC_STACK 0)