dynamic_gpio.rs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #![deny(unsafe_code)]
  2. #![no_std]
  3. #![no_main]
  4. use panic_halt as _;
  5. use nb::block;
  6. use cortex_m_rt::entry;
  7. use cortex_m_semihosting::hprintln;
  8. use embedded_hal::digital::v2::{InputPin, OutputPin};
  9. use stm32f1xx_hal::{pac, prelude::*, timer::Timer};
  10. #[entry]
  11. fn main() -> ! {
  12. // Get access to the core peripherals from the cortex-m crate
  13. let cp = cortex_m::Peripherals::take().unwrap();
  14. // Get access to the device specific peripherals from the peripheral access crate
  15. let dp = pac::Peripherals::take().unwrap();
  16. // Take ownership over the raw flash and rcc devices and convert them into the corresponding
  17. // HAL structs
  18. let mut flash = dp.FLASH.constrain();
  19. let mut rcc = dp.RCC.constrain();
  20. // Freeze the configuration of all the clocks in the system and store the frozen frequencies in
  21. // `clocks`
  22. let clocks = rcc.cfgr.freeze(&mut flash.acr);
  23. // Acquire the GPIOC peripheral
  24. let mut gpioc = dp.GPIOC.split(&mut rcc.apb2);
  25. let mut pin = gpioc.pc13.into_dynamic(&mut gpioc.crh);
  26. // Configure the syst timer to trigger an update every second
  27. let mut timer = Timer::syst(cp.SYST, &clocks).start_count_down(1.hz());
  28. // Wait for the timer to trigger an update and change the state of the LED
  29. loop {
  30. pin.make_floating_input(&mut gpioc.crh);
  31. block!(timer.wait()).unwrap();
  32. hprintln!("{}", pin.is_high().unwrap()).unwrap();
  33. pin.make_push_pull_output(&mut gpioc.crh);
  34. pin.set_high().unwrap();
  35. block!(timer.wait()).unwrap();
  36. pin.set_low().unwrap();
  37. block!(timer.wait()).unwrap();
  38. }
  39. }