|
| 1 | +#![deny(unsafe_code)] |
| 2 | +#![no_main] |
| 3 | +#![no_std] |
| 4 | + |
| 5 | +// Halt on panic |
| 6 | +#[allow(unused_extern_crates)] // NOTE(allow) bug rust-lang/rust#53964 |
| 7 | +extern crate panic_halt; // panic handler |
| 8 | + |
| 9 | +use cortex_m; |
| 10 | +use cortex_m_rt::entry; |
| 11 | +use stm32f4xx_hal as hal; |
| 12 | + |
| 13 | +use crate::hal::{prelude::*, stm32, systick::SysTickTime}; |
| 14 | + |
| 15 | +#[entry] |
| 16 | +fn main() -> ! { |
| 17 | + if let (Some(dp), Some(cp)) = ( |
| 18 | + stm32::Peripherals::take(), |
| 19 | + cortex_m::peripheral::Peripherals::take(), |
| 20 | + ) { |
| 21 | + // Set up the LEDs. On the STM32F429I-DISC[O1] they are connected to pin PG13/14. |
| 22 | + let gpiog = dp.GPIOG.split(); |
| 23 | + let mut led1 = gpiog.pg13.into_push_pull_output(); |
| 24 | + let mut led2 = gpiog.pg14.into_push_pull_output(); |
| 25 | + |
| 26 | + // Set up the system clock. We want to run at 48MHz for this one. |
| 27 | + let rcc = dp.RCC.constrain(); |
| 28 | + let clocks = rcc.cfgr.sysclk(48.mhz()).freeze(); |
| 29 | + |
| 30 | + // Create a delay abstraction based on SysTick |
| 31 | + let mut systime = cp.SYST.to_systemtime(1000.hz(), clocks); |
| 32 | + |
| 33 | + loop { |
| 34 | + // On for 1s, off for 1s. |
| 35 | + led1.set_high().unwrap(); |
| 36 | + led2.set_low().unwrap(); |
| 37 | + systime.delay_ms(1000_u32); |
| 38 | + led1.set_low().unwrap(); |
| 39 | + led2.set_high().unwrap(); |
| 40 | + systime.delay_ms(1000_u32); |
| 41 | + // Also you can get the current time |
| 42 | + let _t = systime.s(); // in seconds as double |
| 43 | + let _t = systime.ms(); // in milliseconds as u64 |
| 44 | + let _t = systime.us(); // in microseconds as u64 |
| 45 | + let _t = systime.ns(); // in nanoseconds as u64 |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + loop {} |
| 50 | +} |
0 commit comments