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
|
use gd32vf103xx_hal::prelude::_embedded_hal_timer_CountDown;
use core::convert::Infallible;
use gd32vf103xx_hal::{
eclic::{self, EclicExt},
pac::{self, Interrupt},
time::Hertz,
timer::{Event, Timer},
};
use nb;
use crate::led::LED;
enum State {
WaitForTimer,
ToggleLED,
}
pub struct Task {
timer: Timer<pac::TIMER6>,
frequency: Hertz,
led: LED,
state: State,
}
impl Task {
pub fn new(mut timer: Timer<pac::TIMER6>, frequency: Hertz, led: LED) -> Self {
pac::ECLIC::setup(Interrupt::TIMER6, eclic::TriggerType::RisingEdge, eclic::Level::L0, eclic::Priority::P3);
unsafe { pac::ECLIC::unmask(Interrupt::TIMER6); }
if !pac::ECLIC::is_enabled(Interrupt::TIMER6) {
panic!("timer6 interrupt not enabled");
}
timer.listen(Event::Update);
Self { timer, frequency, led, state: State::ToggleLED }
}
pub fn poll(&mut self) -> nb::Result<(), Infallible> {
match self.state {
State::WaitForTimer => {
if let Ok(_) = self.timer.wait() {
self.state = State::ToggleLED;
// self.timer.unlisten(Event::Update);
Ok(())
} else {
Err(nb::Error::WouldBlock)
}
},
State::ToggleLED => {
self.led.toggle();
self.timer.start(self.frequency);
self.state = State::WaitForTimer;
// self.timer.listen(Event::Update);
Err(nb::Error::WouldBlock)
}
}
}
}
|