Microcontrollers
On this page
This page shows how to build dmrgo with TinyGo for a microcontroller, which boards CI builds, and the memory and CPU limits of the radio code.
What you need
- TinyGo 0.41 or 0.44. CI uses the TinyGo 0.44 image:
image: tinygo/tinygo:0.44.0@sha256:98126534a0653966913b803d26805ea79250a244217d921d74044cfedc1d24a2- The Go release that your TinyGo needs on the
PATH: Go 1.25 to 1.27 for TinyGo 0.44, and Go 1.24 to 1.26 for TinyGo 0.41. - A board that TinyGo supports, such as a Raspberry Pi Pico, and a USB cable. The steps below use the Pico.
- A clone of the repository. The example embeds a capture from it.
Build and flash
Connect the board, then from the v2 directory of the clone:
tinygo flash -target=pico ./examples/tinygov2/examples/tinygo decodes and encodes again each burst of a short voice call, follows the call with a voice.Receiver and relays it with a voice.Transmitter. It runs the capture once a second.
Run
Open the serial console of the board, for example with tinygo monitor. Each second the example prints one line with its counts. The Go example of the program runs the same code once and prints these counts:
bursts 15 voice 13 data 2 re-encoded 15 corrected 0
call from 3009999 to 9990 calls 1 end Terminator relayed 20
To run that example on the host:
go test -run '^Example$' -v ./examples/tinygoBoards and builds in CI
The TinyGo job of CI builds these targets from the v2 directory:
- name: Build the example
run: |
tinygo build -target=pico -size short -o tinygo-pico.uf2 ./examples/tinygo
for target in pico2 esp32c3-generic wasip1; do
tinygo build -target="$target" -o "tinygo-$target.elf" ./examples/tinygo
done
- name: Build the receiver and modulator examples for WebAssembly
run: |
for target in wasm wasip1; do
for example in rxaudio txaudio txiq; do
tinygo build -target="$target" -o "$example-$target.wasm" "./examples/$example"
done
done
- name: Build the Tier 0 receiver example without a heap
run: tinygo build -target=feather-m4 -serial=uart -gc=none -scheduler=none -size short -o rxaudio.elf ./examples/rxaudio
- name: Build the Tier 0 modulator examples without a heap
run: |
tinygo build -target=feather-m4 -serial=uart -gc=none -scheduler=none -size short -o txaudio.elf ./examples/txaudio
tinygo build -target=feather-m4 -serial=uart -gc=none -scheduler=none -size short -o txiq.elf ./examples/txiqpico,pico2andesp32c3-genericare the Raspberry Pi Pico (RP2040), the Pico 2 (RP2350) and a generic ESP32-C3 board.feather-m4is a Cortex-M4F board. The builds with-gc=nonehave no heap: the link fails when the program allocates. v2/examples/rxaudio is the Tier 0 receiver in static memory. v2/examples/txaudio and v2/examples/txiq are the Tier 0 modulators.wasmandwasip1are WebAssembly. See Browser.
The same job runs the library tests with TinyGo, except the commands, rf and the examples other than examples/tinygo, and runs the allocation tests at the stack size of the Pico:
- name: Test the library
run: tinygo test $(go list ./... | grep -v -e /cmd/ -e /examples/ -e /rf$) ./examples/tinygo
- name: Test the example at the pico stack size
run: tinygo test -stack-size=8KB ./examples/tinygo
- name: Test the allocations at the pico stack size and collector
run: |
tinygo test -gc=conservative ./internal/allocs
tinygo test -stack-size=8KB -gc=conservative ./internal/allocs
- name: Check the heap allocation sites of the whole API for the pico and riscv-qemu
run: DMRGO_TINYGO_ALLOCS=1 go test -run 'TestTinyGoHeapAllocations|TestTinyGoTablesInFlash' -v ./internal/allocgateTier 0 limits
Tier 0 is the tier of rf for a microcontroller. Init sets up a Tier 0 receiver or transmitter in memory of the caller, such as a package-level variable, and does not allocate. Under TinyGo, the default tier is Tier 0. The doc comments of the four types give their budgets.
rf.Receiver.Init, for I/Q that a radio chip filtered to the channel:
Init sets up r for c, in place of any state it holds, in Tier0 at a Rate of 9600 or 12000. It does not allocate, so that r can live in memory of the caller. On a Cortex-M4F at 9.6 kHz the receiver needs up to 7.2 MHz and 3,000 B with its stack, built with TinyGo -opt z, over 2 KB by the accepted cost of the detection of standalone RC bursts and of the MS bursts of both TDMA slots and of the clock rate tracking. Init returns dmrgo.ErrNilReceiver for a nil r, and a *dmrgo.ConfigError, with r unchanged, when a field of c is out of range.
rf.AudioReceiver, for the output of an FM discriminator:
AudioReceiver recovers DMR bursts from the output of an FM discriminator, as the radio chips of hotspots and MMDVM modems give it: real samples in any units, with any gain and DC offset, which the receiver measures itself. It takes audio of either polarity. Above Tier0, an AudioReceiver from NewAudioReceiver also takes audio that a high-pass filter cut below about 300 Hz, as the speaker output of a scanner gives it. With AudioRxConfig.PreEmphasis, every tier also takes the de-emphasized audio of the speaker or accessory output of a radio. With Tier0 it keeps about 1.4 KB of state, for microcontrollers; on a Cortex-M4F at 9.6 kHz it needs up to 5.9 MHz and 2,628 B with its stack, built with TinyGo -opt z, over 2 KB by the accepted cost of the detection of standalone RC bursts and of the MS bursts of both TDMA slots and of the clock rate tracking. An AudioReceiver that Init sets up in memory of the caller, such as a package variable, uses no heap in stream mode. An AudioReceiver is not safe for concurrent use, and it must not be copied after Init or NewAudioReceiver.
rf.Transmitter.Init, for I/Q:
Init sets up t for c, in place of any state it holds, in Tier0 at a Rate of 96000. It does not allocate, so that t can live in memory of the caller. It returns dmrgo.ErrNilReceiver for a nil t, and a *dmrgo.ConfigError or *RegionError, with t unchanged, as NewTransmitter does.
rf.AudioTransmitter.Init, for the modulator input of an FM radio:
Init sets up t for c, in place of any state it holds, in Tier0 at a Rate of 24000 or 48000. It does not allocate, so that t can live in memory of the caller. It returns the errors of Transmitter.Init.
The Receiver datasheet gives the sensitivity of each tier, and the Transmitter datasheet gives the CPU, RAM and flash of the Tier 0 modulators on a Cortex-M4F.
Higher tiers
Tier 0 is the tier for the smallest boards. A board with more RAM and CPU, such as an STM32H7 or an ESP32-S3, can run a higher tier. Set Tier in the config of rf.NewAudioReceiver or rf.NewReceiver:
rx, err := rf.NewAudioReceiver(&rf.AudioRxConfig{Rate: 9600, Tier: rf.Tier1, OnBursts: onBursts})- The constructors allocate the stages of the tier once. After that,
WriteandFlushdo not allocate. Build with a heap, for example-gc=leakingor-gc=conservative, not-gc=none. - A program that calls only
Initlinks none of the higher tiers. Its size is the size of a Tier 0 build. - The transmitters take tiers 1 and 2 the same way, through
rf.NewTransmitterandrf.NewAudioTransmitter. - Use a target with a hardware FPU. The
cortex-m7target of TinyGo uses soft float.cmd/mcubenchbuilds for a Cortex-M7 with an FPv5-D16 FPU.
The sample rates that each tier takes:
| Input | Tier 0 | Tier 1 | Tier 2 | Tier 3 |
|---|---|---|---|---|
| FM discriminator audio | 8 kHz to 48 kHz | 8 kHz to 1 MHz | 8 kHz to 1 MHz | 8 kHz to 1 MHz |
| I/Q | 9.6 kHz, 12 kHz, and 24 kHz up | 24 kHz up | 48 kHz up | 48 kHz up |
The Receiver datasheet gives the CPU, RAM and flash of tiers 1 and 2 on a Cortex-M7, and the RAM of each tier on a PC. An STM32H743 links 512 KB of RAM for TinyGo, and an ESP32-S3 has 416 KB of data RAM under TinyGo.
Heap rules
Decode and encode make no heap garbage at the default stack size of these targets (8 KB). TinyGo puts each local variable larger than 256 bytes on the heap, and the library has no such local on those paths.
TinyGo also puts a local variable of your code on the heap when its address goes into the library, such as the Data of layer2.BuildBurst. The package doc of dmrgo lists those calls. Keep such values in package-level variables, or in a struct that you allocate once. v2/examples/rxaudio keeps its receiver and its sample buffer at package level:
var (
rx rf.AudioReceiver
buf [256]int16
bursts int
)Its main then sets up the receiver in that memory with Init and writes the samples to it:
func main() {
if err := rx.Init(rf.AudioRxConfig{Rate: 9600, OnBursts: onBursts}); err != nil {
return
}
for range 1000 {
read(buf[:])
if _, err := rx.Write(buf[:]); err != nil {
return
}
}
if err := rx.Flush(); err != nil {
return
}
println("bursts:", bursts > 0)
}Allocation lists each place where the library can allocate, and the tests that check it.
Other limits
- vocoder/codec2 always uses its stub under TinyGo, so codec2.New returns codec2.ErrUnavailable.
- CI does not run the tests of
rfunder TinyGo. Thefeather-m4builds above check that its Tier 0 code builds and links without a heap,internal/rxtierschecks under TinyGo that tiers 1 and 2 decode the same bursts as with gc, andinternal/txtiersthat the transmitters of tiers 1 and 2 give the same samples.