diff options
| author | huntc <[email protected]> | 2022-02-04 16:26:23 +1100 |
|---|---|---|
| committer | huntc <[email protected]> | 2022-02-04 16:26:23 +1100 |
| commit | fe5501293f39307fbfa419d6f882f4a2cd10c115 (patch) | |
| tree | 301798af1dbcb2fe7d9296c1ba1a79ca1890831a /examples | |
| parent | 25be00878c44550a7ecbb6f6501490dba770c20e (diff) | |
Expose PWM
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/nrf/src/bin/pwm_sequence_ppi.rs | 19 | ||||
| -rw-r--r-- | examples/nrf/src/bin/pwm_sequence_ws2812b.rs | 9 |
2 files changed, 17 insertions, 11 deletions
diff --git a/examples/nrf/src/bin/pwm_sequence_ppi.rs b/examples/nrf/src/bin/pwm_sequence_ppi.rs index f5d734bb1..593e7590d 100644 --- a/examples/nrf/src/bin/pwm_sequence_ppi.rs +++ b/examples/nrf/src/bin/pwm_sequence_ppi.rs | |||
| @@ -11,12 +11,14 @@ use embassy::executor::Spawner; | |||
| 11 | use embassy_nrf::gpio::{Input, NoPin, Pull}; | 11 | use embassy_nrf::gpio::{Input, NoPin, Pull}; |
| 12 | use embassy_nrf::gpiote::{InputChannel, InputChannelPolarity}; | 12 | use embassy_nrf::gpiote::{InputChannel, InputChannelPolarity}; |
| 13 | use embassy_nrf::ppi::Ppi; | 13 | use embassy_nrf::ppi::Ppi; |
| 14 | use embassy_nrf::pwm::{Config, Prescaler, Sequence, SequenceConfig, SequenceMode, SequencePwm}; | 14 | use embassy_nrf::pwm::{ |
| 15 | Config, Prescaler, Sequence, SequenceConfig, SequenceMode, SequencePwm, Sequences, | ||
| 16 | }; | ||
| 15 | use embassy_nrf::Peripherals; | 17 | use embassy_nrf::Peripherals; |
| 16 | 18 | ||
| 17 | #[embassy::main] | 19 | #[embassy::main] |
| 18 | async fn main(_spawner: Spawner, p: Peripherals) { | 20 | async fn main(_spawner: Spawner, p: Peripherals) { |
| 19 | let mut seq_words: [u16; 5] = [1000, 250, 100, 50, 0]; | 21 | let seq_words: [u16; 5] = [1000, 250, 100, 50, 0]; |
| 20 | 22 | ||
| 21 | let mut config = Config::default(); | 23 | let mut config = Config::default(); |
| 22 | config.prescaler = Prescaler::Div128; | 24 | config.prescaler = Prescaler::Div128; |
| @@ -31,11 +33,10 @@ async fn main(_spawner: Spawner, p: Peripherals) { | |||
| 31 | p.PWM0, p.P0_13, NoPin, NoPin, NoPin, config, | 33 | p.PWM0, p.P0_13, NoPin, NoPin, NoPin, config, |
| 32 | )); | 34 | )); |
| 33 | 35 | ||
| 34 | let _ = pwm.start( | 36 | let sequence0 = Sequence::new(&seq_words, seq_config); |
| 35 | Sequence::new(&mut seq_words, seq_config), | 37 | let sequences = Sequences::new(&mut pwm, sequence0, None); |
| 36 | None, | 38 | unwrap!(sequences.start(SequenceMode::Infinite)); |
| 37 | SequenceMode::Infinite, | 39 | |
| 38 | ); | ||
| 39 | // pwm.stop() deconfigures pins, and then the task_start_seq0 task cant work | 40 | // pwm.stop() deconfigures pins, and then the task_start_seq0 task cant work |
| 40 | // so its going to have to start running in order load the configuration | 41 | // so its going to have to start running in order load the configuration |
| 41 | 42 | ||
| @@ -53,8 +54,8 @@ async fn main(_spawner: Spawner, p: Peripherals) { | |||
| 53 | 54 | ||
| 54 | // messing with the pwm tasks is ill advised | 55 | // messing with the pwm tasks is ill advised |
| 55 | // Times::Ininite and Times even are seq0, Times odd is seq1 | 56 | // Times::Ininite and Times even are seq0, Times odd is seq1 |
| 56 | let start = unsafe { pwm.task_start_seq0() }; | 57 | let start = unsafe { sequences.pwm.task_start_seq0() }; |
| 57 | let stop = unsafe { pwm.task_stop() }; | 58 | let stop = unsafe { sequences.pwm.task_stop() }; |
| 58 | 59 | ||
| 59 | let mut ppi = Ppi::new_one_to_one(p.PPI_CH1, button1.event_in(), start); | 60 | let mut ppi = Ppi::new_one_to_one(p.PPI_CH1, button1.event_in(), start); |
| 60 | ppi.enable(); | 61 | ppi.enable(); |
diff --git a/examples/nrf/src/bin/pwm_sequence_ws2812b.rs b/examples/nrf/src/bin/pwm_sequence_ws2812b.rs index 310842d8d..7706f91da 100644 --- a/examples/nrf/src/bin/pwm_sequence_ws2812b.rs +++ b/examples/nrf/src/bin/pwm_sequence_ws2812b.rs | |||
| @@ -7,11 +7,12 @@ mod example_common; | |||
| 7 | use defmt::*; | 7 | use defmt::*; |
| 8 | use embassy::executor::Spawner; | 8 | use embassy::executor::Spawner; |
| 9 | use embassy::time::{Duration, Timer}; | 9 | use embassy::time::{Duration, Timer}; |
| 10 | use embassy::util::Forever; | ||
| 10 | use embassy_nrf::gpio::NoPin; | 11 | use embassy_nrf::gpio::NoPin; |
| 11 | use embassy_nrf::pwm::{ | 12 | use embassy_nrf::pwm::{ |
| 12 | Config, Prescaler, Sequence, SequenceConfig, SequenceLoad, SequenceMode, SequencePwm, Sequences, | 13 | Config, Prescaler, Sequence, SequenceConfig, SequenceLoad, SequenceMode, SequencePwm, Sequences, |
| 13 | }; | 14 | }; |
| 14 | use embassy_nrf::Peripherals; | 15 | use embassy_nrf::{peripherals, Peripherals}; |
| 15 | 16 | ||
| 16 | // WS2812B LED light demonstration. Drives just one light. | 17 | // WS2812B LED light demonstration. Drives just one light. |
| 17 | // The following reference on WS2812B may be of use: | 18 | // 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 | |||
| 26 | const T0H: u16 = 0x8000 | 7; // Duty 7/20 ticks (0.4us/1.25us) for a 0 | 27 | const T0H: u16 = 0x8000 | 7; // Duty 7/20 ticks (0.4us/1.25us) for a 0 |
| 27 | const RES: u16 = 0x8000; | 28 | const RES: u16 = 0x8000; |
| 28 | 29 | ||
| 30 | static PWM: Forever<SequencePwm<peripherals::PWM0>> = Forever::new(); | ||
| 31 | |||
| 29 | // Provides data to a WS2812b (Neopixel) LED and makes it go blue. The data | 32 | // Provides data to a WS2812b (Neopixel) LED and makes it go blue. The data |
| 30 | // line is assumed to be P1_05. | 33 | // line is assumed to be P1_05. |
| 31 | #[embassy::main] | 34 | #[embassy::main] |
| @@ -34,10 +37,12 @@ async fn main(_spawner: Spawner, p: Peripherals) { | |||
| 34 | config.sequence_load = SequenceLoad::Common; | 37 | config.sequence_load = SequenceLoad::Common; |
| 35 | config.prescaler = Prescaler::Div1; | 38 | config.prescaler = Prescaler::Div1; |
| 36 | config.max_duty = 20; // 1.25us (1s / 16Mhz * 20) | 39 | config.max_duty = 20; // 1.25us (1s / 16Mhz * 20) |
| 37 | let mut pwm = unwrap!(SequencePwm::new( | 40 | let pwm = unwrap!(SequencePwm::new( |
| 38 | p.PWM0, p.P1_05, NoPin, NoPin, NoPin, config, | 41 | p.PWM0, p.P1_05, NoPin, NoPin, NoPin, config, |
| 39 | )); | 42 | )); |
| 40 | 43 | ||
| 44 | let mut pwm = PWM.put(pwm); | ||
| 45 | |||
| 41 | // Declare the bits of 24 bits in a buffer we'll be | 46 | // Declare the bits of 24 bits in a buffer we'll be |
| 42 | // mutating later. | 47 | // mutating later. |
| 43 | let mut seq_words = [ | 48 | let mut seq_words = [ |
