forked from stm32-rs/stm32h7xx-hal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serial-halfduplex.rs
86 lines (72 loc) · 2.47 KB
/
serial-halfduplex.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//! Example of half-duplex USART communication between two USART's.
//!
//! Connect the USART3_TX (PC10) and USART6_TX (PC6) and an external pullup resistor together.
//! i.e., connect one end of a resistor to a 3.3V pin and connect both PC10 and PC6 pins to the other end
//! of the resistor. 330 ohm resistor worked but exact value is not critical.
//!
//! It will print "Hello, world!" over and over again one character at a time.
#![deny(warnings)]
#![no_main]
#![no_std]
use cortex_m_rt::entry;
#[macro_use]
mod utilities;
use log::info;
use stm32h7xx_hal::{pac, prelude::*, serial};
use nb::block;
#[entry]
fn main() -> ! {
utilities::logger::init();
let dp = pac::Peripherals::take().unwrap();
// Constrain and Freeze power
let pwr = dp.PWR.constrain();
let pwrcfg = example_power!(pwr).freeze();
// Constrain and Freeze clock
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 usart3txpin = gpioc.pc10.into_alternate::<7>();
let usart3rxpin = gpioc.pc11.into_alternate::<7>();
let usart6txpin = gpioc.pc6.into_alternate::<7>();
let usart6rxpin = gpioc.pc7.into_alternate::<7>();
info!("stm32h7xx-hal example - USART half-duplex");
// Configure the serial peripheral.
let config = serial::config::Config::new(9600.bps())
.parity_none()
.halfduplex(true)
.stopbits(serial::config::StopBits::Stop1);
let mut usart3 = dp
.USART3
.serial(
(usart3txpin, usart3rxpin),
config,
ccdr.peripheral.USART3,
&ccdr.clocks,
)
.unwrap();
let mut usart6 = dp
.USART6
.serial(
(usart6txpin, usart6rxpin),
config,
ccdr.peripheral.USART6,
&ccdr.clocks,
)
.unwrap();
loop {
// Tx from usart3
for c in "Hello, world!".chars() {
block!(usart3.write(c as u8)).unwrap();
let received = block!(usart6.read()).unwrap();
info!("usart6 rx {}", received as char);
}
// Tx from usart6
for c in "Hello, world!".chars() {
block!(usart6.write(c as u8)).unwrap();
let received = block!(usart3.read()).unwrap();
info!("usart3 rx {}", received as char);
}
}
}