Skip to content

Commit 7f3f7e0

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

File tree

11 files changed

+132
-67
lines changed

11 files changed

+132
-67
lines changed

ports/stm32/Makefile

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,6 @@ TINYUSB_SRC_C += $(addprefix lib/tinyusb/src/,\
249249
portable/synopsys/dwc2/dcd_dwc2.c \
250250
tusb.c \
251251
)
252-
253252
LDFLAGS += -Wl,--wrap=dcd_event_handler
254253

255254
SRC_C += \
@@ -258,10 +257,7 @@ SRC_C += \
258257
stm32_it.c \
259258
usbd.c \
260259
usbd_conf.c \
261-
usbd_desc.c \
262-
usbd_cdc_interface.c \
263-
usbd_hid_interface.c \
264-
usbd_msc_interface.c \
260+
usb.c \
265261
mphalport.c \
266262
mpnetworkport.c \
267263
mpthreadport.c \
@@ -291,7 +287,6 @@ SRC_C += \
291287
can.c \
292288
fdcan.c \
293289
pyb_can.c \
294-
usb.c \
295290
eth.c \
296291
eth_phy.c \
297292
gccollect.c \
@@ -437,15 +432,6 @@ HAL_SRC_C += $(addprefix $(STM32LIB_HAL_BASE)/Src/stm32$(MCU_SERIES)xx_,\
437432
)
438433
endif
439434

440-
USBDEV_SRC_C += $(addprefix $(USBDEV_DIR)/,\
441-
core/src/usbd_core.c \
442-
core/src/usbd_ctlreq.c \
443-
core/src/usbd_ioreq.c \
444-
class/src/usbd_cdc_msc_hid.c \
445-
class/src/usbd_msc_bot.c \
446-
class/src/usbd_msc_scsi.c \
447-
)
448-
449435
ifeq ($(MICROPY_SSL_MBEDTLS),1)
450436
LIB_SRC_C += mbedtls/mbedtls_port.c
451437
endif

ports/stm32/main.c

Lines changed: 14 additions & 9 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,9 +88,12 @@
8988
#include "can.h"
9089
#include "subghz.h"
9190

91+
#if MICROPY_HW_TINYUSB_STACK
9292
#include "usbd_conf.h"
9393
#include "shared/tinyusb/mp_usbd.h"
94-
94+
#else
95+
#include "usb.h"
96+
#endif
9597

