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
use core::convert::Infallible;
use core::ops::Deref;
use embedded_hal::serial;
use nb;
use crate::clock::Clocks;
use crate::gpio::{gpio0, IOF0};
use crate::time::Bps;
use core::mem;
#[allow(unused_imports)]
use e310x::{uart0, UART0, UART1};
pub unsafe trait TxPin<UART> {}
pub unsafe trait RxPin<UART> {}
unsafe impl<T> TxPin<UART0> for gpio0::Pin17<IOF0<T>> {}
unsafe impl<T> RxPin<UART0> for gpio0::Pin16<IOF0<T>> {}
#[cfg(feature = "g002")]
mod g002_ims {
use super::{gpio0, RxPin, TxPin, IOF0, UART1};
unsafe impl<T> TxPin<UART1> for gpio0::Pin18<IOF0<T>> {}
unsafe impl<T> RxPin<UART1> for gpio0::Pin23<IOF0<T>> {}
}
#[doc(hidden)]
pub trait UartX: Deref<Target = uart0::RegisterBlock> {}
impl UartX for UART0 {}
impl UartX for UART1 {}
pub struct Serial<UART, PINS> {
uart: UART,
pins: PINS,
}
pub struct Rx<UART> {
uart: UART,
}
pub struct Tx<UART> {
uart: UART,
}
impl<UART: UartX, TX, RX> Serial<UART, (TX, RX)> {
pub fn new(uart: UART, pins: (TX, RX), baud_rate: Bps, clocks: Clocks) -> Self
where
TX: TxPin<UART>,
RX: RxPin<UART>,
{
let div = clocks.tlclk().0 / baud_rate.0 - 1;
unsafe {
uart.ie.write(|w| w.txwm().bit(false).rxwm().bit(false));
uart.div.write(|w| w.bits(div));
uart.txctrl
.write(|w| w.counter().bits(1).enable().bit(true));
uart.rxctrl.write(|w| w.enable().bit(true));
}
Serial { uart, 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
}
pub fn split(self) -> (Tx<UART>, Rx<UART>) {
(
Tx {
uart: unsafe { mem::zeroed() },
},
Rx { uart: self.uart },
)
}
pub fn free(self) -> (UART, (TX, RX)) {
(self.uart, self.pins)
}
}
impl<UART: UartX> serial::Read<u8> for Rx<UART> {
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<UART: UartX> serial::Write<u8> for Tx<UART> {
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 {
self.uart.txdata.write(|w| w.data().bits(byte));
}
Ok(())
}
}
fn flush(&mut self) -> nb::Result<(), Infallible> {
if self.uart.ip.read().txwm().bit_is_set() {
Ok(())
} else {
Err(nb::Error::WouldBlock)
}
}
}
impl<TX, RX> Serial<UART0, (TX, RX)> {
#[deprecated(note = "Please use Serial::new function instead")]
pub fn uart0(uart: UART0, pins: (TX, RX), baud_rate: Bps, clocks: Clocks) -> Self
where
TX: TxPin<UART0>,
RX: RxPin<UART0>,
{
Self::new(uart, pins, baud_rate, clocks)
}
}
#[cfg(feature = "g002")]
impl<TX, RX> Serial<UART1, (TX, RX)> {
#[deprecated(note = "Please use Serial::new function instead")]
pub fn uart1(uart: UART1, pins: (TX, RX), baud_rate: Bps, clocks: Clocks) -> Self
where
TX: TxPin<UART1>,
RX: RxPin<UART1>,
{
Self::new(uart, pins, baud_rate, clocks)
}
}