From ef95441442e0ccab0ac6d62264dedb7254d4cb84 Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Fri, 29 Oct 2021 17:09:34 -0700 Subject: [PATCH] a runtime generated sin table example --- examples/nrf/Cargo.toml | 1 + examples/nrf/src/bin/pwm_simple_sin.rs | 41 ++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 examples/nrf/src/bin/pwm_simple_sin.rs diff --git a/examples/nrf/Cargo.toml b/examples/nrf/Cargo.toml index b89aa513..e0025b73 100644 --- a/examples/nrf/Cargo.toml +++ b/examples/nrf/Cargo.toml @@ -31,3 +31,4 @@ panic-probe = { version = "0.2.0", features = ["print-defmt"] } futures = { version = "0.3.17", default-features = false, features = ["async-await"] } rand = { version = "0.8.4", default-features = false } embedded-storage = "0.2.0" +micromath = "2.0.0" \ No newline at end of file diff --git a/examples/nrf/src/bin/pwm_simple_sin.rs b/examples/nrf/src/bin/pwm_simple_sin.rs new file mode 100644 index 00000000..866202a4 --- /dev/null +++ b/examples/nrf/src/bin/pwm_simple_sin.rs @@ -0,0 +1,41 @@ +#![no_std] +#![no_main] +#![feature(type_alias_impl_trait)] +#![feature(array_from_fn)] + +#[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::{CounterMode, LoopingConfig, Prescaler, Pwm, SequenceLoad}; +use embassy_nrf::Peripherals; +use micromath::F32Ext; + +const W1: f32 = core::f32::consts::PI / 128.0; + +#[embassy::main] +async fn main(_spawner: Spawner, p: Peripherals) { + // probably not best use of resources to create the table at runtime, but makes testing fast + let seq_values: [u16; 220] = core::array::from_fn(|n| ((W1 * n as f32).sin() * 10000.0) as u16); + + let config = LoopingConfig { + counter_mode: CounterMode::UpAndDown, + top: 12000, + prescaler: Prescaler::Div16, + sequence: &seq_values, + sequence_load: SequenceLoad::Common, + repeats: 0, + enddelay: 0, + }; + + let _pwm = unwrap!(Pwm::simple_playback( + p.PWM0, p.P0_13, NoPin, NoPin, NoPin, config + )); + info!("pwm started!"); + + loop { + Timer::after(Duration::from_millis(1000)).await; + } +}