9698
#if MICROPY_PY_THREAD
9799
static pyb_thread_t pyb_thread_main;
@@ -544,8 +546,12 @@ void stm32_main(uint32_t reset_mode) {
544546
#endif
545547

546548
#if MICROPY_HW_ENABLE_USB
549+
#if MICROPY_HW_TINYUSB_STACK
547550
pyb_usbd_init();
548551
mp_usbd_init();
552+
#else
553+
pyb_usb_init0();
554+
#endif
549555
#endif
550556

551557
#if MICROPY_PY_MACHINE_I2S
@@ -570,11 +576,11 @@ void stm32_main(uint32_t reset_mode) {
570576
}
571577
#endif
572578

573-
#if MICROPY_HW_ENABLE_USB
579+
#if MICROPY_HW_ENABLE_USB && !MICROPY_HW_TINYUSB_STACK
574580
// if the SD card isn't used as the USB MSC medium then use the internal flash
575-
// if (pyb_usb_storage_medium == PYB_USB_STORAGE_MEDIUM_NONE) {
576-
// pyb_usb_storage_medium = PYB_USB_STORAGE_MEDIUM_FLASH;
577-
// }
581+
if (pyb_usb_storage_medium == PYB_USB_STORAGE_MEDIUM_NONE) {
582+
pyb_usb_storage_medium = PYB_USB_STORAGE_MEDIUM_FLASH;
583+
}
578584
#endif
579585

580586
// set sys.path based on mounted filesystems (/sd is first so it can override /flash)
@@ -604,8 +610,7 @@ void stm32_main(uint32_t reset_mode) {
604610
// or whose initialisation can be safely deferred until after running
605611
// boot.py.
606612

607-
608-
#if 0
613+
#if MICROPY_HW_ENABLE_USB && !MICROPY_HW_TINYUSB_STACK
609614
// init USB device to default setting if it was not already configured
610615
if (!(pyb_usb_flags & PYB_USB_FLAG_USB_MODE_CALLED)) {
611616
#if MICROPY_HW_USB_MSC
@@ -707,7 +712,7 @@ void stm32_main(uint32_t reset_mode) {
707712
#else
708713
MP_STATE_PORT(pyb_stdio_uart) = NULL;
709714
#endif
710-
#if MICROPY_HW_ENABLE_USB_RUNTIME_DEVICE
715+
#if MICROPY_HW_ENABLE_USB_RUNTIME_DEVICE && MICROPY_HW_TINYUSB_STACK
711716
mp_usbd_deinit();
712717
#endif
713718

ports/stm32/modmachine.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -289,10 +289,10 @@ 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
293-
// pyb_usb_dev_deinit();
292+
#if MICROPY_HW_ENABLE_USB && !MICROPY_HW_TINYUSB_STACK
293+
pyb_usb_dev_deinit();
294294
#endif
295-
#if MICROPY_HW_ENABLE_USB_RUNTIME_DEVICE
295+
#if MICROPY_HW_ENABLE_USB_RUNTIME_DEVICE && MICROPY_HW_TINYUSB_STACK
296296
mp_usbd_deinit();
297297
#endif
298298

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 && 0
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 && 0
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 && 0
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: 20 additions & 17 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,22 +170,22 @@ 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
171-
// { MP_ROM_QSTR(MP_QSTR_usb_mode), MP_ROM_PTR(&pyb_usb_mode_obj) },
172-
// #if MICROPY_HW_USB_HID
173-
// { MP_ROM_QSTR(MP_QSTR_hid_mouse), MP_ROM_PTR(&pyb_usb_hid_mouse_obj) },
174-
// { MP_ROM_QSTR(MP_QSTR_hid_keyboard), MP_ROM_PTR(&pyb_usb_hid_keyboard_obj) },
175-
// { MP_ROM_QSTR(MP_QSTR_USB_HID), MP_ROM_PTR(&pyb_usb_hid_type) },
176-
// #endif
177-
// { MP_ROM_QSTR(MP_QSTR_USB_VCP), MP_ROM_PTR(&pyb_usb_vcp_type) },
178-
// #if MICROPY_PY_PYB_LEGACY
179-
// // these 2 are deprecated; use USB_VCP.isconnected and USB_HID.send instead
180-
// { MP_ROM_QSTR(MP_QSTR_have_cdc), MP_ROM_PTR(&pyb_have_cdc_obj) },
181-
// #if MICROPY_HW_USB_HID
182-
// { MP_ROM_QSTR(MP_QSTR_hid), MP_ROM_PTR(&pyb_hid_send_report_obj) },
183-
// #endif
184-
// #endif
185-
// #endif
173+
#if MICROPY_HW_ENABLE_USB && !MICROPY_HW_TINYUSB_STACK
174+
{ MP_ROM_QSTR(MP_QSTR_usb_mode), MP_ROM_PTR(&pyb_usb_mode_obj) },
175+
#if MICROPY_HW_USB_HID
176+
{ MP_ROM_QSTR(MP_QSTR_hid_mouse), MP_ROM_PTR(&pyb_usb_hid_mouse_obj) },
177+
{ MP_ROM_QSTR(MP_QSTR_hid_keyboard), MP_ROM_PTR(&pyb_usb_hid_keyboard_obj) },
178+
{ MP_ROM_QSTR(MP_QSTR_USB_HID), MP_ROM_PTR(&pyb_usb_hid_type) },
179+
#endif
180+
{ MP_ROM_QSTR(MP_QSTR_USB_VCP), MP_ROM_PTR(&pyb_usb_vcp_type) },
181+
#if MICROPY_PY_PYB_LEGACY
182+
// these 2 are deprecated; use USB_VCP.isconnected and USB_HID.send instead
183+
{ MP_ROM_QSTR(MP_QSTR_have_cdc), MP_ROM_PTR(&pyb_have_cdc_obj) },
184+
#if MICROPY_HW_USB_HID
185+
{ MP_ROM_QSTR(MP_QSTR_hid), MP_ROM_PTR(&pyb_hid_send_report_obj) },
186+
#endif
187+
#endif
188+
#endif
186189

187190
#if MICROPY_PY_PYB_LEGACY
188191
{ MP_ROM_QSTR(MP_QSTR_millis), MP_ROM_PTR(&mp_time_ticks_ms_obj) },

ports/stm32/mpconfigboard_common.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@
110110
#define MICROPY_HW_USB_CDC 1
111111
#define MICROPY_HW_USB_FS (1)
112112

113+
#ifndef MICROPY_HW_TINYUSB_STACK
114+
#define MICROPY_HW_TINYUSB_STACK (MICROPY_HW_ENABLE_USB)
115+
#endif
116+
113117
// Whether to enable the PA0-PA3 servo driver, exposed as pyb.Servo
114118
#ifndef MICROPY_HW_ENABLE_SERVO
115119
#define MICROPY_HW_ENABLE_SERVO (0)

ports/stm32/mphalport.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,19 @@
77
#include "extmod/misc.h"
88
#include "usb.h"
99
#include "uart.h"
10+
11+
#if MICROPY_HW_TINYUSB_STACK
1012
#include "shared/tinyusb/mp_usbd_cdc.h"
1113

1214
#ifndef MICROPY_HW_STDIN_BUFFER_LEN
1315
#define MICROPY_HW_STDIN_BUFFER_LEN 128
1416
#endif
1517

16-
extern volatile uint32_t ticks_us64_upper;
18+
// extern volatile uint32_t ticks_us64_upper;
1719

1820
static uint8_t stdin_ringbuf_array[MICROPY_HW_STDIN_BUFFER_LEN];
1921
ringbuf_t stdin_ringbuf = { stdin_ringbuf_array, sizeof(stdin_ringbuf_array), 0, 0 };
22+
#endif
2023

2124
// this table converts from HAL_StatusTypeDef to POSIX errno
2225
const byte mp_hal_status_to_errno_table[4] = {
@@ -36,7 +39,7 @@ NORETURN void mp_hal_raise(HAL_StatusTypeDef status) {
3639

3740
MP_WEAK uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
3841
uintptr_t ret = 0;
39-
#if MICROPY_HW_ENABLE_USBDEV && MICROPY_HW_USB_CDC
42+
#if MICROPY_HW_ENABLE_USBDEV && MICROPY_HW_USB_CDC && MICROPY_HW_TINYUSB_STACK
4043
ret |= mp_usbd_cdc_poll_interfaces(poll_flags);
4144
#endif
4245
if (MP_STATE_PORT(pyb_stdio_uart) != NULL) {
@@ -66,13 +69,13 @@ MP_WEAK int mp_hal_stdin_rx_chr(void) {
6669
if (dupterm_c >= 0) {
6770
return dupterm_c;
6871
}
69-
#if MICROPY_HW_ENABLE_USBDEV && MICROPY_HW_USB_CDC
72+
#if MICROPY_HW_ENABLE_USBDEV && MICROPY_HW_USB_CDC && MICROPY_HW_TINYUSB_STACK
7073
mp_usbd_cdc_poll_interfaces(0);
71-
#endif
7274
int c = ringbuf_get(&stdin_ringbuf);
7375
if (c != -1) {
7476
return c;
7577
}
78+
#endif
7679
MICROPY_EVENT_POLL_HOOK
7780
}
7881
}
@@ -84,7 +87,7 @@ MP_WEAK mp_uint_t mp_hal_stdout_tx_strn(const char *str, size_t len) {
8487
uart_tx_strn(MP_STATE_PORT(pyb_stdio_uart), str, len);
8588
did_write = true;
8689
}
87-
#if MICROPY_HW_ENABLE_USBDEV && MICROPY_HW_USB_CDC
90+
#if MICROPY_HW_ENABLE_USBDEV && MICROPY_HW_USB_CDC && MICROPY_HW_TINYUSB_STACK
8891
mp_uint_t cdc_res = mp_usbd_cdc_tx_strn(str, len);
8992
if (cdc_res > 0) {
9093
did_write = true;

ports/stm32/mphalport.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include STM32_HAL_H
33
#include "pin.h"
44
#include "py/ringbuf.h"
5+
#include "shared/runtime/interrupt_char.h"
56

67
extern uint8_t mp_hal_unique_id_address[12];
78

@@ -38,7 +39,6 @@ static inline int mp_hal_status_to_neg_errno(HAL_StatusTypeDef status) {
3839
return -mp_hal_status_to_errno_table[status];
3940
}
4041

41-
extern int mp_interrupt_char;
4242
extern ringbuf_t stdin_ringbuf;
4343

4444
NORETURN void mp_hal_raise(HAL_StatusTypeDef status);

0 commit comments

Comments
 (0)