Make the sequence a little nicer to pass around
This commit is contained in:
parent
47aeab152f
commit
12ce024574
@ -137,19 +137,16 @@ impl<'d, T: Instance> SequencePwm<'d, T> {
|
|||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn start(
|
pub fn start(
|
||||||
&mut self,
|
&mut self,
|
||||||
sequence0: &'d [u16],
|
sequence0: Sequence<'d>,
|
||||||
sequence_config0: SequenceConfig,
|
sequence1: Option<Sequence<'d>>,
|
||||||
sequence1: Option<&'d [u16]>,
|
|
||||||
sequence_config1: Option<SequenceConfig>,
|
|
||||||
times: SequenceMode,
|
times: SequenceMode,
|
||||||
) -> Result<(), Error> {
|
) -> Result<(), Error> {
|
||||||
let alt_sequence = sequence1.unwrap_or(sequence0);
|
let alt_sequence = sequence1.as_ref().unwrap_or(&sequence0);
|
||||||
let alt_sequence_config = (&sequence_config1).as_ref().unwrap_or(&sequence_config0);
|
|
||||||
|
|
||||||
slice_in_ram_or(sequence0, Error::DMABufferNotInDataMemory)?;
|
slice_in_ram_or(sequence0.words, Error::DMABufferNotInDataMemory)?;
|
||||||
slice_in_ram_or(alt_sequence, 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);
|
return Err(Error::SequenceTooLong);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -163,29 +160,29 @@ impl<'d, T: Instance> SequencePwm<'d, T> {
|
|||||||
|
|
||||||
r.seq0
|
r.seq0
|
||||||
.refresh
|
.refresh
|
||||||
.write(|w| unsafe { w.bits(sequence_config0.refresh) });
|
.write(|w| unsafe { w.bits(sequence0.config.refresh) });
|
||||||
r.seq0
|
r.seq0
|
||||||
.enddelay
|
.enddelay
|
||||||
.write(|w| unsafe { w.bits(sequence_config0.end_delay) });
|
.write(|w| unsafe { w.bits(sequence0.config.end_delay) });
|
||||||
r.seq0
|
r.seq0
|
||||||
.ptr
|
.ptr
|
||||||
.write(|w| unsafe { w.bits(sequence0.as_ptr() as u32) });
|
.write(|w| unsafe { w.bits(sequence0.words.as_ptr() as u32) });
|
||||||
r.seq0
|
r.seq0
|
||||||
.cnt
|
.cnt
|
||||||
.write(|w| unsafe { w.bits(sequence0.len() as u32) });
|
.write(|w| unsafe { w.bits(sequence0.words.len() as u32) });
|
||||||
|
|
||||||
r.seq1
|
r.seq1
|
||||||
.refresh
|
.refresh
|
||||||
.write(|w| unsafe { w.bits(alt_sequence_config.refresh) });
|
.write(|w| unsafe { w.bits(alt_sequence.config.refresh) });
|
||||||
r.seq1
|
r.seq1
|
||||||
.enddelay
|
.enddelay
|
||||||
.write(|w| unsafe { w.bits(alt_sequence_config.end_delay) });
|
.write(|w| unsafe { w.bits(alt_sequence.config.end_delay) });
|
||||||
r.seq1
|
r.seq1
|
||||||
.ptr
|
.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
|
r.seq1
|
||||||
.cnt
|
.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());
|
r.enable.write(|w| w.enable().enabled());
|
||||||
|
|
||||||
@ -400,6 +397,7 @@ impl Default for Config {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[non_exhaustive]
|
#[non_exhaustive]
|
||||||
|
#[derive(Clone)]
|
||||||
pub struct SequenceConfig {
|
pub struct SequenceConfig {
|
||||||
/// Number of PWM periods to delay between each sequence sample
|
/// Number of PWM periods to delay between each sequence sample
|
||||||
pub refresh: u32,
|
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
|
/// How many times to run the sequence
|
||||||
#[derive(Debug, Eq, PartialEq, Clone, Copy)]
|
#[derive(Debug, Eq, PartialEq, Clone, Copy)]
|
||||||
pub enum SequenceMode {
|
pub enum SequenceMode {
|
||||||
|
@ -8,13 +8,13 @@ use defmt::*;
|
|||||||
use embassy::executor::Spawner;
|
use embassy::executor::Spawner;
|
||||||
use embassy::time::{Duration, Timer};
|
use embassy::time::{Duration, Timer};
|
||||||
use embassy_nrf::gpio::NoPin;
|
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;
|
use embassy_nrf::Peripherals;
|
||||||
|
|
||||||
#[embassy::main]
|
#[embassy::main]
|
||||||
async fn main(_spawner: Spawner, p: Peripherals) {
|
async fn main(_spawner: Spawner, p: Peripherals) {
|
||||||
let seq_values_1: [u16; 5] = [1000, 250, 100, 50, 0];
|
let seq_words_1: [u16; 5] = [1000, 250, 100, 50, 0];
|
||||||
let seq_values_2: [u16; 5] = [0, 50, 100, 250, 1000];
|
let seq_words_2: [u16; 5] = [0, 50, 100, 250, 1000];
|
||||||
|
|
||||||
let mut config = Config::default();
|
let mut config = Config::default();
|
||||||
config.prescaler = Prescaler::Div128;
|
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
|
// 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
|
// 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)
|
// 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;
|
seq_config.refresh = 624;
|
||||||
// thus our sequence takes 5 * 5000ms or 25 seconds
|
// 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,
|
p.PWM0, p.P0_13, NoPin, NoPin, NoPin, config,
|
||||||
));
|
));
|
||||||
let _ = pwm.start(
|
let _ = pwm.start(
|
||||||
&seq_values_1,
|
Sequence::new(&seq_words_1, seq_config.clone()),
|
||||||
seq_config,
|
|
||||||
None,
|
None,
|
||||||
None,
|
SequenceMode::Infinite,
|
||||||
SeqSequenceMode::Infinite,
|
|
||||||
);
|
);
|
||||||
|
|
||||||
info!("pwm started!");
|
info!("pwm started!");
|
||||||
@ -43,9 +41,7 @@ async fn main(_spawner: Spawner, p: Peripherals) {
|
|||||||
info!("pwm starting with another sequence!");
|
info!("pwm starting with another sequence!");
|
||||||
|
|
||||||
let _ = pwm.start(
|
let _ = pwm.start(
|
||||||
&seq_values_2,
|
Sequence::new(&seq_words_2, seq_config),
|
||||||
seq_config,
|
|
||||||
None,
|
|
||||||
None,
|
None,
|
||||||
SequenceMode::Infinite,
|
SequenceMode::Infinite,
|
||||||
);
|
);
|
||||||
|
@ -11,12 +11,12 @@ use embassy::executor::Spawner;
|
|||||||
use embassy_nrf::gpio::{Input, NoPin, Pull};
|
use embassy_nrf::gpio::{Input, NoPin, Pull};
|
||||||
use embassy_nrf::gpiote::{InputChannel, InputChannelPolarity};
|
use embassy_nrf::gpiote::{InputChannel, InputChannelPolarity};
|
||||||
use embassy_nrf::ppi::Ppi;
|
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;
|
use embassy_nrf::Peripherals;
|
||||||
|
|
||||||
#[embassy::main]
|
#[embassy::main]
|
||||||
async fn main(_spawner: Spawner, p: Peripherals) {
|
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();
|
let mut config = Config::default();
|
||||||
config.prescaler = Prescaler::Div128;
|
config.prescaler = Prescaler::Div128;
|
||||||
@ -31,7 +31,11 @@ async fn main(_spawner: Spawner, p: Peripherals) {
|
|||||||
p.PWM0, p.P0_13, NoPin, NoPin, NoPin, config,
|
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
|
// 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
|
// so its going to have to start running in order load the configuration
|
||||||
|
|
||||||
|
@ -9,7 +9,7 @@ use embassy::executor::Spawner;
|
|||||||
use embassy::time::{Duration, Timer};
|
use embassy::time::{Duration, Timer};
|
||||||
use embassy_nrf::gpio::NoPin;
|
use embassy_nrf::gpio::NoPin;
|
||||||
use embassy_nrf::pwm::{
|
use embassy_nrf::pwm::{
|
||||||
Config, Prescaler, SequenceConfig, SequenceLoad, SequenceMode, SequencePwm,
|
Config, Prescaler, Sequence, SequenceConfig, SequenceLoad, SequenceMode, SequencePwm,
|
||||||
};
|
};
|
||||||
use embassy_nrf::Peripherals;
|
use embassy_nrf::Peripherals;
|
||||||
|
|
||||||
@ -29,12 +29,17 @@ const RES: u16 = 0x8000;
|
|||||||
#[embassy::main]
|
#[embassy::main]
|
||||||
async fn main(_spawner: Spawner, p: Peripherals) {
|
async fn main(_spawner: Spawner, p: Peripherals) {
|
||||||
// Declare the bits of 24 bits
|
// 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, // G
|
||||||
T0H, T0H, T0H, T0H, T0H, T0H, T0H, T0H, // R
|
T0H, T0H, T0H, T0H, T0H, T0H, T0H, T0H, // R
|
||||||
T1H, T1H, T1H, T1H, T1H, T1H, T1H, T1H, // B
|
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();
|
let mut config = Config::default();
|
||||||
config.sequence_load = SequenceLoad::Common;
|
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,
|
p.PWM0, p.P1_05, NoPin, NoPin, NoPin, config,
|
||||||
));
|
));
|
||||||
|
|
||||||
let blue_seq_config = SequenceConfig::default();
|
unwrap!(pwm.start(blue_seq, Some(reset_seq), SequenceMode::Times(2)));
|
||||||
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)
|
|
||||||
));
|
|
||||||
|
|
||||||
Timer::after(Duration::from_millis(20000)).await;
|
Timer::after(Duration::from_millis(20000)).await;
|
||||||
info!("Program stopped");
|
info!("Program stopped");
|
||||||
|
Loading…
Reference in New Issue
Block a user