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 .claude/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"permissions": {
"allow": [
"Bash(*)"
]
Comment thread
gabrielfrasantos marked this conversation as resolved.
}
}
Comment thread
gabrielfrasantos marked this conversation as resolved.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ out/
megalinter-reports/
install/
.megalinter_github_conf
.claude/scheduled_tasks.lock
1 change: 1 addition & 0 deletions .ls-lint.yml
Comment thread
gabrielfrasantos marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ ls:
.cpp: PascalCase

ignore:
- .claude
- .devcontainer
- .git
- .github
Expand Down
72 changes: 72 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# hal-st — Agent Rules (canonical)

Single source of truth for **Claude, Copilot, and sub-agents**. `CLAUDE.md` points here. Detailed C++ coding rules: `.github/instructions/hal-st-cpp.instructions.md` (binding for all `*.hpp/*.cpp/*.h/*.c` changes). Copilot custom agents: `.github/agents/`. Build presets: `CMakePresets.json`.

hal-st is a Hardware Abstraction Layer for ST ARM Cortex-M microcontrollers (F4, F7, G0, G4, H5, WB, WBA families), implementing [embedded-infra-lib](https://github.com/embedded-pro/embedded-infra-lib) HAL interfaces over the STM32 HAL/LL library. It's a copy of [philips-software/amp-hal-st](https://github.com/philips-software/amp-hal-st).

## Architecture

- `hal_st/cortex/` — ARM Cortex-M core (`InterruptCortex`, `DataWatchpointAndTrace`)
- `hal_st/stm32fxxx/` — STM32 peripheral drivers (Uart, Can, Spi, Adc, Gpio, Dma, Timer, Flash, Ethernet, USB, …), split into `ip/` (peripheral IP blocks) and `mcu/` (family wiring)
- `hal_st/synchronous_stm32fxxx/` — Blocking driver variants (`SynchronousUart`, `SynchronousSpiMaster`, …)
- `hal_st/instantiations/` — Board event infrastructure (`StmEventInfrastructure`, `NucleoUi`, `DiscoveryUi`)
- `hal_st/default_init/` — Startup code and atomics shim
- `hal_st/middlewares/` — `STM32_WPAN`, `ble_middleware`
- `hal_st_lwip/` — lwIP network stack instantiations
- `st/` — CMSIS headers, STM32 HAL driver sources (per family), `hal_conf/`, `ldscripts/`
- `services/st_util/` — ST bootloader communicator services
- `integration_test/` — hardware-in-the-loop cucumber test rig (`pcb/`, `flasher/`, `tester/`, `tested/`, `runner/`, `logic/`)
- `examples/` — `blink`, `helloworld`, `sesame`, `freertos`

## Memory — no heap

This is a driver library that always ends up running on constrained MCUs. Forbidden everywhere: `new`/`delete`/`malloc`/`free`, `make_unique`/`make_shared`, `std::vector`/`string`/`deque`/`list`/`map`/`set`. No recursion in driver code — stack depth must be statically bounded.

Use: `infra::BoundedVector<T>`, `infra::BoundedString`, `infra::BoundedDeque<T>`, `infra::MemoryRange<T>` (buffer params, not raw pointer+size), `std::array<T,N>`, `std::optional<T>`.

## STM32 HAL/LL & driver conventions

Full detail lives in `.github/instructions/hal-st-cpp.instructions.md` — read it before touching driver code. Key points:

- `HAL_*`/`LL_*` only; never write to registers via magic offsets
- `HAL_FOO_Init` in constructor, `HAL_FOO_DeInit` + clock disable in destructor (RAII)
- Interrupt handlers: `private InterruptHandler` (single-vector) or `DispatchedInterruptHandler` (multi-vector, one member per vector); never call `NVIC_EnableIRQ` directly
- Every alternate-function pin: a `PeripheralPinStm` member, declared in constructor-init order
- Every driver: inner `Config` struct with mandatory `constexpr Config() {}` and sensible field defaults
- `oneBasedIndex` convention for peripheral indices; `really_assert` bounds; table access as `table[oneBasedIndex - 1]`
- `HAS_PERIPHERAL_xxx` guards come from generated `PeripheralTable.hpp` — never hand-edit anything under `generated/`
- DMA: `DMA_STREAM_BASED` (F4/F7) vs `DMA_CHANNEL_BASED` (G0/G4/WB/WBA/H5) — use `hal_st` DMA wrappers, not raw HAL DMA handles
- Naming: `FooStm` drivers, `SynchronousFooStm` blocking variants

## Style

- Allman braces, 4-space indent, `.clang-format` authoritative
- PascalCase types/methods, camelCase members/locals; `const`-correct on all observer/query methods
- `#pragma once` for new/modified headers; legacy `#ifndef` guards may stay untouched
- No C-style casts — `static_cast<>`; `reinterpret_cast<>` only where the HAL requires register/void-pointer casts
- **No comments** except non-obvious *why*. No `TODO`/`FIXME`/`HACK`, no commented-out code

## Interfaces & errors

- Interfaces = pure virtual; `virtual ~I() = default` — **never** `= 0` destructors
- No exceptions. `std::optional<T>` or status enums. `really_assert()` for preconditions
- No global mutable state — all state lives in driver class members

## Testing

No unit tests in this repo. hal-st is validated by manual testing on Nucleo/Discovery boards, logic-analyser/scope verification, and the `integration_test/` hardware-in-the-loop rig — not by GoogleTest suites. Don't add unit tests for new or changed drivers. (`services/st_util/test/` is a pre-existing exception gated behind `HALST_BUILD_TESTS`; leave it as-is, don't extend the pattern elsewhere.)

