Skip to content
Snippets Groups Projects
Commit bdd67760 authored by Per Lindgren's avatar Per Lindgren
Browse files

rtfm-serial-echo

parent c91369f8
Branches
No related tags found
No related merge requests found
//! Serial interface echo server (reactive version)
//!
//! This is a reactive version of the `serial-echo` example. Here the processor sleeps most of the
//! time and only wakes up to sent back received bytes.
//!
//! This example uses the [Real Time For the Masses framework](https://docs.rs/cortex-m-rtfm/~0.3)
#![deny(unsafe_code)]
#![deny(warnings)]
#![feature(proc_macro)]
#![no_std]
extern crate cortex_m_rtfm as rtfm;
extern crate stm32f4x_hal as hal;
use hal::prelude::*;
use hal::serial::{Event, Rx, Serial, Tx};
use hal::stm32f4x;
use rtfm::{app, Threshold};
app! {
device: stm32f4x,
resources: {
static TX: Tx<stm32f4x::USART2>;
static RX: Rx<stm32f4x::USART2>;
},
tasks: {
USART2: {
path: echo,
resources: [TX, RX],
}
},
}
fn init(p: init::Peripherals) -> init::LateResources {
let mut flash = p.device.FLASH.constrain();
let mut rcc = p.device.RCC.constrain();
let mut gpioa = p.device.GPIOA.split(&mut rcc.ahb1);
let clocks = rcc.cfgr.freeze(&mut flash.acr);
let tx = gpioa.pa2.into_af7(&mut gpioa.moder, &mut gpioa.afrl);
let rx = gpioa.pa3.into_af7(&mut gpioa.moder, &mut gpioa.afrl);
let mut serial = Serial::usart2(
p.device.USART2,
(tx, rx),
115_200.bps(),
clocks,
&mut rcc.apb1,
);
serial.listen(Event::Rxne);
let (tx, rx) = serial.split();
init::LateResources { TX: tx, RX: rx }
}
fn idle() -> ! {
// sleep
loop {
// rtfm::wfi();
}
}
fn echo(_: &mut Threshold, mut r: USART2::Resources) {
let byte = r.RX.read().unwrap();
r.TX.write(byte).unwrap();
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment