Switched to embedded hal
This commit is contained in:
parent
d92e379722
commit
9f55b6dc08
21
Cargo.toml
21
Cargo.toml
|
@ -6,25 +6,24 @@ name = "STM32F1Test"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
cortex-m = "0.5.8"
|
cortex-m = "0.6.0"
|
||||||
cortex-m-rt = "0.6.5"
|
cortex-m-rt = "0.6.8"
|
||||||
cortex-m-semihosting = "0.3.2"
|
cortex-m-semihosting = "0.3.2"
|
||||||
panic-halt = "0.2.0"
|
panic-halt = "0.2.0"
|
||||||
|
nb = "0.1.2"
|
||||||
|
|
||||||
[dependencies.stm32f1]
|
[dependencies.stm32f1]
|
||||||
version = "0.7.0"
|
version = "0.8.0"
|
||||||
features = ["stm32f103"]
|
features = ["stm32f103"]
|
||||||
|
|
||||||
# Uncomment for the panic example.
|
[dependencies.stm32f1xx-hal]
|
||||||
# panic-itm = "0.4.0"
|
version = "0.4.0"
|
||||||
|
features = ["stm32f103", "rt"]
|
||||||
|
|
||||||
# Uncomment for the allocator example.
|
[dependencies.embedded-hal]
|
||||||
# alloc-cortex-m = "0.3.5"
|
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`!
|
# this lets you use `cargo fix`!
|
||||||
[[bin]]
|
[[bin]]
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
#!/bin/bash
|
||||||
|
openocd -f interface/stlink-v2.cfg -f target/stm32f1x.cfg
|
87
src/main.rs
87
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_std]
|
||||||
#![no_main]
|
#![no_main]
|
||||||
|
|
||||||
// pick a panicking behavior
|
use panic_halt as _;
|
||||||
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 cortex_m::asm;
|
use nb::block;
|
||||||
|
|
||||||
|
use stm32f1xx_hal::{
|
||||||
|
prelude::*,
|
||||||
|
pac,
|
||||||
|
timer::Timer,
|
||||||
|
};
|
||||||
use cortex_m_rt::entry;
|
use cortex_m_rt::entry;
|
||||||
use stm32f1::stm32f103;
|
use embedded_hal::digital::v2::OutputPin;
|
||||||
|
|
||||||
#[entry]
|
#[entry]
|
||||||
fn main() -> ! {
|
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;
|
// Freeze the configuration of all the clocks in the system and store the frozen frequencies in
|
||||||
let rcc = &peripherals.RCC;
|
// `clocks`
|
||||||
let flash = &peripherals.FLASH;
|
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());
|
// Configure gpio C pin 13 as a push-pull output. The `crh` register is passed to the function
|
||||||
while rcc.cr.read().hserdy().is_not_ready() {};
|
// 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();
|
// Wait for the timer to trigger an update and change the state of the LED
|
||||||
|
loop {
|
||||||
rcc.cfgr.write(|w| {
|
block!(timer.wait()).unwrap();
|
||||||
w.pllmul().mul12()
|
led.set_high().unwrap();
|
||||||
.pllsrc().hse_div_prediv()
|
block!(timer.wait()).unwrap();
|
||||||
.pllxtpre().div2()
|
led.set_low().unwrap();
|
||||||
.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();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue