Skip to content

Commit

Permalink
Merge pull request #419 from wjklimek1/stm32h503_nucleo
Browse files Browse the repository at this point in the history
Support for STM32H503 on Nucleo board
  • Loading branch information
hathach authored Dec 6, 2024
2 parents 4858e90 + ee886a9 commit 46de518
Show file tree
Hide file tree
Showing 30 changed files with 2,463 additions and 10 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ jobs:
# Build ARM
# ---------------------------------------
arm:
#if: false
needs: set-matrix
uses: ./.github/workflows/build_util.yml
strategy:
Expand All @@ -67,6 +66,7 @@ jobs:
- 'mimxrt10xx'
- 'stm32f3'
- 'stm32f4'
- 'stm32h5'
- 'stm32l4'
with:
port: ${{ matrix.port }}
Expand All @@ -78,7 +78,6 @@ jobs:
# Build ESP
# ---------------------------------------
esp:
#if: false
needs: set-matrix
uses: ./.github/workflows/build_util.yml
secrets: inherit
Expand Down Expand Up @@ -121,6 +120,7 @@ jobs:
# Build ARM with CMake
# ---------------------------------------
arm-cmake:
if: false
runs-on: ubuntu-latest
strategy:
fail-fast: false
Expand Down
8 changes: 2 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ Supported features are
Not all features are implemented for all MCUs, following is supported MCUs and its feature

| MCU | MSC | Double Reset | Self-update | Write Protection | Neopixel | TFT |
| :---------- | :--: | :----------: | :---------: | :--------------: | :------: | :--: |
|:------------| :--: | :----------: |:-----------:| :--------------: |:--------:| :--: |
| ESP32 S2/S3 || Need RC || |||
| K32L2 ||| | | | |
| LPC55 ||| | || |
| iMXRT |||| || |
| STM32F3 |||||| |
| STM32F4 |||||| |
| STM32H5 ||||| | |

## Build and Flash

Expand Down Expand Up @@ -108,12 +109,7 @@ By default log message is printed via on-board UART which is slow and take lots
- Cons: requires jlink as the debugger.
- Pros: work with most if not all MCUs
- Software viewer is JLink RTT Viewer/Client/Logger which is bundled with JLink driver package.
- `LOGGER=swo`: Use dedicated SWO pin of ARM Cortex SWD debug header.
- Cons: only work with ARM Cortex MCUs minus M0
- Pros: should be compatible with more debugger that support SWO.
- Software viewer should be provided along with your debugger driver.

```
$ make BOARD=feather_stm32f405_express LOG=2 LOGGER=rtt all
$ make BOARD=feather_stm32f405_express LOG=2 LOGGER=swo all
```
90 changes: 90 additions & 0 deletions apps/blinky/blinky.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2020 Ha Thach (tinyusb.org) for Adafruit Industries
*
* 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.
*/

#include <stdlib.h>
#include <stdio.h>

#include "board_api.h"

// Blinky app to test tinyuf2 bootloader

//--------------------------------------------------------------------+
// MACRO TYPEDEF CONSTANT ENUM DECLARATION
//--------------------------------------------------------------------+
uint8_t const RGB_WRITING[] = { 0xcc, 0x66, 0x00 };
uint8_t const RGB_OFF[] = { 0x00, 0x00, 0x00 };
static volatile uint32_t _timer_count = 0;

int main(void) {
TUF2_LOG1_LOCATION();
board_init();
TUF2_LOG1_LOCATION();
board_timer_start(1);

while (1) {
// nothing to do
}
}

void board_timer_handler(void)
{
_timer_count++;

if ((_timer_count & 0xfful) == 0) {
// Fast toggle with both LED and RGB
static bool is_on = false;
is_on = !is_on;

// fast blink LED if available
board_led_write(is_on ? 0xff : 0x000);

// blink RGB if available
board_rgb_write(is_on ? RGB_WRITING : RGB_OFF);
}
}

//--------------------------------------------------------------------+
// Logger newlib retarget
//--------------------------------------------------------------------+

// Enable only with LOG is enabled (Note: ESP32-S2 has built-in support already)
#if TUF2_LOG // && (CFG_TUSB_MCU != OPT_MCU_ESP32S2)

#if defined(LOGGER_RTT)
#include "SEGGER_RTT.h"
#endif

__attribute__ ((used)) int _write (int fhdl, const void *buf, size_t count)
{
(void) fhdl;

#if defined(LOGGER_RTT)
SEGGER_RTT_Write(0, (char*) buf, (int) count);
return count;
#else
return board_uart_write(buf, count);
#endif
}

#endif
13 changes: 13 additions & 0 deletions ports/family_support.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ function(family_add_uf2 TARGET FAMILY_ID)
set(BIN_FILE $<TARGET_FILE_DIR:${TARGET}>/${TARGET}.${BIN_EXT})

