<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Firmware on Tiziano Basile | Software Engineer</title><link>https://bstz.it/tags/firmware/</link><description>Recent content in Firmware on Tiziano Basile | Software Engineer</description><generator>Hugo -- gohugo.io</generator><language>en</language><copyright>Tiziano Basile</copyright><lastBuildDate>Mon, 23 Nov 2026 00:00:00 +0000</lastBuildDate><atom:link href="https://bstz.it/tags/firmware/index.xml" rel="self" type="application/rss+xml"/><item><title>Bonooru Devlog #2: My own firmware, as a Pro Controller</title><link>https://bstz.it/p/bonooru-devlog-2/</link><pubDate>Mon, 23 Nov 2026 00:00:00 +0000</pubDate><guid>https://bstz.it/p/bonooru-devlog-2/</guid><description>&lt;img src="https://bstz.it/p/bonooru-devlog-2/switch-2-with-the-boards.jpg" alt="Featured image of post Bonooru Devlog #2: My own firmware, as a Pro Controller" /&gt;&lt;p&gt;The Switch 2 now plays &lt;em&gt;Ode to Joy&lt;/em&gt; on Link&amp;rsquo;s ocarina from my own firmware. 62 notes out of 62, every one at the right pitch, timed by the controller board&amp;rsquo;s own clock, and the firmware says it applied every note at most 14 µs late.&lt;/p&gt;
&lt;p&gt;&lt;a class="link" href="https://github.com/basteez/bonooru" target="_blank" rel="noopener"
&gt;Bonooru&lt;/a&gt; is my side project to play arbitrary melodies on the ocarina in &lt;em&gt;The Legend of Zelda: Ocarina of Time&lt;/em&gt; on Switch 2. It does that by pretending to be a wired controller. The chain is a desktop app on the PC, a &amp;ldquo;bridge&amp;rdquo; RP2040 board on USB, a UART link, and a &amp;ldquo;controller&amp;rdquo; RP2040 board that the Switch 2 sees as a Pro Controller. &lt;a class="link" href="https://bstz.it/p/bonooru-devlog-0/" target="_blank" rel="noopener"
&gt;Devlog #0&lt;/a&gt; explains the idea and the plan. &lt;a class="link" href="https://bstz.it/p/bonooru-devlog-1/" target="_blank" rel="noopener"
&gt;Devlog #1&lt;/a&gt; is the two days of measuring the ocarina itself.&lt;/p&gt;
&lt;figure class="mermaid-figure"&gt;
&lt;pre class="mermaid"&gt;flowchart LR
PC[&amp;#34;PC&amp;lt;br/&amp;gt;(probe scripts today,&amp;lt;br/&amp;gt;the app later)&amp;#34;] -- &amp;#34;USB CDC&amp;#34; --&amp;gt; BR[&amp;#34;bridge RP2040-Zero&amp;lt;br/&amp;gt;(2wiCC_Comms for now)&amp;#34;]
BR -- &amp;#34;UART 460800&amp;#34; --&amp;gt; CT[&amp;#34;controller RP2040-Zero&amp;lt;br/&amp;gt;(my firmware)&amp;#34;]
CT -- &amp;#34;USB HID,&amp;lt;br/&amp;gt;Pro Controller&amp;#34; --&amp;gt; SW[&amp;#34;Switch 2&amp;#34;]&lt;/pre&gt;
&lt;figcaption class="mermaid-caption"&gt;The Bonooru chain. This epic built the controller board&amp;#39;s firmware; the bridge is still someone else&amp;#39;s&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;h2 id="where-we-were"&gt;Where we were
&lt;/h2&gt;&lt;p&gt;At the end of Epic 1 the Switch 2 had played the same melody, but through &lt;a class="link" href="https://github.com/knflrpn/2wiCC" target="_blank" rel="noopener"
&gt;2wiCC&lt;/a&gt; by knflrpn, someone else&amp;rsquo;s controller firmware. I had a measured profile of the ocarina (which button plays which note, how short a press can be, how fast notes can follow each other) and nothing of my own on the boards.&lt;/p&gt;
&lt;p&gt;The goal of Epic 2 was milestone M3: my own controller firmware enumerates as a wired Pro Controller, enforces the safety rules, and plays a hard-coded melody on the console with at most one frame of jitter. Seven stories, seven PRs (&lt;a class="link" href="https://github.com/basteez/bonooru/pull/15" target="_blank" rel="noopener"
&gt;#15&lt;/a&gt; to &lt;a class="link" href="https://github.com/basteez/bonooru/pull/19" target="_blank" rel="noopener"
&gt;#19&lt;/a&gt;, &lt;a class="link" href="https://github.com/basteez/bonooru/pull/23" target="_blank" rel="noopener"
&gt;#23&lt;/a&gt;, &lt;a class="link" href="https://github.com/basteez/bonooru/pull/24" target="_blank" rel="noopener"
&gt;#24&lt;/a&gt;), from 21 to 23 November.&lt;/p&gt;
&lt;p&gt;The stories were not built in the order you&amp;rsquo;d expect. The parts that need no console came first: the build, the wire protocol, the playback core, the UART link and its watchdog. The USB side, the bit the console actually talks to, came sixth. That way, when I finally plugged the board into the Switch, everything behind the USB endpoint had already been tested on my Mac for a day.&lt;/p&gt;
&lt;h2 id="a-skeleton-that-blinks"&gt;A skeleton that blinks
&lt;/h2&gt;&lt;p&gt;Story 2.1 (&lt;a class="link" href="https://github.com/basteez/bonooru/pull/15" target="_blank" rel="noopener"
&gt;PR #15&lt;/a&gt;) is the boring one, and the one everything else stands on. pico-sdk 2.3.1 is a git submodule, with only TinyUSB 0.18.0 initialised inside it. The Arm GNU Toolchain 15.2.Rel1 and picotool 2.3.1 are pinned with their published checksums. One CMake project builds two UF2 files, &lt;code&gt;bridge.uf2&lt;/code&gt; and &lt;code&gt;controller.uf2&lt;/code&gt;, and a third preset builds the host tests for the PC.&lt;/p&gt;
&lt;p&gt;At this stage each board did one thing: blink the onboard WS2812 RGB LED through a PIO driver, blue at 1 Hz for the bridge and amber at 2 Hz for the controller, so I can tell them apart on the breadboard at a glance. Every GPIO number lives in one header, &lt;code&gt;pins.h&lt;/code&gt;, and CI fails if a grep finds a pin number anywhere else. That rule sounds pedantic until two boards share a breadboard and a wrong pin can drive one board&amp;rsquo;s TX into the other&amp;rsquo;s.&lt;/p&gt;
&lt;h2 id="one-protocol-one-library-239-vectors"&gt;One protocol, one library, 239 vectors
&lt;/h2&gt;&lt;p&gt;Story 2.2 (&lt;a class="link" href="https://github.com/basteez/bonooru/pull/16" target="_blank" rel="noopener"
&gt;PR #16&lt;/a&gt;) wrote the link protocol as a pure C99 library, &lt;code&gt;ocb_proto&lt;/code&gt;. No pico-sdk include, no heap, no global state. Frames are &lt;code&gt;type | seq | len | body | crc16le&lt;/code&gt;, wrapped in COBS (Consistent Overhead Byte Stuffing, which removes every &lt;code&gt;0x00&lt;/code&gt; from the frame so a single &lt;code&gt;0x00&lt;/code&gt; can mark where one frame ends), and checked with CRC-16/CCITT-FALSE.&lt;/p&gt;
&lt;p&gt;The PC app will be written in Rust later, so the library is tested against language-neutral text vectors rather than against itself: 239 cases in 7 files, covering every message, the COBS and CRC edge cases, maximum-length frames, an empty frame, a bad CRC, an overlong frame and the 8-byte neutral controller state. The Rust codec will have to pass the same files.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;# HELLO, seq 0: crc16(&amp;#34;01 00 01 01&amp;#34;) = d164, sent LE as &amp;#34;64 d1&amp;#34;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;&lt;em&gt;One line of &lt;a class="link" href="https://github.com/basteez/bonooru/blob/f999406/firmware/common/proto/vectors/messages.txt" target="_blank" rel="noopener"
&gt;&lt;code&gt;firmware/common/proto/vectors/messages.txt&lt;/code&gt;&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;The code review found one important bug here: when a COBS decode overflowed its output buffer, the decoder reported it in a different order than the architecture asks for (COBS first, then length, then CRC). Small, but it&amp;rsquo;s exactly the kind of disagreement between two implementations that the vectors exist to catch, so the fix came with new vector cases that failed against the old decoder first.&lt;/p&gt;
&lt;h2 id="the-core-what-is-allowed-to-reach-the-console"&gt;The core: what is allowed to reach the console
&lt;/h2&gt;&lt;p&gt;Story 2.4 (&lt;a class="link" href="https://github.com/basteez/bonooru/pull/17" target="_blank" rel="noopener"
&gt;PR #17&lt;/a&gt;) is the playback logic, again in plain C tested on the PC with a fake clock. Three modes:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;IDLE&lt;/strong&gt;: nothing is pressed.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;DIRECT&lt;/strong&gt;: the PC sets the state live (to navigate menus, for example).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;PLAYING&lt;/strong&gt;: the board plays a queue of timed events (up to 512, 12 bytes each) from its own clock.&lt;/li&gt;
&lt;/ul&gt;
&lt;figure class="mermaid-figure"&gt;
&lt;pre class="mermaid"&gt;stateDiagram-v2
[*] --&amp;gt; IDLE
IDLE --&amp;gt; DIRECT: SET_STATE
IDLE --&amp;gt; PLAYING: START (queue loaded)
DIRECT --&amp;gt; IDLE: STOP / abort
PLAYING --&amp;gt; IDLE: last event (NEUTRAL + END) / STOP / abort&lt;/pre&gt;
&lt;figcaption class="mermaid-caption"&gt;Controller modes. Every abort returns NEUTRAL and latches until the app says STOP or HELLO&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;In PLAYING a safety mask lets through only the buttons the ocarina needs, so a corrupted event can never press HOME or open a menu in the middle of a song. A test pushes all 65,536 button values through the mask. Every abort (link lost, USB gone, an event that arrives too late) sends the state to NEUTRAL, clears the queue and latches, so the board doesn&amp;rsquo;t resume playing by itself after a glitch.&lt;/p&gt;
&lt;h2 id="the-uart-link"&gt;The UART link
&lt;/h2&gt;&lt;p&gt;Story 2.6 (&lt;a class="link" href="https://github.com/basteez/bonooru/pull/18" target="_blank" rel="noopener"
&gt;PR #18&lt;/a&gt;) is the controller&amp;rsquo;s side of the link: UART0 at 460800 baud, an interrupt that drains the receive FIFO including the error bits (framing, parity, break, overrun), frame decoding, a go-back-N retransmission scheme so a lost or corrupted &lt;code&gt;QUEUE_EVENT&lt;/code&gt; is resent rather than skipped or duplicated, and a STATUS frame every 50 ms.&lt;/p&gt;
&lt;p&gt;The bridge on my bench still runs knflrpn&amp;rsquo;s &lt;a class="link" href="https://github.com/knflrpn/2wiCC_Comms" target="_blank" rel="noopener"
&gt;2wiCC_Comms&lt;/a&gt;, which copies bytes between USB and UART unchanged. It has no flow control and drops bytes silently when its 1 KB buffer fills, so my bench script paces its writes to the UART rate. The first soak failed on bugs in that script, not in the firmware; the one that passed sent 20,000 events with 2% of frames corrupted, 2% dropped and garbage in between:&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Soak run (H.5)&lt;/th&gt;
&lt;th&gt;Events&lt;/th&gt;
&lt;th&gt;Resends&lt;/th&gt;
&lt;th&gt;Rewinds&lt;/th&gt;
&lt;th&gt;Semantic errors&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;seed 1, impaired&lt;/td&gt;
&lt;td&gt;20,000, all in order&lt;/td&gt;
&lt;td&gt;36&lt;/td&gt;
&lt;td&gt;1,806&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;seed 2, impaired&lt;/td&gt;
&lt;td&gt;20,000, all in order&lt;/td&gt;
&lt;td&gt;34&lt;/td&gt;
&lt;td&gt;1,772&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;clean&lt;/td&gt;
&lt;td&gt;20,000, all in order&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;The other part of this story is electrical. When only one of the two boards is powered, its TX pin must not push current into the other&amp;rsquo;s RX. So the controller keeps its TX pin high-impedance at power-up, and only drives it after its own RX line has been idle-high for 1 ms, which means the bridge is alive. With a multimeter: TX 0 V with the controller alone, 3.28 V once the peer idles high.&lt;/p&gt;
&lt;p&gt;&lt;img src="https://bstz.it/p/bonooru-devlog-2/bridge-and-controller-breadboard.jpg"
width="960"
height="1280"
srcset="https://bstz.it/p/bonooru-devlog-2/bridge-and-controller-breadboard_hu_6f7d9e7d9080feab.jpg 480w, https://bstz.it/p/bonooru-devlog-2/bridge-and-controller-breadboard_hu_58f0436bee6ea846.jpg 1024w"
loading="lazy"
alt="Two RP2040-Zero boards on a breadboard, wired by UART"
class="gallery-image"
data-flex-grow="75"
data-flex-basis="180px"
&gt;
&lt;em&gt;The bench: the bridge RP2040-Zero at the top (still 2wiCC_Comms), my controller firmware at the bottom, UART and GND between them. The wiring is ugly; the GND continuity was measured first.&lt;/em&gt;&lt;/p&gt;
&lt;h2 id="the-link-watchdog"&gt;The link watchdog
&lt;/h2&gt;&lt;p&gt;Story 2.7 (&lt;a class="link" href="https://github.com/basteez/bonooru/pull/19" target="_blank" rel="noopener"
&gt;PR #19&lt;/a&gt;) answers a question that matters more than any other in this project: what happens if the PC or the bridge disappears while a note is pressed?&lt;/p&gt;
&lt;p&gt;The bridge sends a &lt;code&gt;HEARTBEAT&lt;/code&gt; every 20 ms. If the controller sees none for 80 ms, or sees a UART break (an unplugged wire reads as a break, because the RX pin has a pull-down), it aborts: NEUTRAL, queue cleared, an &lt;code&gt;EVENT LINK_LOST&lt;/code&gt; to the app, then TX released. On the bench, three runs of the &amp;ldquo;stop the heartbeat&amp;rdquo; test gave &lt;code&gt;LINK_LOST&lt;/code&gt; 81.6, 81.1 and 81.3 ms after the last heartbeat. Pulling the wire by hand, ten rounds in a row, gave one abort per round and nothing left pressed.&lt;/p&gt;
&lt;p&gt;The test that failed here twice was, again, the bench script. A bouncing replug of the wire left half a frame in the controller&amp;rsquo;s receiver, and the script never resent the event that frame had eaten. Later, the bounce of one replug arrived 77 to 104 ms after the previous round had ended and the script took it for the next unplug. The firmware did the right thing in every one of those runs. I&amp;rsquo;m starting to think that half of embedded work is debugging your own test tools.&lt;/p&gt;
&lt;h2 id="the-switch-2-meets-a-stranger"&gt;The Switch 2 meets a stranger
&lt;/h2&gt;&lt;p&gt;Then Story 2.3 (&lt;a class="link" href="https://github.com/basteez/bonooru/pull/23" target="_blank" rel="noopener"
&gt;PR #23&lt;/a&gt;): make the controller board a USB device the console accepts.&lt;/p&gt;
&lt;h3 id="clean-room-on-purpose"&gt;Clean-room, on purpose
&lt;/h3&gt;&lt;p&gt;2wiCC has no licence, so I can&amp;rsquo;t read its code and then write mine; that would make Bonooru a derivative of something I have no permission to use. The Linux &lt;code&gt;hid-nintendo&lt;/code&gt; driver is GPL, which would force a licence I didn&amp;rsquo;t choose. So the whole Pro Controller conversation was rebuilt from public descriptions and captures, used for facts only (byte values, offsets, sequences), never for code:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;dekuNukem&amp;rsquo;s &lt;a class="link" href="https://github.com/dekuNukem/Nintendo_Switch_Reverse_Engineering" target="_blank" rel="noopener"
&gt;Nintendo_Switch_Reverse_Engineering&lt;/a&gt; notes: the &lt;code&gt;0x80&lt;/code&gt; USB commands, the &lt;code&gt;0x30&lt;/code&gt; input report, the subcommands, the SPI flash map.&lt;/li&gt;
&lt;li&gt;Chromium&amp;rsquo;s &lt;a class="link" href="https://github.com/chromium/chromium/blob/main/device/gamepad/nintendo_controller.cc" target="_blank" rel="noopener"
&gt;&lt;code&gt;nintendo_controller.cc&lt;/code&gt;&lt;/a&gt; and SDL&amp;rsquo;s &lt;a class="link" href="https://github.com/libsdl-org/SDL/blob/main/src/joystick/hidapi/SDL_hidapi_switch.c" target="_blank" rel="noopener"
&gt;&lt;code&gt;SDL_hidapi_switch.c&lt;/code&gt;&lt;/a&gt;: what a host does when it opens a wired Pro Controller.&lt;/li&gt;
&lt;li&gt;mzyy94&amp;rsquo;s &lt;a class="link" href="https://mzyy94.com/blog/2020/03/20/nintendo-switch-pro-controller-usb-gadget/" target="_blank" rel="noopener"
&gt;USB gadget write-up&lt;/a&gt; and its capture log, and tmk&amp;rsquo;s &lt;a class="link" href="https://gist.github.com/tmk/9725596c6726b561ad2bf1f1e07e1523" target="_blank" rel="noopener"
&gt;&lt;code&gt;lsusb -v&lt;/code&gt; dump&lt;/a&gt;: the real descriptors and a real Switch 1 talking to a real controller.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;All of it lives in &lt;code&gt;ocb_p2&lt;/code&gt;, another pure C99 library: descriptors, the &lt;code&gt;0x80&lt;/code&gt; commands, the subcommand answers, the emulated SPI flash, the &lt;code&gt;0x30&lt;/code&gt; report. It knows nothing about TinyUSB or the RP2040, so it is tested and fuzzed on the PC. A thin TinyUSB glue on core 0 connects it to the hardware.&lt;/p&gt;
&lt;p&gt;There is one rule the library is strict about: the console must never be able to update or rewrite my controller. SPI writes and erases answer &amp;ldquo;write protected&amp;rdquo;, the MCU firmware update path and the feature reports that unlock the controller&amp;rsquo;s memory are refused and counted. And refusing things is where the first attempt broke.&lt;/p&gt;
&lt;h3 id="attempt-1-the-pairing-loop"&gt;Attempt 1: the pairing loop
&lt;/h3&gt;&lt;p&gt;The first time I plugged the board into the Switch 2, my bench script printed PASS: handshake done, &lt;code&gt;console_ready&lt;/code&gt; 1 after 24 s, the console polling the IN endpoint every 8000 µs. No controller icon on the screen.&lt;/p&gt;
&lt;p&gt;The debug build keeps a log of every request the console sends and leaves it inside the STATUS frames, so I could read the Switch 2&amp;rsquo;s side of the handshake. It went through the expected init and then asked for subcommand &lt;code&gt;01&lt;/code&gt;: Bluetooth pairing. My firmware refused it, as the &amp;ldquo;change nothing&amp;rdquo; rule said. The console asked again. And again. About 30 times, until the log was full.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;restart bus-reset, restart mount, usb 80 05, usb 80 01, usb 80 02, sub 03, HANDSHAKE,
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;usb 80 04, sub 02, sub 08, sub 10, sub 10, sub 01, REFUSED 01, sub 01, REFUSED 01, ...
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;mzyy94&amp;rsquo;s full capture showed what a real controller does: it answers &lt;code&gt;01&lt;/code&gt; with &lt;code&gt;81 01 03&lt;/code&gt;, and the console&amp;rsquo;s MCU request &lt;code&gt;21&lt;/code&gt; with &lt;code&gt;A0 21&lt;/code&gt; and a 34-byte standby report. Neither answer pairs, stores or configures anything, so both fit the rule. The rule was right; my reading of &amp;ldquo;refuse anything that could change state&amp;rdquo; was too broad.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;span class="lnt"&gt;4
&lt;/span&gt;&lt;span class="lnt"&gt;5
&lt;/span&gt;&lt;span class="lnt"&gt;6
&lt;/span&gt;&lt;span class="lnt"&gt;7
&lt;/span&gt;&lt;span class="lnt"&gt;8
&lt;/span&gt;&lt;span class="lnt"&gt;9
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nl"&gt;OCB_P2_SC_BT_PAIRING&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="cm"&gt;/* D34: the console sends this during its wired init (S12: 01 04 +
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="cm"&gt; * its address and name) and repeats it until answered; a NACK
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="cm"&gt; * stalled the Switch 2 (bench H.15 attempt 1). The real controller
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="cm"&gt; * answers 81 01 03 (S12; S13 same shape). Nothing is paired or
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="cm"&gt; * stored: the host address is discarded, no state changes. */&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;uint8_t&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;OCB_P2_PAIRING_STATUS&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;reply_sub&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;OCB_P2_ACK_PAIRING&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;sizeof&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;&lt;em&gt;&lt;a class="link" href="https://github.com/basteez/bonooru/blob/f999406/firmware/controller/profile_p2/p2.c#L387-L395" target="_blank" rel="noopener"
&gt;&lt;code&gt;firmware/controller/profile_p2/p2.c&lt;/code&gt;&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;The whole captured init, 28 output reports, is now a host test. And the debug log learned to compress repeats, so the next loop would read &lt;code&gt;sub 01, REFUSED 01, (repeated 57 more)&lt;/code&gt; instead of filling the buffer.&lt;/p&gt;
&lt;h3 id="attempt-2-the-inputs-that-went-nowhere"&gt;Attempt 2: the inputs that went nowhere
&lt;/h3&gt;&lt;p&gt;Second try. This time the console went all the way through its init, almost request for request like the Switch 1 in the capture, and finished with subcommand &lt;code&gt;30&lt;/code&gt;: player lights. A console that assigns a player slot has accepted the controller. Still no icon. I pressed D-pad down five times from the bench script, every press was acknowledged, nothing moved on the HOME screen.&lt;/p&gt;
&lt;p&gt;That one was simple once I looked: in that build the USB report was &lt;em&gt;always&lt;/em&gt; NEUTRAL. The path from core 1 (where the link and the scheduler live) to core 0 (where the USB report is built) was a stub that returned NEUTRAL, waiting for Story 2.5. My presses reached core 1 and stopped there.&lt;/p&gt;
&lt;figure class="mermaid-figure"&gt;
&lt;pre class="mermaid"&gt;sequenceDiagram
participant S as Switch 2
participant C as controller (ocb_p2)
S-&amp;gt;&amp;gt;C: 80 05, 80 01 (status)
C--&amp;gt;&amp;gt;S: 81 01 00 03 + MAC
S-&amp;gt;&amp;gt;C: 80 02 (handshake)
C--&amp;gt;&amp;gt;S: 81 02
S-&amp;gt;&amp;gt;C: 03 30 (standard input mode)
S-&amp;gt;&amp;gt;C: 80 04 (stay in USB mode)
Note over C: start the 0x30 report stream
S-&amp;gt;&amp;gt;C: 02, 08, 10 ×2 (device info, SPI reads)
S-&amp;gt;&amp;gt;C: 01 (pairing)
C--&amp;gt;&amp;gt;S: 81 01 03
S-&amp;gt;&amp;gt;C: 03 30, 04, 10 ×5, 40
S-&amp;gt;&amp;gt;C: 30 (player lights)
C--&amp;gt;&amp;gt;S: 80 30
Note over S,C: then only the 0x30 stream, every 8 ms&lt;/pre&gt;
&lt;figcaption class="mermaid-caption"&gt;The Switch 2&amp;#39;s wired init as my firmware logged it, attempt 2 (simplified)&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;h3 id="attempt-3-the-console-waits-for-you"&gt;Attempt 3: the console waits for you
&lt;/h3&gt;&lt;p&gt;For the third attempt the debug build got a real handoff: core 1 publishes its state through a sequence lock (more on that below), core 0 reads it into the report. First a check on the Mac with no console: five D-pad presses, &lt;code&gt;Down&lt;/code&gt; seen five times in the USB reports.&lt;/p&gt;
&lt;p&gt;Then the Switch 2. Same init, &lt;code&gt;console_ready&lt;/code&gt; 1 after 9.2 s. No icon, nothing in the Controllers menu. Then I sent A, and Zelda launched from the HOME screen.&lt;/p&gt;
&lt;p&gt;That&amp;rsquo;s the finding I didn&amp;rsquo;t expect: &lt;strong&gt;the Switch 2 doesn&amp;rsquo;t show a wired controller until its first input.&lt;/strong&gt; It assigns a player slot, it polls the reports, but until a button is pressed there is no icon and no menu entry. I couldn&amp;rsquo;t find this written anywhere public, so attempt 1&amp;rsquo;s lesson (&amp;ldquo;a PASS from my script isn&amp;rsquo;t a pass&amp;rdquo;) almost made me chase a bug that didn&amp;rsquo;t exist.&lt;/p&gt;
&lt;p&gt;&lt;img src="https://bstz.it/p/bonooru-devlog-2/switch-2-with-the-boards.jpg"
width="1280"
height="960"
srcset="https://bstz.it/p/bonooru-devlog-2/switch-2-with-the-boards_hu_542b9e76b627900e.jpg 480w, https://bstz.it/p/bonooru-devlog-2/switch-2-with-the-boards_hu_81bac29f018fc489.jpg 1024w"
loading="lazy"
alt="A Switch 2 with its screen off, wired to the boards and a laptop"
class="gallery-image"
data-flex-grow="133"
data-flex-basis="320px"
&gt;
&lt;em&gt;The whole bench: the Mac drives the bridge, the controller goes into the Switch 2&amp;rsquo;s top USB-C port. Screen off on purpose: no game images here. And yes, sorry about the limoncello tablecloth.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Other things the console taught me in the same session:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;It polls every 8 ms&lt;/strong&gt;, whatever the descriptor asks. Mine asks for 1 ms; the Mac polls the same build at about 2 ms. The architecture had assumed 1 ms, so that assumption is now marked refuted.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Sleep is a power cut.&lt;/strong&gt; The Switch 2 removes USB power while it sleeps, so the controller boots again on wake, and becomes ready 1.7 s after the console&amp;rsquo;s first request.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;macOS has opinions.&lt;/strong&gt; With &amp;ldquo;Allow accessories to connect&amp;rdquo; on &amp;ldquo;Ask&amp;rdquo;, a pending prompt left the board suspended and gone from the USB tree. That failed one test until a replug.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="a-melody-from-the-boards-own-clock"&gt;A melody from the board&amp;rsquo;s own clock
&lt;/h2&gt;&lt;p&gt;Story 2.5 (&lt;a class="link" href="https://github.com/basteez/bonooru/pull/24" target="_blank" rel="noopener"
&gt;PR #24&lt;/a&gt;) put the pieces into their final shape and played the first melody.&lt;/p&gt;
&lt;figure class="mermaid-figure"&gt;
&lt;pre class="mermaid"&gt;flowchart LR
subgraph C1[&amp;#34;core 1: super-loop from RAM&amp;#34;]
L[&amp;#34;UART link&amp;lt;br/&amp;gt;+ link watchdog&amp;#34;] --&amp;gt; SCH[&amp;#34;scheduler&amp;lt;br/&amp;gt;(1 MHz timer, polled)&amp;#34;]
end
subgraph C0[&amp;#34;core 0&amp;#34;]
U[&amp;#34;TinyUSB + ocb_p2&amp;lt;br/&amp;gt;0x30 report on every poll&amp;#34;]
end
SCH -- &amp;#34;seqlock&amp;lt;br/&amp;gt;double buffer&amp;#34; --&amp;gt; U
U -- &amp;#34;mailbox&amp;lt;br/&amp;gt;(USB events, poll stats)&amp;#34; --&amp;gt; L
U -- &amp;#34;USB, polled every 8 ms&amp;#34; --&amp;gt; SW[&amp;#34;Switch 2&amp;#34;]&lt;/pre&gt;
&lt;figcaption class="mermaid-caption"&gt;The two cores of the controller RP2040&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;Core 1 runs a loop with no sleep and no interrupts of its own, polling the 1 MHz timer and applying each event when its time comes. The whole program runs from RAM (about 32 KB copied at boot), so a flash cache miss can&amp;rsquo;t delay a note. The state crosses to core 0 through a sequence-locked double buffer: the writer makes a counter odd, fills the inactive slot, makes it even; the reader copies and checks that the counter didn&amp;rsquo;t move in a way that means a torn copy. It tries three times, then gives up and reports NEUTRAL.&lt;/p&gt;
&lt;p&gt;My first version of that check was wrong. An exhaustive test of every interleaving of writer and reader steps found a case where the reader accepted a slot the writer was rewriting: a torn state, half one note and half the next. The fix was to count half publishes, not whole ones. The test now runs over 7 million interleavings under AddressSanitizer in about a second. The Cortex-M0+ barrier needed a look too: GCC&amp;rsquo;s &lt;code&gt;__atomic_thread_fence&lt;/code&gt; compiles to &lt;code&gt;dmb ish&lt;/code&gt;, an option ARMv6-M reserves, so the firmware uses the SDK&amp;rsquo;s &lt;code&gt;__dmb()&lt;/code&gt; (&lt;code&gt;dmb sy&lt;/code&gt;), and a CI gate counts the barriers in the disassembly.&lt;/p&gt;
&lt;h3 id="the-firmware-measures-itself"&gt;The firmware measures itself
&lt;/h3&gt;&lt;p&gt;I have no logic analyser, so the timing is measured in two layers: the firmware times itself, and a microphone listens to the console.&lt;/p&gt;
&lt;p&gt;The timeline build carries &lt;em&gt;Ode to Joy&lt;/em&gt; from Epic 1, 158 events, generated from the measured profile. The PC only sends HELLO and START. For every applied event the firmware records how late it was against its due time; for every USB poll, the interval since the previous one. Three runs on the Switch 2 (H.18):&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Measure&lt;/th&gt;
&lt;th&gt;Result&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Lateness at apply, max (3 runs)&lt;/td&gt;
&lt;td&gt;10 / 14 / 10 µs&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Lateness, mean&lt;/td&gt;
&lt;td&gt;about 5 µs&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Events in the first histogram bucket&lt;/td&gt;
&lt;td&gt;158 / 158&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;IN poll interval: median, p95&lt;/td&gt;
&lt;td&gt;8000 µs, 8000 µs&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Core 1 loop, max&lt;/td&gt;
&lt;td&gt;106 to 117 µs&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Core 1 stack, high-water mark&lt;/td&gt;
&lt;td&gt;1228 to 1244 B (limit 1536)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Retries, fallbacks, reboots&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;A note can be at most 14 µs late at the board. The &amp;ldquo;late&amp;rdquo; threshold, at which the core would abort the piece, is one poll period (8000 µs): about 570 times the worst lateness seen.&lt;/p&gt;
&lt;h3 id="the-microphone-disagrees-and-thats-fine"&gt;The microphone disagrees, and that&amp;rsquo;s fine
&lt;/h3&gt;&lt;p&gt;Then the second layer (H.19): the Mac&amp;rsquo;s microphone near the console speaker, the same melody, an onset detector comparing the gaps between notes with the score. 62 notes, 62 audible, 62 at the right pitch, within −3 to +0 cents. The 95th percentile of the inter-onset error was &lt;strong&gt;18.14 ms&lt;/strong&gt;, which is 1.088 frames at 60 fps: above the one-frame target.&lt;/p&gt;
&lt;p&gt;Where do 18 ms come from, if the board is accurate to 14 µs? From two things the firmware can&amp;rsquo;t change:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A state change waits for the next USB poll: anywhere from 0 to 8 ms.&lt;/li&gt;
&lt;li&gt;The game samples input once per frame: anywhere from 0 to 16.67 ms. For the gap between two notes, two independent errors like that give a p95 of 0.776 frame, 12.94 ms, even with a perfect scheduler.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The analysis tool computes both bounds and checks what&amp;rsquo;s left over: firmware bound 8.01 ms (14 µs of lateness plus one poll), game floor 12.94 ms, residual +2.92 ms against a 4 ms allowance for the analyser&amp;rsquo;s own noise. Verdict: &lt;strong&gt;EXPLAINED&lt;/strong&gt;. The acceptance criterion allowed exactly that branch, &amp;ldquo;≤ 1 frame, or the gap explained with the self-timing data&amp;rdquo;, because I knew from the start that part of the jitter belongs to the console.&lt;/p&gt;
&lt;figure class="mermaid-figure"&gt;
&lt;pre class="mermaid"&gt;xychart-beta
title &amp;#34;Timing budget (ms)&amp;#34;
x-axis [&amp;#34;board lateness&amp;#34;, &amp;#34;USB poll&amp;#34;, &amp;#34;firmware bound&amp;#34;, &amp;#34;game floor (p95)&amp;#34;, &amp;#34;1 frame&amp;#34;, &amp;#34;measured IOI p95&amp;#34;]
y-axis &amp;#34;ms&amp;#34; 0 --&amp;gt; 20
bar [0.014, 8, 8.01, 12.94, 16.67, 18.14]&lt;/pre&gt;
&lt;figcaption class="mermaid-caption"&gt;Where the jitter comes from, in ms (H.18 and H.19)&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;The board&amp;rsquo;s bar is invisible on that chart. That&amp;rsquo;s the point.&lt;/p&gt;
&lt;h2 id="two-watchdogs-and-a-chip-that-lies-about-one-of-them"&gt;Two watchdogs, and a chip that lies about one of them
&lt;/h2&gt;&lt;p&gt;The link watchdog of Story 2.7 covers a dead PC. Story 2.5 adds the other case: a dead core. If either core hangs with a note pressed, the board must reset itself, the USB must disappear, and the console must see nothing pressed, within 100 ms.&lt;/p&gt;
&lt;p&gt;The rules: each core watches the other&amp;rsquo;s pass counter. If core 1 sees core 0 stuck for 20 ms, it reboots the chip at once. If core 0 sees core 1 stuck for 20 ms, it switches the report to NEUTRAL and stops feeding the 60 ms hardware watchdog. Worst case 20 + 60 + 5 = 85 ms, and that sum is a compile-time assertion: change a constant past 100 ms and the build fails.&lt;/p&gt;
&lt;figure class="mermaid-figure"&gt;
&lt;pre class="mermaid"&gt;stateDiagram-v2
Running --&amp;gt; Core0Stale: core 0 counter frozen &amp;gt; 20 ms
Running --&amp;gt; Core1Stale: core 1 counter frozen &amp;gt; 20 ms
Core0Stale --&amp;gt; Reset: core 1 reboots the chip now
Core1Stale --&amp;gt; Neutral: core 0 reports NEUTRAL, stops feeding
Neutral --&amp;gt; Reset: hardware watchdog, 60 ms after the last feed
Reset --&amp;gt; [*]: USB detaches, boots with nothing pressed&lt;/pre&gt;
&lt;figcaption class="mermaid-caption"&gt;Liveness rules on the controller&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;To test it I built two debug flavours that hang one core on purpose 50 ms into the first note. The first bench run failed: the Mac saw the device vanish about 180 ms after the press. It turned out macOS takes about &lt;strong&gt;113 ms&lt;/strong&gt; to notice a removed USB device, so the host can&amp;rsquo;t measure a 20 ms reset. So the board now measures it: at the reset it writes the stall-to-reset interval into a watchdog scratch register, which survives the reset, and reports it after boot. Core 0 hung: &lt;strong&gt;20.0 ms&lt;/strong&gt;, five runs out of five.&lt;/p&gt;
&lt;p&gt;Core 1 hung: the board said &lt;strong&gt;270 ms&lt;/strong&gt;. That&amp;rsquo;s a fail, by a lot. Except the host saw NEUTRAL reports keep coming until about 77 to 79 ms after the hang, then the device vanish 115 ms later, the macOS delay again. The board was resetting on time; it was measuring wrong. To get &amp;ldquo;time until the hardware watchdog fires&amp;rdquo; I had called &lt;code&gt;watchdog_get_time_remaining_us()&lt;/code&gt;, and the pico-sdk header says, for the RP2040:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;On RP2040 this method returns the last value set instead of the remaining time due to a h/w bug.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;So now the firmware computes it from the last time core 0 fed the watchdog:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;span class="lnt"&gt;4
&lt;/span&gt;&lt;span class="lnt"&gt;5
&lt;/span&gt;&lt;span class="lnt"&gt;6
&lt;/span&gt;&lt;span class="lnt"&gt;7
&lt;/span&gt;&lt;span class="lnt"&gt;8
&lt;/span&gt;&lt;span class="lnt"&gt;9
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kt"&gt;uint32_t&lt;/span&gt; &lt;span class="nf"&gt;ocb_live_watchdog_stall_to_reset_us&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint64_t&lt;/span&gt; &lt;span class="n"&gt;peer_last_change_us&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;uint64_t&lt;/span&gt; &lt;span class="n"&gt;last_feed_us&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;uint64_t&lt;/span&gt; &lt;span class="n"&gt;reset_us&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;last_feed_us&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint64_t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;OCB_WDT_TIMEOUT_MS&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;1000u&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;reset_us&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;peer_last_change_us&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0u&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;uint64_t&lt;/span&gt; &lt;span class="n"&gt;span&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;reset_us&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;peer_last_change_us&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;span&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint64_t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;UINT32_MAX&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="nl"&gt;UINT32_MAX&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint32_t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;span&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;&lt;em&gt;&lt;a class="link" href="https://github.com/basteez/bonooru/blob/f999406/firmware/controller/core/live.c#L97-L105" target="_blank" rel="noopener"
&gt;&lt;code&gt;firmware/controller/core/live.c&lt;/code&gt;&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Second run: &lt;strong&gt;80.0 ms&lt;/strong&gt; (79,990 µs), host and board agree within 5 ms, the board came back with nothing pressed. Both within the 100 ms budget.&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Hang&lt;/th&gt;
&lt;th&gt;Detected&lt;/th&gt;
&lt;th&gt;Reset after the stall&lt;/th&gt;
&lt;th&gt;macOS sees the removal&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;core 0 (5 runs)&lt;/td&gt;
&lt;td&gt;by core 1, 20 ms&lt;/td&gt;
&lt;td&gt;20.0 ms&lt;/td&gt;
&lt;td&gt;median 113.1 ms later&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;core 1&lt;/td&gt;
&lt;td&gt;by core 0, 20 ms (NEUTRAL from then)&lt;/td&gt;
&lt;td&gt;80.0 ms&lt;/td&gt;
&lt;td&gt;114.4 ms later&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;h2 id="what-went-wrong-and-what-i-learned"&gt;What went wrong, and what I learned
&lt;/h2&gt;&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;My refusal rule was too broad.&lt;/strong&gt; &amp;ldquo;Change nothing&amp;rdquo; became &amp;ldquo;refuse everything&amp;rdquo;, and the console looped on the pairing request. Answering it the way a real controller does changes nothing either.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;A green script is not a pass.&lt;/strong&gt; Attempt 1 printed PASS with no controller on screen. The criterion is now &amp;ldquo;the first input acted on&amp;rdquo;, checked by me.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The console hides wired controllers until the first input.&lt;/strong&gt; Not a bug, but it cost an attempt.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The 1 ms poll was an assumption; the console polls at 8 ms.&lt;/strong&gt; Measured, recorded, the architecture note updated.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The RP2040 watchdog can&amp;rsquo;t tell you how long it has left.&lt;/strong&gt; Read the SDK header before trusting a register.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The Mac adds ~113 ms to every USB removal.&lt;/strong&gt; The board measures its own reset now.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;My seqlock was wrong&lt;/strong&gt; until an exhaustive interleaving test showed a torn read.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Many failures were my bench tools&lt;/strong&gt;, not the firmware: a soak sender bug, a missed resend after a bouncing wire, a bounce taken for an unplug, a fresh-boot check that sent HELLO before reading. Every one was fixed and re-run.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="how-its-made"&gt;How it&amp;rsquo;s made
&lt;/h2&gt;&lt;p&gt;I plan and build Bonooru with the &lt;a class="link" href="https://github.com/bmad-code-org/BMAD-METHOD" target="_blank" rel="noopener"
&gt;BMad Method&lt;/a&gt; and AI agents (Claude). Each story gets a file with its acceptance criteria and every open decision recorded; an agent implements it, a fresh-context agent reviews it, and the findings are fixed or recorded. What needs hardware is mine: I ran every bench step at the Switch and the Mac, the agent wrote the probe tools and recorded the numbers exactly as measured, including the failed attempts.&lt;/p&gt;
&lt;p&gt;Epic 2 is 7 PRs. At the end, the firmware has 39 host tests (ctest) that run under AddressSanitizer and UndefinedBehaviorSanitizer with no hardware, plus CI gates for pin numbers, library purity, memory barriers, RAM placement and the core 1 stack. The rules, decisions and bench numbers live in &lt;a class="link" href="https://github.com/basteez/bonooru/blob/f999406/docs/test-plan.md" target="_blank" rel="noopener"
&gt;&lt;code&gt;docs/test-plan.md&lt;/code&gt;&lt;/a&gt;, &lt;a class="link" href="https://github.com/basteez/bonooru/blob/f999406/docs/link-protocol.md" target="_blank" rel="noopener"
&gt;&lt;code&gt;docs/link-protocol.md&lt;/code&gt;&lt;/a&gt; and &lt;a class="link" href="https://github.com/basteez/bonooru/blob/f999406/docs/usb-controller-emulation.md" target="_blank" rel="noopener"
&gt;&lt;code&gt;docs/usb-controller-emulation.md&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id="next-up"&gt;Next up
&lt;/h2&gt;&lt;p&gt;The controller is mine now; the bridge isn&amp;rsquo;t. Next epic: my own bridge firmware, and the PC driving the controller through it, end to end.&lt;/p&gt;
&lt;p&gt;In Epic 1 a melody played through someone else&amp;rsquo;s firmware. Today it plays through mine, and I know where every millisecond goes.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;&lt;em&gt;This is an unofficial fan project for personal, offline fun. It is not affiliated with, endorsed or sponsored by Nintendo. The Legend of Zelda and Nintendo Switch are trademarks of Nintendo. No game code, assets or console modifications are involved.&lt;/em&gt;&lt;/p&gt;</description></item></channel></rss>