stm32f1_hal/common/uart/
mod.rs

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
12// pub mod uart_dma_tx;
13// pub use uart_dma_tx::*;
14// pub mod uart_dma_ringbuf_rx;
15// pub use uart_dma_ringbuf_rx::*;
16// pub mod uart_dma_ringbuf_tx;
17// pub use uart_dma_ringbuf_tx::*;
18
19/// UART Transmitter
20pub 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 get_interrupt_handler(&self) -> UartInterrupt<U> {
30    //     UartInterrupt::new(unsafe { self.uart.steal_mut() })
31    // }
32
33    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    // pub fn into_dma<CH>(self, dma_ch: CH) -> UartDmaTx<U, CH>
53    // where
54    //     CH: BindDmaTx<U>,
55    // {
56    //     UartDmaTx::<U, CH>::new(self.uart, dma_ch)
57    // }
58
59    // pub fn into_dma_ringbuf<CH>(self, dma_ch: CH, buf_size: usize) -> UartDmaBufTx<U, CH>
60    // where
61    //     CH: BindDmaTx<U>,
62    // {
63    //     UartDmaBufTx::<U, CH>::new(self.uart, dma_ch, buf_size)
64    // }
65}
66
67// ------------------------------------------------------------------------------------------------
68
69/// UART Receiver
70pub 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    // pub fn into_dma_circle<CH>(self, dma_ch: CH, buf_size: usize) -> UartDmaBufRx<U, CH>
98    // where
99    //     CH: BindDmaRx<U>,
100    // {
101    //     UartDmaBufRx::<U, CH>::new(self.uart, dma_ch, buf_size)
102    // }
103}
104
105// ------------------------------------------------------------------------------------------------
106
107// UART idle interrupt handler
108pub 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
133// Peripheral Trait -----------------------------------------------------------
134
135pub 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    /// New data can be sent
158    TxEmpty,
159    /// New data has been received
160    RxNotEmpty,
161    /// Idle line state detected
162    Idle,
163}
164
165/// UART error
166#[derive(Debug)]
167#[non_exhaustive]
168pub enum Error {
169    /// The peripheral receive buffer was overrun.
170    Overrun,
171    /// Received data does not conform to the peripheral configuration.
172    /// Can be caused by a misconfigured device on either end of the serial line.
173    FrameFormat,
174    /// Parity check failed.
175    Parity,
176    /// UART line is too noisy to read valid data.
177    Noise,
178    /// UART is busy and cannot accept new data.
179    Busy,
180    /// A different error occurred. The original error may contain more information.
181    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    /// When parity is enabled, a word has 7 data bits + 1 parity bit,
214    /// otherwise 8 data bits.
215    Bits8,
216    /// When parity is enabled, a word has 8 data bits + 1 parity bit,
217    /// otherwise 9 data bits.
218    Bits9,
219}
220
221pub enum Parity {
222    ParityNone,
223    ParityEven,
224    ParityOdd,
225}
226
227pub enum StopBits {
228    /// 1 stop bit
229    STOP1,
230    /// 0.5 stop bits
231    STOP0P5,
232    /// 2 stop bits
233    STOP2,
234    /// 1.5 stop bits
235    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}