Make the sequence a little nicer to pass around

This commit is contained in:
huntc 2022-01-28 13:38:20 +11:00
parent 47aeab152f
commit 12ce024574
4 changed files with 52 additions and 44 deletions

View File

@ -137,19 +137,16 @@ impl<'d, T: Instance> SequencePwm<'d, T> {
#[inline(always)]
pub fn start(
&mut self,
sequence0: &'d [u16],
sequence_config0: SequenceConfig,
sequence1: Option<&'d [u16]>,
sequence_config1: Option<SequenceConfig>,
sequence0: Sequence<'d>,
sequence1: Option<Sequence<'d>>,
times: SequenceMode,
) -> Result<(), Error> {
let alt_sequence = sequence1.unwrap_or(sequence0);
let alt_sequence_config = (&sequence_config1).as_ref().unwrap_or(&sequence_config0);
let alt_sequence = sequence1.as_ref().unwrap_or(&sequence0);
slice_in_ram_or(sequence0, Error::DMABufferNotInDataMemory)?;
slice_in_ram_or(alt_sequence, Error::DMABufferNotInDataMemory)?;
slice_in_ram_or(sequence0.words, Error::DMABufferNotInDataMemory)?;
slice_in_ram_or(alt_sequence.words, Error::DMABufferNotInDataMemory)?;
if sequence0.len() > MAX_SEQUENCE_LEN || alt_sequence.len() > MAX_SEQUENCE_LEN {
if sequence0.words.len() > MAX_SEQUENCE_LEN || alt_sequence.words.len() > MAX_SEQUENCE_LEN {
return Err(Error::SequenceTooLong);
}
@ -163,29 +160,29 @@ impl<'d, T: Instance> SequencePwm<'d, T> {
r.seq0
.refresh
.write(|w| unsafe { w.bits(sequence_config0.refresh) });
.write(|w| unsafe { w.bits(sequence0.config.refresh) });
r.seq0
.enddelay
.write(|w| unsafe { w.bits(sequence_config0.end_delay) });
.write(|w| unsafe { w.bits(sequence0.config.end_delay) });
r.seq0
.ptr
.write(|w| unsafe { w.bits(sequence0.as_ptr() as u32) });
.write(|w| unsafe { w.bits(sequence0.words.as_ptr() as u32) });
r.seq0
.cnt
.write(|w| unsafe { w.bits(sequence0.len() as u32) });
.write(|w| unsafe { w.bits(sequence0.words.len() as u32) });
r.seq1
.refresh
.write(|w| unsafe { w.bits(alt_sequence_config.refresh) });
.write(|w| unsafe { w.bits(alt_sequence.config.refresh) });
r.seq1
.enddelay
.write(|w| unsafe { w.bits(alt_sequence_config.end_delay) });
.write(|w| unsafe { w.bits(alt_sequence.config.end_delay) });
r.seq1
.ptr
.write(|w| unsafe { w.bits(alt_sequence.as_ptr() as u32) });
.write(|w| unsafe { w.bits(alt_sequence.words.as_ptr() as u32) });
r.seq1
.cnt
.write(|w| unsafe { w.bits(alt_sequence.len() as u32) });
.write(|w| unsafe { w.bits(alt_sequence.words.len() as u32) });
r.enable.write(|w| w.enable().enabled());
@ -400,6 +397,7 @@ impl Default for Config {
}
#[non_exhaustive]
#[derive(Clone)]
pub struct SequenceConfig {
/// Number of PWM periods to delay between each sequence sample
pub refresh: u32,
@ -416,6 +414,20 @@ impl Default for SequenceConfig {
}
}
#[non_exhaustive]
pub struct Sequence<'d> {
/// The words comprising the sequence. Must not exceed 32767 words.
pub words: &'d [u16],
/// Configuration associated with the sequence.
pub config: SequenceConfig,
}
impl<'d> Sequence<'d> {
pub fn new(words: &'d [u16], config: SequenceConfig) -> Self {
Self { words, config }
}
}
/// How many times to run the sequence
#[derive(Debug, Eq, PartialEq, Clone, Copy)]
pub enum SequenceMode {

View File

@ -8,13 +8,13 @@ use defmt::*;
use embassy::executor::Spawner;
use embassy::time::{Duration, Timer};
use embassy_nrf::gpio::NoPin;
use embassy_nrf::pwm::{Config, Prescaler, SequenceMode, SequencePwm};
use embassy_nrf::pwm::{Config, Prescaler, Sequence, SequenceConfig, SequenceMode, SequencePwm};
use embassy_nrf::Peripherals;
#[embassy::main]
async fn main(_spawner: Spawner, p: Peripherals) {
let seq_values_1: [u16; 5] = [1000, 250, 100, 50, 0];
let seq_values_2: [u16; 5] = [0, 50, 100, 250, 1000];
let seq_words_1: [u16; 5] = [1000, 250, 100, 50, 0];
let seq_words_2: [u16; 5] = [0, 50, 100, 250, 1000];
let mut config = Config::default();
config.prescaler = Prescaler::Div128;
@ -22,7 +22,7 @@ async fn main(_spawner: Spawner, p: Peripherals) {
// but say we want to hold the value for 5000ms
// so we want to repeat our value as many times as necessary until 5000ms passes
// want 5000/8 = 625 periods total to occur, so 624 (we get the one period for free remember)
let mut seq_config = Config::default();
let mut seq_config = SequenceConfig::default();
seq_config.refresh = 624;
// thus our sequence takes 5 * 5000ms or 25 seconds
@ -30,11 +30,9 @@ async fn main(_spawner: Spawner, p: Peripherals) {
p.PWM0, p.P0_13, NoPin, NoPin, NoPin, config,
));
let _ = pwm.start(
&seq_values_1,
seq_config,
Sequence::new(&seq_words_1, seq_config.clone()),
None,
None,
SeqSequenceMode::Infinite,
SequenceMode::Infinite,
);
info!("pwm started!");
@ -43,9 +41,7 @@ async fn main(_spawner: Spawner, p: Peripherals) {
info!("pwm starting with another sequence!");
let _ = pwm.start(
&seq_values_2,
seq_config,
None,
Sequence::new(&seq_words_2, seq_config),
None,
SequenceMode::Infinite,
);

View File

@ -11,12 +11,12 @@ use embassy::executor::Spawner;
use embassy_nrf::gpio::{Input, NoPin, Pull};
use embassy_nrf::gpiote::{InputChannel, InputChannelPolarity};
use embassy_nrf::ppi::Ppi;
use embassy_nrf::pwm::{Config, Prescaler, SequenceConfig, SequenceMode, SequencePwm};
use embassy_nrf::pwm::{Config, Prescaler, Sequence, SequenceConfig, SequenceMode, SequencePwm};
use embassy_nrf::Peripherals;
#[embassy::main]
async fn main(_spawner: Spawner, p: Peripherals) {
let seq_values: [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,7 +31,11 @@ async fn main(_spawner: Spawner, p: Peripherals) {
p.PWM0, p.P0_13, NoPin, NoPin, NoPin, config,
));
let _ = pwm.start(&seq_values, seq_config, None, None, SequenceMode::Infinite);
let _ = pwm.start(
Sequence::new(&seq_words, seq_config),
None,
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

View File

@ -9,7 +9,7 @@ use embassy::executor::Spawner;
use embassy::time::{Duration, Timer};
use embassy_nrf::gpio::NoPin;
use embassy_nrf::pwm::{
Config, Prescaler, SequenceConfig, SequenceLoad, SequenceMode, SequencePwm,
Config, Prescaler, Sequence, SequenceConfig, SequenceLoad, SequenceMode, SequencePwm,
};
use embassy_nrf::Peripherals;
@ -29,12 +29,17 @@ const RES: u16 = 0x8000;
#[embassy::main]
async fn main(_spawner: Spawner, p: Peripherals) {
// Declare the bits of 24 bits
let mut blue_seq: [u16; 8 * 3] = [
let blue_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 reset_seq = [RES; 1];
let blue_seq = Sequence::new(&blue_seq_words, SequenceConfig::default());
let 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(&reset_seq_words, reset_seq_config);
let mut config = Config::default();
config.sequence_load = SequenceLoad::Common;
@ -44,16 +49,7 @@ async fn main(_spawner: Spawner, p: Peripherals) {
p.PWM0, p.P1_05, NoPin, NoPin, NoPin, config,
));
let blue_seq_config = 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
unwrap!(pwm.start(
&blue_seq,
blue_seq_config,
Some(&reset_seq),
Some(reset_seq_config),
SequenceMode::Times(2)
));
unwrap!(pwm.start(blue_seq, Some(reset_seq), SequenceMode::Times(2)));
Timer::after(Duration::from_millis(20000)).await;
info!("Program stopped");