1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
use embedded_hal::serial::{Read, Write};
use nb::block;
pub trait LegacyStdio: Send {
fn getchar(&mut self) -> u8;
fn putchar(&mut self, ch: u8);
}
struct EmbeddedHalSerial<T> {
inner: T,
}
impl<T> EmbeddedHalSerial<T> {
fn new(inner: T) -> Self {
Self { inner }
}
}
impl<T: Send> LegacyStdio for EmbeddedHalSerial<T>
where
T: Read<u8> + Write<u8>,
{
fn getchar(&mut self) -> u8 {
block!(self.inner.try_read()).ok().unwrap()
}
fn putchar(&mut self, ch: u8) {
block!(self.inner.try_write(ch)).ok();
block!(self.inner.try_flush()).ok();
}
}
struct Fused<T, R>(T, R);
impl<T, R> LegacyStdio for Fused<T, R>
where
T: Write<u8> + Send + 'static,
R: Read<u8> + Send + 'static,
{
fn getchar(&mut self) -> u8 {
block!(self.1.try_read()).ok().unwrap()
}
fn putchar(&mut self, ch: u8) {
block!(self.0.try_write(ch)).ok();
block!(self.0.try_flush()).ok();
}
}
use alloc::boxed::Box;
use spin::Mutex;
lazy_static::lazy_static! {
static ref LEGACY_STDIO: Mutex<Option<Box<dyn LegacyStdio>>> =
Mutex::new(None);
}
#[doc(hidden)]
pub fn init_legacy_stdio_embedded_hal<T: Read<u8> + Write<u8> + Send + 'static>(serial: T) {
let serial = EmbeddedHalSerial::new(serial);
*LEGACY_STDIO.lock() = Some(Box::new(serial));
}
#[doc(hidden)]
pub fn init_legacy_stdio_embedded_hal_fuse<T, R>(tx: T, rx: R)
where
T: Write<u8> + Send + 'static,
R: Read<u8> + Send + 'static,
{
let serial = Fused(tx, rx);
*LEGACY_STDIO.lock() = Some(Box::new(serial));
}
pub fn legacy_stdio_putchar(ch: u8) {
if let Some(stdio) = LEGACY_STDIO.lock().as_mut() {
stdio.putchar(ch)
}
}
pub fn legacy_stdio_getchar() -> u8 {
if let Some(stdio) = LEGACY_STDIO.lock().as_mut() {
stdio.getchar()
} else {
0
}
}
use core::fmt;
struct Stdout;
impl fmt::Write for Stdout {
fn write_str(&mut self, s: &str) -> fmt::Result {
if let Some(stdio) = LEGACY_STDIO.lock().as_mut() {
for byte in s.as_bytes() {
stdio.putchar(*byte)
}
}
Ok(())
}
}
#[doc(hidden)]
pub fn _print(args: fmt::Arguments) {
use fmt::Write;
Stdout.write_fmt(args).unwrap();
}
#[macro_export(local_inner_macros)]
macro_rules! print {
($($arg:tt)*) => ({
$crate::legacy_stdio::_print(core::format_args!($($arg)*));
});
}
#[macro_export(local_inner_macros)]
macro_rules! println {
($fmt: literal $(, $($arg: tt)+)?) => {
$crate::legacy_stdio::_print(core::format_args!(core::concat!($fmt, "\n") $(, $($arg)+)?));
}
}