Skip to content

Commit dcd0f39

Browse files
authored
Merge pull request #2750 from hathach/fix-ch32v203-setup
rework fsdev driver, fix ch32v203 race condition and stability issue
2 parents a7d1888 + e7e6fe7 commit dcd0f39

File tree

15 files changed

+659
-654
lines changed

15 files changed

+659
-654
lines changed

.github/workflows/hil_test.yml

Lines changed: 7 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,17 @@ jobs:
4444
echo "BOARDS_LIST=$BOARDS_LIST" >> $GITHUB_ENV
4545
echo "BOARDS_LIST=$BOARDS_LIST" >> $GITHUB_OUTPUT
4646
47-
- name: Setup Toolchain
47+
- name: Setup arm-gcc toolchain
4848
uses: ./.github/actions/setup_toolchain
4949
with:
5050
toolchain: 'arm-gcc'
5151

52+
- name: Setup risv-gcc toolchain
53+
uses: ./.github/actions/setup_toolchain
54+
with:
55+
toolchain: 'riscv-gcc'
56+
toolchain_url: 'https://github.com/xpack-dev-tools/riscv-none-elf-gcc-xpack/releases/download/v13.2.0-2/xpack-riscv-none-elf-gcc-13.2.0-2-linux-x64.tar.gz'
57+
5258
- name: Get Dependencies
5359
uses: ./.github/actions/get_deps
5460
with:
@@ -65,56 +71,6 @@ jobs:
6571
cmake-build/cmake-build-*/*/*/*.elf
6672
cmake-build/cmake-build-*/*/*/*.bin
6773
68-
# ---------------------------------------
69-
# Build Espressif (skipped since CP210x cause USB bus issue)
70-
# cp210x ttyUSB0: usb_serial_generic_write_bulk_callback - nonzero urb status: -71
71-
# ---------------------------------------
72-
build-esp:
73-
if: false
74-
runs-on: ubuntu-latest
75-
outputs:
76-
BOARDS_LIST: ${{ steps.parse_hil_json.outputs.BOARDS_LIST }}
77-
steps:
78-
- name: Checkout TinyUSB
79-
uses: actions/checkout@v4
80-
81-
- name: Parse HIL json
82-
id: parse_hil_json
83-
run: |
84-
sudo apt install -y jq
85-
# Espressif boards
86-
BOARDS_LIST=$(jq -r '.boards[] | select(.flasher == "esptool") | "-b " + .name' ${{ env.HIL_JSON }} | tr '\n' ' ')
87-
echo "BOARDS_LIST=$BOARDS_LIST"
88-
echo "BOARDS_LIST=$BOARDS_LIST" >> $GITHUB_ENV
89-
echo "BOARDS_LIST=$BOARDS_LIST" >> $GITHUB_OUTPUT
90-
91-
- name: Setup ESP-IDF
92-
if: env.BOARDS_LIST != ''
93-
uses: ./.github/actions/setup_toolchain
94-
with:
95-
toolchain: 'esp-idf'
96-
toolchain_url: 'v5.1.1'
97-
98-
- name: Get Dependencies
99-
uses: ./.github/actions/get_deps
100-
with:
101-
arg: ${{ env.BOARDS_LIST }}
102-
103-
- name: Build Espressif
104-
if: env.BOARDS_LIST != ''
105-
run: docker run --rm -v $PWD:/project -w /project espressif/idf:v5.1.1 python3 tools/build.py $BOARDS_LIST
106-
107-
- name: Upload Artifacts for Hardware Testing
108-
uses: actions/upload-artifact@v4
109-
with:
110-
name: hil_rpi_esp
111-
path: |
112-
cmake-build/cmake-build-*/*/*/*.bin
113-
cmake-build/cmake-build-*/*/*/bootloader/bootloader.bin
114-
cmake-build/cmake-build-*/*/*/partition_table/partition-table.bin
115-
cmake-build/cmake-build-*/*/*/config.env
116-
cmake-build/cmake-build-*/*/*/flash_args
117-
11874
# ---------------------------------------
11975
# Hardware in the loop (HIL)
12076
# self-hosted running on an RPI. For attached hardware checkout test/hil/rpi.json
@@ -123,7 +79,6 @@ jobs:
12379
if: github.repository_owner == 'hathach'
12480
needs:
12581
- build
126-
#- build-esp
12782
runs-on: [self-hosted, ARM64, rpi, hardware-in-the-loop]
12883
env:
12984
BOARDS_LIST: "${{ needs.build-esp.outputs.BOARDS_LIST }} ${{ needs.build.outputs.BOARDS_LIST }}"

