Skip to content
Merged
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
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[submodule "firmware/bsp/hpm_sdk"]
path = firmware/rmcs_board/bsp/hpm_sdk
url = https://github.com/Alliance-Algorithm/hpm_sdk.git
[submodule "firmware/c_board/bsp/tinyusb"]
path = firmware/c_board/bsp/tinyusb
url = https://github.com/qzhhhi/tinyusb.git
30 changes: 30 additions & 0 deletions firmware/c_board/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ enable_language(C CXX ASM)
set(LIBRMCS_PROJECT_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/../..")
cmake_path(ABSOLUTE_PATH LIBRMCS_PROJECT_ROOT NORMALIZE)

set(LIBRMCS_TINYUSB_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/bsp/tinyusb")
if(NOT EXISTS "${LIBRMCS_TINYUSB_ROOT}/src/tusb.h")
message(FATAL_ERROR "TinyUSB submodule not found at ${LIBRMCS_TINYUSB_ROOT}. "
"Run: git submodule update --init firmware/c_board/bsp/tinyusb")
endif()

if(NOT DEFINED LIBRMCS_PROJECT_VERSION OR LIBRMCS_PROJECT_VERSION STREQUAL "")
execute_process(
COMMAND ".scripts/generate_version"
Expand All @@ -43,6 +49,29 @@ list(APPEND TOOLCHAIN_LINK_LIBRARIES "stdc++")
# Bring in CubeMX generated HAL build definitions.
add_subdirectory(bsp/HAL/cmake/stm32cubemx)

add_library(tinyusb_device OBJECT)
target_sources(tinyusb_device PRIVATE
"${LIBRMCS_TINYUSB_ROOT}/src/tusb.c"
"${LIBRMCS_TINYUSB_ROOT}/src/common/tusb_fifo.c"
"${LIBRMCS_TINYUSB_ROOT}/src/device/usbd.c"
"${LIBRMCS_TINYUSB_ROOT}/src/device/usbd_control.c"
"${LIBRMCS_TINYUSB_ROOT}/src/class/vendor/vendor_device.c"
"${LIBRMCS_TINYUSB_ROOT}/src/portable/synopsys/dwc2/dwc2_common.c"
"${LIBRMCS_TINYUSB_ROOT}/src/portable/synopsys/dwc2/dcd_dwc2.c"
)
target_include_directories(tinyusb_device SYSTEM PUBLIC
"${LIBRMCS_TINYUSB_ROOT}/src"
)
target_include_directories(tinyusb_device PUBLIC
"${CMAKE_CURRENT_SOURCE_DIR}/include"
)
target_compile_definitions(tinyusb_device PUBLIC
CFG_TUSB_MCU=OPT_MCU_STM32F4
)
target_link_libraries(tinyusb_device PUBLIC
stm32cubemx
)

# Mark CubeMX/HAL headers as SYSTEM so they show up as `-isystem` in
# compile_commands.json (clang-tidy should not warn on third-party code).
if(TARGET stm32cubemx)
Expand Down Expand Up @@ -126,4 +155,5 @@ list(REMOVE_ITEM CMAKE_C_IMPLICIT_LINK_LIBRARIES ob)

target_link_libraries(${CMAKE_PROJECT_NAME}
stm32cubemx
tinyusb_device
)
40 changes: 15 additions & 25 deletions firmware/c_board/bsp/HAL/.mxproject

Large diffs are not rendered by default.

52 changes: 52 additions & 0 deletions firmware/c_board/bsp/HAL/Core/Inc/usb_otg.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file usb_otg.h
* @brief This file contains all the function prototypes for
* the usb_otg.c file
******************************************************************************
* @attention
*
* Copyright (c) 2026 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __USB_OTG_H__
#define __USB_OTG_H__

#ifdef __cplusplus
extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "main.h"

/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

extern PCD_HandleTypeDef hpcd_USB_OTG_FS;

/* USER CODE BEGIN Private defines */

/* USER CODE END Private defines */

void MX_USB_OTG_FS_PCD_Init(void);

/* USER CODE BEGIN Prototypes */

/* USER CODE END Prototypes */

#ifdef __cplusplus
}
#endif

#endif /* __USB_OTG_H__ */

19 changes: 2 additions & 17 deletions firmware/c_board/bsp/HAL/Core/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include "spi.h"
#include "tim.h"
#include "usart.h"
#include "usb_device.h"
#include "usb_otg.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
Expand Down Expand Up @@ -92,33 +92,18 @@ int main(void)
DWT->CYCCNT = 0;
DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;

// Generate USB PID from STM32 12-byte unique id using simple CRC-16 algorithm.
extern __ALIGN_BEGIN uint8_t USBD_FS_DeviceDesc[USB_LEN_DEV_DESC] __ALIGN_END;
uint16_t pid = 0xFFFF;
uint8_t *data = (uint8_t*)0x1FFF7A10u;
size_t len = 12;
while (len--) {
pid ^= *data++;
for (int i = 0; i < 8; i++) {
if (pid & 1) pid = (pid >> 1) ^ 0x8408;
else pid = (pid >> 1);
}
}
USBD_FS_DeviceDesc[10] = LOBYTE(pid);
USBD_FS_DeviceDesc[11] = HIBYTE(pid);

/* USER CODE END SysInit */

/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_SPI1_Init();
MX_USB_DEVICE_Init();
MX_CAN1_Init();
MX_CAN2_Init();
MX_USART1_UART_Init();
MX_USART3_UART_Init();
MX_USART6_UART_Init();
MX_TIM5_Init();
MX_USB_OTG_FS_PCD_Init();
/* USER CODE BEGIN 2 */

AppEntry();
Expand Down
23 changes: 13 additions & 10 deletions firmware/c_board/bsp/HAL/Core/Src/stm32f4xx_it.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stm32f4xx_it.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <tusb.h>
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN TD */
Expand Down Expand Up @@ -55,13 +56,13 @@
/* USER CODE END 0 */

/* External variables --------------------------------------------------------*/
extern PCD_HandleTypeDef hpcd_USB_OTG_FS;
extern CAN_HandleTypeDef hcan1;
extern CAN_HandleTypeDef hcan2;
extern SPI_HandleTypeDef hspi1;
extern UART_HandleTypeDef huart1;
extern UART_HandleTypeDef huart3;
extern UART_HandleTypeDef huart6;
extern PCD_HandleTypeDef hpcd_USB_OTG_FS;
/* USER CODE BEGIN EV */

/* USER CODE END EV */
Expand Down Expand Up @@ -305,12 +306,14 @@ void CAN2_RX0_IRQHandler(void)
/**
* @brief This function handles USB On The Go FS global interrupt.
*/
void OTG_FS_IRQHandler(void)
{
/* USER CODE BEGIN OTG_FS_IRQn 0 */

/* USER CODE END OTG_FS_IRQn 0 */
HAL_PCD_IRQHandler(&hpcd_USB_OTG_FS);
void OTG_FS_IRQHandler(void)
{
/* USER CODE BEGIN OTG_FS_IRQn 0 */
tusb_int_handler(0, true);
return;

/* USER CODE END OTG_FS_IRQn 0 */
HAL_PCD_IRQHandler(&hpcd_USB_OTG_FS);
/* USER CODE BEGIN OTG_FS_IRQn 1 */

/* USER CODE END OTG_FS_IRQn 1 */
Expand Down
122 changes: 122 additions & 0 deletions firmware/c_board/bsp/HAL/Core/Src/usb_otg.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file usb_otg.c
* @brief This file provides code for the configuration
* of the USB_OTG instances.
******************************************************************************
* @attention
*
* Copyright (c) 2026 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "usb_otg.h"

/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

PCD_HandleTypeDef hpcd_USB_OTG_FS;

/* USB_OTG_FS init function */

void MX_USB_OTG_FS_PCD_Init(void)
{

/* USER CODE BEGIN USB_OTG_FS_Init 0 */

/* USER CODE END USB_OTG_FS_Init 0 */

/* USER CODE BEGIN USB_OTG_FS_Init 1 */

/* USER CODE END USB_OTG_FS_Init 1 */
hpcd_USB_OTG_FS.Instance = USB_OTG_FS;
hpcd_USB_OTG_FS.Init.dev_endpoints = 4;
hpcd_USB_OTG_FS.Init.speed = PCD_SPEED_FULL;
hpcd_USB_OTG_FS.Init.dma_enable = DISABLE;
hpcd_USB_OTG_FS.Init.phy_itface = PCD_PHY_EMBEDDED;
hpcd_USB_OTG_FS.Init.Sof_enable = DISABLE;
hpcd_USB_OTG_FS.Init.low_power_enable = DISABLE;
hpcd_USB_OTG_FS.Init.lpm_enable = DISABLE;
hpcd_USB_OTG_FS.Init.vbus_sensing_enable = DISABLE;
hpcd_USB_OTG_FS.Init.use_dedicated_ep1 = DISABLE;
if (HAL_PCD_Init(&hpcd_USB_OTG_FS) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN USB_OTG_FS_Init 2 */

/* USER CODE END USB_OTG_FS_Init 2 */

}

void HAL_PCD_MspInit(PCD_HandleTypeDef* pcdHandle)
{

GPIO_InitTypeDef GPIO_InitStruct = {0};
if(pcdHandle->Instance==USB_OTG_FS)
{
/* USER CODE BEGIN USB_OTG_FS_MspInit 0 */

/* USER CODE END USB_OTG_FS_MspInit 0 */

__HAL_RCC_GPIOA_CLK_ENABLE();
/**USB_OTG_FS GPIO Configuration
PA12 ------> USB_OTG_FS_DP
PA11 ------> USB_OTG_FS_DM
*/
GPIO_InitStruct.Pin = GPIO_PIN_12|GPIO_PIN_11;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF10_OTG_FS;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

/* USB_OTG_FS clock enable */
__HAL_RCC_USB_OTG_FS_CLK_ENABLE();

/* USB_OTG_FS interrupt Init */
HAL_NVIC_SetPriority(OTG_FS_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(OTG_FS_IRQn);
/* USER CODE BEGIN USB_OTG_FS_MspInit 1 */

/* USER CODE END USB_OTG_FS_MspInit 1 */
}
}

void HAL_PCD_MspDeInit(PCD_HandleTypeDef* pcdHandle)
{

if(pcdHandle->Instance==USB_OTG_FS)
{
/* USER CODE BEGIN USB_OTG_FS_MspDeInit 0 */

/* USER CODE END USB_OTG_FS_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_USB_OTG_FS_CLK_DISABLE();

/**USB_OTG_FS GPIO Configuration
PA12 ------> USB_OTG_FS_DP
PA11 ------> USB_OTG_FS_DM
*/
HAL_GPIO_DeInit(GPIOA, GPIO_PIN_12|GPIO_PIN_11);

/* USB_OTG_FS interrupt Deinit */
HAL_NVIC_DisableIRQ(OTG_FS_IRQn);
/* USER CODE BEGIN USB_OTG_FS_MspDeInit 1 */

/* USER CODE END USB_OTG_FS_MspDeInit 1 */
}
}

/* USER CODE BEGIN 1 */

/* USER CODE END 1 */
Loading