Skip to main content
rule

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.

Install into Cursor · Manually: put the files in place All methods and platforms ↓
.cursor/rules/embedded-stm32-hal.mdc
Open on GitHub

What 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.mdc

Frontmatter: description, globs, alwaysApply

Step 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

Similar IDE rules

All IDE rules for Programming languages →

From the same set68

htmx-django-cursorrules-prompt-file A Cursor rule that keeps the agent on Django rails while it writes HTMX code: templates, forms, CSRF, class-based views. ★ 41,011repo stars how-to-documentation-cursorrules-prompt-file A Cursor rule that turns technical steps, test scripts and screenshots into step-by-step guides for non-technical users. Useful when you write How To docs for a feature. ★ 41,011repo stars graphical-apps-development-cursorrules-prompt-file A Cursor rule that feeds the agent the Pyllments context: graph-based LLM apps in Python built on Panel and Langchain. ★ 41,011repo stars flutter-development-guidelines-cursorrules-prompt-file Locks down folder layout, naming and Dart style for Flutter apps built on MVVM with Riverpod. Useful when the agent scatters files across the project. ★ 41,011repo stars dragonruby-best-practices-cursorrules-prompt-file A Cursor rule that sets Ruby coding style for DragonRuby Game Toolkit: naming, file structure, error handling. ★ 41,011repo stars database A Cursor rule with database practices for Prisma and Supabase: schemas, migrations, queries, RLS policies. Scoped to database files by glob. ★ 41,011repo stars cypress-defect-tracking-cursorrules-prompt-file A Cursor rule that turns the agent into a QA engineer: writes Cypress tests tagged by team, category and case ID, and builds reports through qa-shadow-report. ★ 41,011repo stars cursorrules-cursor-ai-wordpress-draft-macos-prompt A Cursor rule that describes the PressThat project: a system tray app that connects to WordPress and creates draft posts. Useful if you work on that app in Cursor. ★ 41,011repo stars

Project sponsors

Companies that keep this analytics open
Gold sponsor

Impressions, clicks and code copies — open statistics. We keep the numbers public for the same reason NeBlask itself is public. Become a sponsor