## Build

```bash
cmake --preset host && cmake --build --preset host-Debug # host tooling/build check
cmake --preset stm32f407 && cmake --build --preset stm32f407-RelWithDebInfo # embedded target
```

Other target presets: `stm32wb55`, `stm32g070`, `stm32g431`, `stm32f429`, `stm32f746`, `stm32f767`, `stm32g474`, `stm32wba52`, `stm32wba65`, `stm32h563`, `stm32h573`.

## Assistant behavior — be terse

- Minimal prose. No preamble/postamble, no restating the plan, no summaries unless asked
- Report results as file paths + build pass/fail (no test suite to report)
- Don't re-read files already read; batch reads; prefer targeted edits
12 changes: 12 additions & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
hal-st — Claude Instructions
Canonical rules: AGENTS.md (shared with Copilot and sub-agents). C++ coding detail: .github/instructions/hal-st-cpp.instructions.md. Copilot agents: .github/agents/. Build presets: CMakePresets.json.

Essentials (full detail in AGENTS.md):

No heap — bounded containers / std::array / std::optional; no recursion in driver code. Applies repo-wide (this is an MCU HAL library).
STM32 HAL/LL — HAL_*/LL_* only, never raw registers; HAL_FOO_Init/DeInit in ctor/dtor (RAII); InterruptHandler/DispatchedInterruptHandler, never NVIC_EnableIRQ directly; PeripheralPinStm for AF pins; DMA_STREAM_BASED vs DMA_CHANNEL_BASED wrappers.
Driver Config — inner Config struct, mandatory constexpr Config() {}, oneBasedIndex convention, HAS_PERIPHERAL_xxx guards from generated PeripheralTable.hpp (never hand-edit generated/).
Style — Allman braces, 4-space, PascalCase types/methods, camelCase members. No comments except non-obvious why.
No tests — hal-st has no unit test suite; validation is on real hardware (Nucleo/Discovery, logic analyser) and integration_test/. Don't add unit tests for driver changes.
No exceptions — std::optional/status enums; interfaces virtual ~I() = default.
Be terse — minimal prose; report file paths + build pass/fail.
9 changes: 8 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,21 @@ if (HALST_STANDALONE)
FetchContent_Declare(
emil
GIT_REPOSITORY https://github.com/embedded-pro/embedded-infra-lib.git
GIT_TAG b1056e80ab5376d7e4ee821c5b3c5680bc01f751 # Unreleased
GIT_TAG 321a369d9ef75a41b212c38578aba774bd47bfb0 # Unreleased
)

