Rename examples -> embassy-nrf-examples
This commit is contained in:
84
embassy-nrf-examples/src/bin/buffered_uart.rs
Normal file
84
embassy-nrf-examples/src/bin/buffered_uart.rs
Normal file
@ -0,0 +1,84 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
|
||||
#[path = "../example_common.rs"]
|
||||
mod example_common;
|
||||
use example_common::*;
|
||||
|
||||
use cortex_m_rt::entry;
|
||||
use defmt::panic;
|
||||
use futures::pin_mut;
|
||||
use nrf52840_hal::gpio;
|
||||
|
||||
use embassy::executor::{task, Executor};
|
||||
use embassy::io::{AsyncBufRead, AsyncBufReadExt, AsyncWrite, AsyncWriteExt};
|
||||
use embassy::util::Forever;
|
||||
use embassy_nrf::buffered_uarte;
|
||||
use embassy_nrf::interrupt;
|
||||
|
||||
#[task]
|
||||
async fn run() {
|
||||
let p = unwrap!(embassy_nrf::pac::Peripherals::take());
|
||||
|
||||
let port0 = gpio::p0::Parts::new(p.P0);
|
||||
|
||||
let pins = buffered_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,
|
||||
};
|
||||
|
||||
let irq = interrupt::take!(UARTE0_UART0);
|
||||
let u = buffered_uarte::BufferedUarte::new(
|
||||
p.UARTE0,
|
||||
irq,
|
||||
pins,
|
||||
buffered_uarte::Parity::EXCLUDED,
|
||||
buffered_uarte::Baudrate::BAUD115200,
|
||||
);
|
||||
pin_mut!(u);
|
||||
|
||||
info!("uarte initialized!");
|
||||
|
||||
unwrap!(u.write_all(b"Hello!\r\n").await);
|
||||
info!("wrote hello in uart!");
|
||||
|
||||
// Simple demo, reading 8-char chunks and echoing them back reversed.
|
||||
loop {
|
||||
info!("reading...");
|
||||
let mut buf = [0u8; 8];
|
||||
unwrap!(u.read_exact(&mut buf).await);
|
||||
info!("read done, got {:[u8]}", buf);
|
||||
|
||||
// Reverse buf
|
||||
for i in 0..4 {
|
||||
let tmp = buf[i];
|
||||
buf[i] = buf[7 - i];
|
||||
buf[7 - i] = tmp;
|
||||
}
|
||||
|
||||
info!("writing...");
|
||||
unwrap!(u.write_all(&buf).await);
|
||||
info!("write done");
|
||||
}
|
||||
}
|
||||
|
||||
static EXECUTOR: Forever<Executor> = Forever::new();
|
||||
|
||||
#[entry]
|
||||
fn main() -> ! {
|
||||
info!("Hello World!");
|
||||
|
||||
let executor = EXECUTOR.put(Executor::new(cortex_m::asm::sev));
|
||||
unwrap!(executor.spawn(run()));
|
||||
|
||||
loop {
|
||||
executor.run();
|
||||
cortex_m::asm::wfe();
|
||||
}
|
||||
}
|
74
embassy-nrf-examples/src/bin/executor_fairness_test.rs
Normal file
74
embassy-nrf-examples/src/bin/executor_fairness_test.rs
Normal file
@ -0,0 +1,74 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
|
||||
#[path = "../example_common.rs"]
|
||||
mod example_common;
|
||||
use example_common::*;
|
||||
|
||||
use core::task::Poll;
|
||||
use cortex_m_rt::entry;
|
||||
use defmt::panic;
|
||||
use embassy::executor::{task, Executor};
|
||||
use embassy::time::{Duration, Instant, Timer};
|
||||
use embassy::util::Forever;
|
||||
use embassy_nrf::pac;
|
||||
use embassy_nrf::{interrupt, rtc};
|
||||
use nrf52840_hal::clocks;
|
||||
|
||||
#[task]
|
||||
async fn run1() {
|
||||
loop {
|
||||
info!("DING DONG");
|
||||
Timer::after(Duration::from_ticks(16000)).await;
|
||||
}
|
||||
}
|
||||
|
||||
#[task]
|
||||
async fn run2() {
|
||||
loop {
|
||||
Timer::at(Instant::from_ticks(0)).await;
|
||||
}
|
||||
}
|
||||
|
||||
#[task]
|
||||
async fn run3() {
|
||||
futures::future::poll_fn(|cx| {
|
||||
cx.waker().wake_by_ref();
|
||||
Poll::<()>::Pending
|
||||
})
|
||||
.await;
|
||||
}
|
||||
|
||||
static RTC: Forever<rtc::RTC<pac::RTC1>> = Forever::new();
|
||||
static ALARM: Forever<rtc::Alarm<pac::RTC1>> = Forever::new();
|
||||
static EXECUTOR: Forever<Executor> = Forever::new();
|
||||
|
||||
#[entry]
|
||||
fn main() -> ! {
|
||||
info!("Hello World!");
|
||||
|
||||
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_with_alarm(alarm, cortex_m::asm::sev));
|
||||
|
||||
unwrap!(executor.spawn(run1()));
|
||||
unwrap!(executor.spawn(run2()));
|
||||
unwrap!(executor.spawn(run3()));
|
||||
|
||||
loop {
|
||||
executor.run();
|
||||
cortex_m::asm::wfe();
|
||||
}
|
||||
}
|
83
embassy-nrf-examples/src/bin/gpiote.rs
Normal file
83
embassy-nrf-examples/src/bin/gpiote.rs
Normal file
@ -0,0 +1,83 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
|
||||
#[path = "../example_common.rs"]
|
||||
mod example_common;
|
||||
use example_common::*;
|
||||
|
||||
use cortex_m_rt::entry;
|
||||
use defmt::panic;
|
||||
use nrf52840_hal::gpio;
|
||||
|
||||
use embassy::executor::{task, Executor};
|
||||
use embassy::util::Forever;
|
||||
use embassy_nrf::gpiote;
|
||||
use embassy_nrf::interrupt;
|
||||
|
||||
#[task]
|
||||
async fn run() {
|
||||
let p = unwrap!(embassy_nrf::pac::Peripherals::take());
|
||||
let port0 = gpio::p0::Parts::new(p.P0);
|
||||
|
||||
let g = gpiote::Gpiote::new(p.GPIOTE, interrupt::take!(GPIOTE));
|
||||
|
||||
info!("Starting!");
|
||||
|
||||
let pin1 = port0.p0_11.into_pullup_input().degrade();
|
||||
let button1 = async {
|
||||
let ch = unwrap!(g.new_input_channel(pin1, gpiote::InputChannelPolarity::HiToLo));
|
||||
|
||||
loop {
|
||||
ch.wait().await;
|
||||
info!("Button 1 pressed")
|
||||
}
|
||||
};
|
||||
|
||||
let pin2 = port0.p0_12.into_pullup_input().degrade();
|
||||
let button2 = async {
|
||||
let ch = unwrap!(g.new_input_channel(pin2, gpiote::InputChannelPolarity::LoToHi));
|
||||
|
||||
loop {
|
||||
ch.wait().await;
|
||||
info!("Button 2 released")
|
||||
}
|
||||
};
|
||||
|
||||
let pin3 = port0.p0_24.into_pullup_input().degrade();
|
||||
let button3 = async {
|
||||
let ch = unwrap!(g.new_input_channel(pin3, gpiote::InputChannelPolarity::Toggle));
|
||||
|
||||
loop {
|
||||
ch.wait().await;
|
||||
info!("Button 3 toggled")
|
||||
}
|
||||
};
|
||||
|
||||
let pin4 = port0.p0_25.into_pullup_input().degrade();
|
||||
let button4 = async {
|
||||
let ch = unwrap!(g.new_input_channel(pin4, gpiote::InputChannelPolarity::Toggle));
|
||||
|
||||
loop {
|
||||
ch.wait().await;
|
||||
info!("Button 4 toggled")
|
||||
}
|
||||
};
|
||||
|
||||
futures::join!(button1, button2, button3, button4);
|
||||
}
|
||||
|
||||
static EXECUTOR: Forever<Executor> = Forever::new();
|
||||
|
||||
#[entry]
|
||||
fn main() -> ! {
|
||||
info!("Hello World!");
|
||||
|
||||
let executor = EXECUTOR.put(Executor::new(cortex_m::asm::sev));
|
||||
unwrap!(executor.spawn(run()));
|
||||
|
||||
loop {
|
||||
executor.run();
|
||||
cortex_m::asm::wfe();
|
||||
}
|
||||
}
|
62
embassy-nrf-examples/src/bin/gpiote_port.rs
Normal file
62
embassy-nrf-examples/src/bin/gpiote_port.rs
Normal file
@ -0,0 +1,62 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
|
||||
#[path = "../example_common.rs"]
|
||||
mod example_common;
|
||||
use example_common::*;
|
||||
|
||||
use core::mem;
|
||||
use cortex_m_rt::entry;
|
||||
use defmt::panic;
|
||||
use nrf52840_hal::gpio;
|
||||
|
||||
use embassy::executor::{task, Executor};
|
||||
use embassy::util::Forever;
|
||||
use embassy_nrf::gpiote::{Gpiote, PortInputPolarity};
|
||||
use embassy_nrf::interrupt;
|
||||
|
||||
async fn button(g: &Gpiote, n: usize, pin: gpio::Pin<gpio::Input<gpio::PullUp>>) {
|
||||
loop {
|
||||
g.wait_port_input(&pin, PortInputPolarity::Low).await;
|
||||
info!("Button {:?} pressed!", n);
|
||||
g.wait_port_input(&pin, PortInputPolarity::High).await;
|
||||
info!("Button {:?} released!", n);
|
||||
}
|
||||
}
|
||||
|
||||
#[task]
|
||||
async fn run() {
|
||||
let p = unwrap!(embassy_nrf::pac::Peripherals::take());
|
||||
let port0 = gpio::p0::Parts::new(p.P0);
|
||||
|
||||
let g = Gpiote::new(p.GPIOTE, interrupt::take!(GPIOTE));
|
||||
info!(
|
||||
"sizeof Signal<()> = {:usize}",
|
||||
mem::size_of::<embassy::util::Signal<()>>()
|
||||
);
|
||||
info!("sizeof gpiote = {:usize}", mem::size_of::<Gpiote>());
|
||||
|
||||
info!("Starting!");
|
||||
|
||||
let button1 = button(&g, 1, port0.p0_11.into_pullup_input().degrade());
|
||||
let button2 = button(&g, 2, port0.p0_12.into_pullup_input().degrade());
|
||||
let button3 = button(&g, 3, port0.p0_24.into_pullup_input().degrade());
|
||||
let button4 = button(&g, 4, port0.p0_25.into_pullup_input().degrade());
|
||||
futures::join!(button1, button2, button3, button4);
|
||||
}
|
||||
|
||||
static EXECUTOR: Forever<Executor> = Forever::new();
|
||||
|
||||
#[entry]
|
||||
fn main() -> ! {
|
||||
info!("Hello World!");
|
||||
|
||||
let executor = EXECUTOR.put(Executor::new(cortex_m::asm::sev));
|
||||
unwrap!(executor.spawn(run()));
|
||||
|
||||
loop {
|
||||
executor.run();
|
||||
cortex_m::asm::wfe();
|
||||
}
|
||||
}
|
176
embassy-nrf-examples/src/bin/multiprio.rs
Normal file
176
embassy-nrf-examples/src/bin/multiprio.rs
Normal file
@ -0,0 +1,176 @@
|
||||
//! This example showcases how to create multiple Executor instances to run tasks at
|
||||
//! different priority levels.
|
||||
//!
|
||||
//! Low priority executor runs in thread mode (not interrupt), and uses `sev` for signaling
|
||||
//! there's work in the queue, and `wfe` for waiting for work.
|
||||
//!
|
||||
//! Medium and high priority executors run in two interrupts with different priorities.
|
||||
//! Signaling work is done by pending the interrupt. No "waiting" needs to be done explicitly, since
|
||||
//! when there's work the interrupt will trigger and run the executor.
|
||||
//!
|
||||
//! Sample output below. Note that high priority ticks can interrupt everything else, and
|
||||
//! medium priority computations can interrupt low priority computations, making them to appear
|
||||
//! to take significantly longer time.
|
||||
//!
|
||||
//! ```not_rust
|
||||
//! [med] Starting long computation
|
||||
//! [med] done in 992 ms
|
||||
//! [high] tick!
|
||||
//! [low] Starting long computation
|
||||
//! [med] Starting long computation
|
||||
//! [high] tick!
|
||||
//! [high] tick!
|
||||
//! [med] done in 993 ms
|
||||
//! [med] Starting long computation
|
||||
//! [high] tick!
|
||||
//! [high] tick!
|
||||
//! [med] done in 993 ms
|
||||
//! [low] done in 3972 ms
|
||||
//! [med] Starting long computation
|
||||
//! [high] tick!
|
||||
//! [high] tick!
|
||||
//! [med] done in 993 ms
|
||||
//! ```
|
||||
//!
|
||||
//! For comparison, try changing the code so all 3 tasks get spawned on the low priority executor.
|
||||
//! You will get an output like the following. Note that no computation is ever interrupted.
|
||||
//!
|
||||
//! ```not_rust
|
||||
//! [high] tick!
|
||||
//! [med] Starting long computation
|
||||
//! [med] done in 496 ms
|
||||
//! [low] Starting long computation
|
||||
//! [low] done in 992 ms
|
||||
//! [med] Starting long computation
|
||||
//! [med] done in 496 ms
|
||||
//! [high] tick!
|
||||
//! [low] Starting long computation
|
||||
//! [low] done in 992 ms
|
||||
//! [high] tick!
|
||||
//! [med] Starting long computation
|
||||
//! [med] done in 496 ms
|
||||
//! [high] tick!
|
||||
//! ```
|
||||
//!
|
||||
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
|
||||
#[path = "../example_common.rs"]
|
||||
mod example_common;
|
||||
use example_common::*;
|
||||
|
||||
use cortex_m::peripheral::NVIC;
|
||||
use cortex_m_rt::entry;
|
||||
use defmt::panic;
|
||||
use nrf52840_hal::clocks;
|
||||
|
||||
use embassy::executor::{task, Executor};
|
||||
use embassy::time::{Duration, Instant, Timer};
|
||||
use embassy::util::Forever;
|
||||
use embassy_nrf::{interrupt, pac, rtc};
|
||||
|
||||
#[task]
|
||||
async fn run_high() {
|
||||
loop {
|
||||
info!(" [high] tick!");
|
||||
Timer::after(Duration::from_ticks(27374)).await;
|
||||
}
|
||||
}
|
||||
|
||||
#[task]
|
||||
async fn run_med() {
|
||||
loop {
|
||||
let start = Instant::now();
|
||||
info!(" [med] Starting long computation");
|
||||
|
||||
// Spin-wait to simulate a long CPU computation
|
||||
cortex_m::asm::delay(32_000_000); // ~1 second
|
||||
|
||||
let end = Instant::now();
|
||||
let ms = end.duration_since(start).as_ticks() / 33;
|
||||
info!(" [med] done in {:u64} ms", ms);
|
||||
|
||||
Timer::after(Duration::from_ticks(23421)).await;
|
||||
}
|
||||
}
|
||||
|
||||
#[task]
|
||||
async fn run_low() {
|
||||
loop {
|
||||
let start = Instant::now();
|
||||
info!("[low] Starting long computation");
|
||||
|
||||
// Spin-wait to simulate a long CPU computation
|
||||
cortex_m::asm::delay(64_000_000); // ~2 seconds
|
||||
|
||||
let end = Instant::now();
|
||||
let ms = end.duration_since(start).as_ticks() / 33;
|
||||
info!("[low] done in {:u64} ms", ms);
|
||||
|
||||
Timer::after(Duration::from_ticks(32983)).await;
|
||||
}
|
||||
}
|
||||
|
||||
static RTC: Forever<rtc::RTC<pac::RTC1>> = Forever::new();
|
||||
static ALARM_LOW: Forever<rtc::Alarm<pac::RTC1>> = Forever::new();
|
||||
static EXECUTOR_LOW: Forever<Executor> = Forever::new();
|
||||
static ALARM_MED: Forever<rtc::Alarm<pac::RTC1>> = Forever::new();
|
||||
static EXECUTOR_MED: Forever<Executor> = Forever::new();
|
||||
static ALARM_HIGH: Forever<rtc::Alarm<pac::RTC1>> = Forever::new();
|
||||
static EXECUTOR_HIGH: Forever<Executor> = Forever::new();
|
||||
|
||||
#[entry]
|
||||
fn main() -> ! {
|
||||
info!("Hello World!");
|
||||
|
||||
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_low = ALARM_LOW.put(rtc.alarm0());
|
||||
let executor_low = EXECUTOR_LOW.put(Executor::new_with_alarm(alarm_low, cortex_m::asm::sev));
|
||||
let alarm_med = ALARM_MED.put(rtc.alarm1());
|
||||
let executor_med = EXECUTOR_MED.put(Executor::new_with_alarm(alarm_med, || {
|
||||
NVIC::pend(interrupt::SWI0_EGU0)
|
||||
}));
|
||||
let alarm_high = ALARM_HIGH.put(rtc.alarm2());
|
||||
let executor_high = EXECUTOR_HIGH.put(Executor::new_with_alarm(alarm_high, || {
|
||||
NVIC::pend(interrupt::SWI1_EGU1)
|
||||
}));
|
||||
|
||||
unsafe {
|
||||
let mut nvic: NVIC = core::mem::transmute(());
|
||||
nvic.set_priority(interrupt::SWI0_EGU0, 7 << 5);
|
||||
nvic.set_priority(interrupt::SWI1_EGU1, 6 << 5);
|
||||
NVIC::unmask(interrupt::SWI0_EGU0);
|
||||
NVIC::unmask(interrupt::SWI1_EGU1);
|
||||
}
|
||||
|
||||
unwrap!(executor_low.spawn(run_low()));
|
||||
unwrap!(executor_med.spawn(run_med()));
|
||||
unwrap!(executor_high.spawn(run_high()));
|
||||
|
||||
loop {
|
||||
executor_low.run();
|
||||
cortex_m::asm::wfe();
|
||||
}
|
||||
}
|
||||
|
||||
#[interrupt]
|
||||
unsafe fn SWI0_EGU0() {
|
||||
EXECUTOR_MED.steal().run()
|
||||
}
|
||||
|
||||
#[interrupt]
|
||||
unsafe fn SWI1_EGU1() {
|
||||
EXECUTOR_HIGH.steal().run()
|
||||
}
|
134
embassy-nrf-examples/src/bin/qspi.rs
Normal file
134
embassy-nrf-examples/src/bin/qspi.rs
Normal file
@ -0,0 +1,134 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
|
||||
#[path = "../example_common.rs"]
|
||||
mod example_common;
|
||||
use example_common::*;
|
||||
|
||||
use cortex_m_rt::entry;
|
||||
use defmt::{assert_eq, panic, *};
|
||||
use nrf52840_hal::gpio;
|
||||
|
||||
use embassy::executor::{task, Executor};
|
||||
use embassy::flash::Flash;
|
||||
use embassy::util::Forever;
|
||||
use embassy_nrf::{interrupt, qspi};
|
||||
|
||||
const PAGE_SIZE: usize = 4096;
|
||||
|
||||
// Workaround for alignment requirements.
|
||||
// Nicer API will probably come in the future.
|
||||
#[repr(C, align(4))]
|
||||
struct AlignedBuf([u8; 4096]);
|
||||
|
||||
#[task]
|
||||
async fn run() {
|
||||
let p = unwrap!(embassy_nrf::pac::Peripherals::take());
|
||||
|
||||
let port0 = gpio::p0::Parts::new(p.P0);
|
||||
|
||||
let pins = qspi::Pins {
|
||||
csn: port0
|
||||
.p0_17
|
||||
.into_push_pull_output(gpio::Level::High)
|
||||
.degrade(),
|
||||
sck: port0
|
||||
.p0_19
|
||||
.into_push_pull_output(gpio::Level::High)
|
||||
.degrade(),
|
||||
io0: port0
|
||||
.p0_20
|
||||
.into_push_pull_output(gpio::Level::High)
|
||||
.degrade(),
|
||||
io1: port0
|
||||
.p0_21
|
||||
.into_push_pull_output(gpio::Level::High)
|
||||
.degrade(),
|
||||
io2: Some(
|
||||
port0
|
||||
.p0_22
|
||||
.into_push_pull_output(gpio::Level::High)
|
||||
.degrade(),
|
||||
),
|
||||
io3: Some(
|
||||
port0
|
||||
.p0_23
|
||||
.into_push_pull_output(gpio::Level::High)
|
||||
.degrade(),
|
||||
),
|
||||
};
|
||||
|
||||
let config = qspi::Config {
|
||||
pins,
|
||||
read_opcode: qspi::ReadOpcode::READ4IO,
|
||||
write_opcode: qspi::WriteOpcode::PP4IO,
|
||||
xip_offset: 0,
|
||||
write_page_size: qspi::WritePageSize::_256BYTES,
|
||||
deep_power_down: None,
|
||||
};
|
||||
|
||||
let irq = interrupt::take!(QSPI);
|
||||
let mut q = qspi::Qspi::new(p.QSPI, irq, config);
|
||||
|
||||
let mut id = [1; 3];
|
||||
q.custom_instruction(0x9F, &[], &mut id).await.unwrap();
|
||||
info!("id: {:[u8]}", id);
|
||||
|
||||
// Read status register
|
||||
let mut status = [0; 1];
|
||||
q.custom_instruction(0x05, &[], &mut status).await.unwrap();
|
||||
|
||||
info!("status: {:?}", status[0]);
|
||||
|
||||
if status[0] & 0x40 == 0 {
|
||||
status[0] |= 0x40;
|
||||
|
||||
q.custom_instruction(0x01, &status, &mut []).await.unwrap();
|
||||
|
||||
info!("enabled quad in status");
|
||||
}
|
||||
|
||||
let mut buf = AlignedBuf([0u8; PAGE_SIZE]);
|
||||
|
||||
let pattern = |a: u32| (a ^ (a >> 8) ^ (a >> 16) ^ (a >> 24)) as u8;
|
||||
|
||||
for i in 0..8 {
|
||||
info!("page {:?}: erasing... ", i);
|
||||
q.erase(i * PAGE_SIZE).await.unwrap();
|
||||
|
||||
for j in 0..PAGE_SIZE {
|
||||
buf.0[j] = pattern((j + i * PAGE_SIZE) as u32);
|
||||
}
|
||||
|
||||
info!("programming...");
|
||||
q.write(i * PAGE_SIZE, &buf.0).await.unwrap();
|
||||
}
|
||||
|
||||
for i in 0..8 {
|
||||
info!("page {:?}: reading... ", i);
|
||||
q.read(i * PAGE_SIZE, &mut buf.0).await.unwrap();
|
||||
|
||||
info!("verifying...");
|
||||
for j in 0..PAGE_SIZE {
|
||||
assert_eq!(buf.0[j], pattern((j + i * PAGE_SIZE) as u32));
|
||||
}
|
||||
}
|
||||
|
||||
info!("done!")
|
||||
}
|
||||
|
||||
static EXECUTOR: Forever<Executor> = Forever::new();
|
||||
|
||||
#[entry]
|
||||
fn main() -> ! {
|
||||
info!("Hello World!");
|
||||
|
||||
let executor = EXECUTOR.put(Executor::new(cortex_m::asm::sev));
|
||||
unwrap!(executor.spawn(run()));
|
||||
|
||||
loop {
|
||||
executor.run();
|
||||
cortex_m::asm::wfe();
|
||||
}
|
||||
}
|
65
embassy-nrf-examples/src/bin/rtc_async.rs
Normal file
65
embassy-nrf-examples/src/bin/rtc_async.rs
Normal file
@ -0,0 +1,65 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
|
||||
#[path = "../example_common.rs"]
|
||||
mod example_common;
|
||||
use example_common::*;
|
||||
|
||||
use core::mem::MaybeUninit;
|
||||
use cortex_m_rt::entry;
|
||||
use defmt::panic;
|
||||
use embassy::executor::{task, Executor};
|
||||
use embassy::time::{Clock, Duration, Timer};
|
||||
use embassy::util::Forever;
|
||||
use embassy_nrf::pac;
|
||||
use embassy_nrf::{interrupt, rtc};
|
||||
use nrf52840_hal::clocks;
|
||||
|
||||
#[task]
|
||||
async fn run1() {
|
||||
loop {
|
||||
info!("BIG INFREQUENT TICK");
|
||||
Timer::after(Duration::from_ticks(64000)).await;
|
||||
}
|
||||
}
|
||||
|
||||
#[task]
|
||||
async fn run2() {
|
||||
loop {
|
||||
info!("tick");
|
||||
Timer::after(Duration::from_ticks(13000)).await;
|
||||
}
|
||||
}
|
||||
|
||||
static RTC: Forever<rtc::RTC<pac::RTC1>> = Forever::new();
|
||||
static ALARM: Forever<rtc::Alarm<pac::RTC1>> = Forever::new();
|
||||
static EXECUTOR: Forever<Executor> = Forever::new();
|
||||
|
||||
#[entry]
|
||||
fn main() -> ! {
|
||||
info!("Hello World!");
|
||||
|
||||
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_with_alarm(alarm, cortex_m::asm::sev));
|
||||
|
||||
unwrap!(executor.spawn(run1()));
|
||||
unwrap!(executor.spawn(run2()));
|
||||
|
||||
loop {
|
||||
executor.run();
|
||||
cortex_m::asm::wfe();
|
||||
}
|
||||
}
|
63
embassy-nrf-examples/src/bin/rtc_raw.rs
Normal file
63
embassy-nrf-examples/src/bin/rtc_raw.rs
Normal file
@ -0,0 +1,63 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
|
||||
#[path = "../example_common.rs"]
|
||||
mod example_common;
|
||||
use example_common::*;
|
||||
|
||||
use core::mem::MaybeUninit;
|
||||
use cortex_m_rt::entry;
|
||||
use defmt::panic;
|
||||
use embassy::time::{Alarm, Clock};
|
||||
use embassy_nrf::{interrupt, rtc};
|
||||
use nrf52840_hal::clocks;
|
||||
|
||||
static mut RTC: MaybeUninit<rtc::RTC<embassy_nrf::pac::RTC1>> = MaybeUninit::uninit();
|
||||
|
||||
#[entry]
|
||||
fn main() -> ! {
|
||||
info!("Hello World!");
|
||||
|
||||
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 irq = interrupt::take!(RTC1);
|
||||
|
||||
let rtc: &'static _ = unsafe {
|
||||
let ptr = RTC.as_mut_ptr();
|
||||
ptr.write(rtc::RTC::new(p.RTC1, irq));
|
||||
&*ptr
|
||||
};
|
||||
|
||||
let alarm = rtc.alarm0();
|
||||
|
||||
rtc.start();
|
||||
|
||||
alarm.set_callback(|| info!("ALARM TRIGGERED"));
|
||||
alarm.set(53719);
|
||||
|
||||
info!("initialized!");
|
||||
|
||||
let mut val = 0;
|
||||
let mut printval = 0;
|
||||
loop {
|
||||
let val2 = rtc.now();
|
||||
if val2 < val {
|
||||
info!(
|
||||
"timer ran backwards! {:u32} -> {:u32}",
|
||||
val as u32, val2 as u32
|
||||
);
|
||||
}
|
||||
val = val2;
|
||||
|
||||
if val > printval + 32768 {
|
||||
info!("tick {:u32}", val as u32);
|
||||
printval = val;
|
||||
}
|
||||
}
|
||||
}
|
107
embassy-nrf-examples/src/bin/uart.rs
Normal file
107
embassy-nrf-examples/src/bin/uart.rs
Normal file
@ -0,0 +1,107 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
|
||||
#[path = "../example_common.rs"]
|
||||
mod example_common;
|
||||
use example_common::*;
|
||||
|
||||
use cortex_m_rt::entry;
|
||||
use defmt::panic;
|
||||
use embassy::executor::{task, Executor};
|
||||
use embassy::time::{Duration, Timer};
|
||||
use embassy::util::Forever;
|
||||
use embassy_nrf::{interrupt, pac, rtc, uarte};
|
||||
use futures::future::{select, Either};
|
||||
use nrf52840_hal::clocks;
|
||||
use nrf52840_hal::gpio;
|
||||
|
||||
#[task]
|
||||
async fn run(mut uart: uarte::Uarte<pac::UARTE0>) {
|
||||
info!("uarte initialized!");
|
||||
|
||||
// Message must be in SRAM
|
||||
let mut buf = [0; 8];
|
||||
buf.copy_from_slice(b"Hello!\r\n");
|
||||
|
||||
uart.send(&buf).await;
|
||||
info!("wrote hello in uart!");
|
||||
|
||||
info!("reading...");
|
||||
loop {
|
||||
let received = match select(
|
||||
uart.receive(&mut buf),
|
||||
Timer::after(Duration::from_millis(10)),
|
||||
)
|
||||
.await
|
||||
{
|
||||
Either::Left((buf, _)) => buf,
|
||||
Either::Right((_, read)) => {
|
||||
let (buf, n) = read.stop().await;
|
||||
&buf[..n]
|
||||
}
|
||||
};
|
||||
|
||||
if received.len() > 0 {
|
||||
info!("read done, got {:[u8]}", received);
|
||||
|
||||
// Echo back received data
|
||||
uart.send(received).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static RTC: Forever<rtc::RTC<pac::RTC1>> = Forever::new();
|
||||
static ALARM: Forever<rtc::Alarm<pac::RTC1>> = Forever::new();
|
||||
static EXECUTOR: Forever<Executor> = Forever::new();
|
||||
|
||||
#[entry]
|
||||
fn main() -> ! {
|
||||
info!("Hello World!");
|
||||
|
||||
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_with_alarm(alarm, cortex_m::asm::sev));
|
||||
|
||||
// Init UART
|
||||
let port0 = gpio::p0::Parts::new(p.P0);
|
||||
|
||||
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 uart = unsafe {
|
||||
uarte::Uarte::new(
|
||||
p.UARTE0,
|
||||
interrupt::take!(UARTE0_UART0),
|
||||
pins,
|
||||
uarte::Parity::EXCLUDED,
|
||||
uarte::Baudrate::BAUD115200,
|
||||
)
|
||||
};
|
||||
|
||||
unwrap!(executor.spawn(run(uart)));
|
||||
|
||||
loop {
|
||||
executor.run();
|
||||
cortex_m::asm::wfe();
|
||||
}
|
||||
}
|
18
embassy-nrf-examples/src/example_common.rs
Normal file
18
embassy-nrf-examples/src/example_common.rs
Normal file
@ -0,0 +1,18 @@
|
||||
#![macro_use]
|
||||
|
||||
use defmt_rtt as _; // global logger
|
||||
use nrf52840_hal as _;
|
||||
use panic_probe as _;
|
||||
|
||||
pub use defmt::*;
|
||||
|
||||
use core::sync::atomic::{AtomicUsize, Ordering};
|
||||
|
||||
#[defmt::timestamp]
|
||||
fn timestamp() -> u64 {
|
||||
static COUNT: AtomicUsize = AtomicUsize::new(0);
|
||||
// NOTE(no-CAS) `timestamps` runs with interrupts disabled
|
||||
let n = COUNT.load(Ordering::Relaxed);
|
||||
COUNT.store(n + 1, Ordering::Relaxed);
|
||||
n as u64
|
||||
}
|
Reference in New Issue
Block a user