diff options
author | 2017-03-08 13:54:56 -0500 | |
---|---|---|
committer | 2017-03-08 13:54:56 -0500 | |
commit | 559da5e265d4c1ac88c8bf07f5a4d317c922b408 (patch) | |
tree | ba3324cdda1a2d664ac6235a039ebbf67f8feec6 /src | |
parent | 0e628b32ac27d47aa897dfd7930ddad85903bd37 (diff) | |
download | cortex-m-559da5e265d4c1ac88c8bf07f5a4d317c922b408.tar.gz cortex-m-559da5e265d4c1ac88c8bf07f5a4d317c922b408.tar.zst cortex-m-559da5e265d4c1ac88c8bf07f5a4d317c922b408.zip |
add macros for writing to an ITM port
Diffstat (limited to 'src')
-rw-r--r-- | src/itm.rs | 38 | ||||
-rw-r--r-- | src/lib.rs | 1 | ||||
-rw-r--r-- | src/macros.rs | 25 |
3 files changed, 64 insertions, 0 deletions
diff --git a/src/itm.rs b/src/itm.rs new file mode 100644 index 0000000..6022bbe --- /dev/null +++ b/src/itm.rs @@ -0,0 +1,38 @@ +//! Instrumentation Trace Macrocell + +use core::fmt; + +struct Itm { + port: u8, +} + +impl Itm { + fn write_all(&self, buffer: &[u8]) { + let stim = + unsafe { &(*::peripheral::ITM.get()).stim[self.port as usize] }; + + for byte in buffer { + while !stim.is_fifo_ready() {} + stim.write_u8(*byte); + } + } +} + +impl fmt::Write for Itm { + fn write_str(&mut self, s: &str) -> fmt::Result { + self.write_all(s.as_bytes()); + Ok(()) + } +} + +/// Writes `fmt::Arguments` to the ITM `port` +pub fn write_fmt(port: u8, args: fmt::Arguments) { + use core::fmt::Write; + + Itm { port }.write_fmt(args).ok(); +} + +/// Writes a string to the ITM `port` +pub fn write_str(port: u8, string: &str) { + Itm { port }.write_all(string.as_bytes()) +} @@ -29,6 +29,7 @@ pub mod asm; pub mod ctxt; pub mod exception; pub mod interrupt; +pub mod itm; pub mod peripheral; pub mod register; diff --git a/src/macros.rs b/src/macros.rs index bd31167..1c39f18 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -27,3 +27,28 @@ macro_rules! hprintln { ($fmt:expr) => (hprint!(concat!($fmt, "\n"))); ($fmt:expr, $($arg:tt)*) => (hprint!(concat!($fmt, "\n"), $($arg)*)); } + +/// Macro for sending a formatted string through an ITM channel +#[macro_export] +macro_rules! iprint { + ($channel:expr, $s:expr) => { + $crate::itm::write_str($channel, $s); + }; + ($channel:expr, $($arg:tt)*) => { + $crate::itm::write_fmt($channel, format_args!($($arg)*)); + }; +} + +/// Macro for sending a formatted string through an ITM channel, with a newline. +#[macro_export] +macro_rules! iprintln { + ($channel:expr) => { + iprint!($channel, "\n"); + }; + ($channel:expr, $fmt:expr) => { + iprint!($channel, concat!($fmt, "\n")); + }; + ($channel:expr, $fmt:expr, $($arg:tt)*) => { + iprint!($channel, concat!($fmt, "\n"), $($arg)*); + }; +} |