forked from stm32-rs/stm32h7xx-hal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serial.rs
61 lines (46 loc) · 1.51 KB
/
serial.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//! Creates a 19200 baud USART on pins PC10 and PC11. It prints "Hello, world!"
//! and then echos any received bytes
#![deny(warnings)]
#![no_main]
#![no_std]
use cortex_m_rt::entry;
#[macro_use]
mod utilities;
use log::info;
use stm32h7xx_hal::{pac, prelude::*};
use core::fmt::Write;
use nb::block;
#[entry]
fn main() -> ! {
utilities::logger::init();
let dp = pac::Peripherals::take().unwrap();
// Constrain and Freeze power
info!("Setup PWR... ");
let pwr = dp.PWR.constrain();
let pwrcfg = example_power!(pwr).freeze();
// Constrain and Freeze clock
info!("Setup RCC... ");
let rcc = dp.RCC.constrain();
let ccdr = rcc.sys_ck(160.MHz()).freeze(pwrcfg, &dp.SYSCFG);
// Acquire the GPIOC peripheral. This also enables the clock for
// GPIOC in the RCC register.
let gpioc = dp.GPIOC.split(ccdr.peripheral.GPIOC);
let tx = gpioc.pc10.into_alternate();
let rx = gpioc.pc11.into_alternate();
info!("");
info!("stm32h7xx-hal example - USART");
info!("");
// Configure the serial peripheral.
let serial = dp
.USART3
.serial((tx, rx), 19_200.bps(), ccdr.peripheral.USART3, &ccdr.clocks)
.unwrap();
let (mut tx, mut rx) = serial.split();
// core::fmt::Write is implemented for tx.
writeln!(tx, "Hello, world!").unwrap();
loop {
// Echo what is received on the serial link.
let received = block!(rx.read()).unwrap();
block!(tx.write(received)).ok();
}
}