forked from havardh/geckoboot.rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstruct.rs
99 lines (70 loc) · 1.62 KB
/
struct.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
85
86
87
88
89
90
91
92
93
94
95
96
97
#![no_std]
#![feature(lang_items)]
extern crate core;
use emlib::cmu;
use emlib::gpio;
use cmsis::sys_tick;
use core::intrinsics::{volatile_load, volatile_store};
mod emlib;
mod cmsis;
mod zero { pub mod zero; }
static mut msTicks: u32 = 0;
#[no_mangle]
pub extern fn SysTick_Handler() {
unsafe {
let ticks = volatile_load(&msTicks as *const u32);
volatile_store(&mut msTicks as *mut u32, ticks + 1);
}
}
fn delay(dlyTicks: u32) {
unsafe {
let curTicks = volatile_load(&msTicks as *const u32);
while volatile_load(&msTicks as *const u32) - curTicks < dlyTicks {}
}
}
#[no_mangle]
pub unsafe extern fn _start() {
main()
}
const LED0: u32 = 2;
const LED1: u32 = 3;
struct Led {
pin: u32
}
impl Led {
pub fn init(&self) {
gpio::pin_mode_set(gpio::Port::E, self.pin, gpio::Mode::PushPull, 0);
}
pub fn set(&self) {
gpio::pin_out_set(gpio::Port::E, self.pin);
}
pub fn clear(&self) {
gpio::pin_out_clear(gpio::Port::E, self.pin);
}
pub fn toggle(&self) {
gpio::pin_out_toggle(gpio::Port::E, self.pin);
}
}
fn init() {
let freq = cmu::clock_freq_get(cmu::Clock::CORE);
if sys_tick::config(freq) != 0 {
loop {}
}
cmu::clock_enable(cmu::Clock::HFPER, true);
cmu::clock_enable(cmu::Clock::GPIO, true);
}
#[no_mangle]
pub unsafe extern fn main() {
init();
let led0 = Led { pin:LED0 };
let led1 = Led { pin:LED1 };
led0.init();
led1.init();
led0.set();
led1.clear();
loop {
led0.toggle();
led1.toggle();
delay(1);
}
}