Skip to content

Commit 9bb266e

Browse files
pi-anldpgeorge
authored andcommitted
stm32/usb: Add support for using TinyUSB stack.
This commit adapts the stm32 port to allow switching from STM USB stack to TinyUSB stack. Using TinyUSB improves consistancy with other MicroPython ports and brings in the ability to use the runtime USB definition support recently added to other TinyUSB based ports. By default the existing STM USB stack is used. TinyUSB can be enabled in a board configuration with: #define MICROPY_HW_TINYUSB_STACK (1) Or, it can be enabled from the command line with: make -C ports/stm32 CFLAGS_EXTRA='-DMICROPY_HW_TINYUSB_STACK=1' Signed-off-by: Andrew Leech <andrew@alelec.net>
1 parent 6b0e1c2 commit 9bb266e

File tree

14 files changed

+256
-40
lines changed

14 files changed

+256
-40
lines changed

ports/stm32/Makefile

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ MBOOT_TEXT0_ADDR ?= 0x08000000
5858
include $(TOP)/py/py.mk
5959
include $(TOP)/extmod/extmod.mk
6060

61-
GIT_SUBMODULES += lib/libhydrogen lib/stm32lib
61+
GIT_SUBMODULES += lib/libhydrogen lib/stm32lib lib/tinyusb
6262

6363
CROSS_COMPILE ?= arm-none-eabi-
6464
LD_DIR=boards
@@ -110,6 +110,8 @@ INC += -I$(STM32LIB_CMSIS_ABS)/Include
110110
INC += -I$(STM32LIB_HAL_ABS)/Inc
111111
INC += -I$(USBDEV_DIR)/core/inc -I$(USBDEV_DIR)/class/inc
112112
#INC += -I$(USBHOST_DIR)
113+
INC += -I$(TOP)/lib/tinyusb/src
114+
INC += -I$(TOP)/shared/tinyusb/
113115
INC += -Ilwip_inc
114116

115117
CFLAGS += $(INC) -Wall -Wpointer-arith -Werror -Wdouble-promotion -Wfloat-conversion -std=gnu99 -nostdlib $(CFLAGS_EXTRA)
@@ -213,6 +215,10 @@ SHARED_SRC_C += $(addprefix shared/,\
213215
runtime/stdout_helpers.c \
214216
runtime/sys_stdio_mphal.c \
215217
timeutils/timeutils.c \
218+
tinyusb/mp_usbd.c \
219+
tinyusb/mp_usbd_cdc.c \
220+
tinyusb/mp_usbd_descriptor.c \
221+
tinyusb/mp_usbd_runtime.c \
216222
)
217223

218224
ifeq ($(MICROPY_FLOAT_IMPL),double)
@@ -242,7 +248,9 @@ SRC_C += \
242248
boardctrl.c \
243249
main.c \
244250
stm32_it.c \
251+
usbd.c \
245252
usbd_conf.c \
253+
usb.c \
246254
usbd_desc.c \
247255
usbd_cdc_interface.c \
248256
usbd_hid_interface.c \
@@ -277,7 +285,6 @@ SRC_C += \
277285
can.c \
278286
fdcan.c \
279287
pyb_can.c \
280-
usb.c \
281288
eth.c \
282289
eth_phy.c \
283290
gccollect.c \
@@ -460,6 +467,16 @@ USBDEV_SRC_C += $(addprefix $(USBDEV_DIR)/,\
460467
class/src/usbd_msc_scsi.c \
461468
)
462469

