Merge #591
591: PWM WS2812B example and flexible sequence config r=Dirbaio a=huntc I've permitted the PWM sequences to be mutated on stopping the PWM by associating them with a new `SingleSequencer` structure. This is so that we can perform effects on the LEDs (and other use-cases, I'm sure!). The example has been updated to illustrate the use of this by flashing a WS2812B LED. There's also a `Sequencer` structure for more sophisticated PWM interactions, along with a `pwm_double_sequence` example to illustrate. These changes should make it possible to attain all of the nRF PWM functionality available. Co-authored-by: huntc <huntchr@gmail.com>
This commit is contained in:
46
examples/nrf/src/bin/pwm_double_sequence.rs
Normal file
46
examples/nrf/src/bin/pwm_double_sequence.rs
Normal file
@ -0,0 +1,46 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
|
||||
#[path = "../example_common.rs"]
|
||||
mod example_common;
|
||||
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, Sequencer,
|
||||
StartSequence,
|
||||
};
|
||||
use embassy_nrf::Peripherals;
|
||||
|
||||
#[embassy::main]
|
||||
async fn main(_spawner: Spawner, p: Peripherals) {
|
||||
let seq_words_0: [u16; 5] = [1000, 250, 100, 50, 0];
|
||||
let seq_words_1: [u16; 4] = [50, 100, 250, 1000];
|
||||
|
||||
let mut config = Config::default();
|
||||
config.prescaler = Prescaler::Div128;
|
||||
// 1 period is 1000 * (128/16mhz = 0.000008s = 0.008ms) = 8us
|
||||
// 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 = SequenceConfig::default();
|
||||
seq_config.refresh = 624;
|
||||
// thus our sequence takes 5 * 5000ms or 25 seconds
|
||||
|
||||
let mut pwm = unwrap!(SequencePwm::new(
|
||||
p.PWM0, p.P0_13, NoPin, NoPin, NoPin, config,
|
||||
));
|
||||
|
||||
let sequence_0 = Sequence::new(&seq_words_0, seq_config.clone());
|
||||
let sequence_1 = Sequence::new(&seq_words_1, seq_config);
|
||||
let sequencer = Sequencer::new(&mut pwm, sequence_0, Some(sequence_1));
|
||||
unwrap!(sequencer.start(StartSequence::Zero, SequenceMode::Loop(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
|
||||
// when it goes out of scope
|
||||
Timer::after(Duration::from_millis(40000)).await;
|
||||
info!("pwm stopped early!");
|
||||
}
|
@ -8,34 +8,31 @@ use defmt::*;
|
||||
use embassy::executor::Spawner;
|
||||
use embassy::time::{Duration, Timer};
|
||||
use embassy_nrf::gpio::NoPin;
|
||||
use embassy_nrf::pwm::{Prescaler, SequenceConfig, SequenceMode, SequencePwm};
|
||||
use embassy_nrf::pwm::{
|
||||
Config, Prescaler, SequenceConfig, SequencePwm, SingleSequenceMode, SingleSequencer,
|
||||
};
|
||||
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: [u16; 5] = [1000, 250, 100, 50, 0];
|
||||
|
||||
let mut config = SequenceConfig::default();
|
||||
let mut config = Config::default();
|
||||
config.prescaler = Prescaler::Div128;
|
||||
// 1 period is 1000 * (128/16mhz = 0.000008s = 0.008ms) = 8us
|
||||
// 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)
|
||||
config.refresh = 624;
|
||||
let mut seq_config = SequenceConfig::default();
|
||||
seq_config.refresh = 624;
|
||||
// thus our sequence takes 5 * 5000ms or 25 seconds
|
||||
|
||||
let mut pwm = unwrap!(SequencePwm::new(
|
||||
p.PWM0, p.P0_13, NoPin, NoPin, NoPin, config,
|
||||
));
|
||||
let _ = pwm.start(&seq_values_1, SequenceMode::Infinite);
|
||||
|
||||
info!("pwm started!");
|
||||
|
||||
Timer::after(Duration::from_millis(20000)).await;
|
||||
info!("pwm starting with another sequence!");
|
||||
|
||||
let _ = pwm.start(&seq_values_2, SequenceMode::Infinite);
|
||||
let sequencer = SingleSequencer::new(&mut pwm, &seq_words, seq_config);
|
||||
unwrap!(sequencer.start(SingleSequenceMode::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
|
||||
|
@ -11,26 +11,28 @@ 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::{Prescaler, SequenceConfig, SequenceMode, SequencePwm};
|
||||
use embassy_nrf::pwm::{
|
||||
Config, Prescaler, SequenceConfig, SequencePwm, SingleSequenceMode, SingleSequencer,
|
||||
};
|
||||
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 = SequenceConfig::default();
|
||||
let mut config = Config::default();
|
||||
config.prescaler = Prescaler::Div128;
|
||||
// 1 period is 1000 * (128/16mhz = 0.000008s = 0.008ms) = 8us
|
||||
// but say we want to hold the value for 250ms 250ms/8 = 31.25 periods
|
||||
// so round to 31 - 1 (we get the one period for free remember)
|
||||
// thus our sequence takes 5 * 250ms or 1.25 seconds
|
||||
config.refresh = 30;
|
||||
let mut seq_config = SequenceConfig::default();
|
||||
seq_config.refresh = 30;
|
||||
|
||||
let mut pwm = unwrap!(SequencePwm::new(
|
||||
p.PWM0, p.P0_13, NoPin, NoPin, NoPin, config,
|
||||
));
|
||||
|
||||
let _ = pwm.start(&seq_values, SequenceMode::Times(1));
|
||||
// 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
|
||||
|
||||
@ -51,6 +53,9 @@ async fn main(_spawner: Spawner, p: Peripherals) {
|
||||
let start = unsafe { pwm.task_start_seq0() };
|
||||
let stop = unsafe { pwm.task_stop() };
|
||||
|
||||
let sequencer = SingleSequencer::new(&mut pwm, &seq_words, seq_config);
|
||||
unwrap!(sequencer.start(SingleSequenceMode::Infinite));
|
||||
|
||||
let mut ppi = Ppi::new_one_to_one(p.PPI_CH1, button1.event_in(), start);
|
||||
ppi.enable();
|
||||
|
||||
|
80
examples/nrf/src/bin/pwm_sequence_ws2812b.rs
Normal file
80
examples/nrf/src/bin/pwm_sequence_ws2812b.rs
Normal file
@ -0,0 +1,80 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
|
||||
#[path = "../example_common.rs"]
|
||||
mod example_common;
|
||||
use defmt::*;
|
||||
use embassy::executor::Spawner;
|
||||
use embassy::time::{Duration, Timer};
|
||||
use embassy_nrf::gpio::NoPin;
|
||||
use embassy_nrf::pwm::{
|
||||
Config, Prescaler, SequenceConfig, SequenceLoad, SequencePwm, SingleSequenceMode,
|
||||
SingleSequencer,
|
||||
};
|
||||
use embassy_nrf::Peripherals;
|
||||
|
||||
// WS2812B LED light demonstration. Drives just one light.
|
||||
// The following reference on WS2812B may be of use:
|
||||
// https://cdn-shop.adafruit.com/datasheets/WS2812B.pdf.
|
||||
// This demo lights up a single LED in blue. It then proceeds
|
||||
// to pulsate the LED rapidly.
|
||||
|
||||
// In the following declarations, setting the high bit tells the PWM
|
||||
// to reverse polarity, which is what the WS2812B expects.
|
||||
|
||||
const T1H: u16 = 0x8000 | 13; // Duty = 13/20 ticks (0.8us/1.25us) for a 1
|
||||
const T0H: u16 = 0x8000 | 7; // Duty 7/20 ticks (0.4us/1.25us) for a 0
|
||||
const RES: u16 = 0x8000;
|
||||
|
||||
// Provides data to a WS2812b (Neopixel) LED and makes it go blue. The data
|
||||
// line is assumed to be P1_05.
|
||||
#[embassy::main]
|
||||
async fn main(_spawner: Spawner, p: Peripherals) {
|
||||
let mut config = Config::default();
|
||||
config.sequence_load = SequenceLoad::Common;
|
||||
config.prescaler = Prescaler::Div1;
|
||||
config.max_duty = 20; // 1.25us (1s / 16Mhz * 20)
|
||||
let mut pwm = unwrap!(SequencePwm::new(
|
||||
p.PWM0, p.P1_05, NoPin, NoPin, NoPin, config,
|
||||
));
|
||||
|
||||
// Declare the bits of 24 bits in a buffer we'll be
|
||||
// mutating later.
|
||||
let mut 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
|
||||
RES,
|
||||
];
|
||||
let mut seq_config = SequenceConfig::default();
|
||||
seq_config.end_delay = 799; // 50us (20 ticks * 40) - 1 tick because we've already got one RES;
|
||||
|
||||
let mut color_bit = 16;
|
||||
let mut bit_value = T0H;
|
||||
|
||||
loop {
|
||||
let sequences = SingleSequencer::new(&mut pwm, &seq_words, seq_config.clone());
|
||||
unwrap!(sequences.start(SingleSequenceMode::Times(1)));
|
||||
|
||||
Timer::after(Duration::from_millis(50)).await;
|
||||
|
||||
if bit_value == T0H {
|
||||
if color_bit == 20 {
|
||||
bit_value = T1H;
|
||||
} else {
|
||||
color_bit += 1;
|
||||
}
|
||||
} else {
|
||||
if color_bit == 16 {
|
||||
bit_value = T0H;
|
||||
} else {
|
||||
color_bit -= 1;
|
||||
}
|
||||
}
|
||||
|
||||
drop(sequences);
|
||||
|
||||
seq_words[color_bit] = bit_value;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user