embassy/examples/nrf/src/bin/pwm_sequence.rs
Jacob Rosenthal 67baec472d nrf: dump the pwm_sequence example for clarity
It is basically impossible to directly convert that example to a sequence for various reasons. You cant have multiple channels on same buffer with one sequence instance for starters, also at that clock rate and max_duty 1 period is far longer than the 3ms it was using, which would require using a new max_duty and thus require regenerating the sine table which makes it not representitive of the original example anymore
2021-11-11 23:31:10 -07:00

43 lines
1.2 KiB
Rust

#![no_std]
#![no_main]
#![feature(type_alias_impl_trait)]
#[path = "../example_common.rs"]
mod example_common;
use defmt::*;
use embassy::executor::Spawner;
use embassy::time::{Duration, Timer};
use embassy_nrf::gpio::NoPin;
use embassy_nrf::pwm::{Prescaler, SequenceConfig, SequenceMode, SequencePwm};
use embassy_nrf::Peripherals;
#[embassy::main]
async fn main(_spawner: Spawner, p: Peripherals) {
let seq_values: [u16; 5] = [1000, 250, 100, 50, 0];
let mut config = SequenceConfig::default();
config.prescaler = Prescaler::Div128;
// 1 period is 1000 * (128/16mhz = 0.000008s = 0.008ms) = 8ms
// 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)
config.refresh = 624;
let pwm = unwrap!(SequencePwm::new(
p.PWM0,
p.P0_13,
NoPin,
NoPin,
NoPin,
config,
&seq_values
));
let _ = pwm.start(SequenceMode::Infinite);
info!("pwm started!");
loop {
Timer::after(Duration::from_millis(5000)).await;
}
}