Fitting a pager decoder into 61,440 bytes of flash
How the UV-K5 Receiver firmware freed room in a full handheld radio and used it for POCSAG and MDC-1200 decoding, with host-side tests for every decoder.
The UV-K5 is an inexpensive handheld radio with an unusually capable receiver chip, the BK4819, and a microcontroller with 61,440 bytes of flash for its firmware. A popular open-source custom firmware for it uses about 60,800 of those bytes, leaving roughly 640 free. That is not enough room for a new decoder. The UV-K5 Receiver firmware started by making space.
Making room before adding anything
The first step was a receiver-focused baseline. Features that a monitoring radio does not need, such as the FM broadcast receiver and the spectrum display, were switched off. The spectrum view alone takes about 5 KB. The result uses 46,136 bytes and leaves 15,304 free, about twenty-four times the original headroom.
Nothing was deleted outright. Each removed feature is a one-line flag in the build file, so anyone who wants it back can turn it on and rebuild. The compiler version is pinned as well, because a different compiler can produce a larger image or new warnings, and the build treats every warning as an error. In a budget this tight, a few hundred bytes of compiler drift can break the build.
The baseline also opens up wider receive coverage, from 18 to 630 MHz and from 840 to 1300 MHz. The gap in the middle is a limit of the BK4819’s synthesizer, not of the software.
Let the radio chip do the hard part
The BK4819 has a built-in FSK modem, and that modem is what makes decoding possible in so little code. For POCSAG, the pager protocol, the modem watches for the 32-bit sync word 0x7CD215D8 and then hands the firmware a 64-byte batch of 16 codewords. The microcontroller never has to demodulate audio itself.
The firmware’s part is to make sense of each batch. It checks every 32-bit codeword against its BCH(31,21) code and parity, recovers the pager address and the frame it arrived in, and assembles the message text from 7-bit ASCII. Errors are detected, not corrected: a damaged codeword is flagged rather than guessed at, so the radio never shows a page it is not sure of. Pages at 1200 baud are supported, and 512 baud is experimental.
Because there is only one modem, the radio can listen for POCSAG or for MDC-1200, not both at once. Rather than hide that, the menu has a single Data setting that picks one.
MDC-1200 in a few hundred lines
MDC-1200 is the short data burst many two-way radio systems send at the start or end of a transmission to identify the unit. It uses 1200 and 1800 Hz audio tones. The firmware receives a 112-bit codeword, undoes the interleaving and checks a CRC-16/CCITT. There is no error correction, so the CRC is the only defense against a wrong ID. In testing, every corrupted frame in a systematic sweep was rejected and none produced a wrong ID.
MDC decoding has been confirmed on the air, decoding real transmissions as well as the test frames.
Showing what went wrong
A decoder that shows nothing is hard to debug from the field. The screen keeps two counters: how many times the sync pattern was found and how many frames then failed their checks. If sync never fires, the problem is the signal or the tuning. If sync fires and frames fail, the problem is noise or the decoder. A data page keeps the last 16 messages so a burst of pages is not lost while the radio is in a pocket.
Testing without a radio
Flashing a radio for every change is slow, and a handheld has no room for a debugger. So the decoders also build for a desktop computer, with stand-ins for the BK4819 registers, and a host test suite runs them against known frames and deliberately corrupted ones. Frame sync, BCH checking and CRC-16 are exercised on every change before anything touches hardware. The same habit runs through the other firmware in the portfolio: the DM-1701 P25 Workspace self-tests under a CPU emulator, and the DM-1701 Tier III Monitor was built step by step from recorded captures.
Lessons for any constrained device
- Measure the budget first, and make room before writing new code.
- Make removals reversible with build flags, so nothing is lost for good.
- Pin the toolchain when every byte counts.
- Use the hardware’s own features before writing software to replace them.
- Prefer detecting an error to guessing a fix, and show the user which kind of failure it was.
- Build the core logic for a desktop too, so it can be tested on every change.
Receiving pager and data signals may be restricted where you live, and the firmware’s documentation says so. The techniques apply to any small device that needs a new protocol taught to it without new hardware.