From 9a57deef9b531b8dae9d98a5accf5aeb128ab86d Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Tue, 22 Sep 2020 18:03:43 +0200 Subject: First commit --- examples/Cargo.toml | 31 +++++++++++ examples/build.rs | 31 +++++++++++ examples/memory.x | 7 +++ examples/src/bin/qspi.rs | 123 +++++++++++++++++++++++++++++++++++++++++ examples/src/bin/uart.rs | 72 ++++++++++++++++++++++++ examples/src/example_common.rs | 68 +++++++++++++++++++++++ 6 files changed, 332 insertions(+) create mode 100644 examples/Cargo.toml create mode 100644 examples/build.rs create mode 100644 examples/memory.x create mode 100644 examples/src/bin/qspi.rs create mode 100644 examples/src/bin/uart.rs create mode 100644 examples/src/example_common.rs (limited to 'examples') diff --git a/examples/Cargo.toml b/examples/Cargo.toml new file mode 100644 index 000000000..c243691a7 --- /dev/null +++ b/examples/Cargo.toml @@ -0,0 +1,31 @@ +[package] +authors = ["Dario Nieuwenhuis "] +edition = "2018" +name = "embassy-examples" +version = "0.1.0" + +[features] +default = [ + "defmt-default", +] +defmt-default = [] +defmt-trace = [] +defmt-debug = [] +defmt-info = [] +defmt-warn = [] +defmt-error = [] + + +[dependencies] +cortex-m = { version = "0.6.3" } +cortex-m-rt = "0.6.12" +defmt = "0.1.0" +embedded-hal = { version = "0.2.4" } +defmt-rtt = "0.1.0" +panic-probe = "0.1.0" +nrf52840-hal = { version = "0.11.0" } +embassy = { version = "0.1.0", path = "../embassy" } +embassy-nrf = { version = "0.1.0", path = "../embassy-nrf", features = ["defmt-trace", "nrf52840"] } +static-executor = { version = "0.1.0", features=["defmt"]} +static-executor-cortex-m = { version = "0.1.0" } +futures = { version = "0.3.5", default-features = false } diff --git a/examples/build.rs b/examples/build.rs new file mode 100644 index 000000000..d534cc3df --- /dev/null +++ b/examples/build.rs @@ -0,0 +1,31 @@ +//! This build script copies the `memory.x` file from the crate root into +//! a directory where the linker can always find it at build time. +//! For many projects this is optional, as the linker always searches the +//! project root directory -- wherever `Cargo.toml` is. However, if you +//! are using a workspace or have a more complicated build setup, this +//! build script becomes required. Additionally, by requesting that +//! Cargo re-run the build script whenever `memory.x` is changed, +//! updating `memory.x` ensures a rebuild of the application with the +//! new memory settings. + +use std::env; +use std::fs::File; +use std::io::Write; +use std::path::PathBuf; + +fn main() { + // Put `memory.x` in our output directory and ensure it's + // on the linker search path. + let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap()); + File::create(out.join("memory.x")) + .unwrap() + .write_all(include_bytes!("memory.x")) + .unwrap(); + println!("cargo:rustc-link-search={}", out.display()); + + // By default, Cargo will re-run a build script whenever + // any file in the project changes. By specifying `memory.x` + // here, we ensure the build script is only re-run when + // `memory.x` is changed. + println!("cargo:rerun-if-changed=memory.x"); +} diff --git a/examples/memory.x b/examples/memory.x new file mode 100644 index 000000000..9b04edec0 --- /dev/null +++ b/examples/memory.x @@ -0,0 +1,7 @@ +MEMORY +{ + /* NOTE 1 K = 1 KiBi = 1024 bytes */ + /* These values correspond to the NRF52840 with Softdevices S140 7.0.1 */ + FLASH : ORIGIN = 0x00000000, LENGTH = 1024K + RAM : ORIGIN = 0x20000000, LENGTH = 256K +} diff --git a/examples/src/bin/qspi.rs b/examples/src/bin/qspi.rs new file mode 100644 index 000000000..395422e7f --- /dev/null +++ b/examples/src/bin/qspi.rs @@ -0,0 +1,123 @@ +#![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 embassy::flash::Flash; +use embassy_nrf::qspi; +use nrf52840_hal::gpio; + +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]); + +#[static_executor::task] +async fn run() { + let p = embassy_nrf::pac::Peripherals::take().dewrap(); + + 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, + }; + + let mut q = qspi::Qspi::new(p.QSPI, 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!") +} + +#[entry] +fn main() -> ! { + info!("Hello World!"); + + unsafe { + run.spawn().dewrap(); + static_executor::run(); + } +} diff --git a/examples/src/bin/uart.rs b/examples/src/bin/uart.rs new file mode 100644 index 000000000..21e26e3ad --- /dev/null +++ b/examples/src/bin/uart.rs @@ -0,0 +1,72 @@ +#![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 embassy::io::{AsyncBufRead, AsyncBufReadExt, AsyncWrite, AsyncWriteExt}; +use embassy_nrf::uarte; +use futures::pin_mut; +use nrf52840_hal::gpio; + +#[static_executor::task] +async fn run() { + let p = embassy_nrf::pac::Peripherals::take().dewrap(); + + 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, + }; + + let u = uarte::Uarte::new( + p.UARTE0, + pins, + uarte::Parity::EXCLUDED, + uarte::Baudrate::BAUD115200, + ); + pin_mut!(u); + + info!("uarte initialized!"); + + u.write_all(b"Hello!\r\n").await.dewrap(); + info!("wrote hello in uart!"); + + // Simple demo, reading 8-char chunks and echoing them back reversed. + loop { + info!("reading..."); + let mut buf = [0u8; 8]; + u.read_exact(&mut buf).await.dewrap(); + 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..."); + u.write_all(&buf).await.dewrap(); + info!("write done"); + } +} + +#[entry] +fn main() -> ! { + info!("Hello World!"); + + unsafe { + run.spawn().dewrap(); + static_executor::run(); + } +} diff --git a/examples/src/example_common.rs b/examples/src/example_common.rs new file mode 100644 index 000000000..e9919153c --- /dev/null +++ b/examples/src/example_common.rs @@ -0,0 +1,68 @@ +#![macro_use] + +use defmt_rtt as _; // global logger +use nrf52840_hal as _; +use panic_probe as _; +use static_executor_cortex_m as _; + +pub use defmt::{info, intern}; + +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 +} + +macro_rules! depanic { + ($( $i:expr ),*) => { + { + defmt::error!($( $i ),*); + panic!(); + } + } +} + +pub trait Dewrap { + /// dewrap = defmt unwrap + fn dewrap(self) -> T; + + /// dexpect = defmt expect + fn dexpect(self, msg: M) -> T; +} + +impl Dewrap for Option { + fn dewrap(self) -> T { + match self { + Some(t) => t, + None => depanic!("Dewrap failed: enum is none"), + } + } + + fn dexpect(self, msg: M) -> T { + match self { + Some(t) => t, + None => depanic!("Unexpected None: {:?}", msg), + } + } +} + +impl Dewrap for Result { + fn dewrap(self) -> T { + match self { + Ok(t) => t, + Err(e) => depanic!("Dewrap failed: {:?}", e), + } + } + + fn dexpect(self, msg: M) -> T { + match self { + Ok(t) => t, + Err(e) => depanic!("Unexpected error: {:?}: {:?}", msg, e), + } + } +} -- cgit