Skip to content

Commit

Permalink
Make Uart::write slightly more conventional
Browse files Browse the repository at this point in the history
  • Loading branch information
hegza committed Aug 20, 2024
1 parent 68c1ba7 commit b590b5d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
2 changes: 1 addition & 1 deletion examples/headsail-bsp/examples/uart0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ use headsail_bsp::{apb_uart::ApbUart0, rt::entry};
fn main() -> ! {
let (soc_freq, baud) = (30_000_000, 115_200);
let mut uart = ApbUart0::init(soc_freq, baud);
uart.write("Hello world!");
uart.write_str("Hello world!");
loop {}
}
18 changes: 16 additions & 2 deletions examples/headsail-bsp/src/apb_uart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,26 @@ impl<const BASE_ADDR: usize> ApbUart<BASE_ADDR> {
}

#[inline]
pub fn write(&mut self, s: &str) {
for b in s.as_bytes() {
pub fn write(&mut self, buf: &[u8]) {
for b in buf {
self.putc(*b);
}
}

#[inline]
pub fn write_str(&mut self, s: &str) {
self.write(s.as_bytes());
}

/// Flush this output stream, blocking until all intermediately buffered contents reach their
/// destination.
#[inline]
pub fn flush(&mut self) {
// Wait for hardware to report completion
#[cfg(feature = "asic")]
while !self.is_transmit_empty() {}
}

#[inline]
pub fn putc(&mut self, c: u8) {
// Wait for hardware to report completion
Expand Down
2 changes: 1 addition & 1 deletion examples/headsail-bsp/src/sprintln.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl ufmt::uWrite for ApbUart0 {
type Error = core::convert::Infallible;

fn write_str(&mut self, s: &str) -> Result<(), Self::Error> {
self.write(s);
self.write_str(s);
Ok(())
}
}
6 changes: 3 additions & 3 deletions examples/headsail-bsp/src/tb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ pub const TAG_OK: &str = "[OK]";

pub fn report_pass() {
let mut uart = unsafe { ApbUart0::instance() };
uart.write(TAG_PASS);
uart.write_str(TAG_PASS);
}

pub fn report_fail() {
let mut uart = unsafe { ApbUart0::instance() };
uart.write(TAG_FAIL);
uart.write_str(TAG_FAIL);
}

pub fn report_ok() {
let mut uart = unsafe { ApbUart0::instance() };
uart.write(TAG_OK);
uart.write_str(TAG_OK);
}

0 comments on commit b590b5d

Please sign in to comment.