Skip to content

Commit 3bfec9c

Browse files
committed
Add serial example using RTIC
1 parent 50b2e34 commit 3bfec9c

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ required-features = ["rt"]
100100
name = "rtic_frame_serial_dma"
101101
required-features = ["rt", "stm32l4x2"]
102102

103+
[[example]]
104+
name = "serial_echo_rtic"
105+
required-features = ["rt", "stm32l4x3"]
106+
103107
[[example]]
104108
name = "timer"
105109
required-features = ["rt"]

examples/serial_echo_rtic.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#![no_main]
2+
#![no_std]
3+
4+
extern crate panic_halt;
5+
6+
use nb::block;
7+
use stm32l4xx_hal::{
8+
prelude::*,
9+
pac::{
10+
self,
11+
USART2,
12+
},
13+
serial::{
14+
self,
15+
Config,
16+
Serial,
17+
},
18+
};
19+
20+
#[rtic::app(device = stm32l4xx_hal::pac)]
21+
const APP: () = {
22+
struct Resources {
23+
rx: serial::Rx<USART2>,
24+
tx: serial::Tx<USART2>,
25+
}
26+
27+
#[init]
28+
fn init(_: init::Context) -> init::LateResources {
29+
let p = pac::Peripherals::take().unwrap();
30+
31+
let mut rcc = p.RCC.constrain();
32+
let mut flash = p.FLASH.constrain();
33+
let mut pwr = p.PWR.constrain(&mut rcc.apb1r1);
34+
35+
let clocks = rcc.cfgr.freeze(&mut flash.acr, &mut pwr);
36+
37+
let mut gpioa = p.GPIOA.split(&mut rcc.ahb2);
38+
39+
let tx_pin = gpioa.pa2.into_af7(&mut gpioa.moder, &mut gpioa.afrl);
40+
let rx_pin = gpioa.pa3.into_af7(&mut gpioa.moder, &mut gpioa.afrl);
41+
42+
let serial = Serial::usart2(
43+
p.USART2,
44+
(tx_pin, rx_pin),
45+
Config::default()
46+
.baudrate(115_200.bps()),
47+
clocks,
48+
&mut rcc.apb1r1,
49+
);
50+
let (tx, rx) = serial.split();
51+
52+
init::LateResources {
53+
rx,
54+
tx,
55+
}
56+
}
57+
58+
#[idle(resources = [rx, tx])]
59+
fn idle(cx: idle::Context) -> ! {
60+
let rx = cx.resources.rx;
61+
let tx = cx.resources.tx;
62+
63+
loop {
64+
let b = block!(rx.read()).unwrap();
65+
block!(tx.write(b)).unwrap();
66+
}
67+
}
68+
};

0 commit comments

Comments
 (0)