Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions firmware/defmt-itm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
authors = ["The Knurling-rs developers"]
categories = ["embedded", "no-std"]
description = "Transmit defmt log messages over the ITM (Instrumentation Trace Macrocell) stimulus port"
edition = "2018"
keywords = ["knurling", "logging", "formatting"]
license = "MIT OR Apache-2.0"
name = "defmt-itm"
readme = "README.md"
repository = "https://github.com/knurling-rs/defmt"
version = "0.1.0"

[dependencies]
cortex-m = "0.6.4"
defmt = { version = "0.1.2", path = "../.." }
34 changes: 34 additions & 0 deletions firmware/defmt-itm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# `defmt-itm`

> Transmit [`defmt`] log messages over the ITM (Instrumentation Trace Macrocell) stimulus port 0

[`defmt`]: https://github.com/knurling-rs/defmt

## Support

`defmt-rtt` is part of the [Knurling] project, [Ferrous Systems]' effort at
improving tooling used to develop for embedded systems.

If you think that our work is useful, consider sponsoring it via [GitHub
Sponsors].

## License

Licensed under either of

- Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or
http://www.apache.org/licenses/LICENSE-2.0)

- MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)

at your option.

### Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted
for inclusion in the work by you, as defined in the Apache-2.0 license, shall be
licensed as above, without any additional terms or conditions.

[Knurling]: https://knurling.ferrous-systems.com/
[Ferrous Systems]: https://ferrous-systems.com/
[GitHub Sponsors]: https://github.com/sponsors/knurling-rs
76 changes: 76 additions & 0 deletions firmware/defmt-itm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//! [`defmt`](https://github.com/knurling-rs/defmt) global logger over RTT.
//!
//! NOTE when using this crate it's not possible to use (link to) the `rtt-target` crate
Comment thread
jonas-schievink marked this conversation as resolved.
Outdated
//!
//! To use this crate, link to it by importing it somewhere in your project.
//!
//! ```
//! // src/main.rs or src/bin/my-app.rs
//! use defmt_rtt as _;
//! ```

#![no_std]

use core::{
ptr::NonNull,
sync::atomic::{AtomicBool, Ordering},
};

use cortex_m::{interrupt, itm, peripheral::ITM, register};

static ENABLED: AtomicBool = AtomicBool::new(false);

/// Enables defmt logging over the ITM stimulus port 0
Comment thread
japaric marked this conversation as resolved.
Outdated
pub fn enable(itm: ITM) {
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we may want to make the stimulus port configurable in the feature that can be added backwards compatibility by adding a second function with signature fn(ITM, port_number: u8)

// enable stimulus port 0
itm.ter[0].write(1);
drop(itm);
ENABLED.store(true, Ordering::Relaxed);
}

#[defmt::global_logger]
struct Logger;

impl defmt::Write for Logger {
Comment thread
jonas-schievink marked this conversation as resolved.
fn write(&mut self, bytes: &[u8]) {
// NOTE(unsafe) this function will be invoked *after* `enable` has run so this crate now has
// ownership over the ITM thus it's OK to instantiate the ITM register block here
unsafe { itm::write_all(&mut (*ITM::ptr()).stim[0], bytes) }
}
}

static TAKEN: AtomicBool = AtomicBool::new(false);
static INTERRUPTS_ACTIVE: AtomicBool = AtomicBool::new(false);

unsafe impl defmt::Logger for Logger {
fn acquire() -> Option<NonNull<dyn defmt::Write>> {
if !ENABLED.load(Ordering::Relaxed) {
return None;
}

let primask = register::primask::read();
interrupt::disable();
if !TAKEN.load(Ordering::Relaxed) {
// no need for CAS because interrupts are disabled
TAKEN.store(true, Ordering::Relaxed);

INTERRUPTS_ACTIVE.store(primask.is_active(), Ordering::Relaxed);

Some(NonNull::from(&Logger as &dyn defmt::Write))
} else {
if primask.is_active() {
// re-enable interrupts
unsafe { interrupt::enable() }
}
None
}
}

unsafe fn release(_: NonNull<dyn defmt::Write>) {
TAKEN.store(false, Ordering::Relaxed);
if INTERRUPTS_ACTIVE.load(Ordering::Relaxed) {
// re-enable interrupts
interrupt::enable()
}
}
}