-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlib.rs
More file actions
66 lines (54 loc) · 2.06 KB
/
lib.rs
File metadata and controls
66 lines (54 loc) · 2.06 KB
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
#![feature(type_alias_impl_trait)]
use solid::{singleton::pin_singleton, thread::CpuCx, timer};
#[no_mangle]
pub extern "C" fn slo_main() {
println!("Starting LED blinker");
// Configure the LED port
green_led::init();
// Tracks the latest LED state. Moved into the handler closure when
// we create `solid::timer::Timer`.
let mut state = false;
// Construct a timer object on a global variable
let mut timer = pin_singleton!(: Timer<_> = timer::Timer::new(
timer::Schedule::Interval(timer::Usecs32(200_000)),
move |_: CpuCx<'_>| {
// Determine the next LED state
state = !state;
// Toggle the LED
green_led::update(state);
},
))
.unwrap();
// Start the timer
assert!(
timer.as_mut().start().expect("unable to start timer"),
"timer was already running"
);
assert!(timer.is_running());
}
mod green_led {
use bcm2711_pac::gpio;
use tock_registers::interfaces::{ReadWriteable, Writeable};
const GPIO_NUM: usize = 42;
fn gpio_regs() -> &'static gpio::Registers {
// Safety: SOLID for RaPi4B provides an identity mapping in this area, and we don't alter
// the mapping
unsafe { &*(gpio::BASE.to_arm_pa().unwrap() as usize as *const gpio::Registers) }
}
pub fn init() {
// Configure the GPIO pin for output
gpio_regs().gpfsel[GPIO_NUM / gpio::GPFSEL::PINS_PER_REGISTER].modify(
gpio::GPFSEL::pin(GPIO_NUM % gpio::GPFSEL::PINS_PER_REGISTER).val(gpio::GPFSEL::OUTPUT),
);
}
pub fn update(new_state: bool) {
if new_state {
gpio_regs().gpset[GPIO_NUM / gpio::GPSET::PINS_PER_REGISTER]
.write(gpio::GPSET::set(GPIO_NUM % gpio::GPSET::PINS_PER_REGISTER));
} else {
gpio_regs().gpclr[GPIO_NUM / gpio::GPCLR::PINS_PER_REGISTER].write(gpio::GPCLR::clear(
GPIO_NUM % gpio::GPCLR::PINS_PER_REGISTER,
));
}
}
}