cnt

Crates.iocnt
lib.rscnt
version0.1.0
created_at2026-01-11 15:54:42.248736+00
updated_at2026-01-11 15:54:42.248736+00
descriptionRAM counters macro for microcontrollers, when logging is not an option
homepage
repositoryhttps://github.com/romixlab/embedded_bedrock
max_upload_size
id2036000
size52,574
Roman Isaikin (romixlab)

documentation

README

cnt

Crates.io Version

logo

When logging is not an option - count

In microcontroller firmwares it is not always possible or desirable to log things, for example due to:

  • Timing constraints in interrupts
  • Memory constraints
  • Absence of a logging interface
  • Absence of log recording from firmware boot, making accurate calculations impossible
  • Inconvenience of analyzing log output

This crate provide a convenient way to count events, errors or anything else, using a continuous RAM array:

fn high_frequency_irq() {
    let r = do_something();
    cnt::cnt_if!(r.is_err(), unpack_errors: u64);
}

Under the hood, a simple linker trick is used to obtain a unique ID for each count statement (similar to defmt). Then an element of a _CNT_RAM_BUFFER is increment (or two in the case of u64).

u32 counters are supported as well, and you can pass true to count unconditionally:

fn process_packet() {
    cnt::cnt_if!(true, packet_count: u32);
}

How to use

  • Add cnt = "0.1.0" to Cargo.coml
  • Add "-C", "link-arg=-Tcnt.x", to config.toml
  • Optionally set CNT_RAM_BUFFER_SIZE_WORDS in the [env] section as well, default value is 64 words (256 bytes).

How to get counters data from fw itself

Call counters_ram_buffer:

fn main() {
    let counters: &[u32] = cnt::counters_ram_buffer();
    // send using whatever interface to host
}

How to get counters data live from a running device

Basic idea is to read _CNT_RAM_BUFFER from RAM using JTAG or SWD interface. A CLI tool to do that is not yet ready though.

How to get IDs

CLI tool to analyze ELF and present the data in a nice way is not yet ready. But for now, a simple nm command will show all the counters present:

arm-none-eabi-nm ./path/to/elf_fw | grep cnt_ram
Commit count: 51

cargo fmt