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