1
1
//! API for the integrated USART ports
2
2
//!
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`.
4
7
//!
5
8
//! # Examples
9
+ //! Echo
6
10
//! ``` no_run
7
11
//! use stm32f0xx_hal as hal;
8
12
//!
20
24
//! let tx = gpioa.pa9.into_alternate_af1(cs);
21
25
//! let rx = gpioa.pa10.into_alternate_af1(cs);
22
26
//!
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);
24
52
//!
25
- //! let ( mut tx, mut rx) = serial.split( );
53
+ //! let mut serial = Serial::usart1tx(p.USART1, tx, 115_200.bps(), &mut rcc );
26
54
//!
27
55
//! loop {
28
- //! let received = block!(rx.read()).unwrap();
29
- //! block!(tx.write(received)).ok();
56
+ //! serial.write_str("Hello World!\r\n");
30
57
//! }
31
58
//! });
32
59
//! ```
@@ -466,6 +493,19 @@ where
466
493
}
467
494
}
468
495
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
+
469
509
/// Ensures that none of the previously written words are still buffered
470
510
fn flush ( usart : * const SerialRegisterBlock ) -> nb:: Result < ( ) , void:: Void > {
471
511
// NOTE(unsafe) atomic read with no side effects
@@ -478,7 +518,7 @@ fn flush(usart: *const SerialRegisterBlock) -> nb::Result<(), void::Void> {
478
518
}
479
519
}
480
520
481
- /// Tries to write a byte to the uart
521
+ /// Tries to write a byte to the UART
482
522
/// Fails if the transmit buffer is full
483
523
fn write ( usart : * const SerialRegisterBlock , byte : u8 ) -> nb:: Result < ( ) , void:: Void > {
484
524
// NOTE(unsafe) atomic read with no side effects
@@ -494,7 +534,7 @@ fn write(usart: *const SerialRegisterBlock, byte: u8) -> nb::Result<(), void::Vo
494
534
}
495
535
}
496
536
497
- /// Tries to read a byte from the uart
537
+ /// Tries to read a byte from the UART
498
538
fn read ( usart : * const SerialRegisterBlock ) -> nb:: Result < u8 , Error > {
499
539
// NOTE(unsafe) atomic read with no side effects
500
540
let isr = unsafe { ( * usart) . isr . read ( ) } ;
0 commit comments