aboutsummaryrefslogtreecommitdiff
path: root/examples/nrf52840/src
diff options
context:
space:
mode:
authorQuentin Smith <[email protected]>2023-07-17 21:31:43 -0400
committerQuentin Smith <[email protected]>2023-07-17 21:31:43 -0400
commit6f02403184eb7fb7990fb88fc9df9c4328a690a3 (patch)
tree748f510e190bb2724750507a6e69ed1a8e08cb20 /examples/nrf52840/src
parentd896f80405aa8963877049ed999e4aba25d6e2bb (diff)
parent6b5df4523aa1c4902f02e803450ae4b418e0e3ca (diff)
Merge remote-tracking branch 'origin/main' into nrf-pdm
Diffstat (limited to 'examples/nrf52840/src')
-rw-r--r--examples/nrf52840/src/bin/blinky.rs21
-rw-r--r--examples/nrf52840/src/bin/buffered_uart.rs54
-rw-r--r--examples/nrf52840/src/bin/channel.rs43
-rw-r--r--examples/nrf52840/src/bin/channel_sender_receiver.rs50
-rw-r--r--examples/nrf52840/src/bin/executor_fairness_test.rs43
-rw-r--r--examples/nrf52840/src/bin/gpiote_channel.rs66
-rw-r--r--examples/nrf52840/src/bin/gpiote_port.rs34
-rw-r--r--examples/nrf52840/src/bin/i2s_effect.rs116
-rw-r--r--examples/nrf52840/src/bin/i2s_monitor.rs118
-rw-r--r--examples/nrf52840/src/bin/i2s_waveform.rs154
-rw-r--r--examples/nrf52840/src/bin/lora_cad.rs99
-rw-r--r--examples/nrf52840/src/bin/lora_lorawan.rs83
-rw-r--r--examples/nrf52840/src/bin/lora_p2p_receive.rs121
-rw-r--r--examples/nrf52840/src/bin/lora_p2p_receive_duty_cycle.rs131
-rw-r--r--examples/nrf52840/src/bin/lora_p2p_send.rs104
-rw-r--r--examples/nrf52840/src/bin/manually_create_executor.rs49
-rw-r--r--examples/nrf52840/src/bin/multiprio.rs146
-rw-r--r--examples/nrf52840/src/bin/mutex.rs42
-rw-r--r--examples/nrf52840/src/bin/nvmc.rs43
-rw-r--r--examples/nrf52840/src/bin/pdm.rs55
-rw-r--r--examples/nrf52840/src/bin/pdm_continuous.rs80
-rw-r--r--examples/nrf52840/src/bin/ppi.rs73
-rw-r--r--examples/nrf52840/src/bin/pubsub.rs107
-rw-r--r--examples/nrf52840/src/bin/pwm.rs89
-rw-r--r--examples/nrf52840/src/bin/pwm_double_sequence.rs41
-rw-r--r--examples/nrf52840/src/bin/pwm_sequence.rs36
-rw-r--r--examples/nrf52840/src/bin/pwm_sequence_ppi.rs67
-rw-r--r--examples/nrf52840/src/bin/pwm_sequence_ws2812b.rs75
-rw-r--r--examples/nrf52840/src/bin/pwm_servo.rs47
-rw-r--r--examples/nrf52840/src/bin/qdec.rs27
-rw-r--r--examples/nrf52840/src/bin/qspi.rs82
-rw-r--r--examples/nrf52840/src/bin/qspi_lowpower.rs84
-rw-r--r--examples/nrf52840/src/bin/raw_spawn.rs52
-rw-r--r--examples/nrf52840/src/bin/rng.rs34
-rw-r--r--examples/nrf52840/src/bin/saadc.rs29
-rw-r--r--examples/nrf52840/src/bin/saadc_continuous.rs72
-rw-r--r--examples/nrf52840/src/bin/self_spawn.rs26
-rw-r--r--examples/nrf52840/src/bin/self_spawn_current_executor.rs22
-rw-r--r--examples/nrf52840/src/bin/spim.rs71
-rw-r--r--examples/nrf52840/src/bin/spis.rs30
-rw-r--r--examples/nrf52840/src/bin/temp.rs26
-rw-r--r--examples/nrf52840/src/bin/timer.rs31
-rw-r--r--examples/nrf52840/src/bin/twim.rs34
-rw-r--r--examples/nrf52840/src/bin/twim_lowpower.rs53
-rw-r--r--examples/nrf52840/src/bin/twis.rs48
-rw-r--r--examples/nrf52840/src/bin/uart.rs38
-rw-r--r--examples/nrf52840/src/bin/uart_idle.rs39
-rw-r--r--examples/nrf52840/src/bin/uart_split.rs63
-rw-r--r--examples/nrf52840/src/bin/usb_ethernet.rs165
-rw-r--r--examples/nrf52840/src/bin/usb_hid_keyboard.rs229
-rw-r--r--examples/nrf52840/src/bin/usb_hid_mouse.rs129
-rw-r--r--examples/nrf52840/src/bin/usb_serial.rs115
-rw-r--r--examples/nrf52840/src/bin/usb_serial_multitask.rs109
-rw-r--r--examples/nrf52840/src/bin/usb_serial_winusb.rs134
-rw-r--r--examples/nrf52840/src/bin/wdt.rs41
-rw-r--r--examples/nrf52840/src/bin/wifi_esp_hosted.rs143
56 files changed, 4113 insertions, 0 deletions
diff --git a/examples/nrf52840/src/bin/blinky.rs b/examples/nrf52840/src/bin/blinky.rs
new file mode 100644
index 000000000..513f6cd82
--- /dev/null
+++ b/examples/nrf52840/src/bin/blinky.rs
@@ -0,0 +1,21 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5use embassy_executor::Spawner;
6use embassy_nrf::gpio::{Level, Output, OutputDrive};
7use embassy_time::{Duration, Timer};
8use {defmt_rtt as _, panic_probe as _};
9
10#[embassy_executor::main]
11async fn main(_spawner: Spawner) {
12 let p = embassy_nrf::init(Default::default());
13 let mut led = Output::new(p.P0_13, 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/nrf52840/src/bin/buffered_uart.rs b/examples/nrf52840/src/bin/buffered_uart.rs
new file mode 100644
index 000000000..238695371
--- /dev/null
+++ b/examples/nrf52840/src/bin/buffered_uart.rs
@@ -0,0 +1,54 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5use defmt::*;
6use embassy_executor::Spawner;
7use embassy_nrf::buffered_uarte::{self, BufferedUarte};
8use embassy_nrf::{bind_interrupts, peripherals, uarte};
9use embedded_io::asynch::Write;
10use {defmt_rtt as _, panic_probe as _};
11
12bind_interrupts!(struct Irqs {
13 UARTE0_UART0 => buffered_uarte::InterruptHandler<peripherals::UARTE0>;
14});
15
16#[embassy_executor::main]
17async 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 mut tx_buffer = [0u8; 4096];
24 let mut rx_buffer = [0u8; 4096];
25
26 let mut u = BufferedUarte::new(
27 p.UARTE0,
28 p.TIMER0,
29 p.PPI_CH0,
30 p.PPI_CH1,
31 p.PPI_GROUP0,
32 Irqs,
33 p.P0_08,
34 p.P0_06,
35 config,
36 &mut rx_buffer,
37 &mut tx_buffer,
38 );
39
40 info!("uarte initialized!");
41
42 unwrap!(u.write_all(b"Hello!\r\n").await);
43 info!("wrote hello in uart!");
44
45 loop {
46 info!("reading...");
47 let buf = unwrap!(u.fill_buf().await);
48 info!("read done, got {}", buf);
49
50 // Read bytes have to be explicitly consumed, otherwise fill_buf() will return them again
51 let n = buf.len();
52 u.consume(n);
53 }
54}
diff --git a/examples/nrf52840/src/bin/channel.rs b/examples/nrf52840/src/bin/channel.rs
new file mode 100644
index 000000000..d782a79e7
--- /dev/null
+++ b/examples/nrf52840/src/bin/channel.rs
@@ -0,0 +1,43 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5use defmt::unwrap;
6use embassy_executor::Spawner;
7use embassy_nrf::gpio::{Level, Output, OutputDrive};
8use embassy_sync::blocking_mutex::raw::ThreadModeRawMutex;
9use embassy_sync::channel::Channel;
10use embassy_time::{Duration, Timer};
11use {defmt_rtt as _, panic_probe as _};
12
13enum LedState {
14 On,
15 Off,
16}
17
18static CHANNEL: Channel<ThreadModeRawMutex, LedState, 1> = Channel::new();
19
20#[embassy_executor::task]
21async 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]
31async fn main(spawner: Spawner) {
32 let p = embassy_nrf::init(Default::default());
33 let mut led = Output::new(p.P0_13, 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/nrf52840/src/bin/channel_sender_receiver.rs b/examples/nrf52840/src/bin/channel_sender_receiver.rs
new file mode 100644
index 000000000..fcccdaed5
--- /dev/null
+++ b/examples/nrf52840/src/bin/channel_sender_receiver.rs
@@ -0,0 +1,50 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5use defmt::unwrap;
6use embassy_executor::Spawner;
7use embassy_nrf::gpio::{AnyPin, Level, Output, OutputDrive, Pin};
8use embassy_sync::blocking_mutex::raw::NoopRawMutex;
9use embassy_sync::channel::{Channel, Receiver, Sender};
10use embassy_time::{Duration, Timer};
11use static_cell::StaticCell;
12use {defmt_rtt as _, panic_probe as _};
13
14enum LedState {
15 On,
16 Off,
17}
18
19static CHANNEL: StaticCell<Channel<NoopRawMutex, LedState, 1>> = StaticCell::new();
20
21#[embassy_executor::task]
22async 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]
32async 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]
44async 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_13.degrade(), channel.receiver())));
50}
diff --git a/examples/nrf52840/src/bin/executor_fairness_test.rs b/examples/nrf52840/src/bin/executor_fairness_test.rs
new file mode 100644
index 000000000..2a28f2763
--- /dev/null
+++ b/examples/nrf52840/src/bin/executor_fairness_test.rs
@@ -0,0 +1,43 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5use core::future::poll_fn;
6use core::task::Poll;
7
8use defmt::{info, unwrap};
9use embassy_executor::Spawner;
10use embassy_time::{Duration, Instant, Timer};
11use {defmt_rtt as _, panic_probe as _};
12
13#[embassy_executor::task]
14async fn run1() {
15 loop {
16 info!("DING DONG");
17 Timer::after(Duration::from_ticks(16000)).await;
18 }
19}
20
21#[embassy_executor::task]
22async fn run2() {
23 loop {
24 Timer::at(Instant::from_ticks(0)).await;
25 }
26}
27
28#[embassy_executor::task]
29async fn run3() {
30 poll_fn(|cx| {
31 cx.waker().wake_by_ref();
32 Poll::<()>::Pending
33 })
34 .await;
35}
36
37#[embassy_executor::main]
38async fn main(spawner: Spawner) {
39 let _p = embassy_nrf::init(Default::default());
40 unwrap!(spawner.spawn(run1()));
41 unwrap!(spawner.spawn(run2()));
42 unwrap!(spawner.spawn(run3()));
43}
diff --git a/examples/nrf52840/src/bin/gpiote_channel.rs b/examples/nrf52840/src/bin/gpiote_channel.rs
new file mode 100644
index 000000000..5bfd02465
--- /dev/null
+++ b/examples/nrf52840/src/bin/gpiote_channel.rs
@@ -0,0 +1,66 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5use defmt::info;
6use embassy_executor::Spawner;
7use embassy_nrf::gpio::{Input, Pull};
8use embassy_nrf::gpiote::{InputChannel, InputChannelPolarity};
9use {defmt_rtt as _, panic_probe as _};
10
11#[embassy_executor::main]
12async 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_11, Pull::Up),
19 InputChannelPolarity::HiToLo,
20 );
21 let ch2 = InputChannel::new(
22 p.GPIOTE_CH1,
23 Input::new(p.P0_12, Pull::Up),
24 InputChannelPolarity::LoToHi,
25 );
26 let ch3 = InputChannel::new(
27 p.GPIOTE_CH2,
28 Input::new(p.P0_24, Pull::Up),
29 InputChannelPolarity::Toggle,
30 );
31 let ch4 = InputChannel::new(
32 p.GPIOTE_CH3,
33 Input::new(p.P0_25, 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/nrf52840/src/bin/gpiote_port.rs b/examples/nrf52840/src/bin/gpiote_port.rs
new file mode 100644
index 000000000..0155d539e
--- /dev/null
+++ b/examples/nrf52840/src/bin/gpiote_port.rs
@@ -0,0 +1,34 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5use defmt::{info, unwrap};
6use embassy_executor::Spawner;
7use embassy_nrf::gpio::{AnyPin, Input, Pin as _, Pull};
8use {defmt_rtt as _, panic_probe as _};
9
10#[embassy_executor::task(pool_size = 4)]
11async 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]
21async fn main(spawner: Spawner) {
22 let p = embassy_nrf::init(Default::default());
23 info!("Starting!");
24
25 let btn1 = Input::new(p.P0_11.degrade(), Pull::Up);
26 let btn2 = Input::new(p.P0_12.degrade(), Pull::Up);
27 let btn3 = Input::new(p.P0_24.degrade(), Pull::Up);
28 let btn4 = Input::new(p.P0_25.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/nrf52840/src/bin/i2s_effect.rs b/examples/nrf52840/src/bin/i2s_effect.rs
new file mode 100644
index 000000000..391514d93
--- /dev/null
+++ b/examples/nrf52840/src/bin/i2s_effect.rs
@@ -0,0 +1,116 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5use core::f32::consts::PI;
6
7use defmt::{error, info};
8use embassy_executor::Spawner;
9use embassy_nrf::i2s::{self, Channels, Config, MasterClock, MultiBuffering, Sample as _, SampleWidth, I2S};
10use embassy_nrf::{bind_interrupts, peripherals};
11use {defmt_rtt as _, panic_probe as _};
12
13type Sample = i16;
14
15const NUM_BUFFERS: usize = 2;
16const NUM_SAMPLES: usize = 4;
17
18bind_interrupts!(struct Irqs {
19 I2S => i2s::InterruptHandler<peripherals::I2S>;
20});
21
22#[embassy_executor::main]
23async fn main(_spawner: Spawner) {
24 let p = embassy_nrf::init(Default::default());
25
26 let master_clock: MasterClock = i2s::ExactSampleRate::_50000.into();
27
28 let sample_rate = master_clock.sample_rate();
29 info!("Sample rate: {}", sample_rate);
30
31 let mut config = Config::default();
32 config.sample_width = SampleWidth::_16bit;
33 config.channels = Channels::MonoLeft;
34
35 let buffers_out = MultiBuffering::<Sample, NUM_BUFFERS, NUM_SAMPLES>::new();
36 let buffers_in = MultiBuffering::<Sample, NUM_BUFFERS, NUM_SAMPLES>::new();
37 let mut full_duplex_stream = I2S::new_master(p.I2S, Irqs, p.P0_25, p.P0_26, p.P0_27, master_clock, config)
38 .full_duplex(p.P0_29, p.P0_28, buffers_out, buffers_in);
39
40 let mut modulator = SineOsc::new();
41 modulator.set_frequency(8.0, 1.0 / sample_rate as f32);
42 modulator.set_amplitude(1.0);
43
44 full_duplex_stream.start().await.expect("I2S Start");
45
46 loop {
47 let (buff_out, buff_in) = full_duplex_stream.buffers();
48 for i in 0..NUM_SAMPLES {
49 let modulation = (Sample::SCALE as f32 * bipolar_to_unipolar(modulator.generate())) as Sample;
50 buff_out[i] = buff_in[i] * modulation;
51 }
52
53 if let Err(err) = full_duplex_stream.send_and_receive().await {
54 error!("{}", err);
55 }
56 }
57}
58
59struct SineOsc {
60 amplitude: f32,
61 modulo: f32,
62 phase_inc: f32,
63}
64
65impl SineOsc {
66 const B: f32 = 4.0 / PI;
67 const C: f32 = -4.0 / (PI * PI);
68 const P: f32 = 0.225;
69
70 pub fn new() -> Self {
71 Self {
72 amplitude: 1.0,
73 modulo: 0.0,
74 phase_inc: 0.0,
75 }
76 }
77
78 pub fn set_frequency(&mut self, freq: f32, inv_sample_rate: f32) {
79 self.phase_inc = freq * inv_sample_rate;
80 }
81
82 pub fn set_amplitude(&mut self, amplitude: f32) {
83 self.amplitude = amplitude;
84 }
85
86 pub fn generate(&mut self) -> f32 {
87 let signal = self.parabolic_sin(self.modulo);
88 self.modulo += self.phase_inc;
89 if self.modulo < 0.0 {
90 self.modulo += 1.0;
91 } else if self.modulo > 1.0 {
92 self.modulo -= 1.0;
93 }
94 signal * self.amplitude
95 }
96
97 fn parabolic_sin(&mut self, modulo: f32) -> f32 {
98 let angle = PI - modulo * 2.0 * PI;
99 let y = Self::B * angle + Self::C * angle * abs(angle);
100 Self::P * (y * abs(y) - y) + y
101 }
102}
103
104#[inline]
105fn abs(value: f32) -> f32 {
106 if value < 0.0 {
107 -value
108 } else {
109 value
110 }
111}
112
113#[inline]
114fn bipolar_to_unipolar(value: f32) -> f32 {
115 (value + 1.0) / 2.0
116}
diff --git a/examples/nrf52840/src/bin/i2s_monitor.rs b/examples/nrf52840/src/bin/i2s_monitor.rs
new file mode 100644
index 000000000..4ed597c0d
--- /dev/null
+++ b/examples/nrf52840/src/bin/i2s_monitor.rs
@@ -0,0 +1,118 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5use defmt::{debug, error, info};
6use embassy_executor::Spawner;
7use embassy_nrf::i2s::{self, Channels, Config, DoubleBuffering, MasterClock, Sample as _, SampleWidth, I2S};
8use embassy_nrf::pwm::{Prescaler, SimplePwm};
9use embassy_nrf::{bind_interrupts, peripherals};
10use {defmt_rtt as _, panic_probe as _};
11
12type Sample = i16;
13
14const NUM_SAMPLES: usize = 500;
15
16bind_interrupts!(struct Irqs {
17 I2S => i2s::InterruptHandler<peripherals::I2S>;
18});
19
20#[embassy_executor::main]
21async fn main(_spawner: Spawner) {
22 let p = embassy_nrf::init(Default::default());
23
24 let master_clock: MasterClock = i2s::ExactSampleRate::_50000.into();
25
26 let sample_rate = master_clock.sample_rate();
27 info!("Sample rate: {}", sample_rate);
28
29 let mut config = Config::default();
30 config.sample_width = SampleWidth::_16bit;
31 config.channels = Channels::MonoLeft;
32
33 let buffers = DoubleBuffering::<Sample, NUM_SAMPLES>::new();
34 let mut input_stream =
35 I2S::new_master(p.I2S, Irqs, p.P0_25, p.P0_26, p.P0_27, master_clock, config).input(p.P0_29, buffers);
36
37 // Configure the PWM to use the pins corresponding to the RGB leds
38 let mut pwm = SimplePwm::new_3ch(p.PWM0, p.P0_23, p.P0_22, p.P0_24);
39 pwm.set_prescaler(Prescaler::Div1);
40 pwm.set_max_duty(255);
41
42 let mut rms_online = RmsOnline::<NUM_SAMPLES>::default();
43
44 input_stream.start().await.expect("I2S Start");
45
46 loop {
47 let rms = rms_online.process(input_stream.buffer());
48 let rgb = rgb_from_rms(rms);
49
50 debug!("RMS: {}, RGB: {:?}", rms, rgb);
51 for i in 0..3 {
52 pwm.set_duty(i, rgb[i].into());
53 }
54
55 if let Err(err) = input_stream.receive().await {
56 error!("{}", err);
57 }
58 }
59}
60
61/// RMS from 0.0 until 0.75 will give green with a proportional intensity
62/// RMS from 0.75 until 0.9 will give a blend between orange and red proportionally to the intensity
63/// RMS above 0.9 will give a red with a proportional intensity
64fn rgb_from_rms(rms: f32) -> [u8; 3] {
65 if rms < 0.75 {
66 let intensity = rms / 0.75;
67 [0, (intensity * 165.0) as u8, 0]
68 } else if rms < 0.9 {
69 let intensity = (rms - 0.75) / 0.15;
70 [200, 165 - (165.0 * intensity) as u8, 0]
71 } else {
72 let intensity = (rms - 0.9) / 0.1;
73 [200 + (55.0 * intensity) as u8, 0, 0]
74 }
75}
76
77pub struct RmsOnline<const N: usize> {
78 pub squares: [f32; N],
79 pub head: usize,
80}
81
82impl<const N: usize> Default for RmsOnline<N> {
83 fn default() -> Self {
84 RmsOnline {
85 squares: [0.0; N],
86 head: 0,
87 }
88 }
89}
90
91impl<const N: usize> RmsOnline<N> {
92 pub fn reset(&mut self) {
93 self.squares = [0.0; N];
94 self.head = 0;
95 }
96
97 pub fn process(&mut self, buf: &[Sample]) -> f32 {
98 buf.iter()
99 .for_each(|sample| self.push(*sample as f32 / Sample::SCALE as f32));
100
101 let sum_of_squares = self.squares.iter().fold(0.0, |acc, v| acc + *v);
102 Self::approx_sqrt(sum_of_squares / N as f32)
103 }
104
105 pub fn push(&mut self, signal: f32) {
106 let square = signal * signal;
107 self.squares[self.head] = square;
108 self.head = (self.head + 1) % N;
109 }
110
111 /// Approximated sqrt taken from [micromath]
112 ///
113 /// [micromath]: https://docs.rs/micromath/latest/src/micromath/float/sqrt.rs.html#11-17
114 ///
115 fn approx_sqrt(value: f32) -> f32 {
116 f32::from_bits((value.to_bits() + 0x3f80_0000) >> 1)
117 }
118}
diff --git a/examples/nrf52840/src/bin/i2s_waveform.rs b/examples/nrf52840/src/bin/i2s_waveform.rs
new file mode 100644
index 000000000..f2c1166b1
--- /dev/null
+++ b/examples/nrf52840/src/bin/i2s_waveform.rs
@@ -0,0 +1,154 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5use core::f32::consts::PI;
6
7use defmt::{error, info};
8use embassy_executor::Spawner;
9use embassy_nrf::i2s::{self, Channels, Config, DoubleBuffering, MasterClock, Sample as _, SampleWidth, I2S};
10use embassy_nrf::{bind_interrupts, peripherals};
11use {defmt_rtt as _, panic_probe as _};
12
13type Sample = i16;
14
15const NUM_SAMPLES: usize = 50;
16
17bind_interrupts!(struct Irqs {
18 I2S => i2s::InterruptHandler<peripherals::I2S>;
19});
20
21#[embassy_executor::main]
22async fn main(_spawner: Spawner) {
23 let p = embassy_nrf::init(Default::default());
24
25 let master_clock: MasterClock = i2s::ExactSampleRate::_50000.into();
26
27 let sample_rate = master_clock.sample_rate();
28 info!("Sample rate: {}", sample_rate);
29
30 let mut config = Config::default();
31 config.sample_width = SampleWidth::_16bit;
32 config.channels = Channels::MonoLeft;
33
34 let buffers = DoubleBuffering::<Sample, NUM_SAMPLES>::new();
35 let mut output_stream =
36 I2S::new_master(p.I2S, Irqs, p.P0_25, p.P0_26, p.P0_27, master_clock, config).output(p.P0_28, buffers);
37
38 let mut waveform = Waveform::new(1.0 / sample_rate as f32);
39
40 waveform.process(output_stream.buffer());
41
42 output_stream.start().await.expect("I2S Start");
43
44 loop {
45 waveform.process(output_stream.buffer());
46
47 if let Err(err) = output_stream.send().await {
48 error!("{}", err);
49 }
50 }
51}
52
53struct Waveform {
54 inv_sample_rate: f32,
55 carrier: SineOsc,
56 freq_mod: SineOsc,
57 amp_mod: SineOsc,
58}
59
60impl Waveform {
61 fn new(inv_sample_rate: f32) -> Self {
62 let mut carrier = SineOsc::new();
63 carrier.set_frequency(110.0, inv_sample_rate);
64
65 let mut freq_mod = SineOsc::new();
66 freq_mod.set_frequency(1.0, inv_sample_rate);
67 freq_mod.set_amplitude(1.0);
68
69 let mut amp_mod = SineOsc::new();
70 amp_mod.set_frequency(16.0, inv_sample_rate);
71 amp_mod.set_amplitude(0.5);
72
73 Self {
74 inv_sample_rate,
75 carrier,
76 freq_mod,
77 amp_mod,
78 }
79 }
80
81 fn process(&mut self, buf: &mut [Sample]) {
82 for sample in buf.chunks_mut(1) {
83 let freq_modulation = bipolar_to_unipolar(self.freq_mod.generate());
84 self.carrier
85 .set_frequency(110.0 + 440.0 * freq_modulation, self.inv_sample_rate);
86
87 let amp_modulation = bipolar_to_unipolar(self.amp_mod.generate());
88 self.carrier.set_amplitude(amp_modulation);
89
90 let signal = self.carrier.generate();
91
92 sample[0] = (Sample::SCALE as f32 * signal) as Sample;
93 }
94 }
95}
96
97struct SineOsc {
98 amplitude: f32,
99 modulo: f32,
100 phase_inc: f32,
101}
102
103impl SineOsc {
104 const B: f32 = 4.0 / PI;
105 const C: f32 = -4.0 / (PI * PI);
106 const P: f32 = 0.225;
107
108 pub fn new() -> Self {
109 Self {
110 amplitude: 1.0,
111 modulo: 0.0,
112 phase_inc: 0.0,
113 }
114 }
115
116 pub fn set_frequency(&mut self, freq: f32, inv_sample_rate: f32) {
117 self.phase_inc = freq * inv_sample_rate;
118 }
119
120 pub fn set_amplitude(&mut self, amplitude: f32) {
121 self.amplitude = amplitude;
122 }
123
124 pub fn generate(&mut self) -> f32 {
125 let signal = self.parabolic_sin(self.modulo);
126 self.modulo += self.phase_inc;
127 if self.modulo < 0.0 {
128 self.modulo += 1.0;
129 } else if self.modulo > 1.0 {
130 self.modulo -= 1.0;
131 }
132 signal * self.amplitude
133 }
134
135 fn parabolic_sin(&mut self, modulo: f32) -> f32 {
136 let angle = PI - modulo * 2.0 * PI;
137 let y = Self::B * angle + Self::C * angle * abs(angle);
138 Self::P * (y * abs(y) - y) + y
139 }
140}
141
142#[inline]
143fn abs(value: f32) -> f32 {
144 if value < 0.0 {
145 -value
146 } else {
147 value
148 }
149}
150
151#[inline]
152fn bipolar_to_unipolar(value: f32) -> f32 {
153 (value + 1.0) / 2.0
154}
diff --git a/examples/nrf52840/src/bin/lora_cad.rs b/examples/nrf52840/src/bin/lora_cad.rs
new file mode 100644
index 000000000..beca061ed
--- /dev/null
+++ b/examples/nrf52840/src/bin/lora_cad.rs
@@ -0,0 +1,99 @@
1//! This example runs on the RAK4631 WisBlock, which has an nRF52840 MCU and Semtech Sx126x radio.
2//! Other nrf/sx126x combinations may work with appropriate pin modifications.
3//! It demonstrates LORA CAD functionality.
4#![no_std]
5#![no_main]
6#![macro_use]
7#![feature(type_alias_impl_trait)]
8
9use defmt::*;
10use embassy_executor::Spawner;
11use embassy_lora::iv::GenericSx126xInterfaceVariant;
12use embassy_nrf::gpio::{Input, Level, Output, OutputDrive, Pin as _, Pull};
13use embassy_nrf::{bind_interrupts, peripherals, spim};
14use embassy_time::{Delay, Duration, Timer};
15use lora_phy::mod_params::*;
16use lora_phy::sx1261_2::SX1261_2;
17use lora_phy::LoRa;
18use {defmt_rtt as _, panic_probe as _};
19
20const LORA_FREQUENCY_IN_HZ: u32 = 903_900_000; // warning: set this appropriately for the region
21
22bind_interrupts!(struct Irqs {
23 SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1 => spim::InterruptHandler<peripherals::TWISPI1>;
24});
25
26#[embassy_executor::main]
27async fn main(_spawner: Spawner) {
28 let p = embassy_nrf::init(Default::default());
29 let mut spi_config = spim::Config::default();
30 spi_config.frequency = spim::Frequency::M16;
31
32 let spim = spim::Spim::new(p.TWISPI1, Irqs, p.P1_11, p.P1_13, p.P1_12, spi_config);
33
34 let nss = Output::new(p.P1_10.degrade(), Level::High, OutputDrive::Standard);
35 let reset = Output::new(p.P1_06.degrade(), Level::High, OutputDrive::Standard);
36 let dio1 = Input::new(p.P1_15.degrade(), Pull::Down);
37 let busy = Input::new(p.P1_14.degrade(), Pull::Down);
38 let rf_switch_rx = Output::new(p.P1_05.degrade(), Level::Low, OutputDrive::Standard);
39 let rf_switch_tx = Output::new(p.P1_07.degrade(), Level::Low, OutputDrive::Standard);
40
41 let iv =
42 GenericSx126xInterfaceVariant::new(nss, reset, dio1, busy, Some(rf_switch_rx), Some(rf_switch_tx)).unwrap();
43
44 let mut delay = Delay;
45
46 let mut lora = {
47 match LoRa::new(SX1261_2::new(BoardType::Rak4631Sx1262, spim, iv), false, &mut delay).await {
48 Ok(l) => l,
49 Err(err) => {
50 info!("Radio error = {}", err);
51 return;
52 }
53 }
54 };
55
56 let mut debug_indicator = Output::new(p.P1_03, Level::Low, OutputDrive::Standard);
57 let mut start_indicator = Output::new(p.P1_04, Level::Low, OutputDrive::Standard);
58
59 start_indicator.set_high();
60 Timer::after(Duration::from_secs(5)).await;
61 start_indicator.set_low();
62
63 let mdltn_params = {
64 match lora.create_modulation_params(
65 SpreadingFactor::_10,
66 Bandwidth::_250KHz,
67 CodingRate::_4_8,
68 LORA_FREQUENCY_IN_HZ,
69 ) {
70 Ok(mp) => mp,
71 Err(err) => {
72 info!("Radio error = {}", err);
73 return;
74 }
75 }
76 };
77
78 match lora.prepare_for_cad(&mdltn_params, true).await {
79 Ok(()) => {}
80 Err(err) => {
81 info!("Radio error = {}", err);
82 return;
83 }
84 };
85
86 match lora.cad().await {
87 Ok(cad_activity_detected) => {
88 if cad_activity_detected {
89 info!("cad successful with activity detected")
90 } else {
91 info!("cad successful without activity detected")
92 }
93 debug_indicator.set_high();
94 Timer::after(Duration::from_secs(5)).await;
95 debug_indicator.set_low();
96 }
97 Err(err) => info!("cad unsuccessful = {}", err),
98 }
99}
diff --git a/examples/nrf52840/src/bin/lora_lorawan.rs b/examples/nrf52840/src/bin/lora_lorawan.rs
new file mode 100644
index 000000000..c953680c6
--- /dev/null
+++ b/examples/nrf52840/src/bin/lora_lorawan.rs
@@ -0,0 +1,83 @@
1//! This example runs on the RAK4631 WisBlock, which has an nRF52840 MCU and Semtech Sx126x radio.
2//! Other nrf/sx126x combinations may work with appropriate pin modifications.
3//! It demonstrates LoRaWAN join functionality.
4#![no_std]
5#![no_main]
6#![macro_use]
7#![feature(type_alias_impl_trait)]
8
9use defmt::*;
10use embassy_executor::Spawner;
11use embassy_lora::iv::GenericSx126xInterfaceVariant;
12use embassy_lora::LoraTimer;
13use embassy_nrf::gpio::{Input, Level, Output, OutputDrive, Pin as _, Pull};
14use embassy_nrf::rng::Rng;
15use embassy_nrf::{bind_interrupts, peripherals, rng, spim};
16use embassy_time::Delay;
17use lora_phy::mod_params::*;
18use lora_phy::sx1261_2::SX1261_2;
19use lora_phy::LoRa;
20use lorawan::default_crypto::DefaultFactory as Crypto;
21use lorawan_device::async_device::lora_radio::LoRaRadio;
22use lorawan_device::async_device::{region, Device, JoinMode};
23use {defmt_rtt as _, panic_probe as _};
24
25const LORAWAN_REGION: region::Region = region::Region::EU868; // warning: set this appropriately for the region
26
27bind_interrupts!(struct Irqs {
28 SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1 => spim::InterruptHandler<peripherals::TWISPI1>;
29 RNG => rng::InterruptHandler<peripherals::RNG>;
30});
31
32#[embassy_executor::main]
33async fn main(_spawner: Spawner) {
34 let p = embassy_nrf::init(Default::default());
35 let mut spi_config = spim::Config::default();
36 spi_config.frequency = spim::Frequency::M16;
37
38 let spim = spim::Spim::new(p.TWISPI1, Irqs, p.P1_11, p.P1_13, p.P1_12, spi_config);
39
40 let nss = Output::new(p.P1_10.degrade(), Level::High, OutputDrive::Standard);
41 let reset = Output::new(p.P1_06.degrade(), Level::High, OutputDrive::Standard);
42 let dio1 = Input::new(p.P1_15.degrade(), Pull::Down);
43 let busy = Input::new(p.P1_14.degrade(), Pull::Down);
44 let rf_switch_rx = Output::new(p.P1_05.degrade(), Level::Low, OutputDrive::Standard);
45 let rf_switch_tx = Output::new(p.P1_07.degrade(), Level::Low, OutputDrive::Standard);
46
47 let iv =
48 GenericSx126xInterfaceVariant::new(nss, reset, dio1, busy, Some(rf_switch_rx), Some(rf_switch_tx)).unwrap();
49
50 let mut delay = Delay;
51
52 let lora = {
53 match LoRa::new(SX1261_2::new(BoardType::Rak4631Sx1262, spim, iv), true, &mut delay).await {
54 Ok(l) => l,
55 Err(err) => {
56 info!("Radio error = {}", err);
57 return;
58 }
59 }
60 };
61
62 let radio = LoRaRadio::new(lora);
63 let region: region::Configuration = region::Configuration::new(LORAWAN_REGION);
64 let mut device: Device<_, Crypto, _, _> = Device::new(region, radio, LoraTimer::new(), Rng::new(p.RNG, Irqs));
65
66 defmt::info!("Joining LoRaWAN network");
67
68 // TODO: Adjust the EUI and Keys according to your network credentials
69 match device
70 .join(&JoinMode::OTAA {
71 deveui: [0, 0, 0, 0, 0, 0, 0, 0],
72 appeui: [0, 0, 0, 0, 0, 0, 0, 0],
73 appkey: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
74 })
75 .await
76 {
77 Ok(()) => defmt::info!("LoRaWAN network joined"),
78 Err(err) => {
79 info!("Radio error = {}", err);
80 return;
81 }
82 };
83}
diff --git a/examples/nrf52840/src/bin/lora_p2p_receive.rs b/examples/nrf52840/src/bin/lora_p2p_receive.rs
new file mode 100644
index 000000000..563fe42ec
--- /dev/null
+++ b/examples/nrf52840/src/bin/lora_p2p_receive.rs
@@ -0,0 +1,121 @@
1//! This example runs on the RAK4631 WisBlock, which has an nRF52840 MCU and Semtech Sx126x radio.
2//! Other nrf/sx126x combinations may work with appropriate pin modifications.
3//! It demonstrates LORA P2P receive functionality in conjunction with the lora_p2p_send example.
4#![no_std]
5#![no_main]
6#![macro_use]
7#![feature(type_alias_impl_trait)]
8
9use defmt::*;
10use embassy_executor::Spawner;
11use embassy_lora::iv::GenericSx126xInterfaceVariant;
12use embassy_nrf::gpio::{Input, Level, Output, OutputDrive, Pin as _, Pull};
13use embassy_nrf::{bind_interrupts, peripherals, spim};
14use embassy_time::{Delay, Duration, Timer};
15use lora_phy::mod_params::*;
16use lora_phy::sx1261_2::SX1261_2;
17use lora_phy::LoRa;
18use {defmt_rtt as _, panic_probe as _};
19
20const LORA_FREQUENCY_IN_HZ: u32 = 903_900_000; // warning: set this appropriately for the region
21
22bind_interrupts!(struct Irqs {
23 SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1 => spim::InterruptHandler<peripherals::TWISPI1>;
24});
25
26#[embassy_executor::main]
27async fn main(_spawner: Spawner) {
28 let p = embassy_nrf::init(Default::default());
29 let mut spi_config = spim::Config::default();
30 spi_config.frequency = spim::Frequency::M16;
31
32 let spim = spim::Spim::new(p.TWISPI1, Irqs, p.P1_11, p.P1_13, p.P1_12, spi_config);
33
34 let nss = Output::new(p.P1_10.degrade(), Level::High, OutputDrive::Standard);
35 let reset = Output::new(p.P1_06.degrade(), Level::High, OutputDrive::Standard);
36 let dio1 = Input::new(p.P1_15.degrade(), Pull::Down);
37 let busy = Input::new(p.P1_14.degrade(), Pull::Down);
38 let rf_switch_rx = Output::new(p.P1_05.degrade(), Level::Low, OutputDrive::Standard);
39 let rf_switch_tx = Output::new(p.P1_07.degrade(), Level::Low, OutputDrive::Standard);
40
41 let iv =
42 GenericSx126xInterfaceVariant::new(nss, reset, dio1, busy, Some(rf_switch_rx), Some(rf_switch_tx)).unwrap();
43
44 let mut delay = Delay;
45
46 let mut lora = {
47 match LoRa::new(SX1261_2::new(BoardType::Rak4631Sx1262, spim, iv), false, &mut delay).await {
48 Ok(l) => l,
49 Err(err) => {
50 info!("Radio error = {}", err);
51 return;
52 }
53 }
54 };
55
56 let mut debug_indicator = Output::new(p.P1_03, Level::Low, OutputDrive::Standard);
57 let mut start_indicator = Output::new(p.P1_04, Level::Low, OutputDrive::Standard);
58
59 start_indicator.set_high();
60 Timer::after(Duration::from_secs(5)).await;
61 start_indicator.set_low();
62
63 let mut receiving_buffer = [00u8; 100];
64
65 let mdltn_params = {
66 match lora.create_modulation_params(
67 SpreadingFactor::_10,
68 Bandwidth::_250KHz,
69 CodingRate::_4_8,
70 LORA_FREQUENCY_IN_HZ,
71 ) {
72 Ok(mp) => mp,
73 Err(err) => {
74 info!("Radio error = {}", err);
75 return;
76 }
77 }
78 };
79
80 let rx_pkt_params = {
81 match lora.create_rx_packet_params(4, false, receiving_buffer.len() as u8, true, false, &mdltn_params) {
82 Ok(pp) => pp,
83 Err(err) => {
84 info!("Radio error = {}", err);
85 return;
86 }
87 }
88 };
89
90 match lora
91 .prepare_for_rx(&mdltn_params, &rx_pkt_params, None, true, false, 0, 0x00ffffffu32)
92 .await
93 {
94 Ok(()) => {}
95 Err(err) => {
96 info!("Radio error = {}", err);
97 return;
98 }
99 };
100
101 loop {
102 receiving_buffer = [00u8; 100];
103 match lora.rx(&rx_pkt_params, &mut receiving_buffer).await {
104 Ok((received_len, _rx_pkt_status)) => {
105 if (received_len == 3)
106 && (receiving_buffer[0] == 0x01u8)
107 && (receiving_buffer[1] == 0x02u8)
108 && (receiving_buffer[2] == 0x03u8)
109 {
110 info!("rx successful");
111 debug_indicator.set_high();
112 Timer::after(Duration::from_secs(5)).await;
113 debug_indicator.set_low();
114 } else {
115 info!("rx unknown packet");
116 }
117 }
118 Err(err) => info!("rx unsuccessful = {}", err),
119 }
120 }
121}
diff --git a/examples/nrf52840/src/bin/lora_p2p_receive_duty_cycle.rs b/examples/nrf52840/src/bin/lora_p2p_receive_duty_cycle.rs
new file mode 100644
index 000000000..1fd8f61a2
--- /dev/null
+++ b/examples/nrf52840/src/bin/lora_p2p_receive_duty_cycle.rs
@@ -0,0 +1,131 @@
1//! This example runs on the RAK4631 WisBlock, which has an nRF52840 MCU and Semtech Sx126x radio.
2//! Other nrf/sx126x combinations may work with appropriate pin modifications.
3//! It demonstrates LoRa Rx duty cycle functionality in conjunction with the lora_p2p_send example.
4#![no_std]
5#![no_main]
6#![macro_use]
7#![feature(type_alias_impl_trait)]
8
9use defmt::*;
10use embassy_executor::Spawner;
11use embassy_lora::iv::GenericSx126xInterfaceVariant;
12use embassy_nrf::gpio::{Input, Level, Output, OutputDrive, Pin as _, Pull};
13use embassy_nrf::{bind_interrupts, peripherals, spim};
14use embassy_time::{Delay, Duration, Timer};
15use lora_phy::mod_params::*;
16use lora_phy::sx1261_2::SX1261_2;
17use lora_phy::LoRa;
18use {defmt_rtt as _, panic_probe as _};
19
20const LORA_FREQUENCY_IN_HZ: u32 = 903_900_000; // warning: set this appropriately for the region
21
22bind_interrupts!(struct Irqs {
23 SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1 => spim::InterruptHandler<peripherals::TWISPI1>;
24});
25
26#[embassy_executor::main]
27async fn main(_spawner: Spawner) {
28 let p = embassy_nrf::init(Default::default());
29 let mut spi_config = spim::Config::default();
30 spi_config.frequency = spim::Frequency::M16;
31
32 let spim = spim::Spim::new(p.TWISPI1, Irqs, p.P1_11, p.P1_13, p.P1_12, spi_config);
33
34 let nss = Output::new(p.P1_10.degrade(), Level::High, OutputDrive::Standard);
35 let reset = Output::new(p.P1_06.degrade(), Level::High, OutputDrive::Standard);
36 let dio1 = Input::new(p.P1_15.degrade(), Pull::Down);
37 let busy = Input::new(p.P1_14.degrade(), Pull::Down);
38 let rf_switch_rx = Output::new(p.P1_05.degrade(), Level::Low, OutputDrive::Standard);
39 let rf_switch_tx = Output::new(p.P1_07.degrade(), Level::Low, OutputDrive::Standard);
40
41 let iv =
42 GenericSx126xInterfaceVariant::new(nss, reset, dio1, busy, Some(rf_switch_rx), Some(rf_switch_tx)).unwrap();
43
44 let mut delay = Delay;
45
46 let mut lora = {
47 match LoRa::new(SX1261_2::new(BoardType::Rak4631Sx1262, spim, iv), false, &mut delay).await {
48 Ok(l) => l,
49 Err(err) => {
50 info!("Radio error = {}", err);
51 return;
52 }
53 }
54 };
55
56 let mut debug_indicator = Output::new(p.P1_03, Level::Low, OutputDrive::Standard);
57 let mut start_indicator = Output::new(p.P1_04, Level::Low, OutputDrive::Standard);
58
59 start_indicator.set_high();
60 Timer::after(Duration::from_secs(5)).await;
61 start_indicator.set_low();
62
63 let mut receiving_buffer = [00u8; 100];
64
65 let mdltn_params = {
66 match lora.create_modulation_params(
67 SpreadingFactor::_10,
68 Bandwidth::_250KHz,
69 CodingRate::_4_8,
70 LORA_FREQUENCY_IN_HZ,
71 ) {
72 Ok(mp) => mp,
73 Err(err) => {
74 info!("Radio error = {}", err);
75 return;
76 }
77 }
78 };
79
80 let rx_pkt_params = {
81 match lora.create_rx_packet_params(4, false, receiving_buffer.len() as u8, true, false, &mdltn_params) {
82 Ok(pp) => pp,
83 Err(err) => {
84 info!("Radio error = {}", err);
85 return;
86 }
87 }
88 };
89
90 // See "RM0453 Reference manual STM32WL5x advanced Arm®-based 32-bit MCUs with sub-GHz radio solution" for the best explanation of Rx duty cycle processing.
91 match lora
92 .prepare_for_rx(
93 &mdltn_params,
94 &rx_pkt_params,
95 Some(&DutyCycleParams {
96 rx_time: 300_000, // 300_000 units * 15.625 us/unit = 4.69 s
97 sleep_time: 200_000, // 200_000 units * 15.625 us/unit = 3.13 s
98 }),
99 false,
100 false,
101 0,
102 0,
103 )
104 .await
105 {
106 Ok(()) => {}
107 Err(err) => {
108 info!("Radio error = {}", err);
109 return;
110 }
111 };
112
113 receiving_buffer = [00u8; 100];
114 match lora.rx(&rx_pkt_params, &mut receiving_buffer).await {
115 Ok((received_len, _rx_pkt_status)) => {
116 if (received_len == 3)
117 && (receiving_buffer[0] == 0x01u8)
118 && (receiving_buffer[1] == 0x02u8)
119 && (receiving_buffer[2] == 0x03u8)
120 {
121 info!("rx successful");
122 debug_indicator.set_high();
123 Timer::after(Duration::from_secs(5)).await;
124 debug_indicator.set_low();
125 } else {
126 info!("rx unknown packet")
127 }
128 }
129 Err(err) => info!("rx unsuccessful = {}", err),
130 }
131}
diff --git a/examples/nrf52840/src/bin/lora_p2p_send.rs b/examples/nrf52840/src/bin/lora_p2p_send.rs
new file mode 100644
index 000000000..1c8bbc27a
--- /dev/null
+++ b/examples/nrf52840/src/bin/lora_p2p_send.rs
@@ -0,0 +1,104 @@
1//! This example runs on the RAK4631 WisBlock, which has an nRF52840 MCU and Semtech Sx126x radio.
2//! Other nrf/sx126x combinations may work with appropriate pin modifications.
3//! It demonstrates LORA P2P send functionality.
4#![no_std]
5#![no_main]
6#![macro_use]
7#![feature(type_alias_impl_trait)]
8
9use defmt::*;
10use embassy_executor::Spawner;
11use embassy_lora::iv::GenericSx126xInterfaceVariant;
12use embassy_nrf::gpio::{Input, Level, Output, OutputDrive, Pin as _, Pull};
13use embassy_nrf::{bind_interrupts, peripherals, spim};
14use embassy_time::Delay;
15use lora_phy::mod_params::*;
16use lora_phy::sx1261_2::SX1261_2;
17use lora_phy::LoRa;
18use {defmt_rtt as _, panic_probe as _};
19
20const LORA_FREQUENCY_IN_HZ: u32 = 903_900_000; // warning: set this appropriately for the region
21
22bind_interrupts!(struct Irqs {
23 SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1 => spim::InterruptHandler<peripherals::TWISPI1>;
24});
25
26#[embassy_executor::main]
27async fn main(_spawner: Spawner) {
28 let p = embassy_nrf::init(Default::default());
29 let mut spi_config = spim::Config::default();
30 spi_config.frequency = spim::Frequency::M16;
31
32 let spim = spim::Spim::new(p.TWISPI1, Irqs, p.P1_11, p.P1_13, p.P1_12, spi_config);
33
34 let nss = Output::new(p.P1_10.degrade(), Level::High, OutputDrive::Standard);
35 let reset = Output::new(p.P1_06.degrade(), Level::High, OutputDrive::Standard);
36 let dio1 = Input::new(p.P1_15.degrade(), Pull::Down);
37 let busy = Input::new(p.P1_14.degrade(), Pull::Down);
38 let rf_switch_rx = Output::new(p.P1_05.degrade(), Level::Low, OutputDrive::Standard);
39 let rf_switch_tx = Output::new(p.P1_07.degrade(), Level::Low, OutputDrive::Standard);
40
41 let iv =
42 GenericSx126xInterfaceVariant::new(nss, reset, dio1, busy, Some(rf_switch_rx), Some(rf_switch_tx)).unwrap();
43
44 let mut delay = Delay;
45
46 let mut lora = {
47 match LoRa::new(SX1261_2::new(BoardType::Rak4631Sx1262, spim, iv), false, &mut delay).await {
48 Ok(l) => l,
49 Err(err) => {
50 info!("Radio error = {}", err);
51 return;
52 }
53 }
54 };
55
56 let mdltn_params = {
57 match lora.create_modulation_params(
58 SpreadingFactor::_10,
59 Bandwidth::_250KHz,
60 CodingRate::_4_8,
61 LORA_FREQUENCY_IN_HZ,
62 ) {
63 Ok(mp) => mp,
64 Err(err) => {
65 info!("Radio error = {}", err);
66 return;
67 }
68 }
69 };
70
71 let mut tx_pkt_params = {
72 match lora.create_tx_packet_params(4, false, true, false, &mdltn_params) {
73 Ok(pp) => pp,
74 Err(err) => {
75 info!("Radio error = {}", err);
76 return;
77 }
78 }
79 };
80
81 match lora.prepare_for_tx(&mdltn_params, 20, false).await {
82 Ok(()) => {}
83 Err(err) => {
84 info!("Radio error = {}", err);
85 return;
86 }
87 };
88
89 let buffer = [0x01u8, 0x02u8, 0x03u8];
90 match lora.tx(&mdltn_params, &mut tx_pkt_params, &buffer, 0xffffff).await {
91 Ok(()) => {
92 info!("TX DONE");
93 }
94 Err(err) => {
95 info!("Radio error = {}", err);
96 return;
97 }
98 };
99
100 match lora.sleep(&mut delay).await {
101 Ok(()) => info!("Sleep successful"),
102 Err(err) => info!("Sleep unsuccessful = {}", err),
103 }
104}
diff --git a/examples/nrf52840/src/bin/manually_create_executor.rs b/examples/nrf52840/src/bin/manually_create_executor.rs
new file mode 100644
index 000000000..12ce660f9
--- /dev/null
+++ b/examples/nrf52840/src/bin/manually_create_executor.rs
@@ -0,0 +1,49 @@
1// This example showcases how to manually create an executor.
2// This is what the #[embassy::main] macro does behind the scenes.
3
4#![no_std]
5#![no_main]
6#![feature(type_alias_impl_trait)]
7
8use cortex_m_rt::entry;
9use defmt::{info, unwrap};
10use embassy_executor::Executor;
11use embassy_time::{Duration, Timer};
12use static_cell::StaticCell;
13use {defmt_rtt as _, panic_probe as _};
14
15#[embassy_executor::task]
16async fn run1() {
17 loop {
18 info!("BIG INFREQUENT TICK");
19 Timer::after(Duration::from_ticks(64000)).await;
20 }
21}
22
23#[embassy_executor::task]
24async fn run2() {
25 loop {
26 info!("tick");
27 Timer::after(Duration::from_ticks(13000)).await;
28 }
29}
30
31static EXECUTOR: StaticCell<Executor> = StaticCell::new();
32
33#[entry]
34fn main() -> ! {
35 info!("Hello World!");
36
37 let _p = embassy_nrf::init(Default::default());
38
39 // Create the executor and put it in a StaticCell, because `run` needs `&'static mut Executor`.
40 let executor = EXECUTOR.init(Executor::new());
41
42 // Run it.
43 // `run` calls the closure then runs the executor forever. It never returns.
44 executor.run(|spawner| {
45 // Here we get access to a spawner to spawn the initial tasks.
46 unwrap!(spawner.spawn(run1()));
47 unwrap!(spawner.spawn(run2()));
48 });
49}
diff --git a/examples/nrf52840/src/bin/multiprio.rs b/examples/nrf52840/src/bin/multiprio.rs
new file mode 100644
index 000000000..aab819117
--- /dev/null
+++ b/examples/nrf52840/src/bin/multiprio.rs
@@ -0,0 +1,146 @@
1//! This example showcases how to create multiple Executor instances to run tasks at
2//! different priority levels.
3//!
4//! Low priority executor runs in thread mode (not interrupt), and uses `sev` for signaling
5//! there's work in the queue, and `wfe` for waiting for work.
6//!
7//! Medium and high priority executors run in two interrupts with different priorities.
8//! Signaling work is done by pending the interrupt. No "waiting" needs to be done explicitly, since
9//! when there's work the interrupt will trigger and run the executor.
10//!
11//! Sample output below. Note that high priority ticks can interrupt everything else, and
12//! medium priority computations can interrupt low priority computations, making them to appear
13//! to take significantly longer time.
14//!
15//! ```not_rust
16//! [med] Starting long computation
17//! [med] done in 992 ms
18//! [high] tick!
19//! [low] Starting long computation
20//! [med] Starting long computation
21//! [high] tick!
22//! [high] tick!
23//! [med] done in 993 ms
24//! [med] Starting long computation
25//! [high] tick!
26//! [high] tick!
27//! [med] done in 993 ms
28//! [low] done in 3972 ms
29//! [med] Starting long computation
30//! [high] tick!
31//! [high] tick!
32//! [med] done in 993 ms
33//! ```
34//!
35//! For comparison, try changing the code so all 3 tasks get spawned on the low priority executor.
36//! You will get an output like the following. Note that no computation is ever interrupted.
37//!
38//! ```not_rust
39//! [high] tick!
40//! [med] Starting long computation
41//! [med] done in 496 ms
42//! [low] Starting long computation
43//! [low] done in 992 ms
44//! [med] Starting long computation
45//! [med] done in 496 ms
46//! [high] tick!
47//! [low] Starting long computation
48//! [low] done in 992 ms
49//! [high] tick!
50//! [med] Starting long computation
51//! [med] done in 496 ms
52//! [high] tick!
53//! ```
54//!
55
56#![no_std]
57#![no_main]
58#![feature(type_alias_impl_trait)]
59
60use cortex_m_rt::entry;
61use defmt::{info, unwrap};
62use embassy_executor::{Executor, InterruptExecutor};
63use embassy_nrf::interrupt;
64use embassy_nrf::interrupt::{InterruptExt, Priority};
65use embassy_time::{Duration, Instant, Timer};
66use static_cell::StaticCell;
67use {defmt_rtt as _, panic_probe as _};
68
69#[embassy_executor::task]
70async fn run_high() {
71 loop {
72 info!(" [high] tick!");
73 Timer::after(Duration::from_ticks(27374)).await;
74 }
75}
76
77#[embassy_executor::task]
78async fn run_med() {
79 loop {
80 let start = Instant::now();
81 info!(" [med] Starting long computation");
82
83 // Spin-wait to simulate a long CPU computation
84 cortex_m::asm::delay(32_000_000); // ~1 second
85
86 let end = Instant::now();
87 let ms = end.duration_since(start).as_ticks() / 33;
88 info!(" [med] done in {} ms", ms);
89
90 Timer::after(Duration::from_ticks(23421)).await;
91 }
92}
93
94#[embassy_executor::task]
95async fn run_low() {
96 loop {
97 let start = Instant::now();
98 info!("[low] Starting long computation");
99
100 // Spin-wait to simulate a long CPU computation
101 cortex_m::asm::delay(64_000_000); // ~2 seconds
102
103 let end = Instant::now();
104 let ms = end.duration_since(start).as_ticks() / 33;
105 info!("[low] done in {} ms", ms);
106
107 Timer::after(Duration::from_ticks(32983)).await;
108 }
109}
110
111static EXECUTOR_HIGH: InterruptExecutor = InterruptExecutor::new();
112static EXECUTOR_MED: InterruptExecutor = InterruptExecutor::new();
113static EXECUTOR_LOW: StaticCell<Executor> = StaticCell::new();
114
115#[interrupt]
116unsafe fn SWI1_EGU1() {
117 EXECUTOR_HIGH.on_interrupt()
118}
119
120#[interrupt]
121unsafe fn SWI0_EGU0() {
122 EXECUTOR_MED.on_interrupt()
123}
124
125#[entry]
126fn main() -> ! {
127 info!("Hello World!");
128
129 let _p = embassy_nrf::init(Default::default());
130
131 // High-priority executor: SWI1_EGU1, priority level 6
132 interrupt::SWI1_EGU1.set_priority(Priority::P6);
133 let spawner = EXECUTOR_HIGH.start(interrupt::SWI1_EGU1);
134 unwrap!(spawner.spawn(run_high()));
135
136 // Medium-priority executor: SWI0_EGU0, priority level 7
137 interrupt::SWI0_EGU0.set_priority(Priority::P7);
138 let spawner = EXECUTOR_MED.start(interrupt::SWI0_EGU0);
139 unwrap!(spawner.spawn(run_med()));
140
141 // Low priority executor: runs in thread mode, using WFE/SEV
142 let executor = EXECUTOR_LOW.init(Executor::new());
143 executor.run(|spawner| {
144 unwrap!(spawner.spawn(run_low()));
145 });
146}
diff --git a/examples/nrf52840/src/bin/mutex.rs b/examples/nrf52840/src/bin/mutex.rs
new file mode 100644
index 000000000..c402c6ba1
--- /dev/null
+++ b/examples/nrf52840/src/bin/mutex.rs
@@ -0,0 +1,42 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5use defmt::{info, unwrap};
6use embassy_executor::Spawner;
7use embassy_sync::blocking_mutex::raw::ThreadModeRawMutex;
8use embassy_sync::mutex::Mutex;
9use embassy_time::{Duration, Timer};
10use {defmt_rtt as _, panic_probe as _};
11
12static MUTEX: Mutex<ThreadModeRawMutex, u32> = Mutex::new(0);
13
14#[embassy_executor::task]
15async fn my_task() {
16 loop {
17 {
18 let mut m = MUTEX.lock().await;
19 info!("start long operation");
20 *m += 1000;
21
22 // Hold the mutex for a long time.
23 Timer::after(Duration::from_secs(1)).await;
24 info!("end long operation: count = {}", *m);
25 }
26
27 Timer::after(Duration::from_secs(1)).await;
28 }
29}
30
31#[embassy_executor::main]
32async fn main(spawner: Spawner) {
33 let _p = embassy_nrf::init(Default::default());
34 unwrap!(spawner.spawn(my_task()));
35
36 loop {
37 Timer::after(Duration::from_millis(300)).await;
38 let mut m = MUTEX.lock().await;
39 *m += 1;
40 info!("short operation: count = {}", *m);
41 }
42}
diff --git a/examples/nrf52840/src/bin/nvmc.rs b/examples/nrf52840/src/bin/nvmc.rs
new file mode 100644
index 000000000..31c6fe4b6
--- /dev/null
+++ b/examples/nrf52840/src/bin/nvmc.rs
@@ -0,0 +1,43 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5use defmt::{info, unwrap};
6use embassy_executor::Spawner;
7use embassy_nrf::nvmc::Nvmc;
8use embassy_time::{Duration, Timer};
9use embedded_storage::nor_flash::{NorFlash, ReadNorFlash};
10use {defmt_rtt as _, panic_probe as _};
11
12#[embassy_executor::main]
13async fn main(_spawner: Spawner) {
14 let p = embassy_nrf::init(Default::default());
15 info!("Hello NVMC!");
16
17 // probe-rs run breaks without this, I'm not sure why.
18 Timer::after(Duration::from_secs(1)).await;
19
20 let mut f = Nvmc::new(p.NVMC);
21 const ADDR: u32 = 0x80000;
22
23 info!("Reading...");
24 let mut buf = [0u8; 4];
25 unwrap!(f.read(ADDR, &mut buf));
26 info!("Read: {=[u8]:x}", buf);
27
28 info!("Erasing...");
29 unwrap!(f.erase(ADDR, ADDR + 4096));
30
31 info!("Reading...");
32 let mut buf = [0u8; 4];
33 unwrap!(f.read(ADDR, &mut buf));
34 info!("Read: {=[u8]:x}", buf);
35
36 info!("Writing...");
37 unwrap!(f.write(ADDR, &[1, 2, 3, 4]));
38
39 info!("Reading...");
40 let mut buf = [0u8; 4];
41 unwrap!(f.read(ADDR, &mut buf));
42 info!("Read: {=[u8]:x}", buf);
43}
diff --git a/examples/nrf52840/src/bin/pdm.rs b/examples/nrf52840/src/bin/pdm.rs
new file mode 100644
index 000000000..47fe67733
--- /dev/null
+++ b/examples/nrf52840/src/bin/pdm.rs
@@ -0,0 +1,55 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5use defmt::info;
6use embassy_executor::Spawner;
7use embassy_nrf::pdm::{self, Config, Pdm};
8use embassy_nrf::{bind_interrupts, peripherals};
9use embassy_time::{Duration, Timer};
10use fixed::types::I7F1;
11use num_integer::Roots;
12use {defmt_rtt as _, panic_probe as _};
13
14bind_interrupts!(struct Irqs {
15 PDM => pdm::InterruptHandler<peripherals::PDM>;
16});
17
18#[embassy_executor::main]
19async fn main(_p: Spawner) {
20 let p = embassy_nrf::init(Default::default());
21 let config = Config::default();
22 let mut pdm = Pdm::new(p.PDM, Irqs, p.P0_01, p.P0_00, config);
23
24 loop {
25 for gain in [I7F1::from_num(-20), I7F1::from_num(0), I7F1::from_num(20)] {
26 pdm.set_gain(gain, gain);
27 info!("Gain = {} dB", defmt::Debug2Format(&gain));
28 pdm.start().await;
29
30 // wait some time till the microphon settled
31 Timer::after(Duration::from_millis(1000)).await;
32
33 const SAMPLES: usize = 2048;
34 let mut buf = [0i16; SAMPLES];
35 pdm.sample(&mut buf).await.unwrap();
36
37 let mean = (buf.iter().map(|v| i32::from(*v)).sum::<i32>() / buf.len() as i32) as i16;
38 info!(
39 "{} samples, min {=i16}, max {=i16}, mean {=i16}, AC RMS {=i16}",
40 buf.len(),
41 buf.iter().min().unwrap(),
42 buf.iter().max().unwrap(),
43 mean,
44 (
45 buf.iter().map(|v| i32::from(*v - mean).pow(2)).fold(0i32, |a,b| a.saturating_add(b))
46 / buf.len() as i32).sqrt() as i16,
47 );
48
49 info!("samples: {:?}", &buf);
50
51 pdm.stop().await;
52 Timer::after(Duration::from_millis(100)).await;
53 }
54 }
55}
diff --git a/examples/nrf52840/src/bin/pdm_continuous.rs b/examples/nrf52840/src/bin/pdm_continuous.rs
new file mode 100644
index 000000000..9eaf30717
--- /dev/null
+++ b/examples/nrf52840/src/bin/pdm_continuous.rs
@@ -0,0 +1,80 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5use defmt::info;
6use core::cmp::Ordering;
7use embassy_executor::Spawner;
8use embassy_nrf::{bind_interrupts, peripherals};
9use embassy_nrf::pdm::{self, Config, OperationMode, Pdm, SamplerState, Frequency, Ratio};
10use fixed::types::I7F1;
11use num_integer::Roots;
12use microfft::real::rfft_1024;
13use {defmt_rtt as _, panic_probe as _};
14
15// Demonstrates both continuous sampling and scanning multiple channels driven by a PPI linked timer
16
17bind_interrupts!(struct Irqs {
18 PDM => pdm::InterruptHandler<peripherals::PDM>;
19});
20
21#[embassy_executor::main]
22async fn main(_p: Spawner) {
23 let mut p = embassy_nrf::init(Default::default());
24 let mut config = Config::default();
25 // Pins are correct for the onboard microphone on the Feather nRF52840 Sense.
26 config.frequency = Frequency::_1280K; // 16 kHz sample rate
27 config.ratio = Ratio::RATIO80;
28 config.operation_mode = OperationMode::Mono;
29 config.gain_left = I7F1::from_bits(5); // 2.5 dB
30 let mut pdm = Pdm::new(p.PDM, Irqs, &mut p.P0_00, &mut p.P0_01, config);
31
32 let mut bufs = [[0; 1024]; 2];
33
34 pdm
35 .run_task_sampler(
36 &mut bufs,
37 move |buf| {
38 // NOTE: It is important that the time spent within this callback
39 // does not exceed the time taken to acquire the 1500 samples we
40 // have in this example, which would be 10us + 2us per
41 // sample * 1500 = 18ms. You need to measure the time taken here
42 // and set the sample buffer size accordingly. Exceeding this
43 // time can lead to the peripheral re-writing the other buffer.
44 let mean = (buf.iter().map(|v| i32::from(*v)).sum::<i32>() / buf.len() as i32) as i16;
45 let (peak_freq_index, peak_mag) = fft_peak_freq(&buf);
46 let peak_freq = peak_freq_index * 16000 / buf.len();
47 info!(
48 "{} samples, min {=i16}, max {=i16}, mean {=i16}, AC RMS {=i16}, peak {} @ {} Hz",
49 buf.len(),
50 buf.iter().min().unwrap(),
51 buf.iter().max().unwrap(),
52 mean,
53 (
54 buf.iter().map(|v| i32::from(*v - mean).pow(2)).fold(0i32, |a,b| a.saturating_add(b))
55 / buf.len() as i32).sqrt() as i16,
56 peak_mag, peak_freq,
57 );
58 SamplerState::Sampled
59 },
60 )
61 .await.unwrap();
62}
63
64fn fft_peak_freq(input: &[i16; 1024]) -> (usize, u32) {
65 let mut f = [0f32; 1024];
66 for i in 0..input.len() {
67 f[i] = (input[i] as f32) / 32768.0;
68 }
69 // N.B. rfft_1024 does the FFT in-place so result is actually also a reference to f.
70 let result = rfft_1024(&mut f);
71 result[0].im = 0.0;
72
73 result
74 .iter()
75 .map(|c| c.norm_sqr())
76 .enumerate()
77 .max_by(|(_, a), (_, b)| a.partial_cmp(b).unwrap_or(Ordering::Equal))
78 .map(|(i, v)| (i, ((v*32768.0) as u32).sqrt()))
79 .unwrap()
80}
diff --git a/examples/nrf52840/src/bin/ppi.rs b/examples/nrf52840/src/bin/ppi.rs
new file mode 100644
index 000000000..d74ce4064
--- /dev/null
+++ b/examples/nrf52840/src/bin/ppi.rs
@@ -0,0 +1,73 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5use core::future::pending;
6
7use defmt::info;
8use embassy_executor::Spawner;
9use embassy_nrf::gpio::{Input, Level, Output, OutputDrive, Pull};
10use embassy_nrf::gpiote::{self, InputChannel, InputChannelPolarity};
11use embassy_nrf::ppi::Ppi;
12use gpiote::{OutputChannel, OutputChannelPolarity};
13use {defmt_rtt as _, panic_probe as _};
14
15#[embassy_executor::main]
16async fn main(_spawner: Spawner) {
17 let p = embassy_nrf::init(Default::default());
18 info!("Starting!");
19
20 let button1 = InputChannel::new(
21 p.GPIOTE_CH0,
22 Input::new(p.P0_11, Pull::Up),
23 InputChannelPolarity::HiToLo,
24 );
25 let button2 = InputChannel::new(
26 p.GPIOTE_CH1,
27 Input::new(p.P0_12, Pull::Up),
28 InputChannelPolarity::HiToLo,
29 );
30 let button3 = InputChannel::new(
31 p.GPIOTE_CH2,
32 Input::new(p.P0_24, Pull::Up),
33 InputChannelPolarity::HiToLo,
34 );
35 let button4 = InputChannel::new(
36 p.GPIOTE_CH3,
37 Input::new(p.P0_25, Pull::Up),
38 InputChannelPolarity::HiToLo,
39 );
40
41 let led1 = OutputChannel::new(
42 p.GPIOTE_CH4,
43 Output::new(p.P0_13, Level::Low, OutputDrive::Standard),
44 OutputChannelPolarity::Toggle,
45 );
46
47 let led2 = OutputChannel::new(
48 p.GPIOTE_CH5,
49 Output::new(p.P0_14, Level::Low, OutputDrive::Standard),
50 OutputChannelPolarity::Toggle,
51 );
52
53 let mut ppi = Ppi::new_one_to_one(p.PPI_CH0, button1.event_in(), led1.task_out());
54 ppi.enable();
55
56 let mut ppi = Ppi::new_one_to_one(p.PPI_CH1, button2.event_in(), led1.task_clr());
57 ppi.enable();
58
59 let mut ppi = Ppi::new_one_to_one(p.PPI_CH2, button3.event_in(), led1.task_set());
60 ppi.enable();
61
62 let mut ppi = Ppi::new_one_to_two(p.PPI_CH3, button4.event_in(), led1.task_out(), led2.task_out());
63 ppi.enable();
64
65 info!("PPI setup!");
66 info!("Press button 1 to toggle LED 1");
67 info!("Press button 2 to turn on LED 1");
68 info!("Press button 3 to turn off LED 1");
69 info!("Press button 4 to toggle LEDs 1 and 2");
70
71 // Block forever so the above drivers don't get dropped
72 pending::<()>().await;
73}
diff --git a/examples/nrf52840/src/bin/pubsub.rs b/examples/nrf52840/src/bin/pubsub.rs
new file mode 100644
index 000000000..cca60ebc9
--- /dev/null
+++ b/examples/nrf52840/src/bin/pubsub.rs
@@ -0,0 +1,107 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5use defmt::unwrap;
6use embassy_executor::Spawner;
7use embassy_sync::blocking_mutex::raw::ThreadModeRawMutex;
8use embassy_sync::pubsub::{DynSubscriber, PubSubChannel, Subscriber};
9use embassy_time::{Duration, Timer};
10use {defmt_rtt as _, panic_probe as _};
11
12/// Create the message bus. It has a queue of 4, supports 3 subscribers and 1 publisher
13static MESSAGE_BUS: PubSubChannel<ThreadModeRawMutex, Message, 4, 3, 1> = PubSubChannel::new();
14
15#[derive(Clone, defmt::Format)]
16enum Message {
17 A,
18 B,
19 C,
20}
21
22#[embassy_executor::main]
23async fn main(spawner: Spawner) {
24 let _p = embassy_nrf::init(Default::default());
25 defmt::info!("Hello World!");
26
27 // It's good to set up the subscribers before publishing anything.
28 // A subscriber will only yield messages that have been published after its creation.
29
30 spawner.must_spawn(fast_logger(unwrap!(MESSAGE_BUS.subscriber())));
31 spawner.must_spawn(slow_logger(unwrap!(MESSAGE_BUS.dyn_subscriber())));
32 spawner.must_spawn(slow_logger_pure(unwrap!(MESSAGE_BUS.dyn_subscriber())));
33
34 // Get a publisher
35 let message_publisher = unwrap!(MESSAGE_BUS.publisher());
36 // We can't get more (normal) publishers
37 // We can have an infinite amount of immediate publishers. They can't await a publish, only do an immediate publish
38 defmt::assert!(MESSAGE_BUS.publisher().is_err());
39
40 let mut index = 0;
41 loop {
42 Timer::after(Duration::from_millis(500)).await;
43
44 let message = match index % 3 {
45 0 => Message::A,
46 1 => Message::B,
47 2..=u32::MAX => Message::C,
48 };
49
50 // We publish immediately and don't await anything.
51 // If the queue is full, it will cause the oldest message to not be received by some/all subscribers
52 message_publisher.publish_immediate(message);
53
54 // Try to comment out the last one and uncomment this line below.
55 // The behaviour will change:
56 // - The subscribers won't miss any messages any more
57 // - Trying to publish now has some wait time when the queue is full
58
59 // message_publisher.publish(message).await;
60
61 index += 1;
62 }
63}
64
65/// A logger task that just awaits the messages it receives
66///
67/// This takes the generic `Subscriber`. This is most performant, but requires you to write down all of the generics
68#[embassy_executor::task]
69async fn fast_logger(mut messages: Subscriber<'static, ThreadModeRawMutex, Message, 4, 3, 1>) {
70 loop {
71 let message = messages.next_message().await;
72 defmt::info!("Received message at fast logger: {:?}", message);
73 }
74}
75
76/// A logger task that awaits the messages, but also does some other work.
77/// Because of this, depending on how the messages were published, the subscriber might miss some messages.
78///
79/// This takes the dynamic `DynSubscriber`. This is not as performant as the generic version, but let's you ignore some of the generics.
80#[embassy_executor::task]
81async fn slow_logger(mut messages: DynSubscriber<'static, Message>) {
82 loop {
83 // Do some work
84 Timer::after(Duration::from_millis(2000)).await;
85
86 // If the publisher has used the `publish_immediate` function, then we may receive a lag message here
87 let message = messages.next_message().await;
88 defmt::info!("Received message at slow logger: {:?}", message);
89
90 // If the previous one was a lag message, then we should receive the next message here immediately
91 let message = messages.next_message().await;
92 defmt::info!("Received message at slow logger: {:?}", message);
93 }
94}
95
96/// Same as `slow_logger` but it ignores lag results
97#[embassy_executor::task]
98async fn slow_logger_pure(mut messages: DynSubscriber<'static, Message>) {
99 loop {
100 // Do some work
101 Timer::after(Duration::from_millis(2000)).await;
102
103 // Instead of receiving lags here, we just ignore that and read the next message
104 let message = messages.next_message_pure().await;
105 defmt::info!("Received message at slow logger pure: {:?}", message);
106 }
107}
diff --git a/examples/nrf52840/src/bin/pwm.rs b/examples/nrf52840/src/bin/pwm.rs
new file mode 100644
index 000000000..1698c0bc8
--- /dev/null
+++ b/examples/nrf52840/src/bin/pwm.rs
@@ -0,0 +1,89 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5use defmt::*;
6use embassy_executor::Spawner;
7use embassy_nrf::pwm::{Prescaler, SimplePwm};
8use embassy_time::{Duration, Timer};
9use {defmt_rtt as _, panic_probe as _};
10
11// for i in range(1024): print(int((math.sin(i/512*math.pi)*0.4+0.5)**2*32767), ', ', end='')
12static DUTY: [u16; 1024] = [
13 8191, 8272, 8353, 8434, 8516, 8598, 8681, 8764, 8847, 8931, 9015, 9099, 9184, 9269, 9354, 9440, 9526, 9613, 9700,
14 9787, 9874, 9962, 10050, 10139, 10227, 10316, 10406, 10495, 10585, 10675, 10766, 10857, 10948, 11039, 11131, 11223,
15 11315, 11407, 11500, 11592, 11685, 11779, 11872, 11966, 12060, 12154, 12248, 12343, 12438, 12533, 12628, 12723,
16 12818, 12914, 13010, 13106, 13202, 13298, 13394, 13491, 13587, 13684, 13781, 13878, 13975, 14072, 14169, 14266,
17 14364, 14461, 14558, 14656, 14754, 14851, 14949, 15046, 15144, 15242, 15339, 15437, 15535, 15632, 15730, 15828,
18 15925, 16023, 16120, 16218, 16315, 16412, 16510, 16607, 16704, 16801, 16898, 16995, 17091, 17188, 17284, 17380,
19 17477, 17572, 17668, 17764, 17859, 17955, 18050, 18145, 18239, 18334, 18428, 18522, 18616, 18710, 18803, 18896,
20 18989, 19082, 19174, 19266, 19358, 19449, 19540, 19631, 19722, 19812, 19902, 19991, 20081, 20169, 20258, 20346,
21 20434, 20521, 20608, 20695, 20781, 20867, 20952, 21037, 21122, 21206, 21290, 21373, 21456, 21538, 21620, 21701,
22 21782, 21863, 21943, 22022, 22101, 22179, 22257, 22335, 22412, 22488, 22564, 22639, 22714, 22788, 22861, 22934,
23 23007, 23079, 23150, 23220, 23290, 23360, 23429, 23497, 23564, 23631, 23698, 23763, 23828, 23892, 23956, 24019,
24 24081, 24143, 24204, 24264, 24324, 24383, 24441, 24499, 24555, 24611, 24667, 24721, 24775, 24828, 24881, 24933,
25 24983, 25034, 25083, 25132, 25180, 25227, 25273, 25319, 25363, 25407, 25451, 25493, 25535, 25575, 25615, 25655,
26 25693, 25731, 25767, 25803, 25838, 25873, 25906, 25939, 25971, 26002, 26032, 26061, 26089, 26117, 26144, 26170,
27 26195, 26219, 26242, 26264, 26286, 26307, 26327, 26346, 26364, 26381, 26397, 26413, 26427, 26441, 26454, 26466,
28 26477, 26487, 26496, 26505, 26512, 26519, 26525, 26530, 26534, 26537, 26539, 26540, 26541, 26540, 26539, 26537,
29 26534, 26530, 26525, 26519, 26512, 26505, 26496, 26487, 26477, 26466, 26454, 26441, 26427, 26413, 26397, 26381,
30 26364, 26346, 26327, 26307, 26286, 26264, 26242, 26219, 26195, 26170, 26144, 26117, 26089, 26061, 26032, 26002,
31 25971, 25939, 25906, 25873, 25838, 25803, 25767, 25731, 25693, 25655, 25615, 25575, 25535, 25493, 25451, 25407,
32 25363, 25319, 25273, 25227, 25180, 25132, 25083, 25034, 24983, 24933, 24881, 24828, 24775, 24721, 24667, 24611,
33 24555, 24499, 24441, 24383, 24324, 24264, 24204, 24143, 24081, 24019, 23956, 23892, 23828, 23763, 23698, 23631,
34 23564, 23497, 23429, 23360, 23290, 23220, 23150, 23079, 23007, 22934, 22861, 22788, 22714, 22639, 22564, 22488,
35 22412, 22335, 22257, 22179, 22101, 22022, 21943, 21863, 21782, 21701, 21620, 21538, 21456, 21373, 21290, 21206,
36 21122, 21037, 20952, 20867, 20781, 20695, 20608, 20521, 20434, 20346, 20258, 20169, 20081, 19991, 19902, 19812,
37 19722, 19631, 19540, 19449, 19358, 19266, 19174, 19082, 18989, 18896, 18803, 18710, 18616, 18522, 18428, 18334,
38 18239, 18145, 18050, 17955, 17859, 17764, 17668, 17572, 17477, 17380, 17284, 17188, 17091, 16995, 16898, 16801,
39 16704, 16607, 16510, 16412, 16315, 16218, 16120, 16023, 15925, 15828, 15730, 15632, 15535, 15437, 15339, 15242,
40 15144, 15046, 14949, 14851, 14754, 14656, 14558, 14461, 14364, 14266, 14169, 14072, 13975, 13878, 13781, 13684,
41 13587, 13491, 13394, 13298, 13202, 13106, 13010, 12914, 12818, 12723, 12628, 12533, 12438, 12343, 12248, 12154,
42 12060, 11966, 11872, 11779, 11685, 11592, 11500, 11407, 11315, 11223, 11131, 11039, 10948, 10857, 10766, 10675,
43 10585, 10495, 10406, 10316, 10227, 10139, 10050, 9962, 9874, 9787, 9700, 9613, 9526, 9440, 9354, 9269, 9184, 9099,
44 9015, 8931, 8847, 8764, 8681, 8598, 8516, 8434, 8353, 8272, 8191, 8111, 8031, 7952, 7873, 7794, 7716, 7638, 7561,
45 7484, 7407, 7331, 7255, 7180, 7105, 7031, 6957, 6883, 6810, 6738, 6665, 6594, 6522, 6451, 6381, 6311, 6241, 6172,
46 6104, 6036, 5968, 5901, 5834, 5767, 5702, 5636, 5571, 5507, 5443, 5379, 5316, 5253, 5191, 5130, 5068, 5008, 4947,
47 4888, 4828, 4769, 4711, 4653, 4596, 4539, 4482, 4426, 4371, 4316, 4261, 4207, 4153, 4100, 4047, 3995, 3943, 3892,
48 3841, 3791, 3741, 3691, 3642, 3594, 3546, 3498, 3451, 3404, 3358, 3312, 3267, 3222, 3178, 3134, 3090, 3047, 3005,
49 2962, 2921, 2879, 2839, 2798, 2758, 2719, 2680, 2641, 2603, 2565, 2528, 2491, 2454, 2418, 2382, 2347, 2312, 2278,
50 2244, 2210, 2177, 2144, 2112, 2080, 2048, 2017, 1986, 1956, 1926, 1896, 1867, 1838, 1810, 1781, 1754, 1726, 1699,
51 1673, 1646, 1620, 1595, 1570, 1545, 1520, 1496, 1472, 1449, 1426, 1403, 1380, 1358, 1336, 1315, 1294, 1273, 1252,
52 1232, 1212, 1192, 1173, 1154, 1135, 1117, 1099, 1081, 1063, 1046, 1029, 1012, 996, 980, 964, 948, 933, 918, 903,
53 888, 874, 860, 846, 833, 819, 806, 793, 781, 768, 756, 744, 733, 721, 710, 699, 688, 677, 667, 657, 647, 637, 627,
54 618, 609, 599, 591, 582, 574, 565, 557, 549, 541, 534, 526, 519, 512, 505, 498, 492, 485, 479, 473, 467, 461, 455,
55 450, 444, 439, 434, 429, 424, 419, 415, 410, 406, 402, 398, 394, 390, 386, 383, 379, 376, 373, 370, 367, 364, 361,
56 359, 356, 354, 351, 349, 347, 345, 343, 342, 340, 338, 337, 336, 334, 333, 332, 331, 330, 330, 329, 328, 328, 328,
57 327, 327, 327, 327, 327, 328, 328, 328, 329, 330, 330, 331, 332, 333, 334, 336, 337, 338, 340, 342, 343, 345, 347,
58 349, 351, 354, 356, 359, 361, 364, 367, 370, 373, 376, 379, 383, 386, 390, 394, 398, 402, 406, 410, 415, 419, 424,
59 429, 434, 439, 444, 450, 455, 461, 467, 473, 479, 485, 492, 498, 505, 512, 519, 526, 534, 541, 549, 557, 565, 574,
60 582, 591, 599, 609, 618, 627, 637, 647, 657, 667, 677, 688, 699, 710, 721, 733, 744, 756, 768, 781, 793, 806, 819,
61 833, 846, 860, 874, 888, 903, 918, 933, 948, 964, 980, 996, 1012, 1029, 1046, 1063, 1081, 1099, 1117, 1135, 1154,
62 1173, 1192, 1212, 1232, 1252, 1273, 1294, 1315, 1336, 1358, 1380, 1403, 1426, 1449, 1472, 1496, 1520, 1545, 1570,
63 1595, 1620, 1646, 1673, 1699, 1726, 1754, 1781, 1810, 1838, 1867, 1896, 1926, 1956, 1986, 2017, 2048, 2080, 2112,
64 2144, 2177, 2210, 2244, 2278, 2312, 2347, 2382, 2418, 2454, 2491, 2528, 2565, 2603, 2641, 2680, 2719, 2758, 2798,
65 2839, 2879, 2921, 2962, 3005, 3047, 3090, 3134, 3178, 3222, 3267, 3312, 3358, 3404, 3451, 3498, 3546, 3594, 3642,
66 3691, 3741, 3791, 3841, 3892, 3943, 3995, 4047, 4100, 4153, 4207, 4261, 4316, 4371, 4426, 4482, 4539, 4596, 4653,
67 4711, 4769, 4828, 4888, 4947, 5008, 5068, 5130, 5191, 5253, 5316, 5379, 5443, 5507, 5571, 5636, 5702, 5767, 5834,
68 5901, 5968, 6036, 6104, 6172, 6241, 6311, 6381, 6451, 6522, 6594, 6665, 6738, 6810, 6883, 6957, 7031, 7105, 7180,
69 7255, 7331, 7407, 7484, 7561, 7638, 7716, 7794, 7873, 7952, 8031, 8111,
70];
71
72#[embassy_executor::main]
73async fn main(_spawner: Spawner) {
74 let p = embassy_nrf::init(Default::default());
75 let mut pwm = SimplePwm::new_4ch(p.PWM0, p.P0_13, p.P0_14, p.P0_16, p.P0_15);
76 pwm.set_prescaler(Prescaler::Div1);
77 pwm.set_max_duty(32767);
78 info!("pwm initialized!");
79
80 let mut i = 0;
81 loop {
82 i += 1;
83 pwm.set_duty(0, DUTY[i % 1024]);
84 pwm.set_duty(1, DUTY[(i + 256) % 1024]);
85 pwm.set_duty(2, DUTY[(i + 512) % 1024]);
86 pwm.set_duty(3, DUTY[(i + 768) % 1024]);
87 Timer::after(Duration::from_millis(3)).await;
88 }
89}
diff --git a/examples/nrf52840/src/bin/pwm_double_sequence.rs b/examples/nrf52840/src/bin/pwm_double_sequence.rs
new file mode 100644
index 000000000..16e50e909
--- /dev/null
+++ b/examples/nrf52840/src/bin/pwm_double_sequence.rs
@@ -0,0 +1,41 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5use defmt::*;
6use embassy_executor::Spawner;
7use embassy_nrf::pwm::{
8 Config, Prescaler, Sequence, SequenceConfig, SequenceMode, SequencePwm, Sequencer, StartSequence,
9};
10use embassy_time::{Duration, Timer};
11use {defmt_rtt as _, panic_probe as _};
12
13#[embassy_executor::main]
14async fn main(_spawner: Spawner) {
15 let p = embassy_nrf::init(Default::default());
16 let seq_words_0: [u16; 5] = [1000, 250, 100, 50, 0];
17 let seq_words_1: [u16; 4] = [50, 100, 250, 1000];
18
19 let mut config = Config::default();
20 config.prescaler = Prescaler::Div128;
21 // 1 period is 1000 * (128/16mhz = 0.000008s = 0.008ms) = 8us
22 // but say we want to hold the value for 5000ms
23 // so we want to repeat our value as many times as necessary until 5000ms passes
24 // want 5000/8 = 625 periods total to occur, so 624 (we get the one period for free remember)
25 let mut seq_config = SequenceConfig::default();
26 seq_config.refresh = 624;
27 // thus our sequence takes 5 * 5000ms or 25 seconds
28
29 let mut pwm = unwrap!(SequencePwm::new_1ch(p.PWM0, p.P0_13, config));
30
31 let sequence_0 = Sequence::new(&seq_words_0, seq_config.clone());
32 let sequence_1 = Sequence::new(&seq_words_1, seq_config);
33 let sequencer = Sequencer::new(&mut pwm, sequence_0, Some(sequence_1));
34 unwrap!(sequencer.start(StartSequence::Zero, SequenceMode::Loop(1)));
35
36 // we can abort a sequence if we need to before its complete with pwm.stop()
37 // or stop is also implicitly called when the pwm peripheral is dropped
38 // when it goes out of scope
39 Timer::after(Duration::from_millis(40000)).await;
40 info!("pwm stopped early!");
41}
diff --git a/examples/nrf52840/src/bin/pwm_sequence.rs b/examples/nrf52840/src/bin/pwm_sequence.rs
new file mode 100644
index 000000000..b9aca9aaa
--- /dev/null
+++ b/examples/nrf52840/src/bin/pwm_sequence.rs
@@ -0,0 +1,36 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5use defmt::*;
6use embassy_executor::Spawner;
7use embassy_nrf::pwm::{Config, Prescaler, SequenceConfig, SequencePwm, SingleSequenceMode, SingleSequencer};
8use embassy_time::{Duration, Timer};
9use {defmt_rtt as _, panic_probe as _};
10
11#[embassy_executor::main]
12async fn main(_spawner: Spawner) {
13 let p = embassy_nrf::init(Default::default());
14 let seq_words: [u16; 5] = [1000, 250, 100, 50, 0];
15
16 let mut config = Config::default();
17 config.prescaler = Prescaler::Div128;
18 // 1 period is 1000 * (128/16mhz = 0.000008s = 0.008ms) = 8us
19 // but say we want to hold the value for 5000ms
20 // so we want to repeat our value as many times as necessary until 5000ms passes
21 // want 5000/8 = 625 periods total to occur, so 624 (we get the one period for free remember)
22 let mut seq_config = SequenceConfig::default();
23 seq_config.refresh = 624;
24 // thus our sequence takes 5 * 5000ms or 25 seconds
25
26 let mut pwm = unwrap!(SequencePwm::new_1ch(p.PWM0, p.P0_13, config,));
27
28 let sequencer = SingleSequencer::new(&mut pwm, &seq_words, seq_config);
29 unwrap!(sequencer.start(SingleSequenceMode::Times(1)));
30
31 // we can abort a sequence if we need to before its complete with pwm.stop()
32 // or stop is also implicitly called when the pwm peripheral is dropped
33 // when it goes out of scope
34 Timer::after(Duration::from_millis(20000)).await;
35 info!("pwm stopped early!");
36}
diff --git a/examples/nrf52840/src/bin/pwm_sequence_ppi.rs b/examples/nrf52840/src/bin/pwm_sequence_ppi.rs
new file mode 100644
index 000000000..6594fa348
--- /dev/null
+++ b/examples/nrf52840/src/bin/pwm_sequence_ppi.rs
@@ -0,0 +1,67 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5use core::future::pending;
6
7use defmt::*;
8use embassy_executor::Spawner;
9use embassy_nrf::gpio::{Input, Pull};
10use embassy_nrf::gpiote::{InputChannel, InputChannelPolarity};
11use embassy_nrf::ppi::Ppi;
12use embassy_nrf::pwm::{Config, Prescaler, SequenceConfig, SequencePwm, SingleSequenceMode, SingleSequencer};
13use {defmt_rtt as _, panic_probe as _};
14
15#[embassy_executor::main]
16async fn main(_spawner: Spawner) {
17 let p = embassy_nrf::init(Default::default());
18 let seq_words: [u16; 5] = [1000, 250, 100, 50, 0];
19
20 let mut config = Config::default();
21 config.prescaler = Prescaler::Div128;
22 // 1 period is 1000 * (128/16mhz = 0.000008s = 0.008ms) = 8us
23 // but say we want to hold the value for 250ms 250ms/8 = 31.25 periods
24 // so round to 31 - 1 (we get the one period for free remember)
25 // thus our sequence takes 5 * 250ms or 1.25 seconds
26 let mut seq_config = SequenceConfig::default();
27 seq_config.refresh = 30;
28
29 let mut pwm = unwrap!(SequencePwm::new_1ch(p.PWM0, p.P0_13, config));
30
31 // pwm.stop() deconfigures pins, and then the task_start_seq0 task cant work
32 // so its going to have to start running in order load the configuration
33
34 let button1 = InputChannel::new(
35 p.GPIOTE_CH0,
36 Input::new(p.P0_11, Pull::Up),
37 InputChannelPolarity::HiToLo,
38 );
39
40 let button2 = InputChannel::new(
41 p.GPIOTE_CH1,
42 Input::new(p.P0_12, Pull::Up),
43 InputChannelPolarity::HiToLo,
44 );
45
46 // messing with the pwm tasks is ill advised
47 // Times::Ininite and Times even are seq0, Times odd is seq1
48 let start = unsafe { pwm.task_start_seq0() };
49 let stop = unsafe { pwm.task_stop() };
50
51 let sequencer = SingleSequencer::new(&mut pwm, &seq_words, seq_config);
52 unwrap!(sequencer.start(SingleSequenceMode::Infinite));
53
54 let mut ppi = Ppi::new_one_to_one(p.PPI_CH1, button1.event_in(), start);
55 ppi.enable();
56
57 let mut ppi2 = Ppi::new_one_to_one(p.PPI_CH0, button2.event_in(), stop);
58 ppi2.enable();
59
60 info!("PPI setup!");
61 info!("Press button 1 to start LED 1");
62 info!("Press button 2 to stop LED 1");
63 info!("Note! task_stop stops the sequence, but not the pin output");
64
65 // Block forever so the above drivers don't get dropped
66 pending::<()>().await;
67}
diff --git a/examples/nrf52840/src/bin/pwm_sequence_ws2812b.rs b/examples/nrf52840/src/bin/pwm_sequence_ws2812b.rs
new file mode 100644
index 000000000..711c8a17b
--- /dev/null
+++ b/examples/nrf52840/src/bin/pwm_sequence_ws2812b.rs
@@ -0,0 +1,75 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5use defmt::*;
6use embassy_executor::Spawner;
7use embassy_nrf::pwm::{
8 Config, Prescaler, SequenceConfig, SequenceLoad, SequencePwm, SingleSequenceMode, SingleSequencer,
9};
10use embassy_time::{Duration, Timer};
11use {defmt_rtt as _, panic_probe as _};
12
13// WS2812B LED light demonstration. Drives just one light.
14// The following reference on WS2812B may be of use:
15// https://cdn-shop.adafruit.com/datasheets/WS2812B.pdf.
16// This demo lights up a single LED in blue. It then proceeds
17// to pulsate the LED rapidly.
18
19// In the following declarations, setting the high bit tells the PWM
20// to reverse polarity, which is what the WS2812B expects.
21
22const T1H: u16 = 0x8000 | 13; // Duty = 13/20 ticks (0.8us/1.25us) for a 1
23const T0H: u16 = 0x8000 | 7; // Duty 7/20 ticks (0.4us/1.25us) for a 0
24const RES: u16 = 0x8000;
25
26// Provides data to a WS2812b (Neopixel) LED and makes it go blue. The data
27// line is assumed to be P1_05.
28#[embassy_executor::main]
29async fn main(_spawner: Spawner) {
30 let p = embassy_nrf::init(Default::default());
31 let mut config = Config::default();
32 config.sequence_load = SequenceLoad::Common;
33 config.prescaler = Prescaler::Div1;
34 config.max_duty = 20; // 1.25us (1s / 16Mhz * 20)
35 let mut pwm = unwrap!(SequencePwm::new_1ch(p.PWM0, p.P1_05, config));
36
37 // Declare the bits of 24 bits in a buffer we'll be
38 // mutating later.
39 let mut seq_words = [
40 T0H, T0H, T0H, T0H, T0H, T0H, T0H, T0H, // G
41 T0H, T0H, T0H, T0H, T0H, T0H, T0H, T0H, // R
42 T1H, T1H, T1H, T1H, T1H, T1H, T1H, T1H, // B
43 RES,
44 ];
45 let mut seq_config = SequenceConfig::default();
46 seq_config.end_delay = 799; // 50us (20 ticks * 40) - 1 tick because we've already got one RES;
47
48 let mut color_bit = 16;
49 let mut bit_value = T0H;
50
51 loop {
52 let sequences = SingleSequencer::new(&mut pwm, &seq_words, seq_config.clone());
53 unwrap!(sequences.start(SingleSequenceMode::Times(1)));
54
55 Timer::after(Duration::from_millis(50)).await;
56
57 if bit_value == T0H {
58 if color_bit == 20 {
59 bit_value = T1H;
60 } else {
61 color_bit += 1;
62 }
63 } else {
64 if color_bit == 16 {
65 bit_value = T0H;
66 } else {
67 color_bit -= 1;
68 }
69 }
70
71 drop(sequences);
72
73 seq_words[color_bit] = bit_value;
74 }
75}
diff --git a/examples/nrf52840/src/bin/pwm_servo.rs b/examples/nrf52840/src/bin/pwm_servo.rs
new file mode 100644
index 000000000..19228f433
--- /dev/null
+++ b/examples/nrf52840/src/bin/pwm_servo.rs
@@ -0,0 +1,47 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5use defmt::*;
6use embassy_executor::Spawner;
7use embassy_nrf::pwm::{Prescaler, SimplePwm};
8use embassy_time::{Duration, Timer};
9use {defmt_rtt as _, panic_probe as _};
10
11#[embassy_executor::main]
12async fn main(_spawner: Spawner) {
13 let p = embassy_nrf::init(Default::default());
14 let mut pwm = SimplePwm::new_1ch(p.PWM0, p.P0_05);
15 // sg90 microervo requires 50hz or 20ms period
16 // set_period can only set down to 125khz so we cant use it directly
17 // Div128 is 125khz or 0.000008s or 0.008ms, 20/0.008 is 2500 is top
18 pwm.set_prescaler(Prescaler::Div128);
19 pwm.set_max_duty(2500);
20 info!("pwm initialized!");
21
22 Timer::after(Duration::from_millis(5000)).await;
23
24 // 1ms 0deg (1/.008=125), 1.5ms 90deg (1.5/.008=187.5), 2ms 180deg (2/.008=250),
25 loop {
26 info!("45 deg");
27 // poor mans inverting, subtract our value from max_duty
28 pwm.set_duty(0, 2500 - 156);
29 Timer::after(Duration::from_millis(5000)).await;
30
31 info!("90 deg");
32 pwm.set_duty(0, 2500 - 187);
33 Timer::after(Duration::from_millis(5000)).await;
34
35 info!("135 deg");
36 pwm.set_duty(0, 2500 - 218);
37 Timer::after(Duration::from_millis(5000)).await;
38
39 info!("180 deg");
40 pwm.set_duty(0, 2500 - 250);
41 Timer::after(Duration::from_millis(5000)).await;
42
43 info!("0 deg");
44 pwm.set_duty(0, 2500 - 125);
45 Timer::after(Duration::from_millis(5000)).await;
46 }
47}
diff --git a/examples/nrf52840/src/bin/qdec.rs b/examples/nrf52840/src/bin/qdec.rs
new file mode 100644
index 000000000..59783d312
--- /dev/null
+++ b/examples/nrf52840/src/bin/qdec.rs
@@ -0,0 +1,27 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5use defmt::info;
6use embassy_executor::Spawner;
7use embassy_nrf::qdec::{self, Qdec};
8use embassy_nrf::{bind_interrupts, peripherals};
9use {defmt_rtt as _, panic_probe as _};
10
11bind_interrupts!(struct Irqs {
12 QDEC => qdec::InterruptHandler<peripherals::QDEC>;
13});
14
15#[embassy_executor::main]
16async fn main(_spawner: Spawner) {
17 let p = embassy_nrf::init(Default::default());
18 let config = qdec::Config::default();
19 let mut rotary_enc = Qdec::new(p.QDEC, Irqs, p.P0_31, p.P0_30, config);
20
21 info!("Turn rotary encoder!");
22 let mut value = 0;
23 loop {
24 value += rotary_enc.read().await;
25 info!("Value: {}", value);
26 }
27}
diff --git a/examples/nrf52840/src/bin/qspi.rs b/examples/nrf52840/src/bin/qspi.rs
new file mode 100644
index 000000000..9e8a01f4e
--- /dev/null
+++ b/examples/nrf52840/src/bin/qspi.rs
@@ -0,0 +1,82 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5use defmt::{assert_eq, info, unwrap};
6use embassy_executor::Spawner;
7use embassy_nrf::qspi::Frequency;
8use embassy_nrf::{bind_interrupts, peripherals, qspi};
9use {defmt_rtt as _, panic_probe as _};
10
11const PAGE_SIZE: usize = 4096;
12
13// Workaround for alignment requirements.
14// Nicer API will probably come in the future.
15#[repr(C, align(4))]
16struct AlignedBuf([u8; 4096]);
17
18bind_interrupts!(struct Irqs {
19 QSPI => qspi::InterruptHandler<peripherals::QSPI>;
20});
21
22#[embassy_executor::main]
23async fn main(_spawner: Spawner) {
24 let p = embassy_nrf::init(Default::default());
25 // Config for the MX25R64 present in the nRF52840 DK
26 let mut config = qspi::Config::default();
27 config.capacity = 8 * 1024 * 1024; // 8 MB
28 config.frequency = Frequency::M32;
29 config.read_opcode = qspi::ReadOpcode::READ4IO;
30 config.write_opcode = qspi::WriteOpcode::PP4IO;
31 config.write_page_size = qspi::WritePageSize::_256BYTES;
32
33 let mut q = qspi::Qspi::new(
34 p.QSPI, Irqs, p.P0_19, p.P0_17, p.P0_20, p.P0_21, p.P0_22, p.P0_23, config,
35 );
36
37 let mut id = [1; 3];
38 unwrap!(q.custom_instruction(0x9F, &[], &mut id).await);
39 info!("id: {}", id);
40
41 // Read status register
42 let mut status = [4; 1];
43 unwrap!(q.custom_instruction(0x05, &[], &mut status).await);
44
45 info!("status: {:?}", status[0]);
46
47 if status[0] & 0x40 == 0 {
48 status[0] |= 0x40;
49
50 unwrap!(q.custom_instruction(0x01, &status, &mut []).await);
51
52 info!("enabled quad in status");
53 }
54
55 let mut buf = AlignedBuf([0u8; PAGE_SIZE]);
56
57 let pattern = |a: u32| (a ^ (a >> 8) ^ (a >> 16) ^ (a >> 24)) as u8;
58
59 for i in 0..8 {
60 info!("page {:?}: erasing... ", i);
61 unwrap!(q.erase(i * PAGE_SIZE as u32).await);
62
63 for j in 0..PAGE_SIZE {
64 buf.0[j] = pattern((j as u32 + i * PAGE_SIZE as u32) as u32);
65 }
66
67 info!("programming...");
68 unwrap!(q.write(i * PAGE_SIZE as u32, &buf.0).await);
69 }
70
71 for i in 0..8 {
72 info!("page {:?}: reading... ", i);
73 unwrap!(q.read(i * PAGE_SIZE as u32, &mut buf.0).await);
74
75 info!("verifying...");
76 for j in 0..PAGE_SIZE {
77 assert_eq!(buf.0[j], pattern((j as u32 + i * PAGE_SIZE as u32) as u32));
78 }
79 }
80
81 info!("done!")
82}
diff --git a/examples/nrf52840/src/bin/qspi_lowpower.rs b/examples/nrf52840/src/bin/qspi_lowpower.rs
new file mode 100644
index 000000000..22a5c0c6d
--- /dev/null
+++ b/examples/nrf52840/src/bin/qspi_lowpower.rs
@@ -0,0 +1,84 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5use core::mem;
6
7use defmt::{info, unwrap};
8use embassy_executor::Spawner;
9use embassy_nrf::qspi::Frequency;
10use embassy_nrf::{bind_interrupts, peripherals, qspi};
11use embassy_time::{Duration, Timer};
12use {defmt_rtt as _, panic_probe as _};
13
14// Workaround for alignment requirements.
15// Nicer API will probably come in the future.
16#[repr(C, align(4))]
17struct AlignedBuf([u8; 64]);
18
19bind_interrupts!(struct Irqs {
20 QSPI => qspi::InterruptHandler<peripherals::QSPI>;
21});
22
23#[embassy_executor::main]
24async fn main(_p: Spawner) {
25 let mut p = embassy_nrf::init(Default::default());
26
27 loop {
28 // Config for the MX25R64 present in the nRF52840 DK
29 let mut config = qspi::Config::default();
30 config.capacity = 8 * 1024 * 1024; // 8 MB
31 config.frequency = Frequency::M32;
32 config.read_opcode = qspi::ReadOpcode::READ4IO;
33 config.write_opcode = qspi::WriteOpcode::PP4IO;
34 config.write_page_size = qspi::WritePageSize::_256BYTES;
35 config.deep_power_down = Some(qspi::DeepPowerDownConfig {
36 enter_time: 3, // tDP = 30uS
37 exit_time: 3, // tRDP = 35uS
38 });
39
40 let mut q = qspi::Qspi::new(
41 &mut p.QSPI,
42 Irqs,
43 &mut p.P0_19,
44 &mut p.P0_17,
45 &mut p.P0_20,
46 &mut p.P0_21,
47 &mut p.P0_22,
48 &mut p.P0_23,
49 config,
50 );
51
52 let mut id = [1; 3];
53 unwrap!(q.custom_instruction(0x9F, &[], &mut id).await);
54 info!("id: {}", id);
55
56 // Read status register
57 let mut status = [4; 1];
58 unwrap!(q.custom_instruction(0x05, &[], &mut status).await);
59
60 info!("status: {:?}", status[0]);
61
62 if status[0] & 0x40 == 0 {
63 status[0] |= 0x40;
64
65 unwrap!(q.custom_instruction(0x01, &status, &mut []).await);
66
67 info!("enabled quad in status");
68 }
69
70 let mut buf = AlignedBuf([0u8; 64]);
71
72 info!("reading...");
73 unwrap!(q.read(0, &mut buf.0).await);
74 info!("read: {=[u8]:x}", buf.0);
75
76 // Drop the QSPI instance. This disables the peripehral and deconfigures the pins.
77 // This clears the borrow on the singletons, so they can now be used again.
78 mem::drop(q);
79
80 // Sleep for 1 second. The executor ensures the core sleeps with a WFE when it has nothing to do.
81 // During this sleep, the nRF chip should only use ~3uA
82 Timer::after(Duration::from_secs(1)).await;
83 }
84}
diff --git a/examples/nrf52840/src/bin/raw_spawn.rs b/examples/nrf52840/src/bin/raw_spawn.rs
new file mode 100644
index 000000000..1b067f5e4
--- /dev/null
+++ b/examples/nrf52840/src/bin/raw_spawn.rs
@@ -0,0 +1,52 @@
1#![no_std]
2#![no_main]
3
4use core::mem;
5
6use cortex_m_rt::entry;
7use defmt::{info, unwrap};
8use embassy_executor::raw::TaskStorage;
9use embassy_executor::Executor;
10use embassy_time::{Duration, Timer};
11use static_cell::StaticCell;
12use {defmt_rtt as _, panic_probe as _};
13
14async fn run1() {
15 loop {
16 info!("BIG INFREQUENT TICK");
17 Timer::after(Duration::from_ticks(64000)).await;
18 }
19}
20
21async fn run2() {
22 loop {
23 info!("tick");
24 Timer::after(Duration::from_ticks(13000)).await;
25 }
26}
27
28static EXECUTOR: StaticCell<Executor> = StaticCell::new();
29
30#[entry]
31fn main() -> ! {
32 info!("Hello World!");
33
34 let _p = embassy_nrf::init(Default::default());
35 let executor = EXECUTOR.init(Executor::new());
36
37 let run1_task = TaskStorage::new();
38 let run2_task = TaskStorage::new();
39
40 // Safety: these variables do live forever if main never returns.
41 let run1_task = unsafe { make_static(&run1_task) };
42 let run2_task = unsafe { make_static(&run2_task) };
43
44 executor.run(|spawner| {
45 unwrap!(spawner.spawn(run1_task.spawn(|| run1())));
46 unwrap!(spawner.spawn(run2_task.spawn(|| run2())));
47 });
48}
49
50unsafe fn make_static<T>(t: &T) -> &'static T {
51 mem::transmute(t)
52}
diff --git a/examples/nrf52840/src/bin/rng.rs b/examples/nrf52840/src/bin/rng.rs
new file mode 100644
index 000000000..855743f50
--- /dev/null
+++ b/examples/nrf52840/src/bin/rng.rs
@@ -0,0 +1,34 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5use embassy_executor::Spawner;
6use embassy_nrf::rng::Rng;
7use embassy_nrf::{bind_interrupts, peripherals, rng};
8use rand::Rng as _;
9use {defmt_rtt as _, panic_probe as _};
10
11bind_interrupts!(struct Irqs {
12 RNG => rng::InterruptHandler<peripherals::RNG>;
13});
14
15#[embassy_executor::main]
16async fn main(_spawner: Spawner) {
17 let p = embassy_nrf::init(Default::default());
18 let mut rng = Rng::new(p.RNG, Irqs);
19
20 // Async API
21 let mut bytes = [0; 4];
22 rng.fill_bytes(&mut bytes).await;
23 defmt::info!("Some random bytes: {:?}", bytes);
24
25 // Sync API with `rand`
26 defmt::info!("A random number from 1 to 10: {:?}", rng.gen_range(1..=10));
27
28 let mut bytes = [0; 1024];
29 rng.fill_bytes(&mut bytes).await;
30 let zero_count: u32 = bytes.iter().fold(0, |acc, val| acc + val.count_zeros());
31 let one_count: u32 = bytes.iter().fold(0, |acc, val| acc + val.count_ones());
32 defmt::info!("Chance of zero: {}%", zero_count * 100 / (bytes.len() as u32 * 8));
33 defmt::info!("Chance of one: {}%", one_count * 100 / (bytes.len() as u32 * 8));
34}
diff --git a/examples/nrf52840/src/bin/saadc.rs b/examples/nrf52840/src/bin/saadc.rs
new file mode 100644
index 000000000..ffd9a7f4b
--- /dev/null
+++ b/examples/nrf52840/src/bin/saadc.rs
@@ -0,0 +1,29 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5use defmt::info;
6use embassy_executor::Spawner;
7use embassy_nrf::saadc::{ChannelConfig, Config, Saadc};
8use embassy_nrf::{bind_interrupts, saadc};
9use embassy_time::{Duration, Timer};
10use {defmt_rtt as _, panic_probe as _};
11
12bind_interrupts!(struct Irqs {
13 SAADC => saadc::InterruptHandler;
14});
15
16#[embassy_executor::main]
17async fn main(_p: Spawner) {
18 let mut p = embassy_nrf::init(Default::default());
19 let config = Config::default();
20 let channel_config = ChannelConfig::single_ended(&mut p.P0_02);
21 let mut saadc = Saadc::new(p.SAADC, Irqs, config, [channel_config]);
22
23 loop {
24 let mut buf = [0; 1];
25 saadc.sample(&mut buf).await;
26 info!("sample: {=i16}", &buf[0]);
27 Timer::after(Duration::from_millis(100)).await;
28 }
29}
diff --git a/examples/nrf52840/src/bin/saadc_continuous.rs b/examples/nrf52840/src/bin/saadc_continuous.rs
new file mode 100644
index 000000000..a25e17465
--- /dev/null
+++ b/examples/nrf52840/src/bin/saadc_continuous.rs
@@ -0,0 +1,72 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5use defmt::info;
6use embassy_executor::Spawner;
7use embassy_nrf::saadc::{CallbackResult, ChannelConfig, Config, Saadc};
8use embassy_nrf::timer::Frequency;
9use embassy_nrf::{bind_interrupts, saadc};
10use embassy_time::Duration;
11use {defmt_rtt as _, panic_probe as _};
12
13// Demonstrates both continuous sampling and scanning multiple channels driven by a PPI linked timer
14
15bind_interrupts!(struct Irqs {
16 SAADC => saadc::InterruptHandler;
17});
18
19#[embassy_executor::main]
20async fn main(_p: Spawner) {
21 let mut p = embassy_nrf::init(Default::default());
22 let config = Config::default();
23 let channel_1_config = ChannelConfig::single_ended(&mut p.P0_02);
24 let channel_2_config = ChannelConfig::single_ended(&mut p.P0_03);
25 let channel_3_config = ChannelConfig::single_ended(&mut p.P0_04);
26 let mut saadc = Saadc::new(
27 p.SAADC,
28 Irqs,
29 config,
30 [channel_1_config, channel_2_config, channel_3_config],
31 );
32
33 // This delay demonstrates that starting the timer prior to running
34 // the task sampler is benign given the calibration that follows.
35 embassy_time::Timer::after(Duration::from_millis(500)).await;
36 saadc.calibrate().await;
37
38 let mut bufs = [[[0; 3]; 500]; 2];
39
40 let mut c = 0;
41 let mut a: i32 = 0;
42
43 saadc
44 .run_task_sampler(
45 &mut p.TIMER0,
46 &mut p.PPI_CH0,
47 &mut p.PPI_CH1,
48 Frequency::F1MHz,
49 1000, // We want to sample at 1KHz
50 &mut bufs,
51 move |buf| {
52 // NOTE: It is important that the time spent within this callback
53 // does not exceed the time taken to acquire the 1500 samples we
54 // have in this example, which would be 10us + 2us per
55 // sample * 1500 = 18ms. You need to measure the time taken here
56 // and set the sample buffer size accordingly. Exceeding this
57 // time can lead to the peripheral re-writing the other buffer.
58 for b in buf {
59 a += b[0] as i32;
60 }
61 c += buf.len();
62 if c > 1000 {
63 a = a / c as i32;
64 info!("channel 1: {=i32}", a);
65 c = 0;
66 a = 0;
67 }
68 CallbackResult::Continue
69 },
70 )
71 .await;
72}
diff --git a/examples/nrf52840/src/bin/self_spawn.rs b/examples/nrf52840/src/bin/self_spawn.rs
new file mode 100644
index 000000000..31ea6c81e
--- /dev/null
+++ b/examples/nrf52840/src/bin/self_spawn.rs
@@ -0,0 +1,26 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5use defmt::{info, unwrap};
6use embassy_executor::Spawner;
7use embassy_time::{Duration, Timer};
8use {defmt_rtt as _, panic_probe as _};
9
10mod config {
11 pub const MY_TASK_POOL_SIZE: usize = 2;
12}
13
14#[embassy_executor::task(pool_size = config::MY_TASK_POOL_SIZE)]
15async fn my_task(spawner: Spawner, n: u32) {
16 Timer::after(Duration::from_secs(1)).await;
17 info!("Spawning self! {}", n);
18 unwrap!(spawner.spawn(my_task(spawner, n + 1)));
19}
20
21#[embassy_executor::main]
22async fn main(spawner: Spawner) {
23 let _p = embassy_nrf::init(Default::default());
24 info!("Hello World!");
25 unwrap!(spawner.spawn(my_task(spawner, 0)));
26}
diff --git a/examples/nrf52840/src/bin/self_spawn_current_executor.rs b/examples/nrf52840/src/bin/self_spawn_current_executor.rs
new file mode 100644
index 000000000..8a179886c
--- /dev/null
+++ b/examples/nrf52840/src/bin/self_spawn_current_executor.rs
@@ -0,0 +1,22 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5use defmt::{info, unwrap};
6use embassy_executor::Spawner;
7use embassy_time::{Duration, Timer};
8use {defmt_rtt as _, panic_probe as _};
9
10#[embassy_executor::task(pool_size = 2)]
11async fn my_task(n: u32) {
12 Timer::after(Duration::from_secs(1)).await;
13 info!("Spawning self! {}", n);
14 unwrap!(Spawner::for_current_executor().await.spawn(my_task(n + 1)));
15}
16
17#[embassy_executor::main]
18async fn main(spawner: Spawner) {
19 let _p = embassy_nrf::init(Default::default());
20 info!("Hello World!");
21 unwrap!(spawner.spawn(my_task(0)));
22}
diff --git a/examples/nrf52840/src/bin/spim.rs b/examples/nrf52840/src/bin/spim.rs
new file mode 100644
index 000000000..9d1843a8f
--- /dev/null
+++ b/examples/nrf52840/src/bin/spim.rs
@@ -0,0 +1,71 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5use defmt::{info, unwrap};
6use embassy_executor::Spawner;
7use embassy_nrf::gpio::{Level, Output, OutputDrive};
8use embassy_nrf::{bind_interrupts, peripherals, spim};
9use {defmt_rtt as _, panic_probe as _};
10
11bind_interrupts!(struct Irqs {
12 SPIM3 => spim::InterruptHandler<peripherals::SPI3>;
13});
14
15#[embassy_executor::main]
16async fn main(_spawner: Spawner) {
17 let p = embassy_nrf::init(Default::default());
18 info!("running!");
19
20 let mut config = spim::Config::default();
21 config.frequency = spim::Frequency::M16;
22
23 let mut spim = spim::Spim::new(p.SPI3, Irqs, p.P0_29, p.P0_28, p.P0_30, config);
24
25 let mut ncs = Output::new(p.P0_31, Level::High, OutputDrive::Standard);
26
27 // Example on how to talk to an ENC28J60 chip
28
29 // softreset
30 cortex_m::asm::delay(10);
31 ncs.set_low();
32 cortex_m::asm::delay(5);
33 let tx = [0xFF];
34 unwrap!(spim.transfer(&mut [], &tx).await);
35 cortex_m::asm::delay(10);
36 ncs.set_high();
37
38 cortex_m::asm::delay(100000);
39
40 let mut rx = [0; 2];
41
42 // read ESTAT
43 cortex_m::asm::delay(5000);
44 ncs.set_low();
45 cortex_m::asm::delay(5000);
46 let tx = [0b000_11101, 0];
47 unwrap!(spim.transfer(&mut rx, &tx).await);
48 cortex_m::asm::delay(5000);
49 ncs.set_high();
50 info!("estat: {=[?]}", rx);
51
52 // Switch to bank 3
53 cortex_m::asm::delay(10);
54 ncs.set_low();
55 cortex_m::asm::delay(5);
56 let tx = [0b100_11111, 0b11];
57 unwrap!(spim.transfer(&mut rx, &tx).await);
58 cortex_m::asm::delay(10);
59 ncs.set_high();
60
61 // read EREVID
62 cortex_m::asm::delay(10);
63 ncs.set_low();
64 cortex_m::asm::delay(5);
65 let tx = [0b000_10010, 0];
66 unwrap!(spim.transfer(&mut rx, &tx).await);
67 cortex_m::asm::delay(10);
68 ncs.set_high();
69
70 info!("erevid: {=[?]}", rx);
71}
diff --git a/examples/nrf52840/src/bin/spis.rs b/examples/nrf52840/src/bin/spis.rs
new file mode 100644
index 000000000..77b6e8b64
--- /dev/null
+++ b/examples/nrf52840/src/bin/spis.rs
@@ -0,0 +1,30 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5use defmt::info;
6use embassy_executor::Spawner;
7use embassy_nrf::spis::{Config, Spis};
8use embassy_nrf::{bind_interrupts, peripherals, spis};
9use {defmt_rtt as _, panic_probe as _};
10
11bind_interrupts!(struct Irqs {
12 SPIM2_SPIS2_SPI2 => spis::InterruptHandler<peripherals::SPI2>;
13});
14
15#[embassy_executor::main]
16async fn main(_spawner: Spawner) {
17 let p = embassy_nrf::init(Default::default());
18 info!("Running!");
19
20 let mut spis = Spis::new(p.SPI2, Irqs, p.P0_31, p.P0_29, p.P0_28, p.P0_30, Config::default());
21
22 loop {
23 let mut rx_buf = [0_u8; 64];
24 let tx_buf = [1_u8, 2, 3, 4, 5, 6, 7, 8];
25 if let Ok((n_rx, n_tx)) = spis.transfer(&mut rx_buf, &tx_buf).await {
26 info!("RX: {:?}", rx_buf[..n_rx]);
27 info!("TX: {:?}", tx_buf[..n_tx]);
28 }
29 }
30}
diff --git a/examples/nrf52840/src/bin/temp.rs b/examples/nrf52840/src/bin/temp.rs
new file mode 100644
index 000000000..70957548f
--- /dev/null
+++ b/examples/nrf52840/src/bin/temp.rs
@@ -0,0 +1,26 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5use defmt::info;
6use embassy_executor::Spawner;
7use embassy_nrf::temp::Temp;
8use embassy_nrf::{bind_interrupts, temp};
9use embassy_time::{Duration, Timer};
10use {defmt_rtt as _, panic_probe as _};
11
12bind_interrupts!(struct Irqs {
13 TEMP => temp::InterruptHandler;
14});
15
16#[embassy_executor::main]
17async fn main(_spawner: Spawner) {
18 let p = embassy_nrf::init(Default::default());
19 let mut temp = Temp::new(p.TEMP, Irqs);
20
21 loop {
22 let value = temp.read().await;
23 info!("temperature: {}℃", value.to_num::<u16>());
24 Timer::after(Duration::from_secs(1)).await;
25 }
26}
diff --git a/examples/nrf52840/src/bin/timer.rs b/examples/nrf52840/src/bin/timer.rs
new file mode 100644
index 000000000..c22b5acd5
--- /dev/null
+++ b/examples/nrf52840/src/bin/timer.rs
@@ -0,0 +1,31 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5use defmt::{info, unwrap};
6use embassy_executor::Spawner;
7use embassy_time::{Duration, Timer};
8use {defmt_rtt as _, panic_probe as _};
9
10#[embassy_executor::task]
11async fn run1() {
12 loop {
13 info!("BIG INFREQUENT TICK");
14 Timer::after(Duration::from_ticks(64000)).await;
15 }
16}
17
18#[embassy_executor::task]
19async fn run2() {
20 loop {
21 info!("tick");
22 Timer::after(Duration::from_ticks(13000)).await;
23 }
24}
25
26#[embassy_executor::main]
27async fn main(spawner: Spawner) {
28 let _p = embassy_nrf::init(Default::default());
29 unwrap!(spawner.spawn(run1()));
30 unwrap!(spawner.spawn(run2()));
31}
diff --git a/examples/nrf52840/src/bin/twim.rs b/examples/nrf52840/src/bin/twim.rs
new file mode 100644
index 000000000..959e3a4be
--- /dev/null
+++ b/examples/nrf52840/src/bin/twim.rs
@@ -0,0 +1,34 @@
1//! Example on how to read a 24C/24LC i2c eeprom.
2//!
3//! Connect SDA to P0.03, SCL to P0.04
4
5#![no_std]
6#![no_main]
7#![feature(type_alias_impl_trait)]
8
9use defmt::*;
10use embassy_executor::Spawner;
11use embassy_nrf::twim::{self, Twim};
12use embassy_nrf::{bind_interrupts, peripherals};
13use {defmt_rtt as _, panic_probe as _};
14
15const ADDRESS: u8 = 0x50;
16
17bind_interrupts!(struct Irqs {
18 SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0 => twim::InterruptHandler<peripherals::TWISPI0>;
19});
20
21#[embassy_executor::main]
22async fn main(_spawner: Spawner) {
23 let p = embassy_nrf::init(Default::default());
24 info!("Initializing TWI...");
25 let config = twim::Config::default();
26 let mut twi = Twim::new(p.TWISPI0, Irqs, p.P0_03, p.P0_04, config);
27
28 info!("Reading...");
29
30 let mut buf = [0u8; 16];
31 unwrap!(twi.blocking_write_read(ADDRESS, &mut [0x00], &mut buf));
32
33 info!("Read: {=[u8]:x}", buf);
34}
diff --git a/examples/nrf52840/src/bin/twim_lowpower.rs b/examples/nrf52840/src/bin/twim_lowpower.rs
new file mode 100644
index 000000000..0970d3c3c
--- /dev/null
+++ b/examples/nrf52840/src/bin/twim_lowpower.rs
@@ -0,0 +1,53 @@
1//! Example on how to read a 24C/24LC i2c eeprom with low power consumption.
2//! The eeprom is read every 1 second, while ensuring lowest possible power while
3//! sleeping between reads.
4//!
5//! Connect SDA to P0.03, SCL to P0.04
6
7#![no_std]
8#![no_main]
9#![feature(type_alias_impl_trait)]
10
11use core::mem;
12
13use defmt::*;
14use embassy_executor::Spawner;
15use embassy_nrf::twim::{self, Twim};
16use embassy_nrf::{bind_interrupts, peripherals};
17use embassy_time::{Duration, Timer};
18use {defmt_rtt as _, panic_probe as _};
19
20const ADDRESS: u8 = 0x50;
21
22bind_interrupts!(struct Irqs {
23 SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0 => twim::InterruptHandler<peripherals::TWISPI0>;
24});
25
26#[embassy_executor::main]
27async fn main(_p: Spawner) {
28 let mut p = embassy_nrf::init(Default::default());
29 info!("Started!");
30
31 loop {
32 info!("Initializing TWI...");
33 let config = twim::Config::default();
34
35 // Create the TWIM instance with borrowed singletons, so they're not consumed.
36 let mut twi = Twim::new(&mut p.TWISPI0, Irqs, &mut p.P0_03, &mut p.P0_04, config);
37
38 info!("Reading...");
39
40 let mut buf = [0u8; 16];
41 unwrap!(twi.blocking_write_read(ADDRESS, &mut [0x00], &mut buf));
42
43 info!("Read: {=[u8]:x}", buf);
44
45 // Drop the TWIM instance. This disables the peripehral and deconfigures the pins.
46 // This clears the borrow on the singletons, so they can now be used again.
47 mem::drop(twi);
48
49 // Sleep for 1 second. The executor ensures the core sleeps with a WFE when it has nothing to do.
50 // During this sleep, the nRF chip should only use ~3uA
51 Timer::after(Duration::from_secs(1)).await;
52 }
53}
diff --git a/examples/nrf52840/src/bin/twis.rs b/examples/nrf52840/src/bin/twis.rs
new file mode 100644
index 000000000..aa42b679e
--- /dev/null
+++ b/examples/nrf52840/src/bin/twis.rs
@@ -0,0 +1,48 @@
1//! TWIS example
2
3#![no_std]
4#![no_main]
5#![feature(type_alias_impl_trait)]
6
7use defmt::*;
8use embassy_executor::Spawner;
9use embassy_nrf::twis::{self, Command, Twis};
10use embassy_nrf::{bind_interrupts, peripherals};
11use {defmt_rtt as _, panic_probe as _};
12
13bind_interrupts!(struct Irqs {
14 SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0 => twis::InterruptHandler<peripherals::TWISPI0>;
15});
16
17#[embassy_executor::main]
18async fn main(_spawner: Spawner) {
19 let p = embassy_nrf::init(Default::default());
20
21 let mut config = twis::Config::default();
22 config.address0 = 0x55; // Set i2c address
23 let mut i2c = Twis::new(p.TWISPI0, Irqs, p.P0_03, p.P0_04, config);
24
25 info!("Listening...");
26 loop {
27 let response = [1, 2, 3, 4, 5, 6, 7, 8];
28 // This buffer is used if the i2c master performs a Write or WriteRead
29 let mut buf = [0u8; 16];
30 match i2c.listen(&mut buf).await {
31 Ok(Command::Read) => {
32 info!("Got READ command. Respond with data:\n{:?}\n", response);
33 if let Err(e) = i2c.respond_to_read(&response).await {
34 error!("{:?}", e);
35 }
36 }
37 Ok(Command::Write(n)) => info!("Got WRITE command with data:\n{:?}\n", buf[..n]),
38 Ok(Command::WriteRead(n)) => {
39 info!("Got WRITE/READ command with data:\n{:?}", buf[..n]);
40 info!("Respond with data:\n{:?}\n", response);
41 if let Err(e) = i2c.respond_to_read(&response).await {
42 error!("{:?}", e);
43 }
44 }
45 Err(e) => error!("{:?}", e),
46 }
47 }
48}
diff --git a/examples/nrf52840/src/bin/uart.rs b/examples/nrf52840/src/bin/uart.rs
new file mode 100644
index 000000000..50d5cab8c
--- /dev/null
+++ b/examples/nrf52840/src/bin/uart.rs
@@ -0,0 +1,38 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5use defmt::*;
6use embassy_executor::Spawner;
7use embassy_nrf::{bind_interrupts, peripherals, uarte};
8use {defmt_rtt as _, panic_probe as _};
9
10bind_interrupts!(struct Irqs {
11 UARTE0_UART0 => uarte::InterruptHandler<peripherals::UARTE0>;
12});
13
14#[embassy_executor::main]
15async fn main(_spawner: Spawner) {
16 let p = embassy_nrf::init(Default::default());
17 let mut config = uarte::Config::default();
18 config.parity = uarte::Parity::EXCLUDED;
19 config.baudrate = uarte::Baudrate::BAUD115200;
20
21 let mut uart = uarte::Uarte::new(p.UARTE0, Irqs, p.P0_08, p.P0_06, config);
22
23 info!("uarte initialized!");
24
25 // Message must be in SRAM
26 let mut buf = [0; 8];
27 buf.copy_from_slice(b"Hello!\r\n");
28
29 unwrap!(uart.write(&buf).await);
30 info!("wrote hello in uart!");
31
32 loop {
33 info!("reading...");
34 unwrap!(uart.read(&mut buf).await);
35 info!("writing...");
36 unwrap!(uart.write(&buf).await);
37 }
38}
diff --git a/examples/nrf52840/src/bin/uart_idle.rs b/examples/nrf52840/src/bin/uart_idle.rs
new file mode 100644
index 000000000..e1f42fa6c
--- /dev/null
+++ b/examples/nrf52840/src/bin/uart_idle.rs
@@ -0,0 +1,39 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5use defmt::*;
6use embassy_executor::Spawner;
7use embassy_nrf::peripherals::UARTE0;
8use embassy_nrf::{bind_interrupts, uarte};
9use {defmt_rtt as _, panic_probe as _};
10
11bind_interrupts!(struct Irqs {
12 UARTE0_UART0 => uarte::InterruptHandler<UARTE0>;
13});
14
15#[embassy_executor::main]
16async fn main(_spawner: Spawner) {
17 let p = embassy_nrf::init(Default::default());
18 let mut config = uarte::Config::default();
19 config.parity = uarte::Parity::EXCLUDED;
20 config.baudrate = uarte::Baudrate::BAUD115200;
21
22 let uart = uarte::Uarte::new(p.UARTE0, Irqs, p.P0_08, p.P0_06, config);
23 let (mut tx, mut rx) = uart.split_with_idle(p.TIMER0, p.PPI_CH0, p.PPI_CH1);
24
25 info!("uarte initialized!");
26
27 // Message must be in SRAM
28 let mut buf = [0; 8];
29 buf.copy_from_slice(b"Hello!\r\n");
30
31 unwrap!(tx.write(&buf).await);
32 info!("wrote hello in uart!");
33
34 loop {
35 info!("reading...");
36 let n = unwrap!(rx.read_until_idle(&mut buf).await);
37 info!("got {} bytes", n);
38 }
39}
diff --git a/examples/nrf52840/src/bin/uart_split.rs b/examples/nrf52840/src/bin/uart_split.rs
new file mode 100644
index 000000000..9979a1d53
--- /dev/null
+++ b/examples/nrf52840/src/bin/uart_split.rs
@@ -0,0 +1,63 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5use defmt::*;
6use embassy_executor::Spawner;
7use embassy_nrf::peripherals::UARTE0;
8use embassy_nrf::uarte::UarteRx;
9use embassy_nrf::{bind_interrupts, uarte};
10use embassy_sync::blocking_mutex::raw::ThreadModeRawMutex;
11use embassy_sync::channel::Channel;
12use {defmt_rtt as _, panic_probe as _};
13
14static CHANNEL: Channel<ThreadModeRawMutex, [u8; 8], 1> = Channel::new();
15
16bind_interrupts!(struct Irqs {
17 UARTE0_UART0 => uarte::InterruptHandler<UARTE0>;
18});
19
20#[embassy_executor::main]
21async fn main(spawner: Spawner) {
22 let p = embassy_nrf::init(Default::default());
23 let mut config = uarte::Config::default();
24 config.parity = uarte::Parity::EXCLUDED;
25 config.baudrate = uarte::Baudrate::BAUD115200;
26
27 let uart = uarte::Uarte::new(p.UARTE0, Irqs, p.P0_08, p.P0_06, config);
28 let (mut tx, rx) = uart.split();
29
30 info!("uarte initialized!");
31
32 // Spawn a task responsible purely for reading
33
34 unwrap!(spawner.spawn(reader(rx)));
35
36 // Message must be in SRAM
37 {
38 let mut buf = [0; 23];
39 buf.copy_from_slice(b"Type 8 chars to echo!\r\n");
40
41 unwrap!(tx.write(&buf).await);
42 info!("wrote hello in uart!");
43 }
44
45 // Continue reading in this main task and write
46 // back out the buffer we receive from the read
47 // task.
48 loop {
49 let buf = CHANNEL.recv().await;
50 info!("writing...");
51 unwrap!(tx.write(&buf).await);
52 }
53}
54
55#[embassy_executor::task]
56async fn reader(mut rx: UarteRx<'static, UARTE0>) {
57 let mut buf = [0; 8];
58 loop {
59 info!("reading...");
60 unwrap!(rx.read(&mut buf).await);
61 CHANNEL.send(buf).await;
62 }
63}
diff --git a/examples/nrf52840/src/bin/usb_ethernet.rs b/examples/nrf52840/src/bin/usb_ethernet.rs
new file mode 100644
index 000000000..f527c0d7f
--- /dev/null
+++ b/examples/nrf52840/src/bin/usb_ethernet.rs
@@ -0,0 +1,165 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5use core::mem;
6
7use defmt::*;
8use embassy_executor::Spawner;
9use embassy_net::tcp::TcpSocket;
10use embassy_net::{Stack, StackResources};
11use embassy_nrf::rng::Rng;
12use embassy_nrf::usb::vbus_detect::HardwareVbusDetect;
13use embassy_nrf::usb::Driver;
14use embassy_nrf::{bind_interrupts, pac, peripherals, rng, usb};
15use embassy_usb::class::cdc_ncm::embassy_net::{Device, Runner, State as NetState};
16use embassy_usb::class::cdc_ncm::{CdcNcmClass, State};
17use embassy_usb::{Builder, Config, UsbDevice};
18use embedded_io::asynch::Write;
19use static_cell::make_static;
20use {defmt_rtt as _, panic_probe as _};
21
22bind_interrupts!(struct Irqs {
23 USBD => usb::InterruptHandler<peripherals::USBD>;
24 POWER_CLOCK => usb::vbus_detect::InterruptHandler;
25 RNG => rng::InterruptHandler<peripherals::RNG>;
26});
27
28type MyDriver = Driver<'static, peripherals::USBD, HardwareVbusDetect>;
29
30const MTU: usize = 1514;
31
32#[embassy_executor::task]
33async fn usb_task(mut device: UsbDevice<'static, MyDriver>) -> ! {
34 device.run().await
35}
36
37#[embassy_executor::task]
38async fn usb_ncm_task(class: Runner<'static, MyDriver, MTU>) -> ! {
39 class.run().await
40}
41
42#[embassy_executor::task]
43async fn net_task(stack: &'static Stack<Device<'static, MTU>>) -> ! {
44 stack.run().await
45}
46
47#[embassy_executor::main]
48async fn main(spawner: Spawner) {
49 let p = embassy_nrf::init(Default::default());
50 let clock: pac::CLOCK = unsafe { mem::transmute(()) };
51
52 info!("Enabling ext hfosc...");
53 clock.tasks_hfclkstart.write(|w| unsafe { w.bits(1) });
54 while clock.events_hfclkstarted.read().bits() != 1 {}
55
56 // Create the driver, from the HAL.
57 let driver = Driver::new(p.USBD, Irqs, HardwareVbusDetect::new(Irqs));
58
59 // Create embassy-usb Config
60 let mut config = Config::new(0xc0de, 0xcafe);
61 config.manufacturer = Some("Embassy");
62 config.product = Some("USB-Ethernet example");
63 config.serial_number = Some("12345678");
64 config.max_power = 100;
65 config.max_packet_size_0 = 64;
66
67 // Required for Windows support.
68 config.composite_with_iads = true;
69 config.device_class = 0xEF;
70 config.device_sub_class = 0x02;
71 config.device_protocol = 0x01;
72
73 // Create embassy-usb DeviceBuilder using the driver and config.
74 let mut builder = Builder::new(
75 driver,
76 config,
77 &mut make_static!([0; 256])[..],
78 &mut make_static!([0; 256])[..],
79 &mut make_static!([0; 256])[..],
80 &mut make_static!([0; 128])[..],
81 &mut make_static!([0; 128])[..],
82 );
83
84 // Our MAC addr.
85 let our_mac_addr = [0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC];
86 // Host's MAC addr. This is the MAC the host "thinks" its USB-to-ethernet adapter has.
87 let host_mac_addr = [0x88, 0x88, 0x88, 0x88, 0x88, 0x88];
88
89 // Create classes on the builder.
90 let class = CdcNcmClass::new(&mut builder, make_static!(State::new()), host_mac_addr, 64);
91
92 // Build the builder.
93 let usb = builder.build();
94
95 unwrap!(spawner.spawn(usb_task(usb)));
96
97 let (runner, device) = class.into_embassy_net_device::<MTU, 4, 4>(make_static!(NetState::new()), our_mac_addr);
98 unwrap!(spawner.spawn(usb_ncm_task(runner)));
99
100 let config = embassy_net::Config::dhcpv4(Default::default());
101 // let config = embassy_net::Config::ipv4_static(embassy_net::StaticConfigV4 {
102 // address: Ipv4Cidr::new(Ipv4Address::new(10, 42, 0, 61), 24),
103 // dns_servers: Vec::new(),
104 // gateway: Some(Ipv4Address::new(10, 42, 0, 1)),
105 // });
106
107 // Generate random seed
108 let mut rng = Rng::new(p.RNG, Irqs);
109 let mut seed = [0; 8];
110 rng.blocking_fill_bytes(&mut seed);
111 let seed = u64::from_le_bytes(seed);
112
113 // Init network stack
114 let stack = &*make_static!(Stack::new(
115 device,
116 config,
117 make_static!(StackResources::<2>::new()),
118 seed
119 ));
120
121 unwrap!(spawner.spawn(net_task(stack)));
122
123 // And now we can use it!
124
125 let mut rx_buffer = [0; 4096];
126 let mut tx_buffer = [0; 4096];
127 let mut buf = [0; 4096];
128
129 loop {
130 let mut socket = TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer);
131 socket.set_timeout(Some(embassy_time::Duration::from_secs(10)));
132
133 info!("Listening on TCP:1234...");
134 if let Err(e) = socket.accept(1234).await {
135 warn!("accept error: {:?}", e);
136 continue;
137 }
138
139 info!("Received connection from {:?}", socket.remote_endpoint());
140
141 loop {
142 let n = match socket.read(&mut buf).await {
143 Ok(0) => {
144 warn!("read EOF");
145 break;
146 }
147 Ok(n) => n,
148 Err(e) => {
149 warn!("read error: {:?}", e);
150 break;
151 }
152 };
153
154 info!("rxd {:02x}", &buf[..n]);
155
156 match socket.write_all(&buf[..n]).await {
157 Ok(()) => {}
158 Err(e) => {
159 warn!("write error: {:?}", e);
160 break;
161 }
162 };
163 }
164 }
165}
diff --git a/examples/nrf52840/src/bin/usb_hid_keyboard.rs b/examples/nrf52840/src/bin/usb_hid_keyboard.rs
new file mode 100644
index 000000000..7ccd2946a
--- /dev/null
+++ b/examples/nrf52840/src/bin/usb_hid_keyboard.rs
@@ -0,0 +1,229 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5use core::mem;
6use core::sync::atomic::{AtomicBool, Ordering};
7
8use defmt::*;
9use embassy_executor::Spawner;
10use embassy_futures::join::join;
11use embassy_futures::select::{select, Either};
12use embassy_nrf::gpio::{Input, Pin, Pull};
13use embassy_nrf::usb::vbus_detect::HardwareVbusDetect;
14use embassy_nrf::usb::Driver;
15use embassy_nrf::{bind_interrupts, pac, peripherals, usb};
16use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
17use embassy_sync::signal::Signal;
18use embassy_usb::class::hid::{HidReaderWriter, ReportId, RequestHandler, State};
19use embassy_usb::control::OutResponse;
20use embassy_usb::{Builder, Config, Handler};
21use usbd_hid::descriptor::{KeyboardReport, SerializedDescriptor};
22use {defmt_rtt as _, panic_probe as _};
23
24bind_interrupts!(struct Irqs {
25 USBD => usb::InterruptHandler<peripherals::USBD>;
26 POWER_CLOCK => usb::vbus_detect::InterruptHandler;
27});
28
29static SUSPENDED: AtomicBool = AtomicBool::new(false);
30
31#[embassy_executor::main]
32async fn main(_spawner: Spawner) {
33 let p = embassy_nrf::init(Default::default());
34 let clock: pac::CLOCK = unsafe { mem::transmute(()) };
35
36 info!("Enabling ext hfosc...");
37 clock.tasks_hfclkstart.write(|w| unsafe { w.bits(1) });
38 while clock.events_hfclkstarted.read().bits() != 1 {}
39
40 // Create the driver, from the HAL.
41 let driver = Driver::new(p.USBD, Irqs, HardwareVbusDetect::new(Irqs));
42
43 // Create embassy-usb Config
44 let mut config = Config::new(0xc0de, 0xcafe);
45 config.manufacturer = Some("Embassy");
46 config.product = Some("HID keyboard example");
47 config.serial_number = Some("12345678");
48 config.max_power = 100;
49 config.max_packet_size_0 = 64;
50 config.supports_remote_wakeup = true;
51
52 // Create embassy-usb DeviceBuilder using the driver and config.
53 // It needs some buffers for building the descriptors.
54 let mut device_descriptor = [0; 256];
55 let mut config_descriptor = [0; 256];
56 let mut bos_descriptor = [0; 256];
57 let mut msos_descriptor = [0; 256];
58 let mut control_buf = [0; 64];
59 let request_handler = MyRequestHandler {};
60 let mut device_handler = MyDeviceHandler::new();
61
62 let mut state = State::new();
63
64 let mut builder = Builder::new(
65 driver,
66 config,
67 &mut device_descriptor,
68 &mut config_descriptor,
69 &mut bos_descriptor,
70 &mut msos_descriptor,
71 &mut control_buf,
72 );
73
74 builder.handler(&mut device_handler);
75
76 // Create classes on the builder.
77 let config = embassy_usb::class::hid::Config {
78 report_descriptor: KeyboardReport::desc(),
79 request_handler: Some(&request_handler),
80 poll_ms: 60,
81 max_packet_size: 64,
82 };
83 let hid = HidReaderWriter::<_, 1, 8>::new(&mut builder, &mut state, config);
84
85 // Build the builder.
86 let mut usb = builder.build();
87
88 let remote_wakeup: Signal<CriticalSectionRawMutex, _> = Signal::new();
89
90 // Run the USB device.
91 let usb_fut = async {
92 loop {
93 usb.run_until_suspend().await;
94 match select(usb.wait_resume(), remote_wakeup.wait()).await {
95 Either::First(_) => (),
96 Either::Second(_) => unwrap!(usb.remote_wakeup().await),
97 }
98 }
99 };
100
101 let mut button = Input::new(p.P0_11.degrade(), Pull::Up);
102
103 let (reader, mut writer) = hid.split();
104
105 // Do stuff with the class!
106 let in_fut = async {
107 loop {
108 button.wait_for_low().await;
109 info!("PRESSED");
110
111 if SUSPENDED.load(Ordering::Acquire) {
112 info!("Triggering remote wakeup");
113 remote_wakeup.signal(());
114 } else {
115 let report = KeyboardReport {
116 keycodes: [4, 0, 0, 0, 0, 0],
117 leds: 0,
118 modifier: 0,
119 reserved: 0,
120 };
121 match writer.write_serialize(&report).await {
122 Ok(()) => {}
123 Err(e) => warn!("Failed to send report: {:?}", e),
124 };
125 }
126
127 button.wait_for_high().await;
128 info!("RELEASED");
129 let report = KeyboardReport {
130 keycodes: [0, 0, 0, 0, 0, 0],
131 leds: 0,
132 modifier: 0,
133 reserved: 0,
134 };
135 match writer.write_serialize(&report).await {
136 Ok(()) => {}
137 Err(e) => warn!("Failed to send report: {:?}", e),
138 };
139 }
140 };
141
142 let out_fut = async {
143 reader.run(false, &request_handler).await;
144 };
145
146 // Run everything concurrently.
147 // If we had made everything `'static` above instead, we could do this using separate tasks instead.
148 join(usb_fut, join(in_fut, out_fut)).await;
149}
150
151struct MyRequestHandler {}
152
153impl RequestHandler for MyRequestHandler {
154 fn get_report(&self, id: ReportId, _buf: &mut [u8]) -> Option<usize> {
155 info!("Get report for {:?}", id);
156 None
157 }
158
159 fn set_report(&self, id: ReportId, data: &[u8]) -> OutResponse {
160 info!("Set report for {:?}: {=[u8]}", id, data);
161 OutResponse::Accepted
162 }
163
164 fn set_idle_ms(&self, id: Option<ReportId>, dur: u32) {
165 info!("Set idle rate for {:?} to {:?}", id, dur);
166 }
167
168 fn get_idle_ms(&self, id: Option<ReportId>) -> Option<u32> {
169 info!("Get idle rate for {:?}", id);
170 None
171 }
172}
173
174struct MyDeviceHandler {
175 configured: AtomicBool,
176}
177
178impl MyDeviceHandler {
179 fn new() -> Self {
180 MyDeviceHandler {
181 configured: AtomicBool::new(false),
182 }
183 }
184}
185
186impl Handler for MyDeviceHandler {
187 fn enabled(&mut self, enabled: bool) {
188 self.configured.store(false, Ordering::Relaxed);
189 SUSPENDED.store(false, Ordering::Release);
190 if enabled {
191 info!("Device enabled");
192 } else {
193 info!("Device disabled");
194 }
195 }
196
197 fn reset(&mut self) {
198 self.configured.store(false, Ordering::Relaxed);
199 info!("Bus reset, the Vbus current limit is 100mA");
200 }
201
202 fn addressed(&mut self, addr: u8) {
203 self.configured.store(false, Ordering::Relaxed);
204 info!("USB address set to: {}", addr);
205 }
206
207 fn configured(&mut self, configured: bool) {
208 self.configured.store(configured, Ordering::Relaxed);
209 if configured {
210 info!("Device configured, it may now draw up to the configured current limit from Vbus.")
211 } else {
212 info!("Device is no longer configured, the Vbus current limit is 100mA.");
213 }
214 }
215
216 fn suspended(&mut self, suspended: bool) {
217 if suspended {
218 info!("Device suspended, the Vbus current limit is 500µA (or 2.5mA for high-power devices with remote wakeup enabled).");
219 SUSPENDED.store(true, Ordering::Release);
220 } else {
221 SUSPENDED.store(false, Ordering::Release);
222 if self.configured.load(Ordering::Relaxed) {
223 info!("Device resumed, it may now draw up to the configured current limit from Vbus");
224 } else {
225 info!("Device resumed, the Vbus current limit is 100mA");
226 }
227 }
228 }
229}
diff --git a/examples/nrf52840/src/bin/usb_hid_mouse.rs b/examples/nrf52840/src/bin/usb_hid_mouse.rs
new file mode 100644
index 000000000..edf634a5e
--- /dev/null
+++ b/examples/nrf52840/src/bin/usb_hid_mouse.rs
@@ -0,0 +1,129 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5use core::mem;
6
7use defmt::*;
8use embassy_executor::Spawner;
9use embassy_futures::join::join;
10use embassy_nrf::usb::vbus_detect::HardwareVbusDetect;
11use embassy_nrf::usb::Driver;
12use embassy_nrf::{bind_interrupts, pac, peripherals, usb};
13use embassy_time::{Duration, Timer};
14use embassy_usb::class::hid::{HidWriter, ReportId, RequestHandler, State};
15use embassy_usb::control::OutResponse;
16use embassy_usb::{Builder, Config};
17use usbd_hid::descriptor::{MouseReport, SerializedDescriptor};
18use {defmt_rtt as _, panic_probe as _};
19
20bind_interrupts!(struct Irqs {
21 USBD => usb::InterruptHandler<peripherals::USBD>;
22 POWER_CLOCK => usb::vbus_detect::InterruptHandler;
23});
24
25#[embassy_executor::main]
26async fn main(_spawner: Spawner) {
27 let p = embassy_nrf::init(Default::default());
28 let clock: pac::CLOCK = unsafe { mem::transmute(()) };
29
30 info!("Enabling ext hfosc...");
31 clock.tasks_hfclkstart.write(|w| unsafe { w.bits(1) });
32 while clock.events_hfclkstarted.read().bits() != 1 {}
33
34 // Create the driver, from the HAL.
35 let driver = Driver::new(p.USBD, Irqs, HardwareVbusDetect::new(Irqs));
36
37 // Create embassy-usb Config
38 let mut config = Config::new(0xc0de, 0xcafe);
39 config.manufacturer = Some("Embassy");
40 config.product = Some("HID mouse example");
41 config.serial_number = Some("12345678");
42 config.max_power = 100;
43 config.max_packet_size_0 = 64;
44
45 // Create embassy-usb DeviceBuilder using the driver and config.
46 // It needs some buffers for building the descriptors.
47 let mut device_descriptor = [0; 256];
48 let mut config_descriptor = [0; 256];
49 let mut bos_descriptor = [0; 256];
50 let mut msos_descriptor = [0; 256];
51 let mut control_buf = [0; 64];
52 let request_handler = MyRequestHandler {};
53
54 let mut state = State::new();
55
56 let mut builder = Builder::new(
57 driver,
58 config,
59 &mut device_descriptor,
60 &mut config_descriptor,
61 &mut bos_descriptor,
62 &mut msos_descriptor,
63 &mut control_buf,
64 );
65
66 // Create classes on the builder.
67 let config = embassy_usb::class::hid::Config {
68 report_descriptor: MouseReport::desc(),
69 request_handler: Some(&request_handler),
70 poll_ms: 60,
71 max_packet_size: 8,
72 };
73
74 let mut writer = HidWriter::<_, 5>::new(&mut builder, &mut state, config);
75
76 // Build the builder.
77 let mut usb = builder.build();
78
79 // Run the USB device.
80 let usb_fut = usb.run();
81
82 // Do stuff with the class!
83 let hid_fut = async {
84 let mut y: i8 = 5;
85 loop {
86 Timer::after(Duration::from_millis(500)).await;
87
88 y = -y;
89 let report = MouseReport {
90 buttons: 0,
91 x: 0,
92 y,
93 wheel: 0,
94 pan: 0,
95 };
96 match writer.write_serialize(&report).await {
97 Ok(()) => {}
98 Err(e) => warn!("Failed to send report: {:?}", e),
99 }
100 }
101 };
102
103 // Run everything concurrently.
104 // If we had made everything `'static` above instead, we could do this using separate tasks instead.
105 join(usb_fut, hid_fut).await;
106}
107
108struct MyRequestHandler {}
109
110impl RequestHandler for MyRequestHandler {
111 fn get_report(&self, id: ReportId, _buf: &mut [u8]) -> Option<usize> {
112 info!("Get report for {:?}", id);
113 None
114 }
115
116 fn set_report(&self, id: ReportId, data: &[u8]) -> OutResponse {
117 info!("Set report for {:?}: {=[u8]}", id, data);
118 OutResponse::Accepted
119 }
120
121 fn set_idle_ms(&self, id: Option<ReportId>, dur: u32) {
122 info!("Set idle rate for {:?} to {:?}", id, dur);
123 }
124
125 fn get_idle_ms(&self, id: Option<ReportId>) -> Option<u32> {
126 info!("Get idle rate for {:?}", id);
127 None
128 }
129}
diff --git a/examples/nrf52840/src/bin/usb_serial.rs b/examples/nrf52840/src/bin/usb_serial.rs
new file mode 100644
index 000000000..dc95cde84
--- /dev/null
+++ b/examples/nrf52840/src/bin/usb_serial.rs
@@ -0,0 +1,115 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5use core::mem;
6
7use defmt::{info, panic};
8use embassy_executor::Spawner;
9use embassy_futures::join::join;
10use embassy_nrf::usb::vbus_detect::{HardwareVbusDetect, VbusDetect};
11use embassy_nrf::usb::{Driver, Instance};
12use embassy_nrf::{bind_interrupts, pac, peripherals, usb};
13use embassy_usb::class::cdc_acm::{CdcAcmClass, State};
14use embassy_usb::driver::EndpointError;
15use embassy_usb::{Builder, Config};
16use {defmt_rtt as _, panic_probe as _};
17
18bind_interrupts!(struct Irqs {
19 USBD => usb::InterruptHandler<peripherals::USBD>;
20 POWER_CLOCK => usb::vbus_detect::InterruptHandler;
21});
22
23#[embassy_executor::main]
24async fn main(_spawner: Spawner) {
25 let p = embassy_nrf::init(Default::default());
26 let clock: pac::CLOCK = unsafe { mem::transmute(()) };
27
28 info!("Enabling ext hfosc...");
29 clock.tasks_hfclkstart.write(|w| unsafe { w.bits(1) });
30 while clock.events_hfclkstarted.read().bits() != 1 {}
31
32 // Create the driver, from the HAL.
33 let driver = Driver::new(p.USBD, Irqs, HardwareVbusDetect::new(Irqs));
34
35 // Create embassy-usb Config
36 let mut config = Config::new(0xc0de, 0xcafe);
37 config.manufacturer = Some("Embassy");
38 config.product = Some("USB-serial example");
39 config.serial_number = Some("12345678");
40 config.max_power = 100;
41 config.max_packet_size_0 = 64;
42
43 // Required for windows compatibility.
44 // https://developer.nordicsemi.com/nRF_Connect_SDK/doc/1.9.1/kconfig/CONFIG_CDC_ACM_IAD.html#help
45 config.device_class = 0xEF;
46 config.device_sub_class = 0x02;
47 config.device_protocol = 0x01;
48 config.composite_with_iads = true;
49
50 // Create embassy-usb DeviceBuilder using the driver and config.
51 // It needs some buffers for building the descriptors.
52 let mut device_descriptor = [0; 256];
53 let mut config_descriptor = [0; 256];
54 let mut bos_descriptor = [0; 256];
55 let mut msos_descriptor = [0; 256];
56 let mut control_buf = [0; 64];
57
58 let mut state = State::new();
59
60 let mut builder = Builder::new(
61 driver,
62 config,
63 &mut device_descriptor,
64 &mut config_descriptor,
65 &mut bos_descriptor,
66 &mut msos_descriptor,
67 &mut control_buf,
68 );
69
70 // Create classes on the builder.
71 let mut class = CdcAcmClass::new(&mut builder, &mut state, 64);
72
73 // Build the builder.
74 let mut usb = builder.build();
75
76 // Run the USB device.
77 let usb_fut = usb.run();
78
79 // Do stuff with the class!
80 let echo_fut = async {
81 loop {
82 class.wait_connection().await;
83 info!("Connected");
84 let _ = echo(&mut class).await;
85 info!("Disconnected");
86 }
87 };
88
89 // Run everything concurrently.
90 // If we had made everything `'static` above instead, we could do this using separate tasks instead.
91 join(usb_fut, echo_fut).await;
92}
93
94struct Disconnected {}
95
96impl From<EndpointError> for Disconnected {
97 fn from(val: EndpointError) -> Self {
98 match val {
99 EndpointError::BufferOverflow => panic!("Buffer overflow"),
100 EndpointError::Disabled => Disconnected {},
101 }
102 }
103}
104
105async fn echo<'d, T: Instance + 'd, P: VbusDetect + 'd>(
106 class: &mut CdcAcmClass<'d, Driver<'d, T, P>>,
107) -> Result<(), Disconnected> {
108 let mut buf = [0; 64];
109 loop {
110 let n = class.read_packet(&mut buf).await?;
111 let data = &buf[..n];
112 info!("data: {:x}", data);
113 class.write_packet(data).await?;
114 }
115}
diff --git a/examples/nrf52840/src/bin/usb_serial_multitask.rs b/examples/nrf52840/src/bin/usb_serial_multitask.rs
new file mode 100644
index 000000000..cd4392903
--- /dev/null
+++ b/examples/nrf52840/src/bin/usb_serial_multitask.rs
@@ -0,0 +1,109 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5use core::mem;
6
7use defmt::{info, panic, unwrap};
8use embassy_executor::Spawner;
9use embassy_nrf::usb::vbus_detect::HardwareVbusDetect;
10use embassy_nrf::usb::Driver;
11use embassy_nrf::{bind_interrupts, pac, peripherals, usb};
12use embassy_usb::class::cdc_acm::{CdcAcmClass, State};
13use embassy_usb::driver::EndpointError;
14use embassy_usb::{Builder, Config, UsbDevice};
15use static_cell::make_static;
16use {defmt_rtt as _, panic_probe as _};
17
18bind_interrupts!(struct Irqs {
19 USBD => usb::InterruptHandler<peripherals::USBD>;
20 POWER_CLOCK => usb::vbus_detect::InterruptHandler;
21});
22
23type MyDriver = Driver<'static, peripherals::USBD, HardwareVbusDetect>;
24
25#[embassy_executor::task]
26async fn usb_task(mut device: UsbDevice<'static, MyDriver>) {
27 device.run().await;
28}
29
30#[embassy_executor::task]
31async fn echo_task(mut class: CdcAcmClass<'static, MyDriver>) {
32 loop {
33 class.wait_connection().await;
34 info!("Connected");
35 let _ = echo(&mut class).await;
36 info!("Disconnected");
37 }
38}
39
40#[embassy_executor::main]
41async fn main(spawner: Spawner) {
42 let p = embassy_nrf::init(Default::default());
43 let clock: pac::CLOCK = unsafe { mem::transmute(()) };
44
45 info!("Enabling ext hfosc...");
46 clock.tasks_hfclkstart.write(|w| unsafe { w.bits(1) });
47 while clock.events_hfclkstarted.read().bits() != 1 {}
48
49 // Create the driver, from the HAL.
50 let driver = Driver::new(p.USBD, Irqs, HardwareVbusDetect::new(Irqs));
51
52 // Create embassy-usb Config
53 let mut config = Config::new(0xc0de, 0xcafe);
54 config.manufacturer = Some("Embassy");
55 config.product = Some("USB-serial example");
56 config.serial_number = Some("12345678");
57 config.max_power = 100;
58 config.max_packet_size_0 = 64;
59
60 // Required for windows compatibility.
61 // https://developer.nordicsemi.com/nRF_Connect_SDK/doc/1.9.1/kconfig/CONFIG_CDC_ACM_IAD.html#help
62 config.device_class = 0xEF;
63 config.device_sub_class = 0x02;
64 config.device_protocol = 0x01;
65 config.composite_with_iads = true;
66
67 let state = make_static!(State::new());
68
69 // Create embassy-usb DeviceBuilder using the driver and config.
70 let mut builder = Builder::new(
71 driver,
72 config,
73 &mut make_static!([0; 256])[..],
74 &mut make_static!([0; 256])[..],
75 &mut make_static!([0; 256])[..],
76 &mut make_static!([0; 128])[..],
77 &mut make_static!([0; 128])[..],
78 );
79
80 // Create classes on the builder.
81 let class = CdcAcmClass::new(&mut builder, state, 64);
82
83 // Build the builder.
84 let usb = builder.build();
85
86 unwrap!(spawner.spawn(usb_task(usb)));
87 unwrap!(spawner.spawn(echo_task(class)));
88}
89
90struct Disconnected {}
91
92impl From<EndpointError> for Disconnected {
93 fn from(val: EndpointError) -> Self {
94 match val {
95 EndpointError::BufferOverflow => panic!("Buffer overflow"),
96 EndpointError::Disabled => Disconnected {},
97 }
98 }
99}
100
101async fn echo(class: &mut CdcAcmClass<'static, MyDriver>) -> Result<(), Disconnected> {
102 let mut buf = [0; 64];
103 loop {
104 let n = class.read_packet(&mut buf).await?;
105 let data = &buf[..n];
106 info!("data: {:x}", data);
107 class.write_packet(data).await?;
108 }
109}
diff --git a/examples/nrf52840/src/bin/usb_serial_winusb.rs b/examples/nrf52840/src/bin/usb_serial_winusb.rs
new file mode 100644
index 000000000..1d39d3841
--- /dev/null
+++ b/examples/nrf52840/src/bin/usb_serial_winusb.rs
@@ -0,0 +1,134 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5use core::mem;
6
7use defmt::{info, panic};
8use embassy_executor::Spawner;
9use embassy_futures::join::join;
10use embassy_nrf::usb::vbus_detect::{HardwareVbusDetect, VbusDetect};
11use embassy_nrf::usb::{Driver, Instance};
12use embassy_nrf::{bind_interrupts, pac, peripherals, usb};
13use embassy_usb::class::cdc_acm::{CdcAcmClass, State};
14use embassy_usb::driver::EndpointError;
15use embassy_usb::msos::{self, windows_version};
16use embassy_usb::types::InterfaceNumber;
17use embassy_usb::{Builder, Config};
18use {defmt_rtt as _, panic_probe as _};
19
20bind_interrupts!(struct Irqs {
21 USBD => usb::InterruptHandler<peripherals::USBD>;
22 POWER_CLOCK => usb::vbus_detect::InterruptHandler;
23});
24
25// This is a randomly generated GUID to allow clients on Windows to find our device
26const DEVICE_INTERFACE_GUIDS: &[&str] = &["{EAA9A5DC-30BA-44BC-9232-606CDC875321}"];
27
28#[embassy_executor::main]
29async fn main(_spawner: Spawner) {
30 let p = embassy_nrf::init(Default::default());
31 let clock: pac::CLOCK = unsafe { mem::transmute(()) };
32
33 info!("Enabling ext hfosc...");
34 clock.tasks_hfclkstart.write(|w| unsafe { w.bits(1) });
35 while clock.events_hfclkstarted.read().bits() != 1 {}
36
37 // Create the driver, from the HAL.
38 let driver = Driver::new(p.USBD, Irqs, HardwareVbusDetect::new(Irqs));
39
40 // Create embassy-usb Config
41 let mut config = Config::new(0xc0de, 0xcafe);
42 config.manufacturer = Some("Embassy");
43 config.product = Some("USB-serial example");
44 config.serial_number = Some("12345678");
45 config.max_power = 100;
46 config.max_packet_size_0 = 64;
47
48 // Required for windows compatibility.
49 // https://developer.nordicsemi.com/nRF_Connect_SDK/doc/1.9.1/kconfig/CONFIG_CDC_ACM_IAD.html#help
50 config.device_class = 0xEF;
51 config.device_sub_class = 0x02;
52 config.device_protocol = 0x01;
53 config.composite_with_iads = true;
54
55 // Create embassy-usb DeviceBuilder using the driver and config.
56 // It needs some buffers for building the descriptors.
57 let mut device_descriptor = [0; 256];
58 let mut config_descriptor = [0; 256];
59 let mut bos_descriptor = [0; 256];
60 let mut msos_descriptor = [0; 256];
61 let mut control_buf = [0; 64];
62
63 let mut state = State::new();
64
65 let mut builder = Builder::new(
66 driver,
67 config,
68 &mut device_descriptor,
69 &mut config_descriptor,
70 &mut bos_descriptor,
71 &mut msos_descriptor,
72 &mut control_buf,
73 );
74
75 builder.msos_descriptor(windows_version::WIN8_1, 2);
76
77 // Create classes on the builder.
78 let mut class = CdcAcmClass::new(&mut builder, &mut state, 64);
79
80 // Since we want to create MS OS feature descriptors that apply to a function that has already been added to the
81 // builder, need to get the MsOsDescriptorWriter from the builder and manually add those descriptors.
82 // Inside a class constructor, you would just need to call `FunctionBuilder::msos_feature` instead.
83 let msos_writer = builder.msos_writer();
84 msos_writer.configuration(0);
85 msos_writer.function(InterfaceNumber(0));
86 msos_writer.function_feature(msos::CompatibleIdFeatureDescriptor::new("WINUSB", ""));
87 msos_writer.function_feature(msos::RegistryPropertyFeatureDescriptor::new(
88 "DeviceInterfaceGUIDs",
89 msos::PropertyData::RegMultiSz(DEVICE_INTERFACE_GUIDS),
90 ));
91
92 // Build the builder.
93 let mut usb = builder.build();
94
95 // Run the USB device.
96 let usb_fut = usb.run();
97
98 // Do stuff with the class!
99 let echo_fut = async {
100 loop {
101 class.wait_connection().await;
102 info!("Connected");
103 let _ = echo(&mut class).await;
104 info!("Disconnected");
105 }
106 };
107
108 // Run everything concurrently.
109 // If we had made everything `'static` above instead, we could do this using separate tasks instead.
110 join(usb_fut, echo_fut).await;
111}
112
113struct Disconnected {}
114
115impl From<EndpointError> for Disconnected {
116 fn from(val: EndpointError) -> Self {
117 match val {
118 EndpointError::BufferOverflow => panic!("Buffer overflow"),
119 EndpointError::Disabled => Disconnected {},
120 }
121 }
122}
123
124async fn echo<'d, T: Instance + 'd, P: VbusDetect + 'd>(
125 class: &mut CdcAcmClass<'d, Driver<'d, T, P>>,
126) -> Result<(), Disconnected> {
127 let mut buf = [0; 64];
128 loop {
129 let n = class.read_packet(&mut buf).await?;
130 let data = &buf[..n];
131 info!("data: {:x}", data);
132 class.write_packet(data).await?;
133 }
134}
diff --git a/examples/nrf52840/src/bin/wdt.rs b/examples/nrf52840/src/bin/wdt.rs
new file mode 100644
index 000000000..058746518
--- /dev/null
+++ b/examples/nrf52840/src/bin/wdt.rs
@@ -0,0 +1,41 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5use defmt::*;
6use embassy_executor::Spawner;
7use embassy_nrf::gpio::{Input, Pull};
8use embassy_nrf::wdt::{Config, Watchdog};
9use {defmt_rtt as _, panic_probe as _};
10
11#[embassy_executor::main]
12async fn main(_spawner: Spawner) {
13 let p = embassy_nrf::init(Default::default());
14 info!("Hello World!");
15
16 let mut config = Config::default();
17 config.timeout_ticks = 32768 * 3; // 3 seconds
18
19 // This is needed for `probe-rs run` to be able to catch the panic message
20 // in the WDT interrupt. The core resets 2 ticks after firing the interrupt.
21 config.run_during_debug_halt = false;
22
23 let (_wdt, [mut handle]) = match Watchdog::try_new(p.WDT, config) {
24 Ok(x) => x,
25 Err(_) => {
26 info!("Watchdog already active with wrong config, waiting for it to timeout...");
27 loop {}
28 }
29 };
30
31 let mut button = Input::new(p.P0_11, Pull::Up);
32
33 info!("Watchdog started, press button 1 to pet it or I'll reset in 3 seconds!");
34
35 loop {
36 button.wait_for_high().await;
37 button.wait_for_low().await;
38 info!("Button pressed, petting watchdog!");
39 handle.pet();
40 }
41}
diff --git a/examples/nrf52840/src/bin/wifi_esp_hosted.rs b/examples/nrf52840/src/bin/wifi_esp_hosted.rs
new file mode 100644
index 000000000..112e41bcd
--- /dev/null
+++ b/examples/nrf52840/src/bin/wifi_esp_hosted.rs
@@ -0,0 +1,143 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5use defmt::{info, unwrap, warn};
6use embassy_executor::Spawner;
7use embassy_net::tcp::TcpSocket;
8use embassy_net::{Stack, StackResources};
9use embassy_nrf::gpio::{AnyPin, Input, Level, Output, OutputDrive, Pin, Pull};
10use embassy_nrf::rng::Rng;
11use embassy_nrf::spim::{self, Spim};
12use embassy_nrf::{bind_interrupts, peripherals};
13use embassy_time::Delay;
14use embedded_hal_async::spi::ExclusiveDevice;
15use embedded_io::asynch::Write;
16use static_cell::make_static;
17use {defmt_rtt as _, embassy_net_esp_hosted as hosted, panic_probe as _};
18
19const WIFI_NETWORK: &str = "EmbassyTest";
20const WIFI_PASSWORD: &str = "V8YxhKt5CdIAJFud";
21
22bind_interrupts!(struct Irqs {
23 SPIM3 => spim::InterruptHandler<peripherals::SPI3>;
24 RNG => embassy_nrf::rng::InterruptHandler<peripherals::RNG>;
25});
26
27#[embassy_executor::task]
28async fn wifi_task(
29 runner: hosted::Runner<
30 'static,
31 ExclusiveDevice<Spim<'static, peripherals::SPI3>, Output<'static, peripherals::P0_31>, Delay>,
32 Input<'static, AnyPin>,
33 Output<'static, peripherals::P1_05>,
34 >,
35) -> ! {
36 runner.run().await
37}
38
39#[embassy_executor::task]
40async fn net_task(stack: &'static Stack<hosted::NetDriver<'static>>) -> ! {
41 stack.run().await
42}
43
44#[embassy_executor::main]
45async fn main(spawner: Spawner) {
46 info!("Hello World!");
47
48 let p = embassy_nrf::init(Default::default());
49
50 let miso = p.P0_28;
51 let sck = p.P0_29;
52 let mosi = p.P0_30;
53 let cs = Output::new(p.P0_31, Level::High, OutputDrive::HighDrive);
54 let handshake = Input::new(p.P1_01.degrade(), Pull::Up);
55 let ready = Input::new(p.P1_04.degrade(), Pull::None);
56 let reset = Output::new(p.P1_05, Level::Low, OutputDrive::Standard);
57
58 let mut config = spim::Config::default();
59 config.frequency = spim::Frequency::M32;
60 config.mode = spim::MODE_2; // !!!
61 let spi = spim::Spim::new(p.SPI3, Irqs, sck, miso, mosi, config);
62 let spi = ExclusiveDevice::new(spi, cs, Delay);
63
64 let (device, mut control, runner) = embassy_net_esp_hosted::new(
65 make_static!(embassy_net_esp_hosted::State::new()),
66 spi,
67 handshake,
68 ready,
69 reset,
70 )
71 .await;
72
73 unwrap!(spawner.spawn(wifi_task(runner)));
74
75 control.init().await;
76 control.join(WIFI_NETWORK, WIFI_PASSWORD).await;
77
78 let config = embassy_net::Config::dhcpv4(Default::default());
79 // let config = embassy_net::Config::ipv4_static(embassy_net::StaticConfigV4 {
80 // address: Ipv4Cidr::new(Ipv4Address::new(10, 42, 0, 61), 24),
81 // dns_servers: Vec::new(),
82 // gateway: Some(Ipv4Address::new(10, 42, 0, 1)),
83 // });
84
85 // Generate random seed
86 let mut rng = Rng::new(p.RNG, Irqs);
87 let mut seed = [0; 8];
88 rng.blocking_fill_bytes(&mut seed);
89 let seed = u64::from_le_bytes(seed);
90
91 // Init network stack
92 let stack = &*make_static!(Stack::new(
93 device,
94 config,
95 make_static!(StackResources::<2>::new()),
96 seed
97 ));
98
99 unwrap!(spawner.spawn(net_task(stack)));
100
101 // And now we can use it!
102
103 let mut rx_buffer = [0; 4096];
104 let mut tx_buffer = [0; 4096];
105 let mut buf = [0; 4096];
106
107 loop {
108 let mut socket = TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer);
109 socket.set_timeout(Some(embassy_time::Duration::from_secs(10)));
110
111 info!("Listening on TCP:1234...");
112 if let Err(e) = socket.accept(1234).await {
113 warn!("accept error: {:?}", e);
114 continue;
115 }
116
117 info!("Received connection from {:?}", socket.remote_endpoint());
118
119 loop {
120 let n = match socket.read(&mut buf).await {
121 Ok(0) => {
122 warn!("read EOF");
123 break;
124 }
125 Ok(n) => n,
126 Err(e) => {
127 warn!("read error: {:?}", e);
128 break;
129 }
130 };
131
132 info!("rxd {:02x}", &buf[..n]);
133
134 match socket.write_all(&buf[..n]).await {
135 Ok(()) => {}
136 Err(e) => {
137 warn!("write error: {:?}", e);
138 break;
139 }
140 };
141 }
142 }
143}