periph-designing-firmware-pcb-and-debugging
Use when building your own peripheral: designing a custom peripheral from requirements to bill of materials, firmware including the USB stack choices, PCB and mechanical design, enumeration and debugging when a device will not appear or misbehaves, drivers and OS integration across Windows, macOS and Linux, and measuring latency properly rather than trusting a spec sheet.
- License 许可
- MIT
- 最近更新
- 2026-11-15
- Stars
- ★ 5
- Forks
- 3
01 前言 Front Matter
- name
- periph-designing-firmware-pcb-and-debugging
- description
- Use when building your own peripheral: designing a custom peripheral from requirements to bill of materials, firmware including the USB stack choices, PCB and mechanical design, enumeration and debugging when a device will not appear or misbehaves, drivers and OS integration across Windows, macOS and Linux, and measuring latency properly rather than trusting a spec sheet.
03 SKILL.md 正文
Computer Peripherals: Designing a Custom Peripheral, Firmware, PCB and Mechanical Design, Enumeration and Debugging, Drivers and OS Integration, and Measuring Latency
Part 4 of 6 of the Computer Peripherals: Design, Building, Standards and Programming reference (plugin
computer-peripherals-design-and-standards), covering §16–§21. Sibling skills:periph-stack-usb-thunderbolt-and-wireless(§0–§5),periph-buses-pcie-hid-keyboards-mice-and-displays(§6–§11),periph-audio-printers-storage-controllers-and-haptics(§12–§15),periph-compliance-accessibility-and-security(§22–§24),periph-reference(§25–§30). Section numbers are shared across the set; a reference written as §N →skillpoints into that sibling skill.Currency: The protocols are stable and long-lived. Two areas are moving. See §25 →
periph-referencefor USB bandwidth and labelling, and display interface bandwidth tiers.
⚠️ The layer where a computer meets the physical world — and the one where standards politics, signal integrity, human factors and firmware all collide at once.
Complements a computer-hardware reference (the host side), an electromagnetism reference (signal integrity, differential pairs, EMC), and a semiconductor reference (the microcontrollers involved).
⚠️ GOTCHA boxes mark where the marketing name and the actual capability diverge — and this domain has the worst naming in computing.
The three ideas that organize this document:
- ⚠️ THE CONNECTOR IS NOT THE PROTOCOL (§2 →
periph-stack-usb-thunderbolt-and-wireless, §3 →periph-stack-usb-thunderbolt-and-wireless, §25.1 →periph-reference). A USB-C port can be anything from 480 Mbps and 5 W to 80 Gbps and 240 W with video and PCIe tunnelling. Identical-looking ports and cables behave completely differently, and this single confusion causes more peripheral problems than any technical failure.- ⚠️ DESCRIPTORS ARE THE INTERFACE (§8 →
periph-buses-pcie-hid-keyboards-mice-and-displays, §19). A USB device declares what it is by handing over a data structure at enumeration. Almost all "device not recognized" problems are descriptor problems, and once you can read descriptors you can debug nearly anything.- ⚠️ LATENCY IS A CHAIN, AND PEOPLE OPTIMIZE THE WRONG LINK (§9 →
periph-buses-pcie-hid-keyboards-mice-and-displays, §21). Polling rate, debounce, USB scheduling, OS input stack, render queue and display scanout all add up. Buying an 8 kHz mouse while running a 60 Hz display optimizes a link that wasn't the problem.
§16. Designing a Custom Peripheral
⚠️ THE DECISION ORDER
⚠️ 1. What DEVICE CLASS is it? (§8) ⚠️ If HID fits, you get
cross-platform support free. ⚠️ Vendor-specific means
writing and maintaining drivers forever — avoid unless
genuinely necessary
⚠️ 2. Wired or wireless? (§5)
⚠️ 3. Power budget and source
⚠️ 4. Microcontroller selection — ⚠️ NATIVE USB peripheral vs
bit-banged vs a USB bridge chip
⚠️ 5. Enclosure and manufacturing method
⚠️ COMMON MCU CHOICES ⚠️ RP2040/RP2350 (cheap, native USB, PIO) ·
STM32 · ATmega32U4 (the classic HID choice) · nRF52 (BLE) ·
ESP32 (Wi-Fi/BLE)
⚠️ ⚠️ VID/PID ⚠️ USB Vendor IDs are ISSUED BY THE USB-IF AND COST
MONEY. ⚠️ For hobby projects, use a PID sublicensed from a
chip vendor's range or a community allocation — ⚠️ do NOT
ship products with a squatted VID, which is common and wrong
⚠️ PROTOTYPE PATH breadboard → dev board → custom PCB →
⚠️ and expect at least two board revisions
§17. Firmware
⚠️ The mature open ecosystems save enormous effort: ⚠️ QMK and ZMK for keyboards
(⚠️ ZMK being BLE-first), CircuitPython and Arduino for rapid work, TinyUSB as the
portable USB stack, and vendor HALs underneath.
⚠️ What the firmware actually does: ⚠️ matrix scanning and debounce (§9 → periph-buses-pcie-hid-keyboards-mice-and-displays), layer and
macro handling, descriptor generation, endpoint management, and persistent configuration.
⚠️ The real-time discipline: ⚠️ interrupt latency, avoiding blocking in the main loop,
and keeping the USB polling deadline no matter what else is happening.
⚠️ Power management for battery devices is usually where naive firmware fails —
⚠️ sleep states, wake sources, and connection interval negotiation dominate battery life
far more than the radio's rated current.
⚠️ DFU and bootloaders — ⚠️ and always leave a hardware recovery path, because
bricking a device with no bootloader entry is the classic first-project disaster.
§18. PCB and Mechanical Design
⚠️ For USB specifically (see an electromagnetism reference): ⚠️ 90 Ω differential impedance for D+/D−, length matching within the pair, avoiding stubs and layer changes on high-speed pairs, and a continuous ground reference plane under the pair — a split plane under a differential pair is the classic EMC failure. ⚠️ ESD protection on every externally exposed line — ⚠️ TVS diodes at the connector, and this is not optional on a product. ⚠️ Power integrity: ⚠️ decoupling capacitors close to pins, bulk capacitance, and a clean supply for analog sections. ⚠️ Mechanical: ⚠️ connector retention (⚠️ through-hole or reinforced SMD, because a ripped-off USB connector is the most common physical failure), tolerance stack-up, and the enclosure process choice — 3D print, injection mould, CNC or sheet metal (see a manufacturing reference). ⚠️ Design for assembly and test — ⚠️ test points, and a way to program the board after assembly.
§19. ⚠️ Enumeration and Debugging
⚠️ Where most peripheral development time actually goes.
⚠️ THE ENUMERATION SEQUENCE ⚠️ attach and speed detection via
pull-ups → reset → address assignment → ⚠️ DEVICE DESCRIPTOR
read → configuration, interface, endpoint descriptors →
⚠️ HID REPORT DESCRIPTOR → configuration selected → operational
⚠️ ⚠️ ALMOST EVERY "DEVICE NOT RECOGNIZED" IS A DESCRIPTOR OR
POWER PROBLEM. ⚠️ A malformed report descriptor typically
fails SILENTLY — the device enumerates and then does nothing,
or does something bizarre
⚠️ THE TOOLS, in order of usefulness
⚠️ lsusb -v / USBView / USB Prober — ⚠️ read the actual
descriptors first, always
⚠️ HID descriptor validators and parsers
⚠️ Wireshark with usbmon (Linux) — ⚠️ free protocol capture
⚠️ Hardware protocol analysers — expensive and decisive
⚠️ OSCILLOSCOPE for physical-layer problems, ⚠️ and eye
diagrams for signal integrity
⚠️ THE COMMON FAULTS ⚠️ descriptor length mismatches · wrong
usage page · endpoint size vs report size disagreement ·
⚠️ requesting more current than declared · missing or wrong
pull-ups · ⚠️ bus-powered device browning out on inrush ·
host controller quirks that differ across OSes
⚠️ ⚠️ TEST ON ALL TARGET PLATFORMS. ⚠️ Windows, macOS, Linux, BIOS
and phones parse descriptors differently and tolerate
different sloppiness
§20. Drivers and OS Integration
⚠️ Prefer the class driver (§1 → periph-stack-usb-thunderbolt-and-wireless, §8 → periph-buses-pcie-hid-keyboards-mice-and-displays) — ⚠️ a custom driver is a permanent maintenance
liability across OS versions.
⚠️ Userspace access is the middle path: ⚠️ libusb, HIDAPI, WebUSB and WebHID —
⚠️ and WebHID in particular means a configuration tool can be a web page rather than an
installed application, which is a genuine improvement in the peripheral world.
⚠️ When a kernel driver is genuinely required: ⚠️ Windows WDF/UMDF and driver signing;
Linux kernel modules; macOS DriverKit having replaced kexts.
⚠️ Permissions: ⚠️ udev rules on Linux, and the mistake of instructing users to run
things as root rather than shipping a rules file.
⚠️ The honest observation about vendor software: ⚠️ much peripheral configuration
software is heavy, runs at startup, and exists partly for telemetry — which is why
ONBOARD PROFILE STORAGE is a real feature, letting the device keep its configuration
without resident software.
§21. ⚠️ Measuring Latency
⚠️ §1 →
periph-stack-usb-thunderbolt-and-wireless's third organizing idea, made concrete.
⚠️ THE FULL CHAIN, and every link adds
⚠️ Physical actuation and switch travel
⚠️ DEBOUNCE window (§9)
⚠️ Matrix scan interval
⚠️ ⚠️ USB POLLING INTERVAL — ⚠️ 1000 Hz = 1 ms, 125 Hz = 8 ms
⚠️ OS input stack and application
⚠️ Render and present queue
⚠️ ⚠️ DISPLAY SCANOUT AND PIXEL RESPONSE — ⚠️ at 60 Hz this
alone averages ~8 ms of the total
⚠️ ⚠️ THE ARITHMETIC PEOPLE SKIP: going from 1000 Hz to 8000 Hz
polling saves at most ~0.9 ms. ⚠️ Going from a 60 Hz to a
240 Hz display saves several times that. ⚠️ Optimize the
largest term
⚠️ HOW TO ACTUALLY MEASURE ⚠️ high-speed camera on the input and
the screen together (the ground truth) · ⚠️ instrumented
hardware that shorts a switch and watches the display ·
⚠️ latency test tools in-OS. ⚠️ Software timestamps alone
cannot see the ends of the chain
⚠️ VARIANCE MATTERS MORE THAN MEAN ⚠️ consistent 5 ms feels
better than 2 ms averaging with occasional 20 ms spikes
PART IV — CROSS-CUTTING
04 同仓库更多 Skills
white-paper-writing
Use when writing white papers, technical reports, policy documents, thought leadership pieces, or any long-form authoritative document. Triggers on requests to write, structure, outline, or review white papers, technical reports, position papers, policy briefs, or evidence-driven arguments. Also use for B2B content marketing strategy, technical documentation requiring authority, academic-style reports, or any document that must educate while persuading. Use when someone asks "how should I structure this" for a long-form document.
prose-technical-mechanics
Use when editing or writing prose at a technical/mechanical level: choosing between Anglo-Saxon and Latinate diction, placing information in topic vs stress positions, writing periodic vs cumulative sentences, handling sentence rhythm, punctuation as logic, transitions, paragraph structure, or phonesthetics. Use when someone wants to understand WHY prose works or doesn't, not just surface-level style advice. Triggers on word choice, sentence structure, emphasis, rhythm, diction, or any deep writing mechanics question.
prose-mechanics
Use when writing, editing, or analyzing prose style; when trying to avoid generic AI-sounding writing; when applying specific prose styles such as journalistic, academic, narrative, KJV biblical, Hemingway plain, Latinate/Jamesian, Orwellian, Augustan, Victorian, Early Modern (Bacon, Browne, Milton), or free indirect discourse; or when coaching someone on writing voice and mechanics. Triggers on any request to improve, analyze, or write in a specific prose style, to fix "AI-sounding" writing, to make prose more vivid or concrete, to vary sentence rhythm, or to understand how a particular writer achieves their effects.
narrative-structure
Use when structuring stories, pitches, presentations, white papers, or any persuasive narrative; analyzing the hero's journey in fiction or non-fiction; applying StoryBrand for business communication; designing narrative arcs for thought leadership; or understanding Vogler, Snyder, Propp, or Campbell frameworks. Triggers on any narrative structure, storytelling, pitch structure, StoryBrand, hero's journey, three-act structure, Freytag, Save the Cat, narrative arc, or "how should I structure this story/pitch/presentation" request.
legal-brief-writing
Use when writing legal briefs, motions, or any court filing; when analyzing judicial cognition and persuasion; when applying elite brief-writing techniques from Roberts, Clement, Kagan, Garner; or when structuring legal arguments for maximum persuasion. Also use for autistic/neurodivergent advocates who need to understand how their natural communication style maps to elite legal advocacy. Triggers on any legal writing, brief drafting, motion preparation, or legal argument structuring request.
journal-publishability-criteria
Use whenever a research paper, article or preprint is being written, revised, reviewed or published, and specifically before every publication of docs/paper.md. Covers what legitimate journals actually gate on, in order of gating — the editor's desk screen (scope, novelty, package problems), what reviewers judge (methodological soundness, claims the evidence can carry, statistical and qualitative reporting), EQUATOR reporting guidelines, the ICMJE authorship criteria, COPE's view of text recycling, the near-universal generative-AI policy (no AI author, disclosure, author responsibility), TOP 2025 transparency standards, how to verify a journal is legitimate, submitting without an institution, and a pre-submission checklist. Triggers on requests to evaluate, review, prepare or submit a paper, to judge publishability, to write a declarations or data-availability section, to disclose AI use, or to check a journal.