-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecho.rs
140 lines (112 loc) · 3.07 KB
/
echo.rs
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
//! Implements a trivial serial "console" that echoes text back at the user
#![no_std]
#![no_main]
extern crate panic_halt;
use core::fmt::Write;
use core::str::Utf8Error;
use embedded_hal::serial::Read;
use gd32vf103_pac::Peripherals;
use gd32vf103xx_hal::afio::AfioExt;
use gd32vf103xx_hal::gpio::GpioExt;
use gd32vf103xx_hal::rcu::RcuExt;
use gd32vf103xx_hal::serial::{Config, Serial};
use gd32vf103xx_hal::time::U32Ext;
/// Fixed size of internal `LineReader` buffer
const LINE_BUF_LEN: usize = 512;
/// Errors generated by `LineReader::read_line`
#[derive(Debug)]
enum LineReaderError<R>
{
// Invalid string
Invalid(Utf8Error),
// Too large for internal buffer
Overflow,
// Error receiving bytes
Receive(R),
}
/// Reads full lines from a serial connection
///
/// Similar to (but much simpler than) `std::io::BufReader`
struct LineReader<R>
{
rx: R,
buf: [u8; LINE_BUF_LEN],
}
impl<R> LineReader<R>
{
fn new(rx: R) -> Self
{
Self { rx, buf: [0; LINE_BUF_LEN] }
}
}
impl<R> LineReader<R>
where
R: Read<u8>
{
/// Reads and returns a full line of text from the serial connection
fn read_line<T>(&mut self, tx: &mut T) -> Result<&str, LineReaderError<R::Error>>
where
T: Write
{
let mut index = 0;
loop
{
self.buf[index] = match nb::block!(self.rx.read())
{
Ok(c) => c,
Err(e) => return Err(LineReaderError::Receive(e)),
};
let c = char::from(self.buf[index]);
// Echo characters like a normal console
let _ = write!(tx, "{}", c);
if c == '\r'
{
let _ = write!(tx, "\n");
match core::str::from_utf8(&self.buf[..index + 1])
{
Ok(s) => return Ok(s),
Err(e) => return Err(LineReaderError::Invalid(e)),
}
}
else
{
index += 1;
if index >= LINE_BUF_LEN
{
return Err(LineReaderError::Overflow);
}
}
}
}
}
#[riscv_rt::entry]
fn main() -> !
{
let peripherals = Peripherals::take().unwrap();
let mut rcu = peripherals.RCU.configure()
.freeze();
let mut afio = peripherals.AFIO.constrain(&mut rcu);
let gpioa = peripherals.GPIOA.split(&mut rcu);
let tx = gpioa.pa9;
let rx = gpioa.pa10;
let config = Config::default()
.baudrate(115_200.bps());
let serial = Serial::new(peripherals.USART0, (tx, rx), config, &mut afio, &mut rcu);
let (mut tx, rx) = serial.split();
let _ = write!(tx, "Starting echo console...\r\n");
let mut reader = LineReader::new(rx);
loop {
let _ = write!(tx, "> ");
match reader.read_line(&mut tx)
{
Ok(line) =>
{
let _ = write!(tx, "{}\r\n", line);
},
Err(err) =>
{
let _ = write!(tx, "\r\nERROR: {:?}\r\n", err);
}
}
}
}