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
7 changes: 7 additions & 0 deletions .github/workflows/test-configs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1614,6 +1614,13 @@ jobs:
config-file: ./config/examples/stm32wb.config
make-args: WOLFHAL=1 BOARD=stm32wb_nucleo UART_FLASH=1

stm32wb_wolfhal_uart_flash_encryption_test:
uses: ./.github/workflows/test-build.yml
with:
arch: arm
config-file: ./config/examples/stm32wb-uart-flash-encryption.config
make-args: WOLFHAL=1 BOARD=stm32wb_nucleo

# SPI_FLASH=1 variant: ext_flash_* is mapped to src/spi_flash.c in
# include/hal.h, so hal/wolfhal.c must not build its own.
stm32wb_wolfhal_spi_flash_test:
Expand Down
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -264,14 +264,14 @@ ifeq ($(WOLFHAL),1)
ifeq ($(strip $(BOARD)),)
$(error WOLFHAL=1 requires BOARD to be set, e.g. BOARD=stm32wb_nucleo)
endif
ifeq ($(wildcard hal/boards/$(BOARD)/board.mk),)
$(error BOARD=$(BOARD) has no hal/boards/$(BOARD)/board.mk)
ifeq ($(wildcard $(BOARD_DIR)/board.mk),)
$(error BOARD=$(BOARD) has no $(BOARD_DIR)/board.mk)
endif
# wolfHAL backend: hal/wolfhal.o replaces hal/$(TARGET).o above. The
# board's wolfHAL_board.c provides hal_init/hal_prepare_boot and the wolfHAL
# device handles; board.mk pulls in chip drivers.
OBJS+=./hal/boards/$(BOARD)/wolfHAL_board.o
include hal/boards/$(BOARD)/board.mk
OBJS+=$(BOARD_DIR)/wolfHAL_board.o
include $(BOARD_DIR)/board.mk
endif

# Set only here, so that host-tool sub-makes which also include options.mk
Expand Down
4 changes: 3 additions & 1 deletion arch.mk
Original file line number Diff line number Diff line change
Expand Up @@ -1375,7 +1375,9 @@ endif

ifeq ($(WOLFHAL),1)
WOLFHAL_ROOT?=$(WOLFBOOT_ROOT)/lib/wolfHAL
CFLAGS+=-I$(WOLFHAL_ROOT) -Ihal/boards/$(BOARD)
BOARD_DIR?=hal/boards/$(BOARD)
override BOARD_DIR:=$(if $(filter /%,$(BOARD_DIR)),$(BOARD_DIR),$(WOLFBOOT_ROOT)/$(BOARD_DIR))
CFLAGS+=-DWOLFHAL -I$(WOLFHAL_ROOT) -I$(BOARD_DIR)
endif


Expand Down
159 changes: 154 additions & 5 deletions docs/wolfHAL.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ config/examples/<name>.config
└─ TARGET=<chip> BOARD=<board> WOLFHAL=1

arch.mk
└─ Sets WOLFHAL_ROOT, CFLAGS += -Ihal/boards/$(BOARD)
└─ Sets WOLFHAL_ROOT, BOARD_DIR, CFLAGS += -I$(BOARD_DIR)

Makefile
└─ OBJS += hal/wolfhal.o (replaces hal/$(TARGET).o)
└─ OBJS += hal/boards/$(BOARD)/wolfHAL_board.o
└─ include hal/boards/$(BOARD)/board.mk
└─ OBJS += $(BOARD_DIR)/wolfHAL_board.o
└─ include $(BOARD_DIR)/board.mk

hal/wolfhal.c (generic — calls whal_Flash_Write, whal_Uart_Send, etc.)
└─ #include "wolfHAL_board.h" (resolved via -I to the board directory)
Expand Down Expand Up @@ -87,14 +87,36 @@ WOLFHAL=1
- `TARGET` keeps its usual meaning (the chip family).
- `WOLFHAL=1` swaps the legacy `hal/$(TARGET).c` for the generic wolfHAL shim
(`hal/wolfhal.c`) and includes the per-board build pieces.
- `BOARD` selects the board directory under `hal/boards/`.
- `BOARD` names the board. By default its files are read from
`hal/boards/$(BOARD)/`.
- `BOARD_DIR` (optional) overrides the board directory, so board files can live
outside the wolfBoot tree. See [External Boards](#external-boards).

See `config/examples/stm32wba.config` and
`config/examples/versal2_vek385.config` for complete examples.

### External Boards

Set `BOARD_DIR` to a directory containing the same three files as an in-tree
board (`wolfHAL_board.h`, `wolfHAL_board.c` and `board.mk`):

```
make WOLFHAL=1 BOARD=my_board BOARD_DIR=/path/to/my_board
```

A relative `BOARD_DIR` is resolved against the wolfBoot root, for both the
bootloader and the test application. Notes for an external `board.mk`:

- Paths relative to the wolfBoot root, such as wolfBoot's own linker scripts
(`$(call LSCRIPT_IN_DEFAULT,hal/stm32wb.ld)`), still work. Refer to files
shipped with the board as `$(BOARD_DIR)/...`.
- `wolfHAL_board.o` is built inside `BOARD_DIR`. Pass the same `BOARD` and
`BOARD_DIR` to `make clean` to remove it.

## Adding a New Board

To add a new board, create a directory `hal/boards/<board_name>/` with three files:
To add a new board, create a directory `hal/boards/<board_name>/` (or an
external directory passed as `BOARD_DIR`) with three files:

### 1. `wolfHAL_board.h` — Driver Headers and Pin Enums

Expand Down Expand Up @@ -274,6 +296,133 @@ that are substituted at build time. When `RAM_CODE=1`, these expand to
flash into the `.data` section (loaded to RAM at startup). When `RAM_CODE` is not
set, all code remains in flash as normal.

## UART Flash

wolfHAL boards can place the update and swap partitions on a remote host over
UART with `UART_FLASH=1` (see [remote_flash.md](remote_flash.md)). The target
side uses the generic `src/uart_flash.c` protocol, and the host side is
`tools/uart-flash-server`.

### How It Works

`hal/wolfhal.c` provides the UART functions that `src/uart_flash.c` calls, on
top of the wolfHAL UART API and `BOARD_UART_FLASH_DEV`:

| Function | Behavior |
|---|---|
| `uart_init` | No-op; the board configures the UART in `hal_init`, so `UART_FLASH_BITRATE` is ignored and the board's baud rate setting applies |
| `uart_tx` | Sends one byte with `whal_Uart_Send`; returns `1` on success, `-1` on error |
| `uart_rx` | Receives one byte with `whal_Uart_Recv`; returns `1` on success, `0` on timeout |

With `WOLFHAL=1`, the build defines `WOLFHAL` and does not link the legacy
`hal/uart/uart_drv_<target>.c` driver.

On legacy targets, `uart_rx` returns immediately and `src/uart_flash.c` builds
its timeouts by calling it in a loop. With `WOLFHAL` defined, `uart_rx` waits
on the board's UART timeout instead, so `src/uart_flash.c` counts board
timeouts rather than loop iterations. Each ACK and each read byte get one board
timeout, and the final erase ACK gets `ERASE_TIMEOUT` (5) board timeouts.
While waiting for an ACK, bytes other than ACK are skipped rather than ending
the wait. `ext_flash_lock` and `ext_flash_unlock` also wait for an ACK that
`uart-flash-server` does not send, so each one costs a full timeout.

### Board Requirements

1. **`BOARD_UART_FLASH_DEV`.** Define it in `wolfHAL_board.h`. It can be the
debug UART:

```c
#define BOARD_UART_FLASH_DEV BOARD_UART_DEV
```

If it is the same UART as debug output, do not combine `UART_FLASH=1` with
`DEBUG_UART=1`, since log text would corrupt the protocol stream.
2. **UART enabled without `DEBUG_UART`.** Initialize the UART clocks, pins and
device in `hal_init` (and deinitialize them in `hal_prepare_boot`) when
either `DEBUG_UART` or `UART_FLASH` is set, and link the GPIO and UART
drivers in `board.mk` for both:

```makefile
ifneq (,$(filter 1,$(DEBUG_UART) $(UART_FLASH)))
WOLFHAL_OBJS+=$(WOLFHAL_ROOT)/src/gpio/<family>_gpio.o
WOLFHAL_OBJS+=$(WOLFHAL_ROOT)/src/uart/<family>_uart.o
endif
```
3. **Timeouts enabled.** Do not define `WHAL_CFG_NO_TIMEOUT` when
`UART_FLASH=1`:

```makefile
ifneq ($(UART_FLASH),1)
CFLAGS+=-DWHAL_CFG_NO_TIMEOUT
endif
```
4. **A UART timeout.** Point the UART config's `.timeout` at a `whal_Timeout`.
Without one, `whal_Uart_Recv` waits forever and boot hangs at the first
unanswered wait. The timeout bounds both `Send` and `Recv`, so it must be
longer than one byte time and long enough for the host to answer.

### Timeout Example

The `stm32wb_nucleo` board counts register polls rather than time, which needs
no timer peripheral. wolfHAL calls `GetTick` once per status register read, so
`timeoutTicks` is a poll count. 500000 polls matches the legacy loop count.

`wolfHAL_board.h`:

```c
#ifdef UART_FLASH
extern whal_Timeout g_wbUartTimeout;
#define BOARD_UART_TIMEOUT (&g_wbUartTimeout)
#else
#define BOARD_UART_TIMEOUT NULL
#endif

#define WHAL_CFG_STM32WB_UART_DEV { \
.base = WHAL_STM32WB55_UART1_BASE, \
.cfg = (void *)&(const whal_Stm32wb_Uart_Cfg){ \
.brr = WHAL_STM32WB_UART_BRR(64000000, 115200), \
.timeout = BOARD_UART_TIMEOUT, \
}, \
}
```

`wolfHAL_board.c`:

```c
#ifdef UART_FLASH
#define UART_FLASH_TIMEOUT_POLLS 500000

static uint32_t g_pollCount;

static uint32_t board_poll_tick(void)
{
return ++g_pollCount;
}

whal_Timeout g_wbUartTimeout = {
.timeoutTicks = UART_FLASH_TIMEOUT_POLLS,
.GetTick = board_poll_tick,
};
#endif
```

A poll count depends on CPU speed. A board that needs a timeout in real time
can provide a time-based `GetTick`, such as one driven by SysTick, instead.

### Building and Testing

```
cp config/examples/stm32wb-uart-flash-encryption.config .config
make WOLFHAL=1 BOARD=stm32wb_nucleo
```

To run an encrypted update over UART flash on a NUCLEO-WB55 board with
`st-flash` and `uart-flash-server`:

```
make test-enc-update WOLFHAL=1 BOARD=stm32wb_nucleo
```

## Test Application

The generic test application (`test-app/app_wolfhal.c`) demonstrates using wolfHAL
Expand Down
6 changes: 4 additions & 2 deletions hal/boards/stm32wb_nucleo/board.mk
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
ARCH_FLASH_OFFSET=0x08000000
$(call LSCRIPT_IN_DEFAULT,hal/stm32wb.ld)

CFLAGS+=-DWHAL_CFG_NO_TIMEOUT
ifneq ($(UART_FLASH),1)
CFLAGS+=-DWHAL_CFG_NO_TIMEOUT
endif

# Upstream wolfHAL drivers from lib/wolfHAL/src/. wolfBoot's hal_flash_*
# contract is satisfied by hal/wolfhal.c (added automatically because
Expand All @@ -16,7 +18,7 @@ CFLAGS+=-DWHAL_CFG_STM32WB_UART_SINGLE_INSTANCE

WOLFHAL_OBJS+=$(WOLFHAL_ROOT)/src/reg.o
WOLFHAL_OBJS+=$(WOLFHAL_ROOT)/src/flash/stm32wb_flash.o
ifeq ($(DEBUG_UART),1)
ifneq (,$(filter 1,$(DEBUG_UART) $(UART_FLASH)))
WOLFHAL_OBJS+=$(WOLFHAL_ROOT)/src/gpio/stm32wb_gpio.o
WOLFHAL_OBJS+=$(WOLFHAL_ROOT)/src/uart/stm32wb_uart.o
endif
Expand Down
23 changes: 20 additions & 3 deletions hal/boards/stm32wb_nucleo/wolfHAL_board.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,31 @@
#include "hal.h"
#include "wolfHAL_board.h"

#ifdef DEBUG_UART
#if defined(DEBUG_UART) || defined(UART_FLASH)
static const whal_Stm32wb_Rcc_PeriphClk g_periphClks[] = {
{WHAL_STM32WB55_GPIOB_GATE},
{WHAL_STM32WB55_UART1_GATE},
};
#define PERIPH_CLK_COUNT (sizeof(g_periphClks) / sizeof(g_periphClks[0]))
#endif

#ifdef UART_FLASH
#define UART_FLASH_TIMEOUT_POLLS 500000

static uint32_t g_pollCount;

/* One tick per register poll, so timeoutTicks counts polls. */
static uint32_t board_poll_tick(void)
{
return ++g_pollCount;
}

whal_Timeout g_wbUartTimeout = {
.timeoutTicks = UART_FLASH_TIMEOUT_POLLS,
.GetTick = board_poll_tick,
};
#endif

/* MSI 4 MHz -> PLL VCO 128 MHz -> PLLR /2 = 64 MHz -> SYSCLK */
static void pll_clock_on(void)
{
Expand All @@ -60,7 +77,7 @@ void hal_init(void)
pll_clock_on();
whal_Flash_Init(BOARD_FLASH_DEV);

#ifdef DEBUG_UART
#if defined(DEBUG_UART) || defined(UART_FLASH)
for (size_t i = 0; i < PERIPH_CLK_COUNT; i++) {
whal_Stm32wb_Rcc_EnablePeriphClk(&g_periphClks[i]);
}
Expand All @@ -72,7 +89,7 @@ void hal_init(void)

void hal_prepare_boot(void)
{
#ifdef DEBUG_UART
#if defined(DEBUG_UART) || defined(UART_FLASH)
whal_Uart_Deinit(BOARD_UART_DEV);
whal_Gpio_Deinit(BOARD_GPIO_DEV);

Expand Down
9 changes: 9 additions & 0 deletions hal/boards/stm32wb_nucleo/wolfHAL_board.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,16 @@ extern const whal_Uart whal_Stm32wb_Uart_Dev;
* without API churn. */
#define BOARD_GPIO_DEV WHAL_INTERNAL_DEV
#define BOARD_UART_DEV WHAL_INTERNAL_DEV
#define BOARD_UART_FLASH_DEV BOARD_UART_DEV
#define BOARD_FLASH_DEV WHAL_INTERNAL_DEV

#ifdef UART_FLASH
extern whal_Timeout g_wbUartTimeout;
#define BOARD_UART_TIMEOUT (&g_wbUartTimeout)
#else
#define BOARD_UART_TIMEOUT NULL
#endif

/* GPIO singleton initializer — instantiated by stm32wb_gpio.c. */
#define WHAL_CFG_STM32WB_GPIO_DEV { \
.base = WHAL_STM32WB55_GPIO_BASE, \
Expand Down Expand Up @@ -95,6 +103,7 @@ extern const whal_Uart whal_Stm32wb_Uart_Dev;
.base = WHAL_STM32WB55_UART1_BASE, \
.cfg = (void *)&(const whal_Stm32wb_Uart_Cfg){ \
.brr = WHAL_STM32WB_UART_BRR(64000000, 115200), \
.timeout = BOARD_UART_TIMEOUT, \
}, \
}

Expand Down
26 changes: 26 additions & 0 deletions hal/wolfhal.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

#include <stdint.h>
#include "hal.h"
#include "uart_drv.h"
#include "wolfboot/wolfboot.h"
#include "wolfHAL_board.h"

Expand Down Expand Up @@ -228,6 +229,31 @@ void ext_flash_unlock(void)
#endif /* BOARD_EXT_FLASH_DEV */
#endif /* EXT_FLASH */

#ifdef UART_FLASH
int uart_init(uint32_t bitrate, uint8_t data, char parity, uint8_t stop)
Comment thread
dgarske marked this conversation as resolved.
Resolved
{
(void)bitrate;
(void)data;
(void)parity;
(void)stop;
return 0;
}

int uart_tx(const uint8_t c)
{
if (whal_Uart_Send(BOARD_UART_FLASH_DEV, &c, 1) != WHAL_SUCCESS)
return -1;
return 1;
}

int uart_rx(uint8_t *c)
{
if (whal_Uart_Recv(BOARD_UART_FLASH_DEV, c, 1) != WHAL_SUCCESS)
return 0;
return 1;
}
#endif

#ifdef DEBUG_UART
#ifdef WOLFHAL_UART_CRLF
/* Send len bytes, with '\n' sent as "\r\n" */
Expand Down
4 changes: 3 additions & 1 deletion options.mk
Original file line number Diff line number Diff line change
Expand Up @@ -1255,7 +1255,9 @@ ifeq ($(EXT_FLASH),1)
ifeq ($(UART_FLASH),1)
CFLAGS+=-D"UART_FLASH=1"
OBJS+=src/uart_flash.o
WOLFCRYPT_OBJS+=hal/uart/uart_drv_$(UART_TARGET).o
ifneq ($(WOLFHAL),1)
WOLFCRYPT_OBJS+=hal/uart/uart_drv_$(UART_TARGET).o
endif
endif
endif

Expand Down
Loading
Loading