2020-09-22 16:03:43 +00:00
|
|
|
#![no_std]
|
|
|
|
#![no_main]
|
2021-03-17 01:48:16 +00:00
|
|
|
#![feature(min_type_alias_impl_trait)]
|
|
|
|
#![feature(impl_trait_in_bindings)]
|
2020-09-22 16:03:43 +00:00
|
|
|
#![feature(type_alias_impl_trait)]
|
2021-03-27 02:12:58 +00:00
|
|
|
#![allow(incomplete_features)]
|
2020-09-22 16:03:43 +00:00
|
|
|
|
|
|
|
#[path = "../example_common.rs"]
|
|
|
|
mod example_common;
|
|
|
|
use example_common::*;
|
|
|
|
|
|
|
|
use cortex_m_rt::entry;
|
2020-12-29 00:53:17 +00:00
|
|
|
use defmt::panic;
|
2020-10-19 19:15:24 +00:00
|
|
|
use embassy::executor::{task, Executor};
|
2021-03-22 00:15:44 +00:00
|
|
|
use embassy::traits::uart::{Read, Write};
|
2021-03-27 02:12:58 +00:00
|
|
|
use embassy::util::{Forever, Steal};
|
2021-03-28 22:42:08 +00:00
|
|
|
use embassy_nrf::gpio::NoPin;
|
2021-03-27 02:12:58 +00:00
|
|
|
use embassy_nrf::{interrupt, peripherals, rtc, uarte, Peripherals};
|
2021-03-22 00:15:44 +00:00
|
|
|
use futures::pin_mut;
|
2020-09-24 20:04:45 +00:00
|
|
|
|
|
|
|
#[task]
|
2021-03-22 00:15:44 +00:00
|
|
|
async fn run() {
|
2021-03-27 02:12:58 +00:00
|
|
|
let p = unsafe { Peripherals::steal() };
|
2021-03-22 00:15:44 +00:00
|
|
|
|
|
|
|
let mut config = uarte::Config::default();
|
|
|
|
config.parity = uarte::Parity::EXCLUDED;
|
|
|
|
config.baudrate = uarte::Baudrate::BAUD115200;
|
|
|
|
|
|
|
|
let irq = interrupt::take!(UARTE0_UART0);
|
2021-03-27 02:12:58 +00:00
|
|
|
let uart = unsafe { uarte::Uarte::new(p.UARTE0, irq, p.P0_08, p.P0_06, NoPin, NoPin, config) };
|
2021-03-22 00:15:44 +00:00
|
|
|
pin_mut!(uart);
|
2021-02-02 04:14:52 +00:00
|
|
|
|
2020-09-22 16:03:43 +00:00
|
|
|
info!("uarte initialized!");
|
|
|
|
|
2020-12-23 15:18:29 +00:00
|
|
|
// Message must be in SRAM
|
|
|
|
let mut buf = [0; 8];
|
|
|
|
buf.copy_from_slice(b"Hello!\r\n");
|
|
|
|
|
2021-03-22 00:15:44 +00:00
|
|
|
unwrap!(uart.as_mut().write(&buf).await);
|
2020-09-22 16:03:43 +00:00
|
|
|
info!("wrote hello in uart!");
|
|
|
|
|
|
|
|
loop {
|
2021-01-01 22:04:18 +00:00
|
|
|
info!("reading...");
|
2021-03-22 00:15:44 +00:00
|
|
|
unwrap!(uart.as_mut().read(&mut buf).await);
|
|
|
|
info!("writing...");
|
|
|
|
unwrap!(uart.as_mut().write(&buf).await);
|
2021-01-01 22:04:18 +00:00
|
|
|
|
2021-03-22 00:15:44 +00:00
|
|
|
/*
|
2021-01-01 22:04:18 +00:00
|
|
|
// `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.
|
2021-03-22 00:15:44 +00:00
|
|
|
let recv_fut = uart.read(&mut buf);
|
2021-01-01 22:04:18 +00:00
|
|
|
let timer_fut = Timer::after(Duration::from_millis(1000));
|
2021-01-02 18:14:54 +00:00
|
|
|
let received_len = match select(recv_fut, timer_fut).await {
|
2021-01-01 22:04:18 +00:00
|
|
|
// recv_fut completed first, so we've received `buf_len` bytes.
|
2021-01-02 18:14:54 +00:00
|
|
|
Either::Left(_) => buf_len,
|
2021-01-01 22:04:18 +00: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 18:14:54 +00:00
|
|
|
Either::Right((_, recv_fut)) => recv_fut.stop().await,
|
2020-12-23 15:18:29 +00:00
|
|
|
};
|
2021-01-02 18:14:54 +00:00
|
|
|
let received = &mut buf[..received_len];
|
2020-12-23 15:18:29 +00:00
|
|
|
|
2021-02-14 00:41:36 +00:00
|
|
|
if !received.is_empty() {
|
2021-02-24 07:57:06 +00:00
|
|
|
info!("read done, got {}", received);
|
2020-12-23 15:18:29 +00:00
|
|
|
|
|
|
|
// Echo back received data
|
2021-03-22 00:15:44 +00:00
|
|
|
unwrap!(uart.write(received).await);
|
2020-09-22 16:03:43 +00:00
|
|
|
}
|
2021-03-22 00:15:44 +00:00
|
|
|
*/
|
2020-09-22 16:03:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-27 02:12:58 +00:00
|
|
|
static RTC: Forever<rtc::RTC<peripherals::RTC1>> = Forever::new();
|
|
|
|
static ALARM: Forever<rtc::Alarm<peripherals::RTC1>> = Forever::new();
|
2020-10-31 21:37:24 +00:00
|
|
|
static EXECUTOR: Forever<Executor> = Forever::new();
|
|
|
|
|
2020-09-22 16:03:43 +00:00
|
|
|
#[entry]
|
|
|
|
fn main() -> ! {
|
|
|
|
info!("Hello World!");
|
|
|
|
|
2021-03-27 02:12:58 +00:00
|
|
|
let p = unwrap!(embassy_nrf::Peripherals::take());
|
2020-12-23 15:18:29 +00:00
|
|
|
|
2021-03-28 22:42:08 +00:00
|
|
|
unsafe { embassy_nrf::system::configure(Default::default()) };
|
2020-12-23 15:18:29 +00:00
|
|
|
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());
|
2021-02-02 04:14:52 +00:00
|
|
|
let executor = EXECUTOR.put(Executor::new());
|
|
|
|
executor.set_alarm(alarm);
|
|
|
|
|
|
|
|
executor.run(|spawner| {
|
2021-03-22 00:15:44 +00:00
|
|
|
unwrap!(spawner.spawn(run()));
|
2021-02-02 04:14:52 +00:00
|
|
|
});
|
2020-09-22 16:03:43 +00:00
|
|
|
}
|