Skip to content
This repository has been archived by the owner on May 23, 2024. It is now read-only.

Commit

Permalink
Don't double-lock in defmt
Browse files Browse the repository at this point in the history
  • Loading branch information
bugadani committed Oct 23, 2023
1 parent bc3f8a2 commit 45b7ae9
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 60 deletions.
2 changes: 1 addition & 1 deletion src/defmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,5 @@ unsafe impl defmt::Logger for Logger {
}

fn do_write(bytes: &[u8]) {
Printer.write_bytes(bytes)
Printer.write_bytes_assume_cs(bytes)
}
116 changes: 57 additions & 59 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,20 @@ impl core::fmt::Write for Printer {
}
}

impl Printer {
pub fn write_bytes(&mut self, bytes: &[u8]) {
with(|| self.write_bytes_assume_cs(bytes))
}
}

#[cfg(feature = "rtt")]
mod rtt_printer {
impl super::Printer {
pub fn write_bytes(&mut self, bytes: &[u8]) {
super::with(|| {
let count = crate::rtt::write_bytes_internal(bytes);
if count < bytes.len() {
crate::rtt::write_bytes_internal(&bytes[count..]);
}
})
pub(crate) fn write_bytes_assume_cs(&mut self, bytes: &[u8]) {
let count = crate::rtt::write_bytes_internal(bytes);
if count < bytes.len() {
crate::rtt::write_bytes_internal(&bytes[count..]);
}
}
}
}
Expand Down Expand Up @@ -114,37 +118,35 @@ mod serial_jtag_printer {
feature = "esp32s3"
))]
impl super::Printer {
pub fn write_bytes(&mut self, bytes: &[u8]) {
super::with(|| {
const TIMEOUT_ITERATIONS: usize = 5_000;
pub(crate) fn write_bytes_assume_cs(&mut self, bytes: &[u8]) {
const TIMEOUT_ITERATIONS: usize = 5_000;

let fifo = SERIAL_JTAG_FIFO_REG as *mut u32;
let conf = SERIAL_JTAG_CONF_REG as *mut u32;
let fifo = SERIAL_JTAG_FIFO_REG as *mut u32;
let conf = SERIAL_JTAG_CONF_REG as *mut u32;

if unsafe { conf.read_volatile() } & 0b011 == 0b000 {
// still wasn't able to drain the FIFO - early return
return;
}
if unsafe { conf.read_volatile() } & 0b011 == 0b000 {
// still wasn't able to drain the FIFO - early return
return;
}

// todo 64 byte chunks max
for chunk in bytes.chunks(32) {
unsafe {
for &b in chunk {
fifo.write_volatile(b as u32);
}
conf.write_volatile(0b001);

let mut timeout = TIMEOUT_ITERATIONS;
while conf.read_volatile() & 0b011 == 0b000 {
// wait
timeout -= 1;
if timeout == 0 {
return;
}
// todo 64 byte chunks max
for chunk in bytes.chunks(32) {
unsafe {
for &b in chunk {
fifo.write_volatile(b as u32);
}
conf.write_volatile(0b001);

let mut timeout = TIMEOUT_ITERATIONS;
while conf.read_volatile() & 0b011 == 0b000 {
// wait
timeout -= 1;
if timeout == 0 {
return;
}
}
}
})
}
}
}
}
Expand All @@ -164,38 +166,34 @@ mod uart_printer {

impl super::Printer {
#[cfg(not(feature = "esp32s2"))]
pub fn write_bytes(&mut self, bytes: &[u8]) {
super::with(|| {
for &b in bytes {
unsafe {
let uart_tx_one_char: unsafe extern "C" fn(u8) -> i32 =
core::mem::transmute(UART_TX_ONE_CHAR);
uart_tx_one_char(b)
};
}
})
pub(crate) fn write_bytes_assume_cs(&mut self, bytes: &[u8]) {
for &b in bytes {
unsafe {
let uart_tx_one_char: unsafe extern "C" fn(u8) -> i32 =
core::mem::transmute(UART_TX_ONE_CHAR);
uart_tx_one_char(b)
};
}
}

#[cfg(feature = "esp32s2")]
pub fn write_bytes(&mut self, bytes: &[u8]) {
super::with(|| {
// On ESP32-S2 the UART_TX_ONE_CHAR ROM-function seems to have some issues.
for chunk in bytes.chunks(64) {
for &b in chunk {
unsafe {
// write FIFO
(0x3f400000 as *mut u32).write_volatile(b as u32);
};
}

// wait for TX_DONE
while unsafe { (0x3f400004 as *const u32).read_volatile() } & (1 << 14) == 0 {}
pub(crate) fn write_bytes_assume_cs(&mut self, bytes: &[u8]) {
// On ESP32-S2 the UART_TX_ONE_CHAR ROM-function seems to have some issues.
for chunk in bytes.chunks(64) {
for &b in chunk {
unsafe {
// reset TX_DONE
(0x3f400010 as *mut u32).write_volatile(1 << 14);
}
// write FIFO
(0x3f400000 as *mut u32).write_volatile(b as u32);
};
}
})

// wait for TX_DONE
while unsafe { (0x3f400004 as *const u32).read_volatile() } & (1 << 14) == 0 {}
unsafe {
// reset TX_DONE
(0x3f400010 as *mut u32).write_volatile(1 << 14);
}
}
}
}
}
Expand Down

0 comments on commit 45b7ae9

Please sign in to comment.