Created
January 15, 2019 19:14
-
-
Save nraynaud/675cc2c0217261564d316bad554dee4c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//! Blinks an LED | |
#![deny(unsafe_code)] | |
#![no_std] | |
#![no_main] | |
//MCU: STM32F103C8T699 | |
extern crate panic_halt; | |
use rtfm::{app, Instant, Duration}; | |
use stm32f103xx_hal::{ | |
gpio::gpioc::PC13, | |
gpio::gpioa::PA0, | |
gpio::Output, | |
gpio::PushPull, | |
prelude::*, | |
time::Hertz, | |
}; | |
const STEP_MIN_S: f32 = 1.1e-6; | |
const DIR_MIN_S: f32 = 201e-9; | |
fn hertz_to_cycles(sysclock: Hertz, hertz: Hertz) -> u32 { | |
sysclock.0 / hertz.0 | |
} | |
fn seconds_to_cycles(sysclock: Hertz, seconds: f32) -> u32 { | |
(sysclock.0 as f32 * seconds) as u32 | |
} | |
#[app(device = stm32f103xx)] | |
const APP: () = { | |
static mut LED_GLOBAL: PC13<Output<PushPull>> = (); | |
static mut STEP_PIN: PA0<Output<PushPull>> = (); | |
static mut PERIOD: u32 = (); | |
static STEP_MIN_DURATION: Duration = (); | |
static DIR_MIN_DURATION: Duration = (); | |
#[init(schedule = [toggle])] | |
unsafe fn init() { | |
let mut rcc = device.RCC.constrain(); | |
let mut gpioa = device.GPIOA.split(&mut rcc.apb2); | |
let mut gpioc = device.GPIOC.split(&mut rcc.apb2); | |
let mut flash = device.FLASH.constrain(); | |
let clocks = rcc.cfgr.freeze(&mut flash.acr); | |
let sysclock = clocks.sysclk(); | |
let period = hertz_to_cycles(sysclock, 50.hz()); | |
schedule.toggle(Instant::now() + period.cycles()).unwrap(); | |
PERIOD = period; | |
LED_GLOBAL = gpioc.pc13.into_push_pull_output(&mut gpioc.crh); | |
STEP_PIN = gpioa.pa0.into_push_pull_output(&mut gpioa.crl); | |
STEP_MIN_DURATION = seconds_to_cycles(sysclock, STEP_MIN_S).cycles(); | |
DIR_MIN_DURATION = seconds_to_cycles(sysclock, DIR_MIN_S).cycles(); | |
} | |
#[task(schedule = [toggle, set_step_high, set_step_low], resources = [LED_GLOBAL, PERIOD, STEP_MIN_DURATION])] | |
fn toggle() { | |
resources.LED_GLOBAL.toggle(); | |
*resources.PERIOD = (*resources.PERIOD as f32 * 1.1) as u32; | |
schedule.toggle(scheduled + resources.PERIOD.cycles()).unwrap(); | |
schedule.set_step_high(scheduled + resources.PERIOD.cycles()).unwrap(); | |
schedule.set_step_low(scheduled + resources.PERIOD.cycles() + *resources.STEP_MIN_DURATION).unwrap(); | |
} | |
fn schedule_step() { // <--- error: this item must live outside the `#[app]` module | |
} | |
#[task(resources = [STEP_PIN])] | |
fn set_step_high() { | |
resources.STEP_PIN.set_high(); | |
} | |
#[task(resources = [STEP_PIN])] | |
fn set_step_low() { | |
resources.STEP_PIN.set_low(); | |
} | |
extern "C" { | |
fn USART1(); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment