stm32f1_hal/common/uart/
mod.rs1mod uart_dma;
2pub use uart_dma::*;
3mod uart_it;
4pub use uart_it::*;
5mod uart_poll;
6pub use uart_poll::*;
7
8use embedded_hal_nb as e_nb;
9use embedded_io as e_io;
10
11pub use core::convert::Infallible;
12
13pub struct UartIdleInterrupt<U: UartPeriph> {
22 uart: U,
23}
24
25impl<U: UartPeriph> UartIdleInterrupt<U> {
26 pub fn new(uart: U) -> Self {
27 Self { uart }
28 }
29
30 #[inline]
31 pub fn is_interrupted(&mut self) -> bool {
32 self.uart.is_interrupted(UartEvent::Idle)
33 }
34
35 #[inline]
36 pub fn listen(&mut self) {
37 self.uart.set_interrupt(UartEvent::Idle, true);
38 }
39
40 #[inline]
41 pub fn unlisten(&mut self) {
42 self.uart.set_interrupt(UartEvent::Idle, false);
43 }
44}
45
46pub trait UartPeriph {
49 fn write(&mut self, word: u16) -> nb::Result<(), Error>;
50 fn is_tx_empty(&self) -> bool;
51 fn is_tx_complete(&self) -> bool;
52
53 fn read(&mut self) -> nb::Result<u16, Error>;
54 fn is_rx_not_empty(&self) -> bool;
55
56 fn set_interrupt(&mut self, event: UartEvent, enable: bool);
57 fn is_interrupt_enable(&mut self, event: UartEvent) -> bool;
58 fn is_interrupted(&mut self, event: UartEvent) -> bool;
59
60 fn clear_err_flag(&self);
61
62 fn get_tx_data_reg_addr(&self) -> usize;
63 fn get_rx_data_reg_addr(&self) -> usize;
64 fn enable_dma_tx(&mut self, enable: bool);
65 fn enable_dma_rx(&mut self, enable: bool);
66}
67
68#[derive(Clone, Copy, Debug, PartialEq, Eq)]
69pub enum UartEvent {
70 TxEmpty,
72 RxNotEmpty,
74 Idle,
76}
77
78#[derive(Debug)]
80#[non_exhaustive]
81pub enum Error {
82 Overrun,
84 FrameFormat,
87 Parity,
89 Noise,
91 Busy,
93 Other,
95}
96
97impl embedded_io::Error for Error {
98 #[inline]
99 fn kind(&self) -> e_io::ErrorKind {
100 match self {
101 Error::Overrun => e_io::ErrorKind::InvalidData,
102 Error::FrameFormat => e_io::ErrorKind::InvalidData,
103 Error::Parity => e_io::ErrorKind::InvalidData,
104 Error::Noise => e_io::ErrorKind::InvalidData,
105 Error::Busy => e_io::ErrorKind::WriteZero,
106 Error::Other => e_io::ErrorKind::Other,
107 }
108 }
109}
110
111impl e_nb::serial::Error for Error {
112 #[inline]
113 fn kind(&self) -> e_nb::serial::ErrorKind {
114 match self {
115 Error::Overrun => e_nb::serial::ErrorKind::Overrun,
116 Error::FrameFormat => e_nb::serial::ErrorKind::FrameFormat,
117 Error::Parity => e_nb::serial::ErrorKind::Parity,
118 Error::Noise => e_nb::serial::ErrorKind::Noise,
119 Error::Busy => e_nb::serial::ErrorKind::Other,
120 Error::Other => e_nb::serial::ErrorKind::Other,
121 }
122 }
123}
124
125pub enum WordLength {
126 Bits8,
129 Bits9,
132}
133
134pub enum Parity {
135 ParityNone,
136 ParityEven,
137 ParityOdd,
138}
139
140pub enum StopBits {
141 STOP1,
143 STOP0P5,
145 STOP2,
147 STOP1P5,
149}
150
151pub struct Config {
152 pub baudrate: u32,
153 pub word_length: WordLength,
154 pub parity: Parity,
155 pub stop_bits: StopBits,
156}
157
158impl Default for Config {
159 fn default() -> Config {
160 Config {
161 baudrate: 115_200,
162 word_length: WordLength::Bits8,
163 parity: Parity::ParityNone,
164 stop_bits: StopBits::STOP1,
165 }
166 }
167}
168
169impl Config {
170 pub fn baudrate(mut self, baudrate: u32) -> Self {
171 self.baudrate = baudrate;
172 self
173 }
174
175 pub fn word_length(mut self, wordlength: WordLength) -> Self {
176 self.word_length = wordlength;
177 self
178 }
179
180 pub fn word_length_8bits(mut self) -> Self {
181 self.word_length = WordLength::Bits8;
182 self
183 }
184
185 pub fn word_length_9bits(mut self) -> Self {
186 self.word_length = WordLength::Bits9;
187 self
188 }
189
190 pub fn parity(mut self, parity: Parity) -> Self {
191 self.parity = parity;
192 self
193 }
194
195 pub fn parity_none(mut self) -> Self {
196 self.parity = Parity::ParityNone;
197 self
198 }
199
200 pub fn parity_even(mut self) -> Self {
201 self.parity = Parity::ParityEven;
202 self
203 }
204
205 pub fn parity_odd(mut self) -> Self {
206 self.parity = Parity::ParityOdd;
207 self
208 }
209
210 pub fn stop_bits(mut self, stop_bits: StopBits) -> Self {
211 self.stop_bits = stop_bits;
212 self
213 }
214}