Skip to content

Commit 63c94fe

Browse files
committed
stm32: Fix init sequence of USB hardware and TinyUSB stack.
This commit fixes the initialization sequence for TinyUSB when enabled on the stm32 port: - Following other ports, `mp_usbd_init()` should be called just after running `boot.py`, to give the user a chance to configure USB. - Hardware initialization (via `pyb_usbd_init()`) should only occur once, the first time TinyUSB is started up. This is achieved by adding a hook to the shared TinyUSB bindings to call `pyb_usbd_init()`, and only do the hardware init if TinyUSB was not already initialized. Also, `pyb_usbd_init()` is renamed `mp_usbd_ll_init()` to make it match with the rest of the stared TinyUSB binding code. Signed-off-by: Damien George <damien@micropython.org>
1 parent f43810b commit 63c94fe

File tree

5 files changed

+15
-8
lines changed

5 files changed

+15
-8
lines changed

ports/stm32/main.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -611,14 +611,9 @@ void stm32_main(uint32_t reset_mode) {
611611
pyb_can_init0();
612612
#endif
613613

614-
#if MICROPY_HW_ENABLE_USB
615-
#if MICROPY_HW_TINYUSB_STACK
616-
pyb_usbd_init();
617-
mp_usbd_init();
618-
#else
614+
#if MICROPY_HW_STM_USB_STACK && MICROPY_HW_ENABLE_USB
619615
pyb_usb_init0();
620616
#endif
621-
#endif
622617

623618
#if MICROPY_PY_MACHINE_I2S
624619
machine_i2s_init0();
@@ -690,6 +685,10 @@ void stm32_main(uint32_t reset_mode) {
690685
}
691686
#endif
692687

688+
#if MICROPY_HW_TINYUSB_STACK && MICROPY_HW_ENABLE_USBDEV
689+
mp_usbd_init();
690+
#endif
691+
693692
#if MICROPY_HW_HAS_MMA7660
694693
// MMA accel: init and reset
695694
accel_init();

ports/stm32/mpconfigboard_common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@
261261
#if MICROPY_HW_TINYUSB_STACK
262262
#ifndef MICROPY_HW_ENABLE_USBDEV
263263
#define MICROPY_HW_ENABLE_USBDEV (1)
264+
#define MICROPY_HW_TINYUSB_LL_INIT mp_usbd_ll_init
264265
#endif
265266

266267
#ifndef MICROPY_HW_USB_CDC

ports/stm32/mphalport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// We use the ST Cube HAL library for most hardware peripherals
22
#include STM32_HAL_H
33
#include "pin.h"
4+
#include "usbd_conf.h"
45
#include "py/ringbuf.h"
56
#include "shared/runtime/interrupt_char.h"
67

ports/stm32/usbd_conf.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "usbd_core.h"
3333
#include "py/obj.h"
3434
#include "py/mphal.h"
35+
#include "shared/tinyusb/mp_usbd.h"
3536
#include "irq.h"
3637
#include "usb.h"
3738

@@ -334,7 +335,12 @@ static void mp_usbd_ll_init_hs(void) {
334335

335336
#if MICROPY_HW_TINYUSB_STACK
336337

337-
void pyb_usbd_init(void) {
338+
void mp_usbd_ll_init(void) {
339+
// Only initialize the USB hardware once.
340+
if (tusb_inited()) {
341+
return;
342+
}
343+
338344
#if MICROPY_HW_USB_FS
339345
mp_usbd_ll_init_fs();
340346
#endif

ports/stm32/usbd_conf.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
#define USBD_HS_NUM_FIFO (1 + USBD_HS_NUM_TX_FIFO)
6666

6767
#if MICROPY_HW_TINYUSB_STACK
68-
void pyb_usbd_init(void);
68+
void mp_usbd_ll_init(void);
6969
#endif
7070

7171
#endif // MICROPY_INCLUDED_STM32_USBD_CONF_H

0 commit comments

Comments
 (0)