Skip to content

Commit 12ef683

Browse files
committed
stm32/usb: Add support for using TinyUSB stack by default.
Signed-off-by: Andrew Leech <andrew@alelec.net>
1 parent 3ce7dbc commit 12ef683

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)
@@ -199,6 +202,10 @@ SHARED_SRC_C += $(addprefix shared/,\
199202
runtime/stdout_helpers.c \
200203
runtime/sys_stdio_mphal.c \
201204
timeutils/timeutils.c \
205+
tinyusb/mp_usbd.c \
206+
tinyusb/mp_usbd_cdc.c \
207+
tinyusb/mp_usbd_descriptor.c \
208+
tinyusb/mp_usbd_runtime.c \
202209
)
203210

204211
ifeq ($(MICROPY_FLOAT_IMPL),double)
@@ -223,16 +230,26 @@ DRIVERS_SRC_C += $(addprefix drivers/,\
223230
memory/spiflash.c \
224231
dht/dht.c \
225232
)
233+
234+
TINYUSB_SRC_C += $(addprefix lib/tinyusb/src/,\
235+
class/cdc/cdc_device.c \
236+
class/msc/msc_device.c \
237+
common/tusb_fifo.c \
238+
device/usbd.c \
239+
device/usbd_control.c \
240+
portable/st/stm32_fsdev/dcd_stm32_fsdev.c \
241+
portable/synopsys/dwc2/dcd_dwc2.c \
242+
tusb.c \
243+
)
244+
LDFLAGS += -Wl,--wrap=dcd_event_handler
226245

227246
SRC_C += \
228247
boardctrl.c \
229248
main.c \
230249
stm32_it.c \
231250
usbd_conf.c \
232-
usbd_desc.c \
233-
usbd_cdc_interface.c \
234-
usbd_hid_interface.c \
235-
usbd_msc_interface.c \
251+
usb.c \
252+
usbd.c \
236253
mphalport.c \
237254
mpnetworkport.c \
238255
mpthreadport.c \
@@ -262,7 +279,6 @@ SRC_C += \
262279
can.c \
263280
fdcan.c \
264281
pyb_can.c \
265-
usb.c \
266282
eth.c \
267283
eth_phy.c \
268284
gccollect.c \
@@ -408,15 +424,6 @@ HAL_SRC_C += $(addprefix $(STM32LIB_HAL_BASE)/Src/stm32$(MCU_SERIES)xx_,\
408424
)
409425
endif
410426

411-
USBDEV_SRC_C += $(addprefix $(USBDEV_DIR)/,\
412-
core/src/usbd_core.c \
413-
core/src/usbd_ctlreq.c \
414-
core/src/usbd_ioreq.c \
415-
class/src/usbd_cdc_msc_hid.c \
416-
class/src/usbd_msc_bot.c \
417-
class/src/usbd_msc_scsi.c \
418-
)
419-
420427
ifeq ($(MICROPY_SSL_MBEDTLS),1)
421428
LIB_SRC_C += mbedtls/mbedtls_port.c
422429
endif
@@ -455,7 +462,7 @@ OBJ += $(addprefix $(BUILD)/, $(LIBM_SRC_C:.c=.o))
455462
OBJ += $(addprefix $(BUILD)/, $(SHARED_SRC_C:.c=.o))
456463
OBJ += $(addprefix $(BUILD)/, $(DRIVERS_SRC_C:.c=.o))
457464
OBJ += $(addprefix $(BUILD)/, $(HAL_SRC_C:.c=.o))
458-
OBJ += $(addprefix $(BUILD)/, $(USBDEV_SRC_C:.c=.o))
465+
OBJ += $(addprefix $(BUILD)/, $(TINYUSB_SRC_C:.c=.o))
459466
OBJ += $(addprefix $(BUILD)/, $(SRC_C:.c=.o))
460467
OBJ += $(addprefix $(BUILD)/, $(SRC_CXX:.cpp=.o))
461468
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
@@ -529,8 +535,13 @@ void stm32_main(uint32_t reset_mode) {
529535
#endif
530536

531537
#if MICROPY_HW_ENABLE_USB
538+
#if MICROPY_HW_TINYUSB_STACK
539+
pyb_usbd_init();
540+
mp_usbd_init();
541+
#else
532542
pyb_usb_init0();
533543
#endif
544+
#endif
534545

535546
#if MICROPY_PY_MACHINE_I2S
536547
machine_i2s_init0();
@@ -554,7 +565,7 @@ void stm32_main(uint32_t reset_mode) {
554565
}
555566
#endif
556567

557-
#if MICROPY_HW_ENABLE_USB
568+
#if MICROPY_HW_ENABLE_USB && !MICROPY_HW_TINYUSB_STACK
558569
// if the SD card isn't used as the USB MSC medium then use the internal flash
559570
if (pyb_usb_storage_medium == PYB_USB_STORAGE_MEDIUM_NONE) {
560571
pyb_usb_storage_medium = PYB_USB_STORAGE_MEDIUM_FLASH;
@@ -588,7 +599,7 @@ void stm32_main(uint32_t reset_mode) {
588599
// or whose initialisation can be safely deferred until after running
589600
// boot.py.
590601

591-
#if MICROPY_HW_ENABLE_USB
602+
#if MICROPY_HW_ENABLE_USB && !MICROPY_HW_TINYUSB_STACK
592603
// init USB device to default setting if it was not already configured
593604
if (!(pyb_usb_flags & PYB_USB_FLAG_USB_MODE_CALLED)) {
594605
#if MICROPY_HW_USB_MSC
@@ -686,6 +697,9 @@ void stm32_main(uint32_t reset_mode) {
686697
#else
687698
MP_STATE_PORT(pyb_stdio_uart) = NULL;
688699
#endif
700+
#if MICROPY_HW_ENABLE_USB_RUNTIME_DEVICE && MICROPY_HW_TINYUSB_STACK
701+
mp_usbd_deinit();
702+
#endif
689703

690704
MICROPY_BOARD_END_SOFT_RESET(&state);
691705

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
@@ -161,6 +161,7 @@
161161
#define MICROPY_FATFS_USE_LABEL (1)
162162
#define MICROPY_FATFS_RPATH (2)
163163
#define MICROPY_FATFS_MULTI_PARTITION (1)
164+
#define MICROPY_FATFS_MAX_SS (4096)
164165

165166
#if MICROPY_PY_PYB
166167
extern const struct _mp_obj_module_t pyb_module;

0 commit comments

Comments
 (0)