diff --git a/src/defmt.rs b/src/defmt.rs index b65230f..6baeb07 100644 --- a/src/defmt.rs +++ b/src/defmt.rs @@ -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) } diff --git a/src/lib.rs b/src/lib.rs index a737c42..addb99e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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..]); + } } } } @@ -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; } } } - }) + } } } } @@ -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); + } + } } } }