Skip to content

Commit 916f558

Browse files
committed
stm32/usb: Add support for using TinyUSB stack.
Signed-off-by: Andrew Leech <andrew@alelec.net>
1 parent e2f26a2 commit 916f558

File tree

15 files changed

+417
-198
lines changed

15 files changed

+417
-198
lines changed

ports/stm32/Makefile

Lines changed: 19 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,9 @@ 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/
115+
113116
INC += -Ilwip_inc
114117

115118
CFLAGS += $(INC) -Wall -Wpointer-arith -Werror -Wdouble-promotion -Wfloat-conversion -std=gnu99 -nostdlib $(CFLAGS_EXTRA)
@@ -213,6 +216,10 @@ SHARED_SRC_C += $(addprefix shared/,\
213216
runtime/stdout_helpers.c \
214217
runtime/sys_stdio_mphal.c \
215218
timeutils/timeutils.c \
219+
tinyusb/mp_usbd.c \
220+
tinyusb/mp_usbd_cdc.c \
221+
tinyusb/mp_usbd_descriptor.c \
222+
tinyusb/mp_usbd_runtime.c \
216223
)
217224

218225
ifeq ($(MICROPY_FLOAT_IMPL),double)
@@ -238,11 +245,21 @@ DRIVERS_SRC_C += $(addprefix drivers/,\
238245
dht/dht.c \
239246
)
240247

248+
# TinyUSB Stack source
249+
-include $(TOP)/lib/tinyusb/src/tinyusb.mk
250+
TINYUSB_SRC_C := $(addprefix lib/tinyusb/, \
251+
$(TINYUSB_SRC_C) \
252+
src/portable/st/stm32_fsdev/dcd_stm32_fsdev.c \
253+
src/portable/synopsys/dwc2/dcd_dwc2.c \
254+
)
255+
241256
SRC_C += \
242257
boardctrl.c \
243258
main.c \
244259
stm32_it.c \
260+
usbd.c \
245261
usbd_conf.c \
262+
usb.c \
246263
usbd_desc.c \
247264
usbd_cdc_interface.c \
248265
usbd_hid_interface.c \
@@ -277,7 +294,6 @@ SRC_C += \
277294
can.c \
278295
fdcan.c \
279296
pyb_can.c \
280-
usb.c \
281297
eth.c \
282298
eth_phy.c \
283299
gccollect.c \
@@ -499,6 +515,7 @@ OBJ += $(addprefix $(BUILD)/, $(SHARED_SRC_C:.c=.o))
499515
OBJ += $(addprefix $(BUILD)/, $(DRIVERS_SRC_C:.c=.o))
500516
OBJ += $(addprefix $(BUILD)/, $(HAL_SRC_C:.c=.o))
501517
OBJ += $(addprefix $(BUILD)/, $(USBDEV_SRC_C:.c=.o))
518+
OBJ += $(addprefix $(BUILD)/, $(TINYUSB_SRC_C:.c=.o))
502519
OBJ += $(addprefix $(BUILD)/, $(SRC_C:.c=.o))
503520
OBJ += $(addprefix $(BUILD)/, $(SRC_CXX:.cpp=.o))
504521
OBJ += $(GEN_PINS_SRC:.c=.o)

ports/stm32/main.c

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@
7777
#include "pin.h"
7878
#include "extint.h"
7979
#include "usrsw.h"
80-
#include "usb.h"
8180
#include "rtc.h"
8281
#include "storage.h"
8382
#include "sdcard.h"
@@ -89,6 +88,13 @@
8988
#include "pyb_can.h"
9089
#include "subghz.h"
9190

91+
#if MICROPY_HW_TINYUSB_STACK
92+
#include "usbd_conf.h"
93+
#include "shared/tinyusb/mp_usbd.h"
94+
#else
95+
#include "usb.h"
96+
#endif
97+
9298
#if MICROPY_PY_THREAD
9399
static pyb_thread_t pyb_thread_main;
94100
#endif
@@ -276,14 +282,12 @@ static bool init_sdcard_fs(void) {
276282
}
277283
}
278284

