Skip to content

Commit e7ad9be

Browse files
dpgeorgeiabdalkader
authored andcommitted
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 01fdd2e commit e7ad9be

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
@@ -607,14 +607,9 @@ void stm32_main(uint32_t reset_mode) {
607607
pyb_can_init0();
608608
#endif
609609

610-
#if MICROPY_HW_ENABLE_USB
611-
#if MICROPY_HW_TINYUSB_STACK
612-
pyb_usbd_init();
613-
mp_usbd_init();
614-
#else
610+
#if MICROPY_HW_STM_USB_STACK && MICROPY_HW_ENABLE_USB
615611
pyb_usb_init0();
616612
#endif
617-
#endif
618613

619614
#if MICROPY_PY_MACHINE_I2S
620615
machine_i2s_init0();
@@ -686,6 +681,10 @@ void stm32_main(uint32_t reset_mode) {
686681
}
687682
#endif
688683

684+
#if MICROPY_HW_TINYUSB_STACK && MICROPY_HW_ENABLE_USBDEV
685+
mp_usbd_init();
686+
#endif
687+
689688
#if MICROPY_HW_HAS_MMA7660
690689
// MMA accel: init and reset
691690
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

@@ -305,7 +306,12 @@ static void mp_usbd_ll_init_hs(void) {
305306

306307
#if MICROPY_HW_TINYUSB_STACK
307308

308-
void pyb_usbd_init(void) {
309+
void mp_usbd_ll_init(void) {
310+
// Only initialize the USB hardware once.
311+
if (tusb_inited()) {
312+
return;
313+
}
314+
309315
#if MICROPY_HW_USB_FS
310316
mp_usbd_ll_init_fs();
311317
#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)