Skip to content

Instantly share code, notes, and snippets.

@leshow
Created October 9, 2019 03:33
Show Gist options
  • Save leshow/000dd109eb95f8903e2dc436f144a5b9 to your computer and use it in GitHub Desktop.
Save leshow/000dd109eb95f8903e2dc436f144a5b9 to your computer and use it in GitHub Desktop.
#![no_std]
#![no_main]
// pick a panicking behavior
extern crate panic_halt; // you can put a breakpoint on `rust_begin_unwind` to catch panics
// extern crate panic_abort; // requires nightly
// extern crate panic_itm; // logs messages over ITM; requires ITM support
// extern crate panic_semihosting; // logs messages to the host stderr; requires a debugger
use cortex_m_rt::{entry, exception, ExceptionFrame};
use cortex_m_semihosting::{hio, hprintln};
use hal::{
delay::Delay,
gpio::{self, gpioe},
hal::digital::v2::*,
prelude::*,
stm32::{self, Peripherals},
};
use stm32f3xx_hal as hal;
use core::fmt::Write;
#[entry]
fn main() -> ! {
let cp = cortex_m::Peripherals::take().unwrap();
let peripherals = stm32::Peripherals::take().unwrap();
let mut flash = peripherals.FLASH.constrain();
let mut rcc = peripherals.RCC.constrain();
let clocks = rcc.cfgr.freeze(&mut flash.acr);
let mut delay = Delay::new(cp.SYST, clocks);
let mut gpioa = peripherals.GPIOA.split(&mut rcc.ahb);
let btn = gpioa
.pa3
.into_pull_up_input(&mut gpioa.moder, &mut gpioa.pupdr);
let mut gpioe = peripherals.GPIOE.split(&mut rcc.ahb);
let mut light = gpioe
.pe9
.into_push_pull_output(&mut gpioe.moder, &mut gpioe.otyper);
loop {
if btn.is_high().unwrap() {
light.set_high().unwrap();
} else if btn.is_low().unwrap() {
light.set_low().unwrap();
}
delay.delay_ms(1_00_u16);
}
}
#[exception]
fn HardFault(ef: &ExceptionFrame) -> ! {
if let Ok(mut hstdout) = hio::hstdout() {
writeln!(hstdout, "{:#?}", ef).ok();
}
loop {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment