aboutsummaryrefslogtreecommitdiffstats
path: root/src/logger.rs
blob: 453e26886e3eae9c655eae778e9eaf295345a872 (plain)
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
use core::{
    cell::RefCell,
    fmt::{write, Arguments},
};

use cortex_m::interrupt::{self, Mutex};

#[macro_export]
macro_rules! log {
    ($($args:tt)+) => {
        $crate::logger::log_args(core::format_args!($($args)+))
    }
}

#[macro_export]
macro_rules! logln {
    () => ({ kprint!("\r\n") });
    ($fmt: literal $(, $($arg: tt)+)?) => {
        log!(concat!($fmt, "\n") $(, $($arg)+)?)
    }
}

static TX: Mutex<RefCell<Option<arch::Writer>>> = Mutex::new(RefCell::new(None));

pub fn init(tx: arch::Writer) {
    interrupt::free(|cs| {
        TX.borrow(cs).replace(Some(tx));
    });
}

pub fn log_args(args: Arguments) {
    interrupt::free(|cs| {
        TX.borrow(cs)
            .borrow_mut()
            .as_mut()
            .map(|tx| write(tx, args));
    });
}

/*
 * Because the logger needs to have a size in order to be allocated
 * statically, we need to pull in architecture-specific details about
 * the implementation.
 *
 * By putting everything in an `arch` module, we can more easily swap
 * things out at compile time with feature flags, or a similar
 * mechanism.
 */
mod arch {
    use stm32f1xx_hal::{pac::USART1, serial::Tx};
    pub type Writer = Tx<USART1>;
}