stm32f1_hal/uart/
uart4.rs

1use crate::pac::uart4::cr1;
2type UartX = pac::UART4;
3
4// sync begin
5
6use super::*;
7use crate::{Mcu, pac};
8
9// Initialization -------------------------------------------------------------
10
11impl UartInit<UartX> for UartX {
12    fn constrain(self, mcu: &mut Mcu) -> Uart<UartX> {
13        mcu.rcc.enable(&self);
14        mcu.rcc.reset(&self);
15        Uart { uart: self }
16    }
17}
18
19impl UartPeriphExt for UartX {
20    fn config(&mut self, config: Config, mcu: &mut Mcu) {
21        // Configure baud rate
22        let brr = mcu.rcc.get_clock(self).raw() / config.baudrate;
23        assert!(brr >= 16, "impossible baud rate");
24        self.brr().write(|w| unsafe { w.bits(brr as u16) });
25
26        // Configure word
27        self.cr1().modify(|_, w| {
28            w.m().bit(match config.word_length {
29                WordLength::Bits8 => false,
30                WordLength::Bits9 => true,
31            });
32            w.ps().variant(match config.parity {
33                Parity::ParityOdd => cr1::PS::Odd,
34                _ => cr1::PS::Even,
35            });
36            w.pce().bit(!matches!(config.parity, Parity::ParityNone));
37            w
38        });
39
40        // Configure stop bits
41        self.set_stop_bits(config.stop_bits);
42    }
43
44    fn enable_comm(&mut self, tx: bool, rx: bool) {
45        // UE: enable USART
46        // TE: enable transceiver
47        // RE: enable receiver
48        self.cr1().modify(|_, w| {
49            w.ue().set_bit();
50            w.te().bit(tx);
51            w.re().bit(rx);
52            w
53        });
54    }
55
56    fn set_stop_bits(&mut self, bits: StopBits) {
57        // sync stop_bits_u4
58        use pac::uart4::cr2::STOP;
59
60        // StopBits::STOP0P5 and StopBits::STOP1P5 aren't supported when using UART
61        // STOP_A::STOP1 and STOP_A::STOP2 will be used, respectively
62        self.cr2().write(|w| {
63            w.stop().variant(match bits {
64                StopBits::STOP0P5 | StopBits::STOP1 => STOP::Stop1,
65                StopBits::STOP1P5 | StopBits::STOP2 => STOP::Stop2,
66            })
67        });
68        // sync stop_bits_end
69    }
70}
71
72// Implement Peripheral -------------------------------------------------------
73
74impl UartPeriph for UartX {
75    #[inline]
76    fn is_tx_empty(&self) -> bool {
77        self.sr().read().txe().bit_is_set()
78    }
79
80    #[inline]
81    fn is_tx_complete(&self) -> bool {
82        self.sr().read().tc().bit_is_set()
83    }
84
85    fn write(&mut self, word: u16) -> nb::Result<(), Error> {
86        if self.is_tx_empty() {
87            self.dr().write(|w| unsafe { w.dr().bits(word) });
88            Ok(())
89        } else {
90            Err(nb::Error::WouldBlock)
91        }
92    }
93
94    fn read(&mut self) -> nb::Result<u16, Error> {
95        let sr = self.sr().read();
96
97        // Check if a byte is available
98        if sr.rxne().bit_is_set() {
99            // Read the received byte
100            return Ok(self.dr().read().dr().bits());
101        }
102
103        // Check for any errors
104        let err = if sr.pe().bit_is_set() {
105            Some(Error::Parity)
106        } else if sr.fe().bit_is_set() {
107            Some(Error::FrameFormat)
108        } else if sr.ne().bit_is_set() {
109            Some(Error::Noise)
110        } else if sr.ore().bit_is_set() {
111            Some(Error::Overrun)
112        } else {
113            None
114        };
115
116        if let Some(err) = err {
117            self.clear_err_flag();
118            Err(nb::Error::Other(err))
119        } else {
120            Err(nb::Error::WouldBlock)
121        }
122    }
123
124    #[inline]
125    fn get_tx_data_reg_addr(&self) -> usize {
126        self.dr().as_ptr() as usize
127    }
128
129    #[inline]
130    fn get_rx_data_reg_addr(&self) -> usize {
131        self.dr().as_ptr() as usize
132    }
133
134    #[inline]
135    fn enable_dma_tx(&mut self, enable: bool) {
136        self.cr3().modify(|_, w| w.dmat().bit(enable));
137    }
138
139    #[inline]
140    fn enable_dma_rx(&mut self, enable: bool) {
141        self.cr3().modify(|_, w| w.dmar().bit(enable));
142    }
143
144    #[inline]
145    fn set_interrupt(&mut self, event: UartEvent, enable: bool) {
146        match event {
147            UartEvent::Idle => {
148                self.cr1().modify(|_, w| w.idleie().bit(enable));
149            }
150            UartEvent::RxNotEmpty => {
151                self.cr1().modify(|_, w| w.rxneie().bit(enable));
152            }
153            UartEvent::TxEmpty => {
154                self.cr1().modify(|_, w| w.txeie().bit(enable));
155            }
156        }
157    }
158
159    #[inline]
160    fn is_interrupt_enable(&mut self, event: UartEvent) -> bool {
161        let cr1 = self.cr1().read();
162        match event {
163            UartEvent::Idle => cr1.idleie().bit_is_set(),
164            UartEvent::RxNotEmpty => cr1.rxneie().bit_is_set(),
165            UartEvent::TxEmpty => cr1.txeie().bit_is_set(),
166        }
167    }
168
169    #[inline]
170    fn is_interrupted(&mut self, event: UartEvent) -> bool {
171        let sr = self.sr().read();
172        match event {
173            UartEvent::Idle => {
174                if sr.idle().bit_is_set() && self.cr1().read().idleie().bit_is_set() {
175                    self.clear_err_flag();
176                    return true;
177                }
178            }
179            UartEvent::RxNotEmpty => {
180                if (sr.rxne().bit_is_set() || sr.ore().bit_is_set())
181                    && self.cr1().read().rxneie().bit_is_set()
182                {
183                    return true;
184                }
185            }
186            UartEvent::TxEmpty => {
187                if sr.txe().bit_is_set() && self.cr1().read().txeie().bit_is_set() {
188                    return true;
189                }
190            }
191        }
192        false
193    }
194
195    /// In order to clear that error flag, you have to do a read from the sr register
196    /// followed by a read from the dr register.
197    #[inline]
198    fn clear_err_flag(&self) {
199        let _ = self.sr().read();
200        let _ = self.dr().read();
201    }
202
203    #[inline]
204    fn is_rx_not_empty(&self) -> bool {
205        self.sr().read().rxne().bit_is_set()
206    }
207}
208
209// sync end