Why Ferrite is Rust(ing)
Almost every radio firmware on Earth is C. Ferrite is Rust. Here's why.
Almost every radio firmware on Earth is written in C. The chip vendors ship C. The examples are C. The forum posts from 2011 that you end up reading at midnight are C. When you've got 96 KB of RAM and no operating system, C is what you reach for without thinking.
So naturally, I wrote Ferrite in Rust.
Before you close the tab: this isn't a "rewrite it in Rust" sermon. I picked it before a single line of Ferrite ran on the radio, for boring, practical reasons. They just happen to be very good boring, practical reasons.
It's a computer. With a transmitter.
Pop the back off a Radtel RT-950 Pro and you'll find an Artery AT32F403A (a Cortex-M4 rated for 240 MHz; Ferrite runs it at up to 120), 96 KB of RAM, two Beken BK4829 transceiver chips and an SI4732 for broadcast and shortwave. That little M4 decides what frequency the transmitter keys up on, how hard, and for how long.
Most software bugs give you a crash dialog. Radio firmware bugs give you a carrier on the wrong frequency, and possibly a polite email from someone with a direction-finding antenna.
So I wanted the meanest compiler I could find. Rust's will flat out refuse to build a whole family of classic
firmware bugs: memory used after it's gone, two tasks scribbling on the same thing at once, the match that forgot
a case. Every one of those is a bug I never had to hunt down.
The bench is a time suck
Here's the loop for radio firmware: build an image, flash it through the stock bootloader over the programming cable, then go test it on a real radio with a real signal. Minutes, every single time. The flash alone is about 85 seconds of staring at a progress bar, which is exactly one cup of coffee if you don't stop to let it cool.
A bug the compiler catches costs seconds. A bug the bench catches costs another coffee. A bug the bench doesn't catch costs a lot more than that. So a strict compiler is worth its weight in gold: it's the grumpiest code reviewer I've ever had, it reviews every line, and if it compiles, a lot of things are already true.
A third heavier, and worth it
Before I committed to it, I ported my first C test image to Rust, feature for feature. The Rust build came out about a third bigger. The radio has the room, and I'll trade bytes for bugs-that-can't-happen any day.
Size still bites, though. One innocent extra branch in a drawing routine got copied into every screen that draws, and fixing it gave back 19,312 bytes. Nineteen thousand, three hundred and twelve bytes. For an if-statement. How we keep Ferrite lean is a post of its own: the Rust diet.
Ferrite runs everywhere
Being able to test and simulate the radio on my desk was one of the big reasons for Rust. Everything above the registers builds for a PC as well as for the radio, so thousands of tests run before anything gets flashed, the screen recordings on this site are the firmware's own drawing code, and Ferrite Console runs the radio's own code in your browser. More on this in a future blog post.
Naps are free
Under the hood it's Embassy, an async framework built for exactly this kind of chip. The radio, the screen, the keys, APRS and GPS are each a task that naps until there's work to do. And when everything's napping, Ferrite drops the core from 120 MHz all the way down to 24 MHz.
Sitting on a quiet channel, the stock firmware draws over 100 mA. Ferrite was built around power from the start, switching off everything it isn't using, and that alone gets it to 64.7 mA with the clock pinned at full speed. Let the core nap at 24 MHz and it's 18.6 mA. That's about 107 hours on the stock battery pack. My exact words when the number came in were "this is massive", and I'm sticking with them. (Fair warning: resting is the best case. Scanning, mashing keys and APRS bring the clock back up while they run.)
Out now
Ferrite 0.9 is out: free, with its source public. Get it here, go see what it does, and stick around here. There's a lot more bench work to write up, and some of it involves a bladeRF and questionable life choices.
Questions or comments: the Ferrite Discord. New posts by feed.