Created
June 21, 2020 18:05
-
-
Save rafacouto/496b4e28e2c580a3de360cfd0ccf8efe to your computer and use it in GitHub Desktop.
My first blinky with Rust embedded
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![no_std] | |
#![no_main] | |
use panic_halt as _; // you can put a breakpoint on `rust_begin_unwind` to catch panics | |
use cortex_m_rt::entry; | |
use stm32f4xx_hal as hal; | |
use crate::hal::{prelude::*, stm32}; | |
#[entry] | |
fn main() -> ! { | |
loop { | |
if let (Some(dp), Some(cp)) = ( | |
stm32::Peripherals::take(), | |
cortex_m::peripheral::Peripherals::take(), | |
) { | |
let gpiod = dp.GPIOD.split(); | |
let mut led1 = gpiod.pd13.into_push_pull_output(); | |
let mut led2 = gpiod.pd12.into_push_pull_output(); | |
let mut led3 = gpiod.pd14.into_push_pull_output(); | |
let mut led4 = gpiod.pd15.into_push_pull_output(); | |
// Set up the system clock. We want to run at 48MHz for this one. | |
let rcc = dp.RCC.constrain(); | |
let clocks = rcc.cfgr.sysclk(48.mhz()).freeze(); | |
// Create a delay abstraction based on SysTick | |
let mut delay = hal::delay::Delay::new(cp.SYST, clocks); | |
let ms_delay = 300_u32; | |
loop { | |
led1.set_high().unwrap(); | |
delay.delay_ms(ms_delay); | |
led1.set_low().unwrap(); | |
led2.set_high().unwrap(); | |
delay.delay_ms(ms_delay); | |
led2.set_low().unwrap(); | |
led3.set_high().unwrap(); | |
delay.delay_ms(ms_delay); | |
led3.set_low().unwrap(); | |
led4.set_high().unwrap(); | |
delay.delay_ms(ms_delay); | |
led4.set_low().unwrap(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment