@@ -102,9 +102,12 @@ use crate::pac::{self, RCC};
102102use crate :: rcc:: { BusClock , Clocks , Enable , Reset } ;
103103use crate :: time:: { Bps , U32Ext } ;
104104
105+ pub mod ext;
105106mod hal_02;
106107mod hal_1;
107108
109+ use ext:: { SrR , UartExt } ;
110+
108111pub trait SerialExt : Sized + Instance {
109112 fn serial < Otype , PULL : UpMode > (
110113 self ,
@@ -159,28 +162,56 @@ impl<USART: Instance> SerialExt for USART {
159162 }
160163}
161164
162- use crate :: pac:: usart1 as uart_base;
165+ pub trait RBExt : UartExt {
166+ fn set_stopbits ( & self , bits : StopBits ) ;
167+ }
168+
169+ impl RBExt for pac:: usart1:: RegisterBlock {
170+ fn set_stopbits ( & self , bits : StopBits ) {
171+ use crate :: pac:: usart1:: cr2:: STOP ;
172+
173+ self . cr2 ( ) . write ( |w| {
174+ w. stop ( ) . variant ( match bits {
175+ StopBits :: STOP0P5 => STOP :: Stop0p5 ,
176+ StopBits :: STOP1 => STOP :: Stop1 ,
177+ StopBits :: STOP1P5 => STOP :: Stop1p5 ,
178+ StopBits :: STOP2 => STOP :: Stop2 ,
179+ } )
180+ } ) ;
181+ }
182+ }
183+
184+ #[ cfg( any( all( feature = "stm32f103" , feature = "high" ) , feature = "connectivity" ) ) ]
185+ impl RBExt for pac:: uart4:: RegisterBlock {
186+ fn set_stopbits ( & self , bits : StopBits ) {
187+ use crate :: pac:: uart4:: cr2:: STOP ;
188+
189+ // StopBits::STOP0P5 and StopBits::STOP1P5 aren't supported when using UART
190+ // STOP_A::STOP1 and STOP_A::STOP2 will be used, respectively
191+ self . cr2 ( ) . write ( |w| {
192+ w. stop ( ) . variant ( match bits {
193+ StopBits :: STOP0P5 | StopBits :: STOP1 => STOP :: Stop1 ,
194+ StopBits :: STOP1P5 | StopBits :: STOP2 => STOP :: Stop2 ,
195+ } )
196+ } ) ;
197+ }
198+ }
163199
164200pub trait Instance :
165201 crate :: Sealed
166- + Deref < Target = uart_base:: RegisterBlock >
202+ + crate :: Ptr < RB : RBExt >
203+ + Deref < Target = Self :: RB >
167204 + Enable
168205 + Reset
169206 + BusClock
170207 + afio:: SerialAsync
171208{
172- #[ doc( hidden) ]
173- fn ptr ( ) -> * const uart_base:: RegisterBlock ;
174209}
175210
176211macro_rules! inst {
177212 ( $( $USARTX: ty; ) +) => {
178213 $(
179- impl Instance for $USARTX {
180- fn ptr( ) -> * const uart_base:: RegisterBlock {
181- <$USARTX>:: ptr( )
182- }
183- }
214+ impl Instance for $USARTX { }
184215 ) +
185216 } ;
186217}
@@ -190,6 +221,11 @@ inst! {
190221 pac:: USART2 ;
191222 pac:: USART3 ;
192223}
224+ #[ cfg( any( all( feature = "stm32f103" , feature = "high" ) , feature = "connectivity" ) ) ]
225+ inst ! {
226+ pac:: UART4 ;
227+ pac:: UART5 ;
228+ }
193229
194230/// Serial error
195231#[ derive( Debug ) ]
@@ -519,7 +555,7 @@ fn apply_config<USART: Instance>(config: Config, clocks: &Clocks) {
519555 // Configure baud rate
520556 let brr = USART :: clock ( clocks) . raw ( ) / config. baudrate . 0 ;
521557 assert ! ( brr >= 16 , "impossible baud rate" ) ;
522- usart. brr ( ) . write ( |w| unsafe { w. bits ( brr) } ) ;
558+ usart. brr ( ) . write ( |w| unsafe { w. bits ( brr as u16 ) } ) ;
523559
524560 // Configure word
525561 usart. cr1 ( ) . modify ( |_r, w| {
@@ -537,13 +573,7 @@ fn apply_config<USART: Instance>(config: Config, clocks: &Clocks) {
537573 } ) ;
538574
539575 // Configure stop bits
540- let stop_bits = match config. stopbits {
541- StopBits :: STOP1 => 0b00 ,
542- StopBits :: STOP0P5 => 0b01 ,
543- StopBits :: STOP2 => 0b10 ,
544- StopBits :: STOP1P5 => 0b11 ,
545- } ;
546- usart. cr2 ( ) . modify ( |_r, w| w. stop ( ) . set ( stop_bits) ) ;
576+ usart. set_stopbits ( config. stopbits ) ;
547577}
548578
549579/// Reconfigure the USART instance.
@@ -657,7 +687,7 @@ impl<USART: Instance> Rx<USART> {
657687 Some ( Error :: Parity )
658688 } else if sr. fe ( ) . bit_is_set ( ) {
659689 Some ( Error :: FrameFormat )
660- } else if sr. ne ( ) . bit_is_set ( ) {
690+ } else if sr. nf ( ) . bit_is_set ( ) {
661691 Some ( Error :: Noise )
662692 } else if sr. ore ( ) . bit_is_set ( ) {
663693 Some ( Error :: Overrun )
0 commit comments