From cdf10acaef49c927a13a1fcce221825d863f8bae Mon Sep 17 00:00:00 2001 From: David Garske Date: Tue, 1 Dec 2026 14:30:39 -0800 Subject: [PATCH] stm32h5: add STM32H573I-DK board support and fix the UART baud divisor --- .github/workflows/test-configs.yml | 12 +++ CMakeLists.txt | 3 + arch.mk | 3 + config/examples/stm32h5-dk-tz.config | 36 ++++++++ config/examples/stm32h5-dk.config | 35 ++++++++ docs/Targets.md | 46 ++++++++++ hal/stm32h5.c | 51 ++++++++++- hal/stm32h5.h | 49 ++++++++-- hal/uart/uart_drv_stm32h5.c | 128 ++++++++++++++++++++++++--- test-app/Makefile | 6 +- test-app/app_stm32h5.c | 91 ++++++++++++++++--- test-app/startup_arm.c | 10 +++ tools/config.mk | 2 +- 13 files changed, 440 insertions(+), 32 deletions(-) create mode 100644 config/examples/stm32h5-dk-tz.config create mode 100644 config/examples/stm32h5-dk.config diff --git a/.github/workflows/test-configs.yml b/.github/workflows/test-configs.yml index aef24ad2d9..010074122f 100644 --- a/.github/workflows/test-configs.yml +++ b/.github/workflows/test-configs.yml @@ -1256,6 +1256,18 @@ jobs: arch: arm config-file: ./config/examples/stm32h5-dualbank.config + stm32h5_dk_test: + uses: ./.github/workflows/test-build.yml + with: + arch: arm + config-file: ./config/examples/stm32h5-dk.config + + stm32h5_dk_tz_test: + uses: ./.github/workflows/test-build.yml + with: + arch: arm + config-file: ./config/examples/stm32h5-dk-tz.config + stm32n6_test: uses: ./.github/workflows/test-build.yml with: diff --git a/CMakeLists.txt b/CMakeLists.txt index a4a0a3c180..888accf65e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -741,6 +741,9 @@ if(ARCH STREQUAL "ARM") if(${WOLFBOOT_TARGET} STREQUAL "stm32h5") set(ARCH_FLASH_OFFSET 0x08000000) + if(STM32H5_BOARD STREQUAL "h573i_dk") + list(APPEND WOLFBOOT_DEFS STM32H573I_DK) + endif() if(TZEN) set(WOLFBOOT_ORIGIN 0x0C000000) else() diff --git a/arch.mk b/arch.mk index 9b55016bc9..79e635a7bc 100644 --- a/arch.mk +++ b/arch.mk @@ -958,6 +958,9 @@ ifeq ($(ARCH),ARM) $(call LSCRIPT_IN_DEFAULT,hal/$(TARGET)-ns.ld) endif SPI_TARGET=stm32 + ifeq ($(STM32H5_BOARD),h573i_dk) + CFLAGS+=-DSTM32H573I_DK + endif ifneq ($(DEBUG),0) CFLAGS+=-DPKCS11_SMALL endif diff --git a/config/examples/stm32h5-dk-tz.config b/config/examples/stm32h5-dk-tz.config new file mode 100644 index 0000000000..7b6cf98ed4 --- /dev/null +++ b/config/examples/stm32h5-dk-tz.config @@ -0,0 +1,36 @@ +ARCH?=ARM +TZEN?=1 +STM32H5_BOARD?=h573i_dk +TARGET?=stm32h5 +SIGN?=ECC256 +HASH?=SHA256 +DEBUG?=0 +VTOR?=1 +CORTEX_M0?=0 +CORTEX_M33?=1 +NO_ASM?=0 +NO_MPU=1 +EXT_FLASH?=0 +SPI_FLASH?=0 +ALLOW_DOWNGRADE?=0 +NVM_FLASH_WRITEONCE?=1 +WOLFBOOT_VERSION?=1 +V?=0 +SPMATH?=1 +RAM_CODE?=1 +DUALBANK_SWAP?=0 +WOLFBOOT_PARTITION_SIZE?=0xA0000 +WOLFBOOT_SECTOR_SIZE?=0x2000 +WOLFBOOT_KEYVAULT_ADDRESS?=0x0C040000 +WOLFBOOT_KEYVAULT_SIZE?=0x1C000 +WOLFBOOT_NSC_ADDRESS?=0x0C05C000 +WOLFBOOT_NSC_SIZE?=0x4000 +WOLFBOOT_PARTITION_BOOT_ADDRESS?=0x08060000 +WOLFBOOT_PARTITION_UPDATE_ADDRESS?=0x0C100000 +WOLFBOOT_PARTITION_SWAP_ADDRESS?=0x0C1A0000 +FLAGS_HOME=0 +DISABLE_BACKUP=0 +WOLFCRYPT_TZ=1 +WOLFCRYPT_TZ_PKCS11=1 +IMAGE_HEADER_SIZE?=1024 +ARMORED=1 diff --git a/config/examples/stm32h5-dk.config b/config/examples/stm32h5-dk.config new file mode 100644 index 0000000000..9c3a8fdb2c --- /dev/null +++ b/config/examples/stm32h5-dk.config @@ -0,0 +1,35 @@ +ARCH?=ARM +TZEN?=0 +STM32H5_BOARD?=h573i_dk +TARGET?=stm32h5 +SIGN?=ECC256 +HASH?=SHA256 +DEBUG?=0 +VTOR?=1 +CORTEX_M0?=0 +CORTEX_M33?=1 +NO_ASM?=0 +NO_MPU=1 +EXT_FLASH?=0 +SPI_FLASH?=0 +ALLOW_DOWNGRADE?=0 +NVM_FLASH_WRITEONCE?=1 +WOLFBOOT_VERSION?=1 +V?=0 +SPMATH?=1 +RAM_CODE?=0 +DUALBANK_SWAP?=0 +WOLFBOOT_PARTITION_SIZE?=0xA0000 +#Double sector size to fit header in ML-DSA-87 +WOLFBOOT_SECTOR_SIZE?=0x4000 +WOLFBOOT_KEYVAULT_ADDRESS?=0x0C040000 +WOLFBOOT_KEYVAULT_SIZE?=0x1C000 +WOLFBOOT_NSC_ADDRESS?=0x0C05C000 +WOLFBOOT_NSC_SIZE?=0x4000 +WOLFBOOT_PARTITION_BOOT_ADDRESS?=0x08060000 +WOLFBOOT_PARTITION_UPDATE_ADDRESS?=0x08100000 +WOLFBOOT_PARTITION_SWAP_ADDRESS?=0x081A0000 +FLAGS_HOME=0 +DISABLE_BACKUP=0 +IMAGE_HEADER_SIZE?=1024 +ARMORED=1 diff --git a/docs/Targets.md b/docs/Targets.md index e0f51d7ba3..01c665fc0b 100644 --- a/docs/Targets.md +++ b/docs/Targets.md @@ -1477,6 +1477,52 @@ Any version except the first one will also turn on the orange LED. For a full Zephyr integration walkthrough (build + flash), see: [/zephyr/README.md](/zephyr/README.md) +### STM32H573I-DK + +The STM32H573I-DK Discovery kit is selected with `STM32H5_BOARD=h573i_dk`, which +builds wolfBoot and the test application with `-DSTM32H573I_DK`. Without this +option the NUCLEO-H563ZI settings are used. + +Board settings: + +- HSE is a 25 MHz digital clock (`HSEBYP` and `HSEEXT`); PLL1 uses M=5, N=100, + P=2 for a 250 MHz system clock. +- The console is USART1 on PA9 (TX) / PA10 (RX), 115200 8N1, routed to the + ST-LINK virtual COM port. +- LEDs are active low: PI8 (orange) is the boot LED, PI9 (green) the user LED + and PF4 (blue) the extra LED. +- With TrustZone enabled, wolfBoot releases USART1, its interrupt and ports + GPIOF/GPIOI to the non-secure domain before staging the application. + +Example configurations: + +- `config/examples/stm32h5-dk.config`: TrustZone disabled (`TZEN=0`), swap + update with the same partition layout as `stm32h5-no-tz.config`. Tested on + the STM32H573I-DK (boot, update, confirm and rollback). +- `config/examples/stm32h5-dk-tz.config`: TrustZone enabled, same layout as + `stm32h5-tz.config`, with the option bytes and flashing steps of Scenario 1 + above. Tested on the STM32H573I-DK (boot, update, confirm and rollback). + +```sh +cp config/examples/stm32h5-dk.config .config +make DEBUG_UART=1 +STM32_Programmer_CLI -c port=swd -d factory.bin 0x08000000 +``` + +To test an update, sign a new version, write it to the update partition and +set the update trigger at the end of the partition, then reset the board: + +```sh +IMAGE_HEADER_SIZE=1024 WOLFBOOT_PARTITION_SIZE=0xA0000 WOLFBOOT_SECTOR_SIZE=0x4000 \ + tools/keytools/sign --ecc256 --sha256 test-app/image.bin wolfboot_signing_private_key.der 2 +printf 'pBOOT' > trigger_magic.bin +STM32_Programmer_CLI -c port=swd -d test-app/image_v2_signed.bin 0x08100000 +STM32_Programmer_CLI -c port=swd -d trigger_magic.bin 0x0819FFFB -hardRst +``` + +The `success` command of the test application confirms the new version. An +update that is not confirmed is rolled back on the next reset. + ### STM32H5 Debugging diff --git a/hal/stm32h5.c b/hal/stm32h5.c index 7665ef53fa..ae3fa3f422 100644 --- a/hal/stm32h5.c +++ b/hal/stm32h5.c @@ -452,7 +452,7 @@ static void clock_pll_off(void) } /* If PLL_SRC_HSE is set then HSE (8MHz) is used otherwise HSI 64 MHz is used - * and system clock is 250MHz */ + * and system clock is 250MHz. The STM32H573I-DK HSE is 25MHz. */ static void clock_pll_on(void) { @@ -460,8 +460,13 @@ static void clock_pll_on(void) uint32_t plln, pllm, pllq, pllp, pllr, hpre, apb1pre, apb2pre, apb3pre, flash_waitstates; #if PLL_SRC_HSE +#ifdef STM32H573I_DK + pllm = 5; + plln = 100; +#else pllm = 1; plln = 62; +#endif pllp = 2; /* 250Mhz */ pllq = 5; /* 100Mhz */ pllr = 2; @@ -494,6 +499,10 @@ static void clock_pll_on(void) #if PLL_SRC_HSE /* PLL Oscillator configuration */ +#ifdef STM32H573I_DK + /* Digital clock on OSC_IN: select before turning HSE on */ + RCC_CR |= RCC_CR_HSEBYP | RCC_CR_HSEEXT; +#endif RCC_CR |= RCC_CR_HSEON | RCC_CR_HSEBYP; /* Wait until HSE is Ready */ @@ -546,7 +555,11 @@ static void clock_pll_on(void) DMB(); /* Select PLL1 Input frequency range: VCI */ +#ifdef STM32H573I_DK + RCC_PLL1CFGR |= RCC_PLLCFGR_RGE_4_8 << RCC_PLLCFGR_PLLRGE_SHIFT; +#else RCC_PLL1CFGR |= RCC_PLLCFGR_RGE_2_4 << RCC_PLLCFGR_PLLRGE_SHIFT; +#endif /* Select PLL1 Output frequency range: VCO = 0 */ RCC_PLL1CFGR &= ~RCC_PLLCFGR_PLLVCOSEL; @@ -599,6 +612,7 @@ static void clock_pll_on(void) #define NVIC_ICER_BASE (0xE000E180) #define NVIC_IPRI_BASE (0xE000E400) #define NVIC_USART3_IRQ 60 +#define NVIC_USART1_IRQ 58 /* Cortex M-33 has an extra register to set up non-secure interrupts */ #define NVIC_ITNS_BASE (0xE000E380) @@ -621,7 +635,7 @@ static void periph_unsecure(void) RCC_AHB2_CLOCK_ER |= GPIOD_AHB2_CLOCK_ER; /* Enable clock for LPUART1 */ - RCC_APB2_CLOCK_ER |= UART1_APB2_CLOCK_ER_VAL; + RCC_APB3_CLOCK_ER |= UART1_APB3_CLOCK_ER_VAL; /* Enable clock for USART3 */ RCC_APB1L_CLOCK_ER |= UART3_APB1L_CLOCK_ER_VAL; @@ -629,9 +643,15 @@ static void periph_unsecure(void) PWR_CR2 |= PWR_CR2_IOSV; /* Un-secure User LED GPIO pins */ +#ifdef STM32H573I_DK + GPIO_SECCFGR(GPIOI_BASE) &= ~(1 << LED_BOOT_PIN); /* PI8 - Discovery board - Orange Led */ + GPIO_SECCFGR(GPIOI_BASE) &= ~(1 << LED_USR_PIN); /* PI9 - Discovery board - Green Led */ + GPIO_SECCFGR(GPIOF_BASE) &= ~(1 << LED_EXTRA_PIN); /* PF4 - Discovery board - Blue Led */ +#else GPIO_SECCFGR(GPIOG_BASE) &= ~(1 << LED_BOOT_PIN); /* PG4 - Nucleo board - Orange Led */ GPIO_SECCFGR(GPIOB_BASE) &= ~(1 << LED_USR_PIN); /* PB0 - Nucleo board - Green Led */ GPIO_SECCFGR(GPIOF_BASE) &= ~(1 << LED_EXTRA_PIN); /* PF4 - Nucleo board - Blue Led */ +#endif /* Unsecure LPUART1 */ GPIO_SECCFGR(GPIOB_BASE) &= ~(1< #include "hal/stm32h5.h" -#if defined(USE_UART1) && USE_UART == 1 +#ifdef STM32H573I_DK +static void uart_pins_setup(void) +{ + uint32_t reg; + RCC_AHB2ENR_CLOCK_ER |= GPIOA_AHB2ENR1_CLOCK_ER; + /* Set mode = AF */ + reg = GPIOA_MODE & ~ (0x03 << (USART1_RX_PIN * 2)); + GPIOA_MODE = reg | (2 << (USART1_RX_PIN * 2)); + reg = GPIOA_MODE & ~ (0x03 << (USART1_TX_PIN * 2)); + GPIOA_MODE = reg | (2 << (USART1_TX_PIN * 2)); + + /* Alternate function: use hi pins (9 and 10) */ + reg = GPIOA_AFH & ~(0xf << ((USART1_TX_PIN - 8) * 4)); + GPIOA_AFH = reg | (USART1_PIN_AF << ((USART1_TX_PIN - 8) * 4)); + reg = GPIOA_AFH & ~(0xf << ((USART1_RX_PIN - 8) * 4)); + GPIOA_AFH = reg | (USART1_PIN_AF << ((USART1_RX_PIN - 8) * 4)); + +} +#elif defined(USE_UART1) && USE_UART1 == 1 static void uart_pins_setup(void) { uint32_t reg; @@ -45,7 +63,7 @@ static void uart_pins_setup(void) reg = GPIOB_AFL & ~(0xf << (UART1_TX_PIN * 4)); GPIOB_AFL = reg | (UART1_PIN_AF << (UART1_TX_PIN * 4)); reg = GPIOB_AFL & ~(0xf << ((UART1_RX_PIN) * 4)); - GPIOB_AFH = reg | (UART1_PIN_AF << ((UART1_RX_PIN) * 4)); + GPIOB_AFL = reg | (UART1_PIN_AF << ((UART1_RX_PIN) * 4)); } #else @@ -68,13 +86,100 @@ static void uart_pins_setup(void) } #endif +/* Kernel clock is the PCLK of the bus the UART sits on (xSEL = 0) */ +#ifdef STM32H573I_DK +#define UART_PPRE_SHIFT RCC_CFGR2_PPRE2_SHIFT +#elif defined(USE_UART1) && USE_UART1 == 1 +#define UART_PPRE_SHIFT RCC_CFGR2_PPRE3_SHIFT +#else +#define UART_PPRE_SHIFT RCC_CFGR2_PPRE1_SHIFT +#endif + +uint32_t stm32h5_get_hclk(void) +{ + uint32_t clk, src, divr, pre; + + switch ((RCC_CFGR1 >> RCC_CFGR1_SWS_SHIFT) & RCC_CFGR1_SW_MASK) { + case RCC_CFGR1_SW_CSI: + clk = CSI_FREQ; + break; + case RCC_CFGR1_SW_HSE: + clk = HSE_FREQ; + break; + case RCC_CFGR1_SW_PLL1: + src = RCC_PLL1CFGR & 0x3; + if (src == RCC_PLLCFGR_PLLSRC_HSE) + clk = HSE_FREQ; + else if (src == RCC_PLLCFGR_PLLSRC_CSI) + clk = CSI_FREQ; + else + clk = HSI_FREQ >> ((RCC_CR >> RCC_CR_HSIDIV_SHIFT) & 0x3); + divr = RCC_PLL1DIVR; + clk /= (RCC_PLL1CFGR >> RCC_PLLCFGR_PLLM_SHIFT) & 0x3F; + clk *= ((divr >> RCC_PLLDIVR_DIVN_SHIFT) & 0x1FF) + 1; + clk /= ((divr >> RCC_PLLDIVR_DIVP_SHIFT) & 0x7F) + 1; + break; + default: + clk = HSI_FREQ >> ((RCC_CR >> RCC_CR_HSIDIV_SHIFT) & 0x3); + break; + } + pre = (RCC_CFGR2 >> RCC_CFGR2_HPRE_SHIFT) & 0xF; + if (pre & 0x8) { + pre &= 0x7; + clk >>= pre + ((pre < 4) ? 1 : 2); /* no divide by 32 */ + } + return clk; +} + +static uint32_t uart_kernel_clock(void) +{ + uint32_t clk = stm32h5_get_hclk(); + uint32_t pre; + + pre = (RCC_CFGR2 >> UART_PPRE_SHIFT) & 0x7; + if (pre & 0x4) + clk >>= (pre & 0x3) + 1; + return clk; +} + +/* USART_PRESC/LPUART_PRESC settings 0-11 */ +static const uint16_t uart_presc_div[] = { + 1, 2, 4, 6, 8, 10, 12, 16, 32, 64, 128, 256 +}; + static int uart_base_init(uint32_t base, uint32_t bitrate, uint8_t data, char parity, uint8_t stop) { - uint32_t reg; + uint32_t reg, clk, presc; + /* BRR is 16 bits; LPUART's BRR is 20 bits and holds 256x the ratio */ + uint32_t max_ratio = (base == UART1) ? 0xFFFU : 0xFFFFU; + + /* BRR and PRESC are only writable with UE = 0 */ + if (UART_CR1(base) & UART_CR1_UART_ENABLE) { + while ((UART_ISR(base) & UART_ISR_TC) == 0) + ; + UART_CR1(base) &= ~UART_CR1_UART_ENABLE; + } + + /* Smallest prescaler that keeps the divisor within BRR */ + clk = uart_kernel_clock(); + for (presc = 0; presc < (sizeof(uart_presc_div) / + sizeof(uart_presc_div[0])) - 1; presc++) { + if (clk / uart_presc_div[presc] / bitrate <= max_ratio) + break; + } + UART_PRE(base) = presc; + clk /= uart_presc_div[presc]; /* Configure clock */ - UART_BRR(base) = (uint16_t)(PERIPH_CLOCK_FREQ / bitrate) + 1; + if (base == UART1) { + /* LPUART: BRR = 256 * clk / bitrate, without a 64-bit divide */ + UART_BRR(base) = ((clk / bitrate) << 8) + + (((clk % bitrate) << 8) + bitrate / 2) / bitrate; + } + else { + UART_BRR(base) = (uint16_t)((clk + bitrate / 2) / bitrate); + } /* Configure data bits */ if (data == 8) @@ -101,9 +206,6 @@ static int uart_base_init(uint32_t base, uint32_t bitrate, uint8_t data, else UART_CR2(base) = reg; - /* Prescaler to DIV1 */ - UART_PRE(base) |= 2; - /* Configure for RX+TX, turn on. */ UART_CR1(base) |= UART_CR1_TX_ENABLE | UART_CR1_RX_ENABLE | UART_CR1_UART_ENABLE; @@ -144,12 +246,18 @@ int uart_init(uint32_t bitrate, uint8_t data, char parity, uint8_t stop) { uint32_t reg; uart_pins_setup(); -#if defined(USE_UART1) && USE_UART == 1 +#ifdef STM32H573I_DK + RCC_APB2_CLOCK_ER |= USART1_APB2_CLOCK_ER_VAL; + reg = RCC_CCIPR1 & (~ (RCC_CCIPR1_USART1SEL_MASK << RCC_CCIPR1_USART1SEL_SHIFT)); + RCC_CCIPR1 = reg | (0 << RCC_CCIPR1_USART1SEL_SHIFT); /* PCLK2 */ +#elif defined(USE_UART1) && USE_UART1 == 1 + RCC_APB3_CLOCK_ER |= UART1_APB3_CLOCK_ER_VAL; reg = RCC_CCIPR3 & (~ (RCC_CCIPR3_LPUART1SEL_MASK << RCC_CCIPR3_LPUART1SEL_SHIFT)); - RCC_CCIPR3 = reg | (0 << RCC_CCIPR3_LPUART1SEL_SHIFT); /* PLL2 */ + RCC_CCIPR3 = reg | (0 << RCC_CCIPR3_LPUART1SEL_SHIFT); /* PCLK3 */ #else + RCC_APB1L_CLOCK_ER |= UART3_APB1L_CLOCK_ER_VAL; reg = RCC_CCIPR1 & (~ (RCC_CCIPR1_USART3SEL_MASK << RCC_CCIPR1_USART3SEL_SHIFT)); - RCC_CCIPR1 = reg | (0 << RCC_CCIPR1_USART3SEL_SHIFT); /* PLL2 */ + RCC_CCIPR1 = reg | (0 << RCC_CCIPR1_USART3SEL_SHIFT); /* PCLK1 */ #endif return uart_base_init(USE_UART, bitrate, data, parity, stop); } diff --git a/test-app/Makefile b/test-app/Makefile index 0616529149..67bd2483f0 100644 --- a/test-app/Makefile +++ b/test-app/Makefile @@ -771,7 +771,11 @@ ifeq ($(TARGET),stm32h5) LDFLAGS+=-mcpu=cortex-m33 LDFLAGS+=-Wl,-gc-sections -Wl,-Map=image.map CFLAGS+=-I.. - APP_OBJS+=../hal/uart/uart_drv_$(UART_TARGET).o + ifeq ($(TZEN),1) + APP_OBJS+=../hal/uart/uart_drv_$(UART_TARGET)_ns.o + else + APP_OBJS+=../hal/uart/uart_drv_$(UART_TARGET).o + endif ifneq ($(SIGN),NONE) ifeq ($(FLASH_OTP_KEYSTORE),1) APP_OBJS+=../src/flash_otp_keystore.o diff --git a/test-app/app_stm32h5.c b/test-app/app_stm32h5.c index d47281d175..1dacf1f20d 100644 --- a/test-app/app_stm32h5.c +++ b/test-app/app_stm32h5.c @@ -75,9 +75,11 @@ static uint32_t uart_processed = 0; static int uart_rx_isr(unsigned char *c, int len); static int uart_poll(void); +#ifndef STM32H573I_DK #define LED_BOOT_PIN (4) /* PG4 - Nucleo - Red Led */ #define LED_USR_PIN (0) /* PB0 - Nucleo - Green Led */ #define LED_EXTRA_PIN (4) /* PF4 - Nucleo - Orange Led */ +#endif #define BOOT_TIME_PIN (13) /* PA13 - scope trigger */ #ifdef WOLFBOOT_TEST_FILLER @@ -85,7 +87,11 @@ static int uart_poll(void); static volatile uint8_t filler_data[FILLER_SIZE] = { 0x01, 0x02, 0x03 }; #endif -#define NVIC_USART3_IRQN (60) +#ifdef STM32H573I_DK +#define NVIC_UART_IRQN (58) /* USART1 */ +#else +#define NVIC_UART_IRQN (60) /* USART3 */ +#endif #ifndef GPIOA_MODER #define GPIOA_MODER (*(volatile uint32_t *)(GPIOA_BASE + 0x00)) @@ -94,7 +100,7 @@ static volatile uint8_t filler_data[FILLER_SIZE] = { 0x01, 0x02, 0x03 }; #endif /* SysTick */ -static uint32_t cpu_freq = 250000000; +extern uint32_t stm32h5_get_hclk(void); int clock_gettime (clockid_t clock_id, struct timespec *tp) @@ -107,7 +113,7 @@ int clock_gettime (clockid_t clock_id, struct timespec *tp) static void systick_enable(void) { - SYSTICK_RVR = ((cpu_freq / 1000) - 1); + SYSTICK_RVR = ((stm32h5_get_hclk() / 1000) - 1); SYSTICK_CVR = 0; SYSTICK_CSR |= 0x07; } @@ -117,6 +123,27 @@ void isr_systick(void) jiffies++; } +#ifdef STM32H573I_DK +/* Discovery LEDs are active low */ +static void dk_led_on(volatile uint32_t *gpio, uint32_t clk, uint32_t pin) +{ + uint32_t reg; + + RCC_AHB2ENR_CLOCK_ER |= clk; + /* Delay after an RCC peripheral clock enabling */ + reg = RCC_AHB2ENR_CLOCK_ER; + + gpio[6] = (1 << (pin + 16)); /* BSRR */ + reg = gpio[0] & ~(0x03 << (pin * 2)); /* MODER */ + gpio[0] = reg | (1 << (pin * 2)); + gpio[3] &= ~(0x03 << (pin * 2)); /* PUPDR */ +} + +static void boot_led_on(void) +{ + dk_led_on(&GPIOI_MODER, GPIOI_AHB2ENR1_CLOCK_ER, LED_BOOT_PIN); +} +#else static void boot_led_on(void) { uint32_t reg; @@ -131,6 +158,7 @@ static void boot_led_on(void) GPIOG_PUPDR &= ~(0x03 << (pin * 2)); GPIOG_BSRR |= (1 << (pin)); } +#endif /* STM32H573I_DK */ void boot_time_pin_on_early(void) { @@ -147,6 +175,32 @@ void boot_time_pin_on_early(void) GPIOA_BSRR = (1 << (pin)); } +#ifdef STM32H573I_DK +static void boot_led_off(void) +{ + GPIOI_BSRR = (1 << LED_BOOT_PIN); +} + +void usr_led_on(void) +{ + dk_led_on(&GPIOI_MODER, GPIOI_AHB2ENR1_CLOCK_ER, LED_USR_PIN); +} + +void usr_led_off(void) +{ + GPIOI_BSRR = (1 << LED_USR_PIN); +} + +void extra_led_on(void) +{ + dk_led_on(&GPIOF_MODER, GPIOF_AHB2ENR1_CLOCK_ER, LED_EXTRA_PIN); +} + +void extra_led_off(void) +{ + GPIOF_BSRR = (1 << LED_EXTRA_PIN); +} +#else static void boot_led_off(void) { GPIOG_BSRR |= (1 << (LED_BOOT_PIN + 16)); @@ -191,6 +245,7 @@ void extra_led_off(void) { GPIOF_BSRR |= (1 << (LED_EXTRA_PIN + 16)); } +#endif /* STM32H573I_DK */ /* Command line commands */ static int cmd_help(const char *args); @@ -1420,23 +1475,29 @@ static void console_loop(void) } } +#ifdef STM32H573I_DK +void isr_usart1(void) +#else void isr_usart3(void) +#endif { volatile uint32_t reg; usr_led_on(); - reg = UART_ISR(UART3); + reg = UART_ISR(USE_UART); + if (reg & UART_ORE) + UART_ICR(USE_UART) = UART_ORE; if (reg & UART_ISR_RX_NOTEMPTY) { if (uart_rx_bytes >= 1023) - reg = UART_RDR(UART3); + reg = UART_RDR(USE_UART); else - uart_buf_rx[uart_rx_bytes++] = (unsigned char)(UART_RDR(UART3) & 0xFF); + uart_buf_rx[uart_rx_bytes++] = (unsigned char)(UART_RDR(USE_UART) & 0xFF); } } static int uart_rx_isr(unsigned char *c, int len) { uint32_t avail; - UART_CR1(UART3) &= ~UART_ISR_RX_NOTEMPTY; + UART_CR1(USE_UART) &= ~UART_ISR_RX_NOTEMPTY; if (len < 0) { len = 0; } @@ -1453,7 +1514,7 @@ static int uart_rx_isr(unsigned char *c, int len) usr_led_off(); } } - UART_CR1(UART3) |= UART_ISR_RX_NOTEMPTY; + UART_CR1(USE_UART) |= UART_ISR_RX_NOTEMPTY; return len; } @@ -1483,12 +1544,18 @@ void main(void) app_version = wolfBoot_current_firmware_version(); #endif - nvic_irq_setprio(NVIC_USART3_IRQN, 0); - nvic_irq_enable(NVIC_USART3_IRQN); +#ifndef TZEN + /* wolfBoot leaves interrupts masked when it is not staging through + * TrustZone */ + asm volatile("cpsie i"); +#endif + + nvic_irq_setprio(NVIC_UART_IRQN, 0); + nvic_irq_enable(NVIC_UART_IRQN); uart_init(115200, 8, 'N', 1); - UART_CR1(UART3) |= UART_ISR_RX_NOTEMPTY; - UART_CR3(UART3) |= UART_CR3_RXFTIE; + UART_CR1(USE_UART) |= UART_ISR_RX_NOTEMPTY; + UART_CR3(USE_UART) |= UART_CR3_RXFTIE; printf("========================\r\n"); printf("STM32H5 wolfBoot demo Application\r\n"); diff --git a/test-app/startup_arm.c b/test-app/startup_arm.c index 8de8219caa..504a62f226 100644 --- a/test-app/startup_arm.c +++ b/test-app/startup_arm.c @@ -36,8 +36,12 @@ extern void isr_tim2(void); #endif #ifdef TARGET_stm32h5 +#ifdef STM32H573I_DK +extern void isr_usart1(void); +#else extern void isr_usart3(void); #endif +#endif #ifdef TARGET_s32k1xx extern void isr_debug_uart(void); @@ -413,9 +417,15 @@ void (* const IV[])(void) = isr_empty, // SPI1_IRQHandler isr_empty, // SPI2_IRQHandler isr_empty, // SPI3_IRQHandler +#ifdef STM32H573I_DK + isr_usart1, // USART1_IRQHandler + isr_empty, // USART2_IRQHandler + isr_empty, // USART3_IRQHandler +#else isr_empty, // USART1_IRQHandler isr_empty, // USART2_IRQHandler isr_usart3, // USART3_IRQHandler +#endif isr_empty, // UART4_IRQHandler isr_empty, // UART5_IRQHandler isr_empty, // LPUART1_IRQHandler diff --git a/tools/config.mk b/tools/config.mk index ee5fa9d734..6bc98d6295 100644 --- a/tools/config.mk +++ b/tools/config.mk @@ -125,7 +125,7 @@ CONFIG_VARS:= ARCH TARGET SIGN HASH MCUXSDK MCUXPRESSO MCUXPRESSO_CPU MCUXPRESSO NVM_FLASH_JOURNAL NVM_FLASH_JOURNAL_DUAL NVM_FLASH_ECC \ FLASH_WRITE_UNIT FLASH_ERASE_UNIT \ DISABLE_BACKUP WOLFBOOT_VERSION V NO_MPU ENCRYPT FLAGS_HOME FLAGS_INVERT NO_SWAP_EXT \ - SPMATH SPMATHALL RAM_CODE DUALBANK_SWAP IMAGE_HEADER_SIZE PKA TZEN PSOC6_CRYPTO PSOC6_BOARD PSOC6_CM4_APP \ + SPMATH SPMATHALL RAM_CODE DUALBANK_SWAP IMAGE_HEADER_SIZE PKA TZEN PSOC6_CRYPTO PSOC6_BOARD PSOC6_CM4_APP STM32H5_BOARD \ WOLFTPM WOLFBOOT_TPM_VERIFY MEASURED_BOOT WOLFBOOT_TPM_SEAL WOLFBOOT_TPM_KEYSTORE WOLFBOOT_SECURE_APP WOLFBOOT_SECURE_HANDOFF_ADDRESS \ WOLFBOOT_TPM_MFG_AUTH_DERIVE \ WOLFBOOT_ATTESTATION_IAK \