1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//! Semihosting module

extern crate cortex_m;
extern crate cortex_m_semihosting as sh;

use self::sh::hio;

use ::destination::semihosting::SemihostingComp;
use ::modes::InterruptModer;

use core::marker::PhantomData;

/// Semihosting backed printer
pub struct Semihosting<M: InterruptModer, T: SemihostingComp> {
    inner: T,
    _mod: PhantomData<M>
}

impl<M: InterruptModer, T: SemihostingComp> Semihosting<M, T> {
    ///Constructs new instance
    pub fn new(inner: T) -> Self {
        Self { inner, _mod: PhantomData }
    }
}

impl<M: InterruptModer> Semihosting<M, hio::HStdout> {
    ///Constructs new Semihosting Printer by using `HStdout`
    pub fn stdout() -> Result<Self, ()> {
        hio::hstdout().map(|stdout| Self::new(stdout))
    }
}

impl<M: InterruptModer> Semihosting<M, hio::HStderr> {
    ///Constructs new Semihosting Printer by using `HStderr`
    pub fn stderr() -> Result<Self, ()> {
        hio::hstderr().map(|stderr| Self::new(stderr))
    }
}

impl<Mode: InterruptModer, T: SemihostingComp> super::Printer for Semihosting<Mode, T> {
    type W = T;
    type M = Mode;

    #[inline]
    fn destination(&mut self) -> &mut Self::W {
        &mut self.inner
    }
}

/// Alias for Interrupt free Printer
pub type InterruptFree<T> = Semihosting<::modes::InterruptFree, T>;
/// Alias for Printer without control over interrupts
pub type InterruptOk<T> = Semihosting<::modes::InterruptOk, T>;