Code and libraries
Short, copy-paste snippets and reusable libraries for embedded and FPGA work.
- CRC-8 in C (compact, no table) A tiny, table-free CRC-8 routine for embedded C — configurable polynomial, good for short messages on UART, I2C or sensor frames where a full lookup table isn't worth the flash. View code →
- Reading & writing MCU registers (C) How to access a peripheral register from bare-metal C on an STM32H563 — and why the read-only IDR, read-write ODR and write-only BSRR are not interchangeable. Access type encoded in the C pointer so misuse fails to compile. View code →
- Lock-free SPSC ring buffer (C) A header-only single-producer / single-consumer ring buffer for embedded C — no locks, no disabled interrupts, power-of-two size. Ideal for UART/DMA RX in an ISR feeding the main loop. View code →
- Button debouncer (Verilog) A small, synthesizable Verilog button debouncer with a clean single-cycle rising-edge pulse output — counter-based, parameterized debounce time, plus a two-flop synchronizer for the async input. View code →
- Atomic writes & RMW (C) When a write is actually atomic on a 32-bit MCU, and how to make a read-modify-write safe against an ISR — lock-free with LDREX/STREX, or with a PRIMASK-saving critical section. View code →
- Bitwise operations (C) The canonical set/clear/toggle/test vocabulary plus the field and constant-time idioms every embedded C programmer should know — and the unsigned/precedence traps that bite the ones who don't. View code →
- Clock divider with 50% duty (Verilog) A parameterized clock divider in Verilog that produces an exact 50% duty cycle for both even and odd ratios, plus a clock-enable strobe — the safer way to run slower logic on the original clock. Self-checking testbench passes in Icarus. View code →
- Low-resource dual-port BRAM (Verilog) A parameterized simple dual-port RAM in Verilog that infers a single block RAM — independent write and read ports, registered output, no wasted LUTs or flip-flops. Self-checking testbench passes in Icarus. View code →
- PWM generator (Verilog) A synthesizable PWM generator in Verilog with runtime-adjustable frequency and duty cycle. New settings are shadowed and applied at the period boundary, so changing them on the fly never glitches the output. Self-checking testbench passes in Icarus. View code →
- Stepper motor driver (Verilog) A synthesizable half-step stepper motor driver in Verilog — 8-step drive table for a 4-coil unipolar/bipolar motor, runtime direction and enable (hold), parameterized step rate. Self-checking testbench passes in Icarus. View code →
- UART TX with AXI-Stream interface (Verilog) A synthesizable 8N1 UART transmitter driven by an AXI4-Stream slave port (tdata/tvalid/tready) — drop it on any FIFO or DMA stream and bytes go out the serial line at the configured baud. View code →
- Using volatile correctly (C) What the volatile qualifier actually guarantees in embedded C — memory-mapped registers and ISR-shared flags — and why it is neither atomic nor a memory barrier. View code →