Expose PWM

This commit is contained in:
huntc 2022-02-04 16:26:23 +11:00
parent 25be00878c
commit fe5501293f
3 changed files with 19 additions and 13 deletions

View File

@ -321,7 +321,7 @@ impl<'s> Sequence<'s> {
/// is used. /// is used.
#[non_exhaustive] #[non_exhaustive]
pub struct Sequences<'d, 's, T: Instance> { pub struct Sequences<'d, 's, T: Instance> {
_pwm: &'s mut SequencePwm<'d, T>, pub pwm: &'s mut SequencePwm<'d, T>,
sequence0: Sequence<'s>, sequence0: Sequence<'s>,
sequence1: Option<Sequence<'s>>, sequence1: Option<Sequence<'s>>,
} }
@ -333,7 +333,7 @@ impl<'d, 's, T: Instance> Sequences<'d, 's, T> {
sequence1: Option<Sequence<'s>>, sequence1: Option<Sequence<'s>>,
) -> Self { ) -> Self {
Sequences { Sequences {
_pwm: pwm, pwm,
sequence0, sequence0,
sequence1, sequence1,
} }

View File

@ -11,12 +11,14 @@ 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, Sequence, SequenceConfig, SequenceMode, SequencePwm}; use embassy_nrf::pwm::{
Config, Prescaler, Sequence, SequenceConfig, SequenceMode, SequencePwm, Sequences,
};
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 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(); let mut config = Config::default();
config.prescaler = Prescaler::Div128; config.prescaler = Prescaler::Div128;
@ -31,11 +33,10 @@ 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 sequence0 = Sequence::new(&seq_words, seq_config);
Sequence::new(&mut seq_words, seq_config), let sequences = Sequences::new(&mut pwm, sequence0, None);
None, unwrap!(sequences.start(SequenceMode::Infinite));
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
@ -53,8 +54,8 @@ async fn main(_spawner: Spawner, p: Peripherals) {
// messing with the pwm tasks is ill advised // messing with the pwm tasks is ill advised
// Times::Ininite and Times even are seq0, Times odd is seq1 // Times::Ininite and Times even are seq0, Times odd is seq1
let start = unsafe { pwm.task_start_seq0() }; let start = unsafe { sequences.pwm.task_start_seq0() };
let stop = unsafe { pwm.task_stop() }; let stop = unsafe { sequences.pwm.task_stop() };
let mut ppi = Ppi::new_one_to_one(p.PPI_CH1, button1.event_in(), start); let mut ppi = Ppi::new_one_to_one(p.PPI_CH1, button1.event_in(), start);
ppi.enable(); ppi.enable();

View File

@ -7,11 +7,12 @@ mod example_common;
use defmt::*; use defmt::*;
use embassy::executor::Spawner; use embassy::executor::Spawner;
use embassy::time::{Duration, Timer}; use embassy::time::{Duration, Timer};
use embassy::util::Forever;
use embassy_nrf::gpio::NoPin; use embassy_nrf::gpio::NoPin;
use embassy_nrf::pwm::{ use embassy_nrf::pwm::{
Config, Prescaler, Sequence, SequenceConfig, SequenceLoad, SequenceMode, SequencePwm, Sequences, Config, Prescaler, Sequence, SequenceConfig, SequenceLoad, SequenceMode, SequencePwm, Sequences,
}; };
use embassy_nrf::Peripherals; use embassy_nrf::{peripherals, Peripherals};
// WS2812B LED light demonstration. Drives just one light. // WS2812B LED light demonstration. Drives just one light.
// The following reference on WS2812B may be of use: // The following reference on WS2812B may be of use:
@ -26,6 +27,8 @@ 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 T0H: u16 = 0x8000 | 7; // Duty 7/20 ticks (0.4us/1.25us) for a 0
const RES: u16 = 0x8000; const RES: u16 = 0x8000;
static PWM: Forever<SequencePwm<peripherals::PWM0>> = Forever::new();
// Provides data to a WS2812b (Neopixel) LED and makes it go blue. The data // Provides data to a WS2812b (Neopixel) LED and makes it go blue. The data
// line is assumed to be P1_05. // line is assumed to be P1_05.
#[embassy::main] #[embassy::main]
@ -34,10 +37,12 @@ async fn main(_spawner: Spawner, p: Peripherals) {
config.sequence_load = SequenceLoad::Common; config.sequence_load = SequenceLoad::Common;
config.prescaler = Prescaler::Div1; config.prescaler = Prescaler::Div1;
config.max_duty = 20; // 1.25us (1s / 16Mhz * 20) config.max_duty = 20; // 1.25us (1s / 16Mhz * 20)
let mut pwm = unwrap!(SequencePwm::new( let pwm = unwrap!(SequencePwm::new(
p.PWM0, p.P1_05, NoPin, NoPin, NoPin, config, p.PWM0, p.P1_05, NoPin, NoPin, NoPin, config,
)); ));
let mut pwm = PWM.put(pwm);
// Declare the bits of 24 bits in a buffer we'll be // Declare the bits of 24 bits in a buffer we'll be
// mutating later. // mutating later.
let mut seq_words = [ let mut seq_words = [