Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/footprint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:

- name: Select config
run: |
cp config/examples/stm32f4.config .config && make include/target.h
cp config/examples/stm32f407-discovery.config .config && make include/target.h

- name: Build key tools
run: |
Expand Down
11 changes: 9 additions & 2 deletions .github/workflows/test-build-cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,17 @@ jobs:
- name: Build wolfBoot
run: make -C build

- name: Run CMake build for STM32F4
- name: Run CMake build for STM32F407-DISCOVERY
run: |
rm -rf ./build
cmake -B build -DWOLFBOOT_TARGET=stm32f4 -DWOLFBOOT_PARTITION_SIZE=0x20000 -DWOLFBOOT_SECTOR_SIZE=0x20000 -DWOLFBOOT_PARTITION_BOOT_ADDRESS=0x08020000 -DWOLFBOOT_PARTITION_UPDATE_ADDRESS=0x08040000 -DWOLFBOOT_PARTITION_SWAP_ADDRESS=0x08060000
cmake -B build -DWOLFBOOT_TARGET=stm32f4 -DWOLFBOOT_PARTITION_SIZE=0x20000 -DWOLFBOOT_SECTOR_SIZE=0x20000 -DWOLFBOOT_PARTITION_BOOT_ADDRESS=0x08020000 -DWOLFBOOT_PARTITION_UPDATE_ADDRESS=0x08040000 -DWOLFBOOT_PARTITION_SWAP_ADDRESS=0x08060000 -DCLOCK_SPEED=160000000 -DSTM32_PLLM=8 -DSTM32_PLLN=336 -DSTM32_PLLP=2 -DSTM32_PLLQ=7
- name: Build wolfBoot
run: make -C build

- name: Run CMake build for STM32F411-BLACKPILL
run: |
rm -rf ./build
cmake -B build -DWOLFBOOT_TARGET=stm32f4 -DWOLFBOOT_PARTITION_SIZE=0x20000 -DWOLFBOOT_SECTOR_SIZE=0x20000 -DWOLFBOOT_PARTITION_BOOT_ADDRESS=0x08020000 -DWOLFBOOT_PARTITION_UPDATE_ADDRESS=0x08040000 -DWOLFBOOT_PARTITION_SWAP_ADDRESS=0x08060000 -DCLOCK_SPEED=84000000 -DSTM32_PLLM=25 -DSTM32_PLLN=336 -DSTM32_PLLP=4 -DSTM32_PLLQ=7
- name: Build wolfBoot
run: make -C build

Expand Down
10 changes: 8 additions & 2 deletions .github/workflows/test-configs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -272,11 +272,17 @@ jobs:
arch: arm
config-file: ./config/examples/stm32f4-small-blocks-uart-update.config

stm32f4_test:
stm32f407_discovery_test:
uses: ./.github/workflows/test-build.yml
with:
arch: arm
config-file: ./config/examples/stm32f4.config
config-file: ./config/examples/stm32f407-discovery.config

stm32f411_blackpill_test:
uses: ./.github/workflows/test-build.yml
with:
arch: arm
config-file: ./config/examples/stm32f411-blackpill.config

stm32f7_dualbank_test:
uses: ./.github/workflows/test-build.yml
Expand Down
23 changes: 23 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,29 @@ if(ARCH STREQUAL "ARM")
if(${WOLFBOOT_TARGET} STREQUAL "stm32f4")
set(ARCH_FLASH_OFFSET 0x08000000)
set(WOLFBOOT_ORIGIN ${ARCH_FLASH_OFFSET})

if(NOT DEFINED CLOCK_SPEED)
message(FATAL_ERROR "CLOCK_SPEED must be defined")
endif()
if(NOT DEFINED STM32_PLLM)
message(FATAL_ERROR "STM32_PLLM must be defined")
endif()
if(NOT DEFINED STM32_PLLN)
message(FATAL_ERROR "STM32_PLLN must be defined")
endif()
if(NOT DEFINED STM32_PLLP)
message(FATAL_ERROR "STM32_PLLP must be defined")
endif()
if(NOT DEFINED STM32_PLLQ)
message(FATAL_ERROR "STM32_PLLQ must be defined")
endif()
add_compile_definitions(
CLOCK_SPEED=${CLOCK_SPEED}
STM32_PLLM=${STM32_PLLM}
STM32_PLLN=${STM32_PLLN}
STM32_PLLP=${STM32_PLLP}
STM32_PLLQ=${STM32_PLLQ}
)
endif()

