embedded stm32 hal
via PatrickJS/awesome-cursorrules
C/C++ rules for STM32 HAL, interrupts, DMA, and memory-constrained embedded systems.
What is embedded stm32 hal?
Guidance for embedded MCU development with STM32 HAL, covering peripheral initialization, interrupt handling, DMA configuration, and memory safety. Use this rule when writing firmware for STM32 microcontrollers or similar resource-constrained embedded systems.
- Enforce separation of generated vendor code from application logic and hardware abstraction patterns
- Establish interrupt safety practices: short ISRs, deferred work, critical sections, and explicit priority decisions
- Require DMA buffer ownership documentation and synchronization between DMA and CPU access
- Mandate timeout handling for hardware waits and avoid busy-wait loops
- Promote unit testing of pure logic on host and hardware-in-the-loop testing for peripherals
- Prevent common mistakes: heap allocation in ISRs, buffer sharing without synchronization, and unsafe peripheral reconfiguration
Applies to
File patterns this rule matches.
Rule definition (reference)
Source of truth, from the repository.
Embedded MCU, STM32, and HAL Rules
Project Structure
- Keep board support, drivers, middleware, application logic, and tests separate.
- Isolate generated CubeMX or vendor code from hand-written application code.
- Put hardware abstraction behind narrow interfaces so logic can be tested without hardware.
- Document clock tree, pin mappings, peripheral ownership, and interrupt priorities.
STM32 HAL and Peripherals
- Initialize peripherals in one place and avoid hidden reconfiguration.
- Check return values from HAL calls and handle timeout/error cases.
- Keep blocking HAL calls out of time-critical paths.
- Use DMA for high-throughput UART, SPI, I2C, ADC, or timer capture paths when appropriate.
- Document buffer ownership and lifetime for DMA operations.
- Use
volatileonly for memory shared with ISRs or hardware registers.
Interrupts and Concurrency
- Keep ISRs short and deterministic.
- Defer heavy work from interrupts to the main loop, RTOS task, or event queue.
- Protect shared data with critical sections, atomics, queues, or RTOS primitives.
- Avoid dynamic allocation in interrupts.
- Make interrupt priority decisions explicit.
Memory and Timing
- Avoid heap allocation in firmware unless the project explicitly allows it.
- Check stack usage for ISRs and RTOS tasks.
- Keep lookup tables
constso they can live in flash. - Use fixed-width integer types for hardware-facing code.
- Add timeouts for hardware waits.
- Treat watchdog configuration as part of application design, not a late add-on.
Testing and Debugging
- Unit test pure logic on host builds.
- Use hardware-in-the-loop tests for peripheral behavior.
- Add assertions for impossible hardware states in debug builds.
- Use SWD/JTAG, logic analyzers, and serial logs with rate limits.
- Keep fault handlers useful: capture reset reason, fault registers, and build version when possible.
Common Mistakes
- Do not modify generated files unless the workflow preserves changes.
- Do not busy-wait forever on hardware flags.
- Do not share buffers between DMA and CPU without synchronization.
- Do not assume peripheral reset state after low-power modes.
Related rules
Standardized engineering ticket templates with detailed requirements, acceptance criteria, and implementation guidance.
ES Module development guidelines for Node.js with modern syntax and best practices.

FastAPI best practices and patterns for building modern Python web APIs
4-layer FastAPI architecture with strict boundaries, typed protocols, bulkhead isolation, and domain exceptions.
Expert guidance for Flutter development with clean architecture, BLoC pattern, and Material 3 design.
Flutter development with MVVM, Riverpod state management, and Material Design.