A modern C++17 Hardware Abstraction Layer for STM32 microcontrollers with zero-overhead abstractions.
面向 STM32 微控制器的现代 C++17 硬件抽象层,零开销抽象。
- Compile-time peripheral configuration — constexpr templates 编译时外设配置 — constexpr 模板
- Type-safe GPIO, UART, SPI, I2C, ADC, PWM — compile-time pin validation 类型安全的 GPIO/UART/SPI/I2C/ADC/PWM — 编译时引脚校验
- DMA support — with callback mechanism DMA 支持 — 带回调机制
- Interrupt-driven UART — with lock-free ring buffer 中断驱动 UART — 配无锁环形缓冲区
- I2C device framework — sensors, EEPROM, RTC I2C 设备框架 — 传感器、EEPROM、RTC
- SPI device framework — displays, flash memory SPI 设备框架 — 显示屏、Flash 存储
- Zero runtime overhead — all resolved at compile time 零运行时开销 — 编译时全部解析
// Compile-time pin configuration / 编译时引脚配置
using LED = Gpio<Port::A, 5, Mode::Output>;
using Button = Gpio<Port::C, 13, Mode::Input, Pull::Up>;
// Usage — no runtime overhead / 使用 — 零运行时开销
LED::init();
LED::set();
if (Button::read()) { /* ... */ }
// Type-safe UART / 类型安全的 UART
Uart2::init(115200);
Uart2::send("Hello!\r\n");
while (Uart2::available()) {
uint8_t byte = Uart2::read();
}embedded-hal-stm32/
├── include/
│ ├── gpio.h # GPIO driver / GPIO驱动
│ ├── uart.h # UART driver / UART驱动
│ ├── spi.h # SPI driver / SPI驱动
│ ├── i2c.h # I2C driver / I2C驱动
│ ├── adc.h # ADC driver / ADC驱动
│ ├── pwm.h # PWM driver / PWM驱动
│ └── ring_buffer.h # Lock-free ring buffer / 无锁环形缓冲区
├── examples/
│ ├── blink/main.cpp # LED blink / LED闪烁
│ └── uart_echo/main.cpp # UART echo / UART回显
├── CMakeLists.txt # Build system / 构建系统
└── README.md
This library requires the STM32 CMSIS headers for register definitions. 本库需要 STM32 CMSIS 头文件来定义寄存器。
# Option 1: Install STM32CubeF4 / 安装 STM32CubeF4
# Download from: https://github.com/STMicroelectronics/STM32CubeF4
# Then add to CMAKE_PREFIX_PATH
# Option 2: Use STM32CubeMX to generate project / 使用STM32CubeMX生成项目
# The generated project already includes CMSIS headersNote / 注意: The code uses
stm32f4xx.h(CMSIS) for register access. You need STM32CubeF4 or equivalent CMSIS pack installed. 代码使用stm32f4xx.h(CMSIS)进行寄存器访问,需要安装 STM32CubeF4 或等效的 CMSIS 包。
# Build blink example / 编译闪烁示例
mkdir build && cd build
cmake -DCMAKE_TOOLCHAIN_FILE=toolchain-arm.cmake ..
make blink
# Flash / 烧录
st-flash write blink.bin 0x08000000MIT License
Isaac — Diploma in Electronic Engineering, TAR UMT
Built with ❤️ for learning embedded C++ abstractions 用 ❤️ 构建,用于学习嵌入式C++抽象