if(${WOLFBOOT_TARGET} STREQUAL "stm32u5")
Expand Down
4 changes: 2 additions & 2 deletions IDE/IAR/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ application image starts at address 0x08020000.

```
$template=Get-Content -path ..\..\include\target.h.in;
Get-Content -path ..\..\config\examples\stm32f4.config | ForEach-Object {$v=$_.Split('?='); $a=$v[0]; $b=$v[2]; $template=($template -replace "##$a##",$b) };
Get-Content -path ..\..\config\examples\stm32f407-discovery.config | ForEach-Object {$v=$_.Split('?='); $a=$v[0]; $b=$v[2]; $template=($template -replace "##$a##",$b) };
$template=($template -replace "##.*##","");
Set-Content -path target.h $template
```
Expand Down Expand Up @@ -91,4 +91,4 @@ If you are using a STM32F407-discovery board, a red LED will turn on upon applic

## Armored Mode (Glitch Resistance)

If you would like to enable the "Armored" mode (glitch resistance) in IAR you can set the compiler pre-processor macro `WOLFBOOT_ARMORED`. Note: This has only been tested with ECDSA on Cortex-M.
If you would like to enable the "Armored" mode (glitch resistance) in IAR you can set the compiler pre-processor macro `WOLFBOOT_ARMORED`. Note: This has only been tested with ECDSA on Cortex-M.
5 changes: 5 additions & 0 deletions config/examples/stm32f4-small-blocks-uart-update.config
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,8 @@ WOLFBOOT_PARTITION_UPDATE_ADDRESS?=0x00000
WOLFBOOT_PARTITION_SWAP_ADDRESS?=0x4000
WOLFBOOT_LOAD_ADDRESS?=0x200000
WOLFBOOT_LOAD_DTS_ADDRESS?=0x400000
CLOCK_SPEED?=160000000
STM32_PLLM?=8
STM32_PLLN?=336
STM32_PLLP?=2
STM32_PLLQ?=7
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,8 @@ WOLFBOOT_SECTOR_SIZE?=0x20000
WOLFBOOT_PARTITION_BOOT_ADDRESS?=0x08020000
WOLFBOOT_PARTITION_UPDATE_ADDRESS?=0x08040000
WOLFBOOT_PARTITION_SWAP_ADDRESS?=0x08060000
CLOCK_SPEED?=160000000
STM32_PLLM?=8
STM32_PLLN?=336
STM32_PLLP?=2
STM32_PLLQ?=7
21 changes: 21 additions & 0 deletions config/examples/stm32f411-blackpill.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
ARCH?=ARM
TARGET?=stm32f4
SIGN?=ED25519
HASH?=SHA256
VTOR?=1
SPMATH?=1
WOLFBOOT_ORIGIN?=0x08000000
WOLFBOOT_PARTITION_SIZE?=0x20000
WOLFBOOT_SECTOR_SIZE?=0x20000
WOLFBOOT_PARTITION_BOOT_ADDRESS?=0x08020000
WOLFBOOT_PARTITION_UPDATE_ADDRESS?=0x08040000
WOLFBOOT_PARTITION_SWAP_ADDRESS?=0x08060000
CLOCK_SPEED?=84000000
STM32_PLLM?=25
STM32_PLLN?=336
STM32_PLLP?=4
STM32_PLLQ?=7
USE_UART1?=1
UART_FLASH?=1
DEBUG?=1
STM32?=1
10 changes: 5 additions & 5 deletions hal/stm32f4.c
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,11 @@ static void clock_pll_on(int powersave)
APB1_CLOCK_ER |= PWR_APB1_CLOCK_ER_VAL;

