diff options
| author | Dominik Boehi <[email protected]> | 2023-01-08 15:36:35 +0100 |
|---|---|---|
| committer | Dominik Boehi <[email protected]> | 2023-01-08 15:36:35 +0100 |
| commit | f8afc3c8828c334baaa399f73764ba45a3f69799 (patch) | |
| tree | 4cfcc03cbcdd5604ca4f9ebb736d458cd013ae87 /examples | |
| parent | 3fbedd7c0953cab5f14433da6b9b1e2ff5d76939 (diff) | |
Add samples for nrf5340
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/nrf5340/.cargo/config.toml | 9 | ||||
| -rw-r--r-- | examples/nrf5340/Cargo.toml | 64 | ||||
| -rw-r--r-- | examples/nrf5340/build.rs | 35 | ||||
| -rw-r--r-- | examples/nrf5340/memory.x | 7 | ||||
| -rw-r--r-- | examples/nrf5340/src/bin/awaitable_timer.rs | 26 | ||||
| -rw-r--r-- | examples/nrf5340/src/bin/blinky.rs | 21 | ||||
| -rw-r--r-- | examples/nrf5340/src/bin/buffered_uart.rs | 57 | ||||
| -rw-r--r-- | examples/nrf5340/src/bin/channel.rs | 43 | ||||
| -rw-r--r-- | examples/nrf5340/src/bin/channel_sender_receiver.rs | 50 | ||||
| -rw-r--r-- | examples/nrf5340/src/bin/gpiote_channel.rs | 66 | ||||
| -rw-r--r-- | examples/nrf5340/src/bin/gpiote_port.rs | 34 | ||||
| -rw-r--r-- | examples/nrf5340/src/bin/uart.rs | 35 | ||||
| -rw-r--r-- | examples/nrf5340/src/bin/uart_idle.rs | 35 | ||||
| -rw-r--r-- | examples/nrf5340/src/bin/uart_split.rs | 60 |
14 files changed, 542 insertions, 0 deletions
diff --git a/examples/nrf5340/.cargo/config.toml b/examples/nrf5340/.cargo/config.toml new file mode 100644 index 000000000..ff0879c8c --- /dev/null +++ b/examples/nrf5340/.cargo/config.toml | |||
| @@ -0,0 +1,9 @@ | |||
| 1 | [target.'cfg(all(target_arch = "arm", target_os = "none"))'] | ||
| 2 | # replace nRF5340_xxAA with your chip as listed in `probe-run --list-chips` | ||
| 3 | runner = "probe-run --chip nRF5340_xxAA" | ||
| 4 | |||
| 5 | [build] | ||
| 6 | target = "thumbv8m.main-none-eabihf" | ||
| 7 | |||
| 8 | [env] | ||
| 9 | DEFMT_LOG = "trace" | ||
diff --git a/examples/nrf5340/Cargo.toml b/examples/nrf5340/Cargo.toml new file mode 100644 index 000000000..03485711e --- /dev/null +++ b/examples/nrf5340/Cargo.toml | |||
| @@ -0,0 +1,64 @@ | |||
| 1 | [package] | ||
| 2 | edition = "2021" | ||
| 3 | name = "embassy-nrf-examples" | ||
| 4 | version = "0.1.0" | ||
| 5 | license = "MIT OR Apache-2.0" | ||
| 6 | |||
| 7 | [features] | ||
| 8 | default = ["nightly"] | ||
| 9 | nightly = [ | ||
| 10 | "embassy-executor/nightly", | ||
| 11 | "embassy-nrf/nightly", | ||
| 12 | "embassy-net/nightly", | ||
| 13 | "embassy-nrf/unstable-traits", | ||
| 14 | "embassy-usb", | ||
| 15 | "embedded-io/async", | ||
| 16 | "embassy-net", | ||
| 17 | ] | ||
| 18 | |||
| 19 | [dependencies] | ||
| 20 | embassy-futures = { version = "0.1.0", path = "../../embassy-futures" } | ||
| 21 | embassy-sync = { version = "0.1.0", path = "../../embassy-sync", features = [ | ||
| 22 | "defmt", | ||
| 23 | ] } | ||
| 24 | embassy-executor = { version = "0.1.0", path = "../../embassy-executor", features = [ | ||
| 25 | "defmt", | ||
| 26 | "integrated-timers", | ||
| 27 | ] } | ||
| 28 | embassy-time = { version = "0.1.0", path = "../../embassy-time", features = [ | ||
| 29 | "defmt", | ||
| 30 | "defmt-timestamp-uptime", | ||
| 31 | ] } | ||
| 32 | embassy-nrf = { version = "0.1.0", path = "../../embassy-nrf", features = [ | ||
| 33 | "defmt", | ||
| 34 | "nrf5340-app-s", | ||
| 35 | "time-driver-rtc1", | ||
| 36 | "gpiote", | ||
| 37 | "unstable-pac", | ||
| 38 | ] } | ||
| 39 | embassy-net = { version = "0.1.0", path = "../../embassy-net", features = [ | ||
| 40 | "defmt", | ||
| 41 | "tcp", | ||
| 42 | "dhcpv4", | ||
| 43 | "medium-ethernet", | ||
| 44 | ], optional = true } | ||
| 45 | embassy-usb = { version = "0.1.0", path = "../../embassy-usb", features = [ | ||
| 46 | "defmt", | ||
| 47 | ], optional = true } | ||
| 48 | embedded-io = "0.4.0" | ||
| 49 | |||
| 50 | |||
| 51 | defmt = "0.3" | ||
| 52 | defmt-rtt = "0.4" | ||
| 53 | |||
| 54 | static_cell = "1.0" | ||
| 55 | cortex-m = { version = "0.7.6", features = ["critical-section-single-core"] } | ||
| 56 | cortex-m-rt = "0.7.0" | ||
| 57 | panic-probe = { version = "0.3", features = ["print-defmt"] } | ||
| 58 | futures = { version = "0.3.17", default-features = false, features = [ | ||
| 59 | "async-await", | ||
| 60 | ] } | ||
| 61 | rand = { version = "0.8.4", default-features = false } | ||
| 62 | embedded-storage = "0.3.0" | ||
| 63 | usbd-hid = "0.6.0" | ||
| 64 | serde = { version = "1.0.136", default-features = false } | ||
diff --git a/examples/nrf5340/build.rs b/examples/nrf5340/build.rs new file mode 100644 index 000000000..30691aa97 --- /dev/null +++ b/examples/nrf5340/build.rs | |||
| @@ -0,0 +1,35 @@ | |||
| 1 | //! This build script copies the `memory.x` file from the crate root into | ||
| 2 | //! a directory where the linker can always find it at build time. | ||
| 3 | //! For many projects this is optional, as the linker always searches the | ||
| 4 | //! project root directory -- wherever `Cargo.toml` is. However, if you | ||
| 5 | //! are using a workspace or have a more complicated build setup, this | ||
| 6 | //! build script becomes required. Additionally, by requesting that | ||
| 7 | //! Cargo re-run the build script whenever `memory.x` is changed, | ||
| 8 | //! updating `memory.x` ensures a rebuild of the application with the | ||
| 9 | //! new memory settings. | ||
| 10 | |||
| 11 | use std::env; | ||
| 12 | use std::fs::File; | ||
| 13 | use std::io::Write; | ||
| 14 | use std::path::PathBuf; | ||
| 15 | |||
| 16 | fn main() { | ||
| 17 | // Put `memory.x` in our output directory and ensure it's | ||
| 18 | // on the linker search path. | ||
| 19 | let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap()); | ||
| 20 | File::create(out.join("memory.x")) | ||
| 21 | .unwrap() | ||
| 22 | .write_all(include_bytes!("memory.x")) | ||
| 23 | .unwrap(); | ||
| 24 | println!("cargo:rustc-link-search={}", out.display()); | ||
| 25 | |||
| 26 | // By default, Cargo will re-run a build script whenever | ||
| 27 | // any file in the project changes. By specifying `memory.x` | ||
| 28 | // here, we ensure the build script is only re-run when | ||
| 29 | // `memory.x` is changed. | ||
| 30 | println!("cargo:rerun-if-changed=memory.x"); | ||
| 31 | |||
| 32 | println!("cargo:rustc-link-arg-bins=--nmagic"); | ||
| 33 | println!("cargo:rustc-link-arg-bins=-Tlink.x"); | ||
| 34 | println!("cargo:rustc-link-arg-bins=-Tdefmt.x"); | ||
| 35 | } | ||
diff --git a/examples/nrf5340/memory.x b/examples/nrf5340/memory.x new file mode 100644 index 000000000..a122dc24a --- /dev/null +++ b/examples/nrf5340/memory.x | |||
| @@ -0,0 +1,7 @@ | |||
| 1 | MEMORY | ||
| 2 | { | ||
| 3 | /* NOTE 1 K = 1 KiBi = 1024 bytes */ | ||
| 4 | /* These values correspond to the NRF5340 */ | ||
| 5 | FLASH : ORIGIN = 0x00000000, LENGTH = 1024K | ||
| 6 | RAM : ORIGIN = 0x20000000, LENGTH = 256K | ||
| 7 | } | ||
diff --git a/examples/nrf5340/src/bin/awaitable_timer.rs b/examples/nrf5340/src/bin/awaitable_timer.rs new file mode 100644 index 000000000..b32af236c --- /dev/null +++ b/examples/nrf5340/src/bin/awaitable_timer.rs | |||
| @@ -0,0 +1,26 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | use defmt::info; | ||
| 6 | use embassy_executor::Spawner; | ||
| 7 | use embassy_nrf::interrupt; | ||
| 8 | use embassy_nrf::timer::Timer; | ||
| 9 | use {defmt_rtt as _, panic_probe as _}; | ||
| 10 | |||
| 11 | #[embassy_executor::main] | ||
| 12 | async fn main(_spawner: Spawner) { | ||
| 13 | let p = embassy_nrf::init(Default::default()); | ||
| 14 | let mut t = Timer::new_awaitable(p.TIMER0, interrupt::take!(TIMER0)); | ||
| 15 | // default frequency is 1MHz, so this triggers every second | ||
| 16 | t.cc(0).write(1_000_000); | ||
| 17 | // clear the timer value on cc[0] compare match | ||
| 18 | t.cc(0).short_compare_clear(); | ||
| 19 | t.start(); | ||
| 20 | |||
| 21 | loop { | ||
| 22 | // wait for compare match | ||
| 23 | t.cc(0).wait().await; | ||
| 24 | info!("hardware timer tick"); | ||
| 25 | } | ||
| 26 | } | ||
diff --git a/examples/nrf5340/src/bin/blinky.rs b/examples/nrf5340/src/bin/blinky.rs new file mode 100644 index 000000000..3422cedf0 --- /dev/null +++ b/examples/nrf5340/src/bin/blinky.rs | |||
| @@ -0,0 +1,21 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | use embassy_executor::Spawner; | ||
| 6 | use embassy_nrf::gpio::{Level, Output, OutputDrive}; | ||
| 7 | use embassy_time::{Duration, Timer}; | ||
| 8 | use {defmt_rtt as _, panic_probe as _}; | ||
| 9 | |||
| 10 | #[embassy_executor::main] | ||
| 11 | async fn main(_spawner: Spawner) { | ||
| 12 | let p = embassy_nrf::init(Default::default()); | ||
| 13 | let mut led = Output::new(p.P0_28, Level::Low, OutputDrive::Standard); | ||
| 14 | |||
| 15 | loop { | ||
| 16 | led.set_high(); | ||
| 17 | Timer::after(Duration::from_millis(300)).await; | ||
| 18 | led.set_low(); | ||
| 19 | Timer::after(Duration::from_millis(300)).await; | ||
| 20 | } | ||
| 21 | } | ||
diff --git a/examples/nrf5340/src/bin/buffered_uart.rs b/examples/nrf5340/src/bin/buffered_uart.rs new file mode 100644 index 000000000..25a0ca237 --- /dev/null +++ b/examples/nrf5340/src/bin/buffered_uart.rs | |||
| @@ -0,0 +1,57 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | use defmt::*; | ||
| 6 | use embassy_executor::Spawner; | ||
| 7 | use embassy_nrf::buffered_uarte::{BufferedUarte, State}; | ||
| 8 | use embassy_nrf::{interrupt, uarte}; | ||
| 9 | use embedded_io::asynch::{BufRead, Write}; | ||
| 10 | use futures::pin_mut; | ||
| 11 | use {defmt_rtt as _, panic_probe as _}; | ||
| 12 | |||
| 13 | #[embassy_executor::main] | ||
| 14 | async fn main(_spawner: Spawner) { | ||
| 15 | let p = embassy_nrf::init(Default::default()); | ||
| 16 | let mut config = uarte::Config::default(); | ||
| 17 | config.parity = uarte::Parity::EXCLUDED; | ||
| 18 | config.baudrate = uarte::Baudrate::BAUD115200; | ||
| 19 | |||
| 20 | let mut tx_buffer = [0u8; 4096]; | ||
| 21 | let mut rx_buffer = [0u8; 4096]; | ||
| 22 | |||
| 23 | let irq = interrupt::take!(SERIAL0); | ||
| 24 | let mut state = State::new(); | ||
| 25 | // Please note - important to have hardware flow control (https://github.com/embassy-rs/embassy/issues/536) | ||
| 26 | let u = BufferedUarte::new( | ||
| 27 | &mut state, | ||
| 28 | p.UARTETWISPI0, | ||
| 29 | p.TIMER0, | ||
| 30 | p.PPI_CH0, | ||
| 31 | p.PPI_CH1, | ||
| 32 | irq, | ||
| 33 | p.P0_08, | ||
| 34 | p.P0_06, | ||
| 35 | p.P0_07, | ||
| 36 | p.P0_05, | ||
| 37 | config, | ||
| 38 | &mut rx_buffer, | ||
| 39 | &mut tx_buffer, | ||
| 40 | ); | ||
| 41 | pin_mut!(u); | ||
| 42 | |||
| 43 | info!("uarte initialized!"); | ||
| 44 | |||
| 45 | unwrap!(u.write_all(b"Hello!\r\n").await); | ||
| 46 | info!("wrote hello in uart!"); | ||
| 47 | |||
| 48 | loop { | ||
| 49 | info!("reading..."); | ||
| 50 | let buf = unwrap!(u.fill_buf().await); | ||
| 51 | info!("read done, got {}", buf); | ||
| 52 | |||
| 53 | // Read bytes have to be explicitly consumed, otherwise fill_buf() will return them again | ||
| 54 | let n = buf.len(); | ||
| 55 | u.consume(n); | ||
| 56 | } | ||
| 57 | } | ||
diff --git a/examples/nrf5340/src/bin/channel.rs b/examples/nrf5340/src/bin/channel.rs new file mode 100644 index 000000000..425d43051 --- /dev/null +++ b/examples/nrf5340/src/bin/channel.rs | |||
| @@ -0,0 +1,43 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | use defmt::unwrap; | ||
| 6 | use embassy_executor::Spawner; | ||
| 7 | use embassy_nrf::gpio::{Level, Output, OutputDrive}; | ||
| 8 | use embassy_sync::blocking_mutex::raw::ThreadModeRawMutex; | ||
| 9 | use embassy_sync::channel::Channel; | ||
| 10 | use embassy_time::{Duration, Timer}; | ||
| 11 | use {defmt_rtt as _, panic_probe as _}; | ||
| 12 | |||
| 13 | enum LedState { | ||
| 14 | On, | ||
| 15 | Off, | ||
| 16 | } | ||
| 17 | |||
| 18 | static CHANNEL: Channel<ThreadModeRawMutex, LedState, 1> = Channel::new(); | ||
| 19 | |||
| 20 | #[embassy_executor::task] | ||
| 21 | async fn my_task() { | ||
| 22 | loop { | ||
| 23 | CHANNEL.send(LedState::On).await; | ||
| 24 | Timer::after(Duration::from_secs(1)).await; | ||
| 25 | CHANNEL.send(LedState::Off).await; | ||
| 26 | Timer::after(Duration::from_secs(1)).await; | ||
| 27 | } | ||
| 28 | } | ||
| 29 | |||
| 30 | #[embassy_executor::main] | ||
| 31 | async fn main(spawner: Spawner) { | ||
| 32 | let p = embassy_nrf::init(Default::default()); | ||
| 33 | let mut led = Output::new(p.P0_28, Level::Low, OutputDrive::Standard); | ||
| 34 | |||
| 35 | unwrap!(spawner.spawn(my_task())); | ||
| 36 | |||
| 37 | loop { | ||
| 38 | match CHANNEL.recv().await { | ||
| 39 | LedState::On => led.set_high(), | ||
| 40 | LedState::Off => led.set_low(), | ||
| 41 | } | ||
| 42 | } | ||
| 43 | } | ||
diff --git a/examples/nrf5340/src/bin/channel_sender_receiver.rs b/examples/nrf5340/src/bin/channel_sender_receiver.rs new file mode 100644 index 000000000..9628c0525 --- /dev/null +++ b/examples/nrf5340/src/bin/channel_sender_receiver.rs | |||
| @@ -0,0 +1,50 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | use defmt::unwrap; | ||
| 6 | use embassy_executor::Spawner; | ||
| 7 | use embassy_nrf::gpio::{AnyPin, Level, Output, OutputDrive, Pin}; | ||
| 8 | use embassy_sync::blocking_mutex::raw::NoopRawMutex; | ||
| 9 | use embassy_sync::channel::{Channel, Receiver, Sender}; | ||
| 10 | use embassy_time::{Duration, Timer}; | ||
| 11 | use static_cell::StaticCell; | ||
| 12 | use {defmt_rtt as _, panic_probe as _}; | ||
| 13 | |||
| 14 | enum LedState { | ||
| 15 | On, | ||
| 16 | Off, | ||
| 17 | } | ||
| 18 | |||
| 19 | static CHANNEL: StaticCell<Channel<NoopRawMutex, LedState, 1>> = StaticCell::new(); | ||
| 20 | |||
| 21 | #[embassy_executor::task] | ||
| 22 | async fn send_task(sender: Sender<'static, NoopRawMutex, LedState, 1>) { | ||
| 23 | loop { | ||
| 24 | sender.send(LedState::On).await; | ||
| 25 | Timer::after(Duration::from_secs(1)).await; | ||
| 26 | sender.send(LedState::Off).await; | ||
| 27 | Timer::after(Duration::from_secs(1)).await; | ||
| 28 | } | ||
| 29 | } | ||
| 30 | |||
| 31 | #[embassy_executor::task] | ||
| 32 | async fn recv_task(led: AnyPin, receiver: Receiver<'static, NoopRawMutex, LedState, 1>) { | ||
| 33 | let mut led = Output::new(led, Level::Low, OutputDrive::Standard); | ||
| 34 | |||
| 35 | loop { | ||
| 36 | match receiver.recv().await { | ||
| 37 | LedState::On => led.set_high(), | ||
| 38 | LedState::Off => led.set_low(), | ||
| 39 | } | ||
| 40 | } | ||
| 41 | } | ||
| 42 | |||
| 43 | #[embassy_executor::main] | ||
| 44 | async fn main(spawner: Spawner) { | ||
| 45 | let p = embassy_nrf::init(Default::default()); | ||
| 46 | let channel = CHANNEL.init(Channel::new()); | ||
| 47 | |||
| 48 | unwrap!(spawner.spawn(send_task(channel.sender()))); | ||
| 49 | unwrap!(spawner.spawn(recv_task(p.P0_28.degrade(), channel.receiver()))); | ||
| 50 | } | ||
diff --git a/examples/nrf5340/src/bin/gpiote_channel.rs b/examples/nrf5340/src/bin/gpiote_channel.rs new file mode 100644 index 000000000..ceab1194a --- /dev/null +++ b/examples/nrf5340/src/bin/gpiote_channel.rs | |||
| @@ -0,0 +1,66 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | use defmt::info; | ||
| 6 | use embassy_executor::Spawner; | ||
| 7 | use embassy_nrf::gpio::{Input, Pull}; | ||
| 8 | use embassy_nrf::gpiote::{InputChannel, InputChannelPolarity}; | ||
| 9 | use {defmt_rtt as _, panic_probe as _}; | ||
| 10 | |||
| 11 | #[embassy_executor::main] | ||
| 12 | async fn main(_spawner: Spawner) { | ||
| 13 | let p = embassy_nrf::init(Default::default()); | ||
| 14 | info!("Starting!"); | ||
| 15 | |||
| 16 | let ch1 = InputChannel::new( | ||
| 17 | p.GPIOTE_CH0, | ||
| 18 | Input::new(p.P0_23, Pull::Up), | ||
| 19 | InputChannelPolarity::HiToLo, | ||
| 20 | ); | ||
| 21 | let ch2 = InputChannel::new( | ||
| 22 | p.GPIOTE_CH1, | ||
| 23 | Input::new(p.P0_24, Pull::Up), | ||
| 24 | InputChannelPolarity::LoToHi, | ||
| 25 | ); | ||
| 26 | let ch3 = InputChannel::new( | ||
| 27 | p.GPIOTE_CH2, | ||
| 28 | Input::new(p.P0_08, Pull::Up), | ||
| 29 | InputChannelPolarity::Toggle, | ||
| 30 | ); | ||
| 31 | let ch4 = InputChannel::new( | ||
| 32 | p.GPIOTE_CH3, | ||
| 33 | Input::new(p.P0_09, Pull::Up), | ||
| 34 | InputChannelPolarity::Toggle, | ||
| 35 | ); | ||
| 36 | |||
| 37 | let button1 = async { | ||
| 38 | loop { | ||
| 39 | ch1.wait().await; | ||
| 40 | info!("Button 1 pressed") | ||
| 41 | } | ||
| 42 | }; | ||
| 43 | |||
| 44 | let button2 = async { | ||
| 45 | loop { | ||
| 46 | ch2.wait().await; | ||
| 47 | info!("Button 2 released") | ||
| 48 | } | ||
| 49 | }; | ||
| 50 | |||
| 51 | let button3 = async { | ||
| 52 | loop { | ||
| 53 | ch3.wait().await; | ||
| 54 | info!("Button 3 toggled") | ||
| 55 | } | ||
| 56 | }; | ||
| 57 | |||
| 58 | let button4 = async { | ||
| 59 | loop { | ||
| 60 | ch4.wait().await; | ||
| 61 | info!("Button 4 toggled") | ||
| 62 | } | ||
| 63 | }; | ||
| 64 | |||
| 65 | futures::join!(button1, button2, button3, button4); | ||
| 66 | } | ||
diff --git a/examples/nrf5340/src/bin/gpiote_port.rs b/examples/nrf5340/src/bin/gpiote_port.rs new file mode 100644 index 000000000..0cc911ad2 --- /dev/null +++ b/examples/nrf5340/src/bin/gpiote_port.rs | |||
| @@ -0,0 +1,34 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | use defmt::{info, unwrap}; | ||
| 6 | use embassy_executor::Spawner; | ||
| 7 | use embassy_nrf::gpio::{AnyPin, Input, Pin as _, Pull}; | ||
| 8 | use {defmt_rtt as _, panic_probe as _}; | ||
| 9 | |||
| 10 | #[embassy_executor::task(pool_size = 4)] | ||
| 11 | async fn button_task(n: usize, mut pin: Input<'static, AnyPin>) { | ||
| 12 | loop { | ||
| 13 | pin.wait_for_low().await; | ||
| 14 | info!("Button {:?} pressed!", n); | ||
| 15 | pin.wait_for_high().await; | ||
| 16 | info!("Button {:?} released!", n); | ||
| 17 | } | ||
| 18 | } | ||
| 19 | |||
| 20 | #[embassy_executor::main] | ||
| 21 | async fn main(spawner: Spawner) { | ||
| 22 | let p = embassy_nrf::init(Default::default()); | ||
| 23 | info!("Starting!"); | ||
| 24 | |||
| 25 | let btn1 = Input::new(p.P0_23.degrade(), Pull::Up); | ||
| 26 | let btn2 = Input::new(p.P0_24.degrade(), Pull::Up); | ||
| 27 | let btn3 = Input::new(p.P0_08.degrade(), Pull::Up); | ||
| 28 | let btn4 = Input::new(p.P0_09.degrade(), Pull::Up); | ||
| 29 | |||
| 30 | unwrap!(spawner.spawn(button_task(1, btn1))); | ||
| 31 | unwrap!(spawner.spawn(button_task(2, btn2))); | ||
| 32 | unwrap!(spawner.spawn(button_task(3, btn3))); | ||
| 33 | unwrap!(spawner.spawn(button_task(4, btn4))); | ||
| 34 | } | ||
diff --git a/examples/nrf5340/src/bin/uart.rs b/examples/nrf5340/src/bin/uart.rs new file mode 100644 index 000000000..1db450576 --- /dev/null +++ b/examples/nrf5340/src/bin/uart.rs | |||
| @@ -0,0 +1,35 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | use defmt::*; | ||
| 6 | use embassy_executor::Spawner; | ||
| 7 | use embassy_nrf::{interrupt, uarte}; | ||
| 8 | use {defmt_rtt as _, panic_probe as _}; | ||
| 9 | |||
| 10 | #[embassy_executor::main] | ||
| 11 | async fn main(_spawner: Spawner) { | ||
| 12 | let p = embassy_nrf::init(Default::default()); | ||
| 13 | let mut config = uarte::Config::default(); | ||
| 14 | config.parity = uarte::Parity::EXCLUDED; | ||
| 15 | config.baudrate = uarte::Baudrate::BAUD115200; | ||
| 16 | |||
| 17 | let irq = interrupt::take!(SERIAL0); | ||
| 18 | let mut uart = uarte::Uarte::new(p.UARTETWISPI0, irq, p.P0_08, p.P0_06, config); | ||
| 19 | |||
| 20 | info!("uarte initialized!"); | ||
| 21 | |||
| 22 | // Message must be in SRAM | ||
| 23 | let mut buf = [0; 8]; | ||
| 24 | buf.copy_from_slice(b"Hello!\r\n"); | ||
| 25 | |||
| 26 | unwrap!(uart.write(&buf).await); | ||
| 27 | info!("wrote hello in uart!"); | ||
| 28 | |||
| 29 | loop { | ||
| 30 | info!("reading..."); | ||
| 31 | unwrap!(uart.read(&mut buf).await); | ||
| 32 | info!("writing..."); | ||
| 33 | unwrap!(uart.write(&buf).await); | ||
| 34 | } | ||
| 35 | } | ||
diff --git a/examples/nrf5340/src/bin/uart_idle.rs b/examples/nrf5340/src/bin/uart_idle.rs new file mode 100644 index 000000000..327fc4b23 --- /dev/null +++ b/examples/nrf5340/src/bin/uart_idle.rs | |||
| @@ -0,0 +1,35 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | use defmt::*; | ||
| 6 | use embassy_executor::Spawner; | ||
| 7 | use embassy_nrf::{interrupt, uarte}; | ||
| 8 | use {defmt_rtt as _, panic_probe as _}; | ||
| 9 | |||
| 10 | #[embassy_executor::main] | ||
| 11 | async fn main(_spawner: Spawner) { | ||
| 12 | let p = embassy_nrf::init(Default::default()); | ||
| 13 | let mut config = uarte::Config::default(); | ||
| 14 | config.parity = uarte::Parity::EXCLUDED; | ||
| 15 | config.baudrate = uarte::Baudrate::BAUD115200; | ||
| 16 | |||
| 17 | let irq = interrupt::take!(SERIAL0); | ||
| 18 | let uart = uarte::Uarte::new(p.UARTETWISPI0, irq, p.P0_08, p.P0_06, config); | ||
| 19 | let (mut tx, mut rx) = uart.split_with_idle(p.TIMER0, p.PPI_CH0, p.PPI_CH1); | ||
| 20 | |||
| 21 | info!("uarte initialized!"); | ||
| 22 | |||
| 23 | // Message must be in SRAM | ||
| 24 | let mut buf = [0; 8]; | ||
| 25 | buf.copy_from_slice(b"Hello!\r\n"); | ||
| 26 | |||
| 27 | unwrap!(tx.write(&buf).await); | ||
| 28 | info!("wrote hello in uart!"); | ||
| 29 | |||
| 30 | loop { | ||
| 31 | info!("reading..."); | ||
| 32 | let n = unwrap!(rx.read_until_idle(&mut buf).await); | ||
| 33 | info!("got {} bytes", n); | ||
| 34 | } | ||
| 35 | } | ||
diff --git a/examples/nrf5340/src/bin/uart_split.rs b/examples/nrf5340/src/bin/uart_split.rs new file mode 100644 index 000000000..1bff382fb --- /dev/null +++ b/examples/nrf5340/src/bin/uart_split.rs | |||
| @@ -0,0 +1,60 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | use defmt::*; | ||
| 6 | use embassy_executor::Spawner; | ||
| 7 | use embassy_nrf::peripherals::UARTETWISPI0; | ||
| 8 | use embassy_nrf::uarte::UarteRx; | ||
| 9 | use embassy_nrf::{interrupt, uarte}; | ||
| 10 | use embassy_sync::blocking_mutex::raw::ThreadModeRawMutex; | ||
| 11 | use embassy_sync::channel::Channel; | ||
| 12 | use {defmt_rtt as _, panic_probe as _}; | ||
| 13 | |||
| 14 | static CHANNEL: Channel<ThreadModeRawMutex, [u8; 8], 1> = Channel::new(); | ||
| 15 | |||
| 16 | #[embassy_executor::main] | ||
| 17 | async fn main(spawner: Spawner) { | ||
| 18 | let p = embassy_nrf::init(Default::default()); | ||
| 19 | let mut config = uarte::Config::default(); | ||
| 20 | config.parity = uarte::Parity::EXCLUDED; | ||
| 21 | config.baudrate = uarte::Baudrate::BAUD115200; | ||
| 22 | |||
| 23 | let irq = interrupt::take!(SERIAL0); | ||
| 24 | let uart = uarte::Uarte::new(p.UARTETWISPI0, irq, p.P0_08, p.P0_06, config); | ||
| 25 | let (mut tx, rx) = uart.split(); | ||
| 26 | |||
| 27 | info!("uarte initialized!"); | ||
| 28 | |||
| 29 | // Spawn a task responsible purely for reading | ||
| 30 | |||
| 31 | unwrap!(spawner.spawn(reader(rx))); | ||
| 32 | |||
| 33 | // Message must be in SRAM | ||
| 34 | { | ||
| 35 | let mut buf = [0; 23]; | ||
| 36 | buf.copy_from_slice(b"Type 8 chars to echo!\r\n"); | ||
| 37 | |||
| 38 | unwrap!(tx.write(&buf).await); | ||
| 39 | info!("wrote hello in uart!"); | ||
| 40 | } | ||
| 41 | |||
| 42 | // Continue reading in this main task and write | ||
| 43 | // back out the buffer we receive from the read | ||
| 44 | // task. | ||
| 45 | loop { | ||
| 46 | let buf = CHANNEL.recv().await; | ||
| 47 | info!("writing..."); | ||
| 48 | unwrap!(tx.write(&buf).await); | ||
| 49 | } | ||
| 50 | } | ||
| 51 | |||
| 52 | #[embassy_executor::task] | ||
| 53 | async fn reader(mut rx: UarteRx<'static, UARTETWISPI0>) { | ||
| 54 | let mut buf = [0; 8]; | ||
| 55 | loop { | ||
| 56 | info!("reading..."); | ||
| 57 | unwrap!(rx.read(&mut buf).await); | ||
| 58 | CHANNEL.send(buf).await; | ||
| 59 | } | ||
| 60 | } | ||
