-
-
Notifications
You must be signed in to change notification settings - Fork 125
add defmt-itm #335
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
add defmt-itm #335
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 = "../.." } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| //! | ||
| //! 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 | ||
|
japaric marked this conversation as resolved.
Outdated
|
||
| pub fn enable(itm: ITM) { | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| // 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 { | ||
|
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() | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.