add_definitions(-DEMIL_ENABLE_TRACING=1)

set(EMIL_ENABLE_DOCKER_TOOLS Off CACHE BOOL "" FORCE)
set(EMIL_BUILD_ECHO_COMPILERS On CACHE BOOL "" FORCE)

# hal.cortex_m.runtime contains ARM-only inline assembly; only build it when actually
# cross-compiling for an ST target, not for the host toolchain (TARGET_MCU_VENDOR is
# unset there).
if (TARGET_MCU_VENDOR STREQUAL st)
set(EMIL_BUILD_CORTEX_M On CACHE BOOL "" FORCE)
endif()

FetchContent_MakeAvailable(emil)

if (EMIL_HOST_BUILD)
Expand Down
4 changes: 2 additions & 2 deletions examples/blink/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ target_link_libraries(examples_st.blink_nucleo144 PRIVATE
)

halst_target_default_linker_scripts(examples_st.blink_nucleo144)
halst_target_default_init(examples_st.blink_nucleo144)
halst_target_bringup(examples_st.blink_nucleo144)

emil_generate_artifacts(TARGET examples_st.blink_nucleo144 LST MAP BIN HEX)

Expand Down Expand Up @@ -48,6 +48,6 @@ target_link_libraries(examples_st.blink_nucleo64 PRIVATE
)

halst_target_default_linker_scripts(examples_st.blink_nucleo64)
halst_target_default_init(examples_st.blink_nucleo64)
halst_target_bringup(examples_st.blink_nucleo64)

emil_generate_artifacts(TARGET examples_st.blink_nucleo64 LST MAP BIN HEX)
2 changes: 1 addition & 1 deletion examples/freertos/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ target_link_libraries(examples_st.freertos_nucleo144 PRIVATE
)

halst_target_default_linker_scripts(examples_st.freertos_nucleo144)
halst_target_default_init(examples_st.freertos_nucleo144)
halst_target_bringup(examples_st.freertos_nucleo144)

emil_generate_artifacts(TARGET examples_st.freertos_nucleo144 LST MAP BIN HEX)
2 changes: 1 addition & 1 deletion examples/freertos/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ int main()
// Configure your clock here
// ConfigureDefaultClockNucleo767ZI();

static hal::InterruptTable::WithStorage<128> interruptTable;
static hal::cortex::InterruptTable::WithStorage<128> interruptTable;
static hal::GpioStm gpio{ hal::pinoutTableDefaultStm, hal::analogTableDefaultStm };
static hal::TimerServiceFreeRtos timerService;
static hal::LowPowerStrategyFreeRtos lowPowerStrategy;
Expand Down
4 changes: 2 additions & 2 deletions examples/helloworld/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ target_link_libraries(examples_st.helloworld_nucleo64 PRIVATE
)

halst_target_default_linker_scripts(examples_st.helloworld_nucleo64)
halst_target_default_init(examples_st.helloworld_nucleo64)
halst_target_bringup(examples_st.helloworld_nucleo64)

emil_generate_artifacts(TARGET examples_st.helloworld_nucleo64 LST MAP BIN HEX)

Expand All @@ -45,6 +45,6 @@ target_link_libraries(examples_st.helloworld_nucleo144 PRIVATE
)

halst_target_default_linker_scripts(examples_st.helloworld_nucleo144)
halst_target_default_init(examples_st.helloworld_nucleo144)
halst_target_bringup(examples_st.helloworld_nucleo144)