470+
# TinyUSB Stack source
471+
-include $(TOP)/lib/tinyusb/src/tinyusb.mk
472+
TINYUSB_SRC_C := $(addprefix lib/tinyusb/, \
473+
$(TINYUSB_SRC_C) \
474+
src/portable/st/stm32_fsdev/dcd_stm32_fsdev.c \
475+
src/portable/synopsys/dwc2/dcd_dwc2.c \
476+
src/portable/synopsys/dwc2/dwc2_common.c \
477+
src/portable/synopsys/dwc2/hcd_dwc2.c \
478+
)
479+
463480
ifeq ($(MICROPY_SSL_MBEDTLS),1)
464481
LIB_SRC_C += mbedtls/mbedtls_port.c
465482
endif
@@ -499,6 +516,7 @@ OBJ += $(addprefix $(BUILD)/, $(SHARED_SRC_C:.c=.o))
499516
OBJ += $(addprefix $(BUILD)/, $(DRIVERS_SRC_C:.c=.o))
500517
OBJ += $(addprefix $(BUILD)/, $(HAL_SRC_C:.c=.o))
501518
OBJ += $(addprefix $(BUILD)/, $(USBDEV_SRC_C:.c=.o))
519+
OBJ += $(addprefix $(BUILD)/, $(TINYUSB_SRC_C:.c=.o))
502520
OBJ += $(addprefix $(BUILD)/, $(SRC_C:.c=.o))
503521
OBJ += $(addprefix $(BUILD)/, $(SRC_CXX:.cpp=.o))
504522
OBJ += $(GEN_PINS_SRC:.c=.o)

ports/stm32/main.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@
8888
#include "pyb_can.h"
8989
#include "subghz.h"
9090

91+
#if MICROPY_HW_TINYUSB_STACK
92+
#include "usbd_conf.h"
93+
#include "shared/tinyusb/mp_usbd.h"
94+
#endif
95+
9196
#if MICROPY_PY_THREAD
9297
static pyb_thread_t pyb_thread_main;
9398
#endif
@@ -275,14 +280,12 @@ static bool init_sdcard_fs(void) {
275280
}
276281
}
277282

278-
#if MICROPY_HW_ENABLE_USB
283+
#if MICROPY_HW_ENABLE_USB && !MICROPY_HW_TINYUSB_STACK
279284
if (pyb_usb_storage_medium == PYB_USB_STORAGE_MEDIUM_NONE) {
280285
// if no USB MSC medium is selected then use the SD card
281286
pyb_usb_storage_medium = PYB_USB_STORAGE_MEDIUM_SDCARD;
282287
}
283-
#endif
284288

