2021-10-26 09:37:52 +02:00
|
|
|
#![no_std]
|
|
|
|
#![no_main]
|
|
|
|
#![feature(type_alias_impl_trait)]
|
|
|
|
|
|
|
|
use defmt::*;
|
2022-08-17 23:40:16 +02:00
|
|
|
use embassy_executor::Spawner;
|
2022-06-12 22:15:44 +02:00
|
|
|
use embassy_nrf::pwm::{Config, Prescaler, SequenceConfig, SequencePwm, SingleSequenceMode, SingleSequencer};
|
2022-08-17 23:40:16 +02:00
|
|
|
use embassy_time::{Duration, Timer};
|
2022-06-12 22:15:44 +02:00
|
|
|
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 09:11:15 +01:00
|
|
|
let seq_words: [u16; 5] = [1000, 250, 100, 50, 0];
|
2022-01-30 06:21:23 +01:00
|
|
|
|
2022-01-25 08:06:42 +01:00
|
|
|
let mut config = Config::default();
|
2021-11-12 07:27:03 +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-12 07:27:03 +01:00
|
|
|
// 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)
|
2022-01-28 03:38:20 +01:00
|
|
|
let mut seq_config = SequenceConfig::default();
|
2022-01-25 08:06:42 +01:00
|
|
|
seq_config.refresh = 624;
|
2021-11-12 07:47:35 +01:00
|
|
|
// thus our sequence takes 5 * 5000ms or 25 seconds
|
2021-10-26 09:37:52 +02:00
|
|
|
|
2022-02-12 01:04:01 +01:00
|
|
|
let mut pwm = unwrap!(SequencePwm::new_1ch(p.PWM0, p.P0_13, config,));
|
2022-02-04 06:34:25 +01:00
|
|
|
|
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::Times(1)));
|
2022-01-23 06:21:34 +01:00
|
|
|
|
2021-11-12 07:47:35 +01:00
|
|
|
// 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(20000)).await;
|
|
|
|
info!("pwm stopped early!");
|
2021-10-26 09:37:52 +02:00
|
|
|
}
|