Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions ports/stm32/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ MBOOT_TEXT0_ADDR ?= 0x08000000
include $(TOP)/py/py.mk
include $(TOP)/extmod/extmod.mk

GIT_SUBMODULES += lib/libhydrogen lib/stm32lib
GIT_SUBMODULES += lib/libhydrogen lib/stm32lib lib/tinyusb

CROSS_COMPILE ?= arm-none-eabi-
LD_DIR=boards
Expand Down Expand Up @@ -110,6 +110,9 @@ INC += -I$(STM32LIB_CMSIS_ABS)/Include
INC += -I$(STM32LIB_HAL_ABS)/Inc
INC += -I$(USBDEV_DIR)/core/inc -I$(USBDEV_DIR)/class/inc
#INC += -I$(USBHOST_DIR)
INC += -I$(TOP)/lib/tinyusb/src
INC += -I$(TOP)/shared/tinyusb/

INC += -Ilwip_inc

CFLAGS += $(INC) -Wall -Wpointer-arith -Werror -Wdouble-promotion -Wfloat-conversion -std=gnu99 -nostdlib $(CFLAGS_EXTRA)
Expand Down Expand Up @@ -213,6 +216,10 @@ SHARED_SRC_C += $(addprefix shared/,\
runtime/stdout_helpers.c \
runtime/sys_stdio_mphal.c \
timeutils/timeutils.c \
tinyusb/mp_usbd.c \
tinyusb/mp_usbd_cdc.c \
tinyusb/mp_usbd_descriptor.c \
tinyusb/mp_usbd_runtime.c \
)

ifeq ($(MICROPY_FLOAT_IMPL),double)
Expand All @@ -238,11 +245,21 @@ DRIVERS_SRC_C += $(addprefix drivers/,\
dht/dht.c \
)

# TinyUSB Stack source
-include $(TOP)/lib/tinyusb/src/tinyusb.mk
TINYUSB_SRC_C := $(addprefix lib/tinyusb/, \
$(TINYUSB_SRC_C) \
src/portable/st/stm32_fsdev/dcd_stm32_fsdev.c \
src/portable/synopsys/dwc2/dcd_dwc2.c \
)

SRC_C += \
boardctrl.c \
main.c \
stm32_it.c \
usbd.c \
usbd_conf.c \
usb.c \
usbd_desc.c \
usbd_cdc_interface.c \
usbd_hid_interface.c \
Expand Down Expand Up @@ -277,7 +294,6 @@ SRC_C += \
can.c \
fdcan.c \
pyb_can.c \
usb.c \
eth.c \
eth_phy.c \
gccollect.c \
Expand Down Expand Up @@ -499,6 +515,7 @@ OBJ += $(addprefix $(BUILD)/, $(SHARED_SRC_C:.c=.o))
OBJ += $(addprefix $(BUILD)/, $(DRIVERS_SRC_C:.c=.o))
OBJ += $(addprefix $(BUILD)/, $(HAL_SRC_C:.c=.o))
OBJ += $(addprefix $(BUILD)/, $(USBDEV_SRC_C:.c=.o))
OBJ += $(addprefix $(BUILD)/, $(TINYUSB_SRC_C:.c=.o))
OBJ += $(addprefix $(BUILD)/, $(SRC_C:.c=.o))
OBJ += $(addprefix $(BUILD)/, $(SRC_CXX:.cpp=.o))
OBJ += $(GEN_PINS_SRC:.c=.o)
Expand Down
24 changes: 18 additions & 6 deletions ports/stm32/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@
#include "pin.h"
#include "extint.h"
#include "usrsw.h"
#include "usb.h"
#include "rtc.h"
#include "storage.h"
#include "sdcard.h"
Expand All @@ -89,6 +88,13 @@
#include "pyb_can.h"
#include "subghz.h"

#if MICROPY_HW_TINYUSB_STACK
#include "usbd_conf.h"
#include "shared/tinyusb/mp_usbd.h"
#else
#include "usb.h"
#endif

#if MICROPY_PY_THREAD
static pyb_thread_t pyb_thread_main;
#endif
Expand Down Expand Up @@ -276,14 +282,12 @@ static bool init_sdcard_fs(void) {
}
}

