stm32f1_hal/common/uart/
mod.rs

1mod uart_dma;
2pub use uart_dma::*;
3mod uart_it;
4pub use uart_it::*;
5mod uart_poll;
6pub use uart_poll::*;
7
8use core::fmt::Display;
9use embedded_hal_nb as e_nb;
10use embedded_io as e_io;
11
12pub use core::convert::Infallible;
13
14// pub mod uart_dma_tx;
15// pub use uart_dma_tx::*;
16// pub mod uart_dma_ringbuf_tx;
17// pub use uart_dma_ringbuf_tx::*;
18
19// ------------------------------------------------------------------------------------------------
20
21// UART idle interrupt handler
22pub struct UartIdleInterrupt<U: UartPeriph> {
23    uart: U,
24}
25
26impl<U: UartPeriph> UartIdleInterrupt<U> {
27    pub fn new(uart: U) -> Self {
28        Self { uart }
29    }
30
31    #[inline]
32    pub fn is_interrupted(&mut self) -> bool {
33        self.uart.is_interrupted(UartEvent::Idle)
34    }
35
36    #[inline]
37    pub fn listen(&mut self) {
38        self.uart.set_interrupt(UartEvent::Idle, true);
39    }
40
41    #[inline]
42    pub fn unlisten(&mut self) {
43        self.uart.set_interrupt(UartEvent::Idle, false);
44    }
45}
46
47// Peripheral Trait -----------------------------------------------------------
48
49pub trait UartPeriph {
50    fn write(&mut self, word: u16) -> nb::Result<(), Error>;
51    fn is_tx_empty(&self) -> bool;
52    fn is_tx_complete(&self) -> bool;
53
54    fn read(&mut self) -> nb::Result<u16, Error>;
55    fn is_rx_not_empty(&self) -> bool;
56
57    fn set_interrupt(&mut self, event: UartEvent, enable: bool);
58    fn is_interrupt_enable(&mut self, event: UartEvent) -> bool;
59    fn is_interrupted(&mut self, event: UartEvent) -> bool;
60
61    fn clear_err_flag(&self);
62
63    fn get_tx_data_reg_addr(&self) -> usize;
64    fn get_rx_data_reg_addr(&self) -> usize;
65    fn enable_dma_tx(&mut self, enable: bool);
66    fn enable_dma_rx(&mut self, enable: bool);
67}
68
69#[derive(Clone, Copy, Debug, PartialEq, Eq)]
70pub enum UartEvent {
71    /// New data can be sent
72    TxEmpty,
73    /// New data has been received
74    RxNotEmpty,
75    /// Idle line state detected
76    Idle,
77}
78
79/// UART error
80#[derive(Debug)]
81#[non_exhaustive]
82pub enum Error {
83    /// The peripheral receive buffer was overrun.
84    Overrun,
85    /// Received data does not conform to the peripheral configuration.
86    /// Can be caused by a misconfigured device on either end of the serial line.
87    FrameFormat,
88    /// Parity check failed.
89    Parity,
90    /// UART line is too noisy to read valid data.
91    Noise,
92    /// UART is busy and cannot accept new data.
93    Busy,
94    /// A different error occurred. The original error may contain more information.
95    Other,
96}
97
98impl Display for Error {
99    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
100        match self {
101            Error::Overrun => write!(f, "UART overrun error"),
102            Error::FrameFormat => write!(f, "UART frame format error"),
103            Error::Parity => write!(f, "UART parity error"),
104            Error::Noise => write!(f, "UART noise error"),
105            Error::Busy => write!(f, "UART busy"),
106            Error::Other => write!(f, "UART other error"),
107        }
108    }
109}
110
111impl core::error::Error for Error {}
112
113impl embedded_io::Error for Error {
114    #[inline]
115    fn kind(&self) -> e_io::ErrorKind {
116        match self {
117            Error::Overrun => e_io::ErrorKind::InvalidData,
118            Error::FrameFormat => e_io::ErrorKind::InvalidData,
119            Error::Parity => e_io::ErrorKind::InvalidData,
120            Error::Noise => e_io::ErrorKind::InvalidData,
121            Error::Busy => e_io::ErrorKind::WriteZero,
122            Error::Other => e_io::ErrorKind::Other,
123        }
124    }
125}
126
127impl e_nb::serial::Error for Error {
128    #[inline]
129    fn kind(&self) -> e_nb::serial::ErrorKind {
130        match self {
131            Error::Overrun => e_nb::serial::ErrorKind::Overrun,
132            Error::FrameFormat => e_nb::serial::ErrorKind::FrameFormat,
133            Error::Parity => e_nb::serial::ErrorKind::Parity,
134            Error::Noise => e_nb::serial::ErrorKind::Noise,
135            Error::Busy => e_nb::serial::ErrorKind::Other,
136            Error::Other => e_nb::serial::ErrorKind::Other,
137        }
138    }
139}
140
141pub enum WordLength {
142    /// When parity is enabled, a word has 7 data bits + 1 parity bit,
143    /// otherwise 8 data bits.
144    Bits8,
145    /// When parity is enabled, a word has 8 data bits + 1 parity bit,
146    /// otherwise 9 data bits.
147    Bits9,
148}
149
150pub enum Parity {
151    ParityNone,
152    ParityEven,
153    ParityOdd,
154}
155
156pub enum StopBits {
157    /// 1 stop bit
158    STOP1,
159    /// 0.5 stop bits
160    STOP0P5,
161    /// 2 stop bits
162    STOP2,
163    /// 1.5 stop bits
164    STOP1P5,
165}
166
167pub struct Config {
168    pub baudrate: u32,
169    pub word_length: WordLength,
170    pub parity: Parity,
171    pub stop_bits: StopBits,
172}
173
174impl Default for Config {
175    fn default() -> Config {
176        Config {
177            baudrate: 115_200,
178            word_length: WordLength::Bits8,
179            parity: Parity::ParityNone,
180            stop_bits: StopBits::STOP1,
181        }
182    }
183}
184
185impl Config {
186    pub fn baudrate(mut self, baudrate: u32) -> Self {
187        self.baudrate = baudrate;
188        self
189    }
190
191    pub fn word_length(mut self, wordlength: WordLength) -> Self {
192        self.word_length = wordlength;
193        self
194    }
195
196    pub fn word_length_8bits(mut self) -> Self {
197        self.word_length = WordLength::Bits8;
198        self
199    }
200
201    pub fn word_length_9bits(mut self) -> Self {
202        self.word_length = WordLength::Bits9;
203        self
204    }
205
206    pub fn parity(mut self, parity: Parity) -> Self {
207        self.parity = parity;
208        self
209    }
210
211    pub fn parity_none(mut self) -> Self {
212        self.parity = Parity::ParityNone;
213        self
214    }
215
216    pub fn parity_even(mut self) -> Self {
217        self.parity = Parity::ParityEven;
218        self
219    }
220
221    pub fn parity_odd(mut self) -> Self {
222        self.parity = Parity::ParityOdd;
223        self
224    }
225
226    pub fn stop_bits(mut self, stop_bits: StopBits) -> Self {
227        self.stop_bits = stop_bits;
228        self
229    }
230}