Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

The serial monitor and defmt

nanoflash monitor shows what the application of a board writes to its serial port, and sends what the user types. nanoflash run uses the same monitor after it flashes. This page explains the choices behind it. The options are in Command line.

Why the monitor is in the command

The monitor is in nanoflash-cli, not in the library. It needs a clock, a terminal and threads, and it must not reach the WebAssembly modules.

Signals after the port opens

After it opens a port, the monitor sets DTR and RTS:

  • both cleared for an Espressif board and for a port whose USB identity is Espressif or unknown, because a UART bridge may drive the reset and boot pins of an ESP32;
  • DTR set for the others (a known AVR or SAM-BA identity), because Arduino sketches wait for DTR before they print.

A port that refuses the lines (a pseudo-terminal) only gets a note.

--baud-rate 1200 is refused for a board whose reset is a 1200-baud touch, because that rate makes the sketch restart into its bootloader.

Devices that leave and come back

A reset, or a sketch that restarts its USB device, makes the device leave. The monitor then waits up to 30 s for the same device and opens it again. “The same device” is the application-devices of the board, else the exact USB identity of the port, else the same path. If it does not come back, the exit code is 2.

The defmt decoder

monitor --elf <file> decodes the defmt frames among the text of the port, with defmt-decoder, a dependency of the command only. The decoder (monitor/defmt.rs) reads the table of the .defmt section and the locations of the DWARF information.

  • Framing. The decoder accepts the rzcobs encoding only. A frame in rzcobs never contains a 00 byte, so a frame can be found in a byte stream. The framing is the one of esp-println: the bytes ff 00, the frame, and the byte 00. A plain stream of frames (defmt-serial) is not supported. Every other byte is text.
  • State machine. The decoder is a state machine over the bytes, so a frame may span several reads, and the pieces do not depend on how the reads split the bytes (a property test checks it). A byte ff at the end of a read is kept until the next read decides.
  • Isolation. Each frame is decoded by a new stream decoder, under catch_unwind (guard.rs): a panic or an error gives a malformed frame.
  • End of stream. When the device leaves or the monitor ends, the decoder ends its stream (finish). The kept byte is text, and an unfinished frame is malformed, so half a frame of the old connection does not hide the text of the new one.
  • Order. With --json, the unfinished line is emitted before a frame, so the order of the output is kept.
  • run decodes by itself. run decodes the frames when its image is an ELF file with a defmt table. A table that it cannot use gives a note, not an error, because the user did not ask for decoding. monitor --elf with a file that has no table is an input error.

Limits of the decoder

The limits that change what the user sees (a ff 00 in plain text, the frame size of 4096 bytes, the end of the process and the terminal in raw mode) are in Decode defmt logs. This section gives their causes.

  • The framing cannot tell a ff 00 in plain text from the start of a real frame.
  • The limit of 4096 bytes bounds the nesting of the formats in a frame, which defmt-decoder decodes recursively without a depth limit. A frame of more than 64 bytes is decoded on a thread with a stack of 64 MiB (the system fills it only when it is used), because a frame of 4096 bytes can nest about 3000 levels and overflows a smaller stack in a build without optimisation. If the thread cannot start, the frame is malformed.
  • defmt-decoder 1.2.0 can still end the process. It reads the count of a {=[?]} argument from the frame and allocates the memory for that count before it reads an element. A count of 0xffffffff fails the allocation, and Rust ends the process: catch_unwind does not catch it. A pre-parser of the frame would be needed to prevent it. The end skips the Drop of the console that restores the terminal.
  • The tests do not cover the file, line and module of a frame: they need DWARF information in the fixture, and a hand-written one is long. The conversion is a copy of the fields of defmt-decoder.