Skip to content

Commit fceedc3

Browse files
author
高庆丰
committed
no message
1 parent d02833d commit fceedc3

File tree

3 files changed

+110
-3
lines changed

3 files changed

+110
-3
lines changed

Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,3 +149,8 @@ required-features = ["has-can", "rt"]
149149
[[example]]
150150
name = "gpio_input"
151151
required-features = ["stm32f103"]
152+
153+
[[example]]
154+
name = "serial-interrupt-idle"
155+
required-features = ["stm32f103", "rt", "medium"]
156+
default-target = "thumbv7m-none-eabi"

examples/serial-interrupt-idle.rs

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
//! Serial interface loopback test
2+
//!
3+
//! You have to short the TX and RX pins to make this program work
4+
5+
#![no_main]
6+
#![no_std]
7+
8+
use panic_halt as _;
9+
10+
use cortex_m_rt::entry;
11+
use stm32f1xx_hal::{
12+
pac,
13+
pac::interrupt,
14+
pac::USART1,
15+
prelude::*,
16+
serial::{Config, Rx, Serial, Tx},
17+
};
18+
19+
static mut RX: Option<Rx<USART1>> = None;
20+
static mut TX: Option<Tx<USART1>> = None;
21+
#[entry]
22+
fn main() -> ! {
23+
// Get access to the device specific peripherals from the peripheral access crate
24+
let p = pac::Peripherals::take().unwrap();
25+
26+
// Take ownership over the raw flash and rcc devices and convert them into the corresponding
27+
// HAL structs
28+
let mut flash = p.FLASH.constrain();
29+
let rcc = p.RCC.constrain();
30+
31+
// Freeze the configuration of all the clocks in the system and store the frozen frequencies in
32+
// `clocks`
33+
let clocks = rcc.cfgr.freeze(&mut flash.acr);
34+
35+
// Prepare the alternate function I/O registers
36+
let mut afio = p.AFIO.constrain();
37+
38+
// Prepare the GPIOB peripheral
39+
let mut gpiob = p.GPIOB.split();
40+
41+
// USART1
42+
let tx = gpiob.pb6.into_alternate_push_pull(&mut gpiob.crl);
43+
let rx = gpiob.pb7;
44+
45+
// Set up the usart device. Taks ownership over the USART register and tx/rx pins. The rest of
46+
// the registers are used to enable and configure the device.
47+
let (mut tx, mut rx) = Serial::usart1(
48+
p.USART1,
49+
(tx, rx),
50+
&mut afio.mapr,
51+
Config::default().baudrate(115200.bps()),
52+
clocks,
53+
)
54+
.split();
55+
tx.listen();
56+
rx.listen();
57+
rx.listen_idle();
58+
59+
cortex_m::interrupt::free(|_| unsafe {
60+
TX.replace(tx);
61+
RX.replace(rx);
62+
});
63+
unsafe {
64+
cortex_m::peripheral::NVIC::unmask(pac::Interrupt::USART1);
65+
}
66+
67+
loop {
68+
cortex_m::asm::wfi()
69+
}
70+
}
71+
const BUFFER_LEN: usize = 4096;
72+
static mut BUFFER: &mut [u8; BUFFER_LEN] = &mut [0; BUFFER_LEN];
73+
static mut WIDX: usize = 0;
74+
75+
unsafe fn write(buf: &[u8]) {
76+
if let Some(tx) = TX.as_mut() {
77+
buf.iter()
78+
.for_each(|w| if let Err(_err) = nb::block!(tx.write(*w)) {})
79+
}
80+
}
81+
#[interrupt]
82+
unsafe fn USART1() {
83+
cortex_m::interrupt::free(|_| {
84+
if let Some(rx) = RX.as_mut() {
85+
if rx.is_rxne() {
86+
if let Ok(w) = nb::block!(rx.read()) {
87+
BUFFER[WIDX] = w;
88+
WIDX += 1;
89+
if WIDX >= BUFFER_LEN - 1 {
90+
write(&BUFFER[..]);
91+
WIDX = 0;
92+
}
93+
}
94+
rx.listen_idle();
95+
} else if rx.is_idle() {
96+
rx.unlisten_idle();
97+
write(&BUFFER[0..WIDX]);
98+
WIDX = 0;
99+
}
100+
}
101+
})
102+
}

src/serial.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -344,17 +344,17 @@ where
344344

345345
/// Return true if the line idle status is set
346346
pub fn is_idle(&self) -> bool {
347-
self.usart.sr.read().idle().bit_is_set();
347+
self.usart.sr.read().idle().bit_is_set()
348348
}
349349

350350
/// Return true if the tx register is empty (and can accept data)
351351
pub fn is_txe(&self) -> bool {
352-
self.usart.sr.read().txe().bit_is_set();
352+
self.usart.sr.read().txe().bit_is_set()
353353
}
354354

355355
/// Return true if the rx register is not empty (and can be read)
356356
pub fn is_rxne(&self) -> bool {
357-
self.usart.sr.read().rxne().bit_is_set();
357+
self.usart.sr.read().rxne().bit_is_set()
358358
}
359359

360360
/// Returns ownership of the borrowed register handles

0 commit comments

Comments
 (0)