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

use stm32f1xx_hal::{
    serial::Tx,
    pac::USART1,
};

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

#[macro_export]
macro_rules! log {
    ($($args:tt)+) => {
        $crate::log::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<Tx<USART1>>>> = Mutex::new(RefCell::new(None));

pub fn init(tx: Tx<USART1>) {
    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));
    });
}