/* Select clock parameters (CPU Speed = 168MHz) */
cpu_freq = 168000000; (void)cpu_freq; /* not used */
pllm = 8;
plln = 336;
pllp = 2;
pllq = 7;
cpu_freq = CLOCK_SPEED; (void)cpu_freq; /* not used */
pllm = STM32_PLLM;
plln = STM32_PLLN;
pllp = STM32_PLLP;
pllq = STM32_PLLQ;
pllr = 0; (void)pllr; /* not used */
hpre = RCC_PRESCALER_DIV_NONE;
ppre1 = RCC_PRESCALER_DIV_4;
Expand Down
149 changes: 100 additions & 49 deletions hal/uart/uart_drv_stm32f4.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,11 @@

#include <stdint.h>

/* Driver hardcoded to work on UART3 (PD8/PD9) */
#define UART3 (0x40004800)
#define UART3_PIN_AF 7
#define UART3_RX_PIN 9
#define UART3_TX_PIN 8

#define UART3_SR (*(volatile uint32_t *)(UART3))
#define UART3_DR (*(volatile uint32_t *)(UART3 + 0x04))
#define UART3_BRR (*(volatile uint32_t *)(UART3 + 0x08))
#define UART3_CR1 (*(volatile uint32_t *)(UART3 + 0x0c))
#define UART3_CR2 (*(volatile uint32_t *)(UART3 + 0x10))

/* Common UART Config */
#if !defined(USE_UART1) && !defined(USE_UART3)
#define USE_UART3
#endif
#define UART_PIN_AF 7
#define UART_CR1_UART_ENABLE (1 << 13)
#define UART_CR1_SYMBOL_LEN (1 << 12)
#define UART_CR1_PARITY_ENABLED (1 << 10)
Expand All @@ -51,52 +44,110 @@
#define UART_SR_TX_EMPTY (1 << 7)
#define UART_SR_RX_NOTEMPTY (1 << 5)


#ifndef CLOCK_SPEED
#define CLOCK_SPEED (168000000)
#endif

/* Common GPIO Config */
#define GPIO_MODE_AF (2)

#define APB1_CLOCK_ER (*(volatile uint32_t *)(0x40023840))
#define UART3_APB1_CLOCK_ER_VAL (1 << 18)
/* UART1 Config */
#ifdef USE_UART1
#define UART_RX_PIN 7
#define UART_TX_PIN 6

#define UART1 (0x40011000)
#define UART_SR (*(volatile uint32_t *)(UART1))
#define UART_DR (*(volatile uint32_t *)(UART1 + 0x04))
#define UART_BRR (*(volatile uint32_t *)(UART1 + 0x08))
#define UART_CR1 (*(volatile uint32_t *)(UART1 + 0x0c))
#define UART_CR2 (*(volatile uint32_t *)(UART1 + 0x10))

#define UART_CLOCK_ER (*(volatile uint32_t *)(0x40023844))
#define UART_CLOCK_ER_VAL (1 << 4)

#define GPIO_CLOCK_ER (*(volatile uint32_t *)(0x40023830))
#define GPIO_CLOCK_ER_VAL (1 << 1)
#define GPIOB_BASE 0x40020400
#define GPIO_MODE (*(volatile uint32_t *)(GPIOB_BASE + 0x00))
#define GPIO_AFL (*(volatile uint32_t *)(GPIOB_BASE + 0x20))
#define GPIO_AFH (*(volatile uint32_t *)(GPIOB_BASE + 0x24))
#endif

/* UART3 Config */
#ifdef USE_UART3
#define UART_RX_PIN 9
#define UART_TX_PIN 8

#define AHB1_CLOCK_ER (*(volatile uint32_t *)(0x40023830))
#define GPIOD_AHB1_CLOCK_ER (1 << 3)
#define UART3 (0x40004800)
#define UART_SR (*(volatile uint32_t *)(UART3))
#define UART_DR (*(volatile uint32_t *)(UART3 + 0x04))
#define UART_BRR (*(volatile uint32_t *)(UART3 + 0x08))
#define UART_CR1 (*(volatile uint32_t *)(UART3 + 0x0c))
#define UART_CR2 (*(volatile uint32_t *)(UART3 + 0x10))