.idea/cmake.xml

Lines changed: 8 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

hw/bsp/family_support.cmake

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,11 @@ function(family_flash_jlink TARGET)
399399
set(JLINK_IF swd)
400400
endif ()
401401

402+
if (NOT DEFINED JLINK_OPTION)
403+
set(JLINK_OPTION "")
404+
endif ()
405+
separate_arguments(OPTION_LIST UNIX_COMMAND ${JLINK_OPTION})
406+
402407
file(GENERATE
403408
OUTPUT $<TARGET_FILE_DIR:${TARGET}>/${TARGET}.jlink
404409
CONTENT "halt
@@ -410,7 +415,8 @@ exit"
410415

411416
add_custom_target(${TARGET}-jlink
412417
DEPENDS ${TARGET}
413-
COMMAND ${JLINKEXE} -device ${JLINK_DEVICE} -if ${JLINK_IF} -JTAGConf -1,-1 -speed auto -CommandFile $<TARGET_FILE_DIR:${TARGET}>/${TARGET}.jlink
418+
COMMAND ${JLINKEXE} -device ${JLINK_DEVICE} ${OPTION_LIST} -if ${JLINK_IF} -JTAGConf -1,-1 -speed auto -CommandFile $<TARGET_FILE_DIR:${TARGET}>/${TARGET}.jlink
419+
VERBATIM
414420
)
415421
endfunction()
416422

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
set(MCU_VARIANT stm32f103xe)
2+
set(JLINK_DEVICE stm32f103ze)
3+
4+
string(TOUPPER ${MCU_VARIANT} MCU_VARIANT_UPPER)
5+
6+
set(LD_FILE_GNU ${ST_CMSIS}/Source/Templates/gcc/linker/${MCU_VARIANT_UPPER}_FLASH.ld)
7+
set(LD_FILE_IAR ${ST_CMSIS}/Source/Templates/iar/linker/${MCU_VARIANT}_flash.icf)
8+
9+
function(update_board TARGET)
10+
target_compile_definitions(${TARGET} PUBLIC
11+
STM32F103xE
12+
HSE_VALUE=8000000U
13+
)
14+
endfunction()
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2020, Ha Thach (tinyusb.org)
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*
24+
* This file is part of the TinyUSB stack.
25+
*/
26+
27+
#ifndef BOARD_H_
28+
#define BOARD_H_
29+
30+
#ifdef __cplusplus
31+
extern "C" {
32+
#endif
33+
34+
// LED
35+
#define LED_PORT GPIOF
36+
#define LED_PIN GPIO_PIN_6
37+
#define LED_STATE_ON 1
38+
39+
// Button
40+
#define BUTTON_PORT GPIOG
41+
#define BUTTON_PIN GPIO_PIN_8
42+
#define BUTTON_STATE_ACTIVE 0
43+
44+
// USB Connect
45+
#define USB_CONNECT_PORT GPIOG
46+
#define USB_CONNECT_PIN GPIO_PIN_11
47+
#define USB_CONNECT_STATE 0
48+
49+
// UART
50+
//#define UART_DEV USART1
51+
//#define UART_CLK_EN __HAL_RCC_USART1_CLK_ENABLE
52+
//#define UART_GPIO_PORT GPIOA
53+
//#define UART_GPIO_AF GPIO_AF1_USART1
54+
//#define UART_TX_PIN GPIO_PIN_9
55+
//#define UART_RX_PIN GPIO_PIN_10
56+
57+
//--------------------------------------------------------------------+
58+
// RCC Clock
59+
//--------------------------------------------------------------------+
60+
static inline void board_stm32f1_clock_init(void)
61+
{
62+
RCC_ClkInitTypeDef clkinitstruct = {0};
63+
RCC_OscInitTypeDef oscinitstruct = {0};
64+
RCC_PeriphCLKInitTypeDef rccperiphclkinit = {0};
65+
66+
/* Enable HSE Oscillator and activate PLL with HSE as source */
67+
oscinitstruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
68+
oscinitstruct.HSEState = RCC_HSE_ON;
69+
oscinitstruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
70+
oscinitstruct.PLL.PLLMUL = RCC_PLL_MUL9;
71+
oscinitstruct.PLL.PLLState = RCC_PLL_ON;
72+
oscinitstruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
73+
HAL_RCC_OscConfig(&oscinitstruct);
74+
75+
/* USB clock selection */
76+
rccperiphclkinit.PeriphClockSelection = RCC_PERIPHCLK_USB;
77+
rccperiphclkinit.UsbClockSelection = RCC_USBCLKSOURCE_PLL_DIV1_5;
78+
HAL_RCCEx_PeriphCLKConfig(&rccperiphclkinit);
79+
80+
/* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers */
81+
clkinitstruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
82+
clkinitstruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
83+
clkinitstruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
84+
clkinitstruct.APB1CLKDivider = RCC_HCLK_DIV2;
85+
clkinitstruct.APB2CLKDivider = RCC_HCLK_DIV1;
86+
HAL_RCC_ClockConfig(&clkinitstruct, FLASH_LATENCY_2);
87+
}
88+
89+
static inline void board_vbus_sense_init(void)
90+
{
91+
}
92+
93+
#ifdef __cplusplus
94+
}
95+
#endif
96+
97+
#endif /* BOARD_H_ */
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
MCU_VARIANT = stm32f103xe
2+
3+
CFLAGS += -DSTM32F103xE -DHSE_VALUE=8000000U
4+
5+
# Linker
6+
LD_FILE_GCC = ${ST_CMSIS}/Source/Templates/gcc/linker/STM32F103XE_FLASH.ld
7+
LD_FILE_IAR = ${ST_CMSIS}/Source/Templates/iar/linker/${MCU_VARIANT}_flash.icf
8+
9+
# For flash-jlink target
10+
JLINK_DEVICE = stm32f103ze
11+
12+
# flash target ROM bootloader
13+
flash: flash-jlink

hw/bsp/stm32f1/family.c

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,19 @@ void board_init(void) {
5757
__HAL_RCC_GPIOC_CLK_ENABLE();
5858
__HAL_RCC_GPIOD_CLK_ENABLE();
5959

60+
#ifdef __HAL_RCC_GPIOE_CLK_ENABLE
61+
__HAL_RCC_GPIOE_CLK_ENABLE();
62+
#endif
63+
64+
#ifdef __HAL_RCC_GPIOF_CLK_ENABLE
65+
__HAL_RCC_GPIOF_CLK_ENABLE();
66+
#endif
67+
68+
#ifdef __HAL_RCC_GPIOG_CLK_ENABLE
69+
__HAL_RCC_GPIOG_CLK_ENABLE();
70+
#endif
71+
72+
6073
#if CFG_TUSB_OS == OPT_OS_NONE
6174
// 1ms tick timer
6275
SysTick_Config(SystemCoreClock / 1000);
@@ -107,10 +120,18 @@ void board_init(void) {
107120
HAL_UART_Init(&UartHandle);
108121
#endif
109122

123+
#ifdef USB_CONNECT_PIN
124+
GPIO_InitStruct.Pin = USB_CONNECT_PIN;
125+
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
126+
GPIO_InitStruct.Pull = GPIO_NOPULL;
127+
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
128+
HAL_GPIO_Init(USB_CONNECT_PORT, &GPIO_InitStruct);
129+
#endif
130+
110131
// USB Pins
111132
// Configure USB DM and DP pins.
112133
GPIO_InitStruct.Pin = (GPIO_PIN_11 | GPIO_PIN_12);
113-
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
134+
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
114135
GPIO_InitStruct.Pull = GPIO_NOPULL;
115136
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
116137
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
@@ -119,6 +140,18 @@ void board_init(void) {
119140
__HAL_RCC_USB_CLK_ENABLE();
120141
}
121142

143+
#ifdef USB_CONNECT_PIN
144+
void dcd_disconnect(uint8_t rhport) {
145+
(void)rhport;
146+
HAL_GPIO_WritePin(USB_CONNECT_PORT, USB_CONNECT_PIN, 1-USB_CONNECT_STATE);
147+
}
148+
149+
void dcd_connect(uint8_t rhport) {
150+
(void)rhport;
151+
HAL_GPIO_WritePin(USB_CONNECT_PORT, USB_CONNECT_PIN, USB_CONNECT_STATE);
152+
}
153+
#endif
154+
122155
//--------------------------------------------------------------------+
123156
// Board porting API
124157
//--------------------------------------------------------------------+

hw/bsp/stm32f1/family.mk

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
ST_FAMILY = f1
2-
DEPS_SUBMODULES += lib/CMSIS_5 hw/mcu/st/cmsis_device_$(ST_FAMILY) hw/mcu/st/stm32$(ST_FAMILY)xx_hal_driver
2+
DEPS_SUBMODULES += lib/CMSIS_5 hw/mcu/st/cmsis_device_${ST_FAMILY} hw/mcu/st/stm32${ST_FAMILY}xx_hal_driver
33

4-
ST_CMSIS = hw/mcu/st/cmsis_device_$(ST_FAMILY)
5-
ST_HAL_DRIVER = hw/mcu/st/stm32$(ST_FAMILY)xx_hal_driver
4+
ST_CMSIS = hw/mcu/st/cmsis_device_${ST_FAMILY}
5+
ST_HAL_DRIVER = hw/mcu/st/stm32${ST_FAMILY}xx_hal_driver
66

7-
include $(TOP)/$(BOARD_PATH)/board.mk
7+
include ${TOP}/${BOARD_PATH}/board.mk
88
CPU_CORE ?= cortex-m3
99

1010
# --------------
@@ -29,23 +29,23 @@ LDFLAGS_GCC += \
2929
# ------------------------
3030
SRC_C += \
3131
src/portable/st/stm32_fsdev/dcd_stm32_fsdev.c \
32-
$(ST_CMSIS)/Source/Templates/system_stm32$(ST_FAMILY)xx.c \
33-
$(ST_HAL_DRIVER)/Src/stm32$(ST_FAMILY)xx_hal.c \
34-
$(ST_HAL_DRIVER)/Src/stm32$(ST_FAMILY)xx_hal_cortex.c \
35-
$(ST_HAL_DRIVER)/Src/stm32$(ST_FAMILY)xx_hal_rcc.c \
36-
$(ST_HAL_DRIVER)/Src/stm32$(ST_FAMILY)xx_hal_rcc_ex.c \
37-
$(ST_HAL_DRIVER)/Src/stm32$(ST_FAMILY)xx_hal_gpio.c \
38-
$(ST_HAL_DRIVER)/Src/stm32$(ST_FAMILY)xx_hal_uart.c
32+
${ST_CMSIS}/Source/Templates/system_stm32${ST_FAMILY}xx.c \
33+
${ST_HAL_DRIVER}/Src/stm32${ST_FAMILY}xx_hal.c \
34+
${ST_HAL_DRIVER}/Src/stm32${ST_FAMILY}xx_hal_cortex.c \
35+
${ST_HAL_DRIVER}/Src/stm32${ST_FAMILY}xx_hal_rcc.c \
36+
${ST_HAL_DRIVER}/Src/stm32${ST_FAMILY}xx_hal_rcc_ex.c \
37+
${ST_HAL_DRIVER}/Src/stm32${ST_FAMILY}xx_hal_gpio.c \
38+
${ST_HAL_DRIVER}/Src/stm32${ST_FAMILY}xx_hal_uart.c
3939

4040
INC += \
41-
$(TOP)/$(BOARD_PATH) \
42-
$(TOP)/lib/CMSIS_5/CMSIS/Core/Include \
43-
$(TOP)/$(ST_CMSIS)/Include \
44-
$(TOP)/$(ST_HAL_DRIVER)/Inc
41+
${TOP}/${BOARD_PATH} \
42+
${TOP}/lib/CMSIS_5/CMSIS/Core/Include \
43+
${TOP}/${ST_CMSIS}/Include \
44+
${TOP}/${ST_HAL_DRIVER}/Inc
4545

4646
# Startup
47-
SRC_S_GCC += $(ST_CMSIS)/Source/Templates/gcc/startup_$(MCU_VARIANT).s
48-
SRC_S_IAR += $(ST_CMSIS)/Source/Templates/iar/startup_$(MCU_VARIANT).s
47+
SRC_S_GCC += ${ST_CMSIS}/Source/Templates/gcc/startup_${MCU_VARIANT}.s
48+
SRC_S_IAR += ${ST_CMSIS}/Source/Templates/iar/startup_${MCU_VARIANT}.s
4949

5050
# flash target ROM bootloader: flash-dfu-util
5151
DFU_UTIL_OPTION = -a 0 --dfuse-address 0x08000000

src/device/usbd.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1201,7 +1201,6 @@ TU_ATTR_FAST_FUNC void dcd_event_handler(dcd_event_t const* event, bool in_isr)
12011201
break;
12021202

12031203
case DCD_EVENT_SETUP_RECEIVED:
1204-
// TU_ASSERT(event->setup_received.bRequest != 0,); // for catching issue with ch32v203 and windows with -O0/-Og
12051204
_usbd_queued_setup++;
12061205
send = true;
12071206
break;

0 commit comments

Comments
 (0)