Skills DirectorySkills Directory
SkillsLearnSecurityCategoriesDocsBlogPro
Sign InSubmit Skill
Skills Directory

Security-tested agent skills for Claude, coding agents, and AI workflows.

Directory

  • Browse Skills
  • All Skills A–Z
  • Claude Skills
  • Claude Code Skills
  • Agent Skills
  • Categories
  • Authors
  • Submit a Skill

Learn

  • Learn Hub
  • Install Claude Skills
  • Write SKILL.md
  • Skills vs MCP
  • Directories Compared

Security

  • Security
  • Methodology
  • Secure Claude Skills
  • Security Badges
  • Chrome Extension
  • Skill Manager

Company

  • About
  • Community
  • Blog
  • API Docs
  • Advertise

2026 Skills Directory. All rights reserved.

ProTermsPrivacyRefunds
Back to skills

Uart Serial Baremetal

ASecurity

Use when configuring UART baud rate/BRR, polling or IRQ-driven TX/RX, serial printf retargeting, USART interrupts, or UART with DMA on bare-metal MCUs. Not for the DMA stream: use dma-baremetal.

54 stars
0 votes
0 copies
0 views
Added 9/20/2026
ai-agentsgo

Works with

terminal

Security Analysis

A100/100

Pro scans all 2 files and shows the line behind each finding

Scanned 9/20/2026

$npx -y skills add OutlineDriven/outline-driven-development --skill uart-serial-baremetal --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Uart Serial Baremetal?

Add the live security badge to your README — it updates automatically with every re-scan.

