//! K409: PL011 echo preserves the bytes after the one command separator. use super::*; use turkos_kernel::serial_console::{ConsoleEvent, SerialConsole}; const RXFE: u32 = 1 << 4; const DATA_ERROR: u32 = 0x0f00; const INPUT_COUNT: usize = 24; fn fail(reason: &str) -> ! { write_str("TURKOS/SERIAL-ECHO-ERROR: "); write_line(reason); park() } fn ticks() -> u64 { let value: u64; unsafe { asm!("mrs {value}, CNTPCT_EL0", value = out(reg) value, options(nomem, nostack, preserves_flags)) }; value } fn prompt(number: u64) { write_str("TURKOS/SERIAL-ECHO-PROMPT: NUMBER=0x"); write_hex(number as usize); write_str("\r\n"); } pub(super) fn smoke_test() -> ! { let base = UART_BASE.load(Ordering::Relaxed); let frequency: u64; unsafe { asm!("mrs {value}, CNTFRQ_EL0", value = out(reg) frequency, options(nomem, nostack, preserves_flags)) }; if frequency == 0 { fail("zero counter frequency"); } let mut console = SerialConsole::echo_only(); let mut suppressed = 0usize; prompt(console.prompts()); write_line("TURKOS/SERIAL-ECHO: READY EXPECTED=0x18"); for index in 0..INPUT_COUNT { let deadline = ticks() .checked_add(frequency.saturating_mul(5)) .unwrap_or(u64::MAX); while unsafe { read_volatile((base + 0x18) as *const u32) } & RXFE != 0 { if ticks() >= deadline { fail("input timeout"); } core::hint::spin_loop(); } let data = unsafe { read_volatile(base as *const u32) }; if data & DATA_ERROR != 0 { fail("PL011 data error"); } let byte = data as u8; let label = match console.feed(byte) { ConsoleEvent::Input => "INPUT", ConsoleEvent::SuppressedLf => { suppressed += 1; "SUPPRESS" } ConsoleEvent::Echo { prompt: number, bytes, len, } => { write_str("TURKOS/SERIAL-ECHO-OUTPUT: NUMBER=0x"); write_hex(console.echos() as usize); write_str(" LEN=0x"); write_hex(len); write_str(" DATA="); for value in &bytes[..len] { write_byte(*value); } write_str("\r\n"); prompt(number); "ECHO" } ConsoleEvent::Help { prompt: number } => { write_str("TURKOS/SERIAL-ECHO-HELP: NUMBER=0x"); write_hex(console.helps() as usize); write_str(" COMMANDS="); write_str(console.commands()); write_str("\r\n"); prompt(number); "HELP" } ConsoleEvent::Blank { .. } => fail("unexpected blank command"), ConsoleEvent::List { .. } => fail("unexpected ls command"), ConsoleEvent::Cat { .. } => fail("unexpected cat command"), ConsoleEvent::Run { .. } => fail("unexpected run command"), ConsoleEvent::Wait { .. } => fail("unexpected wait command"), ConsoleEvent::InvalidUtf8 { .. } => { fail("unexpected UTF-8 error in historical fixture") } ConsoleEvent::MalformedArguments { .. } => { fail("unexpected argument error in historical fixture") } ConsoleEvent::Cancelled { .. } => fail("unexpected Ctrl-C in historical fixture"), ConsoleEvent::Unhandled(_, _) => fail("unhandled command"), ConsoleEvent::TooLong => fail("overlong command"), }; write_str("TURKOS/SERIAL-ECHO-BYTE: INDEX=0x"); write_hex(index); write_str(" VALUE=0x"); write_hex(byte as usize); write_str(" EVENT="); write_str(label); write_str(" PROMPTS=0x"); write_hex(console.prompts() as usize); write_str(" ECHOS=0x"); write_hex(console.echos() as usize); write_str(" HELPS=0x"); write_hex(console.helps() as usize); write_str(" SUPPRESSED=0x"); write_hex(suppressed); write_str("\r\n"); } if console.prompts() != 4 || console.echos() != 2 || console.helps() != 1 || console.blanks() != 0 || suppressed != 1 { fail("final echo accounting"); } write_line( "TURKOS/SERIAL-ECHO: PASS PROMPTS=0x4 ECHOS=0x2 HELPS=0x1 SUPPRESSED=0x1 BYTES=0x18", ); park() }