diff --git a/Cargo.toml b/Cargo.toml index 793a99c..a6a0c5d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,25 +6,24 @@ name = "STM32F1Test" version = "0.1.0" [dependencies] -cortex-m = "0.5.8" -cortex-m-rt = "0.6.5" +cortex-m = "0.6.0" +cortex-m-rt = "0.6.8" cortex-m-semihosting = "0.3.2" panic-halt = "0.2.0" +nb = "0.1.2" [dependencies.stm32f1] -version = "0.7.0" +version = "0.8.0" features = ["stm32f103"] -# Uncomment for the panic example. -# panic-itm = "0.4.0" +[dependencies.stm32f1xx-hal] +version = "0.4.0" +features = ["stm32f103", "rt"] -# Uncomment for the allocator example. -# alloc-cortex-m = "0.3.5" +[dependencies.embedded-hal] +version = "0.2.3" +features = ["unproven"] -# Uncomment for the device example. -# [dependencies.stm32f30x] -# features = ["rt"] -# version = "0.7.1" # this lets you use `cargo fix`! [[bin]] diff --git a/openocd.sh b/openocd.sh new file mode 100755 index 0000000..7d889ae --- /dev/null +++ b/openocd.sh @@ -0,0 +1,2 @@ +#!/bin/bash +openocd -f interface/stlink-v2.cfg -f target/stm32f1x.cfg diff --git a/src/main.rs b/src/main.rs index 64731f2..44c3c72 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,61 +1,56 @@ +//! Blinks an LED +//! +//! This assumes that a LED is connected to pc13 as is the case on the blue pill board. +//! +//! Note: Without additional hardware, PC13 should not be used to drive an LED, see page 5.1.2 of +//! the reference manaual for an explanation. This is not an issue on the blue pill. + +#![deny(unsafe_code)] #![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 -use cortex_m_semihosting::hprintln; // logs messages to the host; requires a debugger +use panic_halt as _; -use cortex_m::asm; +use nb::block; + +use stm32f1xx_hal::{ + prelude::*, + pac, + timer::Timer, +}; use cortex_m_rt::entry; -use stm32f1::stm32f103; +use embedded_hal::digital::v2::OutputPin; #[entry] fn main() -> ! { - hprintln!("Hello, world!").unwrap(); + // Get access to the core peripherals from the cortex-m crate + let cp = cortex_m::Peripherals::take().unwrap(); + // Get access to the device specific peripherals from the peripheral access crate + let dp = pac::Peripherals::take().unwrap(); - let peripherals = stm32f103::Peripherals::take().unwrap(); + // Take ownership over the raw flash and rcc devices and convert them into the corresponding + // HAL structs + let mut flash = dp.FLASH.constrain(); + let mut rcc = dp.RCC.constrain(); - let gpioc = &peripherals.GPIOC; - let rcc = &peripherals.RCC; - let flash = &peripherals.FLASH; + // Freeze the configuration of all the clocks in the system and store the frozen frequencies in + // `clocks` + let clocks = rcc.cfgr.freeze(&mut flash.acr); - flash.acr.write(|w| unsafe { w.latency().bits(1)}); + // Acquire the GPIOC peripheral + let mut gpioc = dp.GPIOC.split(&mut rcc.apb2); - rcc.cr.write(|w| w.hseon().set_bit()); - while rcc.cr.read().hserdy().is_not_ready() {}; + // Configure gpio C pin 13 as a push-pull output. The `crh` register is passed to the function + // in order to configure the port. For pins 0-7, crl should be passed instead. + let mut led = gpioc.pc13.into_push_pull_output(&mut gpioc.crh); + // Configure the syst timer to trigger an update every second + let mut timer = Timer::syst(cp.SYST, 1.hz(), clocks); - hprintln!("HSE Ready.").unwrap(); - - rcc.cfgr.write(|w| { - w.pllmul().mul12() - .pllsrc().hse_div_prediv() - .pllxtpre().div2() - .ppre2().div2() - }); - - rcc.cr.modify(|_, w| w.pllon().on()); - while rcc.cr.read().pllrdy().is_not_ready() {}; - - rcc.cfgr.modify(|_, w| { w.sw().pll() }); - - - hprintln!("PLL running.").unwrap(); - - - // enable the GPIO clock for IO port C - rcc.apb2enr.write(|w| w.iopcen().set_bit()); - gpioc.crh.write(|w| { - w.mode13().bits(3) - .cnf13().bits(0b00) - }); - - loop{ - gpioc.bsrr.write(|w| w.bs13().set_bit()); - cortex_m::asm::delay(500_000); - gpioc.brr.write(|w| w.br13().set_bit()); - cortex_m::asm::delay(500_000); - //hprintln!("Blink").unwrap(); + // Wait for the timer to trigger an update and change the state of the LED + loop { + block!(timer.wait()).unwrap(); + led.set_high().unwrap(); + block!(timer.wait()).unwrap(); + led.set_low().unwrap(); } }