#define UART_CLOCK_ER (*(volatile uint32_t *)(0x40023840))
#define UART_CLOCK_ER_VAL (1 << 18)

#define GPIO_CLOCK_ER (*(volatile uint32_t *)(0x40023830))
#define GPIO_CLOCK_ER_VAL (1 << 3)
#define GPIOD_BASE 0x40020c00
#define GPIOD_MODE (*(volatile uint32_t *)(GPIOD_BASE + 0x00))
#define GPIOD_AFL (*(volatile uint32_t *)(GPIOD_BASE + 0x20))
#define GPIOD_AFH (*(volatile uint32_t *)(GPIOD_BASE + 0x24))
#define GPIO_MODE_AF (2)
#define GPIO_MODE (*(volatile uint32_t *)(GPIOD_BASE + 0x00))
#define GPIO_AFL (*(volatile uint32_t *)(GPIOD_BASE + 0x20))
#define GPIO_AFH (*(volatile uint32_t *)(GPIOD_BASE + 0x24))
#endif


static void uart_pins_setup(void)
{
uint32_t reg;
AHB1_CLOCK_ER |= GPIOD_AHB1_CLOCK_ER;
GPIO_CLOCK_ER |= GPIO_CLOCK_ER_VAL;
/* Set mode = AF */
reg = GPIOD_MODE & ~ (0x03 << (UART3_RX_PIN * 2));
GPIOD_MODE = reg | (2 << (UART3_RX_PIN * 2));
reg = GPIOD_MODE & ~ (0x03 << (UART3_TX_PIN * 2));
GPIOD_MODE = reg | (2 << (UART3_TX_PIN * 2));

/* Alternate function: use high pins (8 and 9) */
reg = GPIOD_AFH & ~(0xf << ((UART3_TX_PIN - 8) * 4));
GPIOD_AFH = reg | (UART3_PIN_AF << ((UART3_TX_PIN - 8) * 4));
reg = GPIOD_AFH & ~(0xf << ((UART3_RX_PIN - 8) * 4));
GPIOD_AFH = reg | (UART3_PIN_AF << ((UART3_RX_PIN - 8) * 4));
reg = GPIO_MODE & ~ (0x03 << (UART_RX_PIN * 2));
GPIO_MODE = reg | (2 << (UART_RX_PIN * 2));
reg = GPIO_MODE & ~ (0x03 << (UART_TX_PIN * 2));
GPIO_MODE = reg | (2 << (UART_TX_PIN * 2));

/* The alternate function register is split across two 32bit
* registers (AFL, AFH). AFL covers pins 0 through 7, and
* AFH covers pins 8 through 15. The code below determines
* which register to use at compile time based on the chosen
* pin number
*/

#if UART_TX_PIN > 7
reg = GPIO_AFH & ~(0xf << ((UART_TX_PIN - 8) * 4));
GPIO_AFH = reg | (UART_PIN_AF << ((UART_TX_PIN - 8) * 4));
#else
reg = GPIO_AFL & ~(0xf << (UART_TX_PIN * 4));
GPIO_AFL = reg | (UART_PIN_AF << (UART_TX_PIN * 4));
#endif

#if UART_RX_PIN > 7
reg = GPIO_AFH & ~(0xf << ((UART_RX_PIN - 8) * 4));
GPIO_AFH = reg | (UART_PIN_AF << ((UART_RX_PIN - 8) * 4));
#else
reg = GPIO_AFL & ~(0xf << (UART_RX_PIN * 4));
GPIO_AFL = reg | (UART_PIN_AF << (UART_RX_PIN * 4));
#endif

}

int uart_tx(const uint8_t c)
{
uint32_t reg;
do {
reg = UART3_SR;
reg = UART_SR;
} while ((reg & UART_SR_TX_EMPTY) == 0);
UART3_DR = c;
UART_DR = c;
return 1;
}

