Skip to content

Commit d5e5c17

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

File tree

14 files changed

+398
-179
lines changed

14 files changed

+398
-179
lines changed

ports/stm32/Makefile

Lines changed: 23 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
LD_DIR=boards
6464
USBDEV_DIR=usbdev
@@ -112,6 +112,9 @@ INC += -I$(STM32LIB_CMSIS_ABS)/Include
112112
INC += -I$(STM32LIB_HAL_ABS)/Inc
113113
INC += -I$(USBDEV_DIR)/core/inc -I$(USBDEV_DIR)/class/inc
114114
#INC += -I$(USBHOST_DIR)
115+
INC += -I$(TOP)/lib/tinyusb/src
116+
INC += -I$(TOP)/shared/tinyusb/
117+
115118
INC += -Ilwip_inc
116119

117120
CFLAGS += $(INC) -Wall -Wpointer-arith -Werror -Wdouble-promotion -Wfloat-conversion -std=gnu99 -nostdlib $(CFLAGS_EXTRA)
@@ -207,6 +210,10 @@ SHARED_SRC_C += $(addprefix shared/,\
207210
runtime/stdout_helpers.c \
208211
runtime/sys_stdio_mphal.c \
209212
timeutils/timeutils.c \
213+
tinyusb/mp_usbd.c \
214+
tinyusb/mp_usbd_cdc.c \
215+
tinyusb/mp_usbd_descriptor.c \
216+
tinyusb/mp_usbd_runtime.c \
210217
)
211218

212219
ifeq ($(MICROPY_FLOAT_IMPL),double)
@@ -231,12 +238,26 @@ DRIVERS_SRC_C += $(addprefix drivers/,\
231238
memory/spiflash.c \
232239
dht/dht.c \
233240
)
241+
242+
TINYUSB_SRC_C += $(addprefix lib/tinyusb/src/,\
243+
class/cdc/cdc_device.c \
244+
class/msc/msc_device.c \
245+
common/tusb_fifo.c \
246+
device/usbd.c \
247+
device/usbd_control.c \
248+
portable/st/stm32_fsdev/dcd_stm32_fsdev.c \
249+
portable/synopsys/dwc2/dcd_dwc2.c \
250+
tusb.c \
251+
)
252+
LDFLAGS += -Wl,--wrap=dcd_event_handler
234253

235254
SRC_C += \
236255
boardctrl.c \
237256
main.c \
238257
stm32_it.c \
258+
usbd.c \
239259
usbd_conf.c \
260+
usb.c \
240261
usbd_desc.c \
241262
usbd_cdc_interface.c \
242263
usbd_hid_interface.c \
@@ -270,7 +291,6 @@ SRC_C += \
270291
can.c \
271292
fdcan.c \
272293
pyb_can.c \
273-
usb.c \
274294
eth.c \
275295
eth_phy.c \
276296
gccollect.c \
@@ -466,6 +486,7 @@ OBJ += $(addprefix $(BUILD)/, $(SHARED_SRC_C:.c=.o))
466486
OBJ += $(addprefix $(BUILD)/, $(DRIVERS_SRC_C:.c=.o))
467487
OBJ += $(addprefix $(BUILD)/, $(HAL_SRC_C:.c=.o))
468488
OBJ += $(addprefix $(BUILD)/, $(USBDEV_SRC_C:.c=.o))
489+
OBJ += $(addprefix $(BUILD)/, $(TINYUSB_SRC_C:.c=.o))
469490
OBJ += $(addprefix $(BUILD)/, $(SRC_C:.c=.o))
470491
OBJ += $(addprefix $(BUILD)/, $(SRC_CXX:.cpp=.o))
471492
OBJ += $(GEN_PINS_SRC:.c=.o)

ports/stm32/main.c

