Skip to content

Commit 9ef9b2d

Browse files
Merge #214
214: add USART example r=therealprof a=nebelgrau77 Co-authored-by: Daniel Egger <[email protected]>
2 parents 739a438 + 2ba308b commit 9ef9b2d

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

examples/serial.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#![no_main]
2+
#![no_std]
3+
4+
use panic_halt as _;
5+
6+
use cortex_m;
7+
use cortex_m_rt::entry;
8+
use stm32f4xx_hal as hal;
9+
10+
use crate::hal::{prelude::*, serial::config::Config, serial::Serial, stm32};
11+
12+
use core::fmt::Write; // for pretty formatting of the serial output
13+
14+
#[entry]
15+
fn main() -> ! {
16+
let dp = stm32::Peripherals::take().unwrap();
17+
let cp = cortex_m::peripheral::Peripherals::take().unwrap();
18+
19+
let gpioa = dp.GPIOA.split();
20+
21+
let rcc = dp.RCC.constrain();
22+
23+
let clocks = rcc.cfgr.use_hse(8.mhz()).freeze();
24+
25+
let mut delay = hal::delay::Delay::new(cp.SYST, clocks);
26+
27+
// define RX/TX pins
28+
let tx_pin = gpioa.pa2.into_alternate_af7();
29+
let rx_pin = gpioa.pa3.into_alternate_af7();
30+
31+
// configure serial
32+
let serial = Serial::usart2(
33+
dp.USART2,
34+
(tx_pin, rx_pin),
35+
Config::default().baudrate(9600.bps()),
36+
clocks,
37+
)
38+
.unwrap();
39+
40+
let (mut tx, mut _rx) = serial.split();
41+
42+
let mut value: u8 = 0;
43+
44+
loop {
45+
// print some value every 500 ms, value will overflow after 255
46+
writeln!(tx, "value: {:02}\r", value).unwrap();
47+
value += 1;
48+
delay.delay_ms(500_u32);
49+
}
50+
}

0 commit comments

Comments
 (0)