From 27d320fb6873961c2ce6648cfa216f7e89a4bb2c Mon Sep 17 00:00:00 2001 From: Andrew Leech Date: Sat, 3 Aug 2024 22:47:07 +1000 Subject: [PATCH] stm32/usb: Add support for using TinyUSB stack by default. Signed-off-by: Andrew Leech --- ports/stm32/Makefile | 41 ++++++++------ ports/stm32/main.c | 90 ++++++++++++++++++++++++++++-- ports/stm32/modmachine.c | 6 +- ports/stm32/modos.c | 6 +- ports/stm32/modpyb.c | 34 +++++------ ports/stm32/mpconfigboard_common.h | 31 +++++++++- ports/stm32/mpconfigport.h | 1 + ports/stm32/mphalport.c | 27 +++++++++ ports/stm32/mphalport.h | 4 ++ ports/stm32/stm32_it.c | 29 +++++++--- ports/stm32/usb.h | 4 +- ports/stm32/usbd_conf.c | 8 +-- shared/runtime/pyexec.c | 2 +- shared/tinyusb/tusb_config.h | 7 ++- 14 files changed, 228 insertions(+), 62 deletions(-) diff --git a/ports/stm32/Makefile b/ports/stm32/Makefile index 497e409c66563..e891757670b3d 100644 --- a/ports/stm32/Makefile +++ b/ports/stm32/Makefile @@ -58,7 +58,7 @@ MBOOT_TEXT0_ADDR ?= 0x08000000 include $(TOP)/py/py.mk include $(TOP)/extmod/extmod.mk -GIT_SUBMODULES += lib/libhydrogen lib/stm32lib +GIT_SUBMODULES += lib/libhydrogen lib/stm32lib lib/tinyusb LD_DIR=boards USBDEV_DIR=usbdev @@ -112,6 +112,9 @@ INC += -I$(STM32LIB_CMSIS_ABS)/Include INC += -I$(STM32LIB_HAL_ABS)/Inc INC += -I$(USBDEV_DIR)/core/inc -I$(USBDEV_DIR)/class/inc #INC += -I$(USBHOST_DIR) +INC += -I$(TOP)/lib/tinyusb/src +INC += -I$(TOP)/shared/tinyusb/ + INC += -Ilwip_inc CFLAGS += $(INC) -Wall -Wpointer-arith -Werror -Wdouble-promotion -Wfloat-conversion -std=gnu99 -nostdlib $(CFLAGS_EXTRA) @@ -199,6 +202,10 @@ SHARED_SRC_C += $(addprefix shared/,\ runtime/stdout_helpers.c \ runtime/sys_stdio_mphal.c \ timeutils/timeutils.c \ + tinyusb/mp_usbd.c \ + tinyusb/mp_usbd_cdc.c \ + tinyusb/mp_usbd_descriptor.c \ + tinyusb/mp_usbd_runtime.c \ ) ifeq ($(MICROPY_FLOAT_IMPL),double) @@ -223,16 +230,26 @@ DRIVERS_SRC_C += $(addprefix drivers/,\ memory/spiflash.c \ dht/dht.c \ ) + +TINYUSB_SRC_C += $(addprefix lib/tinyusb/src/,\ + class/cdc/cdc_device.c \ + class/msc/msc_device.c \ + common/tusb_fifo.c \ + device/usbd.c \ + device/usbd_control.c \ + portable/st/stm32_fsdev/dcd_stm32_fsdev.c \ + portable/synopsys/dwc2/dcd_dwc2.c \ + tusb.c \ + ) +LDFLAGS += -Wl,--wrap=dcd_event_handler + +# usbd_conf.c SRC_C += \ boardctrl.c \ main.c \ stm32_it.c \ - usbd_conf.c \ - usbd_desc.c \ - usbd_cdc_interface.c \ - usbd_hid_interface.c \ - usbd_msc_interface.c \ + usbd.c \ mphalport.c \ mpnetworkport.c \ mpthreadport.c \ @@ -262,7 +279,6 @@ SRC_C += \ can.c \ fdcan.c \ pyb_can.c \ - usb.c \ eth.c \ eth_phy.c \ gccollect.c \ @@ -408,15 +424,6 @@ HAL_SRC_C += $(addprefix $(STM32LIB_HAL_BASE)/Src/stm32$(MCU_SERIES)xx_,\ ) endif -USBDEV_SRC_C += $(addprefix $(USBDEV_DIR)/,\ - core/src/usbd_core.c \ - core/src/usbd_ctlreq.c \ - core/src/usbd_ioreq.c \ - class/src/usbd_cdc_msc_hid.c \ - class/src/usbd_msc_bot.c \ - class/src/usbd_msc_scsi.c \ - ) - ifeq ($(MICROPY_SSL_MBEDTLS),1) LIB_SRC_C += mbedtls/mbedtls_port.c endif @@ -455,7 +462,7 @@ OBJ += $(addprefix $(BUILD)/, $(LIBM_SRC_C:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(SHARED_SRC_C:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(DRIVERS_SRC_C:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(HAL_SRC_C:.c=.o)) -OBJ += $(addprefix $(BUILD)/, $(USBDEV_SRC_C:.c=.o)) +OBJ += $(addprefix $(BUILD)/, $(TINYUSB_SRC_C:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(SRC_C:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(SRC_CXX:.cpp=.o)) OBJ += $(GEN_PINS_SRC:.c=.o) diff --git a/ports/stm32/main.c b/ports/stm32/main.c index 90a62c024427e..912dd73c74e9e 100644 --- a/ports/stm32/main.c +++ b/ports/stm32/main.c @@ -77,7 +77,7 @@ #include "pin.h" #include "extint.h" #include "usrsw.h" -#include "usb.h" +// #include "usb.h" #include "rtc.h" #include "storage.h" #include "sdcard.h" @@ -89,6 +89,9 @@ #include "can.h" #include "subghz.h" +#include "shared/tinyusb/mp_usbd.h" + + #if MICROPY_PY_THREAD static pyb_thread_t pyb_thread_main; #endif @@ -299,6 +302,54 @@ static bool init_sdcard_fs(void) { } #endif +void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd) { + GPIO_InitTypeDef GPIO_InitStruct = {0}; + RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0}; + if (hpcd->Instance == USB) { + /* USER CODE BEGIN USB_MspInit 0 */ + + /* USER CODE END USB_MspInit 0 */ + + /** Initializes the peripherals clock + */ + PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_USB; + PeriphClkInitStruct.PLLSAI1.PLLN = 24; + PeriphClkInitStruct.PLLSAI1.PLLP = RCC_PLLP_DIV2; + PeriphClkInitStruct.PLLSAI1.PLLQ = RCC_PLLQ_DIV2; + PeriphClkInitStruct.PLLSAI1.PLLR = RCC_PLLR_DIV2; + PeriphClkInitStruct.PLLSAI1.PLLSAI1ClockOut = RCC_PLLSAI1_USBCLK; + PeriphClkInitStruct.UsbClockSelection = RCC_USBCLKSOURCE_PLLSAI1; + if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) { + // Error_Handler(); + } + + __HAL_RCC_GPIOA_CLK_ENABLE(); + /**USB GPIO Configuration + PA11 ------> USB_DM + PA12 ------> USB_DP + */ + GPIO_InitStruct.Pin = GPIO_PIN_11 | GPIO_PIN_12; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + GPIO_InitStruct.Alternate = GPIO_AF10_USB; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + + /* Peripheral clock enable */ + __HAL_RCC_USB_CLK_ENABLE(); + /* USB interrupt Init */ + HAL_NVIC_SetPriority(USB_LP_IRQn, 0, 0); + HAL_NVIC_EnableIRQ(USB_LP_IRQn); + /* USER CODE BEGIN USB_MspInit 1 */ + + /* USER CODE END USB_MspInit 1 */ + } + +} + + +PCD_HandleTypeDef hpcd_USB_FS; + void stm32_main(uint32_t reset_mode) { // Low-level MCU initialisation. stm32_system_init(); @@ -529,7 +580,30 @@ void stm32_main(uint32_t reset_mode) { #endif #if MICROPY_HW_ENABLE_USB - pyb_usb_init0(); +// hpcd_USB_FS.Instance = USB; +// hpcd_USB_FS.Init.dev_endpoints = 8; +// hpcd_USB_FS.Init.speed = PCD_SPEED_FULL; +// hpcd_USB_FS.Init.phy_itface = PCD_PHY_EMBEDDED; +// hpcd_USB_FS.Init.Sof_enable = ENABLE; +// hpcd_USB_FS.Init.low_power_enable = DISABLE; +// hpcd_USB_FS.Init.lpm_enable = DISABLE; +// hpcd_USB_FS.Init.battery_charging_enable = DISABLE; +// if (HAL_PCD_Init(&hpcd_USB_FS) != HAL_OK) +// { +// } + + GPIO_InitTypeDef GPIO_InitStruct = {0}; + GPIO_InitStruct.Pin = (GPIO_PIN_11 | GPIO_PIN_12); + GPIO_InitStruct.Mode = GPIO_MODE_INPUT; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + + HAL_PWREx_EnableVddUSB(); + __HAL_RCC_USB_CLK_ENABLE(); + mp_usbd_init(); + // mp_usbd_task(); + // pyb_usb_init0(); #endif #if MICROPY_PY_MACHINE_I2S @@ -556,9 +630,9 @@ void stm32_main(uint32_t reset_mode) { #if MICROPY_HW_ENABLE_USB // if the SD card isn't used as the USB MSC medium then use the internal flash - if (pyb_usb_storage_medium == PYB_USB_STORAGE_MEDIUM_NONE) { - pyb_usb_storage_medium = PYB_USB_STORAGE_MEDIUM_FLASH; - } + // if (pyb_usb_storage_medium == PYB_USB_STORAGE_MEDIUM_NONE) { + // pyb_usb_storage_medium = PYB_USB_STORAGE_MEDIUM_FLASH; + // } #endif // set sys.path based on mounted filesystems (/sd is first so it can override /flash) @@ -588,7 +662,8 @@ void stm32_main(uint32_t reset_mode) { // or whose initialisation can be safely deferred until after running // boot.py. - #if MICROPY_HW_ENABLE_USB + + #if 0 // init USB device to default setting if it was not already configured if (!(pyb_usb_flags & PYB_USB_FLAG_USB_MODE_CALLED)) { #if MICROPY_HW_USB_MSC @@ -686,6 +761,9 @@ void stm32_main(uint32_t reset_mode) { #else MP_STATE_PORT(pyb_stdio_uart) = NULL; #endif + #if MICROPY_HW_ENABLE_USB_RUNTIME_DEVICE + mp_usbd_deinit(); + #endif MICROPY_BOARD_END_SOFT_RESET(&state); diff --git a/ports/stm32/modmachine.c b/ports/stm32/modmachine.c index 7c1c4da60bff2..e60d8baf8539d 100644 --- a/ports/stm32/modmachine.c +++ b/ports/stm32/modmachine.c @@ -290,8 +290,12 @@ NORETURN static void mp_machine_reset(void) { // Activate the bootloader without BOOT* pins. NORETURN void mp_machine_bootloader(size_t n_args, const mp_obj_t *args) { #if MICROPY_HW_ENABLE_USB - pyb_usb_dev_deinit(); + // pyb_usb_dev_deinit(); #endif + #if MICROPY_HW_ENABLE_USB_RUNTIME_DEVICE + mp_usbd_deinit(); + #endif + #if MICROPY_HW_ENABLE_STORAGE storage_flush(); #endif diff --git a/ports/stm32/modos.c b/ports/stm32/modos.c index 7949cf87cdecd..f202063025b32 100644 --- a/ports/stm32/modos.c +++ b/ports/stm32/modos.c @@ -52,7 +52,7 @@ bool mp_os_dupterm_is_builtin_stream(mp_const_obj_t stream) { #if MICROPY_PY_MACHINE_UART || type == &machine_uart_type #endif - #if MICROPY_HW_ENABLE_USB + #if MICROPY_HW_ENABLE_USB && 0 || type == &pyb_usb_vcp_type #endif ; @@ -64,7 +64,7 @@ void mp_os_dupterm_stream_detached_attached(mp_obj_t stream_detached, mp_obj_t s uart_attach_to_repl(MP_OBJ_TO_PTR(stream_detached), false); } #endif - #if MICROPY_HW_ENABLE_USB + #if MICROPY_HW_ENABLE_USB && 0 if (mp_obj_get_type(stream_detached) == &pyb_usb_vcp_type) { usb_vcp_attach_to_repl(MP_OBJ_TO_PTR(stream_detached), false); } @@ -75,7 +75,7 @@ void mp_os_dupterm_stream_detached_attached(mp_obj_t stream_detached, mp_obj_t s uart_attach_to_repl(MP_OBJ_TO_PTR(stream_attached), true); } #endif - #if MICROPY_HW_ENABLE_USB + #if MICROPY_HW_ENABLE_USB && 0 if (mp_obj_get_type(stream_attached) == &pyb_usb_vcp_type) { usb_vcp_attach_to_repl(MP_OBJ_TO_PTR(stream_attached), true); } diff --git a/ports/stm32/modpyb.c b/ports/stm32/modpyb.c index 176010a7e5107..76c61bc8481db 100644 --- a/ports/stm32/modpyb.c +++ b/ports/stm32/modpyb.c @@ -49,7 +49,7 @@ #include "servo.h" #include "dac.h" #include "lcd.h" -#include "usb.h" +// #include "usb.h" #include "portmodules.h" #include "modmachine.h" #include "extmod/modmachine.h" @@ -167,22 +167,22 @@ static const mp_rom_map_elem_t pyb_module_globals_table[] = { // Deprecated (use network.country instead). { MP_ROM_QSTR(MP_QSTR_country), MP_ROM_PTR(&mod_network_country_obj) }, - #if MICROPY_HW_ENABLE_USB - { MP_ROM_QSTR(MP_QSTR_usb_mode), MP_ROM_PTR(&pyb_usb_mode_obj) }, - #if MICROPY_HW_USB_HID - { MP_ROM_QSTR(MP_QSTR_hid_mouse), MP_ROM_PTR(&pyb_usb_hid_mouse_obj) }, - { MP_ROM_QSTR(MP_QSTR_hid_keyboard), MP_ROM_PTR(&pyb_usb_hid_keyboard_obj) }, - { MP_ROM_QSTR(MP_QSTR_USB_HID), MP_ROM_PTR(&pyb_usb_hid_type) }, - #endif - { MP_ROM_QSTR(MP_QSTR_USB_VCP), MP_ROM_PTR(&pyb_usb_vcp_type) }, - #if MICROPY_PY_PYB_LEGACY - // these 2 are deprecated; use USB_VCP.isconnected and USB_HID.send instead - { MP_ROM_QSTR(MP_QSTR_have_cdc), MP_ROM_PTR(&pyb_have_cdc_obj) }, - #if MICROPY_HW_USB_HID - { MP_ROM_QSTR(MP_QSTR_hid), MP_ROM_PTR(&pyb_hid_send_report_obj) }, - #endif - #endif - #endif + // #if MICROPY_HW_ENABLE_USB + // { MP_ROM_QSTR(MP_QSTR_usb_mode), MP_ROM_PTR(&pyb_usb_mode_obj) }, + // #if MICROPY_HW_USB_HID + // { MP_ROM_QSTR(MP_QSTR_hid_mouse), MP_ROM_PTR(&pyb_usb_hid_mouse_obj) }, + // { MP_ROM_QSTR(MP_QSTR_hid_keyboard), MP_ROM_PTR(&pyb_usb_hid_keyboard_obj) }, + // { MP_ROM_QSTR(MP_QSTR_USB_HID), MP_ROM_PTR(&pyb_usb_hid_type) }, + // #endif + // { MP_ROM_QSTR(MP_QSTR_USB_VCP), MP_ROM_PTR(&pyb_usb_vcp_type) }, + // #if MICROPY_PY_PYB_LEGACY + // // these 2 are deprecated; use USB_VCP.isconnected and USB_HID.send instead + // { MP_ROM_QSTR(MP_QSTR_have_cdc), MP_ROM_PTR(&pyb_have_cdc_obj) }, + // #if MICROPY_HW_USB_HID + // { MP_ROM_QSTR(MP_QSTR_hid), MP_ROM_PTR(&pyb_hid_send_report_obj) }, + // #endif + // #endif + // #endif #if MICROPY_PY_PYB_LEGACY { MP_ROM_QSTR(MP_QSTR_millis), MP_ROM_PTR(&mp_time_ticks_ms_obj) }, diff --git a/ports/stm32/mpconfigboard_common.h b/ports/stm32/mpconfigboard_common.h index 67a6ee990e399..616c457bfbbcc 100644 --- a/ports/stm32/mpconfigboard_common.h +++ b/ports/stm32/mpconfigboard_common.h @@ -96,6 +96,9 @@ #ifndef MICROPY_HW_ENABLE_USB #define MICROPY_HW_ENABLE_USB (0) #endif +#define MICROPY_HW_ENABLE_USBDEV 1 +#define MICROPY_HW_USB_CDC 1 +#define MICROPY_HW_USB_FS (1) // Whether to enable the PA0-PA3 servo driver, exposed as pyb.Servo #ifndef MICROPY_HW_ENABLE_SERVO @@ -211,6 +214,8 @@ // Windows needs a different PID to distinguish different device configurations. #ifndef MICROPY_HW_USB_VID #define MICROPY_HW_USB_VID (0xf055) +#define MICROPY_HW_USB_PID (0x9802) + #define MICROPY_HW_USB_PID_CDC_MSC (0x9800) #define MICROPY_HW_USB_PID_CDC_HID (0x9801) #define MICROPY_HW_USB_PID_CDC (0x9802) @@ -324,6 +329,8 @@ #endif #define MICROPY_HW_MAX_LPUART (0) +#define CFG_TUSB_MCU OPT_MCU_STM32F4 + // Configuration for STM32F7 series #elif defined(STM32F7) @@ -339,6 +346,8 @@ #define MICROPY_HW_MAX_UART (8) #define MICROPY_HW_MAX_LPUART (0) +#define CFG_TUSB_MCU OPT_MCU_STM32F7 + // Configuration for STM32G0 series #elif defined(STM32G0) @@ -349,6 +358,8 @@ #define MICROPY_HW_MAX_UART (6) #define MICROPY_HW_MAX_LPUART (2) +#define CFG_TUSB_MCU OPT_MCU_STM32G0 + // Configuration for STM32G4 series #elif defined(STM32G4) @@ -359,6 +370,8 @@ #define MICROPY_HW_MAX_UART (5) // UART1-5 + LPUART1 #define MICROPY_HW_MAX_LPUART (1) +#define CFG_TUSB_MCU OPT_MCU_STM32G4 + // Configuration for STM32H5 series #elif defined(STM32H5) @@ -369,6 +382,8 @@ #define MICROPY_HW_MAX_UART (12) #define MICROPY_HW_MAX_LPUART (1) +#define CFG_TUSB_MCU OPT_MCU_STM32H5 + // Configuration for STM32H7A3/B3 series #elif defined(STM32H7A3xx) || defined(STM32H7A3xxQ) || \ defined(STM32H7B3xx) || defined(STM32H7B3xxQ) @@ -380,6 +395,8 @@ #define MICROPY_HW_MAX_UART (10) #define MICROPY_HW_MAX_LPUART (1) +#define CFG_TUSB_MCU OPT_MCU_STM32H7 + // Configuration for STM32H7 series #elif defined(STM32H7) @@ -390,6 +407,8 @@ #define MICROPY_HW_MAX_UART (8) #define MICROPY_HW_MAX_LPUART (1) +#define CFG_TUSB_MCU OPT_MCU_STM32H7 + #if defined(MICROPY_HW_ANALOG_SWITCH_PA0) \ || defined(MICROPY_HW_ANALOG_SWITCH_PA1) \ || defined(MICROPY_HW_ANALOG_SWITCH_PC2) \ @@ -409,6 +428,8 @@ #define MICROPY_HW_MAX_UART (5) #define MICROPY_HW_MAX_LPUART (1) +#define CFG_TUSB_MCU OPT_MCU_STM32L0 + // Configuration for STM32L1 series #elif defined(STM32L1) #define MP_HAL_UNIQUE_ID_ADDRESS (UID_BASE) @@ -419,6 +440,8 @@ #define MICROPY_HW_MAX_UART (5) #define MICROPY_HW_MAX_LPUART (0) +#define CFG_TUSB_MCU OPT_MCU_STM32L1 + // Configuration for STM32L4 series #elif defined(STM32L4) @@ -429,6 +452,8 @@ #define MICROPY_HW_MAX_UART (5) #define MICROPY_HW_MAX_LPUART (1) +#define CFG_TUSB_MCU OPT_MCU_STM32L4 + // Configuration for STM32WB series #elif defined(STM32WB) @@ -439,6 +464,8 @@ #define MICROPY_HW_MAX_UART (1) #define MICROPY_HW_MAX_LPUART (1) +#define CFG_TUSB_MCU OPT_MCU_STM32WB + #ifndef MICROPY_HW_STM32WB_FLASH_SYNCRONISATION #define MICROPY_HW_STM32WB_FLASH_SYNCRONISATION (1) #endif @@ -630,10 +657,10 @@ #define MICROPY_HW_USB_CDC_NUM (1) #endif #ifndef MICROPY_HW_USB_MSC -#define MICROPY_HW_USB_MSC (MICROPY_HW_ENABLE_USB) +#define MICROPY_HW_USB_MSC (0) #endif #ifndef MICROPY_HW_USB_HID -#define MICROPY_HW_USB_HID (MICROPY_HW_ENABLE_USB) +#define MICROPY_HW_USB_HID (0) #endif // Pin definition header file diff --git a/ports/stm32/mpconfigport.h b/ports/stm32/mpconfigport.h index 25fc9e11f9d68..264e077374b98 100644 --- a/ports/stm32/mpconfigport.h +++ b/ports/stm32/mpconfigport.h @@ -161,6 +161,7 @@ #define MICROPY_FATFS_USE_LABEL (1) #define MICROPY_FATFS_RPATH (2) #define MICROPY_FATFS_MULTI_PARTITION (1) +#define MICROPY_FATFS_MAX_SS (4096) #if MICROPY_PY_PYB extern const struct _mp_obj_module_t pyb_module; diff --git a/ports/stm32/mphalport.c b/ports/stm32/mphalport.c index dfd50cebd3dee..25cc0c04be3df 100644 --- a/ports/stm32/mphalport.c +++ b/ports/stm32/mphalport.c @@ -7,6 +7,16 @@ #include "extmod/misc.h" #include "usb.h" #include "uart.h" +#include "shared/tinyusb/mp_usbd_cdc.h" + +#ifndef MICROPY_HW_STDIN_BUFFER_LEN +#define MICROPY_HW_STDIN_BUFFER_LEN 128 +#endif + +extern volatile uint32_t ticks_us64_upper; + +static uint8_t stdin_ringbuf_array[MICROPY_HW_STDIN_BUFFER_LEN]; +ringbuf_t stdin_ringbuf = { stdin_ringbuf_array, sizeof(stdin_ringbuf_array), 0, 0 }; // this table converts from HAL_StatusTypeDef to POSIX errno const byte mp_hal_status_to_errno_table[4] = { @@ -26,6 +36,9 @@ NORETURN void mp_hal_raise(HAL_StatusTypeDef status) { MP_WEAK uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) { uintptr_t ret = 0; + #if MICROPY_HW_ENABLE_USBDEV && MICROPY_HW_USB_CDC + ret |= mp_usbd_cdc_poll_interfaces(poll_flags); + #endif if (MP_STATE_PORT(pyb_stdio_uart) != NULL) { mp_obj_t pyb_stdio_uart = MP_OBJ_FROM_PTR(MP_STATE_PORT(pyb_stdio_uart)); int errcode; @@ -53,6 +66,13 @@ MP_WEAK int mp_hal_stdin_rx_chr(void) { if (dupterm_c >= 0) { return dupterm_c; } + #if MICROPY_HW_ENABLE_USBDEV && MICROPY_HW_USB_CDC + mp_usbd_cdc_poll_interfaces(0); + #endif + int c = ringbuf_get(&stdin_ringbuf); + if (c != -1) { + return c; + } MICROPY_EVENT_POLL_HOOK } } @@ -64,6 +84,13 @@ MP_WEAK mp_uint_t mp_hal_stdout_tx_strn(const char *str, size_t len) { uart_tx_strn(MP_STATE_PORT(pyb_stdio_uart), str, len); did_write = true; } + #if MICROPY_HW_ENABLE_USBDEV && MICROPY_HW_USB_CDC + mp_uint_t cdc_res = mp_usbd_cdc_tx_strn(str, len); + if (cdc_res > 0) { + did_write = true; + ret = MIN(cdc_res, ret); + } + #endif #if 0 && defined(USE_HOST_MODE) && MICROPY_HW_HAS_LCD lcd_print_strn(str, len); #endif diff --git a/ports/stm32/mphalport.h b/ports/stm32/mphalport.h index e520bc54ae6c0..807f11a35d540 100644 --- a/ports/stm32/mphalport.h +++ b/ports/stm32/mphalport.h @@ -1,6 +1,7 @@ // We use the ST Cube HAL library for most hardware peripherals #include STM32_HAL_H #include "pin.h" +#include "py/ringbuf.h" extern uint8_t mp_hal_unique_id_address[12]; @@ -37,6 +38,9 @@ static inline int mp_hal_status_to_neg_errno(HAL_StatusTypeDef status) { return -mp_hal_status_to_errno_table[status]; } +extern int mp_interrupt_char; +extern ringbuf_t stdin_ringbuf; + NORETURN void mp_hal_raise(HAL_StatusTypeDef status); void mp_hal_set_interrupt_char(int c); // -1 to disable diff --git a/ports/stm32/stm32_it.c b/ports/stm32/stm32_it.c index bc1feceb9c2b5..e1af14bb69b2e 100644 --- a/ports/stm32/stm32_it.c +++ b/ports/stm32/stm32_it.c @@ -81,7 +81,9 @@ #include "storage.h" #include "dma.h" #include "i2c.h" -#include "usb.h" + +#include "tusb.h" +#include "shared/tinyusb/mp_usbd.h" #if defined(MICROPY_HW_USB_FS) extern PCD_HandleTypeDef pcd_fs_handle; @@ -149,7 +151,7 @@ void HardFault_C_Handler(ExceptionRegisters_t *regs) { #if MICROPY_HW_ENABLE_USB // We need to disable the USB so it doesn't try to write data out on // the VCP and then block indefinitely waiting for the buffer to drain. - pyb_usb_flags = 0; + // pyb_usb_flags = 0; #endif mp_hal_stdout_tx_str("HardFault\r\n"); @@ -300,7 +302,8 @@ void DebugMon_Handler(void) { #if MICROPY_HW_USB_FS void USB_UCPD1_2_IRQHandler(void) { - HAL_PCD_IRQHandler(&pcd_fs_handle); + tud_int_handler(0); + // HAL_PCD_IRQHandler(&pcd_fs_handle); } #endif @@ -308,7 +311,8 @@ void USB_UCPD1_2_IRQHandler(void) { #if MICROPY_HW_USB_FS void USB_DRD_FS_IRQHandler(void) { - HAL_PCD_IRQHandler(&pcd_fs_handle); + tud_int_handler(0); + // HAL_PCD_IRQHandler(&pcd_fs_handle); } #endif @@ -316,7 +320,8 @@ void USB_DRD_FS_IRQHandler(void) { #if MICROPY_HW_USB_FS void USB_IRQHandler(void) { - HAL_PCD_IRQHandler(&pcd_fs_handle); + tud_int_handler(0); + // HAL_PCD_IRQHandler(&pcd_fs_handle); } #endif @@ -324,10 +329,16 @@ void USB_IRQHandler(void) { #if MICROPY_HW_USB_FS void USB_LP_IRQHandler(void) { - HAL_PCD_IRQHandler(&pcd_fs_handle); + // This function handles USB low priority interrupt, USB wake-up interrupt through EXTI line 28. + tud_int_handler(0); + // HAL_PCD_IRQHandler(&pcd_fs_handle); } #endif +void USB_HP_IRQHandler(void) { + tud_int_handler(0); + // HAL_PCD_IRQHandler(&pcd_fs_handle); +} #else /** @@ -338,14 +349,16 @@ void USB_LP_IRQHandler(void) { #if MICROPY_HW_USB_FS void OTG_FS_IRQHandler(void) { IRQ_ENTER(OTG_FS_IRQn); - HAL_PCD_IRQHandler(&pcd_fs_handle); + tud_int_handler(0); + // HAL_PCD_IRQHandler(&pcd_fs_handle); IRQ_EXIT(OTG_FS_IRQn); } #endif #if MICROPY_HW_USB_HS void OTG_HS_IRQHandler(void) { IRQ_ENTER(OTG_HS_IRQn); - HAL_PCD_IRQHandler(&pcd_hs_handle); + tud_int_handler(0); + // HAL_PCD_IRQHandler(&pcd_hs_handle); IRQ_EXIT(OTG_HS_IRQn); } #endif diff --git a/ports/stm32/usb.h b/ports/stm32/usb.h index 3c382887adce5..f456a4a843562 100644 --- a/ports/stm32/usb.h +++ b/ports/stm32/usb.h @@ -26,7 +26,7 @@ #ifndef MICROPY_INCLUDED_STM32_USB_H #define MICROPY_INCLUDED_STM32_USB_H -#include "usbd_cdc_msc_hid0.h" +// #include "usbd_cdc_msc_hid0.h" #define PYB_USB_FLAG_USB_MODE_CALLED (0x0002) @@ -56,7 +56,7 @@ MP_DECLARE_CONST_FUN_OBJ_1(pyb_hid_send_report_obj); // deprecated void pyb_usb_init0(void); int pyb_usb_dev_detect(void); -bool pyb_usb_dev_init(int dev_id, uint16_t vid, uint16_t pid, uint8_t mode, size_t msc_n, const void *msc_unit, USBD_HID_ModeInfoTypeDef *hid_info); +// bool pyb_usb_dev_init(int dev_id, uint16_t vid, uint16_t pid, uint8_t mode, size_t msc_n, const void *msc_unit, USBD_HID_ModeInfoTypeDef *hid_info); void pyb_usb_dev_deinit(void); bool usb_vcp_is_enabled(void); int usb_vcp_recv_byte(uint8_t *c); // if a byte is available, return 1 and put the byte in *c, else return 0 diff --git a/ports/stm32/usbd_conf.c b/ports/stm32/usbd_conf.c index 5d9e7177de564..865511d590acd 100644 --- a/ports/stm32/usbd_conf.c +++ b/ports/stm32/usbd_conf.c @@ -62,7 +62,7 @@ PCD_HandleTypeDef pcd_hs_handle; */ void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd) { #if MICROPY_HW_USB_FS - if (hpcd->Instance == USB_OTG_FS) { + // if (hpcd->Instance == USB_OTG_FS) { // Configure USB GPIO's. #if defined(STM32G0) || defined(STM32G4) @@ -168,11 +168,11 @@ void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd) { #endif return; - } + // } #endif #if MICROPY_HW_USB_HS - if (hpcd->Instance == USB_OTG_HS) { + // if (hpcd->Instance == USB_OTG_HS) { #if MICROPY_HW_USB_HS_IN_FS // Configure USB GPIO's. @@ -258,7 +258,7 @@ void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd) { // Configure and enable USB HS interrupt NVIC_SetPriority(OTG_HS_IRQn, IRQ_PRI_OTG_HS); HAL_NVIC_EnableIRQ(OTG_HS_IRQn); - } + // } #endif // MICROPY_HW_USB_HS } diff --git a/shared/runtime/pyexec.c b/shared/runtime/pyexec.c index 9dc4446ed4d66..76ca46978fac7 100644 --- a/shared/runtime/pyexec.c +++ b/shared/runtime/pyexec.c @@ -583,7 +583,7 @@ int pyexec_friendly_repl(void) { for (;;) { input_restart: - #if MICROPY_HW_ENABLE_USB + #if MICROPY_HW_ENABLE_USB && 0 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/tusb_config.h b/shared/tinyusb/tusb_config.h index 1f8f5e5f6dc56..445c541c54693 100644 --- a/shared/tinyusb/tusb_config.h +++ b/shared/tinyusb/tusb_config.h @@ -60,8 +60,13 @@ #endif #ifndef CFG_TUSB_RHPORT0_MODE -#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE) +#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE | OPT_MODE_FULL_SPEED) #endif +#define CFG_TUSB_RHPORT1_MODE 0 + +#define BOARD_DEVICE_RHPORT_SPEED OPT_MODE_FULL_SPEED +#define BOARD_DEVICE_RHPORT_NUM 0 + #if MICROPY_HW_USB_CDC #define CFG_TUD_CDC (1)