use cortex_m::interrupt::Mutex;
use crate::destination;
use crate::modes::InterruptModer;
use core::marker::PhantomData;
use core::fmt::{self, Write};
pub struct Itm<M: InterruptModer> {
inner: destination::Itm,
_mod: PhantomData<M>
}
impl<M: InterruptModer> Itm<M> {
pub fn new(itm: destination::Itm) -> Self {
Self { inner: itm, _mod: PhantomData }
}
}
impl<Mode: InterruptModer> super::Printer for Itm<Mode> {
type W = destination::Itm;
type M = Mode;
#[inline]
fn destination(&mut self) -> &mut Self::W {
&mut self.inner
}
}
pub struct ItmSync<M: InterruptModer> {
inner: destination::Itm,
lock: Mutex<()>,
_mod: PhantomData<M>,
}
impl<M: InterruptModer> ItmSync<M> {
pub fn new(itm: destination::Itm) -> Self {
Self { inner: itm, lock: Mutex::new(()), _mod: PhantomData }
}
}
impl<Mode: InterruptModer> super::Printer for ItmSync<Mode> {
type W = destination::Itm;
type M = Mode;
#[inline]
fn destination(&mut self) -> &mut Self::W {
&mut self.inner
}
#[inline]
fn print(&mut self, args: fmt::Arguments) {
cortex_m::interrupt::free(|cs| {
let _lock = self.lock.borrow(cs);
let _ = self.inner.write_fmt(args);
});
}
#[inline]
fn println(&mut self, args: fmt::Arguments) {
cortex_m::interrupt::free(|cs| {
let _lock = self.lock.borrow(cs);
let _ = self.inner.write_fmt(args);
let _ = self.inner.write_str("\n");
});
}
}
unsafe impl<Mode: InterruptModer> Sync for ItmSync<Mode> {}
pub struct ItmAssumeSync {
inner: destination::Itm,
}
impl ItmAssumeSync {
pub unsafe fn new(itm: destination::Itm) -> Self {
Self { inner: itm }
}
}
impl super::Printer for ItmAssumeSync {
type W = destination::Itm;
type M = crate::modes::InterruptOk;
#[inline]
fn destination(&mut self) -> &mut Self::W {
&mut self.inner
}
#[inline]
fn print(&mut self, args: fmt::Arguments) {
let _ = self.inner.write_fmt(args);
}
#[inline]
fn println(&mut self, args: fmt::Arguments) {
let _ = self.inner.write_fmt(args);
let _ = self.inner.write_str("\n");
}
}
unsafe impl Sync for ItmAssumeSync {}
pub type InterruptFree = Itm<crate::modes::InterruptFree>;
pub type InterruptOk = Itm<crate::modes::InterruptOk>;
pub type InterruptSync = ItmSync<crate::modes::InterruptFree>;