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

Clean up serial_jtag and increase timeout #55

Merged
merged 5 commits into from
Oct 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 41 additions & 28 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,15 @@ mod rtt_printer {
}
}

#[cfg(feature = "jtag_serial")]
#[cfg(all(
feature = "jtag_serial",
any(
feature = "esp32c3",
feature = "esp32c6",
feature = "esp32h2",
feature = "esp32s3"
)
))]
mod serial_jtag_printer {
#[cfg(feature = "esp32c3")]
const SERIAL_JTAG_FIFO_REG: usize = 0x6004_3000;
Expand All @@ -107,41 +115,46 @@ mod serial_jtag_printer {
#[cfg(feature = "esp32s3")]
const SERIAL_JTAG_CONF_REG: usize = 0x6003_8004;

#[cfg(any(
feature = "esp32c3",
feature = "esp32c6",
feature = "esp32h2",
feature = "esp32s3"
))]
fn fifo_flush() {
let conf = SERIAL_JTAG_CONF_REG as *mut u32;
unsafe { conf.write_volatile(0b001) };
}

fn fifo_clear() -> bool {
let conf = SERIAL_JTAG_CONF_REG as *mut u32;
unsafe { conf.read_volatile() & 0b010 != 0b000 }
}

fn fifo_write(byte: u8) {
let fifo = SERIAL_JTAG_FIFO_REG as *mut u32;
unsafe { fifo.write_volatile(byte as u32) }
}

impl super::Printer {
pub fn write_bytes(&mut self, bytes: &[u8]) {
super::with(|| {
const TIMEOUT_ITERATIONS: usize = 5_000;
const TIMEOUT_ITERATIONS: usize = 50_000;

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
if !fifo_clear() {
// Still wasn't able to drain the FIFO - early return
// This is important so we don't block forever if there is no host attached.
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;
}
for chunk in bytes.chunks(64) {
for &b in chunk {
fifo_write(b);
}

fifo_flush();

// wait for fifo to clear
let mut timeout = TIMEOUT_ITERATIONS;
while !fifo_clear() {
if timeout == 0 {
return;
}
timeout -= 1;
}
}
})
Expand Down