Skip to content

Commit c8dd9b1

Browse files
Add documentation and update a few examples
Also add the `Write` implementation for Serial
1 parent ec1fa41 commit c8dd9b1

File tree

3 files changed

+54
-18
lines changed

3 files changed

+54
-18
lines changed

examples/serial_echo.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,11 @@ fn main() -> ! {
2626
let tx = gpioa.pa9.into_alternate_af1(cs);
2727
let rx = gpioa.pa10.into_alternate_af1(cs);
2828

29-
let serial = Serial::usart1(p.USART1, (tx, rx), 115_200.bps(), &mut rcc);
30-
31-
let (mut tx, mut rx) = serial.split();
29+
let mut serial = Serial::usart1(p.USART1, (tx, rx), 115_200.bps(), &mut rcc);
3230

3331
loop {
34-
let received = block!(rx.read()).unwrap();
35-
block!(tx.write(received)).ok();
32+
let received = block!(serial.read()).unwrap();
33+
block!(serial.write(received)).ok();
3634
}
3735
});
3836
}

examples/watchdog.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,25 +36,23 @@ fn main() -> ! {
3636
let mut delay = Delay::new(cp.SYST, &rcc);
3737

3838
let tx = gpioa.pa9.into_alternate_af1(cs);
39-
let rx = gpioa.pa10.into_alternate_af1(cs);
4039

41-
let serial = Serial::usart1(p.USART1, (tx, rx), 115_200.bps(), &mut rcc);
40+
let mut serial = Serial::usart1tx(p.USART1, tx, 115_200.bps(), &mut rcc);
4241

43-
let (mut tx, _rx) = serial.split();
44-
tx.write_str("RESET \r\n").ok();
42+
serial.write_str("RESET \r\n").ok();
4543

4644
watchdog.start(Hertz(1));
4745
delay.delay_ms(500_u16);
4846
watchdog.feed();
4947
delay.delay_ms(500_u16);
5048
watchdog.feed();
5149
delay.delay_ms(500_u16);
52-
tx.write_str("This will get printed \r\n").ok();
50+
serial.write_str("This will get printed \r\n").ok();
5351
watchdog.feed();
5452

5553
// Now a reset happens while delaying
5654
delay.delay_ms(1500_u16);
57-
tx.write_str("This won't\r\n").ok();
55+
serial.write_str("This won't\r\n").ok();
5856
});
5957
}
6058

src/serial.rs

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
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
//!
@@ -20,13 +24,36 @@
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
470510
fn 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
483523
fn 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
498538
fn 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

Comments
 (0)