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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
use core::mem;
use core::convert::Infallible;
use embedded_hal::serial;
use crate::pac::{UARTHS,uart1,UART1,UART2,UART3};
use crate::clock::Clocks;
use crate::time::Bps;
use core::marker::PhantomData;
use crate::external_pins::ExternalPin;
use crate::fpioa;
pub trait SerialExt<PINS>: Sized {
fn configure(self, pins: PINS, baud_rate: Bps, clocks: &Clocks) -> Serial<Self, PINS>;
}
pub struct Serial<UART, PINS> {
uart: UART,
pins: PINS,
}
impl<UART, PINS> Serial<UART, PINS> {
pub fn split(self) -> (Tx<UART, PINS>, Rx<UART, PINS>) {
(
Tx {
uart: self.uart,
pins: self.pins
},
Rx {
uart: unsafe { mem::zeroed() },
_pins: PhantomData,
}
)
}
pub fn join(tx: Tx<UART, PINS>, _rx: Rx<UART, PINS>) -> Self {
Serial { uart: tx.uart, pins: tx.pins }
}
pub fn free(self) -> (UART, PINS) {
(self.uart, self.pins)
}
}
pub struct Tx<UART, PINS> {
uart: UART,
pins: PINS,
}
pub struct Rx<UART, PINS> {
uart: UART,
_pins: PhantomData<PINS>,
}
impl<TX: ExternalPin, RX: ExternalPin> SerialExt<(TX, RX)> for UARTHS {
fn configure(self, pins: (TX, RX), baud_rate: Bps, clocks: &Clocks) -> Serial<UARTHS, (TX, RX)>
{
let uart = self;
fpioa::set_function(TX::INDEX, fpioa::Function::UARTHS_TX);
fpioa::set_function(RX::INDEX, fpioa::Function::UARTHS_RX);
let div = clocks.cpu().0 / baud_rate.0 - 1;
unsafe {
uart.div.write(|w| w.bits(div));
}
uart.txctrl.write(|w| w.txen().bit(true));
uart.rxctrl.write(|w| w.rxen().bit(true));
Serial { uart, pins }
}
}
impl<PINS> Serial<UARTHS, PINS> {
pub fn listen(self) -> Self {
self.uart.ie.write(|w| w.txwm().bit(false).rxwm().bit(true));
self
}
pub fn unlisten(self) -> Self {
self.uart
.ie
.write(|w| w.txwm().bit(false).rxwm().bit(false));
self
}
}
impl<PINS> serial::Read<u8> for Rx<UARTHS, PINS> {
type Error = Infallible;
fn read(&mut self) -> nb::Result<u8, Infallible> {
let rxdata = self.uart.rxdata.read();
if rxdata.empty().bit_is_set() {
Err(nb::Error::WouldBlock)
} else {
Ok(rxdata.data().bits() as u8)
}
}
}
impl<PINS> serial::Write<u8> for Tx<UARTHS, PINS> {
type Error = Infallible;
fn write(&mut self, byte: u8) -> nb::Result<(), Infallible> {
let txdata = self.uart.txdata.read();
if txdata.full().bit_is_set() {
Err(nb::Error::WouldBlock)
} else {
unsafe {
(*UARTHS::ptr()).txdata.write(|w| w.data().bits(byte));
}
Ok(())
}
}
fn flush(&mut self) -> nb::Result<(), Infallible> {
let txdata = self.uart.txdata.read();
if txdata.full().bit_is_set() {
Err(nb::Error::WouldBlock)
} else {
Ok(())
}
}
}
mod closed_trait {
use core::ops::Deref;
pub trait UartX: Deref<Target = super::uart1::RegisterBlock> {
const INDEX: u8;
}
}
use closed_trait::UartX;
impl UartX for UART1 { const INDEX: u8 = 1; }
impl UartX for UART2 { const INDEX: u8 = 2; }
impl UartX for UART3 { const INDEX: u8 = 3; }
const UART_RECEIVE_FIFO_1: u32 = 0;
const UART_SEND_FIFO_8: u32 = 3;
impl<UART: UartX, TX: ExternalPin, RX: ExternalPin> SerialExt<(TX, RX)> for UART {
fn configure(self, pins: (TX, RX), baud_rate: Bps, clocks: &Clocks) -> Serial<UART, (TX, RX)> {
let uart = self;
fpioa::set_function(TX::INDEX, fpioa::Function::uart(UART::INDEX, fpioa::UartFunction::TX));
fpioa::set_function(RX::INDEX, fpioa::Function::uart(UART::INDEX, fpioa::UartFunction::RX));
let data_width = 8;
let stopbit_val = 0;
let parity_val = 0;
let divisor = clocks.apb0().0 / baud_rate.0;
let dlh = ((divisor >> 12) & 0xff) as u8;
let dll = ((divisor >> 4) & 0xff) as u8;
let dlf = (divisor & 0xf) as u8;
unsafe {
uart.lcr.write(|w| w.bits(1 << 7));
uart.dlh_ier.write(|w| w.bits(dlh.into()));
uart.rbr_dll_thr.write(|w| w.bits(dll.into()));
uart.dlf.write(|w| w.bits(dlf.into()));
uart.lcr.write(|w| w.bits((data_width - 5) | (stopbit_val << 2) | (parity_val << 3)));
uart.dlh_ier.write(|w| w.bits(0x80));
uart.fcr_iir.write(|w| w.bits(UART_RECEIVE_FIFO_1 << 6 | UART_SEND_FIFO_8 << 4 | 0x1 << 3 | 0x1));
}
Serial { uart, pins }
}
}
impl<UART: UartX, PINS> Serial<UART, PINS> {
pub fn listen(self) -> Self {
self
}
pub fn unlisten(self) -> Self {
self
}
}
impl<UART: UartX, PINS> serial::Read<u8> for Rx<UART, PINS> {
type Error = Infallible;
fn read(&mut self) -> nb::Result<u8, Infallible> {
let lsr = self.uart.lsr.read();
if (lsr.bits() & (1<<0)) == 0 {
Err(nb::Error::WouldBlock)
} else {
let rbr = self.uart.rbr_dll_thr.read();
Ok((rbr.bits() & 0xff) as u8)
}
}
}
impl<UART: UartX, PINS> serial::Write<u8> for Tx<UART, PINS> {
type Error = Infallible;
fn write(&mut self, byte: u8) -> nb::Result<(), Infallible> {
let lsr = self.uart.lsr.read();
if (lsr.bits() & (1<<5)) != 0 {
Err(nb::Error::WouldBlock)
} else {
unsafe {
self.uart.rbr_dll_thr.write(|w| w.bits(byte.into()));
}
Ok(())
}
}
fn flush(&mut self) -> nb::Result<(), Infallible> {
Ok(())
}
}