emil_generate_artifacts(TARGET examples_st.helloworld_nucleo144 LST MAP BIN HEX)
2 changes: 1 addition & 1 deletion examples/sesame/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ macro(add_sesame_example target_name)
)

halst_target_default_linker_scripts(${target_name})
halst_target_default_init(${target_name})
halst_target_bringup(${target_name})

emil_generate_artifacts(TARGET ${target_name} HEX)

Expand Down
3 changes: 1 addition & 2 deletions hal_st/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
add_subdirectory(cortex)
add_subdirectory(stm32fxxx)
add_subdirectory(synchronous_stm32fxxx)
add_subdirectory(middlewares)
add_subdirectory(instantiations)
add_subdirectory(default_init)
add_subdirectory(bringup)
15 changes: 15 additions & 0 deletions hal_st/bringup/Assert.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <cstdint>
#include <cstdlib>

extern "C"
{
void __assert_func(const char*, int, const char*, const char*)
{
std::abort();
}

void assert_failed(uint8_t* file, uint32_t line)
{
std::abort();
}
}
16 changes: 16 additions & 0 deletions hal_st/bringup/Bringup.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include DEVICE_HEADER
#include "hal/cortex_m/InterruptCortex.hpp"

extern "C"
{
// Avoid the SysTick handler from being initialised by HAL_Init
HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
{
return HAL_OK;
}

[[gnu::weak]] void Default_Handler_Forwarded()
{
hal::cortex::InterruptTable::Instance().Invoke(hal::cortex::ActiveInterrupt());
}
}
47 changes: 47 additions & 0 deletions hal_st/bringup/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
add_library(hal_st.bringup STATIC)
emil_build_for(hal_st.bringup TARGET_MCU_VENDOR st PREREQUISITE_BOOL HALST_STANDALONE)

target_include_directories(hal_st.bringup PUBLIC
"$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/../..>"
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
)

target_link_libraries(hal_st.bringup PUBLIC
st.hal_driver
hal.cortex_m
)

# Assembler does not understand -Werror
set_target_properties(hal_st.bringup PROPERTIES COMPILE_WARNING_AS_ERROR Off)

target_sources(hal_st.bringup PRIVATE
Assert.cpp
Bringup.cpp
)

if (TARGET_MCU_VENDOR STREQUAL st)
get_target_property(startup_source st.hal_driver_${TARGET_MCU_FAMILY} HALST_STARTUP_SOURCE)

target_sources(hal_st.bringup PRIVATE
${startup_source}
)
endif()

function(halst_target_bringup target)
# hal.cortex_m's FaultTracer defines HardFault_Handler etc. as weak symbols, same as the
# vendor startup file's ".thumb_set HardFault_Handler,Default_Handler" alias; whichever
# definition the linker sees first for a given weak symbol wins, so hal.cortex_m's objects
# are listed ahead of hal_st.bringup's (which carries the startup object) to make
# FaultTracer's fault handlers win deterministically instead of by accident.
#
# hal.cortex_m.runtime (Atomic/DefaultInit/SysCallStubs) supplies abort(), _sbrk, and the
# __atomic_* shims as weak symbols; per its own design, these must reach the final ELF as
# loose objects rather than archive members, because an archived weak definition is only
# extracted once something already references it, which is too late to beat what newlib
# pulls out of libg_nano.a.
target_link_libraries(${target} PUBLIC
$<TARGET_OBJECTS:hal.cortex_m>
$<TARGET_OBJECTS:hal_st.bringup>
$<TARGET_OBJECTS:hal.cortex_m.runtime>
)
endfunction()
19 changes: 0 additions & 19 deletions hal_st/cortex/CMakeLists.txt

This file was deleted.

32 changes: 0 additions & 32 deletions hal_st/cortex/DataWatchpointAndTrace.cpp

This file was deleted.

22 changes: 0 additions & 22 deletions hal_st/cortex/DataWatchpointAndTrace.hpp

This file was deleted.

Loading
Loading