Own the sequence buffer

This approach owns the sequence buffers which, while introducing an extra move, it eliminates the need to guard the lifetime of the sequence buffer. Given ownership, the buffer will be retained until the PWM sequence task is stopped.
This commit is contained in:
huntc
2022-01-29 15:26:31 +11:00
parent 9ac52a768b
commit 482389a691
4 changed files with 108 additions and 68 deletions

View File

@ -8,14 +8,13 @@ use defmt::*;
use embassy::executor::Spawner;
use embassy::time::{Duration, Timer};
use embassy_nrf::gpio::NoPin;
use embassy_nrf::pwm::{Config, Prescaler, Sequence, SequenceConfig, SequenceMode, SequencePwm};
use embassy_nrf::pwm::{
Config, Prescaler, Sequence, SequenceConfig, SequenceMode, SequencePwm, EMPTY_SEQ,
};
use embassy_nrf::Peripherals;
#[embassy::main]
async fn main(_spawner: Spawner, p: Peripherals) {
let mut seq_words_1: [u16; 5] = [1000, 250, 100, 50, 0];
let mut seq_words_2: [u16; 5] = [0, 50, 100, 250, 1000];
let mut config = Config::default();
config.prescaler = Prescaler::Div128;
// 1 period is 1000 * (128/16mhz = 0.000008s = 0.008ms) = 8us
@ -26,25 +25,20 @@ async fn main(_spawner: Spawner, p: Peripherals) {
seq_config.refresh = 624;
// thus our sequence takes 5 * 5000ms or 25 seconds
let seq_1 = Sequence::new([1000, 250, 100, 50, 0], seq_config.clone());
let seq_2 = Sequence::new([0, 50, 100, 250, 1000], seq_config);
let mut pwm = unwrap!(SequencePwm::new(
p.PWM0, p.P0_13, NoPin, NoPin, NoPin, config,
));
let _ = pwm.start(
Sequence::new(&mut seq_words_1, seq_config.clone()),
None,
SequenceMode::Infinite,
);
unwrap!(pwm.start(seq_1, EMPTY_SEQ, SequenceMode::Times(1)));
info!("pwm started!");
Timer::after(Duration::from_millis(20000)).await;
info!("pwm starting with another sequence!");
let _ = pwm.start(
Sequence::new(&mut seq_words_2, seq_config),
None,
SequenceMode::Infinite,
);
unwrap!(pwm.start(seq_2, EMPTY_SEQ, SequenceMode::Times(1)));
// we can abort a sequence if we need to before its complete with pwm.stop()
// or stop is also implicitly called when the pwm peripheral is dropped