Toddler Tunes - A Digital Music Box · Part 3 of 5

Firmware before the board, and bring-up night

Claude wrote the player's firmware before any hardware existed, and the owner tested it on a bare XIAO. When the boards arrived, one evening of bring-up found a flash-driver timing bug, a serial port being read by another process, and a shutdown bug. A battery log and a battery gauge followed.

  • Published
  • Worked Oct 22 – 30, 2026
  • Claude Opus 5.5
  • firmwarerp2350bring-uppico-sdkdebugging

Firmware before the board

While JLCPCB built the board, the player’s firmware was written in the same Claude Code session as the enclosure. That session’s record didn’t survive, so this chapter rests on the commit, its README and the owner’s account.

Me

As I understood it, the C SDK was needed to support both the USB drive and the programming mode. Claude decided to make the player support multiple songs, though in practice I load only the one song.

Claude

I wrote the firmware without the board, the flash chip or any of the other parts at hand. It’s a C player on the pico-sdk. Press to play or pause, and hold 2 s to turn off. It turns itself off after the last song, after 5 minutes paused, and on a low battery after a warning tune, and power-on resumes the same song at the same place. Plugged into a computer, it becomes a USB drive called TODDLERTUNE: FAT on the W25Q128. minimp3 decodes into a PIO I2S output fed by ping-pong DMA. If the flash chip doesn’t answer, the player falls back to the XIAO’s own flash.

How do you test a player before its board exists?

The player’s behaviour lives in one file, app.c, behind a small platform.h interface. On the Mac, host-side tests drive that logic through a fake platform. They also cover the block device, the resume log, the controls, the mixer, the FAT file system, the playlist and MP3 decoding.

On real hardware, the fallback to the XIAO’s internal flash gives a working USB drive on a bare module. So the drive, playback and resume can be tried before the flash chip is on a board.

Me

I tested it on my bench on a bare XIAO with nothing connected, mostly to verify the mass-storage behaviour. The commit records the drive, playback and resume working, with the decoder at about 10% load.

Bring-up night

On October 28 the boards came back from JLCPCB, assembled except for the parts the owner fits by hand.

Me

The board has returned from the fab, and I’ve connected all of the components except for the volume pot. I need you to connect to it and test out the hardware.

A spare rev 0 mainboard as JLCPCB assembled it, on a blue box: surface-mount parts fitted around the round board, the XIAO's footprint still empty at the top, and the button not yet fitted

That photo is of a spare board, which I took later for my review on JLCPCB.

Claude

The board powered up from USB, and the XIAO enumerated and took new firmware. But the player’s serial console never answered, a volume called NO NAME appeared where TODDLERTUNE should have been, and a 16 MB drive the player had formatted earlier was gone after a reboot.

Me

The Mac reported the disk as unreadable, and I chose Eject.

Claude

I hit two dead ends of my own. I tried to read the firmware’s trace buffer by rebooting into BOOTSEL and dumping RAM, but the bootloader had already cleared it. And my kernel-log queries had silently never run, because log is a zsh builtin. A separate hardware test image lost about half its bytes too: “usb” came back as “sb”. That put the loss on the Mac’s side. I couldn’t see root-owned processes, so I asked you to run sudo lsof on the port.

Me

The output shows a root-owned background process with the player’s serial port open for reading. It has already read 306 bytes.

Claude

That process reads every serial device as it’s opened, which swallowed the console’s replies. I left it alone and moved the test image’s console onto a vendor-class raw USB interface, which creates no serial device at all, and talked to it over libusb the way picotool does. Then the hardware checked out piece by piece. The W25Q128 answered with JEDEC ID EF 40 18 and read cleanly up to 37.5 MHz. Four button presses registered with no bounce. The amplifier played test tones over I2S at 44.1 kHz.

Me

I heard a sound, then the two tones: low, then high.

Claude

Then the real bug, and it was mine. My flash driver’s deselect held /CS high for only about 7 ns between commands, and the chip needs 50 ns. It latched WRITE ENABLE and ignored the erase or program that followed, which is why the first format vanished on reboot. The fix holds /CS high for 100 ns. After it came 4,480 page writes through the player’s own driver with no failures, a freshly formatted TODDLERTUNE drive, and two songs that came back byte-identical after a reboot.

How can 7 nanoseconds break a flash chip?

The W25Q128’s datasheet sets a minimum time that /CS must stay high between commands: tSHSL, 50 ns.

The player’s firmware runs entirely from RAM (copy_to_ram), because the MP3 decoder doesn’t fit the XIP cache. Running from RAM, the driver’s deselect-then-select took a single clock cycle, about 7 ns. The chip latched the short WRITE ENABLE command but ignored the erase or program that came right after it. On disk, that looked like a format that worked until the next reboot.

The hardware test image’s wtest command now exercises the driver, so a regression shows up on the bench.

Making it a player

Me

On a USB charger it powered up, but made no sound when I pressed the button.

Claude

The volume follows the pot, and with no pot fitted, its input floats near zero: the silent bottom of the knob. I flashed a build with a fixed volume to get past it.

