-
Notifications
You must be signed in to change notification settings - Fork 9
/
lib.rs
73 lines (61 loc) · 1.75 KB
/
lib.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
#![no_std]
#![feature(lang_items)]
#[lang = "eh_personality"] extern fn eh_personality() {}
#[lang = "panic_fmt"] extern fn panic_fmt() -> ! { loop {} }
const PERIOD_MS: u32 = 1000;
const ON_MS: u32 = 50;
extern {
fn wait_ms(ms: u32);
}
#[no_mangle]
pub unsafe extern fn main() -> ! {
let row_2 = Pin::new(14);
let col_3 = Pin::new(6);
row_2.set_high();
loop {
col_3.set_low();
wait_ms(ON_MS);
col_3.set_high();
wait_ms(PERIOD_MS - ON_MS);
}
}
struct Pin {
mask: u32,
}
impl Pin {
fn new(number: usize) -> Self {
unsafe {
(*GPIO_BASE).PIN_CNF[number] = GPIO_PIN_CNF_DIR_Output;
}
Pin {
mask: 1 << number,
}
}
fn set_high(&self) {
unsafe {
(*GPIO_BASE).OUTSET = self.mask
}
}
fn set_low(&self) {
unsafe {
(*GPIO_BASE).OUTCLR = self.mask
}
}
}
#[allow(non_upper_case_globals)]
const GPIO_PIN_CNF_DIR_Output: u32 = 1;
const GPIO_BASE: *mut NRF_GPIO_Type = 0x50000000 as *mut _;
#[allow(non_snake_case)]
#[repr(C)]
struct NRF_GPIO_Type {
RESERVED_0: [u32; 321],
OUT: u32, /* Write GPIO port. */
OUTSET: u32, /* Set individual bits in GPIO port. */
OUTCLR: u32, /* Clear individual bits in GPIO port. */
IN: u32, /* Read GPIO port. */
DIR: u32, /* Direction of GPIO pins. */
DIRSET: u32, /* DIR set register. */
DIRCLR: u32, /* DIR clear register. */
RESERVED_1: [u32; 120],
PIN_CNF: [u32; 32], /* Configuration of GPIO pins. */
}