diff options
| author | pennae <[email protected]> | 2023-04-21 00:57:28 +0200 |
|---|---|---|
| committer | pennae <[email protected]> | 2023-04-23 22:49:15 +0200 |
| commit | a4866ad2782b5f66ed1ea67620d4117b0d474ab5 (patch) | |
| tree | 0f1870bcc1c33e4b0476d45a465442b3afad94e0 /tests | |
| parent | 54fe50c685a37c7edaf7bd0fcd2d473109d1374d (diff) | |
rp: add PWM api
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/rp/src/bin/pwm.rs | 142 |
1 files changed, 142 insertions, 0 deletions
diff --git a/tests/rp/src/bin/pwm.rs b/tests/rp/src/bin/pwm.rs new file mode 100644 index 000000000..b8cbe74c8 --- /dev/null +++ b/tests/rp/src/bin/pwm.rs | |||
| @@ -0,0 +1,142 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | use defmt::{assert, assert_eq, assert_ne, *}; | ||
| 6 | use embassy_executor::Spawner; | ||
| 7 | use embassy_rp::gpio::{Input, Level, Output, Pull}; | ||
| 8 | use embassy_rp::pwm::{Config, InputMode, Pwm}; | ||
| 9 | use embassy_time::{Duration, Timer}; | ||
| 10 | use {defmt_rtt as _, panic_probe as _}; | ||
| 11 | |||
| 12 | #[embassy_executor::main] | ||
| 13 | async fn main(_spawner: Spawner) { | ||
| 14 | let mut p = embassy_rp::init(Default::default()); | ||
| 15 | info!("Hello World!"); | ||
| 16 | |||
| 17 | // Connections on CI device: 6 -> 9, 7 -> 11 | ||
| 18 | let (mut p6, mut p7, mut p9, mut p11) = (p.PIN_6, p.PIN_7, p.PIN_9, p.PIN_11); | ||
| 19 | |||
| 20 | let cfg = { | ||
| 21 | let mut c = Config::default(); | ||
| 22 | c.divider = 125.into(); | ||
| 23 | c.top = 10000; | ||
| 24 | c.compare_a = 5000; | ||
| 25 | c.compare_b = 5000; | ||
| 26 | c | ||
| 27 | }; | ||
| 28 | |||
| 29 | // Test free-running clock | ||
| 30 | { | ||
| 31 | let pwm = Pwm::new_free(&mut p.PWM_CH3, cfg.clone()); | ||
| 32 | cortex_m::asm::delay(125); | ||
| 33 | let ctr = pwm.counter(); | ||
| 34 | assert!(ctr > 0); | ||
| 35 | assert!(ctr < 100); | ||
| 36 | cortex_m::asm::delay(125); | ||
| 37 | assert!(ctr < pwm.counter()); | ||
| 38 | } | ||
| 39 | |||
| 40 | for invert_a in [false, true] { | ||
| 41 | info!("free-running, invert A: {}", invert_a); | ||
| 42 | let mut cfg = cfg.clone(); | ||
| 43 | cfg.invert_a = invert_a; | ||
| 44 | cfg.invert_b = !invert_a; | ||
| 45 | |||
| 46 | // Test output from A | ||
| 47 | { | ||
| 48 | let pin1 = Input::new(&mut p9, Pull::None); | ||
| 49 | let _pwm = Pwm::new_output_a(&mut p.PWM_CH3, &mut p6, cfg.clone()); | ||
| 50 | Timer::after(Duration::from_millis(1)).await; | ||
| 51 | assert_eq!(pin1.is_low(), invert_a); | ||
| 52 | Timer::after(Duration::from_millis(5)).await; | ||
| 53 | assert_eq!(pin1.is_high(), invert_a); | ||
| 54 | Timer::after(Duration::from_millis(5)).await; | ||
| 55 | assert_eq!(pin1.is_low(), invert_a); | ||
| 56 | Timer::after(Duration::from_millis(5)).await; | ||
| 57 | assert_eq!(pin1.is_high(), invert_a); | ||
| 58 | } | ||
| 59 | |||
| 60 | // Test output from B | ||
| 61 | { | ||
| 62 | let pin2 = Input::new(&mut p11, Pull::None); | ||
| 63 | let _pwm = Pwm::new_output_b(&mut p.PWM_CH3, &mut p7, cfg.clone()); | ||
| 64 | Timer::after(Duration::from_millis(1)).await; | ||
| 65 | assert_ne!(pin2.is_low(), invert_a); | ||
| 66 | Timer::after(Duration::from_millis(5)).await; | ||
| 67 | assert_ne!(pin2.is_high(), invert_a); | ||
| 68 | Timer::after(Duration::from_millis(5)).await; | ||
| 69 | assert_ne!(pin2.is_low(), invert_a); | ||
| 70 | Timer::after(Duration::from_millis(5)).await; | ||
| 71 | assert_ne!(pin2.is_high(), invert_a); | ||
| 72 | } | ||
| 73 | |||
| 74 | // Test output from A+B | ||
| 75 | { | ||
| 76 | let pin1 = Input::new(&mut p9, Pull::None); | ||
| 77 | let pin2 = Input::new(&mut p11, Pull::None); | ||
| 78 | let _pwm = Pwm::new_output_ab(&mut p.PWM_CH3, &mut p6, &mut p7, cfg.clone()); | ||
| 79 | Timer::after(Duration::from_millis(1)).await; | ||
| 80 | assert_eq!(pin1.is_low(), invert_a); | ||
| 81 | assert_ne!(pin2.is_low(), invert_a); | ||
| 82 | Timer::after(Duration::from_millis(5)).await; | ||
| 83 | assert_eq!(pin1.is_high(), invert_a); | ||
| 84 | assert_ne!(pin2.is_high(), invert_a); | ||
| 85 | Timer::after(Duration::from_millis(5)).await; | ||
| 86 | assert_eq!(pin1.is_low(), invert_a); | ||
| 87 | assert_ne!(pin2.is_low(), invert_a); | ||
| 88 | Timer::after(Duration::from_millis(5)).await; | ||
| 89 | assert_eq!(pin1.is_high(), invert_a); | ||
| 90 | assert_ne!(pin2.is_high(), invert_a); | ||
| 91 | } | ||
| 92 | } | ||
| 93 | |||
| 94 | // Test level-gated | ||
| 95 | { | ||
| 96 | let mut pin2 = Output::new(&mut p11, Level::Low); | ||
| 97 | let pwm = Pwm::new_input(&mut p.PWM_CH3, &mut p7, InputMode::Level, cfg.clone()); | ||
| 98 | assert_eq!(pwm.counter(), 0); | ||
| 99 | Timer::after(Duration::from_millis(5)).await; | ||
| 100 | assert_eq!(pwm.counter(), 0); | ||
| 101 | pin2.set_high(); | ||
| 102 | Timer::after(Duration::from_millis(1)).await; | ||
| 103 | pin2.set_low(); | ||
| 104 | let ctr = pwm.counter(); | ||
| 105 | assert!(ctr >= 1000); | ||
| 106 | Timer::after(Duration::from_millis(1)).await; | ||
| 107 | assert_eq!(pwm.counter(), ctr); | ||
| 108 | } | ||
| 109 | |||
| 110 | // Test rising-gated | ||
| 111 | { | ||
| 112 | let mut pin2 = Output::new(&mut p11, Level::Low); | ||
| 113 | let pwm = Pwm::new_input(&mut p.PWM_CH3, &mut p7, InputMode::RisingEdge, cfg.clone()); | ||
| 114 | assert_eq!(pwm.counter(), 0); | ||
| 115 | Timer::after(Duration::from_millis(5)).await; | ||
| 116 | assert_eq!(pwm.counter(), 0); | ||
| 117 | pin2.set_high(); | ||
| 118 | Timer::after(Duration::from_millis(1)).await; | ||
| 119 | pin2.set_low(); | ||
| 120 | assert_eq!(pwm.counter(), 1); | ||
| 121 | Timer::after(Duration::from_millis(1)).await; | ||
| 122 | assert_eq!(pwm.counter(), 1); | ||
| 123 | } | ||
| 124 | |||
| 125 | // Test falling-gated | ||
| 126 | { | ||
| 127 | let mut pin2 = Output::new(&mut p11, Level::High); | ||
| 128 | let pwm = Pwm::new_input(&mut p.PWM_CH3, &mut p7, InputMode::FallingEdge, cfg.clone()); | ||
| 129 | assert_eq!(pwm.counter(), 0); | ||
| 130 | Timer::after(Duration::from_millis(5)).await; | ||
| 131 | assert_eq!(pwm.counter(), 0); | ||
| 132 | pin2.set_low(); | ||
| 133 | Timer::after(Duration::from_millis(1)).await; | ||
| 134 | pin2.set_high(); | ||
| 135 | assert_eq!(pwm.counter(), 1); | ||
| 136 | Timer::after(Duration::from_millis(1)).await; | ||
| 137 | assert_eq!(pwm.counter(), 1); | ||
| 138 | } | ||
| 139 | |||
| 140 | info!("Test OK"); | ||
| 141 | cortex_m::asm::bkpt(); | ||
| 142 | } | ||
