diff options
Diffstat (limited to 'examples/nrf/src/bin/pwm_double_sequence.rs')
| -rw-r--r-- | examples/nrf/src/bin/pwm_double_sequence.rs | 46 |
1 files changed, 46 insertions, 0 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"] | ||
| 6 | mod example_common; | ||
| 7 | use defmt::*; | ||
| 8 | use embassy::executor::Spawner; | ||
| 9 | use embassy::time::{Duration, Timer}; | ||
| 10 | use embassy_nrf::gpio::NoPin; | ||
| 11 | use embassy_nrf::pwm::{ | ||
| 12 | Config, Prescaler, Sequence, SequenceConfig, SequenceMode, SequencePwm, Sequencer, | ||
| 13 | StartSequence, | ||
| 14 | }; | ||
| 15 | use embassy_nrf::Peripherals; | ||
| 16 | |||
| 17 | #[embassy::main] | ||
| 18 | async 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 | } | ||
