11//! API for the integrated USART ports
22//!
3- //! This only implements the usual asynchronous bidirectional 8-bit transfers, everything else is missing
3+ //! This only implements the usual asynchronous bidirectional 8-bit transfers.
4+ //!
5+ //! It's possible to use a read-only/write-only serial implementation with
6+ //! `usartXrx`/`usartXtx`.
47//!
58//! # Examples
9+ //! Echo
610//! ``` no_run
711//! use stm32f0xx_hal as hal;
812//!
2024//! let tx = gpioa.pa9.into_alternate_af1(cs);
2125//! let rx = gpioa.pa10.into_alternate_af1(cs);
2226//!
23- //! let serial = Serial::usart1(p.USART1, (tx, rx), 115_200.bps(), &mut rcc);
27+ //! let mut serial = Serial::usart1(p.USART1, (tx, rx), 115_200.bps(), &mut rcc);
28+ //!
29+ //! loop {
30+ //! let received = block!(serial.read()).unwrap();
31+ //! block!(serial.write(received)).ok();
32+ //! }
33+ //! });
34+ //! ```
35+ //!
36+ //! Hello World
37+ //! ``` no_run
38+ //! use stm32f0xx_hal as hal;
39+ //!
40+ //! use crate::hal::prelude::*;
41+ //! use crate::hal::serial::Serial;
42+ //! use crate::hal::stm32;
43+ //!
44+ //! use nb::block;
45+ //!
46+ //! cortex_m::interrupt::free(|cs| {
47+ //! let rcc = p.RCC.configure().sysclk(48.mhz()).freeze();
48+ //!
49+ //! let gpioa = p.GPIOA.split(&mut rcc);
50+ //!
51+ //! let tx = gpioa.pa9.into_alternate_af1(cs);
2452//!
25- //! let ( mut tx, mut rx) = serial.split( );
53+ //! let mut serial = Serial::usart1tx(p.USART1, tx, 115_200.bps(), &mut rcc );
2654//!
2755//! loop {
28- //! let received = block!(rx.read()).unwrap();
29- //! block!(tx.write(received)).ok();
56+ //! serial.write_str("Hello World!\r\n");
3057//! }
3158//! });
3259//! ```
@@ -466,6 +493,19 @@ where
466493 }
467494}
468495
496+ impl < USART , TXPIN , RXPIN > Write for Serial < USART , TXPIN , RXPIN >
497+ where
498+ USART : Deref < Target = SerialRegisterBlock > ,
499+ TXPIN : TxPin < USART > ,
500+ {
501+ fn write_str ( & mut self , s : & str ) -> Result {
502+ use nb:: block;
503+
504+ let _ = s. as_bytes ( ) . iter ( ) . map ( |c| block ! ( self . write( * c) ) ) . last ( ) ;
505+ Ok ( ( ) )
506+ }
507+ }
508+
469509/// Ensures that none of the previously written words are still buffered
470510fn flush ( usart : * const SerialRegisterBlock ) -> nb:: Result < ( ) , void:: Void > {
471511 // NOTE(unsafe) atomic read with no side effects
@@ -478,7 +518,7 @@ fn flush(usart: *const SerialRegisterBlock) -> nb::Result<(), void::Void> {
478518 }
479519}
480520
481- /// Tries to write a byte to the uart
521+ /// Tries to write a byte to the UART
482522/// Fails if the transmit buffer is full
483523fn write ( usart : * const SerialRegisterBlock , byte : u8 ) -> nb:: Result < ( ) , void:: Void > {
484524 // NOTE(unsafe) atomic read with no side effects
@@ -494,7 +534,7 @@ fn write(usart: *const SerialRegisterBlock, byte: u8) -> nb::Result<(), void::Vo
494534 }
495535}
496536
497- /// Tries to read a byte from the uart
537+ /// Tries to read a byte from the UART
498538fn read ( usart : * const SerialRegisterBlock ) -> nb:: Result < u8 , Error > {
499539 // NOTE(unsafe) atomic read with no side effects
500540 let isr = unsafe { ( * usart) . isr . read ( ) } ;
0 commit comments