multi_mode_gpio.rs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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::{gpio::State, 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_floating_input(&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. block!(timer.wait()).unwrap();
  31. hprintln!("{}", pin.is_high().unwrap()).unwrap();
  32. pin.as_push_pull_output(&mut gpioc.crh, |out| {
  33. out.set_high().unwrap();
  34. block!(timer.wait()).unwrap();
  35. out.set_low().unwrap();
  36. block!(timer.wait()).unwrap();
  37. });
  38. pin.as_push_pull_output_with_state(&mut gpioc.crh, State::High, |out| {
  39. block!(timer.wait()).unwrap();
  40. out.set_low().unwrap();
  41. block!(timer.wait()).unwrap();
  42. });
  43. }
  44. }