From 20377d4522d513b66406d4ef8231a7cdbfedc157 Mon Sep 17 00:00:00 2001 From: Brian Cully Date: Sun, 6 Nov 2022 16:34:44 -0500 Subject: cargo: rejigger crate type MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit convert to hybrid crate, with arch-dependent stuff as a ‘bin’ crate, with the underlying support in ‘lib’. the reason for this change is to allow for automated unit-testing on the device-independent bits. to facilitate this, a ‘t’ alias is provided which will run the unit tests. this target assumes you're on a linux system, but you can't have everything. --- src/logger.rs | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 src/logger.rs (limited to 'src/logger.rs') diff --git a/src/logger.rs b/src/logger.rs new file mode 100644 index 0000000..453e268 --- /dev/null +++ b/src/logger.rs @@ -0,0 +1,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>> = 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; +} -- cgit v1.2.3