Me

Now it plays audibly. But after a 2 s hold to turn it off, it doesn’t respond to any other button presses.

Claude

That one was my bug. After a hold, the player releases HOLD and waits for 4 s of button release, to tell USB power from batteries. Every press during that wait was ignored and restarted it, so tapping every second or two kept it deaf. Now a press during the wait turns the player straight back on, with a host test to keep it that way.

Me

I found a spare through-hole pot in my parts and wired it to RV1’s terminals. The power-off fix works, and the volume pot works as well. Are there any user LEDs on the XIAO? If so, I’d like them to indicate some things for me.

Claude

There’s an RGB LED: a WS2812 on GPIO22, powered from GPIO23. I offered you a few schemes for what it should show.

Me

Player state, plus a red flash at every power on and off, on batteries and on USB.

Claude

I wrote a WS2812 driver on pio1, since pio0 runs the I2S. Green while playing, a slow green pulse while paused, blue as a USB drive, and dim white when off on USB power, with your red flash at every power on and off.

Me

I saw red, blue, then white. I’ve also installed the batteries and verified that it runs as expected on battery power alone.

Claude

Measured under load, as the firmware sees it, the test image read a steady 4.80 V across the cells.

Me

My meter shows 4.80 V as well.

Claude

So the battery reading needs no calibration. The same night I measured drive writes: 235 KB/s onto blank flash, and 118 KB/s when each 64 KB block has to be erased first.

A battery log and a battery gauge

Me

I’d like a battery log in a text file on the flash: a reading every 30 s or so while it’s on, and a wake-up timer that fires once an hour while it’s powered down.

Claude

The hourly wake-up isn’t possible on this board. Off on batteries means the XIAO has no power at all. A timer chip would need a board revision, the firmware couldn’t tell its wake from a button press (a toy that starts playing at 3 a.m.), and all 11 edge GPIOs are in use. The log while it’s on works: BATTERY.CSV, one row every 30 s. Rows wait in RAM and are written every 10 minutes, on pause, at power-off and before a computer takes the drive. Writing every 30 s would wear out the directory’s flash sector in about a year; batching stretches that to decades.

Me

Yes, build it with those defaults.

Claude

I built it, and the host tests caught the log restarting at session 1 after a reboot, which I fixed. Each save takes 100–200 ms of flash work, so I doubled the audio buffer from 186 to 372 ms. Then I flashed a test build that saves every 20 s and asked you to play both songs and listen for dropouts.

Me

Done, no glitches heard.

The knob is only read by the firmware. With it at minimum, holding the button at power-on could run a self-test with beep patterns. Is that a good idea here, or overkill?

Claude

A full self-test is mostly overkill. The faults it could catch already announce themselves, and the ones that matter would stop it from running at all. The gesture is still free, so I suggested a battery gauge instead: 1 to 5 beeps, then off.

Me

Build the battery gauge; the POST would be useless here.

Claude

Five beeps from 4.25 V, four from 4.05 V, three from 3.75 V, two from 3.45 V, and one below that. Then a rising “bip-bip” if all is well, or a long low tone if the flash chip didn’t answer or there are no songs. The beeps play at a fixed volume, since the knob is at zero. I caught one slip of mine before flashing: with no gap, the verdict could be heard as one more level beep, so it now comes 350 ms later.

Me

I heard 5 beeps and a bip-bip on both USB and battery power.

Loose ends: the battery holder and the song

Me

There are two mounting holes in the battery compartment that align with holes in the PCB. What is the intended mounting method?

Claude

There isn’t one: the design never specified a fixing, and the board has two plain 4 mm holes. I recommended M2.5 countersunk screws from inside the holder, 8 mm or shorter, rather than rivets.

Me

As this is rev 0, can I just use some hot glue instead? The only reason additional mechanical attachment is necessary is to take vibrational stress off the solder joints.

Claude

Yes, if the glue is pushed through both holes from inside the holder, so it forms plugs that lock mechanically; hot glue bonds poorly to the holder’s polypropylene. Then you asked whether plastic rivets exist, and that made me revise my “not rivets” advice. Nylon snap rivets fit the holder’s holes, clamp about the right thickness and sit head-up on the board, with no metal near the button and HOLD tracks.

Me

For rev 0, I’ll stick with the hot glue.

Two days later, on October 30, one task remained: the song itself.

Me

The song I want on the device is a WAV. It needs to be mixed down to mono and encoded at the highest bitrate the device can play correctly, without being too large a file for the flash.

Claude

320 kbps mono at 44.1 kHz: the firmware doesn’t limit the bitrate, and 320 kbps is the most an MP3 can carry. Before handing it over, I ran the file through the firmware’s own decoder on the Mac. All 2:47 decoded cleanly, and the loudest point sits 4.6 dB below clipping. At 6.7 MB it takes about 40% of the drive, and on a 2″ mono speaker 128 kbps would sound no different, if you planned more songs.

Me

I kept 320 kbps, since it was only going to hold the one song.

The printed enclosure parts arrived on November 2. Putting the player into them is the next post.