#[cfg(feature = "critical-section")]
use esp_sync::RestoreState;
use super::{LockToken, PrinterImpl};
#[cfg(feature = "critical-section")]
use crate::LOCK;
#[cfg(feature = "critical-section")]
static mut TAKEN: bool = false;
#[cfg(feature = "critical-section")]
static mut CS_RESTORE: RestoreState = RestoreState::invalid();
static mut ENCODER: defmt::Encoder = defmt::Encoder::new();
#[defmt::global_logger]
pub struct Logger;
unsafe impl defmt::Logger for Logger {
fn acquire() {
#[cfg(feature = "critical-section")]
unsafe {
let restore = LOCK.acquire();
if TAKEN {
panic!("defmt logger taken reentrantly")
}
TAKEN = true;
CS_RESTORE = restore;
}
do_write(&[0xFF, 0x00]);
unsafe { ENCODER.start_frame(do_write) }
}
unsafe fn release() {
unsafe {
ENCODER.end_frame(do_write);
Self::flush();
#[cfg(feature = "critical-section")]
{
TAKEN = false;
let restore = CS_RESTORE;
LOCK.release(restore);
}
}
}
unsafe fn flush() {
let token = unsafe {
LockToken::conjure()
};
PrinterImpl::flush(token);
}
unsafe fn write(bytes: &[u8]) {
unsafe {
ENCODER.write(bytes, do_write);
}
}
}
fn do_write(bytes: &[u8]) {
let token = unsafe {
LockToken::conjure()
};
PrinterImpl::write_bytes_in_cs(bytes, token)
}
#[cfg(feature = "timestamp")]
defmt::timestamp!("{=u64:ms}", {
unsafe extern "Rust" {
fn _esp_println_timestamp() -> u64;
}
unsafe { _esp_println_timestamp() }
});