Add RTT console

This commit is contained in:
Berkus Decker 2019-03-03 22:08:39 +02:00
parent 3923e7c838
commit 39ae164aec
3 changed files with 32 additions and 3 deletions

View File

@ -22,8 +22,9 @@
* SOFTWARE. * SOFTWARE.
*/ */
use crate::jlink_rtt::Output as JLinkOutput;
use crate::platform; use crate::platform;
use core::fmt; use core::fmt::{self, Write};
/// A trait that must be implemented by devices that are candidates for the /// A trait that must be implemented by devices that are candidates for the
/// global console. /// global console.
@ -49,6 +50,7 @@ pub enum Output {
None(NullConsole), None(NullConsole),
MiniUart(platform::MiniUart), MiniUart(platform::MiniUart),
PL011Uart(platform::PL011Uart), PL011Uart(platform::PL011Uart),
RTT(JLinkOutput),
} }
impl From<platform::MiniUart> for Output { impl From<platform::MiniUart> for Output {
@ -63,6 +65,12 @@ impl From<platform::PL011Uart> for Output {
} }
} }
impl From<JLinkOutput> for Output {
fn from(instance: JLinkOutput) -> Self {
Output::RTT(instance)
}
}
pub struct Console { pub struct Console {
output: Output, output: Output,
} }
@ -80,6 +88,7 @@ impl Console {
Output::None(i) => i, Output::None(i) => i,
Output::MiniUart(i) => i, Output::MiniUart(i) => i,
Output::PL011Uart(i) => i, Output::PL011Uart(i) => i,
Output::RTT(i) => i,
} }
} }

View File

@ -216,11 +216,28 @@ impl Output {
} }
} }
impl fmt::Write for Output { impl Drop for Output {
fn write_str(&mut self, s: &str) -> fmt::Result { fn drop(&mut self) {}
}
impl crate::devices::ConsoleOps for Output {
fn puts(&self, s: &str) {
unsafe { unsafe {
_SEGGER_RTT.up.write(s.as_bytes(), true); _SEGGER_RTT.up.write(s.as_bytes(), true);
} }
}
fn putc(&self, c: char) {
let mut buf = [0u8; 4];
let s = c.encode_utf8(&mut buf);
self.puts(s);
}
}
impl fmt::Write for Output {
fn write_str(&mut self, s: &str) -> fmt::Result {
use crate::devices::console::ConsoleOps;
self.puts(s);
Ok(()) Ok(())
} }
} }

View File

@ -83,6 +83,9 @@ fn kmain() -> ! {
// } // }
// jtag_dbg_wait(); // jtag_dbg_wait();
CONSOLE.lock(|c| {
c.replace_with(Output::new().into());
});
let mut out = Output::new(); let mut out = Output::new();
writeln!(out, "JLink RTT is working!"); // @todo RttConsole writeln!(out, "JLink RTT is working!"); // @todo RttConsole