This repository has been archived by the owner on Jun 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.rs
85 lines (66 loc) · 1.79 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#![allow(improper_ctypes)]
#![no_std]
#![no_main]
#![feature(lang_items)]
#![feature(int_uint)] // update fail_bounds_check
#![feature(no_std)]
#![crate_name="blinky"]
//extern crate libc;
use zero::std_types::*;
use libarm::stm32f4xx::*;
mod zero {
pub mod std_types;
pub mod zero;
}
#[macro_use]
mod libarm {
#[macro_use] pub mod stm32f4xx;
#[macro_use] pub mod stm32f4xx_gpio;
#[macro_use] pub mod stm32f4xx_rcc;
}
const LED_GREEN :uint = 12;
const LED_ORANGE :uint = 13;
const LED_RED :uint = 14;
const LED_BLUE :uint = 15;
static LED :uint = LED_RED;
#[no_mangle]
pub extern "C" fn TIM2_IRQHandler() {
let TIM2 = TIM2();
let GPIOD = GPIOD();
let toggle_led = 1 << LED;
// flash on update event
if TIM2.SR & TIM_SR_UIF!() > 0 {
GPIOD.ODR ^= toggle_led;
}
TIM2.SR = 0x0; // reset the status register
}
#[no_mangle]
pub extern "C" fn main()
{
let pin = LED;
let RCC = RCC();
let GPIOD = GPIOD();
let TIM2 = TIM2();
let NVIC = NVIC();
let mode = GPIO_Mode_OUT!() << (pin * 2);
let speed = GPIO_Speed_100MHz!() << (pin * 2);
let otype = GPIO_OType_PP!() << pin;
let pullup = GPIO_PuPd_NOPULL!() << (pin * 2);
let irq_en = 1 << (TIM2_IRQn!() as uint);
RCC.AHB1ENR |= RCC_AHB1ENR_GPIODEN!(); // enable the clock to GPIOD
RCC.APB1ENR |= RCC_APB1ENR_TIM2EN!(); // enable TIM2 clock
//
// Initialise the GPIO port.
//
GPIOD.MODER |= mode;
GPIOD.OSPEEDR |= speed;
GPIOD.OTYPER |= otype;
GPIOD.PUPDR |= pullup;
NVIC.ISER[0] |= irq_en; // enable the TIM2 IRQ
TIM2.PSC = 0xFFFF; // max prescaler
TIM2.DIER |= TIM_DIER_UIE!(); // enable update interrupt
TIM2.ARR = 0xFF; // count to 255 (autoreload value 255)
TIM2.CR1 |= TIM_CR1_ARPE!() | TIM_CR1_CEN!(); // autoreload on, counter enabled
TIM2.EGR = 1; // trigger update event to reload timer registers
loop {}
}