Skip to content

Commit 6f475eb

Browse files
committed
stm32/usb: Add support for using TinyUSB stack.
Signed-off-by: Andrew Leech <[email protected]>
1 parent 15da59f commit 6f475eb

File tree

15 files changed

+415
-116
lines changed

15 files changed

+415
-116
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 \
@@ -276,7 +293,6 @@ SRC_C += \
276293
can.c \
277294
fdcan.c \
278295
pyb_can.c \
279-
usb.c \
280296
eth.c \
281297
eth_phy.c \
282298
gccollect.c \
@@ -498,6 +514,7 @@ OBJ += $(addprefix $(BUILD)/, $(SHARED_SRC_C:.c=.o))
498514
OBJ += $(addprefix $(BUILD)/, $(DRIVERS_SRC_C:.c=.o))
499515
OBJ += $(addprefix $(BUILD)/, $(HAL_SRC_C:.c=.o))
500516
OBJ += $(addprefix $(BUILD)/, $(USBDEV_SRC_C:.c=.o))
517+
OBJ += $(addprefix $(BUILD)/, $(TINYUSB_SRC_C:.c=.o))
501518
OBJ += $(addprefix $(BUILD)/, $(SRC_C:.c=.o))
502519
OBJ += $(addprefix $(BUILD)/, $(SRC_CXX:.cpp=.o))
503520
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
@@ -765,6 +774,9 @@ void stm32_main(uint32_t reset_mode) {
765774
#else
766775
MP_STATE_PORT(pyb_stdio_uart) = NULL;
767776
#endif
777+
#if MICROPY_HW_ENABLE_USB_RUNTIME_DEVICE && MICROPY_HW_TINYUSB_STACK
778+
mp_usbd_deinit();
779+
#endif
768780

769781
MICROPY_BOARD_END_SOFT_RESET(&state);
770782

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: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,20 @@
136136
#define MICROPY_HW_ENABLE_USB (0)
137137
#endif
138138

139+
#if MICROPY_HW_ENABLE_USB
140+
#define MICROPY_HW_ENABLE_USBDEV (1)
141+
#define MICROPY_HW_USB_CDC (1)
142+
#define MICROPY_HW_USB_FS (1)
143+
144+
// Select whether TinyUSB or legacy STM stack is used to provide USB.
145+
#ifndef MICROPY_HW_TINYUSB_STACK
146+
#define MICROPY_HW_TINYUSB_STACK (0)
147+
#endif
148+
149+
// Central definition for STM USB stack (when not using TinyUSB)
150+
#define MICROPY_HW_STM_USB_STACK (MICROPY_HW_ENABLE_USB && !MICROPY_HW_TINYUSB_STACK)
151+
#endif // MICROPY_HW_ENABLE_USB
152+
139153
// Whether to enable the PA0-PA3 servo driver, exposed as pyb.Servo
140154
#ifndef MICROPY_HW_ENABLE_SERVO
141155
#define MICROPY_HW_ENABLE_SERVO (0)
@@ -256,6 +270,8 @@
256270
// Windows needs a different PID to distinguish different device configurations.
257271
#ifndef MICROPY_HW_USB_VID
258272
#define MICROPY_HW_USB_VID (0xf055)
273+
#define MICROPY_HW_USB_PID (0x9802)
274+
259275
#define MICROPY_HW_USB_PID_CDC_MSC (0x9800)
260276
#define MICROPY_HW_USB_PID_CDC_HID (0x9801)
261277
#define MICROPY_HW_USB_PID_CDC (0x9802)
@@ -369,6 +385,8 @@
369385
#endif
370386
#define MICROPY_HW_MAX_LPUART (0)
371387

388+
#define CFG_TUSB_MCU OPT_MCU_STM32F4
389+
372390
// Configuration for STM32F7 series
373391
#elif defined(STM32F7)
374392

@@ -384,6 +402,8 @@
384402
#define MICROPY_HW_MAX_UART (8)
385403
#define MICROPY_HW_MAX_LPUART (0)
386404

405+
#define CFG_TUSB_MCU OPT_MCU_STM32F7
406+
387407
// Configuration for STM32G0 series
388408
#elif defined(STM32G0)
389409

@@ -394,6 +414,8 @@
394414
#define MICROPY_HW_MAX_UART (6)
395415
#define MICROPY_HW_MAX_LPUART (2)
396416

417+
#define CFG_TUSB_MCU OPT_MCU_STM32G0
418+
397419
// Configuration for STM32G4 series
398420
#elif defined(STM32G4)
399421

@@ -404,6 +426,8 @@
404426
#define MICROPY_HW_MAX_UART (5) // UART1-5 + LPUART1
405427
#define MICROPY_HW_MAX_LPUART (1)
406428

429+
#define CFG_TUSB_MCU OPT_MCU_STM32G4
430+
407431
// Configuration for STM32H5 series
408432
#elif defined(STM32H5)
409433

@@ -414,6 +438,8 @@
414438
#define MICROPY_HW_MAX_UART (12)
415439
#define MICROPY_HW_MAX_LPUART (1)
416440

441+
#define CFG_TUSB_MCU OPT_MCU_STM32H5
442+
417443
// Configuration for STM32H7A3/B3 series
418444
#elif defined(STM32H7A3xx) || defined(STM32H7A3xxQ) || \
419445
defined(STM32H7B3xx) || defined(STM32H7B3xxQ)
@@ -425,6 +451,8 @@
425451
#define MICROPY_HW_MAX_UART (10)
426452
#define MICROPY_HW_MAX_LPUART (1)
427453

454+
#define CFG_TUSB_MCU OPT_MCU_STM32H7
455+
428456
// Configuration for STM32H7 series
429457
#elif defined(STM32H7)
430458

@@ -435,6 +463,8 @@
435463
#define MICROPY_HW_MAX_UART (8)
436464
#define MICROPY_HW_MAX_LPUART (1)
437465

466+
#define CFG_TUSB_MCU OPT_MCU_STM32H7
467+
438468
#if defined(MICROPY_HW_ANALOG_SWITCH_PA0) \
439469
|| defined(MICROPY_HW_ANALOG_SWITCH_PA1) \
440470
|| defined(MICROPY_HW_ANALOG_SWITCH_PC2) \
@@ -454,6 +484,8 @@
454484
#define MICROPY_HW_MAX_UART (5)
455485
#define MICROPY_HW_MAX_LPUART (1)
456486

487+
#define CFG_TUSB_MCU OPT_MCU_STM32L0
488+
457489
// Configuration for STM32L1 series
458490
#elif defined(STM32L1)
459491
#define MP_HAL_UNIQUE_ID_ADDRESS (UID_BASE)
@@ -464,6 +496,8 @@
464496
#define MICROPY_HW_MAX_UART (5)
465497
#define MICROPY_HW_MAX_LPUART (0)
466498

499+
#define CFG_TUSB_MCU OPT_MCU_STM32L1
500+
467501
// Configuration for STM32L4 series
468502
#elif defined(STM32L4)
469503

@@ -474,6 +508,8 @@
474508
#define MICROPY_HW_MAX_UART (5)
475509
#define MICROPY_HW_MAX_LPUART (1)
476510

511+
#define CFG_TUSB_MCU OPT_MCU_STM32L4
512+
477513
// Configuration for STM32N6 series
478514
#elif defined(STM32N6)
479515

@@ -494,6 +530,8 @@
494530
#define MICROPY_HW_MAX_UART (1)
495531
#define MICROPY_HW_MAX_LPUART (1)
496532

533+
#define CFG_TUSB_MCU OPT_MCU_STM32WB
534+
497535
#ifndef MICROPY_HW_STM32WB_FLASH_SYNCRONISATION
498536
#define MICROPY_HW_STM32WB_FLASH_SYNCRONISATION (1)
499537
#endif
@@ -693,10 +731,10 @@
693731
#define MICROPY_HW_USB_CDC_NUM (1)
694732
#endif
695733
#ifndef MICROPY_HW_USB_MSC
696-
#define MICROPY_HW_USB_MSC (MICROPY_HW_ENABLE_USB)
734+
#define MICROPY_HW_USB_MSC (MICROPY_HW_STM_USB_STACK)
697735
#endif
698736
#ifndef MICROPY_HW_USB_HID
699-
#define MICROPY_HW_USB_HID (MICROPY_HW_ENABLE_USB)
737+
#define MICROPY_HW_USB_HID (MICROPY_HW_STM_USB_STACK)
700738
#endif
701739

702740
// Pin definition header file

ports/stm32/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@
175175
#define MICROPY_FATFS_USE_LABEL (1)
176176
#define MICROPY_FATFS_RPATH (2)
177177
#define MICROPY_FATFS_MULTI_PARTITION (1)
178+
#define MICROPY_FATFS_MAX_SS (4096)
178179

179180
#if MICROPY_PY_PYB
180181
extern const struct _mp_obj_module_t pyb_module;

0 commit comments

Comments
 (0)