Skip to content

Instantly share code, notes, and snippets.

@lupyuen
Last active July 9, 2018 07:32
Show Gist options
  • Save lupyuen/47efc6c04c026cc3a11e09292adfbea0 to your computer and use it in GitHub Desktop.
Save lupyuen/47efc6c04c026cc3a11e09292adfbea0 to your computer and use it in GitHub Desktop.
fn main() -> ! {
// Show "Hello, world!" on the debug console, which is shown in OpenOCD. "mut" means that this object is mutable, i.e. it can change.
let mut debug_out = hio::hstdout().unwrap();
writeln!(debug_out, "Hello, world!").unwrap();
// Get peripherals (clocks, flash memory, GPIO) for the STM32 Blue Pill microcontroller.
let bluepill = Peripherals::take().unwrap();
// Get the clocks from the STM32 Reset and Clock Control (RCC) and freeze the Flash Access Control Register (ACR).
let mut rcc = bluepill.RCC.constrain();
let mut flash = bluepill.FLASH.constrain();
let clocks = rcc.cfgr.freeze(&mut flash.acr);
// Get GPIO Port C, which also enables the Advanced Peripheral Bus 2 (APB2) clock for Port C.
let mut gpioc = bluepill.GPIOC.split(&mut rcc.apb2);
// Use Pin PC 13 of the Blue Pill for GPIO Port C. Select Output Push/Pull mode for the pin, which is connected to our LED.
let mut led = gpioc.pc13.into_push_pull_output(&mut gpioc.crh);
// Create a delay timer from the RCC clocks.
let cp = cortex_m::Peripherals::take().unwrap();
let mut delay = Delay::new(cp.SYST, clocks);
// Loop forever.
loop {
// Output 3.3V on the LED Pin and show a message in OpenOCD console.
led.set_high();
writeln!(debug_out, "LED is ON!").unwrap();
// Wait 1,000 millisec (1 sec).
delay.delay_ms(1_000_u16);
// Output 0V on the LED Pin and show a message in OpenOCD console.
led.set_low();
writeln!(debug_out, "LED is OFF!").unwrap();
// Wait 1,000 millisec (1 sec).
delay.delay_ms(1_000_u16);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment