Skip to content

Commit

Permalink
stm32/usb: Use TinyUSB stack by default.
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Leech <[email protected]>
  • Loading branch information
pi-anl committed Aug 3, 2024
1 parent 6f27e1c commit 078d339
Show file tree
Hide file tree
Showing 14 changed files with 228 additions and 62 deletions.
41 changes: 24 additions & 17 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

LD_DIR=boards
USBDEV_DIR=usbdev
Expand Down Expand Up @@ -112,6 +112,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 @@ -199,6 +202,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 @@ -223,16 +230,26 @@ DRIVERS_SRC_C += $(addprefix drivers/,\
memory/spiflash.c \
dht/dht.c \
)

TINYUSB_SRC_C += $(addprefix lib/tinyusb/src/,\
class/cdc/cdc_device.c \
class/msc/msc_device.c \
common/tusb_fifo.c \
device/usbd.c \
device/usbd_control.c \
portable/st/stm32_fsdev/dcd_stm32_fsdev.c \
portable/synopsys/dwc2/dcd_dwc2.c \
tusb.c \
)
LDFLAGS += -Wl,--wrap=dcd_event_handler

# usbd_conf.c

SRC_C += \
boardctrl.c \
main.c \
stm32_it.c \
usbd_conf.c \
usbd_desc.c \
usbd_cdc_interface.c \
usbd_hid_interface.c \
usbd_msc_interface.c \
usbd.c \
mphalport.c \
mpnetworkport.c \
mpthreadport.c \
Expand Down Expand Up @@ -262,7 +279,6 @@ SRC_C += \
can.c \
fdcan.c \
pyb_can.c \
usb.c \
eth.c \
eth_phy.c \
gccollect.c \
Expand Down Expand Up @@ -408,15 +424,6 @@ HAL_SRC_C += $(addprefix $(STM32LIB_HAL_BASE)/Src/stm32$(MCU_SERIES)xx_,\
)
endif

USBDEV_SRC_C += $(addprefix $(USBDEV_DIR)/,\
core/src/usbd_core.c \
core/src/usbd_ctlreq.c \
core/src/usbd_ioreq.c \
class/src/usbd_cdc_msc_hid.c \
class/src/usbd_msc_bot.c \
class/src/usbd_msc_scsi.c \
)

ifeq ($(MICROPY_SSL_MBEDTLS),1)
LIB_SRC_C += mbedtls/mbedtls_port.c
endif
Expand Down Expand Up @@ -455,7 +462,7 @@ OBJ += $(addprefix $(BUILD)/, $(LIBM_SRC_C:.c=.o))
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
90 changes: 84 additions & 6 deletions ports/stm32/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
#include "pin.h"
#include "extint.h"
#include "usrsw.h"
#include "usb.h"
// #include "usb.h"
#include "rtc.h"
#include "storage.h"
#include "sdcard.h"
Expand All @@ -89,6 +89,9 @@
#include "can.h"
#include "subghz.h"

#include "shared/tinyusb/mp_usbd.h"


#if MICROPY_PY_THREAD
static pyb_thread_t pyb_thread_main;
#endif
Expand Down Expand Up @@ -299,6 +302,54 @@ static bool init_sdcard_fs(void) {
}
#endif

void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd) {
GPIO_InitTypeDef GPIO_InitStruct = {0};
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
if (hpcd->Instance == USB) {
/* USER CODE BEGIN USB_MspInit 0 */

/* USER CODE END USB_MspInit 0 */

/** Initializes the peripherals clock
*/
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_USB;
PeriphClkInitStruct.PLLSAI1.PLLN = 24;
PeriphClkInitStruct.PLLSAI1.PLLP = RCC_PLLP_DIV2;
PeriphClkInitStruct.PLLSAI1.PLLQ = RCC_PLLQ_DIV2;
PeriphClkInitStruct.PLLSAI1.PLLR = RCC_PLLR_DIV2;
PeriphClkInitStruct.PLLSAI1.PLLSAI1ClockOut = RCC_PLLSAI1_USBCLK;
PeriphClkInitStruct.UsbClockSelection = RCC_USBCLKSOURCE_PLLSAI1;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) {
// Error_Handler();
}

__HAL_RCC_GPIOA_CLK_ENABLE();
/**USB GPIO Configuration
PA11 ------> USB_DM
PA12 ------> USB_DP
*/
GPIO_InitStruct.Pin = GPIO_PIN_11 | GPIO_PIN_12;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF10_USB;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

/* Peripheral clock enable */
__HAL_RCC_USB_CLK_ENABLE();
/* USB interrupt Init */
HAL_NVIC_SetPriority(USB_LP_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(USB_LP_IRQn);
/* USER CODE BEGIN USB_MspInit 1 */

/* USER CODE END USB_MspInit 1 */
}

}


PCD_HandleTypeDef hpcd_USB_FS;

void stm32_main(uint32_t reset_mode) {
// Low-level MCU initialisation.
stm32_system_init();
Expand Down Expand Up @@ -529,7 +580,30 @@ void stm32_main(uint32_t reset_mode) {
#endif

#if MICROPY_HW_ENABLE_USB
pyb_usb_init0();
// hpcd_USB_FS.Instance = USB;
// hpcd_USB_FS.Init.dev_endpoints = 8;
// hpcd_USB_FS.Init.speed = PCD_SPEED_FULL;
// hpcd_USB_FS.Init.phy_itface = PCD_PHY_EMBEDDED;
// hpcd_USB_FS.Init.Sof_enable = ENABLE;
// hpcd_USB_FS.Init.low_power_enable = DISABLE;
// hpcd_USB_FS.Init.lpm_enable = DISABLE;
// hpcd_USB_FS.Init.battery_charging_enable = DISABLE;
// if (HAL_PCD_Init(&hpcd_USB_FS) != HAL_OK)
// {
// }

GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = (GPIO_PIN_11 | GPIO_PIN_12);
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

HAL_PWREx_EnableVddUSB();
__HAL_RCC_USB_CLK_ENABLE();
mp_usbd_init();
// mp_usbd_task();
// pyb_usb_init0();
#endif

#if MICROPY_PY_MACHINE_I2S
Expand All @@ -556,9 +630,9 @@ void stm32_main(uint32_t reset_mode) {

#if MICROPY_HW_ENABLE_USB
// 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;
}
// if (pyb_usb_storage_medium == PYB_USB_STORAGE_MEDIUM_NONE) {
// pyb_usb_storage_medium = PYB_USB_STORAGE_MEDIUM_FLASH;
// }
#endif

// set sys.path based on mounted filesystems (/sd is first so it can override /flash)
Expand Down Expand Up @@ -588,7 +662,8 @@ 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 0
// 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 @@ -686,6 +761,9 @@ void stm32_main(uint32_t reset_mode) {
#else
MP_STATE_PORT(pyb_stdio_uart) = NULL;
#endif
#if MICROPY_HW_ENABLE_USB_RUNTIME_DEVICE
mp_usbd_deinit();
#endif

MICROPY_BOARD_END_SOFT_RESET(&state);

Expand Down
6 changes: 5 additions & 1 deletion ports/stm32/modmachine.c
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,12 @@ NORETURN static void mp_machine_reset(void) {
// Activate the bootloader without BOOT* pins.
NORETURN void mp_machine_bootloader(size_t n_args, const mp_obj_t *args) {
#if MICROPY_HW_ENABLE_USB
pyb_usb_dev_deinit();
// pyb_usb_dev_deinit();
#endif
#if MICROPY_HW_ENABLE_USB_RUNTIME_DEVICE
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_ENABLE_USB && 0
|| 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_ENABLE_USB && 0
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_ENABLE_USB && 0
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
34 changes: 17 additions & 17 deletions ports/stm32/modpyb.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
#include "servo.h"
#include "dac.h"
#include "lcd.h"
#include "usb.h"
// #include "usb.h"
#include "portmodules.h"
#include "modmachine.h"
#include "extmod/modmachine.h"
Expand Down Expand Up @@ -167,22 +167,22 @@ 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
{ 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) },
{ MP_ROM_QSTR(MP_QSTR_hid_keyboard), MP_ROM_PTR(&pyb_usb_hid_keyboard_obj) },
{ MP_ROM_QSTR(MP_QSTR_USB_HID), MP_ROM_PTR(&pyb_usb_hid_type) },
#endif
{ MP_ROM_QSTR(MP_QSTR_USB_VCP), MP_ROM_PTR(&pyb_usb_vcp_type) },
#if MICROPY_PY_PYB_LEGACY
// these 2 are deprecated; use USB_VCP.isconnected and USB_HID.send instead
{ MP_ROM_QSTR(MP_QSTR_have_cdc), MP_ROM_PTR(&pyb_have_cdc_obj) },
#if MICROPY_HW_USB_HID
{ MP_ROM_QSTR(MP_QSTR_hid), MP_ROM_PTR(&pyb_hid_send_report_obj) },
#endif
#endif
#endif
// #if MICROPY_HW_ENABLE_USB
// { 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) },
// { MP_ROM_QSTR(MP_QSTR_hid_keyboard), MP_ROM_PTR(&pyb_usb_hid_keyboard_obj) },
// { MP_ROM_QSTR(MP_QSTR_USB_HID), MP_ROM_PTR(&pyb_usb_hid_type) },
// #endif
// { MP_ROM_QSTR(MP_QSTR_USB_VCP), MP_ROM_PTR(&pyb_usb_vcp_type) },
// #if MICROPY_PY_PYB_LEGACY
// // these 2 are deprecated; use USB_VCP.isconnected and USB_HID.send instead
// { MP_ROM_QSTR(MP_QSTR_have_cdc), MP_ROM_PTR(&pyb_have_cdc_obj) },
// #if MICROPY_HW_USB_HID
// { MP_ROM_QSTR(MP_QSTR_hid), MP_ROM_PTR(&pyb_hid_send_report_obj) },
// #endif
// #endif
// #endif

#if MICROPY_PY_PYB_LEGACY
{ MP_ROM_QSTR(MP_QSTR_millis), MP_ROM_PTR(&mp_time_ticks_ms_obj) },
Expand Down
Loading

0 comments on commit 078d339

Please sign in to comment.