int uart_rx(uint8_t *c)
{
volatile uint32_t reg = UART3_SR;
volatile uint32_t reg = UART_SR;
if ((reg & UART_SR_RX_NOTEMPTY) != 0) {
reg = UART3_DR;
reg = UART_DR;
*c = (uint8_t)(reg & 0xff);
return 1;
}
Expand All @@ -109,42 +160,42 @@ int uart_init(uint32_t bitrate, uint8_t data, char parity, uint8_t stop)
/* Enable pins and configure for AF7 */
uart_pins_setup();
/* Turn on the device */
APB1_CLOCK_ER |= UART3_APB1_CLOCK_ER_VAL;
UART3_CR1 &= ~(UART_CR1_UART_ENABLE);
UART_CLOCK_ER |= UART_CLOCK_ER_VAL;
UART_CR1 &= ~(UART_CR1_UART_ENABLE);

/* Configure for TX + RX */
UART3_CR1 |= (UART_CR1_TX_ENABLE | UART_CR1_RX_ENABLE);
UART_CR1 |= (UART_CR1_TX_ENABLE | UART_CR1_RX_ENABLE);

/* Configure clock */
UART3_BRR = CLOCK_SPEED / bitrate;
UART_BRR = CLOCK_SPEED / bitrate;

/* Configure data bits */
if (data == 8)
UART3_CR1 &= ~UART_CR1_SYMBOL_LEN;
UART_CR1 &= ~UART_CR1_SYMBOL_LEN;
else
UART3_CR1 |= UART_CR1_SYMBOL_LEN;
UART_CR1 |= UART_CR1_SYMBOL_LEN;

/* Configure parity */
switch (parity) {
case 'O':
UART3_CR1 |= UART_CR1_PARITY_ODD;
UART_CR1 |= UART_CR1_PARITY_ODD;
/* fall through to enable parity */
/* FALL THROUGH */
case 'E':
UART3_CR1 |= UART_CR1_PARITY_ENABLED;
UART_CR1 |= UART_CR1_PARITY_ENABLED;
break;
default:
UART3_CR1 &= ~(UART_CR1_PARITY_ENABLED | UART_CR1_PARITY_ODD);
UART_CR1 &= ~(UART_CR1_PARITY_ENABLED | UART_CR1_PARITY_ODD);
}
/* Set stop bits */
reg = UART3_CR2 & ~UART_CR2_STOPBITS;
reg = UART_CR2 & ~UART_CR2_STOPBITS;
if (stop > 1)
UART3_CR2 = reg & (2 << 12);
UART_CR2 = reg & (2 << 12);
else
UART3_CR2 = reg;
UART_CR2 = reg;

/* Turn on uart */
UART3_CR1 |= UART_CR1_UART_ENABLE;
UART_CR1 |= UART_CR1_UART_ENABLE;
return 0;
}

Expand Down
28 changes: 28 additions & 0 deletions options.mk
Original file line number Diff line number Diff line change
Expand Up @@ -948,3 +948,31 @@ ifneq ($(CERT_CHAIN_VERIFY),)
endif
SIGN_OPTIONS += --cert-chain $(CERT_CHAIN_FILE)
endif

# Clock Speed (Hz)
ifneq ($(CLOCK_SPEED),)
CFLAGS += -DCLOCK_SPEED=$(CLOCK_SPEED)
endif

# STM32F4 clock options
ifneq ($(STM32_PLLM),)
CFLAGS += -DSTM32_PLLM=$(STM32_PLLM)
endif
ifneq ($(STM32_PLLN),)
CFLAGS += -DSTM32_PLLN=$(STM32_PLLN)
endif
ifneq ($(STM32_PLLP),)
CFLAGS += -DSTM32_PLLP=$(STM32_PLLP)
endif
ifneq ($(STM32_PLLQ),)
CFLAGS += -DSTM32_PLLQ=$(STM32_PLLQ)
endif

# STM32 UART options
ifeq ($(USE_UART1),1)
CFLAGS += -DUSE_UART1=1
endif

ifeq ($(USE_UART3),1)
CFLAGS += -DUSE_UART3=1
endif
Loading