Security grade badge for Uart Serial Baremetal
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/outlinedriven-uart-serial-baremetal-outline-driven-development/badge)](https://www.skillsdirectory.com/skills/outlinedriven-uart-serial-baremetal-outline-driven-development)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
Files
SKILL.md
---
name: uart-serial-baremetal
description: 'Use when configuring UART baud rate/BRR, polling or IRQ-driven TX/RX, serial printf retargeting, USART interrupts, or UART with DMA on bare-metal MCUs. Not for the DMA stream: use dma-baremetal.'
disable-model-invocation: true
---

# UART serial on bare metal

## Contract

| Field | Bound contract |
|---|---|
| Trigger | The first log output on new hardware is needed, a serial protocol to a module must be implemented, a blocking HAL UART is being replaced, or characters arrive garbled or go missing. |
| Authority | Read-only: emits baud arithmetic, register sequences, ISR and retarget code to chat; the user places them in the firmware. Rollback is not needed because no file is written. No remote mutation. |
| Side effect | Chat output only. |
| Done | The baud register value is computed from the actual peripheral clock with the reference manual formula, the pins are muxed, TX and RX work polled, the interrupt path clears overrun the way the manual states, and `printf` reaches the port when requested. |

## Inputs

- Target part and USART instance, with the bus it sits on and that bus's clock after prescaling.
- Baud rate, frame (8N1 unless stated), and whether oversampling by 8 is needed for high rates.
- TX and RX pins with their alternate-function numbers, configured per `gpio-baremetal`.
- I/O model: polled, interrupt-driven with a ring buffer, or DMA.
- C library in use (newlib, newlib-nano) when `printf` retargeting is requested.

## Procedure

1. Compute the baud register from the actual peripheral clock. On STM32 with oversampling by 16, `USARTDIV = f_CK / (16 * baud)`, and `BRR` stores the mantissa in bits 15 to 4 and the 4-bit fraction in bits 3 to 0, which is why `BRR = f_CK / baud` (rounded) gives the same encoding. For 115200 baud at 84 MHz, `BRR` = 729 (`0x2D9`), an actual rate of 115226 baud and an error of 0.02 %. `f_CK` is the APB clock for that USART, not the system clock. Done when: the computed rate is within 2 % of the target, or oversampling by 8 is selected for a closer match.

   ```c
   void usart2_init(uint32_t pclk, uint32_t baud)
   {
       RCC->APB1ENR |= RCC_APB1ENR_USART2EN;
       (void)RCC->APB1ENR;
       /* PA2 and PA3 as AF7 per gpio-baremetal */
       USART2->BRR = (pclk + baud / 2U) / baud;   /* OVER8 = 0: mantissa[15:4] fraction[3:0] */
       USART2->CR1 = USART_CR1_TE | USART_CR1_RE | USART_CR1_UE;
   }
   ```

2. Write polled transmit and receive: wait for `TXE` before writing the data register, wait for `RXNE` before reading it. Done when: a terminal at the configured baud echoes typed characters.

   ```c
   void uart_putc(USART_TypeDef *u, char c)
   {
       while (!(u->SR & USART_SR_TXE)) { }
       u->DR = (uint8_t)c;
   }

   char uart_getc(USART_TypeDef *u)
   {
       while (!(u->SR & USART_SR_RXNE)) { }
       return (char)(uint8_t)u->DR;
   }
   ```

3. Move reception into an interrupt with a ring buffer for anything beyond a console. Enable `RXNEIE`, enable the NVIC entry, and in the handler push each byte. Handle overrun: on STM32F1 and F4 `ORE` clears only by reading `SR` then `DR`, and a set `ORE` with `RXNEIE` enabled re-enters the handler forever if left uncleared. On the newer USART (F0, F3, L4, G0, G4) write `ORECF` to `ICR`. Done when: a burst longer than the ring buffer is received without the handler spinning.

   ```c
   void USART2_IRQHandler(void)
   {
       uint32_t sr = USART2->SR;
       if (sr & USART_SR_RXNE) {
           rb_push(&rx_rb, (uint8_t)USART2->DR);
       }
       if (sr & USART_SR_ORE) {
           (void)USART2->DR;   /* SR read above plus this DR read clears ORE */
       }
   }
   ```

4. Retarget `printf` when newlib is linked. Provide `_write` and route it to the polled transmit; link with `--specs=nosys.specs` so the other syscalls have stubs, or supply them. Done when: `printf("%d\n", 42)` appears on the terminal.

   ```c
   int _write(int fd, char *ptr, int len)
   {
       (void)fd;
       for (int i = 0; i < len; i++) uart_putc(USART2, ptr[i]);
       return len;
   }
   ```

5. Use DMA for bulk transfers: set `DMAR` or `DMAT` in `CR3` and configure the stream per `dma-baremetal`. The USART side is only the request enable. Done when: the DMA stream counts down as bytes arrive or leave.
6. Verify the timing on the wire, not by eye: measure one bit period on a scope or logic analyzer and compare with `1 / baud`. A garbled console with a plausible-looking baud is almost always a wrong `f_CK` assumption. Done when: the measured bit period matches the configured baud within 2 %.

## Failure and recovery

| Symptom | Cause | Fix |
|---|---|---|
| Garbage characters | `BRR` computed from the wrong clock | Use the APB clock for that USART after its prescaler. |
| Lost bytes, then silence | `ORE` set and never cleared | Read `SR` then `DR` (or write `ORECF`) in the handler. |
| No output | TX pin not in alternate-function mode, or `UE`/`TE` clear | Configure the pin; set `TE` and `UE`. |
| `printf` blocks or prints nothing | `_write` missing or newlib buffering | Provide `_write`; use `\n` or `setvbuf` to flush. |
| Works at 9600, fails at 921600 | Baud error too large at oversampling 16 | Set `OVER8` and recompute `BRR` with the oversampling-by-8 encoding. |

## Output

For the target USART: the baud arithmetic and `BRR` value with its error, the init sequence, polled TX and RX, the interrupt handler with the correct overrun clearing, the `printf` retarget when requested, and the wire measurement that confirms the baud.

Attribution

OutlineDrivenOutlineDriven
View sourceSee grades on GitHubMore from OutlineDriven →
SSkills DirectorySkills Directory

Ship a skill? Prove it's safe.

Free 120-pattern security scan, letter grade, and an embeddable README badge.

Submit a skill

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments (0)

No comments yet. Be the first to comment!

SSkills DirectorySkills Directory

Ship a skill? Prove it's safe.

Free 120-pattern security scan, letter grade, and an embeddable README badge.

Submit a skill

Related Skills

Caveman

Terse caveman voice: answer first, fluff gone, every technical fact kept. Use for /caveman, "caveman mode", "talk like caveman", "be brief", "less tokens". Stays on until "stop caveman" or "normal mode".

1100021 votes

Hyperplan

Adversarial multi-agent planning skill. Self-orchestrates 5 hostile category members (unspecified-low, unspecified-high, deep, ultrabrain, artistry) via team-mode for ruthless cross-critique debate, distills only the defensible insights, then MANDATORILY hands the distilled insight bundle to the `plan` agent for executable plan formalization. Use when planning needs maximum rigor and surfacing of weak assumptions, blind spots, and over-engineering. Triggers: 'hyperplan', 'hpp', '/hyperplan', ...

698621 votes

Writing Skills

Create and manage Claude Code skills in HASH repository following Anthropic best practices. Use when creating new skills, modifying skill-rules.json, understanding trigger patterns, working with hooks, debugging skill activation, or implementing progressive disclosure. Covers skill structure, YAML frontmatter, trigger types (keywords, intent patterns), UserPromptSubmit hook, and the 500-line rule. Includes validation and debugging with SKILL_DEBUG. Examples include rust-error-stack, cargo-dep...

3931 votes

Mcp Code Execution

Routes multi-tool workflows through MCP servers for large datasets and pipelines. Use when Bash tool overhead is limiting throughput on data-heavy tasks.

3421 votes

catchup

Recovers the conversation and failed tool calls of a previous Codex, Amp, Claude Code, Antigravity, Cline, Copilot CLI, Cursor, DeepSeek Harness, Grok Build, Kimi, OpenCode, Pi Agent, or ZCode session. Use when the user says "catch up", "what did the last session do", "get me up to speed", "I switched agents", asks to recover/summarize a previous session before continuing, or asks to diagnose or report a catchup failure. Do NOT use for the current conversation, git history, or any non-agent log.

741 votes
View all in ai-agents →