cortex_m_log/printer/
semihosting.rs

1//! Semihosting module
2//!
3//! Semihosting can direct to either stdout or stderr,
4//! which is determined by second type parameter
5//!
6//! ## Example
7//!
8//! ```rust,no_run
9//! use cortex_m_log::printer::Printer;
10//! use cortex_m_log::printer::semihosting;
11//! use cortex_m_log::printer::semihosting::Semihosting;
12//! use cortex_m_log::modes::InterruptFree;
13//!
14//! //Create new printer to stdout with interrupt free mode
15//! let mut shost = Semihosting::<InterruptFree, _>::stdout().unwrap();
16//!
17//! shost.println(format_args!("Write interrupt free to {}", "stdout"));
18//!
19//! //Create new printer to stderr without need for interrupt control using special alias
20//! let mut shost = semihosting::InterruptOk::<_>::stderr().unwrap();
21//!
22//! shost.println(format_args!("Write to {}", "stderr"));
23//! ```
24
25extern crate cortex_m_semihosting as sh;
26
27pub use self::sh::hio;
28
29use crate::destination::semihosting::SemihostingComp;
30use crate::modes::InterruptModer;
31
32use core::marker::PhantomData;
33
34/// Semihosting backed printer
35pub struct Semihosting<M: InterruptModer, T: SemihostingComp> {
36    inner: T,
37    _mod: PhantomData<M>
38}
39
40impl<M: InterruptModer, T: SemihostingComp> Semihosting<M, T> {
41    #[inline(always)]
42    ///Constructs new instance
43    pub fn new(inner: T) -> Self {
44        Self { inner, _mod: PhantomData }
45    }
46}
47
48impl<M: InterruptModer> Semihosting<M, hio::HostStream> {
49    #[inline(always)]
50    ///Constructs new Semihosting Printer by using `hstdout`
51    pub fn stdout() -> Result<Self, ()> {
52        hio::hstdout().map(Self::new)
53    }
54
55    #[inline(always)]
56    ///Constructs new Semihosting Printer by using `hstderr`
57    pub fn stderr() -> Result<Self, ()> {
58        hio::hstderr().map(Self::new)
59    }
60}
61
62impl<Mode: InterruptModer, T: SemihostingComp> super::Printer for Semihosting<Mode, T> {
63    type W = T;
64    type M = Mode;
65
66    #[inline]
67    fn destination(&mut self) -> &mut Self::W {
68        &mut self.inner
69    }
70}
71
72/// Alias for Interrupt free Printer
73pub type InterruptFree<T> = Semihosting<crate::modes::InterruptFree, T>;
74/// Alias for Printer without control over interrupts
75pub type InterruptOk<T> = Semihosting<crate::modes::InterruptOk, T>;