This commit is contained in:
2024-03-24 16:29:24 +01:00
parent 84f8222b30
commit 4a528eb4b7
44 changed files with 5438 additions and 328 deletions

View File

@ -1,23 +1,25 @@
use core::cell::RefCell;
use core::{cell::RefCell, fmt::Write};
use critical_section::Mutex;
use rtt_target::{rtt_init, UpChannel};
pub type RttWriteFn = fn(&[u8]);
use crate::logger::{LogOutput, TimestampedRecord};
pub type RttWriteFn = fn(&str);
static CHANNEL: Mutex<RefCell<Option<UpChannel>>> = Mutex::new(RefCell::new(None));
/// Write directly to the rtt output. Must call [init_rtt_logger] first.
pub fn rtt_write(bytes: &[u8]) {
pub fn rtt_write(s: &str) {
critical_section::with(|cs| {
let mut slot = CHANNEL.borrow_ref_mut(cs);
if let Some(channel) = slot.as_mut() {
channel.write(bytes);
channel.write(s.as_bytes());
};
});
}
pub fn init_rtt_logger() -> RttWriteFn {
pub fn init_rtt_logger() -> &'static RttLogger {
let channels = rtt_init! {
up: {
0: {
@ -31,12 +33,32 @@ pub fn init_rtt_logger() -> RttWriteFn {
critical_section::with(|cs| {
let mut slot = CHANNEL.borrow_ref_mut(cs);
if slot.is_some() {
return rtt_write;
if slot.is_none() {
*slot = Some(channels.up.0);
}
*slot = Some(channels.up.0);
rtt_write
static RTT_LOGGER: RttLogger = RttLogger;
&RTT_LOGGER
})
}
pub struct RttLogger;
impl LogOutput for RttLogger {
fn log(&self, record: &TimestampedRecord) {
let s = record.timestamp.as_secs();
let ms = record.timestamp.as_millis() % 1000;
let level = record.level();
let mut w = &mut Writer;
let _ = writeln!(&mut w, "[{s}.{ms:04}] ({level}) {}", record.args());
}
}
struct Writer;
impl Write for Writer {
fn write_str(&mut self, s: &str) -> Result<(), core::fmt::Error> {
rtt_write(s);
Ok(())
}
}