aboutsummaryrefslogtreecommitdiffstats
path: root/src/logger.rs
diff options
context:
space:
mode:
authorBrian Cully <bjc@kublai.com>2022-11-06 16:34:44 -0500
committerBrian Cully <bjc@kublai.com>2022-11-06 16:37:38 -0500
commit20377d4522d513b66406d4ef8231a7cdbfedc157 (patch)
tree3b391f48c056c8a0fadbb3915dac11984a8b814d /src/logger.rs
parenta484a97111d0897ac6e0e291c4432a91ebdee416 (diff)
downloadluchie-20377d4522d513b66406d4ef8231a7cdbfedc157.tar.gz
luchie-20377d4522d513b66406d4ef8231a7cdbfedc157.zip
cargo: rejigger crate type
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.
Diffstat (limited to 'src/logger.rs')
-rw-r--r--src/logger.rs52
1 files changed, 52 insertions, 0 deletions
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<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>;
+}