unisat

ADR-007: Weak HAL shim + optional autodetect over full HAL bundling

Status: Accepted — 2026-04-17 Phase: 1 (STM32 target build) Commit: 2fb1847

Context

firmware/stm32/CMakeLists.txt needs to produce a linkable ARM .elf out of the box — a fresh git clone on a machine with arm-none-eabi-gcc installed should produce an artefact without any further setup. But every existing sensor driver (LIS3MDL, BME280, TMP117, …) calls into the STM32Cube HAL (HAL_I2C_Mem_Read, HAL_SPI_TransmitReceive, …) which lives in a separate ST-hosted repository (~500 MB). Three plausible strategies:

  1. Vendor-bundle full HAL. Ship the whole STM32CubeF4 tree under firmware/stm32/Drivers/STM32F4xx_HAL_Driver/ so it’s in every clone.
  2. Require user to run setup_stm32_hal.sh before first build. Fail early with a diagnostic if the HAL directory is missing.
  3. Weak shim + optional autodetect. Ship our own tiny hal_shim.c with weak stubs for every HAL_* function the drivers reference; autodetect the real HAL in CMake and drop the shim when found.

Option 1 bloats the repo by 15 MB + ties us to ST’s licence text. Option 2 is a worse user experience — cmake -B build && make should work on a fresh clone even if the answer is “boots to infinite loop with no peripherals”.

Decision

Option 3 — weak shim + autodetect.

firmware/stm32/Target/hal_shim.c provides a __attribute__((weak)) definition for every HAL entry point the firmware drivers reference (~30 functions across HAL_I2C_*, HAL_SPI_*, HAL_UART_*, HAL_ADC_*, HAL_TIM_Base_*, HAL_IWDG_*, HAL_GPIO_*). Each returns HAL_ERROR (or zero for HAL_GetTick fallback).

firmware/CMakeLists.txt probes firmware/stm32/Drivers/STM32F4xx_HAL_Driver/Src:

Rationale

Consequences

Positive:

Negative:

Alternatives considered

Implementation

firmware/stm32/Target/hal_shim.c         ← weak stubs
firmware/CMakeLists.txt:170              ← autodetect block:
    if(EXISTS "${HAL_DRIVER_DIR}/Src")
        link HAL_SOURCES, drop hal_shim
    else()
        link hal_shim, print reminder
scripts/setup_stm32_hal.sh               ← one-shot fetch

Tested by:

A symmetric pattern is used in ADR-008 for the FreeRTOS kernel (see scripts/setup_freertos.sh + 4b2309a).