2021-11-13 23:03:10 +01:00
|
|
|
#![no_std]
|
|
|
|
#![no_main]
|
|
|
|
#![feature(type_alias_impl_trait)]
|
|
|
|
|
|
|
|
use core::future::pending;
|
2022-06-12 22:15:44 +02:00
|
|
|
|
2021-11-13 23:03:10 +01:00
|
|
|
use defmt::*;
|
2022-08-17 23:40:16 +02:00
|
|
|
use embassy_executor::Spawner;
|
2022-02-12 01:04:01 +01:00
|
|
|
use embassy_nrf::gpio::{Input, Pull};
|
2021-11-13 23:03:10 +01:00
|
|
|
use embassy_nrf::gpiote::{InputChannel, InputChannelPolarity};
|
|
|
|
use embassy_nrf::ppi::Ppi;
|
2022-06-12 22:15:44 +02:00
|
|
|
use embassy_nrf::pwm::{Config, Prescaler, SequenceConfig, SequencePwm, SingleSequenceMode, SingleSequencer};
|
|
|
|
use {defmt_rtt as _, panic_probe as _};
|
2022-04-02 04:35:06 +02:00
|
|
|
|
2022-07-29 21:58:35 +02:00
|
|
|
#[embassy_executor::main]
|
2022-08-17 18:49:55 +02:00
|
|
|
async fn main(_spawner: Spawner) {
|
|
|
|
let p = embassy_nrf::init(Default::default());
|
2022-02-04 06:26:23 +01:00
|
|
|
let seq_words: [u16; 5] = [1000, 250, 100, 50, 0];
|
2021-11-13 23:03:10 +01:00
|
|
|
|
2022-01-25 08:06:42 +01:00
|
|
|
let mut config = Config::default();
|
2021-11-13 23:03:10 +01:00
|
|
|
config.prescaler = Prescaler::Div128;
|
2022-01-25 06:51:24 +01:00
|
|
|
// 1 period is 1000 * (128/16mhz = 0.000008s = 0.008ms) = 8us
|
2021-11-13 23:03:10 +01:00
|
|
|
// 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
|
2022-01-25 08:06:42 +01:00
|
|
|
let mut seq_config = SequenceConfig::default();
|
|
|
|
seq_config.refresh = 30;
|
2021-11-13 23:03:10 +01:00
|
|
|
|
2022-02-12 01:04:01 +01:00
|
|
|
let mut pwm = unwrap!(SequencePwm::new_1ch(p.PWM0, p.P0_13, config));
|
2021-11-13 23:03:10 +01:00
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
|
let button1 = InputChannel::new(
|
|
|
|
p.GPIOTE_CH0,
|
|
|
|
Input::new(p.P0_11, Pull::Up),
|
|
|
|
InputChannelPolarity::HiToLo,
|
|
|
|
);
|
|
|
|
|
|
|
|
let button2 = InputChannel::new(
|
|
|
|
p.GPIOTE_CH1,
|
|
|
|
Input::new(p.P0_12, Pull::Up),
|
|
|
|
InputChannelPolarity::HiToLo,
|
|
|
|
);
|
|
|
|
|
|
|
|
// messing with the pwm tasks is ill advised
|
|
|
|
// Times::Ininite and Times even are seq0, Times odd is seq1
|
2022-02-04 09:11:15 +01:00
|
|
|
let start = unsafe { pwm.task_start_seq0() };
|
|
|
|
let stop = unsafe { pwm.task_stop() };
|
|
|
|
|
2022-02-04 22:05:23 +01:00
|
|
|
let sequencer = SingleSequencer::new(&mut pwm, &seq_words, seq_config);
|
2022-02-04 09:11:15 +01:00
|
|
|
unwrap!(sequencer.start(SingleSequenceMode::Infinite));
|
2021-11-13 23:03:10 +01:00
|
|
|
|
|
|
|
let mut ppi = Ppi::new_one_to_one(p.PPI_CH1, button1.event_in(), start);
|
|
|
|
ppi.enable();
|
|
|
|
|
|
|
|
let mut ppi2 = Ppi::new_one_to_one(p.PPI_CH0, button2.event_in(), stop);
|
|
|
|
ppi2.enable();
|
|
|
|
|
|
|
|
info!("PPI setup!");
|
|
|
|
info!("Press button 1 to start LED 1");
|
|
|
|
info!("Press button 2 to stop LED 1");
|
|
|
|
info!("Note! task_stop stops the sequence, but not the pin output");
|
|
|
|
|
|
|
|
// Block forever so the above drivers don't get dropped
|
|
|
|
pending::<()>().await;
|
|
|
|
}
|