|
| 1 | +//! PCNT tests |
| 2 | +//! |
| 3 | +//! It's assumed GPIO2 is connected to GPIO3 |
| 4 | +
|
| 5 | +//% CHIPS: esp32 esp32c6 esp32h2 esp32s2 esp32s3 |
| 6 | + |
| 7 | +#![no_std] |
| 8 | +#![no_main] |
| 9 | + |
| 10 | +use core::ops::Deref; |
| 11 | + |
| 12 | +use defmt_rtt as _; |
| 13 | +use esp_backtrace as _; |
| 14 | +use esp_hal::{delay::Delay, gpio::GpioPin, pcnt::Pcnt}; |
| 15 | + |
| 16 | +struct Context<'d> { |
| 17 | + pcnt: Pcnt<'d>, |
| 18 | + gpio2: GpioPin<2>, |
| 19 | + gpio3: GpioPin<3>, |
| 20 | + delay: Delay, |
| 21 | +} |
| 22 | + |
| 23 | +#[cfg(test)] |
| 24 | +#[embedded_test::tests] |
| 25 | +mod tests { |
| 26 | + use esp_hal::{ |
| 27 | + clock::ClockControl, |
| 28 | + delay::Delay, |
| 29 | + gpio::{Io, Level, Output, Pull}, |
| 30 | + pcnt::{ |
| 31 | + channel, |
| 32 | + channel::{CtrlMode, EdgeMode, PcntInputConfig, PcntSource}, |
| 33 | + unit, |
| 34 | + }, |
| 35 | + peripherals::Peripherals, |
| 36 | + system::SystemControl, |
| 37 | + }; |
| 38 | + |
| 39 | + use super::*; |
| 40 | + |
| 41 | + #[init] |
| 42 | + fn init() -> Context<'static> { |
| 43 | + let peripherals = Peripherals::take(); |
| 44 | + let system = SystemControl::new(peripherals.SYSTEM); |
| 45 | + let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); |
| 46 | + |
| 47 | + let mut io = Io::new(peripherals.GPIO, peripherals.IO_MUX); |
| 48 | + |
| 49 | + Context { |
| 50 | + pcnt: Pcnt::new(peripherals.PCNT, None), |
| 51 | + gpio2: io.pins.gpio2, |
| 52 | + gpio3: io.pins.gpio3, |
| 53 | + delay: Delay::new(&clocks), |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + #[test] |
| 58 | + fn test_increment_on_pos_edge(ctx: Context<'static>) { |
| 59 | + let mut unit = ctx.pcnt.get_unit(unit::Number::Unit0); |
| 60 | + unit.configure(unit::Config { |
| 61 | + low_limit: -100, |
| 62 | + high_limit: 100, |
| 63 | + ..Default::default() |
| 64 | + }) |
| 65 | + .unwrap(); |
| 66 | + |
| 67 | + // Setup channel 0 to increment the count when gpio2 does LOW -> HIGH |
| 68 | + unit.get_channel(channel::Number::Channel0).configure( |
| 69 | + PcntSource::always_high(), |
| 70 | + PcntSource::from_pin(ctx.gpio2, PcntInputConfig { pull: Pull::Down }), |
| 71 | + channel::Config { |
| 72 | + lctrl_mode: CtrlMode::Keep, |
| 73 | + hctrl_mode: CtrlMode::Keep, |
| 74 | + pos_edge: EdgeMode::Increment, |
| 75 | + neg_edge: EdgeMode::Hold, |
| 76 | + invert_ctrl: false, |
| 77 | + invert_sig: false, |
| 78 | + }, |
| 79 | + ); |
| 80 | + |
| 81 | + // Disable channel 1. |
| 82 | + unit.get_channel(channel::Number::Channel1).configure( |
| 83 | + PcntSource::always_high(), |
| 84 | + PcntSource::always_high(), |
| 85 | + channel::Config { |
| 86 | + lctrl_mode: CtrlMode::Keep, |
| 87 | + hctrl_mode: CtrlMode::Keep, |
| 88 | + pos_edge: EdgeMode::Hold, |
| 89 | + neg_edge: EdgeMode::Hold, |
| 90 | + invert_ctrl: false, |
| 91 | + invert_sig: false, |
| 92 | + }, |
| 93 | + ); |
| 94 | + |
| 95 | + let mut output = Output::new(ctx.gpio3, Level::Low); |
| 96 | + |
| 97 | + unit.resume(); |
| 98 | + |
| 99 | + assert_eq!(0, unit.get_value()); |
| 100 | + |
| 101 | + output.set_high(); |
| 102 | + ctx.delay.delay_micros(1); |
| 103 | + |
| 104 | + assert_eq!(1, unit.get_value()); |
| 105 | + |
| 106 | + output.set_low(); |
| 107 | + ctx.delay.delay_micros(1); |
| 108 | + |
| 109 | + assert_eq!(1, unit.get_value()); |
| 110 | + |
| 111 | + output.set_high(); |
| 112 | + ctx.delay.delay_micros(1); |
| 113 | + |
| 114 | + assert_eq!(2, unit.get_value()); |
| 115 | + |
| 116 | + output.set_low(); |
| 117 | + ctx.delay.delay_micros(1); |
| 118 | + |
| 119 | + assert_eq!(2, unit.get_value()); |
| 120 | + } |
| 121 | +} |
0 commit comments