aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/blink.rs54
-rwxr-xr-xsrc/main.rs60
2 files changed, 65 insertions, 49 deletions
diff --git a/src/blink.rs b/src/blink.rs
new file mode 100644
index 0000000..7c992d7
--- /dev/null
+++ b/src/blink.rs
@@ -0,0 +1,54 @@
+use crate::{PollResult, led::LED};
+
+use gd32vf103xx_hal::prelude::_embedded_hal_timer_CountDown;
+
+use gd32vf103xx_hal::{
+ eclic::{self, EclicExt},
+ pac::{self, Interrupt},
+ time::Hertz,
+ timer,
+};
+
+enum State {
+ WaitForTimer,
+ ToggleLED,
+}
+
+pub struct Task {
+ timer: timer::Timer<pac::TIMER6>,
+ frequency: Hertz,
+ led: LED,
+ state: State,
+}
+
+impl Task {
+ pub fn new(mut timer: 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(timer::Event::Update);
+
+ Self { timer, frequency, led, state: State::ToggleLED }
+ }
+
+ pub fn poll(&mut self) -> PollResult {
+ match self.state {
+ State::WaitForTimer => {
+ if let Ok(_) = self.timer.wait() {
+ self.state = State::ToggleLED;
+ PollResult::Ok
+ } else {
+ PollResult::WouldBlock
+ }
+ },
+ State::ToggleLED => {
+ self.led.toggle();
+ self.timer.start(self.frequency);
+ self.state = State::WaitForTimer;
+ PollResult::WouldBlock
+ }
+ }
+ }
+}
diff --git a/src/main.rs b/src/main.rs
index 2944500..2740d25 100755
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,60 +1,27 @@
#![no_std]
#![no_main]
+mod blink;
mod led;
-//use core::arch::global_asm;
-//use gd32vf103_pac::{self as pac, Interrupt, Peripherals};
use gd32vf103xx_hal::{
eclic::{self, EclicExt},
- pac::{self, Interrupt, Peripherals},
+ pac::{self, Peripherals},
rcu::RcuExt,
time::Hertz,
timer,
};
use riscv::asm::wfi;
use riscv_rt::entry;
-use gd32vf103xx_hal::prelude::_embedded_hal_timer_CountDown;
use led::LED;
// global_asm!(include_str!("boot.S"));
-enum State {
- WaitForTime(timer::Timer<pac::TIMER6>, Hertz, LED),
- ToggleLED(timer::Timer<pac::TIMER6>, Hertz, LED),
-}
-
-static mut DEBUG: usize = 1;
-
-impl State {
- fn next(self) -> Self {
- match self {
- Self::WaitForTime(mut timer, duration, led) => {
- match timer.wait() {
- Ok(_) => {
- unsafe {
- DEBUG += 1
- };
-
- led.toggle();
- timer.start(duration);
- Self::WaitForTime(timer, duration, led)
- },
- Err(_) => Self::WaitForTime(timer, duration, led),
- }
- },
-
- Self::ToggleLED(mut timer, duration, led) => {
- unsafe {
- DEBUG += 1
- };
- led.toggle();
- timer.start(duration);
- Self::WaitForTime(timer, duration, led)
- },
- }
- }
+#[derive(PartialEq)]
+pub enum PollResult {
+ Ok,
+ WouldBlock,
}
#[entry]
@@ -66,19 +33,14 @@ fn main(_hartid: usize) -> ! {
let t6 = p.TIMER6;
let mut rcu = p.RCU.configure().freeze();
- let mut timer = timer::Timer::<pac::TIMER6>::timer6(t6, Hertz(10), &mut rcu);
- 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(timer::Event::Update);
+ let timer = timer::Timer::<pac::TIMER6>::timer6(t6, Hertz(1), &mut rcu);
let led = LED::new();
- let mut state = State::ToggleLED(timer, Hertz(2), led);
+ let mut blink = blink::Task::new(timer, Hertz(5), led);
loop {
- state = state.next();
- unsafe { wfi() };
+ if blink.poll() == PollResult::WouldBlock {
+ unsafe { wfi() };
+ }
}
}