embedded-stm32-hal — Cursor rule for STM32, HAL and DMA
A Cursor rule that keeps the agent inside embedded C/C++ conventions: STM32 HAL, interrupts, DMA and tight memory budgets.
.cursor/rules/embedded-stm32-hal.mdcWhat embedded-stm32-hal does
The file tells the agent how to behave in microcontroller code. Board support, drivers, middleware, application logic and tests stay apart, and generated CubeMX or vendor code is not mixed with hand-written sources. Hardware abstraction sits behind narrow interfaces so logic can be tested without a board.
Peripheral guidance is concrete: initialize in one place, check return values from HAL calls, handle timeouts and errors, keep blocking calls off time-critical paths. For UART, SPI, I2C, ADC and timer capture the rule suggests DMA, and it asks you to document buffer ownership and lifetime. volatile is limited to memory shared with ISRs or to hardware registers.
ISRs stay short and deterministic; heavy work moves to the main loop, an RTOS task or an event queue. Dynamic allocation inside interrupts is out, and heap use in firmware only if the project explicitly allows it. A section on common mistakes warns against editing generated files, spinning forever on hardware flags, and sharing a buffer between DMA and CPU without synchronization. If your project has an .ioc file or platformio.ini, the globs pick the rule up.
What it helps with
- split firmware into drivers, middleware and application code
- move heavy work out of an interrupt handler
- set up DMA for UART, SPI or ADC without losing the buffer
- check HAL return values and add timeouts
- run pure logic on a host build without hardware
Example prompts for the agent
- Review this DMA path: where can the buffer be reused too early?
- Rewrite this interrupt handler so it stays short
- Add timeouts to the peripheral flag polling
How to install embedded-stm32-hal
Manually: put the files in place
.cursor/rules/embedded-stm32-hal.mdcFrontmatter: description, globs, alwaysApply
Manually: put the files in place
.claude/rules/embedded-stm32-hal.mdThe paths frontmatter field limits the files: move globs from the source into paths
AGENTS.md
Codex reads AGENTS.md: paste the rule text there
Manually: put the files in place
.github/instructions/embedded-stm32-hal.instructions.mdThe applyTo field sets which files it applies to: move globs from the source into applyTo
Manually: put the files in place
.devin/rules/embedded-stm32-hal.mdManually: put the files in place
.clinerules/embedded-stm32-hal.mdStep by step, with commands for each platform: Cursor rules: .mdc files, four modes and what replaced .cursorrules
Questions about embedded-stm32-hal
How do I install embedded-stm32-hal in Cursor?
Place the file at .cursor/rules/embedded-stm32-hal.mdc inside your project. Cursor then treats it as a rule for files matching the globs.
What does embedded-stm32-hal do?
It sets agent rules for embedded C/C++: project structure, STM32 HAL usage, interrupts, DMA, memory constraints and hardware-focused testing.
Which files does the rule apply to?
The globs cover **/*.c, **/*.h, **/*.cpp, **/*.hpp, **/*.ioc, CMakeLists.txt, Makefile and platformio.ini. Auto-apply is off, so the rule triggers on those paths.
Source text
embedded-stm32-hal.mdc · CC0-1.0
--- description: Embedded C/C++ rules for MCU, STM32, HAL, interrupts, DMA, memory constraints, and hardware-focused testing globs: ["**/*.c", "**/*.h", "**/*.cpp", "**/*.hpp", "**/*.ioc", "CMakeLists.txt", "Makefile", "platformio.ini"] alwaysApply: false --- # 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 `volatile` only 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 `const` so 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.
© PatrickJS · CC0-1.0 · PatrickJS/awesome-cursorrules