1use crate::ringbuf::{Consumer, Producer, RingBuffer};
2use embedded_hal_nb as e_nb;
3use embedded_io as e_io;
4
5mod uart_it;
6mod uart_poll;
7
8pub use core::convert::Infallible;
9pub use uart_it::*;
10pub use uart_poll::*;
11
12pub struct Tx<U> {
21 uart: [U; 2],
22}
23
24impl<U: UartPeriph> Tx<U> {
25 pub(crate) fn new(uart: [U; 2]) -> Self {
26 Self { uart }
27 }
28
29 pub fn into_poll(self, retry_times: u32, flush_retry_times: u32) -> UartPollTx<U> {
34 let [uart, _] = self.uart;
35 UartPollTx::<U>::new(uart, retry_times, flush_retry_times)
36 }
37
38 pub fn into_interrupt(
39 self,
40 buf_size: usize,
41 transmit_retry_times: u32,
42 flush_retry_times: u32,
43 ) -> (UartInterruptTx<U>, UartInterruptTxHandler<U>) {
44 let (w, r) = RingBuffer::<u8>::new(buf_size);
45 let [u1, u2] = self.uart;
46 (
47 UartInterruptTx::new(u1, w, transmit_retry_times, flush_retry_times),
48 UartInterruptTxHandler::new(u2, r),
49 )
50 }
51
52 }
66
67pub struct Rx<U: UartPeriph> {
71 uart: [U; 2],
72}
73
74impl<U: UartPeriph> Rx<U> {
75 pub(crate) fn new(uart: [U; 2]) -> Self {
76 Self { uart }
77 }
78
79 pub fn into_poll(self, retry_times: u32, continue_retry_times: u32) -> UartPollRx<U> {
80 let [uart, _] = self.uart;
81 UartPollRx::<U>::new(uart, retry_times, continue_retry_times)
82 }
83
84 pub fn into_interrupt(
85 self,
86 buf_size: usize,
87 retry_times: u32,
88 ) -> (UartInterruptRx<U>, UartInterruptRxHandler<U>) {
89 let (w, r) = RingBuffer::<u8>::new(buf_size);
90 let [u1, u2] = self.uart;
91 (
92 UartInterruptRx::new(u1, r, retry_times),
93 UartInterruptRxHandler::new(u2, w),
94 )
95 }
96
97 }
104
105pub struct UartIdleInterrupt<U: UartPeriph> {
109 uart: U,
110}
111
112impl<U: UartPeriph> UartIdleInterrupt<U> {
113 pub(crate) fn new(uart: U) -> Self {
114 Self { uart }
115 }
116
117 #[inline]
118 pub fn is_interrupted(&mut self) -> bool {
119 self.uart.is_interrupted(UartEvent::Idle)
120 }
121
122 #[inline]
123 pub fn listen(&mut self) {
124 self.uart.set_interrupt(UartEvent::Idle, true);
125 }
126
127 #[inline]
128 pub fn unlisten(&mut self) {
129 self.uart.set_interrupt(UartEvent::Idle, false);
130 }
131}
132
133pub trait UartPeriph {
136 fn set_dma_tx(&mut self, enable: bool);
137 fn set_dma_rx(&mut self, enable: bool);
138
139 fn get_tx_data_reg_addr(&self) -> u32;
140 fn write(&mut self, word: u16) -> nb::Result<(), Error>;
141 fn is_tx_empty(&self) -> bool;
142 fn is_tx_complete(&self) -> bool;
143
144 fn get_rx_data_reg_addr(&self) -> u32;
145 fn read(&mut self) -> nb::Result<u16, Error>;
146 fn is_rx_not_empty(&self) -> bool;
147
148 fn set_interrupt(&mut self, event: UartEvent, enable: bool);
149 fn is_interrupt_enable(&mut self, event: UartEvent) -> bool;
150 fn is_interrupted(&mut self, event: UartEvent) -> bool;
151
152 fn clear_err_flag(&self);
153}
154
155#[derive(Clone, Copy, Debug, PartialEq, Eq)]
156pub enum UartEvent {
157 TxEmpty,
159 RxNotEmpty,
161 Idle,
163}
164
165#[derive(Debug)]
167#[non_exhaustive]
168pub enum Error {
169 Overrun,
171 FrameFormat,
174 Parity,
176 Noise,
178 Busy,
180 Other,
182}
183
184impl embedded_io::Error for Error {
185 #[inline]
186 fn kind(&self) -> e_io::ErrorKind {
187 match self {
188 Error::Overrun => e_io::ErrorKind::InvalidData,
189 Error::FrameFormat => e_io::ErrorKind::InvalidData,
190 Error::Parity => e_io::ErrorKind::InvalidData,
191 Error::Noise => e_io::ErrorKind::InvalidData,
192 Error::Busy => e_io::ErrorKind::Interrupted,
193 Error::Other => e_io::ErrorKind::Other,
194 }
195 }
196}
197
198impl e_nb::serial::Error for Error {
199 #[inline]
200 fn kind(&self) -> e_nb::serial::ErrorKind {
201 match self {
202 Error::Overrun => e_nb::serial::ErrorKind::Overrun,
203 Error::FrameFormat => e_nb::serial::ErrorKind::FrameFormat,
204 Error::Parity => e_nb::serial::ErrorKind::Parity,
205 Error::Noise => e_nb::serial::ErrorKind::Noise,
206 Error::Busy => e_nb::serial::ErrorKind::Other,
207 Error::Other => e_nb::serial::ErrorKind::Other,
208 }
209 }
210}
211
212pub enum WordLength {
213 Bits8,
216 Bits9,
219}
220
221pub enum Parity {
222 ParityNone,
223 ParityEven,
224 ParityOdd,
225}
226
227pub enum StopBits {
228 STOP1,
230 STOP0P5,
232 STOP2,
234 STOP1P5,
236}
237
238pub struct Config {
239 pub baudrate: u32,
240 pub word_length: WordLength,
241 pub parity: Parity,
242 pub stop_bits: StopBits,
243}
244
245impl Default for Config {
246 fn default() -> Config {
247 Config {
248 baudrate: 115_200,
249 word_length: WordLength::Bits8,
250 parity: Parity::ParityNone,
251 stop_bits: StopBits::STOP1,
252 }
253 }
254}
255
256impl Config {
257 pub fn baudrate(mut self, baudrate: u32) -> Self {
258 self.baudrate = baudrate;
259 self
260 }
261
262 pub fn word_length(mut self, wordlength: WordLength) -> Self {
263 self.word_length = wordlength;
264 self
265 }
266
267 pub fn word_length_8bits(mut self) -> Self {
268 self.word_length = WordLength::Bits8;
269 self
270 }
271
272 pub fn word_length_9bits(mut self) -> Self {
273 self.word_length = WordLength::Bits9;
274 self
275 }
276
277 pub fn parity(mut self, parity: Parity) -> Self {
278 self.parity = parity;
279 self
280 }
281
282 pub fn parity_none(mut self) -> Self {
283 self.parity = Parity::ParityNone;
284 self
285 }
286
287 pub fn parity_even(mut self) -> Self {
288 self.parity = Parity::ParityEven;
289 self
290 }
291
292 pub fn parity_odd(mut self) -> Self {
293 self.parity = Parity::ParityOdd;
294 self
295 }
296
297 pub fn stop_bits(mut self, stop_bits: StopBits) -> Self {
298 self.stop_bits = stop_bits;
299 self
300 }
301}