Lines changed: 17 additions & 3 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
@@ -547,8 +553,13 @@ void stm32_main(uint32_t reset_mode) {
547553
#endif
548554

549555
#if MICROPY_HW_ENABLE_USB
556+
#if MICROPY_HW_TINYUSB_STACK
557+
pyb_usbd_init();
558+
mp_usbd_init();
559+
#else
550560
pyb_usb_init0();
551561
#endif
562+
#endif
552563

553564
#if MICROPY_PY_MACHINE_I2S
554565
machine_i2s_init0();
@@ -572,7 +583,7 @@ void stm32_main(uint32_t reset_mode) {
572583
}
573584
#endif
574585

575-
#if MICROPY_HW_ENABLE_USB
586+
#if MICROPY_HW_ENABLE_USB && !MICROPY_HW_TINYUSB_STACK
576587
// if the SD card isn't used as the USB MSC medium then use the internal flash
577588
if (pyb_usb_storage_medium == PYB_USB_STORAGE_MEDIUM_NONE) {
578589
pyb_usb_storage_medium = PYB_USB_STORAGE_MEDIUM_FLASH;
@@ -606,7 +617,7 @@ void stm32_main(uint32_t reset_mode) {
606617
// or whose initialisation can be safely deferred until after running
607618
// boot.py.
608619

609-
#if MICROPY_HW_ENABLE_USB
620+
#if MICROPY_HW_ENABLE_USB && !MICROPY_HW_TINYUSB_STACK
610621
// init USB device to default setting if it was not already configured
611622
if (!(pyb_usb_flags & PYB_USB_FLAG_USB_MODE_CALLED)) {
612623
#if MICROPY_HW_USB_MSC
@@ -708,6 +719,9 @@ void stm32_main(uint32_t reset_mode) {
708719
#else
709720
MP_STATE_PORT(pyb_stdio_uart) = NULL;
710721
#endif
722+
#if MICROPY_HW_ENABLE_USB_RUNTIME_DEVICE && MICROPY_HW_TINYUSB_STACK
723+
mp_usbd_deinit();
724+
#endif
711725

712726
MICROPY_BOARD_END_SOFT_RESET(&state);
713727

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 @@ NORETURN static void mp_machine_reset(void) {
297298

298299
// Activate the bootloader without BOOT* pins.
299300
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_ENABLE_USB && !MICROPY_HW_TINYUSB_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_ENABLE_USB && !MICROPY_HW_TINYUSB_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_ENABLE_USB && !MICROPY_HW_TINYUSB_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_ENABLE_USB && !MICROPY_HW_TINYUSB_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: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,22 @@
127127
#define MICROPY_HW_ENABLE_USB (0)
128128
#endif
129129

130+
#if MICROPY_HW_ENABLE_USB
131+
#define MICROPY_HW_ENABLE_USBDEV (1)
132+
#define MICROPY_HW_USB_CDC (1)
133+
#define MICROPY_HW_USB_FS (1)
134+
135+
// Select whether TinyUSB or legacy STM stack is used to provide USB.
136+
#ifndef MICROPY_HW_TINYUSB_STACK
137+
#define MICROPY_HW_TINYUSB_STACK (MICROPY_PREVIEW_VERSION_2)
138+
#endif
139+
140+
// Support machine.USBDevice (only available with TinyUSB)
141+
#ifndef MICROPY_HW_ENABLE_USB_RUNTIME_DEVICE
142+
#define MICROPY_HW_ENABLE_USB_RUNTIME_DEVICE (MICROPY_HW_TINYUSB_STACK)
143+
#endif
144+
#endif // MICROPY_HW_ENABLE_USB
145+
130146
// Whether to enable the PA0-PA3 servo driver, exposed as pyb.Servo
131147
#ifndef MICROPY_HW_ENABLE_SERVO
132148
#define MICROPY_HW_ENABLE_SERVO (0)
@@ -247,6 +263,8 @@
247263
// Windows needs a different PID to distinguish different device configurations.
248264
#ifndef MICROPY_HW_USB_VID
249265
#define MICROPY_HW_USB_VID (0xf055)
266+
#define MICROPY_HW_USB_PID (0x9802)
267+
250268
#define MICROPY_HW_USB_PID_CDC_MSC (0x9800)
251269
#define MICROPY_HW_USB_PID_CDC_HID (0x9801)
252270
#define MICROPY_HW_USB_PID_CDC (0x9802)
@@ -360,6 +378,8 @@
360378
#endif
361379
#define MICROPY_HW_MAX_LPUART (0)
362380

381+
#define CFG_TUSB_MCU OPT_MCU_STM32F4
382+
363383
// Configuration for STM32F7 series
364384
#elif defined(STM32F7)
365385

@@ -375,6 +395,8 @@
375395
#define MICROPY_HW_MAX_UART (8)
376396
#define MICROPY_HW_MAX_LPUART (0)
377397

398+
#define CFG_TUSB_MCU OPT_MCU_STM32F7
399+
378400
// Configuration for STM32G0 series
379401
#elif defined(STM32G0)
380402

@@ -385,6 +407,8 @@
385407
#define MICROPY_HW_MAX_UART (6)
386408
#define MICROPY_HW_MAX_LPUART (2)
387409

410+
#define CFG_TUSB_MCU OPT_MCU_STM32G0
411+
388412
// Configuration for STM32G4 series
389413
#elif defined(STM32G4)
390414

@@ -395,6 +419,8 @@
395419
#define MICROPY_HW_MAX_UART (5) // UART1-5 + LPUART1
396420
#define MICROPY_HW_MAX_LPUART (1)
397421

422+
#define CFG_TUSB_MCU OPT_MCU_STM32G4
423+
398424
// Configuration for STM32H5 series
399425
#elif defined(STM32H5)
400426

@@ -405,6 +431,8 @@
405431
#define MICROPY_HW_MAX_UART (12)
406432
#define MICROPY_HW_MAX_LPUART (1)
407433

434+
#define CFG_TUSB_MCU OPT_MCU_STM32H5
435+
408436
// Configuration for STM32H7A3/B3 series
409437
#elif defined(STM32H7A3xx) || defined(STM32H7A3xxQ) || \
410438
defined(STM32H7B3xx) || defined(STM32H7B3xxQ)
@@ -416,6 +444,8 @@
416444
#define MICROPY_HW_MAX_UART (10)
417445
#define MICROPY_HW_MAX_LPUART (1)
418446

447+
#define CFG_TUSB_MCU OPT_MCU_STM32H7
448+
419449
// Configuration for STM32H7 series
420450
#elif defined(STM32H7)
421451

@@ -426,6 +456,8 @@
426456
#define MICROPY_HW_MAX_UART (8)
427457
#define MICROPY_HW_MAX_LPUART (1)
428458

459+
#define CFG_TUSB_MCU OPT_MCU_STM32H7
460+
429461
#if defined(MICROPY_HW_ANALOG_SWITCH_PA0) \
430462
|| defined(MICROPY_HW_ANALOG_SWITCH_PA1) \
431463
|| defined(MICROPY_HW_ANALOG_SWITCH_PC2) \
@@ -445,6 +477,8 @@
445477
#define MICROPY_HW_MAX_UART (5)
446478
#define MICROPY_HW_MAX_LPUART (1)
447479

480+
#define CFG_TUSB_MCU OPT_MCU_STM32L0
481+
448482
// Configuration for STM32L1 series
449483
#elif defined(STM32L1)
450484
#define MP_HAL_UNIQUE_ID_ADDRESS (UID_BASE)
@@ -455,6 +489,8 @@
455489
#define MICROPY_HW_MAX_UART (5)
456490
#define MICROPY_HW_MAX_LPUART (0)
457491

492+
#define CFG_TUSB_MCU OPT_MCU_STM32L1
493+
458494
// Configuration for STM32L4 series
459495
#elif defined(STM32L4)
460496

@@ -465,6 +501,8 @@
465501
#define MICROPY_HW_MAX_UART (5)
466502
#define MICROPY_HW_MAX_LPUART (1)
467503

504+
#define CFG_TUSB_MCU OPT_MCU_STM32L4
505+
468506
// Configuration for STM32WB series
469507
#elif defined(STM32WB)
470508

@@ -475,6 +513,8 @@
475513
#define MICROPY_HW_MAX_UART (1)
476514
#define MICROPY_HW_MAX_LPUART (1)
477515

516+
#define CFG_TUSB_MCU OPT_MCU_STM32WB
517+
478518
#ifndef MICROPY_HW_STM32WB_FLASH_SYNCRONISATION
479519
#define MICROPY_HW_STM32WB_FLASH_SYNCRONISATION (1)
480520
#endif
@@ -666,10 +706,10 @@
666706
#define MICROPY_HW_USB_CDC_NUM (1)
667707
#endif
668708
#ifndef MICROPY_HW_USB_MSC
669-
#define MICROPY_HW_USB_MSC (MICROPY_HW_ENABLE_USB)
709+
#define MICROPY_HW_USB_MSC (MICROPY_HW_ENABLE_USB && !MICROPY_HW_TINYUSB_STACK)
670710
#endif
671711
#ifndef MICROPY_HW_USB_HID
672-
#define MICROPY_HW_USB_HID (MICROPY_HW_ENABLE_USB)
712+
#define MICROPY_HW_USB_HID (MICROPY_HW_ENABLE_USB && !MICROPY_HW_TINYUSB_STACK)
673713
#endif
674714

675715
// Pin definition header file

ports/stm32/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@
173173
#define MICROPY_FATFS_USE_LABEL (1)
174174
#define MICROPY_FATFS_RPATH (2)
175175
#define MICROPY_FATFS_MULTI_PARTITION (1)
176+
#define MICROPY_FATFS_MAX_SS (4096)
176177

177178
#if MICROPY_PY_PYB
178179
extern const struct _mp_obj_module_t pyb_module;

0 commit comments

Comments
 (0)