Skip to content

Commit 7b072e8

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

File tree

15 files changed

+381
-194
lines changed

15 files changed

+381
-194
lines changed

ports/stm32/Makefile

Lines changed: 23 additions & 16 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,16 +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 \
239258
usbd_conf.c \
240-
usbd_desc.c \
241-
usbd_cdc_interface.c \
242-
usbd_hid_interface.c \
243-
usbd_msc_interface.c \
259+
usb.c \
260+
usbd.c \
244261
mphalport.c \
245262
mpnetworkport.c \
246263
mpthreadport.c \
@@ -270,7 +287,6 @@ SRC_C += \
270287
can.c \
271288
fdcan.c \
272289
pyb_can.c \
273-
usb.c \
274290
eth.c \
275291
eth_phy.c \
276292
gccollect.c \
@@ -416,15 +432,6 @@ HAL_SRC_C += $(addprefix $(STM32LIB_HAL_BASE)/Src/stm32$(MCU_SERIES)xx_,\
416432
)
417433
endif
418434

419-
USBDEV_SRC_C += $(addprefix $(USBDEV_DIR)/,\
420-
core/src/usbd_core.c \
421-
core/src/usbd_ctlreq.c \
422-
core/src/usbd_ioreq.c \
423-
class/src/usbd_cdc_msc_hid.c \
424-
class/src/usbd_msc_bot.c \
425-
class/src/usbd_msc_scsi.c \
426-
)
427-
428435
ifeq ($(MICROPY_SSL_MBEDTLS),1)
429436
LIB_SRC_C += mbedtls/mbedtls_port.c
430437
endif
@@ -463,7 +470,7 @@ OBJ += $(addprefix $(BUILD)/, $(LIBM_SRC_C:.c=.o))
463470
OBJ += $(addprefix $(BUILD)/, $(SHARED_SRC_C:.c=.o))
464471
OBJ += $(addprefix $(BUILD)/, $(DRIVERS_SRC_C:.c=.o))
465472
OBJ += $(addprefix $(BUILD)/, $(HAL_SRC_C:.c=.o))
466-
OBJ += $(addprefix $(BUILD)/, $(USBDEV_SRC_C:.c=.o))
473+
OBJ += $(addprefix $(BUILD)/, $(TINYUSB_SRC_C:.c=.o))
467474
OBJ += $(addprefix $(BUILD)/, $(SRC_C:.c=.o))
468475
OBJ += $(addprefix $(BUILD)/, $(SRC_CXX:.cpp=.o))
469476
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 "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
@@ -540,8 +546,13 @@ void stm32_main(uint32_t reset_mode) {
540546
#endif
541547

542548
#if MICROPY_HW_ENABLE_USB
549+
#if MICROPY_HW_TINYUSB_STACK
550+
pyb_usbd_init();
551+
mp_usbd_init();
552+
#else
543553
pyb_usb_init0();
544554
#endif
555+
#endif
545556

546557
#if MICROPY_PY_MACHINE_I2S
547558
machine_i2s_init0();
@@ -565,7 +576,7 @@ void stm32_main(uint32_t reset_mode) {
565576
}
566577
#endif
567578

568-
#if MICROPY_HW_ENABLE_USB
579+
#if MICROPY_HW_ENABLE_USB && !MICROPY_HW_TINYUSB_STACK
569580
// if the SD card isn't used as the USB MSC medium then use the internal flash
570581
if (pyb_usb_storage_medium == PYB_USB_STORAGE_MEDIUM_NONE) {
571582
pyb_usb_storage_medium = PYB_USB_STORAGE_MEDIUM_FLASH;
@@ -599,7 +610,7 @@ void stm32_main(uint32_t reset_mode) {
599610
// or whose initialisation can be safely deferred until after running
600611
// boot.py.
601612

602-
#if MICROPY_HW_ENABLE_USB
613+
#if MICROPY_HW_ENABLE_USB && !MICROPY_HW_TINYUSB_STACK
603614
// init USB device to default setting if it was not already configured
604615
if (!(pyb_usb_flags & PYB_USB_FLAG_USB_MODE_CALLED)) {
605616
#if MICROPY_HW_USB_MSC
@@ -697,6 +708,9 @@ void stm32_main(uint32_t reset_mode) {
697708
#else
698709
MP_STATE_PORT(pyb_stdio_uart) = NULL;
699710
#endif
711+
#if MICROPY_HW_ENABLE_USB_RUNTIME_DEVICE && MICROPY_HW_TINYUSB_STACK
712+
mp_usbd_deinit();
713+
#endif
700714

701715
MICROPY_BOARD_END_SOFT_RESET(&state);
702716

ports/stm32/modmachine.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,9 +289,13 @@ NORETURN static void mp_machine_reset(void) {
289289

290290
// Activate the bootloader without BOOT* pins.
291291
NORETURN void mp_machine_bootloader(size_t n_args, const mp_obj_t *args) {
292-
#if MICROPY_HW_ENABLE_USB
292+
#if MICROPY_HW_ENABLE_USB && !MICROPY_HW_TINYUSB_STACK
293293
pyb_usb_dev_deinit();
294294
#endif
295+
#if MICROPY_HW_ENABLE_USB_RUNTIME_DEVICE && MICROPY_HW_TINYUSB_STACK
296+
mp_usbd_deinit();
297+
#endif
298+
295299
#if MICROPY_HW_ENABLE_STORAGE
296300
storage_flush();
297301
#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: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@
9696
#ifndef MICROPY_HW_ENABLE_USB
9797
#define MICROPY_HW_ENABLE_USB (0)
9898
#endif
99+
#define MICROPY_HW_ENABLE_USBDEV 1
100+
#define MICROPY_HW_USB_CDC 1
101+
#define MICROPY_HW_USB_FS (1)
99102

100103
// Whether to enable the PA0-PA3 servo driver, exposed as pyb.Servo
101104
#ifndef MICROPY_HW_ENABLE_SERVO
@@ -211,6 +214,8 @@
211214
// Windows needs a different PID to distinguish different device configurations.
212215
#ifndef MICROPY_HW_USB_VID
213216
#define MICROPY_HW_USB_VID (0xf055)
217+
#define MICROPY_HW_USB_PID (0x9802)
218+
214219
#define MICROPY_HW_USB_PID_CDC_MSC (0x9800)
215220
#define MICROPY_HW_USB_PID_CDC_HID (0x9801)
216221
#define MICROPY_HW_USB_PID_CDC (0x9802)
@@ -324,6 +329,8 @@
324329
#endif
325330
#define MICROPY_HW_MAX_LPUART (0)
326331

332+
#define CFG_TUSB_MCU OPT_MCU_STM32F4
333+
327334
// Configuration for STM32F7 series
328335
#elif defined(STM32F7)
329336

@@ -339,6 +346,8 @@
339346
#define MICROPY_HW_MAX_UART (8)
340347
#define MICROPY_HW_MAX_LPUART (0)
341348

349+
#define CFG_TUSB_MCU OPT_MCU_STM32F7
350+
342351
// Configuration for STM32G0 series
343352
#elif defined(STM32G0)
344353

@@ -349,6 +358,8 @@
349358
#define MICROPY_HW_MAX_UART (6)
350359
#define MICROPY_HW_MAX_LPUART (2)
351360

361+
#define CFG_TUSB_MCU OPT_MCU_STM32G0
362+
352363
// Configuration for STM32G4 series
353364
#elif defined(STM32G4)
354365

@@ -359,6 +370,8 @@
359370
#define MICROPY_HW_MAX_UART (5) // UART1-5 + LPUART1
360371
#define MICROPY_HW_MAX_LPUART (1)
361372

373+
#define CFG_TUSB_MCU OPT_MCU_STM32G4
374+
362375
// Configuration for STM32H5 series
363376
#elif defined(STM32H5)
364377

@@ -369,6 +382,8 @@
369382
#define MICROPY_HW_MAX_UART (12)
370383
#define MICROPY_HW_MAX_LPUART (1)
371384

385+
#define CFG_TUSB_MCU OPT_MCU_STM32H5
386+
372387
// Configuration for STM32H7A3/B3 series
373388
#elif defined(STM32H7A3xx) || defined(STM32H7A3xxQ) || \
374389
defined(STM32H7B3xx) || defined(STM32H7B3xxQ)
@@ -380,6 +395,8 @@
380395
#define MICROPY_HW_MAX_UART (10)
381396
#define MICROPY_HW_MAX_LPUART (1)
382397

398+
#define CFG_TUSB_MCU OPT_MCU_STM32H7
399+
383400
// Configuration for STM32H7 series
384401
#elif defined(STM32H7)
385402

@@ -390,6 +407,8 @@
390407
#define MICROPY_HW_MAX_UART (8)
391408
#define MICROPY_HW_MAX_LPUART (1)
392409

410+
#define CFG_TUSB_MCU OPT_MCU_STM32H7
411+
393412
#if defined(MICROPY_HW_ANALOG_SWITCH_PA0) \
394413
|| defined(MICROPY_HW_ANALOG_SWITCH_PA1) \
395414
|| defined(MICROPY_HW_ANALOG_SWITCH_PC2) \
@@ -409,6 +428,8 @@
409428
#define MICROPY_HW_MAX_UART (5)
410429
#define MICROPY_HW_MAX_LPUART (1)
411430

431+
#define CFG_TUSB_MCU OPT_MCU_STM32L0
432+
412433
// Configuration for STM32L1 series
413434
#elif defined(STM32L1)
414435
#define MP_HAL_UNIQUE_ID_ADDRESS (UID_BASE)
@@ -419,6 +440,8 @@
419440
#define MICROPY_HW_MAX_UART (5)
420441
#define MICROPY_HW_MAX_LPUART (0)
421442

443+
#define CFG_TUSB_MCU OPT_MCU_STM32L1
444+
422445
// Configuration for STM32L4 series
423446
#elif defined(STM32L4)
424447

@@ -429,6 +452,8 @@
429452
#define MICROPY_HW_MAX_UART (5)
430453
#define MICROPY_HW_MAX_LPUART (1)
431454

455+
#define CFG_TUSB_MCU OPT_MCU_STM32L4
456+
432457
// Configuration for STM32WB series
433458
#elif defined(STM32WB)
434459

@@ -439,6 +464,8 @@
439464
#define MICROPY_HW_MAX_UART (1)
440465
#define MICROPY_HW_MAX_LPUART (1)
441466

467+
#define CFG_TUSB_MCU OPT_MCU_STM32WB
468+
442469
#ifndef MICROPY_HW_STM32WB_FLASH_SYNCRONISATION
443470
#define MICROPY_HW_STM32WB_FLASH_SYNCRONISATION (1)
444471
#endif
@@ -630,10 +657,10 @@
630657
#define MICROPY_HW_USB_CDC_NUM (1)
631658
#endif
632659
#ifndef MICROPY_HW_USB_MSC
633-
#define MICROPY_HW_USB_MSC (MICROPY_HW_ENABLE_USB)
660+
#define MICROPY_HW_USB_MSC (0)
634661
#endif
635662
#ifndef MICROPY_HW_USB_HID
636-
#define MICROPY_HW_USB_HID (MICROPY_HW_ENABLE_USB)
663+
#define MICROPY_HW_USB_HID (0)
637664
#endif
638665

639666
// Pin definition header file

ports/stm32/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@
162162
#define MICROPY_FATFS_USE_LABEL (1)
163163
#define MICROPY_FATFS_RPATH (2)
164164
#define MICROPY_FATFS_MULTI_PARTITION (1)
165+
#define MICROPY_FATFS_MAX_SS (4096)
165166

166167
#if MICROPY_PY_PYB
167168
extern const struct _mp_obj_module_t pyb_module;

0 commit comments

Comments
 (0)