stm32f1_hal/uart/
usart3.rs

1use crate::pac::usart1::cr1;
2type UartX = pac::USART3;
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_u1
58        use pac::usart1::cr2::STOP;
59
60        self.cr2().write(|w| {
61            w.stop().variant(match bits {
62                StopBits::STOP0P5 => STOP::Stop0p5,
63                StopBits::STOP1 => STOP::Stop1,
64                StopBits::STOP1P5 => STOP::Stop1p5,
65                StopBits::STOP2 => STOP::Stop2,
66            })
67        });
68        // sync stop_bits_u1_end
69    }
70}
71
72// Implement Peripheral -------------------------------------------------------
73
74impl UartPeriph for UartX {
75    #[inline]
76    fn set_dma_tx(&mut self, enable: bool) {
77        self.cr3().modify(|_, w| w.dmat().bit(enable));
78    }
79
80    #[inline]
81    fn set_dma_rx(&mut self, enable: bool) {
82        self.cr3().modify(|_, w| w.dmar().bit(enable));
83    }
84
85    #[inline]
86    fn is_tx_empty(&self) -> bool {
87        self.sr().read().txe().bit_is_set()
88    }
89
90    #[inline]
91    fn is_tx_complete(&self) -> bool {
92        self.sr().read().tc().bit_is_set()
93    }
94
95    fn write(&mut self, word: u16) -> nb::Result<(), Error> {
96        if self.is_tx_empty() {
97            self.dr().write(|w| unsafe { w.dr().bits(word.into()) });
98            Ok(())
99        } else {
100            Err(nb::Error::WouldBlock)
101        }
102    }
103
104    fn read(&mut self) -> nb::Result<u16, Error> {
105        let sr = self.sr().read();
106
107        // Check if a byte is available
108        if sr.rxne().bit_is_set() {
109            // Read the received byte
110            return Ok(self.dr().read().dr().bits());
111        }
112
113        // Check for any errors
114        let err = if sr.pe().bit_is_set() {
115            Some(Error::Parity)
116        } else if sr.fe().bit_is_set() {
117            Some(Error::FrameFormat)
118        } else if sr.ne().bit_is_set() {
119            Some(Error::Noise)
120        } else if sr.ore().bit_is_set() {
121            Some(Error::Overrun)
122        } else {
123            None
124        };
125
126        if let Some(err) = err {
127            self.clear_err_flag();
128            Err(nb::Error::Other(err))
129        } else {
130            Err(nb::Error::WouldBlock)
131        }
132    }
133
134    #[inline]
135    fn get_tx_data_reg_addr(&self) -> u32 {
136        &self.dr() as *const _ as u32
137    }
138
139    #[inline]
140    fn get_rx_data_reg_addr(&self) -> u32 {
141        &self.dr() as *const _ as u32
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