|
| 1 | +#![no_std] |
| 2 | +#![no_main] |
| 3 | + |
| 4 | +#[path = "../examples/utilities/mod.rs"] |
| 5 | +mod utils; |
| 6 | + |
| 7 | +mod common; |
| 8 | + |
| 9 | +use fugit::HertzU32; |
| 10 | +use hal::stm32; |
| 11 | +use stm32h5xx_hal::{self as hal, gpio}; |
| 12 | + |
| 13 | +pub const F_SYS: HertzU32 = HertzU32::MHz(16); |
| 14 | +pub const CYCLES_PER_US: u32 = F_SYS.raw() / 1_000_000; |
| 15 | + |
| 16 | +use crate::common::is_pax_low; |
| 17 | +use embedded_hal::delay::DelayNs; |
| 18 | +use fugit::RateExtU32; |
| 19 | +use stm32h5xx_hal::{ |
| 20 | + delay::Delay, gpio::GpioExt, pwr::PwrExt, rcc::RccExt, stm32::GPIOA, |
| 21 | +}; |
| 22 | + |
| 23 | +#[embedded_test::tests] |
| 24 | +mod tests { |
| 25 | + #[test] |
| 26 | + fn gpio_push_pull() { |
| 27 | + use super::*; |
| 28 | + |
| 29 | + let (gpioa, mut delay) = init(); |
| 30 | + |
| 31 | + let _pa1_important_dont_use_as_output = gpioa.pa1.into_floating_input(); |
| 32 | + let mut pin = gpioa.pa8.into_push_pull_output(); |
| 33 | + let pin_num = 8; // PA8 |
| 34 | + |
| 35 | + pin.set_high(); |
| 36 | + delay.delay_ms(1); // Give the pin plenty of time to go high |
| 37 | + assert!(!is_pax_low(pin_num)); |
| 38 | + |
| 39 | + pin.set_low(); |
| 40 | + delay.delay_ms(1); // Give the pin plenty of time to go low |
| 41 | + assert!(is_pax_low(pin_num)); |
| 42 | + } |
| 43 | + |
| 44 | + #[test] |
| 45 | + fn gpio_open_drain() { |
| 46 | + use super::*; |
| 47 | + |
| 48 | + let (gpioa, mut delay) = init(); |
| 49 | + let _pa1_important_dont_use_as_output = gpioa.pa1.into_floating_input(); |
| 50 | + let mut pin = gpioa.pa8.into_open_drain_output(); |
| 51 | + let pin_num = 8; // PA8 |
| 52 | + |
| 53 | + // Enable pull-up resistor |
| 54 | + { |
| 55 | + let gpioa = unsafe { &*GPIOA::PTR }; |
| 56 | + gpioa.pupdr().modify(|_, w| w.pupd(pin_num).pull_up()); |
| 57 | + } |
| 58 | + |
| 59 | + pin.set_high(); |
| 60 | + delay.delay_ms(1); // Give the pin plenty of time to go high |
| 61 | + assert!(pin.is_high()); |
| 62 | + assert!(!is_pax_low(pin_num)); |
| 63 | + |
| 64 | + pin.set_low(); |
| 65 | + delay.delay_ms(1); // Give the pin plenty of time to go low |
| 66 | + assert!(pin.is_low()); |
| 67 | + assert!(is_pax_low(pin_num)); |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +fn init() -> (gpio::gpioa::Parts, Delay) { |
| 72 | + utils::logger::init(); |
| 73 | + let cp = stm32::CorePeripherals::take().unwrap(); |
| 74 | + let dp = stm32::Peripherals::take().unwrap(); |
| 75 | + let pwr = dp.PWR.constrain(); |
| 76 | + let pwrcfg = pwr.vos0().freeze(); |
| 77 | + |
| 78 | + // Constrain and Freeze clock |
| 79 | + let rcc = dp.RCC.constrain(); |
| 80 | + let ccdr = rcc.sys_ck(250u32.MHz()).freeze(pwrcfg, &dp.SBS); |
| 81 | + |
| 82 | + let delay = Delay::new(cp.SYST, &ccdr.clocks); |
| 83 | + let gpioa = dp.GPIOA.split(ccdr.peripheral.GPIOA); |
| 84 | + |
| 85 | + (gpioa, delay) |
| 86 | +} |
0 commit comments