2022-02-04 08:11:15 +00:00
|
|
|
#![no_std]
|
|
|
|
#![no_main]
|
|
|
|
|
|
|
|
use defmt::*;
|
2022-08-17 21:40:16 +00:00
|
|
|
use embassy_executor::Spawner;
|
2022-02-04 08:11:15 +00:00
|
|
|
use embassy_nrf::pwm::{
|
2022-06-12 20:15:44 +00:00
|
|
|
Config, Prescaler, Sequence, SequenceConfig, SequenceMode, SequencePwm, Sequencer, StartSequence,
|
2022-02-04 08:11:15 +00:00
|
|
|
};
|
2023-10-14 23:57:25 +00:00
|
|
|
use embassy_time::Timer;
|
2022-06-12 20:15:44 +00:00
|
|
|
use {defmt_rtt as _, panic_probe as _};
|
2022-04-02 02:35:06 +00:00
|
|
|
|
2022-07-29 19:58:35 +00:00
|
|
|
#[embassy_executor::main]
|
2022-08-17 16:49:55 +00:00
|
|
|
async fn main(_spawner: Spawner) {
|
|
|
|
let p = embassy_nrf::init(Default::default());
|
2022-02-04 08:11:15 +00:00
|
|
|
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
|
|
|
|
|
2022-02-12 00:04:01 +00:00
|
|
|
let mut pwm = unwrap!(SequencePwm::new_1ch(p.PWM0, p.P0_13, config));
|
2022-02-04 08:11:15 +00:00
|
|
|
|
|
|
|
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
|
2023-10-14 23:57:25 +00:00
|
|
|
Timer::after_millis(40000).await;
|
2022-02-04 08:11:15 +00:00
|
|
|
info!("pwm stopped early!");
|
|
|
|
}
|