add_custom_command(TARGET ${TARGET} POST_BUILD
COMMAND echo ${Python_EXECUTABLE} ${UF2CONV_PY} -f ${FAMILY_ID} ${ADDR_OPT} -c -o $<TARGET_FILE_DIR:${TARGET}>/${TARGET}.uf2 ${BIN_FILE}
COMMAND ${Python_EXECUTABLE} ${UF2CONV_PY} -f ${FAMILY_ID} ${ADDR_OPT} -c -o $<TARGET_FILE_DIR:${TARGET}>/${TARGET}.uf2 ${BIN_FILE}
VERBATIM)
endfunction()
Expand Down Expand Up @@ -264,6 +265,7 @@ function(family_flash_jlink TARGET)
set(BIN_FILE $<TARGET_FILE:${TARGET}>)
endif ()

# flash with jlink
file(GENERATE
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${TARGET}.jlink
CONTENT "halt
Expand All @@ -277,6 +279,17 @@ exit"
DEPENDS ${TARGET}
COMMAND ${JLINKEXE} -device ${JLINK_DEVICE} -if ${JLINK_IF} -JTAGConf -1,-1 -speed auto -CommandFile ${CMAKE_CURRENT_BINARY_DIR}/${TARGET}.jlink
)

# erase with jlink
file(GENERATE
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${TARGET}-erase.jlink
CONTENT "halt
erase
exit
")
add_custom_target(${TARGET}-erase-jlink
COMMAND ${JLINKEXE} -device ${JLINK_DEVICE} -if ${JLINK_IF} -JTAGConf -1,-1 -speed auto -CommandFile ${CMAKE_CURRENT_BINARY_DIR}/${TARGET}-erase.jlink
)
endfunction()


Expand Down
5 changes: 5 additions & 0 deletions ports/rules.mk
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,8 @@ OPENOCD_WCH ?= /home/${USER}/app/riscv-openocd-wch/src/openocd
OPENOCD_WCH_OPTION ?=
flash-openocd-wch: $(BUILD)/$(OUTNAME).elf
$(OPENOCD_WCH) $(OPENOCD_WCH_OPTION) -c init -c halt -c "flash write_image $<" -c reset -c exit

#-------------------- Flash with uf2 -----------------
UF2CONV_PY = $(TOP)/lib/uf2/utils/uf2conv.py
flash-uf2: $(BUILD)/$(OUTNAME).uf2
python ${UF2CONV_PY} -f ${UF2_FAMILY_ID} --deploy $^
28 changes: 28 additions & 0 deletions ports/stm32h5/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
cmake_minimum_required(VERSION 3.17)

include(${CMAKE_CURRENT_LIST_DIR}/../family_support.cmake)

project(tinyuf2 C ASM)
set(CMAKE_EXECUTABLE_SUFFIX .elf)

#------------------------------------
# TinyUF2
#------------------------------------
add_executable(tinyuf2
board_flash.c
boards.c
${TOP}/lib/tinyusb/src/portable/st/stm32_fsdev/dcd_stm32_fsdev.c
)
target_link_options(tinyuf2 PUBLIC
"LINKER:--script=${CMAKE_CURRENT_LIST_DIR}/linker/stm32h5_boot.ld"
)

family_configure_tinyuf2(tinyuf2 OPT_MCU_STM32H5)
family_flash_jlink(tinyuf2)
family_flash_stlink(tinyuf2)

#------------------------------------
# Application (e.g self update)
#------------------------------------
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/apps/self_update)
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/apps/blinky)
21 changes: 21 additions & 0 deletions ports/stm32h5/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# List of git submodules that is included as part of the UF2 version
GIT_SUBMODULES = tinyusb

include ../make.mk
include port.mk
include ../rules.mk

#------------------------------------------
# Self-update
#------------------------------------------

# directory containing Makefile for building update app
SELF_DIR = apps/self_update

$(SELF_DIR)/bootloader_bin.c: $(BUILD)/$(OUTNAME).bin
$(PYTHON3) $(TOP)/lib/uf2/utils/uf2conv.py --carray $^ -o $@

# remove bootloader_bin.c at the end to force re-generate each time
self-update: $(SELF_DIR)/bootloader_bin.c
make -C $(SELF_DIR) BOARD=$(BOARD) LOG=$(LOG) LOGGER=$(LOGGER) self-update
@rm -f $^
27 changes: 27 additions & 0 deletions ports/stm32h5/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# TinyUF2 for STM32H5

TinyUF2 occupies around 16Kb + 1KB for CF2 in flash. Since H5 has uniform sector size of 8KB, ideally we would have bootloader size set to 24KB. However, due to the fact that only H503 can protect/secure individual sector, the rest of H5 family (H52x, H56x) can only secure flash in 4-sector (group) unit. Therefore application should start at
- H503: `0x08006000`, boot size is 24KB
- H52x, H56x, H57x: `0x08008000`, boot size is 32KB
To create a UF2 image from a .bin file, either use family option `STM32H5` or its magic number as follows:

