| Crates.io | cnt |
| lib.rs | cnt |
| version | 0.1.0 |
| created_at | 2026-01-11 15:54:42.248736+00 |
| updated_at | 2026-01-11 15:54:42.248736+00 |
| description | RAM counters macro for microcontrollers, when logging is not an option |
| homepage | |
| repository | https://github.com/romixlab/embedded_bedrock |
| max_upload_size | |
| id | 2036000 |
| size | 52,574 |
When logging is not an option - count
In microcontroller firmwares it is not always possible or desirable to log things, for example due to:
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);
}
cnt = "0.1.0" to Cargo.coml"-C", "link-arg=-Tcnt.x", to config.tomlCNT_RAM_BUFFER_SIZE_WORDS in the [env] section as well, default value is 64 words (256 bytes).Call counters_ram_buffer:
fn main() {
let counters: &[u32] = cnt::counters_ram_buffer();
// send using whatever interface to host
}
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.
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