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:
@ -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
|
||||
|
@ -16,7 +16,7 @@ use embassy_nrf::Peripherals;
|
||||
|
||||
#[embassy::main]
|
||||
async fn main(_spawner: Spawner, p: Peripherals) {
|
||||
let mut seq_words: [u16; 5] = [1000, 250, 100, 50, 0];
|
||||
let seq_words: [u16; 5] = [1000, 250, 100, 50, 0];
|
||||
|
||||
let mut config = Config::default();
|
||||
config.prescaler = Prescaler::Div128;
|
||||
@ -31,11 +31,12 @@ async fn main(_spawner: Spawner, p: Peripherals) {
|
||||
p.PWM0, p.P0_13, NoPin, NoPin, NoPin, config,
|
||||
));
|
||||
|
||||
let _ = pwm.start(
|
||||
Sequence::new(&mut seq_words, seq_config),
|
||||
None,
|
||||
SequenceMode::Infinite,
|
||||
);
|
||||
// If we loop in any way i.e. not Times(1), then we must provide
|
||||
// the PWM peripheral with two sequences.
|
||||
let seq_0 = Sequence::new(seq_words, seq_config);
|
||||
let seq_1 = seq_0.clone();
|
||||
|
||||
unwrap!(pwm.start(seq_0, seq_1, SequenceMode::Infinite));
|
||||
// pwm.stop() deconfigures pins, and then the task_start_seq0 task cant work
|
||||
// so its going to have to start running in order load the configuration
|
||||
|
||||
|
@ -30,19 +30,6 @@ const RES: u16 = 0x8000;
|
||||
// line is assumed to be P1_05.
|
||||
#[embassy::main]
|
||||
async fn main(_spawner: Spawner, p: Peripherals) {
|
||||
// Declare the bits of 24 bits
|
||||
let mut color_seq_words = [
|
||||
T0H, T0H, T0H, T0H, T0H, T0H, T0H, T0H, // G
|
||||
T0H, T0H, T0H, T0H, T0H, T0H, T0H, T0H, // R
|
||||
T1H, T1H, T1H, T1H, T1H, T1H, T1H, T1H, // B
|
||||
];
|
||||
let color_seq = Sequence::new(&mut color_seq_words, SequenceConfig::default());
|
||||
|
||||
let mut reset_seq_words = [RES; 1];
|
||||
let mut reset_seq_config = SequenceConfig::default();
|
||||
reset_seq_config.end_delay = 799; // 50us (20 ticks * 40) - 1 tick because we've already got one RES;
|
||||
let reset_seq = Sequence::new(&mut reset_seq_words, reset_seq_config);
|
||||
|
||||
let mut config = Config::default();
|
||||
config.sequence_load = SequenceLoad::Common;
|
||||
config.prescaler = Prescaler::Div1;
|
||||
@ -51,7 +38,21 @@ async fn main(_spawner: Spawner, p: Peripherals) {
|
||||
p.PWM0, p.P1_05, NoPin, NoPin, NoPin, config,
|
||||
));
|
||||
|
||||
unwrap!(pwm.start(color_seq, Some(reset_seq), SequenceMode::Times(2)));
|
||||
// Declare the bits of 24 bits
|
||||
let color_seq = Sequence::new(
|
||||
[
|
||||
T0H, T0H, T0H, T0H, T0H, T0H, T0H, T0H, // G
|
||||
T0H, T0H, T0H, T0H, T0H, T0H, T0H, T0H, // R
|
||||
T1H, T1H, T1H, T1H, T1H, T1H, T1H, T1H, // B
|
||||
],
|
||||
SequenceConfig::default(),
|
||||
);
|
||||
|
||||
let mut reset_seq_config = SequenceConfig::default();
|
||||
reset_seq_config.end_delay = 799; // 50us (20 ticks * 40) - 1 tick because we've already got one RES;
|
||||
let reset_seq = Sequence::new([RES], reset_seq_config);
|
||||
|
||||
unwrap!(pwm.start(color_seq, reset_seq, SequenceMode::Times(2)));
|
||||
|
||||
Timer::after(Duration::from_millis(1000)).await;
|
||||
|
||||
@ -59,9 +60,9 @@ async fn main(_spawner: Spawner, p: Peripherals) {
|
||||
let mut bit_value = T0H;
|
||||
|
||||
loop {
|
||||
if let (Some(color_seq), Some(reset_seq)) = pwm.stop() {
|
||||
if let (Some(mut color_seq), Some(reset_seq)) = pwm.stop() {
|
||||
color_seq.words[color_bit] = bit_value;
|
||||
unwrap!(pwm.start(color_seq, Some(reset_seq), SequenceMode::Times(2)));
|
||||
unwrap!(pwm.start(color_seq, reset_seq, SequenceMode::Times(2)));
|
||||
}
|
||||
|
||||
Timer::after(Duration::from_millis(50)).await;
|
||||
|
Reference in New Issue
Block a user