@@ -210,16 +210,53 @@ unsafe impl<USART> Send for Tx<USART> {}
210210
211211#[ cfg( feature = "device-selected" ) ]
212212macro_rules! usart {
213- ( $( $USART: ident: ( $usart: ident, $usartXen: ident, $apbenr: ident) , ) +) => {
213+ ( $( $USART: ident: ( $usart: ident, $usarttx : ident , $usartrx : ident , $ usartXen: ident, $apbenr: ident) , ) +) => {
214214 $(
215215 use crate :: stm32:: $USART;
216- impl <TXPIN , RXPIN > Serial <$USART, TXPIN , RXPIN > {
216+ impl <TXPIN , RXPIN > Serial <$USART, TXPIN , RXPIN >
217+ where
218+ TXPIN : TxPin <$USART>,
219+ RXPIN : RxPin <$USART>,
220+ {
217221 /// Creates a new serial instance
218222 pub fn $usart( usart: $USART, pins: ( TXPIN , RXPIN ) , baud_rate: Bps , clocks: Clocks ) -> Self
219- where
220- TXPIN : TxPin <$USART>,
221- RXPIN : RxPin <$USART>,
222223 {
224+ let mut serial = Serial { usart, pins } ;
225+ serial. enable( baud_rate, clocks) ;
226+ serial
227+ }
228+ }
229+
230+ impl <TXPIN > Serial <$USART, TXPIN , ( ) >
231+ where
232+ TXPIN : TxPin <$USART>,
233+ {
234+ /// Creates a new tx-only serial instance
235+ pub fn $usarttx( usart: $USART, txpin: TXPIN , baud_rate: Bps , clocks: Clocks ) -> Self
236+ {
237+ let rxpin = ( ) ;
238+ let mut serial = Serial { usart, pins: ( txpin, rxpin) } ;
239+ serial. enable( baud_rate, clocks) ;
240+ serial
241+ }
242+ }
243+
244+ impl <RXPIN > Serial <$USART, ( ) , RXPIN >
245+ where
246+ RXPIN : RxPin <$USART>,
247+ {
248+ /// Creates a new tx-only serial instance
249+ pub fn $usartrx( usart: $USART, rxpin: RXPIN , baud_rate: Bps , clocks: Clocks ) -> Self
250+ {
251+ let txpin = ( ) ;
252+ let mut serial = Serial { usart, pins: ( txpin, rxpin) } ;
253+ serial. enable( baud_rate, clocks) ;
254+ serial
255+ }
256+ }
257+
258+ impl <TXPIN , RXPIN > Serial <$USART, TXPIN , RXPIN > {
259+ fn enable( & mut self , baud_rate: Bps , clocks: Clocks ) {
223260 // NOTE(unsafe) This executes only during initialisation
224261 let rcc = unsafe { & ( * crate :: stm32:: RCC :: ptr( ) ) } ;
225262
@@ -228,16 +265,14 @@ macro_rules! usart {
228265
229266 // Calculate correct baudrate divisor on the fly
230267 let brr = clocks. pclk( ) . 0 / baud_rate. 0 ;
231- usart. brr. write( |w| unsafe { w. bits( brr) } ) ;
268+ self . usart. brr. write( |w| unsafe { w. bits( brr) } ) ;
232269
233270 // Reset other registers to disable advanced USART features
234- usart. cr2. reset( ) ;
235- usart. cr3. reset( ) ;
271+ self . usart. cr2. reset( ) ;
272+ self . usart. cr3. reset( ) ;
236273
237274 // Enable transmission and receiving
238- usart. cr1. modify( |_, w| w. te( ) . set_bit( ) . re( ) . set_bit( ) . ue( ) . set_bit( ) ) ;
239-
240- Serial { usart, pins }
275+ self . usart. cr1. modify( |_, w| w. te( ) . set_bit( ) . re( ) . set_bit( ) . ue( ) . set_bit( ) ) ;
241276 }
242277
243278 /// Starts listening for an interrupt event
@@ -276,7 +311,7 @@ macro_rules! usart {
276311
277312#[ cfg( feature = "device-selected" ) ]
278313usart ! {
279- USART1 : ( usart1, usart1en, apb2enr) ,
314+ USART1 : ( usart1, usart1tx , usart1rx , usart1en, apb2enr) ,
280315}
281316#[ cfg( any(
282317 feature = "stm32f030x8" ,
@@ -287,7 +322,7 @@ usart! {
287322 feature = "stm32f091" ,
288323) ) ]
289324usart ! {
290- USART2 : ( usart2, usart2en, apb1enr) ,
325+ USART2 : ( usart2, usart2tx , usart2rx , usart2en, apb1enr) ,
291326}
292327#[ cfg( any(
293328 feature = "stm32f030xc" ,
@@ -296,13 +331,13 @@ usart! {
296331 feature = "stm32f091" ,
297332) ) ]
298333usart ! {
299- USART3 : ( usart3, usart3en, apb1enr) ,
300- USART4 : ( usart4, usart4en, apb1enr) ,
334+ USART3 : ( usart3, usart3tx , usart3rx , usart3en, apb1enr) ,
335+ USART4 : ( usart4, usart4tx , usart4rx , usart4en, apb1enr) ,
301336}
302337#[ cfg( any( feature = "stm32f030xc" , feature = "stm32f091" ) ) ]
303338usart ! {
304- USART5 : ( usart5, usart5en, apb1enr) ,
305- USART6 : ( usart6, usart6en, apb2enr) ,
339+ USART5 : ( usart5, usart5tx , usart5rx , usart5en, apb1enr) ,
340+ USART6 : ( usart6, usart6tx , usart6rx , usart6en, apb2enr) ,
306341}
307342
308343// It's s needed for the impls, but rustc doesn't recognize that
@@ -339,6 +374,22 @@ where
339374 }
340375}
341376
377+ impl < USART , TXPIN , RXPIN > embedded_hal:: serial:: Read < u8 > for Serial < USART , TXPIN , RXPIN >
378+ where
379+ USART : Deref < Target = SerialRegisterBlock > ,
380+ RXPIN : RxPin < USART > ,
381+ {
382+ type Error = Error ;
383+
384+ /// Tries to read a byte from the uart
385+ fn read ( & mut self ) -> nb:: Result < u8 , Error > {
386+ Rx {
387+ usart : & self . usart as * const _ ,
388+ }
389+ . read ( )
390+ }
391+ }
392+
342393#[ cfg( feature = "device-selected" ) ]
343394impl < USART > embedded_hal:: serial:: Write < u8 > for Tx < USART >
344395where
@@ -375,14 +426,43 @@ where
375426 }
376427}
377428
429+ impl < USART , TXPIN , RXPIN > embedded_hal:: serial:: Write < u8 > for Serial < USART , TXPIN , RXPIN >
430+ where
431+ USART : Deref < Target = SerialRegisterBlock > ,
432+ TXPIN : TxPin < USART > ,
433+ {
434+ type Error = void:: Void ;
435+
436+ /// Ensures that none of the previously written words are still buffered
437+ fn flush ( & mut self ) -> nb:: Result < ( ) , Self :: Error > {
438+ Tx {
439+ usart : & self . usart as * const _ ,
440+ }
441+ . flush ( )
442+ }
443+
444+ /// Tries to write a byte to the uart
445+ /// Fails if the transmit buffer is full
446+ fn write ( & mut self , byte : u8 ) -> nb:: Result < ( ) , Self :: Error > {
447+ Tx {
448+ usart : & self . usart as * const _ ,
449+ }
450+ . write ( byte)
451+ }
452+ }
453+
378454#[ cfg( feature = "device-selected" ) ]
379455impl < USART , TXPIN , RXPIN > Serial < USART , TXPIN , RXPIN >
380456where
381457 USART : Deref < Target = SerialRegisterBlock > ,
382458{
383459 /// Splits the UART Peripheral in a Tx and an Rx part
384460 /// This is required for sending/receiving
385- pub fn split ( self ) -> ( Tx < USART > , Rx < USART > ) {
461+ pub fn split ( self ) -> ( Tx < USART > , Rx < USART > )
462+ where
463+ TXPIN : TxPin < USART > ,
464+ RXPIN : RxPin < USART > ,
465+ {
386466 (
387467 Tx {
388468 usart : & self . usart as * const _ ,
@@ -392,6 +472,7 @@ where
392472 } ,
393473 )
394474 }
475+
395476 pub fn release ( self ) -> ( USART , ( TXPIN , RXPIN ) ) {
396477 ( self . usart , self . pins )
397478 }
0 commit comments