From eb0bf1fd7a33330425a12420e5d948ca6e88d74f Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Tue, 26 Oct 2021 00:37:52 -0700 Subject: simple_playback api from nrf sdk --- examples/nrf/src/bin/pwm_sequence.rs | 41 ++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 examples/nrf/src/bin/pwm_sequence.rs (limited to 'examples') diff --git a/examples/nrf/src/bin/pwm_sequence.rs b/examples/nrf/src/bin/pwm_sequence.rs new file mode 100644 index 000000000..93ee9f5b2 --- /dev/null +++ b/examples/nrf/src/bin/pwm_sequence.rs @@ -0,0 +1,41 @@ +#![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::{CounterMode, LoopingConfig, Prescaler, Pwm, SequenceLoad}; +use embassy_nrf::Peripherals; + +#[embassy::main] +async fn main(_spawner: Spawner, p: Peripherals) { + let seq_values: [u16; 2] = [0, 0x8000]; + + let config = LoopingConfig { + counter_mode: CounterMode::Up, + top: 31250, + prescaler: Prescaler::Div128, + sequence: &seq_values, + sequence_load: SequenceLoad::Common, + repeats: 1, + enddelay: 0, + }; + + let pwm = unwrap!(Pwm::simple_playback( + p.PWM0, p.P0_13, NoPin, NoPin, NoPin, config, 1 + )); + info!("pwm started!"); + + Timer::after(Duration::from_millis(10000)).await; + + pwm.stop(); + info!("pwm stopped!"); + + loop { + Timer::after(Duration::from_millis(1000)).await; + } +} -- cgit From 1d1d8a848e165e2754720fa442571782616cb822 Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Fri, 29 Oct 2021 16:39:41 -0700 Subject: simplify api, more interesting example --- examples/nrf/src/bin/pwm_sequence.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'examples') diff --git a/examples/nrf/src/bin/pwm_sequence.rs b/examples/nrf/src/bin/pwm_sequence.rs index 93ee9f5b2..bc356c28b 100644 --- a/examples/nrf/src/bin/pwm_sequence.rs +++ b/examples/nrf/src/bin/pwm_sequence.rs @@ -7,26 +7,26 @@ 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; #[embassy::main] async fn main(_spawner: Spawner, p: Peripherals) { - let seq_values: [u16; 2] = [0, 0x8000]; - + let seq_values: [u16; 16] = [ + 0x8000, 0, 0, 0, 0, 0x8000, 0, 0, 0, 0, 0x8000, 0, 0, 0, 0, 0x8000, + ]; let config = LoopingConfig { counter_mode: CounterMode::Up, - top: 31250, + top: 15625, prescaler: Prescaler::Div128, sequence: &seq_values, - sequence_load: SequenceLoad::Common, - repeats: 1, + sequence_load: SequenceLoad::Individual, + repeats: 0, enddelay: 0, }; let pwm = unwrap!(Pwm::simple_playback( - p.PWM0, p.P0_13, NoPin, NoPin, NoPin, config, 1 + p.PWM0, p.P0_13, p.P0_15, p.P0_16, p.P0_14, config )); info!("pwm started!"); -- cgit From ef95441442e0ccab0ac6d62264dedb7254d4cb84 Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Fri, 29 Oct 2021 17:09:34 -0700 Subject: 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 (limited to 'examples') diff --git a/examples/nrf/Cargo.toml b/examples/nrf/Cargo.toml index b89aa513f..e0025b737 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 000000000..866202a4c --- /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; + } +} -- cgit From 763e250dfea4425912674ed68d77c3291b0505e0 Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Sat, 30 Oct 2021 16:16:10 -0700 Subject: add ability to configure loop count from 1 to infinite --- examples/nrf/src/bin/pwm_sequence.rs | 13 +++++-------- examples/nrf/src/bin/pwm_simple_sin.rs | 12 +++++++++--- 2 files changed, 14 insertions(+), 11 deletions(-) (limited to 'examples') diff --git a/examples/nrf/src/bin/pwm_sequence.rs b/examples/nrf/src/bin/pwm_sequence.rs index bc356c28b..8b7aeddcd 100644 --- a/examples/nrf/src/bin/pwm_sequence.rs +++ b/examples/nrf/src/bin/pwm_sequence.rs @@ -7,7 +7,7 @@ mod example_common; use defmt::*; use embassy::executor::Spawner; use embassy::time::{Duration, Timer}; -use embassy_nrf::pwm::{CounterMode, LoopingConfig, Prescaler, Pwm, SequenceLoad}; +use embassy_nrf::pwm::{CounterMode, LoopMode, LoopingConfig, Prescaler, Pwm, SequenceLoad}; use embassy_nrf::Peripherals; #[embassy::main] @@ -15,26 +15,23 @@ async fn main(_spawner: Spawner, p: Peripherals) { let seq_values: [u16; 16] = [ 0x8000, 0, 0, 0, 0, 0x8000, 0, 0, 0, 0, 0x8000, 0, 0, 0, 0, 0x8000, ]; + let config = LoopingConfig { counter_mode: CounterMode::Up, top: 15625, prescaler: Prescaler::Div128, sequence: &seq_values, sequence_load: SequenceLoad::Individual, - repeats: 0, + refresh: 0, enddelay: 0, + additional_loops: LoopMode::Additional(5), }; - let pwm = unwrap!(Pwm::simple_playback( + let _pwm = unwrap!(Pwm::simple_playback( p.PWM0, p.P0_13, p.P0_15, p.P0_16, p.P0_14, config )); info!("pwm started!"); - Timer::after(Duration::from_millis(10000)).await; - - pwm.stop(); - info!("pwm stopped!"); - loop { Timer::after(Duration::from_millis(1000)).await; } diff --git a/examples/nrf/src/bin/pwm_simple_sin.rs b/examples/nrf/src/bin/pwm_simple_sin.rs index 866202a4c..3fbfc960f 100644 --- a/examples/nrf/src/bin/pwm_simple_sin.rs +++ b/examples/nrf/src/bin/pwm_simple_sin.rs @@ -9,7 +9,7 @@ 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::pwm::{CounterMode, LoopMode, LoopingConfig, Prescaler, Pwm, SequenceLoad}; use embassy_nrf::Peripherals; use micromath::F32Ext; @@ -26,15 +26,21 @@ async fn main(_spawner: Spawner, p: Peripherals) { prescaler: Prescaler::Div16, sequence: &seq_values, sequence_load: SequenceLoad::Common, - repeats: 0, + refresh: 0, enddelay: 0, + additional_loops: LoopMode::Infinite, }; - let _pwm = unwrap!(Pwm::simple_playback( + let pwm = unwrap!(Pwm::simple_playback( p.PWM0, p.P0_13, NoPin, NoPin, NoPin, config )); info!("pwm started!"); + Timer::after(Duration::from_millis(20000)).await; + + pwm.stop(); + info!("pwm stopped!"); + loop { Timer::after(Duration::from_millis(1000)).await; } -- cgit From 78e382c9aa421b8319dcf1c9ea44ae046c1a019d Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Sun, 31 Oct 2021 23:00:33 -0700 Subject: stop->sequence_stop --- examples/nrf/src/bin/pwm_simple_sin.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/nrf/src/bin/pwm_simple_sin.rs b/examples/nrf/src/bin/pwm_simple_sin.rs index 3fbfc960f..c7d85381b 100644 --- a/examples/nrf/src/bin/pwm_simple_sin.rs +++ b/examples/nrf/src/bin/pwm_simple_sin.rs @@ -38,7 +38,7 @@ async fn main(_spawner: Spawner, p: Peripherals) { Timer::after(Duration::from_millis(20000)).await; - pwm.stop(); + pwm.sequence_stop(); info!("pwm stopped!"); loop { -- cgit From 14dc524b84b28cfa6f8a9649c38173d6a1977a9e Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Mon, 1 Nov 2021 01:20:01 -0700 Subject: documentation --- examples/nrf/src/bin/pwm_sequence.rs | 2 +- examples/nrf/src/bin/pwm_simple_sin.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'examples') diff --git a/examples/nrf/src/bin/pwm_sequence.rs b/examples/nrf/src/bin/pwm_sequence.rs index 8b7aeddcd..1c5ab2682 100644 --- a/examples/nrf/src/bin/pwm_sequence.rs +++ b/examples/nrf/src/bin/pwm_sequence.rs @@ -23,7 +23,7 @@ async fn main(_spawner: Spawner, p: Peripherals) { sequence: &seq_values, sequence_load: SequenceLoad::Individual, refresh: 0, - enddelay: 0, + end_delay: 0, additional_loops: LoopMode::Additional(5), }; diff --git a/examples/nrf/src/bin/pwm_simple_sin.rs b/examples/nrf/src/bin/pwm_simple_sin.rs index c7d85381b..909b8bf3a 100644 --- a/examples/nrf/src/bin/pwm_simple_sin.rs +++ b/examples/nrf/src/bin/pwm_simple_sin.rs @@ -27,7 +27,7 @@ async fn main(_spawner: Spawner, p: Peripherals) { sequence: &seq_values, sequence_load: SequenceLoad::Common, refresh: 0, - enddelay: 0, + end_delay: 1, additional_loops: LoopMode::Infinite, }; -- cgit From 90be851e4b52703b4297615253bc7a89fdd03b9f Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Mon, 1 Nov 2021 08:45:07 -0700 Subject: reduce complexity of loopmode --- examples/nrf/src/bin/pwm_sequence.rs | 2 +- examples/nrf/src/bin/pwm_simple_sin.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'examples') diff --git a/examples/nrf/src/bin/pwm_sequence.rs b/examples/nrf/src/bin/pwm_sequence.rs index 1c5ab2682..40ba7ab1c 100644 --- a/examples/nrf/src/bin/pwm_sequence.rs +++ b/examples/nrf/src/bin/pwm_sequence.rs @@ -24,7 +24,7 @@ async fn main(_spawner: Spawner, p: Peripherals) { sequence_load: SequenceLoad::Individual, refresh: 0, end_delay: 0, - additional_loops: LoopMode::Additional(5), + times: LoopMode::Times(5), }; let _pwm = unwrap!(Pwm::simple_playback( diff --git a/examples/nrf/src/bin/pwm_simple_sin.rs b/examples/nrf/src/bin/pwm_simple_sin.rs index 909b8bf3a..c1af0db8c 100644 --- a/examples/nrf/src/bin/pwm_simple_sin.rs +++ b/examples/nrf/src/bin/pwm_simple_sin.rs @@ -27,8 +27,8 @@ async fn main(_spawner: Spawner, p: Peripherals) { sequence: &seq_values, sequence_load: SequenceLoad::Common, refresh: 0, - end_delay: 1, - additional_loops: LoopMode::Infinite, + end_delay: 0, + times: LoopMode::Infinite, }; let pwm = unwrap!(Pwm::simple_playback( -- cgit From 12b2c5d5f70b836c853c000f036df2bdc255098d Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Mon, 1 Nov 2021 08:54:07 -0700 Subject: better not as a constructor? --- examples/nrf/src/bin/pwm_sequence.rs | 11 +++++------ examples/nrf/src/bin/pwm_simple_sin.rs | 11 +++++------ 2 files changed, 10 insertions(+), 12 deletions(-) (limited to 'examples') diff --git a/examples/nrf/src/bin/pwm_sequence.rs b/examples/nrf/src/bin/pwm_sequence.rs index 40ba7ab1c..066ab3c03 100644 --- a/examples/nrf/src/bin/pwm_sequence.rs +++ b/examples/nrf/src/bin/pwm_sequence.rs @@ -7,7 +7,7 @@ mod example_common; use defmt::*; use embassy::executor::Spawner; use embassy::time::{Duration, Timer}; -use embassy_nrf::pwm::{CounterMode, LoopMode, LoopingConfig, Prescaler, Pwm, SequenceLoad}; +use embassy_nrf::pwm::{CounterMode, Prescaler, Pwm, SequenceConfig, SequenceLoad, SequenceMode}; use embassy_nrf::Peripherals; #[embassy::main] @@ -16,7 +16,7 @@ async fn main(_spawner: Spawner, p: Peripherals) { 0x8000, 0, 0, 0, 0, 0x8000, 0, 0, 0, 0, 0x8000, 0, 0, 0, 0, 0x8000, ]; - let config = LoopingConfig { + let config = SequenceConfig { counter_mode: CounterMode::Up, top: 15625, prescaler: Prescaler::Div128, @@ -24,12 +24,11 @@ async fn main(_spawner: Spawner, p: Peripherals) { sequence_load: SequenceLoad::Individual, refresh: 0, end_delay: 0, - times: LoopMode::Times(5), + times: SequenceMode::Times(5), }; - let _pwm = unwrap!(Pwm::simple_playback( - p.PWM0, p.P0_13, p.P0_15, p.P0_16, p.P0_14, config - )); + let pwm = Pwm::new(p.PWM0, p.P0_13, p.P0_15, p.P0_16, p.P0_14); + unwrap!(pwm.play_sequence(config)); info!("pwm started!"); loop { diff --git a/examples/nrf/src/bin/pwm_simple_sin.rs b/examples/nrf/src/bin/pwm_simple_sin.rs index c1af0db8c..3c1bddbc7 100644 --- a/examples/nrf/src/bin/pwm_simple_sin.rs +++ b/examples/nrf/src/bin/pwm_simple_sin.rs @@ -9,7 +9,7 @@ use defmt::*; use embassy::executor::Spawner; use embassy::time::{Duration, Timer}; use embassy_nrf::gpio::NoPin; -use embassy_nrf::pwm::{CounterMode, LoopMode, LoopingConfig, Prescaler, Pwm, SequenceLoad}; +use embassy_nrf::pwm::{CounterMode, Prescaler, Pwm, SequenceConfig, SequenceLoad, SequenceMode}; use embassy_nrf::Peripherals; use micromath::F32Ext; @@ -20,7 +20,7 @@ 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 { + let config = SequenceConfig { counter_mode: CounterMode::UpAndDown, top: 12000, prescaler: Prescaler::Div16, @@ -28,12 +28,11 @@ async fn main(_spawner: Spawner, p: Peripherals) { sequence_load: SequenceLoad::Common, refresh: 0, end_delay: 0, - times: LoopMode::Infinite, + times: SequenceMode::Infinite, }; - let pwm = unwrap!(Pwm::simple_playback( - p.PWM0, p.P0_13, NoPin, NoPin, NoPin, config - )); + let pwm = Pwm::new(p.PWM0, p.P0_13, NoPin, NoPin, NoPin); + unwrap!(pwm.play_sequence(config)); info!("pwm started!"); Timer::after(Duration::from_millis(20000)).await; -- cgit From 5285179218a0d5137dc6c1c16f2d01617c310a0a Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Mon, 1 Nov 2021 09:37:34 -0700 Subject: generalize new and change pwm example to a servo --- examples/nrf/src/bin/pwm.rs | 111 ++++++++++++-------------------------------- 1 file changed, 29 insertions(+), 82 deletions(-) (limited to 'examples') diff --git a/examples/nrf/src/bin/pwm.rs b/examples/nrf/src/bin/pwm.rs index ab033eb50..aeda80be8 100644 --- a/examples/nrf/src/bin/pwm.rs +++ b/examples/nrf/src/bin/pwm.rs @@ -7,95 +7,42 @@ mod example_common; use defmt::*; use embassy::executor::Spawner; use embassy::time::{Duration, Timer}; +use embassy_nrf::gpio::NoPin; use embassy_nrf::pwm::{Prescaler, Pwm}; use embassy_nrf::Peripherals; -// for i in range(1024): print(int((math.sin(i/512*math.pi)*0.4+0.5)**2*32767), ', ', end='') -static DUTY: [u16; 1024] = [ - 8191, 8272, 8353, 8434, 8516, 8598, 8681, 8764, 8847, 8931, 9015, 9099, 9184, 9269, 9354, 9440, - 9526, 9613, 9700, 9787, 9874, 9962, 10050, 10139, 10227, 10316, 10406, 10495, 10585, 10675, - 10766, 10857, 10948, 11039, 11131, 11223, 11315, 11407, 11500, 11592, 11685, 11779, 11872, - 11966, 12060, 12154, 12248, 12343, 12438, 12533, 12628, 12723, 12818, 12914, 13010, 13106, - 13202, 13298, 13394, 13491, 13587, 13684, 13781, 13878, 13975, 14072, 14169, 14266, 14364, - 14461, 14558, 14656, 14754, 14851, 14949, 15046, 15144, 15242, 15339, 15437, 15535, 15632, - 15730, 15828, 15925, 16023, 16120, 16218, 16315, 16412, 16510, 16607, 16704, 16801, 16898, - 16995, 17091, 17188, 17284, 17380, 17477, 17572, 17668, 17764, 17859, 17955, 18050, 18145, - 18239, 18334, 18428, 18522, 18616, 18710, 18803, 18896, 18989, 19082, 19174, 19266, 19358, - 19449, 19540, 19631, 19722, 19812, 19902, 19991, 20081, 20169, 20258, 20346, 20434, 20521, - 20608, 20695, 20781, 20867, 20952, 21037, 21122, 21206, 21290, 21373, 21456, 21538, 21620, - 21701, 21782, 21863, 21943, 22022, 22101, 22179, 22257, 22335, 22412, 22488, 22564, 22639, - 22714, 22788, 22861, 22934, 23007, 23079, 23150, 23220, 23290, 23360, 23429, 23497, 23564, - 23631, 23698, 23763, 23828, 23892, 23956, 24019, 24081, 24143, 24204, 24264, 24324, 24383, - 24441, 24499, 24555, 24611, 24667, 24721, 24775, 24828, 24881, 24933, 24983, 25034, 25083, - 25132, 25180, 25227, 25273, 25319, 25363, 25407, 25451, 25493, 25535, 25575, 25615, 25655, - 25693, 25731, 25767, 25803, 25838, 25873, 25906, 25939, 25971, 26002, 26032, 26061, 26089, - 26117, 26144, 26170, 26195, 26219, 26242, 26264, 26286, 26307, 26327, 26346, 26364, 26381, - 26397, 26413, 26427, 26441, 26454, 26466, 26477, 26487, 26496, 26505, 26512, 26519, 26525, - 26530, 26534, 26537, 26539, 26540, 26541, 26540, 26539, 26537, 26534, 26530, 26525, 26519, - 26512, 26505, 26496, 26487, 26477, 26466, 26454, 26441, 26427, 26413, 26397, 26381, 26364, - 26346, 26327, 26307, 26286, 26264, 26242, 26219, 26195, 26170, 26144, 26117, 26089, 26061, - 26032, 26002, 25971, 25939, 25906, 25873, 25838, 25803, 25767, 25731, 25693, 25655, 25615, - 25575, 25535, 25493, 25451, 25407, 25363, 25319, 25273, 25227, 25180, 25132, 25083, 25034, - 24983, 24933, 24881, 24828, 24775, 24721, 24667, 24611, 24555, 24499, 24441, 24383, 24324, - 24264, 24204, 24143, 24081, 24019, 23956, 23892, 23828, 23763, 23698, 23631, 23564, 23497, - 23429, 23360, 23290, 23220, 23150, 23079, 23007, 22934, 22861, 22788, 22714, 22639, 22564, - 22488, 22412, 22335, 22257, 22179, 22101, 22022, 21943, 21863, 21782, 21701, 21620, 21538, - 21456, 21373, 21290, 21206, 21122, 21037, 20952, 20867, 20781, 20695, 20608, 20521, 20434, - 20346, 20258, 20169, 20081, 19991, 19902, 19812, 19722, 19631, 19540, 19449, 19358, 19266, - 19174, 19082, 18989, 18896, 18803, 18710, 18616, 18522, 18428, 18334, 18239, 18145, 18050, - 17955, 17859, 17764, 17668, 17572, 17477, 17380, 17284, 17188, 17091, 16995, 16898, 16801, - 16704, 16607, 16510, 16412, 16315, 16218, 16120, 16023, 15925, 15828, 15730, 15632, 15535, - 15437, 15339, 15242, 15144, 15046, 14949, 14851, 14754, 14656, 14558, 14461, 14364, 14266, - 14169, 14072, 13975, 13878, 13781, 13684, 13587, 13491, 13394, 13298, 13202, 13106, 13010, - 12914, 12818, 12723, 12628, 12533, 12438, 12343, 12248, 12154, 12060, 11966, 11872, 11779, - 11685, 11592, 11500, 11407, 11315, 11223, 11131, 11039, 10948, 10857, 10766, 10675, 10585, - 10495, 10406, 10316, 10227, 10139, 10050, 9962, 9874, 9787, 9700, 9613, 9526, 9440, 9354, 9269, - 9184, 9099, 9015, 8931, 8847, 8764, 8681, 8598, 8516, 8434, 8353, 8272, 8191, 8111, 8031, 7952, - 7873, 7794, 7716, 7638, 7561, 7484, 7407, 7331, 7255, 7180, 7105, 7031, 6957, 6883, 6810, 6738, - 6665, 6594, 6522, 6451, 6381, 6311, 6241, 6172, 6104, 6036, 5968, 5901, 5834, 5767, 5702, 5636, - 5571, 5507, 5443, 5379, 5316, 5253, 5191, 5130, 5068, 5008, 4947, 4888, 4828, 4769, 4711, 4653, - 4596, 4539, 4482, 4426, 4371, 4316, 4261, 4207, 4153, 4100, 4047, 3995, 3943, 3892, 3841, 3791, - 3741, 3691, 3642, 3594, 3546, 3498, 3451, 3404, 3358, 3312, 3267, 3222, 3178, 3134, 3090, 3047, - 3005, 2962, 2921, 2879, 2839, 2798, 2758, 2719, 2680, 2641, 2603, 2565, 2528, 2491, 2454, 2418, - 2382, 2347, 2312, 2278, 2244, 2210, 2177, 2144, 2112, 2080, 2048, 2017, 1986, 1956, 1926, 1896, - 1867, 1838, 1810, 1781, 1754, 1726, 1699, 1673, 1646, 1620, 1595, 1570, 1545, 1520, 1496, 1472, - 1449, 1426, 1403, 1380, 1358, 1336, 1315, 1294, 1273, 1252, 1232, 1212, 1192, 1173, 1154, 1135, - 1117, 1099, 1081, 1063, 1046, 1029, 1012, 996, 980, 964, 948, 933, 918, 903, 888, 874, 860, - 846, 833, 819, 806, 793, 781, 768, 756, 744, 733, 721, 710, 699, 688, 677, 667, 657, 647, 637, - 627, 618, 609, 599, 591, 582, 574, 565, 557, 549, 541, 534, 526, 519, 512, 505, 498, 492, 485, - 479, 473, 467, 461, 455, 450, 444, 439, 434, 429, 424, 419, 415, 410, 406, 402, 398, 394, 390, - 386, 383, 379, 376, 373, 370, 367, 364, 361, 359, 356, 354, 351, 349, 347, 345, 343, 342, 340, - 338, 337, 336, 334, 333, 332, 331, 330, 330, 329, 328, 328, 328, 327, 327, 327, 327, 327, 328, - 328, 328, 329, 330, 330, 331, 332, 333, 334, 336, 337, 338, 340, 342, 343, 345, 347, 349, 351, - 354, 356, 359, 361, 364, 367, 370, 373, 376, 379, 383, 386, 390, 394, 398, 402, 406, 410, 415, - 419, 424, 429, 434, 439, 444, 450, 455, 461, 467, 473, 479, 485, 492, 498, 505, 512, 519, 526, - 534, 541, 549, 557, 565, 574, 582, 591, 599, 609, 618, 627, 637, 647, 657, 667, 677, 688, 699, - 710, 721, 733, 744, 756, 768, 781, 793, 806, 819, 833, 846, 860, 874, 888, 903, 918, 933, 948, - 964, 980, 996, 1012, 1029, 1046, 1063, 1081, 1099, 1117, 1135, 1154, 1173, 1192, 1212, 1232, - 1252, 1273, 1294, 1315, 1336, 1358, 1380, 1403, 1426, 1449, 1472, 1496, 1520, 1545, 1570, 1595, - 1620, 1646, 1673, 1699, 1726, 1754, 1781, 1810, 1838, 1867, 1896, 1926, 1956, 1986, 2017, 2048, - 2080, 2112, 2144, 2177, 2210, 2244, 2278, 2312, 2347, 2382, 2418, 2454, 2491, 2528, 2565, 2603, - 2641, 2680, 2719, 2758, 2798, 2839, 2879, 2921, 2962, 3005, 3047, 3090, 3134, 3178, 3222, 3267, - 3312, 3358, 3404, 3451, 3498, 3546, 3594, 3642, 3691, 3741, 3791, 3841, 3892, 3943, 3995, 4047, - 4100, 4153, 4207, 4261, 4316, 4371, 4426, 4482, 4539, 4596, 4653, 4711, 4769, 4828, 4888, 4947, - 5008, 5068, 5130, 5191, 5253, 5316, 5379, 5443, 5507, 5571, 5636, 5702, 5767, 5834, 5901, 5968, - 6036, 6104, 6172, 6241, 6311, 6381, 6451, 6522, 6594, 6665, 6738, 6810, 6883, 6957, 7031, 7105, - 7180, 7255, 7331, 7407, 7484, 7561, 7638, 7716, 7794, 7873, 7952, 8031, 8111, -]; - #[embassy::main] async fn main(_spawner: Spawner, p: Peripherals) { - let pwm = Pwm::new(p.PWM0, p.P0_13, p.P0_14, p.P0_16, p.P0_15); - pwm.set_prescaler(Prescaler::Div1); + let pwm = Pwm::new(p.PWM0, p.P0_05, NoPin, NoPin, NoPin); + // sg90 microervo requires 50hz or 20ms period + // set_period can only set down to 125khz so we cant use it directly + // Div128 is 125khz or 0.000008s or 0.008ms, 20/0.008 is 2500 is top + pwm.set_prescaler(Prescaler::Div128); + pwm.set_max_duty(2500); info!("pwm initialized!"); - let mut i = 0; + Timer::after(Duration::from_millis(5000)).await; + + // 1ms 0deg (1/.008=125), 1.5ms 90deg (1.5/.008=187.5), 2ms 180deg (2/.008=250), loop { - i += 1; - pwm.set_duty(0, DUTY[i % 1024]); - pwm.set_duty(1, DUTY[(i + 256) % 1024]); - pwm.set_duty(2, DUTY[(i + 512) % 1024]); - pwm.set_duty(3, DUTY[(i + 768) % 1024]); - Timer::after(Duration::from_millis(3)).await; + info!("45 deg"); + pwm.set_duty(0, 2500 - 156); + Timer::after(Duration::from_millis(5000)).await; + + info!("90 deg"); + pwm.set_duty(0, 2500 - 187); + Timer::after(Duration::from_millis(5000)).await; + + info!("135 deg"); + pwm.set_duty(0, 2500 - 218); + Timer::after(Duration::from_millis(5000)).await; + + info!("180 deg"); + pwm.set_duty(0, 2500 - 250); + Timer::after(Duration::from_millis(5000)).await; + + info!("0 deg"); + pwm.set_duty(0, 2500 - 125); + Timer::after(Duration::from_millis(5000)).await; } } -- cgit From b297e5f7bd152d1e5147631c81987c26f9fa0664 Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Mon, 1 Nov 2021 13:51:40 -0700 Subject: led dimming example, dont need to keep all examples, just covering ground to test api --- examples/nrf/src/bin/pwm_led.rs | 47 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 examples/nrf/src/bin/pwm_led.rs (limited to 'examples') diff --git a/examples/nrf/src/bin/pwm_led.rs b/examples/nrf/src/bin/pwm_led.rs new file mode 100644 index 000000000..067f4d2ca --- /dev/null +++ b/examples/nrf/src/bin/pwm_led.rs @@ -0,0 +1,47 @@ +#![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, Pwm}; +use embassy_nrf::Peripherals; + +#[embassy::main] +async fn main(_spawner: Spawner, p: Peripherals) { + let pwm = Pwm::new(p.PWM0, p.P0_13, NoPin, NoPin, NoPin); + // set_period doesnt actually set what you give it, because it only has a + // few options from the hardhware so be explicit instead + // Div128 is slowest, 125khz still crazy fast for our eyes + pwm.set_prescaler(Prescaler::Div128); + + info!("pwm initialized!"); + + // default max_duty if not specified is 1000 + // so 0 would be fully off and 1000 or above would be fully on + loop { + info!("100%"); + pwm.set_duty(0, 1000); + Timer::after(Duration::from_millis(5000)).await; + + info!("25%"); + pwm.set_duty(0, 250); + Timer::after(Duration::from_millis(5000)).await; + + info!("10%"); + pwm.set_duty(0, 100); + Timer::after(Duration::from_millis(5000)).await; + + info!("5%"); + pwm.set_duty(0, 50); + Timer::after(Duration::from_millis(5000)).await; + + info!("0%"); + pwm.set_duty(0, 0); + Timer::after(Duration::from_millis(5000)).await; + } +} -- cgit From 49253152cff2a45bd08cd1801bb6d5f0f3ce9b30 Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Mon, 1 Nov 2021 19:11:37 -0700 Subject: seperate sequence from duty cycle pwm struct --- examples/nrf/src/bin/pwm_sequence.rs | 9 ++++++--- examples/nrf/src/bin/pwm_simple_sin.rs | 9 +++++---- 2 files changed, 11 insertions(+), 7 deletions(-) (limited to 'examples') diff --git a/examples/nrf/src/bin/pwm_sequence.rs b/examples/nrf/src/bin/pwm_sequence.rs index 066ab3c03..0a7bea1c4 100644 --- a/examples/nrf/src/bin/pwm_sequence.rs +++ b/examples/nrf/src/bin/pwm_sequence.rs @@ -7,7 +7,9 @@ mod example_common; use defmt::*; use embassy::executor::Spawner; use embassy::time::{Duration, Timer}; -use embassy_nrf::pwm::{CounterMode, Prescaler, Pwm, SequenceConfig, SequenceLoad, SequenceMode}; +use embassy_nrf::pwm::{ + CounterMode, Prescaler, PwmSeq, SequenceConfig, SequenceLoad, SequenceMode, +}; use embassy_nrf::Peripherals; #[embassy::main] @@ -27,8 +29,9 @@ async fn main(_spawner: Spawner, p: Peripherals) { times: SequenceMode::Times(5), }; - let pwm = Pwm::new(p.PWM0, p.P0_13, p.P0_15, p.P0_16, p.P0_14); - unwrap!(pwm.play_sequence(config)); + let _pwm = unwrap!(PwmSeq::new( + p.PWM0, p.P0_13, p.P0_15, p.P0_16, p.P0_14, config + )); info!("pwm started!"); loop { diff --git a/examples/nrf/src/bin/pwm_simple_sin.rs b/examples/nrf/src/bin/pwm_simple_sin.rs index 3c1bddbc7..6fd59c6a4 100644 --- a/examples/nrf/src/bin/pwm_simple_sin.rs +++ b/examples/nrf/src/bin/pwm_simple_sin.rs @@ -9,7 +9,9 @@ use defmt::*; use embassy::executor::Spawner; use embassy::time::{Duration, Timer}; use embassy_nrf::gpio::NoPin; -use embassy_nrf::pwm::{CounterMode, Prescaler, Pwm, SequenceConfig, SequenceLoad, SequenceMode}; +use embassy_nrf::pwm::{ + CounterMode, Prescaler, PwmSeq, SequenceConfig, SequenceLoad, SequenceMode, +}; use embassy_nrf::Peripherals; use micromath::F32Ext; @@ -31,13 +33,12 @@ async fn main(_spawner: Spawner, p: Peripherals) { times: SequenceMode::Infinite, }; - let pwm = Pwm::new(p.PWM0, p.P0_13, NoPin, NoPin, NoPin); - unwrap!(pwm.play_sequence(config)); + let pwm = unwrap!(PwmSeq::new(p.PWM0, p.P0_13, NoPin, NoPin, NoPin, config)); info!("pwm started!"); Timer::after(Duration::from_millis(20000)).await; - pwm.sequence_stop(); + pwm.stop(); info!("pwm stopped!"); loop { -- cgit From 4647792ad68209f3be6e1cdf1cee9513025c14ab Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Mon, 1 Nov 2021 20:18:24 -0700 Subject: seperate start from pwmseq::new --- examples/nrf/src/bin/pwm_sequence.rs | 4 ++-- examples/nrf/src/bin/pwm_simple_sin.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'examples') diff --git a/examples/nrf/src/bin/pwm_sequence.rs b/examples/nrf/src/bin/pwm_sequence.rs index 0a7bea1c4..6911d0348 100644 --- a/examples/nrf/src/bin/pwm_sequence.rs +++ b/examples/nrf/src/bin/pwm_sequence.rs @@ -26,12 +26,12 @@ async fn main(_spawner: Spawner, p: Peripherals) { sequence_load: SequenceLoad::Individual, refresh: 0, end_delay: 0, - times: SequenceMode::Times(5), }; - let _pwm = unwrap!(PwmSeq::new( + let pwm = unwrap!(PwmSeq::new( p.PWM0, p.P0_13, p.P0_15, p.P0_16, p.P0_14, config )); + unwrap!(pwm.start(SequenceMode::Times(5))); info!("pwm started!"); loop { diff --git a/examples/nrf/src/bin/pwm_simple_sin.rs b/examples/nrf/src/bin/pwm_simple_sin.rs index 6fd59c6a4..985f9d80a 100644 --- a/examples/nrf/src/bin/pwm_simple_sin.rs +++ b/examples/nrf/src/bin/pwm_simple_sin.rs @@ -30,10 +30,10 @@ async fn main(_spawner: Spawner, p: Peripherals) { sequence_load: SequenceLoad::Common, refresh: 0, end_delay: 0, - times: SequenceMode::Infinite, }; let pwm = unwrap!(PwmSeq::new(p.PWM0, p.P0_13, NoPin, NoPin, NoPin, config)); + unwrap!(pwm.start(SequenceMode::Infinite)); info!("pwm started!"); Timer::after(Duration::from_millis(20000)).await; -- cgit From c939edb8d03a6ddf965507e3b2de02b82ca255b4 Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Tue, 2 Nov 2021 10:57:01 -0700 Subject: rename error enum again --- examples/nrf/src/bin/pwm_sequence.rs | 2 +- examples/nrf/src/bin/pwm_simple_sin.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'examples') diff --git a/examples/nrf/src/bin/pwm_sequence.rs b/examples/nrf/src/bin/pwm_sequence.rs index 6911d0348..8830e52d6 100644 --- a/examples/nrf/src/bin/pwm_sequence.rs +++ b/examples/nrf/src/bin/pwm_sequence.rs @@ -31,7 +31,7 @@ async fn main(_spawner: Spawner, p: Peripherals) { let pwm = unwrap!(PwmSeq::new( p.PWM0, p.P0_13, p.P0_15, p.P0_16, p.P0_14, config )); - unwrap!(pwm.start(SequenceMode::Times(5))); + let _ = pwm.start(SequenceMode::Times(5)); info!("pwm started!"); loop { diff --git a/examples/nrf/src/bin/pwm_simple_sin.rs b/examples/nrf/src/bin/pwm_simple_sin.rs index 985f9d80a..ac3089c84 100644 --- a/examples/nrf/src/bin/pwm_simple_sin.rs +++ b/examples/nrf/src/bin/pwm_simple_sin.rs @@ -33,7 +33,7 @@ async fn main(_spawner: Spawner, p: Peripherals) { }; let pwm = unwrap!(PwmSeq::new(p.PWM0, p.P0_13, NoPin, NoPin, NoPin, config)); - unwrap!(pwm.start(SequenceMode::Infinite)); + let _ = pwm.start(SequenceMode::Infinite); info!("pwm started!"); Timer::after(Duration::from_millis(20000)).await; -- cgit From 44375b427c20273d91845e5e012e669d8a2e1cc0 Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Wed, 3 Nov 2021 15:26:44 -0700 Subject: restore example and add set_time_stretch api --- examples/nrf/src/bin/pwm.rs | 113 ++++++++++++++++++++++++++++---------- examples/nrf/src/bin/pwm_servo.rs | 48 ++++++++++++++++ 2 files changed, 132 insertions(+), 29 deletions(-) create mode 100644 examples/nrf/src/bin/pwm_servo.rs (limited to 'examples') diff --git a/examples/nrf/src/bin/pwm.rs b/examples/nrf/src/bin/pwm.rs index aeda80be8..4c18b77b4 100644 --- a/examples/nrf/src/bin/pwm.rs +++ b/examples/nrf/src/bin/pwm.rs @@ -7,42 +7,97 @@ mod example_common; use defmt::*; use embassy::executor::Spawner; use embassy::time::{Duration, Timer}; -use embassy_nrf::gpio::NoPin; use embassy_nrf::pwm::{Prescaler, Pwm}; use embassy_nrf::Peripherals; +// for i in range(1024): print(int((math.sin(i/512*math.pi)*0.4+0.5)**2*32767), ', ', end='') +static DUTY: [u16; 1024] = [ + 8191, 8272, 8353, 8434, 8516, 8598, 8681, 8764, 8847, 8931, 9015, 9099, 9184, 9269, 9354, 9440, + 9526, 9613, 9700, 9787, 9874, 9962, 10050, 10139, 10227, 10316, 10406, 10495, 10585, 10675, + 10766, 10857, 10948, 11039, 11131, 11223, 11315, 11407, 11500, 11592, 11685, 11779, 11872, + 11966, 12060, 12154, 12248, 12343, 12438, 12533, 12628, 12723, 12818, 12914, 13010, 13106, + 13202, 13298, 13394, 13491, 13587, 13684, 13781, 13878, 13975, 14072, 14169, 14266, 14364, + 14461, 14558, 14656, 14754, 14851, 14949, 15046, 15144, 15242, 15339, 15437, 15535, 15632, + 15730, 15828, 15925, 16023, 16120, 16218, 16315, 16412, 16510, 16607, 16704, 16801, 16898, + 16995, 17091, 17188, 17284, 17380, 17477, 17572, 17668, 17764, 17859, 17955, 18050, 18145, + 18239, 18334, 18428, 18522, 18616, 18710, 18803, 18896, 18989, 19082, 19174, 19266, 19358, + 19449, 19540, 19631, 19722, 19812, 19902, 19991, 20081, 20169, 20258, 20346, 20434, 20521, + 20608, 20695, 20781, 20867, 20952, 21037, 21122, 21206, 21290, 21373, 21456, 21538, 21620, + 21701, 21782, 21863, 21943, 22022, 22101, 22179, 22257, 22335, 22412, 22488, 22564, 22639, + 22714, 22788, 22861, 22934, 23007, 23079, 23150, 23220, 23290, 23360, 23429, 23497, 23564, + 23631, 23698, 23763, 23828, 23892, 23956, 24019, 24081, 24143, 24204, 24264, 24324, 24383, + 24441, 24499, 24555, 24611, 24667, 24721, 24775, 24828, 24881, 24933, 24983, 25034, 25083, + 25132, 25180, 25227, 25273, 25319, 25363, 25407, 25451, 25493, 25535, 25575, 25615, 25655, + 25693, 25731, 25767, 25803, 25838, 25873, 25906, 25939, 25971, 26002, 26032, 26061, 26089, + 26117, 26144, 26170, 26195, 26219, 26242, 26264, 26286, 26307, 26327, 26346, 26364, 26381, + 26397, 26413, 26427, 26441, 26454, 26466, 26477, 26487, 26496, 26505, 26512, 26519, 26525, + 26530, 26534, 26537, 26539, 26540, 26541, 26540, 26539, 26537, 26534, 26530, 26525, 26519, + 26512, 26505, 26496, 26487, 26477, 26466, 26454, 26441, 26427, 26413, 26397, 26381, 26364, + 26346, 26327, 26307, 26286, 26264, 26242, 26219, 26195, 26170, 26144, 26117, 26089, 26061, + 26032, 26002, 25971, 25939, 25906, 25873, 25838, 25803, 25767, 25731, 25693, 25655, 25615, + 25575, 25535, 25493, 25451, 25407, 25363, 25319, 25273, 25227, 25180, 25132, 25083, 25034, + 24983, 24933, 24881, 24828, 24775, 24721, 24667, 24611, 24555, 24499, 24441, 24383, 24324, + 24264, 24204, 24143, 24081, 24019, 23956, 23892, 23828, 23763, 23698, 23631, 23564, 23497, + 23429, 23360, 23290, 23220, 23150, 23079, 23007, 22934, 22861, 22788, 22714, 22639, 22564, + 22488, 22412, 22335, 22257, 22179, 22101, 22022, 21943, 21863, 21782, 21701, 21620, 21538, + 21456, 21373, 21290, 21206, 21122, 21037, 20952, 20867, 20781, 20695, 20608, 20521, 20434, + 20346, 20258, 20169, 20081, 19991, 19902, 19812, 19722, 19631, 19540, 19449, 19358, 19266, + 19174, 19082, 18989, 18896, 18803, 18710, 18616, 18522, 18428, 18334, 18239, 18145, 18050, + 17955, 17859, 17764, 17668, 17572, 17477, 17380, 17284, 17188, 17091, 16995, 16898, 16801, + 16704, 16607, 16510, 16412, 16315, 16218, 16120, 16023, 15925, 15828, 15730, 15632, 15535, + 15437, 15339, 15242, 15144, 15046, 14949, 14851, 14754, 14656, 14558, 14461, 14364, 14266, + 14169, 14072, 13975, 13878, 13781, 13684, 13587, 13491, 13394, 13298, 13202, 13106, 13010, + 12914, 12818, 12723, 12628, 12533, 12438, 12343, 12248, 12154, 12060, 11966, 11872, 11779, + 11685, 11592, 11500, 11407, 11315, 11223, 11131, 11039, 10948, 10857, 10766, 10675, 10585, + 10495, 10406, 10316, 10227, 10139, 10050, 9962, 9874, 9787, 9700, 9613, 9526, 9440, 9354, 9269, + 9184, 9099, 9015, 8931, 8847, 8764, 8681, 8598, 8516, 8434, 8353, 8272, 8191, 8111, 8031, 7952, + 7873, 7794, 7716, 7638, 7561, 7484, 7407, 7331, 7255, 7180, 7105, 7031, 6957, 6883, 6810, 6738, + 6665, 6594, 6522, 6451, 6381, 6311, 6241, 6172, 6104, 6036, 5968, 5901, 5834, 5767, 5702, 5636, + 5571, 5507, 5443, 5379, 5316, 5253, 5191, 5130, 5068, 5008, 4947, 4888, 4828, 4769, 4711, 4653, + 4596, 4539, 4482, 4426, 4371, 4316, 4261, 4207, 4153, 4100, 4047, 3995, 3943, 3892, 3841, 3791, + 3741, 3691, 3642, 3594, 3546, 3498, 3451, 3404, 3358, 3312, 3267, 3222, 3178, 3134, 3090, 3047, + 3005, 2962, 2921, 2879, 2839, 2798, 2758, 2719, 2680, 2641, 2603, 2565, 2528, 2491, 2454, 2418, + 2382, 2347, 2312, 2278, 2244, 2210, 2177, 2144, 2112, 2080, 2048, 2017, 1986, 1956, 1926, 1896, + 1867, 1838, 1810, 1781, 1754, 1726, 1699, 1673, 1646, 1620, 1595, 1570, 1545, 1520, 1496, 1472, + 1449, 1426, 1403, 1380, 1358, 1336, 1315, 1294, 1273, 1252, 1232, 1212, 1192, 1173, 1154, 1135, + 1117, 1099, 1081, 1063, 1046, 1029, 1012, 996, 980, 964, 948, 933, 918, 903, 888, 874, 860, + 846, 833, 819, 806, 793, 781, 768, 756, 744, 733, 721, 710, 699, 688, 677, 667, 657, 647, 637, + 627, 618, 609, 599, 591, 582, 574, 565, 557, 549, 541, 534, 526, 519, 512, 505, 498, 492, 485, + 479, 473, 467, 461, 455, 450, 444, 439, 434, 429, 424, 419, 415, 410, 406, 402, 398, 394, 390, + 386, 383, 379, 376, 373, 370, 367, 364, 361, 359, 356, 354, 351, 349, 347, 345, 343, 342, 340, + 338, 337, 336, 334, 333, 332, 331, 330, 330, 329, 328, 328, 328, 327, 327, 327, 327, 327, 328, + 328, 328, 329, 330, 330, 331, 332, 333, 334, 336, 337, 338, 340, 342, 343, 345, 347, 349, 351, + 354, 356, 359, 361, 364, 367, 370, 373, 376, 379, 383, 386, 390, 394, 398, 402, 406, 410, 415, + 419, 424, 429, 434, 439, 444, 450, 455, 461, 467, 473, 479, 485, 492, 498, 505, 512, 519, 526, + 534, 541, 549, 557, 565, 574, 582, 591, 599, 609, 618, 627, 637, 647, 657, 667, 677, 688, 699, + 710, 721, 733, 744, 756, 768, 781, 793, 806, 819, 833, 846, 860, 874, 888, 903, 918, 933, 948, + 964, 980, 996, 1012, 1029, 1046, 1063, 1081, 1099, 1117, 1135, 1154, 1173, 1192, 1212, 1232, + 1252, 1273, 1294, 1315, 1336, 1358, 1380, 1403, 1426, 1449, 1472, 1496, 1520, 1545, 1570, 1595, + 1620, 1646, 1673, 1699, 1726, 1754, 1781, 1810, 1838, 1867, 1896, 1926, 1956, 1986, 2017, 2048, + 2080, 2112, 2144, 2177, 2210, 2244, 2278, 2312, 2347, 2382, 2418, 2454, 2491, 2528, 2565, 2603, + 2641, 2680, 2719, 2758, 2798, 2839, 2879, 2921, 2962, 3005, 3047, 3090, 3134, 3178, 3222, 3267, + 3312, 3358, 3404, 3451, 3498, 3546, 3594, 3642, 3691, 3741, 3791, 3841, 3892, 3943, 3995, 4047, + 4100, 4153, 4207, 4261, 4316, 4371, 4426, 4482, 4539, 4596, 4653, 4711, 4769, 4828, 4888, 4947, + 5008, 5068, 5130, 5191, 5253, 5316, 5379, 5443, 5507, 5571, 5636, 5702, 5767, 5834, 5901, 5968, + 6036, 6104, 6172, 6241, 6311, 6381, 6451, 6522, 6594, 6665, 6738, 6810, 6883, 6957, 7031, 7105, + 7180, 7255, 7331, 7407, 7484, 7561, 7638, 7716, 7794, 7873, 7952, 8031, 8111, +]; + #[embassy::main] async fn main(_spawner: Spawner, p: Peripherals) { - let pwm = Pwm::new(p.PWM0, p.P0_05, NoPin, NoPin, NoPin); - // sg90 microervo requires 50hz or 20ms period - // set_period can only set down to 125khz so we cant use it directly - // Div128 is 125khz or 0.000008s or 0.008ms, 20/0.008 is 2500 is top - pwm.set_prescaler(Prescaler::Div128); - pwm.set_max_duty(2500); + let pwm = Pwm::new(p.PWM0, p.P0_13, p.P0_14, p.P0_16, p.P0_15); + pwm.set_prescaler(Prescaler::Div1); + pwm.set_max_duty(32767); + pwm.set_time_stretch(32); info!("pwm initialized!"); - Timer::after(Duration::from_millis(5000)).await; - - // 1ms 0deg (1/.008=125), 1.5ms 90deg (1.5/.008=187.5), 2ms 180deg (2/.008=250), + let mut i = 0; loop { - info!("45 deg"); - pwm.set_duty(0, 2500 - 156); - Timer::after(Duration::from_millis(5000)).await; - - info!("90 deg"); - pwm.set_duty(0, 2500 - 187); - Timer::after(Duration::from_millis(5000)).await; - - info!("135 deg"); - pwm.set_duty(0, 2500 - 218); - Timer::after(Duration::from_millis(5000)).await; - - info!("180 deg"); - pwm.set_duty(0, 2500 - 250); - Timer::after(Duration::from_millis(5000)).await; - - info!("0 deg"); - pwm.set_duty(0, 2500 - 125); - Timer::after(Duration::from_millis(5000)).await; + i += 1; + pwm.set_duty(0, DUTY[i % 1024]); + pwm.set_duty(1, DUTY[(i + 256) % 1024]); + pwm.set_duty(2, DUTY[(i + 512) % 1024]); + pwm.set_duty(3, DUTY[(i + 768) % 1024]); + Timer::after(Duration::from_millis(3)).await; } } diff --git a/examples/nrf/src/bin/pwm_servo.rs b/examples/nrf/src/bin/pwm_servo.rs new file mode 100644 index 000000000..aeda80be8 --- /dev/null +++ b/examples/nrf/src/bin/pwm_servo.rs @@ -0,0 +1,48 @@ +#![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, Pwm}; +use embassy_nrf::Peripherals; + +#[embassy::main] +async fn main(_spawner: Spawner, p: Peripherals) { + let pwm = Pwm::new(p.PWM0, p.P0_05, NoPin, NoPin, NoPin); + // sg90 microervo requires 50hz or 20ms period + // set_period can only set down to 125khz so we cant use it directly + // Div128 is 125khz or 0.000008s or 0.008ms, 20/0.008 is 2500 is top + pwm.set_prescaler(Prescaler::Div128); + pwm.set_max_duty(2500); + info!("pwm initialized!"); + + Timer::after(Duration::from_millis(5000)).await; + + // 1ms 0deg (1/.008=125), 1.5ms 90deg (1.5/.008=187.5), 2ms 180deg (2/.008=250), + loop { + info!("45 deg"); + pwm.set_duty(0, 2500 - 156); + Timer::after(Duration::from_millis(5000)).await; + + info!("90 deg"); + pwm.set_duty(0, 2500 - 187); + Timer::after(Duration::from_millis(5000)).await; + + info!("135 deg"); + pwm.set_duty(0, 2500 - 218); + Timer::after(Duration::from_millis(5000)).await; + + info!("180 deg"); + pwm.set_duty(0, 2500 - 250); + Timer::after(Duration::from_millis(5000)).await; + + info!("0 deg"); + pwm.set_duty(0, 2500 - 125); + Timer::after(Duration::from_millis(5000)).await; + } +} -- cgit From d961fd1015f7911dc1085035c37b0554b0184982 Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Wed, 3 Nov 2021 18:25:44 -0700 Subject: rename to SimplePwm and SequencePwm --- examples/nrf/src/bin/pwm.rs | 5 ++--- examples/nrf/src/bin/pwm_led.rs | 4 ++-- examples/nrf/src/bin/pwm_sequence.rs | 4 ++-- examples/nrf/src/bin/pwm_servo.rs | 4 ++-- examples/nrf/src/bin/pwm_simple_sin.rs | 6 ++++-- 5 files changed, 12 insertions(+), 11 deletions(-) (limited to 'examples') diff --git a/examples/nrf/src/bin/pwm.rs b/examples/nrf/src/bin/pwm.rs index 4c18b77b4..0cb91cf1e 100644 --- a/examples/nrf/src/bin/pwm.rs +++ b/examples/nrf/src/bin/pwm.rs @@ -7,7 +7,7 @@ mod example_common; use defmt::*; use embassy::executor::Spawner; use embassy::time::{Duration, Timer}; -use embassy_nrf::pwm::{Prescaler, Pwm}; +use embassy_nrf::pwm::{Prescaler, SimplePwm}; use embassy_nrf::Peripherals; // for i in range(1024): print(int((math.sin(i/512*math.pi)*0.4+0.5)**2*32767), ', ', end='') @@ -85,10 +85,9 @@ static DUTY: [u16; 1024] = [ #[embassy::main] async fn main(_spawner: Spawner, p: Peripherals) { - let pwm = Pwm::new(p.PWM0, p.P0_13, p.P0_14, p.P0_16, p.P0_15); + let pwm = SimplePwm::new(p.PWM0, p.P0_13, p.P0_14, p.P0_16, p.P0_15); pwm.set_prescaler(Prescaler::Div1); pwm.set_max_duty(32767); - pwm.set_time_stretch(32); info!("pwm initialized!"); let mut i = 0; diff --git a/examples/nrf/src/bin/pwm_led.rs b/examples/nrf/src/bin/pwm_led.rs index 067f4d2ca..ced6a7136 100644 --- a/examples/nrf/src/bin/pwm_led.rs +++ b/examples/nrf/src/bin/pwm_led.rs @@ -8,12 +8,12 @@ use defmt::*; use embassy::executor::Spawner; use embassy::time::{Duration, Timer}; use embassy_nrf::gpio::NoPin; -use embassy_nrf::pwm::{Prescaler, Pwm}; +use embassy_nrf::pwm::{Prescaler, SimplePwm}; use embassy_nrf::Peripherals; #[embassy::main] async fn main(_spawner: Spawner, p: Peripherals) { - let pwm = Pwm::new(p.PWM0, p.P0_13, NoPin, NoPin, NoPin); + let pwm = SimplePwm::new(p.PWM0, p.P0_13, NoPin, NoPin, NoPin); // set_period doesnt actually set what you give it, because it only has a // few options from the hardhware so be explicit instead // Div128 is slowest, 125khz still crazy fast for our eyes diff --git a/examples/nrf/src/bin/pwm_sequence.rs b/examples/nrf/src/bin/pwm_sequence.rs index 8830e52d6..82575d703 100644 --- a/examples/nrf/src/bin/pwm_sequence.rs +++ b/examples/nrf/src/bin/pwm_sequence.rs @@ -8,7 +8,7 @@ use defmt::*; use embassy::executor::Spawner; use embassy::time::{Duration, Timer}; use embassy_nrf::pwm::{ - CounterMode, Prescaler, PwmSeq, SequenceConfig, SequenceLoad, SequenceMode, + CounterMode, Prescaler, SequenceConfig, SequenceLoad, SequenceMode, SequencePwm, }; use embassy_nrf::Peripherals; @@ -28,7 +28,7 @@ async fn main(_spawner: Spawner, p: Peripherals) { end_delay: 0, }; - let pwm = unwrap!(PwmSeq::new( + let pwm = unwrap!(SequencePwm::new( p.PWM0, p.P0_13, p.P0_15, p.P0_16, p.P0_14, config )); let _ = pwm.start(SequenceMode::Times(5)); diff --git a/examples/nrf/src/bin/pwm_servo.rs b/examples/nrf/src/bin/pwm_servo.rs index aeda80be8..d681a2015 100644 --- a/examples/nrf/src/bin/pwm_servo.rs +++ b/examples/nrf/src/bin/pwm_servo.rs @@ -8,12 +8,12 @@ use defmt::*; use embassy::executor::Spawner; use embassy::time::{Duration, Timer}; use embassy_nrf::gpio::NoPin; -use embassy_nrf::pwm::{Prescaler, Pwm}; +use embassy_nrf::pwm::{Prescaler, SimplePwm}; use embassy_nrf::Peripherals; #[embassy::main] async fn main(_spawner: Spawner, p: Peripherals) { - let pwm = Pwm::new(p.PWM0, p.P0_05, NoPin, NoPin, NoPin); + let pwm = SimplePwm::new(p.PWM0, p.P0_05, NoPin, NoPin, NoPin); // sg90 microervo requires 50hz or 20ms period // set_period can only set down to 125khz so we cant use it directly // Div128 is 125khz or 0.000008s or 0.008ms, 20/0.008 is 2500 is top diff --git a/examples/nrf/src/bin/pwm_simple_sin.rs b/examples/nrf/src/bin/pwm_simple_sin.rs index ac3089c84..afafb5d96 100644 --- a/examples/nrf/src/bin/pwm_simple_sin.rs +++ b/examples/nrf/src/bin/pwm_simple_sin.rs @@ -10,7 +10,7 @@ use embassy::executor::Spawner; use embassy::time::{Duration, Timer}; use embassy_nrf::gpio::NoPin; use embassy_nrf::pwm::{ - CounterMode, Prescaler, PwmSeq, SequenceConfig, SequenceLoad, SequenceMode, + CounterMode, Prescaler, SequenceConfig, SequenceLoad, SequenceMode, SequencePwm, }; use embassy_nrf::Peripherals; use micromath::F32Ext; @@ -32,7 +32,9 @@ async fn main(_spawner: Spawner, p: Peripherals) { end_delay: 0, }; - let pwm = unwrap!(PwmSeq::new(p.PWM0, p.P0_13, NoPin, NoPin, NoPin, config)); + let pwm = unwrap!(SequencePwm::new( + p.PWM0, p.P0_13, NoPin, NoPin, NoPin, config + )); let _ = pwm.start(SequenceMode::Infinite); info!("pwm started!"); -- cgit From b726ef1886c65ab76b01f2c54ad559573e19083d Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Wed, 3 Nov 2021 18:37:54 -0700 Subject: make SequenceConfig struct is consistent with other Config structs, that are always non_exhaustive and have a Default --- examples/nrf/src/bin/pwm_sequence.rs | 25 ++++++++++++------------- examples/nrf/src/bin/pwm_simple_sin.rs | 24 +++++++++++------------- 2 files changed, 23 insertions(+), 26 deletions(-) (limited to 'examples') diff --git a/examples/nrf/src/bin/pwm_sequence.rs b/examples/nrf/src/bin/pwm_sequence.rs index 82575d703..3f8b051dd 100644 --- a/examples/nrf/src/bin/pwm_sequence.rs +++ b/examples/nrf/src/bin/pwm_sequence.rs @@ -7,9 +7,7 @@ mod example_common; use defmt::*; use embassy::executor::Spawner; use embassy::time::{Duration, Timer}; -use embassy_nrf::pwm::{ - CounterMode, Prescaler, SequenceConfig, SequenceLoad, SequenceMode, SequencePwm, -}; +use embassy_nrf::pwm::{Prescaler, SequenceConfig, SequenceLoad, SequenceMode, SequencePwm}; use embassy_nrf::Peripherals; #[embassy::main] @@ -18,18 +16,19 @@ async fn main(_spawner: Spawner, p: Peripherals) { 0x8000, 0, 0, 0, 0, 0x8000, 0, 0, 0, 0, 0x8000, 0, 0, 0, 0, 0x8000, ]; - let config = SequenceConfig { - counter_mode: CounterMode::Up, - top: 15625, - prescaler: Prescaler::Div128, - sequence: &seq_values, - sequence_load: SequenceLoad::Individual, - refresh: 0, - end_delay: 0, - }; + let mut config = SequenceConfig::default(); + config.top = 15625; + config.prescaler = Prescaler::Div128; + config.sequence_load = SequenceLoad::Individual; let pwm = unwrap!(SequencePwm::new( - p.PWM0, p.P0_13, p.P0_15, p.P0_16, p.P0_14, config + p.PWM0, + p.P0_13, + p.P0_15, + p.P0_16, + p.P0_14, + config, + &seq_values, )); let _ = pwm.start(SequenceMode::Times(5)); info!("pwm started!"); diff --git a/examples/nrf/src/bin/pwm_simple_sin.rs b/examples/nrf/src/bin/pwm_simple_sin.rs index afafb5d96..33fa6dcfe 100644 --- a/examples/nrf/src/bin/pwm_simple_sin.rs +++ b/examples/nrf/src/bin/pwm_simple_sin.rs @@ -9,9 +9,7 @@ use defmt::*; use embassy::executor::Spawner; use embassy::time::{Duration, Timer}; use embassy_nrf::gpio::NoPin; -use embassy_nrf::pwm::{ - CounterMode, Prescaler, SequenceConfig, SequenceLoad, SequenceMode, SequencePwm, -}; +use embassy_nrf::pwm::{CounterMode, SequenceConfig, SequenceMode, SequencePwm}; use embassy_nrf::Peripherals; use micromath::F32Ext; @@ -22,18 +20,18 @@ 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 = SequenceConfig { - counter_mode: CounterMode::UpAndDown, - top: 12000, - prescaler: Prescaler::Div16, - sequence: &seq_values, - sequence_load: SequenceLoad::Common, - refresh: 0, - end_delay: 0, - }; + let mut config = SequenceConfig::default(); + config.counter_mode = CounterMode::UpAndDown; + config.top = 12000; let pwm = unwrap!(SequencePwm::new( - p.PWM0, p.P0_13, NoPin, NoPin, NoPin, config + p.PWM0, + p.P0_13, + NoPin, + NoPin, + NoPin, + config, + &seq_values )); let _ = pwm.start(SequenceMode::Infinite); info!("pwm started!"); -- cgit From 751617c2be10c047ccf6986ace489999b80a01b0 Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Wed, 10 Nov 2021 12:48:15 -0700 Subject: fix examples for mut self set_duty --- examples/nrf/src/bin/pwm.rs | 2 +- examples/nrf/src/bin/pwm_led.rs | 2 +- examples/nrf/src/bin/pwm_servo.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) (limited to 'examples') diff --git a/examples/nrf/src/bin/pwm.rs b/examples/nrf/src/bin/pwm.rs index 0cb91cf1e..8679eddd8 100644 --- a/examples/nrf/src/bin/pwm.rs +++ b/examples/nrf/src/bin/pwm.rs @@ -85,7 +85,7 @@ static DUTY: [u16; 1024] = [ #[embassy::main] async fn main(_spawner: Spawner, p: Peripherals) { - let pwm = SimplePwm::new(p.PWM0, p.P0_13, p.P0_14, p.P0_16, p.P0_15); + let mut pwm = SimplePwm::new(p.PWM0, p.P0_13, p.P0_14, p.P0_16, p.P0_15); pwm.set_prescaler(Prescaler::Div1); pwm.set_max_duty(32767); info!("pwm initialized!"); diff --git a/examples/nrf/src/bin/pwm_led.rs b/examples/nrf/src/bin/pwm_led.rs index ced6a7136..d0b71a5cd 100644 --- a/examples/nrf/src/bin/pwm_led.rs +++ b/examples/nrf/src/bin/pwm_led.rs @@ -13,7 +13,7 @@ use embassy_nrf::Peripherals; #[embassy::main] async fn main(_spawner: Spawner, p: Peripherals) { - let pwm = SimplePwm::new(p.PWM0, p.P0_13, NoPin, NoPin, NoPin); + let mut pwm = SimplePwm::new(p.PWM0, p.P0_13, NoPin, NoPin, NoPin); // set_period doesnt actually set what you give it, because it only has a // few options from the hardhware so be explicit instead // Div128 is slowest, 125khz still crazy fast for our eyes diff --git a/examples/nrf/src/bin/pwm_servo.rs b/examples/nrf/src/bin/pwm_servo.rs index d681a2015..700b88574 100644 --- a/examples/nrf/src/bin/pwm_servo.rs +++ b/examples/nrf/src/bin/pwm_servo.rs @@ -13,7 +13,7 @@ use embassy_nrf::Peripherals; #[embassy::main] async fn main(_spawner: Spawner, p: Peripherals) { - let pwm = SimplePwm::new(p.PWM0, p.P0_05, NoPin, NoPin, NoPin); + let mut pwm = SimplePwm::new(p.PWM0, p.P0_05, NoPin, NoPin, NoPin); // sg90 microervo requires 50hz or 20ms period // set_period can only set down to 125khz so we cant use it directly // Div128 is 125khz or 0.000008s or 0.008ms, 20/0.008 is 2500 is top -- cgit From 2973ff4cf05458cb80718c35c7fe9fa690d0cf8b Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Wed, 10 Nov 2021 18:49:24 -0700 Subject: remove unstable feature and dependency, and make pwm_sequence a near mirror of pwm example --- examples/nrf/Cargo.toml | 1 - examples/nrf/src/bin/pwm_sequence.rs | 98 ++++++++++++++++++++++++++++++---- examples/nrf/src/bin/pwm_simple_sin.rs | 47 ---------------- 3 files changed, 87 insertions(+), 59 deletions(-) delete mode 100644 examples/nrf/src/bin/pwm_simple_sin.rs (limited to 'examples') diff --git a/examples/nrf/Cargo.toml b/examples/nrf/Cargo.toml index e0025b737..b89aa513f 100644 --- a/examples/nrf/Cargo.toml +++ b/examples/nrf/Cargo.toml @@ -31,4 +31,3 @@ 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_sequence.rs b/examples/nrf/src/bin/pwm_sequence.rs index 3f8b051dd..d02b0c9c5 100644 --- a/examples/nrf/src/bin/pwm_sequence.rs +++ b/examples/nrf/src/bin/pwm_sequence.rs @@ -7,30 +7,106 @@ mod example_common; use defmt::*; use embassy::executor::Spawner; use embassy::time::{Duration, Timer}; -use embassy_nrf::pwm::{Prescaler, SequenceConfig, SequenceLoad, SequenceMode, SequencePwm}; +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; 16] = [ - 0x8000, 0, 0, 0, 0, 0x8000, 0, 0, 0, 0, 0x8000, 0, 0, 0, 0, 0x8000, + // for i in range(1024): print(int((math.sin(i/512*math.pi)*0.4+0.5)**2*32767), ', ', end='') + let seq_values: [u16; 1024] = [ + 8191, 8272, 8353, 8434, 8516, 8598, 8681, 8764, 8847, 8931, 9015, 9099, 9184, 9269, 9354, + 9440, 9526, 9613, 9700, 9787, 9874, 9962, 10050, 10139, 10227, 10316, 10406, 10495, 10585, + 10675, 10766, 10857, 10948, 11039, 11131, 11223, 11315, 11407, 11500, 11592, 11685, 11779, + 11872, 11966, 12060, 12154, 12248, 12343, 12438, 12533, 12628, 12723, 12818, 12914, 13010, + 13106, 13202, 13298, 13394, 13491, 13587, 13684, 13781, 13878, 13975, 14072, 14169, 14266, + 14364, 14461, 14558, 14656, 14754, 14851, 14949, 15046, 15144, 15242, 15339, 15437, 15535, + 15632, 15730, 15828, 15925, 16023, 16120, 16218, 16315, 16412, 16510, 16607, 16704, 16801, + 16898, 16995, 17091, 17188, 17284, 17380, 17477, 17572, 17668, 17764, 17859, 17955, 18050, + 18145, 18239, 18334, 18428, 18522, 18616, 18710, 18803, 18896, 18989, 19082, 19174, 19266, + 19358, 19449, 19540, 19631, 19722, 19812, 19902, 19991, 20081, 20169, 20258, 20346, 20434, + 20521, 20608, 20695, 20781, 20867, 20952, 21037, 21122, 21206, 21290, 21373, 21456, 21538, + 21620, 21701, 21782, 21863, 21943, 22022, 22101, 22179, 22257, 22335, 22412, 22488, 22564, + 22639, 22714, 22788, 22861, 22934, 23007, 23079, 23150, 23220, 23290, 23360, 23429, 23497, + 23564, 23631, 23698, 23763, 23828, 23892, 23956, 24019, 24081, 24143, 24204, 24264, 24324, + 24383, 24441, 24499, 24555, 24611, 24667, 24721, 24775, 24828, 24881, 24933, 24983, 25034, + 25083, 25132, 25180, 25227, 25273, 25319, 25363, 25407, 25451, 25493, 25535, 25575, 25615, + 25655, 25693, 25731, 25767, 25803, 25838, 25873, 25906, 25939, 25971, 26002, 26032, 26061, + 26089, 26117, 26144, 26170, 26195, 26219, 26242, 26264, 26286, 26307, 26327, 26346, 26364, + 26381, 26397, 26413, 26427, 26441, 26454, 26466, 26477, 26487, 26496, 26505, 26512, 26519, + 26525, 26530, 26534, 26537, 26539, 26540, 26541, 26540, 26539, 26537, 26534, 26530, 26525, + 26519, 26512, 26505, 26496, 26487, 26477, 26466, 26454, 26441, 26427, 26413, 26397, 26381, + 26364, 26346, 26327, 26307, 26286, 26264, 26242, 26219, 26195, 26170, 26144, 26117, 26089, + 26061, 26032, 26002, 25971, 25939, 25906, 25873, 25838, 25803, 25767, 25731, 25693, 25655, + 25615, 25575, 25535, 25493, 25451, 25407, 25363, 25319, 25273, 25227, 25180, 25132, 25083, + 25034, 24983, 24933, 24881, 24828, 24775, 24721, 24667, 24611, 24555, 24499, 24441, 24383, + 24324, 24264, 24204, 24143, 24081, 24019, 23956, 23892, 23828, 23763, 23698, 23631, 23564, + 23497, 23429, 23360, 23290, 23220, 23150, 23079, 23007, 22934, 22861, 22788, 22714, 22639, + 22564, 22488, 22412, 22335, 22257, 22179, 22101, 22022, 21943, 21863, 21782, 21701, 21620, + 21538, 21456, 21373, 21290, 21206, 21122, 21037, 20952, 20867, 20781, 20695, 20608, 20521, + 20434, 20346, 20258, 20169, 20081, 19991, 19902, 19812, 19722, 19631, 19540, 19449, 19358, + 19266, 19174, 19082, 18989, 18896, 18803, 18710, 18616, 18522, 18428, 18334, 18239, 18145, + 18050, 17955, 17859, 17764, 17668, 17572, 17477, 17380, 17284, 17188, 17091, 16995, 16898, + 16801, 16704, 16607, 16510, 16412, 16315, 16218, 16120, 16023, 15925, 15828, 15730, 15632, + 15535, 15437, 15339, 15242, 15144, 15046, 14949, 14851, 14754, 14656, 14558, 14461, 14364, + 14266, 14169, 14072, 13975, 13878, 13781, 13684, 13587, 13491, 13394, 13298, 13202, 13106, + 13010, 12914, 12818, 12723, 12628, 12533, 12438, 12343, 12248, 12154, 12060, 11966, 11872, + 11779, 11685, 11592, 11500, 11407, 11315, 11223, 11131, 11039, 10948, 10857, 10766, 10675, + 10585, 10495, 10406, 10316, 10227, 10139, 10050, 9962, 9874, 9787, 9700, 9613, 9526, 9440, + 9354, 9269, 9184, 9099, 9015, 8931, 8847, 8764, 8681, 8598, 8516, 8434, 8353, 8272, 8191, + 8111, 8031, 7952, 7873, 7794, 7716, 7638, 7561, 7484, 7407, 7331, 7255, 7180, 7105, 7031, + 6957, 6883, 6810, 6738, 6665, 6594, 6522, 6451, 6381, 6311, 6241, 6172, 6104, 6036, 5968, + 5901, 5834, 5767, 5702, 5636, 5571, 5507, 5443, 5379, 5316, 5253, 5191, 5130, 5068, 5008, + 4947, 4888, 4828, 4769, 4711, 4653, 4596, 4539, 4482, 4426, 4371, 4316, 4261, 4207, 4153, + 4100, 4047, 3995, 3943, 3892, 3841, 3791, 3741, 3691, 3642, 3594, 3546, 3498, 3451, 3404, + 3358, 3312, 3267, 3222, 3178, 3134, 3090, 3047, 3005, 2962, 2921, 2879, 2839, 2798, 2758, + 2719, 2680, 2641, 2603, 2565, 2528, 2491, 2454, 2418, 2382, 2347, 2312, 2278, 2244, 2210, + 2177, 2144, 2112, 2080, 2048, 2017, 1986, 1956, 1926, 1896, 1867, 1838, 1810, 1781, 1754, + 1726, 1699, 1673, 1646, 1620, 1595, 1570, 1545, 1520, 1496, 1472, 1449, 1426, 1403, 1380, + 1358, 1336, 1315, 1294, 1273, 1252, 1232, 1212, 1192, 1173, 1154, 1135, 1117, 1099, 1081, + 1063, 1046, 1029, 1012, 996, 980, 964, 948, 933, 918, 903, 888, 874, 860, 846, 833, 819, + 806, 793, 781, 768, 756, 744, 733, 721, 710, 699, 688, 677, 667, 657, 647, 637, 627, 618, + 609, 599, 591, 582, 574, 565, 557, 549, 541, 534, 526, 519, 512, 505, 498, 492, 485, 479, + 473, 467, 461, 455, 450, 444, 439, 434, 429, 424, 419, 415, 410, 406, 402, 398, 394, 390, + 386, 383, 379, 376, 373, 370, 367, 364, 361, 359, 356, 354, 351, 349, 347, 345, 343, 342, + 340, 338, 337, 336, 334, 333, 332, 331, 330, 330, 329, 328, 328, 328, 327, 327, 327, 327, + 327, 328, 328, 328, 329, 330, 330, 331, 332, 333, 334, 336, 337, 338, 340, 342, 343, 345, + 347, 349, 351, 354, 356, 359, 361, 364, 367, 370, 373, 376, 379, 383, 386, 390, 394, 398, + 402, 406, 410, 415, 419, 424, 429, 434, 439, 444, 450, 455, 461, 467, 473, 479, 485, 492, + 498, 505, 512, 519, 526, 534, 541, 549, 557, 565, 574, 582, 591, 599, 609, 618, 627, 637, + 647, 657, 667, 677, 688, 699, 710, 721, 733, 744, 756, 768, 781, 793, 806, 819, 833, 846, + 860, 874, 888, 903, 918, 933, 948, 964, 980, 996, 1012, 1029, 1046, 1063, 1081, 1099, 1117, + 1135, 1154, 1173, 1192, 1212, 1232, 1252, 1273, 1294, 1315, 1336, 1358, 1380, 1403, 1426, + 1449, 1472, 1496, 1520, 1545, 1570, 1595, 1620, 1646, 1673, 1699, 1726, 1754, 1781, 1810, + 1838, 1867, 1896, 1926, 1956, 1986, 2017, 2048, 2080, 2112, 2144, 2177, 2210, 2244, 2278, + 2312, 2347, 2382, 2418, 2454, 2491, 2528, 2565, 2603, 2641, 2680, 2719, 2758, 2798, 2839, + 2879, 2921, 2962, 3005, 3047, 3090, 3134, 3178, 3222, 3267, 3312, 3358, 3404, 3451, 3498, + 3546, 3594, 3642, 3691, 3741, 3791, 3841, 3892, 3943, 3995, 4047, 4100, 4153, 4207, 4261, + 4316, 4371, 4426, 4482, 4539, 4596, 4653, 4711, 4769, 4828, 4888, 4947, 5008, 5068, 5130, + 5191, 5253, 5316, 5379, 5443, 5507, 5571, 5636, 5702, 5767, 5834, 5901, 5968, 6036, 6104, + 6172, 6241, 6311, 6381, 6451, 6522, 6594, 6665, 6738, 6810, 6883, 6957, 7031, 7105, 7180, + 7255, 7331, 7407, 7484, 7561, 7638, 7716, 7794, 7873, 7952, 8031, 8111, ]; let mut config = SequenceConfig::default(); - config.top = 15625; - config.prescaler = Prescaler::Div128; - config.sequence_load = SequenceLoad::Individual; + config.prescaler = Prescaler::Div1; + // 1 period is 32767 * 1/16mhz = 0.002047938 = 2.047938ms + config.top = 32767; + // pwm example is delaying >~3ms before updating duty cycle, our refreshes + // happen exactly at 2.047938ms so we need a delay after each value of >~1ms + // which for us is ~1-2 periods + config.refresh = 3; let pwm = unwrap!(SequencePwm::new( p.PWM0, p.P0_13, - p.P0_15, - p.P0_16, - p.P0_14, + NoPin, + NoPin, + NoPin, config, - &seq_values, + &seq_values )); - let _ = pwm.start(SequenceMode::Times(5)); + let _ = pwm.start(SequenceMode::Infinite); info!("pwm started!"); loop { diff --git a/examples/nrf/src/bin/pwm_simple_sin.rs b/examples/nrf/src/bin/pwm_simple_sin.rs deleted file mode 100644 index 33fa6dcfe..000000000 --- a/examples/nrf/src/bin/pwm_simple_sin.rs +++ /dev/null @@ -1,47 +0,0 @@ -#![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, SequenceConfig, SequenceMode, SequencePwm}; -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 mut config = SequenceConfig::default(); - config.counter_mode = CounterMode::UpAndDown; - config.top = 12000; - - let pwm = unwrap!(SequencePwm::new( - p.PWM0, - p.P0_13, - NoPin, - NoPin, - NoPin, - config, - &seq_values - )); - let _ = pwm.start(SequenceMode::Infinite); - info!("pwm started!"); - - Timer::after(Duration::from_millis(20000)).await; - - pwm.stop(); - info!("pwm stopped!"); - - loop { - Timer::after(Duration::from_millis(1000)).await; - } -} -- cgit