aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorhuntc <[email protected]>2022-02-04 19:11:15 +1100
committerhuntc <[email protected]>2022-02-04 19:11:15 +1100
commit965a5f2c3fba365519bed1c2a955145783d6a05b (patch)
treecc0b60b0a809d01e0e2634032693e6c4bae879d4 /examples
parent9e36ede363b66c3e007d8cb0c477234b88ba0737 (diff)
Introduced the SingleSequencer and a more complex Sequencer
Diffstat (limited to 'examples')
-rw-r--r--examples/nrf/src/bin/pwm_double_sequence.rs46
-rw-r--r--examples/nrf/src/bin/pwm_sequence.rs22
-rw-r--r--examples/nrf/src/bin/pwm_sequence_ppi.rs14
-rw-r--r--examples/nrf/src/bin/pwm_sequence_ws2812b.rs7
4 files changed, 62 insertions, 27 deletions
diff --git a/examples/nrf/src/bin/pwm_double_sequence.rs b/examples/nrf/src/bin/pwm_double_sequence.rs
new file mode 100644
index 000000000..269015f4a
--- /dev/null
+++ b/examples/nrf/src/bin/pwm_double_sequence.rs
@@ -0,0 +1,46 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5#[path = "../example_common.rs"]
6mod example_common;
7use defmt::*;
8use embassy::executor::Spawner;
9use embassy::time::{Duration, Timer};
10use embassy_nrf::gpio::NoPin;
11use embassy_nrf::pwm::{
12 Config, Prescaler, Sequence, SequenceConfig, SequenceMode, SequencePwm, Sequencer,
13 StartSequence,
14};
15use embassy_nrf::Peripherals;
16
17#[embassy::main]
18async fn main(_spawner: Spawner, p: Peripherals) {
19 let seq_words_0: [u16; 5] = [1000, 250, 100, 50, 0];
20 let seq_words_1: [u16; 4] = [50, 100, 250, 1000];
21
22 let mut config = Config::default();
23 config.prescaler = Prescaler::Div128;
24 // 1 period is 1000 * (128/16mhz = 0.000008s = 0.008ms) = 8us
25 // but say we want to hold the value for 5000ms
26 // so we want to repeat our value as many times as necessary until 5000ms passes
27 // want 5000/8 = 625 periods total to occur, so 624 (we get the one period for free remember)
28 let mut seq_config = SequenceConfig::default();
29 seq_config.refresh = 624;
30 // thus our sequence takes 5 * 5000ms or 25 seconds
31
32 let mut pwm = unwrap!(SequencePwm::new(
33 p.PWM0, p.P0_13, NoPin, NoPin, NoPin, config,
34 ));
35
36 let sequence_0 = Sequence::new(&seq_words_0, seq_config.clone());
37 let sequence_1 = Sequence::new(&seq_words_1, seq_config);
38 let sequencer = Sequencer::new(&mut pwm, sequence_0, Some(sequence_1));
39 unwrap!(sequencer.start(StartSequence::Zero, SequenceMode::Loop(1)));
40
41 // we can abort a sequence if we need to before its complete with pwm.stop()
42 // or stop is also implicitly called when the pwm peripheral is dropped
43 // when it goes out of scope
44 Timer::after(Duration::from_millis(40000)).await;
45 info!("pwm stopped early!");
46}
diff --git a/examples/nrf/src/bin/pwm_sequence.rs b/examples/nrf/src/bin/pwm_sequence.rs
index b31c12a23..761ac0f03 100644
--- a/examples/nrf/src/bin/pwm_sequence.rs
+++ b/examples/nrf/src/bin/pwm_sequence.rs
@@ -9,14 +9,13 @@ use embassy::executor::Spawner;
9use embassy::time::{Duration, Timer}; 9use embassy::time::{Duration, Timer};
10use embassy_nrf::gpio::NoPin; 10use embassy_nrf::gpio::NoPin;
11use embassy_nrf::pwm::{ 11use embassy_nrf::pwm::{
12 Config, Prescaler, Sequence, SequenceConfig, SequenceMode, SequencePwm, Sequences, 12 Config, Prescaler, Sequence, SequenceConfig, SequencePwm, SingleSequenceMode, SingleSequencer,
13}; 13};
14use embassy_nrf::Peripherals; 14use embassy_nrf::Peripherals;
15 15
16#[embassy::main] 16#[embassy::main]
17async fn main(_spawner: Spawner, p: Peripherals) { 17async fn main(_spawner: Spawner, p: Peripherals) {
18 let seq_words_1: [u16; 5] = [1000, 250, 100, 50, 0]; 18 let seq_words: [u16; 5] = [1000, 250, 100, 50, 0];
19 let seq_words_2: [u16; 5] = [0, 50, 100, 250, 1000];
20 19
21 let mut config = Config::default(); 20 let mut config = Config::default();
22 config.prescaler = Prescaler::Div128; 21 config.prescaler = Prescaler::Div128;
@@ -32,20 +31,9 @@ async fn main(_spawner: Spawner, p: Peripherals) {
32 p.PWM0, p.P0_13, NoPin, NoPin, NoPin, config, 31 p.PWM0, p.P0_13, NoPin, NoPin, NoPin, config,
33 )); 32 ));
34 33
35 let sequence0 = Sequence::new(&seq_words_1, seq_config.clone()); 34 let sequence = Sequence::new(&seq_words, seq_config.clone());
36 let sequences = Sequences::new(&mut pwm, sequence0, None); 35 let sequencer = SingleSequencer::new(&mut pwm, sequence);
37 unwrap!(sequences.start(SequenceMode::Times(1))); 36 unwrap!(sequencer.start(SingleSequenceMode::Times(1)));
38
39 info!("pwm started!");
40
41 Timer::after(Duration::from_millis(20000)).await;
42 info!("pwm starting with another sequence!");
43
44 drop(sequences); // This stops the previous sequence and returns pwm ownership back
45
46 let sequence0 = Sequence::new(&seq_words_2, seq_config);
47 let sequences = Sequences::new(&mut pwm, sequence0, None);
48 unwrap!(sequences.start(SequenceMode::Times(1)));
49 37
50 // we can abort a sequence if we need to before its complete with pwm.stop() 38 // we can abort a sequence if we need to before its complete with pwm.stop()
51 // or stop is also implicitly called when the pwm peripheral is dropped 39 // or stop is also implicitly called when the pwm peripheral is dropped
diff --git a/examples/nrf/src/bin/pwm_sequence_ppi.rs b/examples/nrf/src/bin/pwm_sequence_ppi.rs
index 593e7590d..7e58c37e6 100644
--- a/examples/nrf/src/bin/pwm_sequence_ppi.rs
+++ b/examples/nrf/src/bin/pwm_sequence_ppi.rs
@@ -12,7 +12,7 @@ use embassy_nrf::gpio::{Input, NoPin, Pull};
12use embassy_nrf::gpiote::{InputChannel, InputChannelPolarity}; 12use embassy_nrf::gpiote::{InputChannel, InputChannelPolarity};
13use embassy_nrf::ppi::Ppi; 13use embassy_nrf::ppi::Ppi;
14use embassy_nrf::pwm::{ 14use embassy_nrf::pwm::{
15 Config, Prescaler, Sequence, SequenceConfig, SequenceMode, SequencePwm, Sequences, 15 Config, Prescaler, Sequence, SequenceConfig, SequencePwm, SingleSequenceMode, SingleSequencer,
16}; 16};
17use embassy_nrf::Peripherals; 17use embassy_nrf::Peripherals;
18 18
@@ -33,10 +33,6 @@ async fn main(_spawner: Spawner, p: Peripherals) {
33 p.PWM0, p.P0_13, NoPin, NoPin, NoPin, config, 33 p.PWM0, p.P0_13, NoPin, NoPin, NoPin, config,
34 )); 34 ));
35 35
36 let sequence0 = Sequence::new(&seq_words, seq_config);
37 let sequences = Sequences::new(&mut pwm, sequence0, None);
38 unwrap!(sequences.start(SequenceMode::Infinite));
39
40 // pwm.stop() deconfigures pins, and then the task_start_seq0 task cant work 36 // 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 37 // so its going to have to start running in order load the configuration
42 38
@@ -54,8 +50,12 @@ async fn main(_spawner: Spawner, p: Peripherals) {
54 50
55 // messing with the pwm tasks is ill advised 51 // messing with the pwm tasks is ill advised
56 // Times::Ininite and Times even are seq0, Times odd is seq1 52 // Times::Ininite and Times even are seq0, Times odd is seq1
57 let start = unsafe { sequences.pwm.task_start_seq0() }; 53 let start = unsafe { pwm.task_start_seq0() };
58 let stop = unsafe { sequences.pwm.task_stop() }; 54 let stop = unsafe { pwm.task_stop() };
55
56 let sequence = Sequence::new(&seq_words, seq_config);
57 let sequencer = SingleSequencer::new(&mut pwm, sequence);
58 unwrap!(sequencer.start(SingleSequenceMode::Infinite));
59 59
60 let mut ppi = Ppi::new_one_to_one(p.PPI_CH1, button1.event_in(), start); 60 let mut ppi = Ppi::new_one_to_one(p.PPI_CH1, button1.event_in(), start);
61 ppi.enable(); 61 ppi.enable();
diff --git a/examples/nrf/src/bin/pwm_sequence_ws2812b.rs b/examples/nrf/src/bin/pwm_sequence_ws2812b.rs
index c0c10373a..71ddd5283 100644
--- a/examples/nrf/src/bin/pwm_sequence_ws2812b.rs
+++ b/examples/nrf/src/bin/pwm_sequence_ws2812b.rs
@@ -9,7 +9,8 @@ use embassy::executor::Spawner;
9use embassy::time::{Duration, Timer}; 9use embassy::time::{Duration, Timer};
10use embassy_nrf::gpio::NoPin; 10use embassy_nrf::gpio::NoPin;
11use embassy_nrf::pwm::{ 11use embassy_nrf::pwm::{
12 Config, Prescaler, Sequence, SequenceConfig, SequenceLoad, SequenceMode, SequencePwm, Sequences, 12 Config, Prescaler, Sequence, SequenceConfig, SequenceLoad, SequencePwm, SingleSequenceMode,
13 SingleSequencer,
13}; 14};
14use embassy_nrf::Peripherals; 15use embassy_nrf::Peripherals;
15 16
@@ -54,8 +55,8 @@ async fn main(_spawner: Spawner, p: Peripherals) {
54 55
55 loop { 56 loop {
56 let sequence0 = Sequence::new(&seq_words, seq_config.clone()); 57 let sequence0 = Sequence::new(&seq_words, seq_config.clone());
57 let sequences = Sequences::new(&mut pwm, sequence0, None); 58 let sequences = SingleSequencer::new(&mut pwm, sequence0);
58 unwrap!(sequences.start(SequenceMode::Times(1))); 59 unwrap!(sequences.start(SingleSequenceMode::Times(1)));
59 60
60 Timer::after(Duration::from_millis(50)).await; 61 Timer::after(Duration::from_millis(50)).await;
61 62