Merge #1149
1149: Add samples for nrf5340 r=lulf a=Tiwalun Samples for the nrf5340, copied from the existing nrf samples. Not sure if copying them is the best way of adding support, or using features in the existing samples would be better? The code is mostly the same, with some different peripherals and pin mappings for the DK. Co-authored-by: Dominik Boehi <dominik.boehi@gmail.com>
This commit is contained in:
commit
dbf7493708
67 changed files with 247 additions and 7 deletions
3
.vscode/settings.json
vendored
3
.vscode/settings.json
vendored
|
@ -13,7 +13,8 @@
|
|||
// Declare for the target you wish to develop
|
||||
// "embassy-executor/Cargo.toml",
|
||||
// "embassy-sync/Cargo.toml",
|
||||
"examples/nrf/Cargo.toml",
|
||||
"examples/nrf52840/Cargo.toml",
|
||||
//"examples/nrf5340/Cargo.toml",
|
||||
// "examples/nrf-rtos-trace/Cargo.toml",
|
||||
// "examples/rp/Cargo.toml",
|
||||
// "examples/std/Cargo.toml",
|
||||
|
|
|
@ -19,7 +19,7 @@ Rust's <a href="https://rust-lang.github.io/async-book/">async/await</a> allows
|
|||
No more messing with hardware timers. <a href="https://docs.embassy.dev/embassy-time">embassy_time</a> provides Instant, Duration and Timer types that are globally available and never overflow.
|
||||
|
||||
- **Real-time ready** -
|
||||
Tasks on the same async executor run cooperatively, but you can create multiple executors with different priorities, so that higher priority tasks preempt lower priority ones. See the <a href="https://github.com/embassy-rs/embassy/blob/master/examples/nrf/src/bin/multiprio.rs">example</a>.
|
||||
Tasks on the same async executor run cooperatively, but you can create multiple executors with different priorities, so that higher priority tasks preempt lower priority ones. See the <a href="https://github.com/embassy-rs/embassy/blob/master/examples/nrf52840/src/bin/multiprio.rs">example</a>.
|
||||
|
||||
- **Low-power ready** -
|
||||
Easily build devices with years of battery life. The async executor automatically puts the core to sleep when there's no work to do. Tasks are woken by interrupts, there is no busy-loop polling while waiting.
|
||||
|
@ -87,7 +87,8 @@ async fn main(spawner: Spawner) {
|
|||
|
||||
Examples are found in the `examples/` folder seperated by the chip manufacturer they are designed to run on. For example:
|
||||
|
||||
* `examples/nrf` run on the `nrf52840-dk` board (PCA10056) but should be easily adaptable to other nRF52 chips and boards.
|
||||
* `examples/nrf52840` run on the `nrf52840-dk` board (PCA10056) but should be easily adaptable to other nRF52 chips and boards.
|
||||
* `examples/nrf5340` run on the `nrf5340-dk` board (PCA10095).
|
||||
* `examples/stm32xx` for the various STM32 families.
|
||||
* `examples/rp` are for the RP2040 chip.
|
||||
* `examples/std` are designed to run locally on your PC.
|
||||
|
@ -110,7 +111,7 @@ cargo install probe-run
|
|||
- Change directory to the sample's base directory. For example:
|
||||
|
||||
```bash
|
||||
cd examples/nrf
|
||||
cd examples/nrf52840
|
||||
```
|
||||
|
||||
- Run the example
|
||||
|
|
3
ci.sh
3
ci.sh
|
@ -89,7 +89,8 @@ cargo batch \
|
|||
--- build --release --manifest-path docs/modules/ROOT/examples/layer-by-layer/blinky-hal/Cargo.toml --target thumbv7em-none-eabi \
|
||||
--- build --release --manifest-path docs/modules/ROOT/examples/layer-by-layer/blinky-irq/Cargo.toml --target thumbv7em-none-eabi \
|
||||
--- build --release --manifest-path docs/modules/ROOT/examples/layer-by-layer/blinky-async/Cargo.toml --target thumbv7em-none-eabi \
|
||||
--- build --release --manifest-path examples/nrf/Cargo.toml --target thumbv7em-none-eabi --out-dir out/examples/nrf \
|
||||
--- build --release --manifest-path examples/nrf52840/Cargo.toml --target thumbv7em-none-eabi --out-dir out/examples/nrf52840 \
|
||||
--- build --release --manifest-path examples/nrf5340/Cargo.toml --target thumbv8m.main-none-eabihf --out-dir out/examples/nrf5340 \
|
||||
--- build --release --manifest-path examples/rp/Cargo.toml --target thumbv6m-none-eabi --out-dir out/examples/rp \
|
||||
--- build --release --manifest-path examples/stm32f0/Cargo.toml --target thumbv6m-none-eabi --out-dir out/examples/stm32f0 \
|
||||
--- build --release --manifest-path examples/stm32f1/Cargo.toml --target thumbv7m-none-eabi --out-dir out/examples/stm32f1 \
|
||||
|
|
|
@ -65,5 +65,5 @@ cargo batch \
|
|||
--- build --release --manifest-path embassy-stm32/Cargo.toml --target thumbv7m-none-eabi --features stm32l151cb-a,defmt,exti,time-driver-any,unstable-traits \
|
||||
--- build --release --manifest-path embassy-stm32/Cargo.toml --target thumbv7m-none-eabi --features stm32f217zg,defmt,exti,time-driver-any \
|
||||
--- build --release --manifest-path embassy-stm32/Cargo.toml --target thumbv7m-none-eabi --features stm32f217zg,defmt,exti,time-driver-any,unstable-traits \
|
||||
--- build --release --manifest-path examples/nrf/Cargo.toml --target thumbv7em-none-eabi --no-default-features --out-dir out/examples/nrf --bin raw_spawn \
|
||||
--- build --release --manifest-path examples/nrf52840/Cargo.toml --target thumbv7em-none-eabi --no-default-features --out-dir out/examples/nrf52840 --bin raw_spawn \
|
||||
--- build --release --manifest-path examples/stm32l0/Cargo.toml --target thumbv6m-none-eabi --no-default-features --out-dir out/examples/stm32l0 --bin raw_spawn \
|
||||
|
|
|
@ -45,7 +45,7 @@ You can run an example by opening a terminal and entering the following commands
|
|||
|
||||
[source, bash]
|
||||
----
|
||||
cd examples/nrf
|
||||
cd examples/nrf52840
|
||||
cargo run --bin blinky --release
|
||||
----
|
||||
|
||||
|
|
9
examples/nrf5340/.cargo/config.toml
Normal file
9
examples/nrf5340/.cargo/config.toml
Normal file
|
@ -0,0 +1,9 @@
|
|||
[target.'cfg(all(target_arch = "arm", target_os = "none"))']
|
||||
# replace nRF5340_xxAA with your chip as listed in `probe-run --list-chips`
|
||||
runner = "probe-run --chip nRF5340_xxAA"
|
||||
|
||||
[build]
|
||||
target = "thumbv8m.main-none-eabihf"
|
||||
|
||||
[env]
|
||||
DEFMT_LOG = "trace"
|
64
examples/nrf5340/Cargo.toml
Normal file
64
examples/nrf5340/Cargo.toml
Normal file
|
@ -0,0 +1,64 @@
|
|||
[package]
|
||||
edition = "2021"
|
||||
name = "embassy-nrf-examples"
|
||||
version = "0.1.0"
|
||||
license = "MIT OR Apache-2.0"
|
||||
|
||||
[features]
|
||||
default = ["nightly"]
|
||||
nightly = [
|
||||
"embassy-executor/nightly",
|
||||
"embassy-nrf/nightly",
|
||||
"embassy-net/nightly",
|
||||
"embassy-nrf/unstable-traits",
|
||||
"embassy-usb",
|
||||
"embedded-io/async",
|
||||
"embassy-net",
|
||||
]
|
||||
|
||||
[dependencies]
|
||||
embassy-futures = { version = "0.1.0", path = "../../embassy-futures" }
|
||||
embassy-sync = { version = "0.1.0", path = "../../embassy-sync", features = [
|
||||
"defmt",
|
||||
] }
|
||||
embassy-executor = { version = "0.1.0", path = "../../embassy-executor", features = [
|
||||
"defmt",
|
||||
"integrated-timers",
|
||||
] }
|
||||
embassy-time = { version = "0.1.0", path = "../../embassy-time", features = [
|
||||
"defmt",
|
||||
"defmt-timestamp-uptime",
|
||||
] }
|
||||
embassy-nrf = { version = "0.1.0", path = "../../embassy-nrf", features = [
|
||||
"defmt",
|
||||
"nrf5340-app-s",
|
||||
"time-driver-rtc1",
|
||||
"gpiote",
|
||||
"unstable-pac",
|
||||
] }
|
||||
embassy-net = { version = "0.1.0", path = "../../embassy-net", features = [
|
||||
"defmt",
|
||||
"tcp",
|
||||
"dhcpv4",
|
||||
"medium-ethernet",
|
||||
], optional = true }
|
||||
embassy-usb = { version = "0.1.0", path = "../../embassy-usb", features = [
|
||||
"defmt",
|
||||
], optional = true }
|
||||
embedded-io = "0.4.0"
|
||||
|
||||
|
||||
defmt = "0.3"
|
||||
defmt-rtt = "0.4"
|
||||
|
||||
static_cell = "1.0"
|
||||
cortex-m = { version = "0.7.6", features = ["critical-section-single-core"] }
|
||||
cortex-m-rt = "0.7.0"
|
||||
panic-probe = { version = "0.3", features = ["print-defmt"] }
|
||||
futures = { version = "0.3.17", default-features = false, features = [
|
||||
"async-await",
|
||||
] }
|
||||
rand = { version = "0.8.4", default-features = false }
|
||||
embedded-storage = "0.3.0"
|
||||
usbd-hid = "0.6.0"
|
||||
serde = { version = "1.0.136", default-features = false }
|
35
examples/nrf5340/build.rs
Normal file
35
examples/nrf5340/build.rs
Normal file
|
@ -0,0 +1,35 @@
|
|||
//! 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");
|
||||
|
||||
println!("cargo:rustc-link-arg-bins=--nmagic");
|
||||
println!("cargo:rustc-link-arg-bins=-Tlink.x");
|
||||
println!("cargo:rustc-link-arg-bins=-Tdefmt.x");
|
||||
}
|
7
examples/nrf5340/memory.x
Normal file
7
examples/nrf5340/memory.x
Normal file
|
@ -0,0 +1,7 @@
|
|||
MEMORY
|
||||
{
|
||||
/* NOTE 1 K = 1 KiBi = 1024 bytes */
|
||||
/* These values correspond to the NRF5340 */
|
||||
FLASH : ORIGIN = 0x00000000, LENGTH = 1024K
|
||||
RAM : ORIGIN = 0x20000000, LENGTH = 256K
|
||||
}
|
21
examples/nrf5340/src/bin/blinky.rs
Normal file
21
examples/nrf5340/src/bin/blinky.rs
Normal file
|
@ -0,0 +1,21 @@
|
|||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
|
||||
use embassy_executor::Spawner;
|
||||
use embassy_nrf::gpio::{Level, Output, OutputDrive};
|
||||
use embassy_time::{Duration, Timer};
|
||||
use {defmt_rtt as _, panic_probe as _};
|
||||
|
||||
#[embassy_executor::main]
|
||||
async fn main(_spawner: Spawner) {
|
||||
let p = embassy_nrf::init(Default::default());
|
||||
let mut led = Output::new(p.P0_28, Level::Low, OutputDrive::Standard);
|
||||
|
||||
loop {
|
||||
led.set_high();
|
||||
Timer::after(Duration::from_millis(300)).await;
|
||||
led.set_low();
|
||||
Timer::after(Duration::from_millis(300)).await;
|
||||
}
|
||||
}
|
66
examples/nrf5340/src/bin/gpiote_channel.rs
Normal file
66
examples/nrf5340/src/bin/gpiote_channel.rs
Normal file
|
@ -0,0 +1,66 @@
|
|||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
|
||||
use defmt::info;
|
||||
use embassy_executor::Spawner;
|
||||
use embassy_nrf::gpio::{Input, Pull};
|
||||
use embassy_nrf::gpiote::{InputChannel, InputChannelPolarity};
|
||||
use {defmt_rtt as _, panic_probe as _};
|
||||
|
||||
#[embassy_executor::main]
|
||||
async fn main(_spawner: Spawner) {
|
||||
let p = embassy_nrf::init(Default::default());
|
||||
info!("Starting!");
|
||||
|
||||
let ch1 = InputChannel::new(
|
||||
p.GPIOTE_CH0,
|
||||
Input::new(p.P0_23, Pull::Up),
|
||||
InputChannelPolarity::HiToLo,
|
||||
);
|
||||
let ch2 = InputChannel::new(
|
||||
p.GPIOTE_CH1,
|
||||
Input::new(p.P0_24, Pull::Up),
|
||||
InputChannelPolarity::LoToHi,
|
||||
);
|
||||
let ch3 = InputChannel::new(
|
||||
p.GPIOTE_CH2,
|
||||
Input::new(p.P0_08, Pull::Up),
|
||||
InputChannelPolarity::Toggle,
|
||||
);
|
||||
let ch4 = InputChannel::new(
|
||||
p.GPIOTE_CH3,
|
||||
Input::new(p.P0_09, Pull::Up),
|
||||
InputChannelPolarity::Toggle,
|
||||
);
|
||||
|
||||
let button1 = async {
|
||||
loop {
|
||||
ch1.wait().await;
|
||||
info!("Button 1 pressed")
|
||||
}
|
||||
};
|
||||
|
||||
let button2 = async {
|
||||
loop {
|
||||
ch2.wait().await;
|
||||
info!("Button 2 released")
|
||||
}
|
||||
};
|
||||
|
||||
let button3 = async {
|
||||
loop {
|
||||
ch3.wait().await;
|
||||
info!("Button 3 toggled")
|
||||
}
|
||||
};
|
||||
|
||||
let button4 = async {
|
||||
loop {
|
||||
ch4.wait().await;
|
||||
info!("Button 4 toggled")
|
||||
}
|
||||
};
|
||||
|
||||
futures::join!(button1, button2, button3, button4);
|
||||
}
|
35
examples/nrf5340/src/bin/uart.rs
Normal file
35
examples/nrf5340/src/bin/uart.rs
Normal file
|
@ -0,0 +1,35 @@
|
|||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
|
||||
use defmt::*;
|
||||
use embassy_executor::Spawner;
|
||||
use embassy_nrf::{interrupt, uarte};
|
||||
use {defmt_rtt as _, panic_probe as _};
|
||||
|
||||
#[embassy_executor::main]
|
||||
async fn main(_spawner: Spawner) {
|
||||
let p = embassy_nrf::init(Default::default());
|
||||
let mut config = uarte::Config::default();
|
||||
config.parity = uarte::Parity::EXCLUDED;
|
||||
config.baudrate = uarte::Baudrate::BAUD115200;
|
||||
|
||||
let irq = interrupt::take!(SERIAL0);
|
||||
let mut uart = uarte::Uarte::new(p.UARTETWISPI0, irq, p.P1_00, p.P1_01, config);
|
||||
|
||||
info!("uarte initialized!");
|
||||
|
||||
// Message must be in SRAM
|
||||
let mut buf = [0; 8];
|
||||
buf.copy_from_slice(b"Hello!\r\n");
|
||||
|
||||
unwrap!(uart.write(&buf).await);
|
||||
info!("wrote hello in uart!");
|
||||
|
||||
loop {
|
||||
info!("reading...");
|
||||
unwrap!(uart.read(&mut buf).await);
|
||||
info!("writing...");
|
||||
unwrap!(uart.write(&buf).await);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue