embassy/embassy-nrf-examples/src/bin/uart.rs

117 lines
3.5 KiB
Rust
Raw Normal View History

2020-09-22 18:03:43 +02:00
#![no_std]
#![no_main]
#![feature(type_alias_impl_trait)]
#[path = "../example_common.rs"]
mod example_common;
use example_common::*;
use cortex_m_rt::entry;
2020-12-29 01:53:17 +01:00
use defmt::panic;
use embassy::executor::{task, Executor};
2020-12-23 16:18:29 +01:00
use embassy::time::{Duration, Timer};
2021-01-02 19:59:37 +01:00
use embassy::uart::Uart;
2020-10-31 22:37:24 +01:00
use embassy::util::Forever;
2020-12-23 16:18:29 +01:00
use embassy_nrf::{interrupt, pac, rtc, uarte};
use futures::future::{select, Either};
use nrf52840_hal::clocks;
use nrf52840_hal::gpio;
2020-09-24 22:04:45 +02:00
#[task]
async fn run(uart: pac::UARTE0, port: pac::P0) {
// Init UART
let port0 = gpio::p0::Parts::new(port);
let pins = uarte::Pins {
rxd: port0.p0_08.into_floating_input().degrade(),
txd: port0
.p0_06
.into_push_pull_output(gpio::Level::Low)
.degrade(),
cts: None,
rts: None,
};
// NOTE(unsafe): Safe becasue we do not use `mem::forget` anywhere.
let mut uart = unsafe {
uarte::Uarte::new(
uart,
interrupt::take!(UARTE0_UART0),
pins,
uarte::Parity::EXCLUDED,
uarte::Baudrate::BAUD115200,
)
};
2020-09-22 18:03:43 +02:00
info!("uarte initialized!");
2020-12-23 16:18:29 +01:00
// Message must be in SRAM
let mut buf = [0; 8];
buf.copy_from_slice(b"Hello!\r\n");
2021-01-02 19:59:37 +01:00
unwrap!(uart.send(&buf).await);
2020-09-22 18:03:43 +02:00
info!("wrote hello in uart!");
loop {
2021-01-02 19:14:54 +01:00
let buf_len = buf.len();
2021-01-01 23:04:18 +01:00
info!("reading...");
// `receive()` doesn't return until the buffer has been completely filled with
// incoming data, which in this case is 8 bytes.
//
// This example shows how to use `select` to run an uart receive concurrently with a
// 1 second timer, effectively adding a timeout to the receive operation.
let recv_fut = uart.receive(&mut buf);
let timer_fut = Timer::after(Duration::from_millis(1000));
2021-01-02 19:14:54 +01:00
let received_len = match select(recv_fut, timer_fut).await {
2021-01-01 23:04:18 +01:00
// recv_fut completed first, so we've received `buf_len` bytes.
2021-01-02 19:14:54 +01:00
Either::Left(_) => buf_len,
2021-01-01 23:04:18 +01:00
// timer_fut completed first. `select` gives us back the future that didn't complete, which
// is `recv_fut` in this case, so we can do further stuff with it.
//
// The recv_fut would stop the uart read automatically when dropped. However, we want to know how
// many bytes have been received, so we have to "gracefully stop" it with `.stop()`.
2021-01-02 19:14:54 +01:00
Either::Right((_, recv_fut)) => recv_fut.stop().await,
2020-12-23 16:18:29 +01:00
};
2021-01-02 19:14:54 +01:00
let received = &mut buf[..received_len];
2020-12-23 16:18:29 +01:00
2021-02-14 01:41:36 +01:00
if !received.is_empty() {
2020-12-23 16:18:29 +01:00
info!("read done, got {:[u8]}", received);
// Echo back received data
2021-01-02 19:59:37 +01:00
unwrap!(uart.send(received).await);
2020-09-22 18:03:43 +02:00
}
}
}
2020-12-23 16:18:29 +01:00
static RTC: Forever<rtc::RTC<pac::RTC1>> = Forever::new();
static ALARM: Forever<rtc::Alarm<pac::RTC1>> = Forever::new();
2020-10-31 22:37:24 +01:00
static EXECUTOR: Forever<Executor> = Forever::new();
2020-09-22 18:03:43 +02:00
#[entry]
fn main() -> ! {
info!("Hello World!");
2020-12-23 16:18:29 +01:00
let p = unwrap!(embassy_nrf::pac::Peripherals::take());
clocks::Clocks::new(p.CLOCK)
.enable_ext_hfosc()
.set_lfclk_src_external(clocks::LfOscConfiguration::NoExternalNoBypass)
.start_lfclk();
let rtc = RTC.put(rtc::RTC::new(p.RTC1, interrupt::take!(RTC1)));
rtc.start();
unsafe { embassy::time::set_clock(rtc) };
let alarm = ALARM.put(rtc.alarm0());
let executor = EXECUTOR.put(Executor::new());
executor.set_alarm(alarm);
let uarte0 = p.UARTE0;
let p0 = p.P0;
executor.run(|spawner| {
unwrap!(spawner.spawn(run(uarte0, p0)));
});
2020-09-22 18:03:43 +02:00
}