Skip to content

Instantly share code, notes, and snippets.

@rafacouto
Created June 21, 2020 18:05
Show Gist options
  • Save rafacouto/496b4e28e2c580a3de360cfd0ccf8efe to your computer and use it in GitHub Desktop.
Save rafacouto/496b4e28e2c580a3de360cfd0ccf8efe to your computer and use it in GitHub Desktop.
My first blinky with Rust embedded
#![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