Skip to content

Commit

Permalink
Merge pull request #2750 from hathach/fix-ch32v203-setup
Browse files Browse the repository at this point in the history
rework fsdev driver, fix ch32v203 race condition and stability issue
  • Loading branch information
hathach authored Aug 8, 2024
2 parents a7d1888 + e7e6fe7 commit dcd0f39
Show file tree
Hide file tree
Showing 15 changed files with 659 additions and 654 deletions.
59 changes: 7 additions & 52 deletions .github/workflows/hil_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,17 @@ jobs:
echo "BOARDS_LIST=$BOARDS_LIST" >> $GITHUB_ENV
echo "BOARDS_LIST=$BOARDS_LIST" >> $GITHUB_OUTPUT
- name: Setup Toolchain
- name: Setup arm-gcc toolchain
uses: ./.github/actions/setup_toolchain
with:
toolchain: 'arm-gcc'

- name: Setup risv-gcc toolchain
uses: ./.github/actions/setup_toolchain
with:
toolchain: 'riscv-gcc'
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'

- name: Get Dependencies
uses: ./.github/actions/get_deps
with:
Expand All @@ -65,56 +71,6 @@ jobs:
cmake-build/cmake-build-*/*/*/*.elf
cmake-build/cmake-build-*/*/*/*.bin
# ---------------------------------------
# Build Espressif (skipped since CP210x cause USB bus issue)
# cp210x ttyUSB0: usb_serial_generic_write_bulk_callback - nonzero urb status: -71
# ---------------------------------------
build-esp:
if: false
runs-on: ubuntu-latest
outputs:
BOARDS_LIST: ${{ steps.parse_hil_json.outputs.BOARDS_LIST }}
steps:
- name: Checkout TinyUSB
uses: actions/checkout@v4

- name: Parse HIL json
id: parse_hil_json
run: |
sudo apt install -y jq
# Espressif boards
BOARDS_LIST=$(jq -r '.boards[] | select(.flasher == "esptool") | "-b " + .name' ${{ env.HIL_JSON }} | tr '\n' ' ')
echo "BOARDS_LIST=$BOARDS_LIST"
echo "BOARDS_LIST=$BOARDS_LIST" >> $GITHUB_ENV
echo "BOARDS_LIST=$BOARDS_LIST" >> $GITHUB_OUTPUT
- name: Setup ESP-IDF
if: env.BOARDS_LIST != ''
uses: ./.github/actions/setup_toolchain
with:
toolchain: 'esp-idf'
toolchain_url: 'v5.1.1'

- name: Get Dependencies
uses: ./.github/actions/get_deps
with:
arg: ${{ env.BOARDS_LIST }}

- name: Build Espressif
if: env.BOARDS_LIST != ''
run: docker run --rm -v $PWD:/project -w /project espressif/idf:v5.1.1 python3 tools/build.py $BOARDS_LIST

- name: Upload Artifacts for Hardware Testing
uses: actions/upload-artifact@v4
with:
name: hil_rpi_esp
path: |
cmake-build/cmake-build-*/*/*/*.bin
cmake-build/cmake-build-*/*/*/bootloader/bootloader.bin
cmake-build/cmake-build-*/*/*/partition_table/partition-table.bin
cmake-build/cmake-build-*/*/*/config.env
cmake-build/cmake-build-*/*/*/flash_args
# ---------------------------------------
# Hardware in the loop (HIL)
# self-hosted running on an RPI. For attached hardware checkout test/hil/rpi.json
Expand All @@ -123,7 +79,6 @@ jobs:
if: github.repository_owner == 'hathach'
needs:
- build
#- build-esp
runs-on: [self-hosted, ARM64, rpi, hardware-in-the-loop]
env:
BOARDS_LIST: "${{ needs.build-esp.outputs.BOARDS_LIST }} ${{ needs.build.outputs.BOARDS_LIST }}"
Expand Down
15 changes: 8 additions & 7 deletions .idea/cmake.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion hw/bsp/family_support.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,11 @@ function(family_flash_jlink TARGET)
set(JLINK_IF swd)
endif ()

if (NOT DEFINED JLINK_OPTION)
set(JLINK_OPTION "")
endif ()
separate_arguments(OPTION_LIST UNIX_COMMAND ${JLINK_OPTION})

file(GENERATE
OUTPUT $<TARGET_FILE_DIR:${TARGET}>/${TARGET}.jlink
CONTENT "halt
Expand All @@ -410,7 +415,8 @@ exit"

add_custom_target(${TARGET}-jlink
DEPENDS ${TARGET}
COMMAND ${JLINKEXE} -device ${JLINK_DEVICE} -if ${JLINK_IF} -JTAGConf -1,-1 -speed auto -CommandFile $<TARGET_FILE_DIR:${TARGET}>/${TARGET}.jlink
COMMAND ${JLINKEXE} -device ${JLINK_DEVICE} ${OPTION_LIST} -if ${JLINK_IF} -JTAGConf -1,-1 -speed auto -CommandFile $<TARGET_FILE_DIR:${TARGET}>/${TARGET}.jlink
VERBATIM
)
endfunction()

Expand Down
14 changes: 14 additions & 0 deletions hw/bsp/stm32f1/boards/stm32f103ze_iar/board.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
set(MCU_VARIANT stm32f103xe)
set(JLINK_DEVICE stm32f103ze)

string(TOUPPER ${MCU_VARIANT} MCU_VARIANT_UPPER)

set(LD_FILE_GNU ${ST_CMSIS}/Source/Templates/gcc/linker/${MCU_VARIANT_UPPER}_FLASH.ld)
set(LD_FILE_IAR ${ST_CMSIS}/Source/Templates/iar/linker/${MCU_VARIANT}_flash.icf)

function(update_board TARGET)
target_compile_definitions(${TARGET} PUBLIC
STM32F103xE
HSE_VALUE=8000000U
)
endfunction()
97 changes: 97 additions & 0 deletions hw/bsp/stm32f1/boards/stm32f103ze_iar/board.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2020, Ha Thach (tinyusb.org)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* This file is part of the TinyUSB stack.
*/

#ifndef BOARD_H_
#define BOARD_H_

#ifdef __cplusplus
extern "C" {
#endif

// LED
#define LED_PORT GPIOF
#define LED_PIN GPIO_PIN_6
#define LED_STATE_ON 1

// Button
#define BUTTON_PORT GPIOG
#define BUTTON_PIN GPIO_PIN_8
#define BUTTON_STATE_ACTIVE 0

// USB Connect
#define USB_CONNECT_PORT GPIOG
#define USB_CONNECT_PIN GPIO_PIN_11
#define USB_CONNECT_STATE 0

// UART
//#define UART_DEV USART1
//#define UART_CLK_EN __HAL_RCC_USART1_CLK_ENABLE
//#define UART_GPIO_PORT GPIOA
//#define UART_GPIO_AF GPIO_AF1_USART1
//#define UART_TX_PIN GPIO_PIN_9
//#define UART_RX_PIN GPIO_PIN_10

//--------------------------------------------------------------------+
// RCC Clock
//--------------------------------------------------------------------+
static inline void board_stm32f1_clock_init(void)
{
RCC_ClkInitTypeDef clkinitstruct = {0};
RCC_OscInitTypeDef oscinitstruct = {0};
RCC_PeriphCLKInitTypeDef rccperiphclkinit = {0};

/* Enable HSE Oscillator and activate PLL with HSE as source */
oscinitstruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
oscinitstruct.HSEState = RCC_HSE_ON;
oscinitstruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
oscinitstruct.PLL.PLLMUL = RCC_PLL_MUL9;
oscinitstruct.PLL.PLLState = RCC_PLL_ON;
oscinitstruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
HAL_RCC_OscConfig(&oscinitstruct);

/* USB clock selection */
rccperiphclkinit.PeriphClockSelection = RCC_PERIPHCLK_USB;
rccperiphclkinit.UsbClockSelection = RCC_USBCLKSOURCE_PLL_DIV1_5;
HAL_RCCEx_PeriphCLKConfig(&rccperiphclkinit);

/* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers */
clkinitstruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
clkinitstruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
clkinitstruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
clkinitstruct.APB1CLKDivider = RCC_HCLK_DIV2;
clkinitstruct.APB2CLKDivider = RCC_HCLK_DIV1;
HAL_RCC_ClockConfig(&clkinitstruct, FLASH_LATENCY_2);
}

static inline void board_vbus_sense_init(void)
{
}

#ifdef __cplusplus
}
#endif

#endif /* BOARD_H_ */
13 changes: 13 additions & 0 deletions hw/bsp/stm32f1/boards/stm32f103ze_iar/board.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
MCU_VARIANT = stm32f103xe

CFLAGS += -DSTM32F103xE -DHSE_VALUE=8000000U

# Linker
LD_FILE_GCC = ${ST_CMSIS}/Source/Templates/gcc/linker/STM32F103XE_FLASH.ld
LD_FILE_IAR = ${ST_CMSIS}/Source/Templates/iar/linker/${MCU_VARIANT}_flash.icf

# For flash-jlink target
JLINK_DEVICE = stm32f103ze

# flash target ROM bootloader
flash: flash-jlink
35 changes: 34 additions & 1 deletion hw/bsp/stm32f1/family.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,19 @@ void board_init(void) {
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();

#ifdef __HAL_RCC_GPIOE_CLK_ENABLE
__HAL_RCC_GPIOE_CLK_ENABLE();
#endif

#ifdef __HAL_RCC_GPIOF_CLK_ENABLE
__HAL_RCC_GPIOF_CLK_ENABLE();
#endif

#ifdef __HAL_RCC_GPIOG_CLK_ENABLE
__HAL_RCC_GPIOG_CLK_ENABLE();
#endif


#if CFG_TUSB_OS == OPT_OS_NONE
// 1ms tick timer
SysTick_Config(SystemCoreClock / 1000);
Expand Down Expand Up @@ -107,10 +120,18 @@ void board_init(void) {
HAL_UART_Init(&UartHandle);
#endif

#ifdef USB_CONNECT_PIN
GPIO_InitStruct.Pin = USB_CONNECT_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(USB_CONNECT_PORT, &GPIO_InitStruct);
#endif

// USB Pins
// Configure USB DM and DP pins.
GPIO_InitStruct.Pin = (GPIO_PIN_11 | GPIO_PIN_12);
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
Expand All @@ -119,6 +140,18 @@ void board_init(void) {
__HAL_RCC_USB_CLK_ENABLE();
}

#ifdef USB_CONNECT_PIN
void dcd_disconnect(uint8_t rhport) {
(void)rhport;
HAL_GPIO_WritePin(USB_CONNECT_PORT, USB_CONNECT_PIN, 1-USB_CONNECT_STATE);
}

void dcd_connect(uint8_t rhport) {
(void)rhport;
HAL_GPIO_WritePin(USB_CONNECT_PORT, USB_CONNECT_PIN, USB_CONNECT_STATE);
}
#endif

//--------------------------------------------------------------------+
// Board porting API
//--------------------------------------------------------------------+
Expand Down
34 changes: 17 additions & 17 deletions hw/bsp/stm32f1/family.mk
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
ST_FAMILY = f1
DEPS_SUBMODULES += lib/CMSIS_5 hw/mcu/st/cmsis_device_$(ST_FAMILY) hw/mcu/st/stm32$(ST_FAMILY)xx_hal_driver
DEPS_SUBMODULES += lib/CMSIS_5 hw/mcu/st/cmsis_device_${ST_FAMILY} hw/mcu/st/stm32${ST_FAMILY}xx_hal_driver

ST_CMSIS = hw/mcu/st/cmsis_device_$(ST_FAMILY)
ST_HAL_DRIVER = hw/mcu/st/stm32$(ST_FAMILY)xx_hal_driver
ST_CMSIS = hw/mcu/st/cmsis_device_${ST_FAMILY}
ST_HAL_DRIVER = hw/mcu/st/stm32${ST_FAMILY}xx_hal_driver

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

# --------------
Expand All @@ -29,23 +29,23 @@ LDFLAGS_GCC += \
# ------------------------
SRC_C += \
src/portable/st/stm32_fsdev/dcd_stm32_fsdev.c \
$(ST_CMSIS)/Source/Templates/system_stm32$(ST_FAMILY)xx.c \
$(ST_HAL_DRIVER)/Src/stm32$(ST_FAMILY)xx_hal.c \
$(ST_HAL_DRIVER)/Src/stm32$(ST_FAMILY)xx_hal_cortex.c \
$(ST_HAL_DRIVER)/Src/stm32$(ST_FAMILY)xx_hal_rcc.c \
$(ST_HAL_DRIVER)/Src/stm32$(ST_FAMILY)xx_hal_rcc_ex.c \
$(ST_HAL_DRIVER)/Src/stm32$(ST_FAMILY)xx_hal_gpio.c \
$(ST_HAL_DRIVER)/Src/stm32$(ST_FAMILY)xx_hal_uart.c
${ST_CMSIS}/Source/Templates/system_stm32${ST_FAMILY}xx.c \
${ST_HAL_DRIVER}/Src/stm32${ST_FAMILY}xx_hal.c \
${ST_HAL_DRIVER}/Src/stm32${ST_FAMILY}xx_hal_cortex.c \
${ST_HAL_DRIVER}/Src/stm32${ST_FAMILY}xx_hal_rcc.c \
${ST_HAL_DRIVER}/Src/stm32${ST_FAMILY}xx_hal_rcc_ex.c \
${ST_HAL_DRIVER}/Src/stm32${ST_FAMILY}xx_hal_gpio.c \
${ST_HAL_DRIVER}/Src/stm32${ST_FAMILY}xx_hal_uart.c

INC += \
$(TOP)/$(BOARD_PATH) \
$(TOP)/lib/CMSIS_5/CMSIS/Core/Include \
$(TOP)/$(ST_CMSIS)/Include \
$(TOP)/$(ST_HAL_DRIVER)/Inc
${TOP}/${BOARD_PATH} \
${TOP}/lib/CMSIS_5/CMSIS/Core/Include \
${TOP}/${ST_CMSIS}/Include \
${TOP}/${ST_HAL_DRIVER}/Inc

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

# flash target ROM bootloader: flash-dfu-util
DFU_UTIL_OPTION = -a 0 --dfuse-address 0x08000000
1 change: 0 additions & 1 deletion src/device/usbd.c
Original file line number Diff line number Diff line change
Expand Up @@ -1201,7 +1201,6 @@ TU_ATTR_FAST_FUNC void dcd_event_handler(dcd_event_t const* event, bool in_isr)
break;

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

0 comments on commit dcd0f39

Please sign in to comment.