From 7de998fbf3c5f3d52f493237f9d0d71d7a16dcd9 Mon Sep 17 00:00:00 2001 From: David Garske Date: Tue, 1 Dec 2026 12:28:23 -0800 Subject: [PATCH] Add NUCLEO-H755ZI-Q support to the STM32H7 target: SMPS supply, 400MHz clock and wolfCrypt test app hooks --- .github/workflows/test-configs.yml | 6 +++ config/examples/stm32h755.config | 27 +++++++++++ docs/Targets.md | 15 +++++++ hal/stm32h7.c | 27 ++++++++++- hal/stm32h7.h | 5 +++ test-app/ARM-stm32h7.ld | 2 + test-app/Makefile | 3 ++ test-app/app_stm32h7.c | 56 +++++++++++++++++++++-- test-app/wolfcrypt_support.c | 72 ++++++++++++++++++++++++++++++ 9 files changed, 208 insertions(+), 5 deletions(-) create mode 100644 config/examples/stm32h755.config diff --git a/.github/workflows/test-configs.yml b/.github/workflows/test-configs.yml index aef24ad2d9..b013f2a3fe 100644 --- a/.github/workflows/test-configs.yml +++ b/.github/workflows/test-configs.yml @@ -1316,6 +1316,12 @@ jobs: arch: arm config-file: ./config/examples/stm32h7.config + stm32h755_test: + uses: ./.github/workflows/test-build.yml + with: + arch: arm + config-file: ./config/examples/stm32h755.config + stm32h7_qspi_test: uses: ./.github/workflows/test-build.yml with: diff --git a/config/examples/stm32h755.config b/config/examples/stm32h755.config new file mode 100644 index 0000000000..a0b173eda6 --- /dev/null +++ b/config/examples/stm32h755.config @@ -0,0 +1,27 @@ +ARCH?=ARM +TARGET?=stm32h7 +SIGN?=ECC256 +HASH?=SHA256 +DEBUG?=0 +DEBUG_UART?=0 +VTOR?=1 +NO_ASM?=0 +EXT_FLASH?=0 +SPI_FLASH?=0 +QSPI_FLASH?=0 +OCTOSPI_FLASH?=0 +ALLOW_DOWNGRADE?=0 +NVM_FLASH_WRITEONCE?=0 +WOLFBOOT_VERSION?=1 +V?=0 +SPMATH?=1 +RAM_CODE?=0 +DUALBANK_SWAP?=0 +WOLFBOOT_PARTITION_SIZE?=0xE0000 +WOLFBOOT_SECTOR_SIZE?=0x20000 +WOLFBOOT_PARTITION_BOOT_ADDRESS?=0x8020000 +WOLFBOOT_PARTITION_UPDATE_ADDRESS?=0x8100000 +WOLFBOOT_PARTITION_SWAP_ADDRESS?=0x81E0000 + +# NUCLEO-H755ZI-Q: core supplied by the SMPS alone (no LDO) +CFLAGS_EXTRA+=-DPWR_SUPPLY_CONFIG=PWR_CR3_SDEN -DSTM32H7_SMPS_ONLY diff --git a/docs/Targets.md b/docs/Targets.md index e0f51d7ba3..ac1bba0471 100644 --- a/docs/Targets.md +++ b/docs/Targets.md @@ -1805,6 +1805,21 @@ make TARGET=stm32h7 SIGN=ECC256 The STM32H7 also supports using the QSPI for external flash. To enable use `QSPI_FLASH=1` in your configuration. The pins are defined in `hal/spi/spi_drv_stm32.h`. A built-in alternate pin configuration can be used with `QSPI_ALT_CONFIGURATION`. The flash and QSPI parameters are defined in `src/qspi_flash.c` and can be overridden at build time. +### STM32H7 with SMPS supply (NUCLEO-H755ZI-Q) + +Use `cp ./config/examples/stm32h755.config .config`. It is the `stm32h7.config` layout plus two defines: + +- `PWR_SUPPLY_CONFIG=PWR_CR3_SDEN` selects the supply written to `PWR_CR3`. The selection can be written once per power-on reset, so it is only written while the register still holds its reset value. Without the define the HAL enables the LDO, which suits boards such as the NUCLEO-H753ZI. +- `STM32H7_SMPS_ONLY` keeps the core at VOS1 and sets PLL1 for 400MHz from the 8MHz HSE bypass clock. VOS0 and 480MHz need the LDO. + +On dual-core parts the HAL runs on the Cortex-M7. The Cortex-M4 boots from `0x08100000` by default, which is the update partition in this layout. Disable it or move its boot address with the option bytes: + +``` +STM32_Programmer_CLI -c port=swd mode=UR -ob BCM4=0 +``` + +The test application runs the wolfCrypt test or benchmark after the version banner when built with `WOLFCRYPT_TEST=1` or `WOLFCRYPT_BENCHMARK=1`. + ### STM32H7 Programming ST-Link Flash Tools: diff --git a/hal/stm32h7.c b/hal/stm32h7.c index 323fa6366f..3df548a1dc 100644 --- a/hal/stm32h7.c +++ b/hal/stm32h7.c @@ -403,20 +403,35 @@ static void clock_pll_on(int powersave) uint32_t plln, pllm, pllq, pllp, pllr, hpre, d1cpre, d1ppre; uint32_t d2ppre1, d2ppre2, d3ppre, flash_waitstates; +#ifdef PWR_SUPPLY_CONFIG + /* The supply selection can be written once per power-on reset */ + if ((PWR_CR3 & PWR_CR3_SUPPLY_MASK) == PWR_CR3_SUPPLY_UNSET) { + PWR_CR3 = (PWR_CR3 & ~((uint32_t)PWR_CR3_SUPPLY_MASK)) | + (PWR_SUPPLY_CONFIG); + } +#else PWR_CR3 |= PWR_CR3_LDOEN; +#endif while ((PWR_CSR1 & PWR_CSR1_ACTVOSRDY) == 0) {}; PWR_D3CR |= (PWR_D3CR_VOS_SCALE_1 << PWR_D3CR_VOS_SHIFT); /* Delay after setting the voltage scaling */ reg32 = PWR_D3CR; +#ifndef STM32H7_SMPS_ONLY SYSCFG_PWRCR |= SYSCFG_PWRCR_ODEN; /* Delay after setting the voltage scaling */ reg32 = PWR_D3CR; +#endif while ((PWR_D3CR & PWR_D3CR_VOSRDY) == 0) {}; - /* Select clock parameters (CPU Speed = 480MHz) */ + /* Select clock parameters (CPU Speed = 480MHz, 400MHz if SMPS only) */ pllm = 1; +#ifdef STM32H7_SMPS_ONLY + /* VOS0 needs the LDO, which leaves VOS1 and 400MHz */ + plln = 100; +#else plln = 120; +#endif pllp = 2; pllq = 20; pllr = 2; @@ -491,12 +506,18 @@ static void clock_pll_on(int powersave) /* PLL Clock source selection + DIVM1 */ reg32 = RCC_PLLCKSELR; +#ifdef STM32H7_SMPS_ONLY + reg32 &= ~((0x3F << 4) | 0x3); +#endif reg32 |= RCC_PLLCKSELR_PLLSRC_HSE; reg32 |= ((pllm) << 4); RCC_PLLCKSELR = reg32; DMB(); reg32 = RCC_PLL1DIVR; +#ifdef STM32H7_SMPS_ONLY + reg32 &= ~((0x7F << 24) | (0x7F << 16) | (0x7F << 9) | 0x1FF); +#endif reg32 |= (plln -1); reg32 |= ((pllp - 1) << 9); reg32 |= ((pllq - 1) << 16); @@ -504,7 +525,11 @@ static void clock_pll_on(int powersave) RCC_PLL1DIVR = reg32; DMB(); +#ifdef STM32H7_SMPS_ONLY + RCC_PLLCFGR |= (RCC_PLLCFGR_PLL1RGE_8_16 << RCC_PLLCFGR_PLL1RGE_SHIFT); +#else RCC_PLLCFGR |= (RCC_PLLCFGR_PLL1RGE_2_4 << RCC_PLLCFGR_PLL1RGE_SHIFT); +#endif RCC_PLLCFGR |= RCC_PLLCFGR_DIVP1EN; RCC_PLLCFGR |= RCC_PLLCFGR_DIVQ1EN; RCC_PLLCFGR |= RCC_PLLCFGR_DIVR1EN; diff --git a/hal/stm32h7.h b/hal/stm32h7.h index 3a373def38..1b95fc278e 100644 --- a/hal/stm32h7.h +++ b/hal/stm32h7.h @@ -72,6 +72,7 @@ #define RCC_PLLCFGR_PLL1VCOSEL (1 << 1) #define RCC_PLLCFGR_PLL1RGE_2_4 0x1 +#define RCC_PLLCFGR_PLL1RGE_8_16 0x3 #define RCC_PLLCFGR_PLL1RGE_SHIFT 0x2 #define RCC_PLLCKSELR_DIVM1 (1 << 4) @@ -106,6 +107,10 @@ #define PWR_CR3 (*(volatile uint32_t *)(PWR_BASE + 0x0C)) /* RM0433 - 5.8.4 */ #define PWR_CR3_SCUEN (1 << 2) #define PWR_CR3_LDOEN (1 << 1) +#define PWR_CR3_SDEN (1 << 2) /* RM0399 name for bit 2 on parts with SMPS */ +#define PWR_CR3_BYPASS (1 << 0) +#define PWR_CR3_SUPPLY_MASK (PWR_CR3_SDEN | PWR_CR3_LDOEN | PWR_CR3_BYPASS) +#define PWR_CR3_SUPPLY_UNSET (PWR_CR3_SDEN | PWR_CR3_LDOEN) #define PWR_D3CR (*(volatile uint32_t *)(PWR_BASE + 0x18)) /* RM0433 - 5.8.6 */ #define PWR_D3CR_VOSRDY (1 << 13) #define PWR_D3CR_VOS_SHIFT (14) diff --git a/test-app/ARM-stm32h7.ld b/test-app/ARM-stm32h7.ld index 4b44d6bd81..2074adddf0 100644 --- a/test-app/ARM-stm32h7.ld +++ b/test-app/ARM-stm32h7.ld @@ -1,3 +1,5 @@ +_Min_Heap_Size = 0x00002000; /* newlib stdio, wolfCrypt test and benchmark */ + MEMORY { FLASH (rx) : ORIGIN = @WOLFBOOT_TEST_APP_ADDRESS@, LENGTH = @WOLFBOOT_TEST_APP_SIZE@ diff --git a/test-app/Makefile b/test-app/Makefile index 0616529149..623d150edb 100644 --- a/test-app/Makefile +++ b/test-app/Makefile @@ -684,6 +684,9 @@ endif ifeq ($(TARGET),stm32h7) LSCRIPT_TEMPLATE=ARM-stm32h7.ld + ifeq ($(WOLFCRYPT_SUPPORT),1) + CFLAGS+=-DAPP_HAS_SYSTICK + endif endif ifeq ($(TARGET),stm32c0) diff --git a/test-app/app_stm32h7.c b/test-app/app_stm32h7.c index 6e64f68c18..997c22ad3c 100644 --- a/test-app/app_stm32h7.c +++ b/test-app/app_stm32h7.c @@ -40,6 +40,22 @@ #include "wolfboot/wolfboot.h" #include "target.h" +#ifdef WOLFCRYPT_TEST +#include +#include +int wolfcrypt_test(void *args); +#endif + +#ifdef WOLFCRYPT_BENCHMARK +#include +#include +int benchmark_test(void *args); +#endif + +#if defined(WOLFCRYPT_TEST) || defined(WOLFCRYPT_BENCHMARK) +#include +#endif + #define SET_BIT(REG, BIT) ((REG) |= (BIT)) #define CLEAR_BIT(REG, BIT) ((REG) &= ~(BIT)) #define READ_BIT(REG, BIT) ((REG) & (BIT)) @@ -65,8 +81,12 @@ /* GENERAL DEFINITIONS */ /* ====================================================================== */ +#ifndef FIRMWARE_A #define FIRMWARE_A 1 +#endif +#ifndef FIRMWARE_B #define FIRMWARE_B 0 +#endif #define LED_OFF 0 #define LED_INIT 1 @@ -337,7 +357,7 @@ int uart_setup(uint32_t bitrate) return -1; } -static void uart_write(const char c) +static void uart_putc(const char c) { /* USART transmit data register(TDR), bit 0-8 contains the data character * to be transmitted. @@ -359,10 +379,23 @@ void uart_print(const char *s) { int i = 0; while (s[i]) { - uart_write(s[i++]); + uart_putc(s[i++]); } } +#if (defined(WOLFCRYPT_TEST) || defined(WOLFCRYPT_BENCHMARK)) && \ + !defined(DEBUG_UART) +void uart_write(const char *buf, unsigned int sz) +{ + unsigned int i; + for (i = 0; i < sz; i++) { + if (buf[i] == '\n') + uart_putc('\r'); + uart_putc(buf[i]); + } +} +#endif + #ifdef WOLFBOOT_TEST_FILLER #define FILLER_SIZE (64 * 1024) static volatile uint8_t filler_data[FILLER_SIZE] = { 0x01, 0x02, 0x03 }; @@ -409,7 +442,7 @@ void main(void) uart_print("\tFIRMWARE VERSION: "); if (firmware_version <= 9) { - uart_write(firmware_version + '0'); + uart_putc(firmware_version + '0'); uart_print(" \n\n\r"); } else { @@ -424,7 +457,7 @@ void main(void) char_temp = READ_REG(UPDATE_CHARACTER_1); uart_print("[DATA] Content of 0x08060000 (1 byte): "); - uart_write(char_temp); + uart_putc(char_temp); uart_print("\n\r"); if ((char_temp == 'W') && FIRMWARE_A) { @@ -435,6 +468,21 @@ void main(void) uart_print("[INFO] User application B is running and update cannot be triggered\n\r"); } +#if defined(WOLFCRYPT_TEST) || defined(WOLFCRYPT_BENCHMARK) + wolfCrypt_Init(); +#ifdef WOLFCRYPT_TEST + uart_print("\n\rRunning wolfCrypt tests...\n\r"); + wolfcrypt_test(NULL); + uart_print("Tests complete.\n\r"); +#endif +#ifdef WOLFCRYPT_BENCHMARK + uart_print("\n\rRunning wolfCrypt benchmarks...\n\r"); + benchmark_test(NULL); + uart_print("Benchmarks complete.\n\r"); +#endif + wolfCrypt_Cleanup(); +#endif + /* busy wait */ while (1) ; diff --git a/test-app/wolfcrypt_support.c b/test-app/wolfcrypt_support.c index 143b71d2e6..435a512f0e 100644 --- a/test-app/wolfcrypt_support.c +++ b/test-app/wolfcrypt_support.c @@ -194,6 +194,69 @@ } #elif defined(TARGET_ti_am64x_r5) extern uint64_t ClockP_getTimeUsec(void); +#elif defined(TARGET_stm32h7) + /* 1ms SysTick. The core clock is read back from the PLL1 registers + * rather than assumed. */ + #define H7_SYST_CSR (*(volatile uint32_t *)(0xE000E010)) + #define H7_SYST_RVR (*(volatile uint32_t *)(0xE000E014)) + #define H7_SYST_CVR (*(volatile uint32_t *)(0xE000E018)) + #define H7_RCC_CR (*(volatile uint32_t *)(0x58024400)) + #define H7_RCC_CFGR (*(volatile uint32_t *)(0x58024410)) + #define H7_RCC_D1CFGR (*(volatile uint32_t *)(0x58024418)) + #define H7_RCC_PLLCKSELR (*(volatile uint32_t *)(0x58024428)) + #define H7_RCC_PLL1DIVR (*(volatile uint32_t *)(0x58024430)) + #ifndef STM32H7_HSE_HZ + #define STM32H7_HSE_HZ 8000000ULL + #endif + #define STM32H7_HSI_HZ 64000000ULL + #define STM32H7_CSI_HZ 4000000ULL + + static volatile uint32_t h7_time_ms = 0; + static uint32_t h7_start_ms = 0; + static int h7_tick_on = 0; + + void isr_systick(void) + { + h7_time_ms++; + } + + static uint64_t h7_get_hz(void) + { + uint64_t hz = STM32H7_HSI_HZ >> ((H7_RCC_CR >> 3) & 0x3); + uint64_t src; + uint32_t divm, d1cpre; + + if (((H7_RCC_CFGR >> 3) & 0x7) == 0x3) { + switch (H7_RCC_PLLCKSELR & 0x3) { + case 1: src = STM32H7_CSI_HZ; break; + case 2: src = STM32H7_HSE_HZ; break; + default: src = hz; break; + } + divm = (H7_RCC_PLLCKSELR >> 4) & 0x3F; + if (divm != 0) { + hz = (src / divm) * + ((H7_RCC_PLL1DIVR & 0x1FF) + 1) / + (((H7_RCC_PLL1DIVR >> 9) & 0x7F) + 1); + } + } + d1cpre = (H7_RCC_D1CFGR >> 8) & 0xF; + if (d1cpre >= 12) + hz >>= (d1cpre - 6); + else if (d1cpre >= 8) + hz >>= (d1cpre - 7); + return hz; + } + + static uint32_t h7_get_ms(void) + { + if (!h7_tick_on) { + H7_SYST_RVR = (uint32_t)(h7_get_hz() / 1000) - 1; + H7_SYST_CVR = 0; + H7_SYST_CSR = 0x7; /* processor clock, interrupt, enable */ + h7_tick_on = 1; + } + return h7_time_ms; + } #else /* Simple tick counter fallback */ static volatile unsigned int tick_counter = 0; @@ -252,6 +315,10 @@ unsigned long my_time(unsigned long* timer) unsigned long t = (unsigned long)(ClockP_getTimeUsec() / 1000000); if (timer) *timer = t; return t; +#elif defined(TARGET_stm32h7) + unsigned long t = (unsigned long)(h7_get_ms() / 1000); + if (timer) *timer = t; + return t; #else /* Simple incrementing counter */ tick_counter++; @@ -307,6 +374,11 @@ double current_time(int reset) #elif defined(TARGET_ti_am64x_r5) (void)reset; return (double)(ClockP_getTimeUsec()) / 1000000.0; +#elif defined(TARGET_stm32h7) + uint32_t ms = h7_get_ms(); + if (reset) + h7_start_ms = ms; + return (double)(ms - h7_start_ms) / 1000.0; #else /* Simple counter-based timing */ if (reset)