|
| 1 | +#![no_std] |
| 2 | +#![no_main] |
| 3 | + |
| 4 | +use defmt_rtt as _; |
| 5 | +use panic_probe as _; |
| 6 | + |
| 7 | +use stm32f3xx_hal as hal; |
| 8 | + |
| 9 | +use hal::gpio::gpioc; |
| 10 | +use hal::gpio::Resistor; |
| 11 | +use hal::gpio::{Input, PXx}; |
| 12 | +use hal::{pac, prelude::*}; |
| 13 | + |
| 14 | +struct State { |
| 15 | + observer: PXx<Input>, |
| 16 | + puller: gpioc::PC1<Input>, |
| 17 | + pupdr: gpioc::PUPDR, |
| 18 | +} |
| 19 | + |
| 20 | +#[defmt_test::tests] |
| 21 | +mod tests { |
| 22 | + use super::*; |
| 23 | + use defmt::{assert, unwrap}; |
| 24 | + |
| 25 | + // Test the defaults with no configuration |
| 26 | + #[init] |
| 27 | + fn init() -> super::State { |
| 28 | + let dp = unwrap!(pac::Peripherals::take()); |
| 29 | + |
| 30 | + let mut rcc = dp.RCC.constrain(); |
| 31 | + let mut gpioc = dp.GPIOC.split(&mut rcc.ahb); |
| 32 | + |
| 33 | + let mut observer = gpioc |
| 34 | + .pc0 |
| 35 | + .into_floating_input(&mut gpioc.moder, &mut gpioc.pupdr); |
| 36 | + observer.set_internal_resistor(&mut gpioc.pupdr, Resistor::Floating); |
| 37 | + |
| 38 | + let observer = observer.downgrade().downgrade(); |
| 39 | + |
| 40 | + let puller = gpioc |
| 41 | + .pc1 |
| 42 | + .into_floating_input(&mut gpioc.moder, &mut gpioc.pupdr); |
| 43 | + |
| 44 | + let pupdr = gpioc.pupdr; |
| 45 | + |
| 46 | + super::State { |
| 47 | + observer, |
| 48 | + puller, |
| 49 | + pupdr, |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + #[test] |
| 54 | + fn pulldown_is_low(state: &mut super::State) { |
| 55 | + state |
| 56 | + .puller |
| 57 | + .set_internal_resistor(&mut state.pupdr, Resistor::PullDown); |
| 58 | + assert!(unwrap!(state.puller.is_low())); |
| 59 | + assert!(unwrap!(state.observer.is_low())); |
| 60 | + } |
| 61 | + |
| 62 | + #[test] |
| 63 | + fn set_high_is_high(state: &mut super::State) { |
| 64 | + state |
| 65 | + .puller |
| 66 | + .set_internal_resistor(&mut state.pupdr, Resistor::PullUp); |
| 67 | + assert!(unwrap!(state.puller.is_high())); |
| 68 | + assert!(unwrap!(state.observer.is_high())); |
| 69 | + } |
| 70 | +} |
0 commit comments