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 embedded_hal_nb as e_nb;
9use embedded_io as e_io;
10
11pub use core::convert::Infallible;
12
13// pub mod uart_dma_tx;
14// pub use uart_dma_tx::*;
15// pub mod uart_dma_ringbuf_tx;
16// pub use uart_dma_ringbuf_tx::*;
17
18// ------------------------------------------------------------------------------------------------
19
20// UART idle interrupt handler
21pub 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
46// Peripheral Trait -----------------------------------------------------------
47
48pub 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    /// New data can be sent
71    TxEmpty,
72    /// New data has been received
73    RxNotEmpty,
74    /// Idle line state detected
75    Idle,
76}
77
78/// UART error
79#[derive(Debug)]
80#[non_exhaustive]
81pub enum Error {
82    /// The peripheral receive buffer was overrun.
83    Overrun,
84    /// Received data does not conform to the peripheral configuration.
85    /// Can be caused by a misconfigured device on either end of the serial line.
86    FrameFormat,
87    /// Parity check failed.
88    Parity,
89    /// UART line is too noisy to read valid data.
90    Noise,
91    /// UART is busy and cannot accept new data.
92    Busy,
93    /// A different error occurred. The original error may contain more information.
94    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    /// When parity is enabled, a word has 7 data bits + 1 parity bit,
127    /// otherwise 8 data bits.
128    Bits8,
129    /// When parity is enabled, a word has 8 data bits + 1 parity bit,
130    /// otherwise 9 data bits.
131    Bits9,
132}
133
134pub enum Parity {
135    ParityNone,
136    ParityEven,
137    ParityOdd,
138}
139
140pub enum StopBits {
141    /// 1 stop bit
142    STOP1,
143    /// 0.5 stop bits
144    STOP0P5,
145    /// 2 stop bits
146    STOP2,
147    /// 1.5 stop bits
148    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}