279-
#if MICROPY_HW_ENABLE_USB
285+
#if MICROPY_HW_ENABLE_USB && !MICROPY_HW_TINYUSB_STACK
280286
if (pyb_usb_storage_medium == PYB_USB_STORAGE_MEDIUM_NONE) {
281287
// if no USB MSC medium is selected then use the SD card
282288
pyb_usb_storage_medium = PYB_USB_STORAGE_MEDIUM_SDCARD;
283289
}
284-
#endif
285290

286-
#if MICROPY_HW_ENABLE_USB
287291
// only use SD card as current directory if that's what the USB medium is
288292
if (pyb_usb_storage_medium == PYB_USB_STORAGE_MEDIUM_SDCARD)
289293
#endif
@@ -604,8 +608,13 @@ void stm32_main(uint32_t reset_mode) {
604608
#endif
605609

606610
#if MICROPY_HW_ENABLE_USB
611+
#if MICROPY_HW_TINYUSB_STACK
612+
pyb_usbd_init();
613+
mp_usbd_init();
614+
#else
607615
pyb_usb_init0();
608616
#endif
617+
#endif
609618

610619
#if MICROPY_PY_MACHINE_I2S
611620
machine_i2s_init0();
@@ -629,7 +638,7 @@ void stm32_main(uint32_t reset_mode) {
629638
}
630639
#endif
631640

632-
#if MICROPY_HW_ENABLE_USB
641+
#if MICROPY_HW_STM_USB_STACK
633642
// if the SD card isn't used as the USB MSC medium then use the internal flash
634643
if (pyb_usb_storage_medium == PYB_USB_STORAGE_MEDIUM_NONE) {
635644
pyb_usb_storage_medium = PYB_USB_STORAGE_MEDIUM_FLASH;
@@ -663,7 +672,7 @@ void stm32_main(uint32_t reset_mode) {
663672
// or whose initialisation can be safely deferred until after running
664673
// boot.py.
665674

666-
#if MICROPY_HW_ENABLE_USB
675+
#if MICROPY_HW_STM_USB_STACK
667676
// init USB device to default setting if it was not already configured
668677
if (!(pyb_usb_flags & PYB_USB_FLAG_USB_MODE_CALLED)) {
669678
#if MICROPY_HW_USB_MSC
@@ -768,6 +777,9 @@ void stm32_main(uint32_t reset_mode) {
768777
#else
769778
MP_STATE_PORT(pyb_stdio_uart) = NULL;
770779
#endif
780+
#if MICROPY_HW_ENABLE_USB_RUNTIME_DEVICE && MICROPY_HW_TINYUSB_STACK
781+
mp_usbd_deinit();
782+
#endif
771783

772784
MICROPY_BOARD_END_SOFT_RESET(&state);
773785

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_ENABLE_USB && !MICROPY_HW_TINYUSB_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: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,17 @@
4949
#include "servo.h"
5050
#include "dac.h"
5151
#include "lcd.h"
52-
#include "usb.h"
5352
#include "portmodules.h"
5453
#include "modmachine.h"
5554
#include "extmod/modmachine.h"
5655
#include "extmod/modnetwork.h"
5756
#include "extmod/vfs.h"
5857
#include "extmod/modtime.h"
5958

59+
#if !MICROPY_HW_TINYUSB_STACK
60+
#include "usb.h"
61+
#endif
62+
6063
#if MICROPY_PY_PYB
6164

6265
static mp_obj_t pyb_fault_debug(mp_obj_t value) {
@@ -167,7 +170,7 @@ static const mp_rom_map_elem_t pyb_module_globals_table[] = {
167170
// Deprecated (use network.country instead).
168171
{ MP_ROM_QSTR(MP_QSTR_country), MP_ROM_PTR(&mod_network_country_obj) },
169172

170-
#if MICROPY_HW_ENABLE_USB
173+
#if MICROPY_HW_STM_USB_STACK
171174
{ MP_ROM_QSTR(MP_QSTR_usb_mode), MP_ROM_PTR(&pyb_usb_mode_obj) },
172175
#if MICROPY_HW_USB_HID
173176
{ MP_ROM_QSTR(MP_QSTR_hid_mouse), MP_ROM_PTR(&pyb_usb_hid_mouse_obj) },

ports/stm32/mpconfigboard_common.h

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,24 @@
136136
#define MICROPY_HW_ENABLE_USB (0)
137137
#endif
138138

139+
#if MICROPY_HW_ENABLE_USB
140+
#define MICROPY_HW_USB_CDC (1)
141+
#define MICROPY_HW_USB_FS (1)
142+
143+
// Select whether TinyUSB or legacy STM stack is used to provide USB.
144+
#ifndef MICROPY_HW_TINYUSB_STACK
145+
#define MICROPY_HW_TINYUSB_STACK (0)
146+
#endif
147+
148+
// Central definition for STM USB stack (when not using TinyUSB)
149+
#define MICROPY_HW_STM_USB_STACK (MICROPY_HW_ENABLE_USB && !MICROPY_HW_TINYUSB_STACK)
150+
151+
#if MICROPY_HW_TINYUSB_STACK
152+
#define MICROPY_HW_ENABLE_USBDEV (1)
153+
#endif
154+
155+
#endif // MICROPY_HW_ENABLE_USB
156+
139157
// Whether to enable the PA0-PA3 servo driver, exposed as pyb.Servo
140158
#ifndef MICROPY_HW_ENABLE_SERVO
141159
#define MICROPY_HW_ENABLE_SERVO (0)
@@ -256,6 +274,8 @@
256274
// Windows needs a different PID to distinguish different device configurations.
257275
#ifndef MICROPY_HW_USB_VID
258276
#define MICROPY_HW_USB_VID (0xf055)
277+
#define MICROPY_HW_USB_PID (0x9802)
278+
259279
#define MICROPY_HW_USB_PID_CDC_MSC (0x9800)
260280
#define MICROPY_HW_USB_PID_CDC_HID (0x9801)
261281
#define MICROPY_HW_USB_PID_CDC (0x9802)
@@ -369,6 +389,8 @@
369389
#endif
370390
#define MICROPY_HW_MAX_LPUART (0)
371391

392+
#define CFG_TUSB_MCU OPT_MCU_STM32F4
393+
372394
// Configuration for STM32F7 series
373395
#elif defined(STM32F7)
374396

@@ -384,6 +406,8 @@
384406
#define MICROPY_HW_MAX_UART (8)
385407
#define MICROPY_HW_MAX_LPUART (0)
386408

409+
#define CFG_TUSB_MCU OPT_MCU_STM32F7
410+
387411
// Configuration for STM32G0 series
388412
#elif defined(STM32G0)
389413

@@ -394,6 +418,8 @@
394418
#define MICROPY_HW_MAX_UART (6)
395419
#define MICROPY_HW_MAX_LPUART (2)
396420

421+
#define CFG_TUSB_MCU OPT_MCU_STM32G0
422+
397423
// Configuration for STM32G4 series
398424
#elif defined(STM32G4)
399425

@@ -404,6 +430,8 @@
404430
#define MICROPY_HW_MAX_UART (5) // UART1-5 + LPUART1
405431
#define MICROPY_HW_MAX_LPUART (1)
406432

433+
#define CFG_TUSB_MCU OPT_MCU_STM32G4
434+
407435
// Configuration for STM32H5 series
408436
#elif defined(STM32H5)
409437

@@ -414,6 +442,8 @@
414442
#define MICROPY_HW_MAX_UART (12)
415443
#define MICROPY_HW_MAX_LPUART (1)
416444

445+
#define CFG_TUSB_MCU OPT_MCU_STM32H5
446+
417447
// Configuration for STM32H7A3/B3 series
418448
#elif defined(STM32H7A3xx) || defined(STM32H7A3xxQ) || \
419449
defined(STM32H7B3xx) || defined(STM32H7B3xxQ)
@@ -425,6 +455,8 @@
425455
#define MICROPY_HW_MAX_UART (10)
426456
#define MICROPY_HW_MAX_LPUART (1)
427457

458+
#define CFG_TUSB_MCU OPT_MCU_STM32H7
459+
428460
// Configuration for STM32H7 series
429461
#elif defined(STM32H7)
430462

@@ -435,6 +467,8 @@
435467
#define MICROPY_HW_MAX_UART (8)
436468
#define MICROPY_HW_MAX_LPUART (1)
437469

470+
#define CFG_TUSB_MCU OPT_MCU_STM32H7
471+
438472
#if defined(MICROPY_HW_ANALOG_SWITCH_PA0) \
439473
|| defined(MICROPY_HW_ANALOG_SWITCH_PA1) \
440474
|| defined(MICROPY_HW_ANALOG_SWITCH_PC2) \
@@ -454,6 +488,8 @@
454488
#define MICROPY_HW_MAX_UART (5)
455489
#define MICROPY_HW_MAX_LPUART (1)
456490

491+
#define CFG_TUSB_MCU OPT_MCU_STM32L0
492+
457493
// Configuration for STM32L1 series
458494
#elif defined(STM32L1)
459495
#define MP_HAL_UNIQUE_ID_ADDRESS (UID_BASE)
@@ -464,6 +500,8 @@
464500
#define MICROPY_HW_MAX_UART (5)
465501
#define MICROPY_HW_MAX_LPUART (0)
466502

503+
#define CFG_TUSB_MCU OPT_MCU_STM32L1
504+
467505
// Configuration for STM32L4 series
468506
#elif defined(STM32L4)
469507

@@ -474,6 +512,8 @@
474512
#define MICROPY_HW_MAX_UART (5)
475513
#define MICROPY_HW_MAX_LPUART (1)
476514

515+
#define CFG_TUSB_MCU OPT_MCU_STM32L4
516+
477517
// Configuration for STM32N6 series
478518
#elif defined(STM32N6)
479519

@@ -494,6 +534,8 @@
494534
#define MICROPY_HW_MAX_UART (1)
495535
#define MICROPY_HW_MAX_LPUART (1)
496536

537+
#define CFG_TUSB_MCU OPT_MCU_STM32WB
538+
497539
#ifndef MICROPY_HW_STM32WB_FLASH_SYNCRONISATION
498540
#define MICROPY_HW_STM32WB_FLASH_SYNCRONISATION (1)
499541
#endif
@@ -699,10 +741,10 @@
699741
#define MICROPY_HW_USB_CDC_NUM (1)
700742
#endif
701743
#ifndef MICROPY_HW_USB_MSC
702-
#define MICROPY_HW_USB_MSC (MICROPY_HW_ENABLE_USB)
744+
#define MICROPY_HW_USB_MSC (MICROPY_HW_STM_USB_STACK)
703745
#endif
704746
#ifndef MICROPY_HW_USB_HID
705-
#define MICROPY_HW_USB_HID (MICROPY_HW_ENABLE_USB)
747+
#define MICROPY_HW_USB_HID (MICROPY_HW_STM_USB_STACK)
706748
#endif
707749

708750
// Pin definition header file

ports/stm32/mpconfigport.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,9 @@
179179
#define MICROPY_FATFS_USE_LABEL (1)
180180
#define MICROPY_FATFS_RPATH (2)
181181
#define MICROPY_FATFS_MULTI_PARTITION (1)
182+
#if MICROPY_HW_TINYUSB_STACK && CFG_TUD_MSC
183+
#define MICROPY_FATFS_MAX_SS (4096)
184+
#endif
182185

183186
#if MICROPY_PY_PYB
184187
extern const struct _mp_obj_module_t pyb_module;

0 commit comments

Comments
 (0)