|
| 1 | +// Required connections: |
| 2 | +// |
| 3 | +// - P0.28 <-> P0.29 |
| 4 | + |
| 5 | +#![deny(warnings)] |
| 6 | +#![no_std] |
| 7 | +#![no_main] |
| 8 | + |
| 9 | +use defmt_rtt as _; |
| 10 | +use nrf52840_hal as _; |
| 11 | +use panic_probe as _; |
| 12 | + |
| 13 | +use nrf52840_hal::gpio::{Floating, Input, Pin}; |
| 14 | + |
| 15 | +struct State { |
| 16 | + input_pin: Pin<Input<Floating>>, |
| 17 | + puller_pin: Option<Pin<Input<Floating>>>, |
| 18 | +} |
| 19 | + |
| 20 | +#[defmt_test::tests] |
| 21 | +mod tests { |
| 22 | + use cortex_m::asm; |
| 23 | + use defmt::{assert, unwrap}; |
| 24 | + use nrf52840_hal::{gpio::p0, pac, prelude::*}; |
| 25 | + |
| 26 | + use super::State; |
| 27 | + |
| 28 | + #[init] |
| 29 | + fn init() -> State { |
| 30 | + let p = unwrap!(pac::Peripherals::take()); |
| 31 | + let port0 = p0::Parts::new(p.P0); |
| 32 | + |
| 33 | + let input_pin = port0.p0_28.into_floating_input().degrade(); |
| 34 | + let puller_pin = Some(port0.p0_29.into_floating_input().degrade()); |
| 35 | + |
| 36 | + State { |
| 37 | + input_pin, |
| 38 | + puller_pin, |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + #[test] |
| 43 | + fn pulldown_is_low(state: &mut State) { |
| 44 | + let puller_pin = unwrap!(state.puller_pin.take()); |
| 45 | + |
| 46 | + let pulldown_pin = puller_pin.into_pulldown_input(); |
| 47 | + // GPIO re-configuration is not instantaneous so a delay is needed |
| 48 | + asm::delay(100); |
| 49 | + assert!(pulldown_pin.is_low().unwrap()); |
| 50 | + |
| 51 | + state.puller_pin = Some(pulldown_pin.into_floating_input()); |
| 52 | + } |
| 53 | + |
| 54 | + #[test] |
| 55 | + fn pulldown_drives_low(state: &mut State) { |
| 56 | + let puller_pin = unwrap!(state.puller_pin.take()); |
| 57 | + |
| 58 | + let pulldown_pin = puller_pin.into_pulldown_input(); |
| 59 | + assert!(state.input_pin.is_low().unwrap()); |
| 60 | + |
| 61 | + state.puller_pin = Some(pulldown_pin.into_floating_input()); |
| 62 | + } |
| 63 | + |
| 64 | + #[test] |
| 65 | + fn pullup_is_high(state: &mut State) { |
| 66 | + let puller_pin = unwrap!(state.puller_pin.take()); |
| 67 | + |
| 68 | + let pullup_pin = puller_pin.into_pullup_input(); |
| 69 | + // GPIO re-configuration is not instantaneous so a delay is needed |
| 70 | + asm::delay(100); |
| 71 | + assert!(pullup_pin.is_high().unwrap()); |
| 72 | + |
| 73 | + state.puller_pin = Some(pullup_pin.into_floating_input()); |
| 74 | + } |
| 75 | + |
| 76 | + #[test] |
| 77 | + fn pullup_drives_high(state: &mut State) { |
| 78 | + let puller_pin = unwrap!(state.puller_pin.take()); |
| 79 | + |
| 80 | + let pullup_pin = puller_pin.into_pullup_input(); |
| 81 | + // GPIO re-configuration is not instantaneous so a delay is needed |
| 82 | + asm::delay(100); |
| 83 | + assert!(state.input_pin.is_high().unwrap()); |
| 84 | + |
| 85 | + state.puller_pin = Some(pullup_pin.into_floating_input()); |
| 86 | + } |
| 87 | +} |
0 commit comments