diff options
Diffstat (limited to 'examples/nrf/src/bin/pwm_sequence.rs')
| -rw-r--r-- | examples/nrf/src/bin/pwm_sequence.rs | 21 |
1 files changed, 9 insertions, 12 deletions
diff --git a/examples/nrf/src/bin/pwm_sequence.rs b/examples/nrf/src/bin/pwm_sequence.rs index 56c865d1c..f06ea0b19 100644 --- a/examples/nrf/src/bin/pwm_sequence.rs +++ b/examples/nrf/src/bin/pwm_sequence.rs | |||
| @@ -8,34 +8,31 @@ use defmt::*; | |||
| 8 | use embassy::executor::Spawner; | 8 | use embassy::executor::Spawner; |
| 9 | use embassy::time::{Duration, Timer}; | 9 | use embassy::time::{Duration, Timer}; |
| 10 | use embassy_nrf::gpio::NoPin; | 10 | use embassy_nrf::gpio::NoPin; |
| 11 | use embassy_nrf::pwm::{Prescaler, SequenceConfig, SequenceMode, SequencePwm}; | 11 | use embassy_nrf::pwm::{ |
| 12 | Config, Prescaler, SequenceConfig, SequencePwm, SingleSequenceMode, SingleSequencer, | ||
| 13 | }; | ||
| 12 | use embassy_nrf::Peripherals; | 14 | use embassy_nrf::Peripherals; |
| 13 | 15 | ||
| 14 | #[embassy::main] | 16 | #[embassy::main] |
| 15 | async fn main(_spawner: Spawner, p: Peripherals) { | 17 | async fn main(_spawner: Spawner, p: Peripherals) { |
| 16 | let seq_values_1: [u16; 5] = [1000, 250, 100, 50, 0]; | 18 | let seq_words: [u16; 5] = [1000, 250, 100, 50, 0]; |
| 17 | let seq_values_2: [u16; 5] = [0, 50, 100, 250, 1000]; | ||
| 18 | 19 | ||
| 19 | let mut config = SequenceConfig::default(); | 20 | let mut config = Config::default(); |
| 20 | config.prescaler = Prescaler::Div128; | 21 | config.prescaler = Prescaler::Div128; |
| 21 | // 1 period is 1000 * (128/16mhz = 0.000008s = 0.008ms) = 8us | 22 | // 1 period is 1000 * (128/16mhz = 0.000008s = 0.008ms) = 8us |
| 22 | // but say we want to hold the value for 5000ms | 23 | // 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 | // 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 | // want 5000/8 = 625 periods total to occur, so 624 (we get the one period for free remember) |
| 25 | config.refresh = 624; | 26 | let mut seq_config = SequenceConfig::default(); |
| 27 | seq_config.refresh = 624; | ||
| 26 | // thus our sequence takes 5 * 5000ms or 25 seconds | 28 | // thus our sequence takes 5 * 5000ms or 25 seconds |
| 27 | 29 | ||
| 28 | let mut pwm = unwrap!(SequencePwm::new( | 30 | let mut pwm = unwrap!(SequencePwm::new( |
| 29 | p.PWM0, p.P0_13, NoPin, NoPin, NoPin, config, | 31 | p.PWM0, p.P0_13, NoPin, NoPin, NoPin, config, |
| 30 | )); | 32 | )); |
| 31 | let _ = pwm.start(&seq_values_1, SequenceMode::Infinite); | ||
| 32 | 33 | ||
| 33 | info!("pwm started!"); | 34 | let sequencer = SingleSequencer::new(&mut pwm, &seq_words, seq_config); |
| 34 | 35 | unwrap!(sequencer.start(SingleSequenceMode::Times(1))); | |
| 35 | Timer::after(Duration::from_millis(20000)).await; | ||
| 36 | info!("pwm starting with another sequence!"); | ||
| 37 | |||
| 38 | let _ = pwm.start(&seq_values_2, SequenceMode::Infinite); | ||
| 39 | 36 | ||
| 40 | // we can abort a sequence if we need to before its complete with pwm.stop() | 37 | // we can abort a sequence if we need to before its complete with pwm.stop() |
| 41 | // or stop is also implicitly called when the pwm peripheral is dropped | 38 | // or stop is also implicitly called when the pwm peripheral is dropped |