#if MICROPY_HW_ENABLE_USB
#if MICROPY_HW_ENABLE_USB && !MICROPY_HW_TINYUSB_STACK
if (pyb_usb_storage_medium == PYB_USB_STORAGE_MEDIUM_NONE) {
// if no USB MSC medium is selected then use the SD card
pyb_usb_storage_medium = PYB_USB_STORAGE_MEDIUM_SDCARD;
}
#endif

#if MICROPY_HW_ENABLE_USB
// only use SD card as current directory if that's what the USB medium is
if (pyb_usb_storage_medium == PYB_USB_STORAGE_MEDIUM_SDCARD)
#endif
Expand Down Expand Up @@ -604,8 +608,13 @@ void stm32_main(uint32_t reset_mode) {
#endif

#if MICROPY_HW_ENABLE_USB
#if MICROPY_HW_TINYUSB_STACK
pyb_usbd_init();
mp_usbd_init();
#else
pyb_usb_init0();
#endif
#endif

#if MICROPY_PY_MACHINE_I2S
machine_i2s_init0();
Expand All @@ -629,7 +638,7 @@ void stm32_main(uint32_t reset_mode) {
}
#endif

#if MICROPY_HW_ENABLE_USB
#if MICROPY_HW_STM_USB_STACK
// if the SD card isn't used as the USB MSC medium then use the internal flash
if (pyb_usb_storage_medium == PYB_USB_STORAGE_MEDIUM_NONE) {
pyb_usb_storage_medium = PYB_USB_STORAGE_MEDIUM_FLASH;
Expand Down Expand Up @@ -663,7 +672,7 @@ void stm32_main(uint32_t reset_mode) {
// or whose initialisation can be safely deferred until after running
// boot.py.

#if MICROPY_HW_ENABLE_USB
#if MICROPY_HW_STM_USB_STACK
// init USB device to default setting if it was not already configured
if (!(pyb_usb_flags & PYB_USB_FLAG_USB_MODE_CALLED)) {
#if MICROPY_HW_USB_MSC
Expand Down Expand Up @@ -768,6 +777,9 @@ void stm32_main(uint32_t reset_mode) {
#else
MP_STATE_PORT(pyb_stdio_uart) = NULL;
#endif
#if MICROPY_HW_ENABLE_USB_RUNTIME_DEVICE && MICROPY_HW_TINYUSB_STACK
mp_usbd_deinit();
#endif

MICROPY_BOARD_END_SOFT_RESET(&state);

Expand Down
7 changes: 6 additions & 1 deletion ports/stm32/modmachine.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include "rtc.h"
#include "i2c.h"
#include "spi.h"
#include "shared/tinyusb/mp_usbd.h"

#if defined(STM32G0)
// G0 has BOR and POR combined
Expand Down Expand Up @@ -297,9 +298,13 @@ MP_NORETURN static void mp_machine_reset(void) {

// Activate the bootloader without BOOT* pins.
MP_NORETURN void mp_machine_bootloader(size_t n_args, const mp_obj_t *args) {
#if MICROPY_HW_ENABLE_USB
#if MICROPY_HW_ENABLE_USB && !MICROPY_HW_TINYUSB_STACK
pyb_usb_dev_deinit();
#endif
#if MICROPY_HW_ENABLE_USB_RUNTIME_DEVICE && MICROPY_HW_TINYUSB_STACK
mp_usbd_deinit();
#endif

#if MICROPY_HW_ENABLE_STORAGE
storage_flush();
#endif
Expand Down
6 changes: 3 additions & 3 deletions ports/stm32/modos.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ bool mp_os_dupterm_is_builtin_stream(mp_const_obj_t stream) {
#if MICROPY_PY_MACHINE_UART
|| type == &machine_uart_type
#endif
#if MICROPY_HW_ENABLE_USB
#if MICROPY_HW_STM_USB_STACK
|| type == &pyb_usb_vcp_type
#endif
;
Expand All @@ -64,7 +64,7 @@ void mp_os_dupterm_stream_detached_attached(mp_obj_t stream_detached, mp_obj_t s
uart_attach_to_repl(MP_OBJ_TO_PTR(stream_detached), false);
}
#endif
#if MICROPY_HW_ENABLE_USB
#if MICROPY_HW_STM_USB_STACK
if (mp_obj_get_type(stream_detached) == &pyb_usb_vcp_type) {
usb_vcp_attach_to_repl(MP_OBJ_TO_PTR(stream_detached), false);
}
Expand All @@ -75,7 +75,7 @@ void mp_os_dupterm_stream_detached_attached(mp_obj_t stream_detached, mp_obj_t s
uart_attach_to_repl(MP_OBJ_TO_PTR(stream_attached), true);
}
#endif
#if MICROPY_HW_ENABLE_USB
#if MICROPY_HW_STM_USB_STACK
if (mp_obj_get_type(stream_attached) == &pyb_usb_vcp_type) {
usb_vcp_attach_to_repl(MP_OBJ_TO_PTR(stream_attached), true);
}
Expand Down
7 changes: 5 additions & 2 deletions ports/stm32/modpyb.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,17 @@
#include "servo.h"
#include "dac.h"
#include "lcd.h"
#include "usb.h"
#include "portmodules.h"
#include "modmachine.h"
#include "extmod/modmachine.h"
#include "extmod/modnetwork.h"
#include "extmod/vfs.h"
#include "extmod/modtime.h"

#if !MICROPY_HW_TINYUSB_STACK
#include "usb.h"
#endif

#if MICROPY_PY_PYB

static mp_obj_t pyb_fault_debug(mp_obj_t value) {
Expand Down Expand Up @@ -167,7 +170,7 @@ static const mp_rom_map_elem_t pyb_module_globals_table[] = {
// Deprecated (use network.country instead).
{ MP_ROM_QSTR(MP_QSTR_country), MP_ROM_PTR(&mod_network_country_obj) },

#if MICROPY_HW_ENABLE_USB
#if MICROPY_HW_STM_USB_STACK
{ MP_ROM_QSTR(MP_QSTR_usb_mode), MP_ROM_PTR(&pyb_usb_mode_obj) },
#if MICROPY_HW_USB_HID
{ MP_ROM_QSTR(MP_QSTR_hid_mouse), MP_ROM_PTR(&pyb_usb_hid_mouse_obj) },
Expand Down
46 changes: 44 additions & 2 deletions ports/stm32/mpconfigboard_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,24 @@
#define MICROPY_HW_ENABLE_USB (0)
#endif

#if MICROPY_HW_ENABLE_USB
#define MICROPY_HW_USB_CDC (1)
#define MICROPY_HW_USB_FS (1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These options cannot be enabled unconditionally, because they may clash with board configurations (when using the STM USB stack).

Either just remove these lines (my preference, if that works) or at least only enable them if MICROPY_HW_TINYUSB_STACK is enabled.


// Select whether TinyUSB or legacy STM stack is used to provide USB.
#ifndef MICROPY_HW_TINYUSB_STACK
#define MICROPY_HW_TINYUSB_STACK (0)
#endif

// Central definition for STM USB stack (when not using TinyUSB)
#define MICROPY_HW_STM_USB_STACK (MICROPY_HW_ENABLE_USB && !MICROPY_HW_TINYUSB_STACK)

#if MICROPY_HW_TINYUSB_STACK
#define MICROPY_HW_ENABLE_USBDEV (1)
#endif

#endif // MICROPY_HW_ENABLE_USB

// Whether to enable the PA0-PA3 servo driver, exposed as pyb.Servo
#ifndef MICROPY_HW_ENABLE_SERVO
#define MICROPY_HW_ENABLE_SERVO (0)
Expand Down Expand Up @@ -256,6 +274,8 @@
// Windows needs a different PID to distinguish different device configurations.
#ifndef MICROPY_HW_USB_VID
#define MICROPY_HW_USB_VID (0xf055)
#define MICROPY_HW_USB_PID (0x9802)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a comment that this is only used in the TinyUSB configuration (and eventually we'll need to change this value depending on whether MSC is enabled etc).


#define MICROPY_HW_USB_PID_CDC_MSC (0x9800)
#define MICROPY_HW_USB_PID_CDC_HID (0x9801)
#define MICROPY_HW_USB_PID_CDC (0x9802)
Expand Down Expand Up @@ -369,6 +389,8 @@
#endif
#define MICROPY_HW_MAX_LPUART (0)

#define CFG_TUSB_MCU OPT_MCU_STM32F4

// Configuration for STM32F7 series
#elif defined(STM32F7)

Expand All @@ -384,6 +406,8 @@
#define MICROPY_HW_MAX_UART (8)
#define MICROPY_HW_MAX_LPUART (0)

#define CFG_TUSB_MCU OPT_MCU_STM32F7

// Configuration for STM32G0 series
#elif defined(STM32G0)

Expand All @@ -394,6 +418,8 @@
#define MICROPY_HW_MAX_UART (6)
#define MICROPY_HW_MAX_LPUART (2)

#define CFG_TUSB_MCU OPT_MCU_STM32G0

// Configuration for STM32G4 series
#elif defined(STM32G4)

Expand All @@ -404,6 +430,8 @@
#define MICROPY_HW_MAX_UART (5) // UART1-5 + LPUART1
#define MICROPY_HW_MAX_LPUART (1)

#define CFG_TUSB_MCU OPT_MCU_STM32G4

// Configuration for STM32H5 series
#elif defined(STM32H5)

Expand All @@ -414,6 +442,8 @@
#define MICROPY_HW_MAX_UART (12)
#define MICROPY_HW_MAX_LPUART (1)

#define CFG_TUSB_MCU OPT_MCU_STM32H5

// Configuration for STM32H7A3/B3 series
#elif defined(STM32H7A3xx) || defined(STM32H7A3xxQ) || \
defined(STM32H7B3xx) || defined(STM32H7B3xxQ)
Expand All @@ -425,6 +455,8 @@
#define MICROPY_HW_MAX_UART (10)
#define MICROPY_HW_MAX_LPUART (1)

#define CFG_TUSB_MCU OPT_MCU_STM32H7

// Configuration for STM32H7 series
#elif defined(STM32H7)

Expand All @@ -435,6 +467,8 @@
#define MICROPY_HW_MAX_UART (8)
#define MICROPY_HW_MAX_LPUART (1)

#define CFG_TUSB_MCU OPT_MCU_STM32H7

#if defined(MICROPY_HW_ANALOG_SWITCH_PA0) \
|| defined(MICROPY_HW_ANALOG_SWITCH_PA1) \
|| defined(MICROPY_HW_ANALOG_SWITCH_PC2) \
Expand All @@ -454,6 +488,8 @@
#define MICROPY_HW_MAX_UART (5)
#define MICROPY_HW_MAX_LPUART (1)

#define CFG_TUSB_MCU OPT_MCU_STM32L0

// Configuration for STM32L1 series
#elif defined(STM32L1)
#define MP_HAL_UNIQUE_ID_ADDRESS (UID_BASE)
Expand All @@ -464,6 +500,8 @@
#define MICROPY_HW_MAX_UART (5)
#define MICROPY_HW_MAX_LPUART (0)

#define CFG_TUSB_MCU OPT_MCU_STM32L1

// Configuration for STM32L4 series
#elif defined(STM32L4)

Expand All @@ -474,6 +512,8 @@
#define MICROPY_HW_MAX_UART (5)
#define MICROPY_HW_MAX_LPUART (1)

#define CFG_TUSB_MCU OPT_MCU_STM32L4

// Configuration for STM32N6 series
#elif defined(STM32N6)

Expand All @@ -494,6 +534,8 @@
#define MICROPY_HW_MAX_UART (1)
#define MICROPY_HW_MAX_LPUART (1)

#define CFG_TUSB_MCU OPT_MCU_STM32WB

#ifndef MICROPY_HW_STM32WB_FLASH_SYNCRONISATION
#define MICROPY_HW_STM32WB_FLASH_SYNCRONISATION (1)
#endif
Expand Down Expand Up @@ -699,10 +741,10 @@
#define MICROPY_HW_USB_CDC_NUM (1)
#endif
#ifndef MICROPY_HW_USB_MSC
#define MICROPY_HW_USB_MSC (MICROPY_HW_ENABLE_USB)
#define MICROPY_HW_USB_MSC (MICROPY_HW_STM_USB_STACK)
#endif
#ifndef MICROPY_HW_USB_HID
#define MICROPY_HW_USB_HID (MICROPY_HW_ENABLE_USB)
#define MICROPY_HW_USB_HID (MICROPY_HW_STM_USB_STACK)
#endif

// Pin definition header file
Expand Down
3 changes: 3 additions & 0 deletions ports/stm32/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,9 @@
#define MICROPY_FATFS_USE_LABEL (1)
#define MICROPY_FATFS_RPATH (2)
#define MICROPY_FATFS_MULTI_PARTITION (1)
#if MICROPY_HW_TINYUSB_STACK && CFG_TUD_MSC
#define MICROPY_FATFS_MAX_SS (4096)
#endif

#if MICROPY_PY_PYB
extern const struct _mp_obj_module_t pyb_module;
Expand Down
Loading
Loading