From hex

```
uf2conv.py -c -f 0x4e8f1c5d firmware.hex
uf2conv.py -c -f STM32H5 firmware.hex
```

From bin for H503

```
uf2conv.py -c -b 0x08006000 -f STM32H5 firmware.bin
uf2conv.py -c -b 0x08006000 -f 0x4e8f1c5d firmware.bin
```

From bin for H52x, H56x, H57x

```
uf2conv.py -c -b 0x08008000 -f STM32H5 firmware.bin
uf2conv.py -c -b 0x08008000 -f 0x4e8f1c5d firmware.bin
```
24 changes: 24 additions & 0 deletions ports/stm32h5/apps/blinky/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#------------------------------------
# This file is meant to be include by add_subdirectory() in the root CMakeLists.txt
#------------------------------------

# self_update target
add_executable(blinky
${TOP}/apps/blinky/blinky.c
${CMAKE_CURRENT_LIST_DIR}/../../boards.c
${CMAKE_CURRENT_LIST_DIR}/../../board_flash.c
)
target_include_directories(blinky PUBLIC
${TOP}/src
)
target_compile_definitions(blinky PUBLIC
BUILD_NO_TINYUSB
BUILD_APPLICATION
)
target_link_options(blinky PUBLIC
"LINKER:--script=${CMAKE_CURRENT_LIST_DIR}/../../linker/stm32h5_app.ld"
)

family_configure_common(blinky)
family_add_uf2(blinky ${UF2_FAMILY_ID})
family_flash_uf2(blinky ${UF2_FAMILY_ID})
19 changes: 19 additions & 0 deletions ports/stm32h5/apps/blinky/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
PORT = stm32h5
OUTNAME = blinky-$(BOARD)

BUILD_APPLICATION = 1
BUILD_NO_TINYUSB = 1

include ../../../make.mk
include ../../port.mk

SRC_C += \
apps/blinky/blinky.c \

include ../../../rules.mk

uf2: $(BUILD)/$(OUTNAME).uf2

$(BUILD)/$(OUTNAME).uf2: $(BUILD)/$(OUTNAME).hex
@echo CREATE $@
$(PYTHON3) $(TOP)/lib/uf2/utils/uf2conv.py -f $(UF2_FAMILY_ID) -c -o $@ $^
2 changes: 2 additions & 0 deletions ports/stm32h5/apps/erase_firmware/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
all:
@echo "not implemented yet"
34 changes: 34 additions & 0 deletions ports/stm32h5/apps/self_update/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#------------------------------------
# Self Update
# This file is meant to be include by add_subdirectory() in the root CMakeLists.txt
#------------------------------------

# Generate bootloader_bin.c
add_custom_command(OUTPUT bootloader_bin.c
COMMAND ${Python_EXECUTABLE} ${UF2CONV_PY} --carray -o bootloader_bin.c $<TARGET_FILE_DIR:tinyuf2>/tinyuf2.bin
DEPENDS tinyuf2
)

# self_update target
add_executable(self_update
${TOP}/apps/self_update/self_update.c
${CMAKE_CURRENT_LIST_DIR}/../../boards.c
${CMAKE_CURRENT_LIST_DIR}/../../board_flash.c
${CMAKE_CURRENT_BINARY_DIR}/bootloader_bin.c
)

target_include_directories(self_update PUBLIC
${TOP}/src
)
target_compile_definitions(self_update PUBLIC
TINYUF2_SELF_UPDATE
BUILD_NO_TINYUSB
BUILD_APPLICATION
)
target_link_options(self_update PUBLIC
"LINKER:--script=${CMAKE_CURRENT_LIST_DIR}/../../linker/stm32h5_app.ld"
)

family_configure_common(self_update)
family_add_uf2(self_update ${UF2_FAMILY_ID})
family_flash_uf2(self_update ${UF2_FAMILY_ID})
25 changes: 25 additions & 0 deletions ports/stm32h5/apps/self_update/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
PORT = stm32h5
OUTNAME = update-tinyuf2-$(BOARD)

# skip bootloader src
BUILD_APPLICATION = 1

# skip tinyusb src
BUILD_NO_TINYUSB = 1

CFLAGS += -DTINYUF2_SELF_UPDATE -Wno-error=stringop-overread

include ../../../make.mk
include ../../port.mk

SRC_C += \
apps/self_update/self_update.c \
$(CURRENT_PATH)/bootloader_bin.c

include ../../../rules.mk

self-update: $(BUILD)/$(OUTNAME).uf2

$(BUILD)/$(OUTNAME).uf2: $(BUILD)/$(OUTNAME).hex
@echo CREATE $@
$(PYTHON3) $(TOP)/lib/uf2/utils/uf2conv.py -f $(UF2_FAMILY_ID) -c -o $@ $^
Loading

0 comments on commit 46de518

Please sign in to comment.