285-
#if MICROPY_HW_ENABLE_USB
286289
// only use SD card as current directory if that's what the USB medium is
287290
if (pyb_usb_storage_medium == PYB_USB_STORAGE_MEDIUM_SDCARD)
288291
#endif
@@ -606,8 +609,13 @@ void stm32_main(uint32_t reset_mode) {
606609
#endif
607610

608611
#if MICROPY_HW_ENABLE_USB
612+
#if MICROPY_HW_TINYUSB_STACK
613+
pyb_usbd_init();
614+
mp_usbd_init();
615+
#else
609616
pyb_usb_init0();
610617
#endif
618+
#endif
611619

612620
#if MICROPY_PY_MACHINE_I2S
613621
machine_i2s_init0();
@@ -631,7 +639,7 @@ void stm32_main(uint32_t reset_mode) {
631639
}
632640
#endif
633641

634-
#if MICROPY_HW_ENABLE_USB
642+
#if MICROPY_HW_STM_USB_STACK
635643
// if the SD card isn't used as the USB MSC medium then use the internal flash
636644
if (pyb_usb_storage_medium == PYB_USB_STORAGE_MEDIUM_NONE) {
637645
pyb_usb_storage_medium = PYB_USB_STORAGE_MEDIUM_FLASH;
@@ -665,7 +673,7 @@ void stm32_main(uint32_t reset_mode) {
665673
// or whose initialisation can be safely deferred until after running
666674
// boot.py.
667675

668-
#if MICROPY_HW_ENABLE_USB
676+
#if MICROPY_HW_STM_USB_STACK
669677
// init USB device to default setting if it was not already configured
670678
if (!(pyb_usb_flags & PYB_USB_FLAG_USB_MODE_CALLED)) {
671679
#if MICROPY_HW_USB_MSC
@@ -770,6 +778,9 @@ void stm32_main(uint32_t reset_mode) {
770778
#else
771779
MP_STATE_PORT(pyb_stdio_uart) = NULL;
772780
#endif
781+
#if MICROPY_HW_ENABLE_USB_RUNTIME_DEVICE && MICROPY_HW_TINYUSB_STACK
782+
mp_usbd_deinit();
783+
#endif
773784

774785
MICROPY_BOARD_END_SOFT_RESET(&state);
775786

ports/stm32/modmachine.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
#include "rtc.h"
4848
#include "i2c.h"
4949
#include "spi.h"
50+
#include "shared/tinyusb/mp_usbd.h"
5051

5152
#if defined(STM32G0)
5253
// G0 has BOR and POR combined
@@ -297,9 +298,13 @@ MP_NORETURN static void mp_machine_reset(void) {
297298

298299
// Activate the bootloader without BOOT* pins.
299300
MP_NORETURN void mp_machine_bootloader(size_t n_args, const mp_obj_t *args) {
300-
#if MICROPY_HW_ENABLE_USB
301+
#if MICROPY_HW_STM_USB_STACK
301302
pyb_usb_dev_deinit();
302303
#endif
304+
#if MICROPY_HW_ENABLE_USB_RUNTIME_DEVICE && MICROPY_HW_TINYUSB_STACK
305+
mp_usbd_deinit();
306+
#endif
307+
303308
#if MICROPY_HW_ENABLE_STORAGE
304309
storage_flush();
305310
#endif

ports/stm32/modos.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ bool mp_os_dupterm_is_builtin_stream(mp_const_obj_t stream) {
5252
#if MICROPY_PY_MACHINE_UART
5353
|| type == &machine_uart_type
5454
#endif
55-
#if MICROPY_HW_ENABLE_USB
55+
#if MICROPY_HW_STM_USB_STACK
5656
|| type == &pyb_usb_vcp_type
5757
#endif
5858
;
@@ -64,7 +64,7 @@ void mp_os_dupterm_stream_detached_attached(mp_obj_t stream_detached, mp_obj_t s
6464
uart_attach_to_repl(MP_OBJ_TO_PTR(stream_detached), false);
6565
}
6666
#endif
67-
#if MICROPY_HW_ENABLE_USB
67+
#if MICROPY_HW_STM_USB_STACK
6868
if (mp_obj_get_type(stream_detached) == &pyb_usb_vcp_type) {
6969
usb_vcp_attach_to_repl(MP_OBJ_TO_PTR(stream_detached), false);
7070
}
@@ -75,7 +75,7 @@ void mp_os_dupterm_stream_detached_attached(mp_obj_t stream_detached, mp_obj_t s
7575
uart_attach_to_repl(MP_OBJ_TO_PTR(stream_attached), true);
7676
}
7777
#endif
78-
#if MICROPY_HW_ENABLE_USB
78+
#if MICROPY_HW_STM_USB_STACK
7979
if (mp_obj_get_type(stream_attached) == &pyb_usb_vcp_type) {
8080
usb_vcp_attach_to_repl(MP_OBJ_TO_PTR(stream_attached), true);
8181
}

ports/stm32/modpyb.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ static const mp_rom_map_elem_t pyb_module_globals_table[] = {
167167
// Deprecated (use network.country instead).
168168
{ MP_ROM_QSTR(MP_QSTR_country), MP_ROM_PTR(&mod_network_country_obj) },
169169

170-
#if MICROPY_HW_ENABLE_USB
170+
#if MICROPY_HW_STM_USB_STACK
171171
{ MP_ROM_QSTR(MP_QSTR_usb_mode), MP_ROM_PTR(&pyb_usb_mode_obj) },
172172
#if MICROPY_HW_USB_HID
173173
{ MP_ROM_QSTR(MP_QSTR_hid_mouse), MP_ROM_PTR(&pyb_usb_hid_mouse_obj) },

ports/stm32/mpconfigboard_common.h

Lines changed: 60 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -250,12 +250,50 @@
250250
#error "Old USBD_xxx configuration option used, renamed to MICROPY_HW_USB_xxx"
251251
#endif
252252

253+
// Select whether TinyUSB or legacy STM stack is used to provide USB.
254+
#ifndef MICROPY_HW_TINYUSB_STACK
255+
#define MICROPY_HW_TINYUSB_STACK (0)
256+
#endif
257+
258+
// Central definition for STM USB stack (when not using TinyUSB)
259+
#define MICROPY_HW_STM_USB_STACK (MICROPY_HW_ENABLE_USB && !MICROPY_HW_TINYUSB_STACK)
260+
261+
#if MICROPY_HW_TINYUSB_STACK
262+
#ifndef MICROPY_HW_ENABLE_USBDEV
263+
#define MICROPY_HW_ENABLE_USBDEV (1)
264+
#endif
265+
266+
#ifndef MICROPY_HW_USB_CDC
267+
#define MICROPY_HW_USB_CDC (1)
268+
#endif
269+
270+
#ifndef MICROPY_HW_ENABLE_USB_RUNTIME_DEVICE
271+
#define MICROPY_HW_ENABLE_USB_RUNTIME_DEVICE (1) // Support machine.USBDevice
272+
#endif
273+
#endif
274+
275+
// Configure maximum number of CDC VCP interfaces, and whether MSC/HID are supported
276+
#ifndef MICROPY_HW_USB_CDC_NUM
277+
#define MICROPY_HW_USB_CDC_NUM (1)
278+
#endif
279+
#ifndef MICROPY_HW_USB_MSC
280+
#define MICROPY_HW_USB_MSC (MICROPY_HW_STM_USB_STACK)
281+
#endif
282+
#ifndef MICROPY_HW_USB_HID
283+
#define MICROPY_HW_USB_HID (MICROPY_HW_STM_USB_STACK)
284+
#endif
285+
253286
// Default VID and PID values to use for the USB device. If MICROPY_HW_USB_VID
254287
// is defined by a board then all needed PID options must also be defined. The
255288
// VID and PID can also be set dynamically in pyb.usb_mode().
256289
// Windows needs a different PID to distinguish different device configurations.
257290
#ifndef MICROPY_HW_USB_VID
258291
#define MICROPY_HW_USB_VID (0xf055)
292+
293+
// USB PID for TinyUSB Stack.
294+
#define MICROPY_HW_USB_PID (0x9802)
295+
296+
// USB PID table for STM USB stack.
259297
#define MICROPY_HW_USB_PID_CDC_MSC (0x9800)
260298
#define MICROPY_HW_USB_PID_CDC_HID (0x9801)
261299
#define MICROPY_HW_USB_PID_CDC (0x9802)
@@ -369,6 +407,8 @@
369407
#endif
370408
#define MICROPY_HW_MAX_LPUART (0)
371409

410+
#define CFG_TUSB_MCU OPT_MCU_STM32F4
411+
372412
// Configuration for STM32F7 series
373413
#elif defined(STM32F7)
374414

@@ -384,6 +424,8 @@
384424
#define MICROPY_HW_MAX_UART (8)
385425
#define MICROPY_HW_MAX_LPUART (0)
386426

427+
#define CFG_TUSB_MCU OPT_MCU_STM32F7
428+
387429
// Configuration for STM32G0 series
388430
#elif defined(STM32G0)
389431

@@ -394,6 +436,8 @@
394436
#define MICROPY_HW_MAX_UART (6)
395437
#define MICROPY_HW_MAX_LPUART (2)
396438

439+
#define CFG_TUSB_MCU OPT_MCU_STM32G0
440+
397441
// Configuration for STM32G4 series
398442
#elif defined(STM32G4)
399443

@@ -404,6 +448,8 @@
404448
#define MICROPY_HW_MAX_UART (5) // UART1-5 + LPUART1
405449
#define MICROPY_HW_MAX_LPUART (1)
406450

451+
#define CFG_TUSB_MCU OPT_MCU_STM32G4
452+
407453
// Configuration for STM32H5 series
408454
#elif defined(STM32H5)
409455

@@ -414,6 +460,8 @@
414460
#define MICROPY_HW_MAX_UART (12)
415461
#define MICROPY_HW_MAX_LPUART (1)
416462

463+
#define CFG_TUSB_MCU OPT_MCU_STM32H5
464+
417465
// Configuration for STM32H7A3/B3 series
418466
#elif defined(STM32H7A3xx) || defined(STM32H7A3xxQ) || \
419467
defined(STM32H7B3xx) || defined(STM32H7B3xxQ)
@@ -425,6 +473,8 @@
425473
#define MICROPY_HW_MAX_UART (10)
426474
#define MICROPY_HW_MAX_LPUART (1)
427475

476+
#define CFG_TUSB_MCU OPT_MCU_STM32H7
477+
428478
// Configuration for STM32H7 series
429479
#elif defined(STM32H7)
430480

@@ -435,6 +485,8 @@
435485
#define MICROPY_HW_MAX_UART (8)
436486
#define MICROPY_HW_MAX_LPUART (1)
437487

488+
#define CFG_TUSB_MCU OPT_MCU_STM32H7
489+
438490
#if defined(MICROPY_HW_ANALOG_SWITCH_PA0) \
439491
|| defined(MICROPY_HW_ANALOG_SWITCH_PA1) \
440492
|| defined(MICROPY_HW_ANALOG_SWITCH_PC2) \
@@ -454,6 +506,8 @@
454506
#define MICROPY_HW_MAX_UART (5)
455507
#define MICROPY_HW_MAX_LPUART (1)
456508

509+
#define CFG_TUSB_MCU OPT_MCU_STM32L0
510+
457511
// Configuration for STM32L1 series
458512
#elif defined(STM32L1)
459513
#define MP_HAL_UNIQUE_ID_ADDRESS (UID_BASE)
@@ -464,6 +518,8 @@
464518
#define MICROPY_HW_MAX_UART (5)
465519
#define MICROPY_HW_MAX_LPUART (0)
466520

521+
#define CFG_TUSB_MCU OPT_MCU_STM32L1
522+
467523
// Configuration for STM32L4 series
468524
#elif defined(STM32L4)
469525

@@ -474,6 +530,8 @@
474530
#define MICROPY_HW_MAX_UART (5)
475531
#define MICROPY_HW_MAX_LPUART (1)
476532

533+
#define CFG_TUSB_MCU OPT_MCU_STM32L4
534+
477535
// Configuration for STM32N6 series
478536
#elif defined(STM32N6)
479537

@@ -508,6 +566,8 @@
508566
#define MICROPY_HW_MAX_UART (1)
509567
#define MICROPY_HW_MAX_LPUART (1)
510568

569+
#define CFG_TUSB_MCU OPT_MCU_STM32WB
570+
511571
#ifndef MICROPY_HW_STM32WB_FLASH_SYNCRONISATION
512572
#define MICROPY_HW_STM32WB_FLASH_SYNCRONISATION (1)
513573
#endif
@@ -715,17 +775,6 @@
715775
#define MICROPY_HW_USB_IS_MULTI_OTG (1)
716776
#endif
717777

718-
// Configure maximum number of CDC VCP interfaces, and whether MSC/HID are supported
719-
#ifndef MICROPY_HW_USB_CDC_NUM
720-
#define MICROPY_HW_USB_CDC_NUM (1)
721-
#endif
722-
#ifndef MICROPY_HW_USB_MSC
723-
#define MICROPY_HW_USB_MSC (MICROPY_HW_ENABLE_USB)
724-
#endif
725-
#ifndef MICROPY_HW_USB_HID
726-
#define MICROPY_HW_USB_HID (MICROPY_HW_ENABLE_USB)
727-
#endif
728-
729778
// Pin definition header file
730779
#define MICROPY_PIN_DEFS_PORT_H "pin_defs_stm32.h"
731780

0 commit comments

Comments
 (0)