aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/nrf/src/bin/pwm_sequence_ppi.rs73
1 files changed, 73 insertions, 0 deletions
diff --git a/examples/nrf/src/bin/pwm_sequence_ppi.rs b/examples/nrf/src/bin/pwm_sequence_ppi.rs
new file mode 100644
index 000000000..aaea9ff00
--- /dev/null
+++ b/examples/nrf/src/bin/pwm_sequence_ppi.rs
@@ -0,0 +1,73 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4#![feature(array_from_fn)]
5
6#[path = "../example_common.rs"]
7mod example_common;
8use core::future::pending;
9use defmt::*;
10use embassy::executor::Spawner;
11use embassy_nrf::gpio::{Input, NoPin, Pull};
12use embassy_nrf::gpiote::{InputChannel, InputChannelPolarity};
13use embassy_nrf::ppi::Ppi;
14use embassy_nrf::pwm::{Prescaler, SequenceConfig, SequenceMode, SequencePwm};
15use embassy_nrf::Peripherals;
16
17#[embassy::main]
18async fn main(_spawner: Spawner, p: Peripherals) {
19 let seq_values: [u16; 5] = [1000, 250, 100, 50, 0];
20
21 let mut config = SequenceConfig::default();
22 config.prescaler = Prescaler::Div128;
23 // 1 period is 1000 * (128/16mhz = 0.000008s = 0.008ms) = 8ms
24 // but say we want to hold the value for 250ms 250ms/8 = 31.25 periods
25 // so round to 31 - 1 (we get the one period for free remember)
26 // thus our sequence takes 5 * 250ms or 1.25 seconds
27 config.refresh = 30;
28
29 let pwm = unwrap!(SequencePwm::new(
30 p.PWM0,
31 p.P0_13,
32 NoPin,
33 NoPin,
34 NoPin,
35 config,
36 &seq_values
37 ));
38
39 let _ = pwm.start(SequenceMode::Times(1));
40 // pwm.stop() deconfigures pins, and then the task_start_seq0 task cant work
41 // so its going to have to start running in order load the configuration
42
43 let button1 = InputChannel::new(
44 p.GPIOTE_CH0,
45 Input::new(p.P0_11, Pull::Up),
46 InputChannelPolarity::HiToLo,
47 );
48
49 let button2 = InputChannel::new(
50 p.GPIOTE_CH1,
51 Input::new(p.P0_12, Pull::Up),
52 InputChannelPolarity::HiToLo,
53 );
54
55 // messing with the pwm tasks is ill advised
56 // Times::Ininite and Times even are seq0, Times odd is seq1
57 let start = unsafe { pwm.task_start_seq0() };
58 let stop = unsafe { pwm.task_stop() };
59
60 let mut ppi = Ppi::new_one_to_one(p.PPI_CH1, button1.event_in(), start);
61 ppi.enable();
62
63 let mut ppi2 = Ppi::new_one_to_one(p.PPI_CH0, button2.event_in(), stop);
64 ppi2.enable();
65
66 info!("PPI setup!");
67 info!("Press button 1 to start LED 1");
68 info!("Press button 2 to stop LED 1");
69 info!("Note! task_stop stops the sequence, but not the pin output");
70
71 // Block forever so the above drivers don't get dropped
72 pending::<()>().await;
73}