From 35248b205cd56633e52b9e634a942e7fd987db0b Mon Sep 17 00:00:00 2001 From: Brian Cully Date: Sat, 6 Aug 2022 12:12:37 -0400 Subject: =?UTF-8?q?Blinky=20on=20a=20hardware=20timer,=20with=20=E2=80=98w?= =?UTF-8?q?fi=E2=80=99=20in=20between.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.rs | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 82 insertions(+), 11 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index aee0767..2944500 100755 --- a/src/main.rs +++ b/src/main.rs @@ -3,27 +3,98 @@ mod led; -use core::arch::global_asm; -use gd32vf103_pac::Peripherals; +//use core::arch::global_asm; +//use gd32vf103_pac::{self as pac, Interrupt, Peripherals}; +use gd32vf103xx_hal::{ + eclic::{self, EclicExt}, + pac::{self, Interrupt, 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")); +// global_asm!(include_str!("boot.S")); -#[no_mangle] +enum State { + WaitForTime(timer::Timer, Hertz, LED), + ToggleLED(timer::Timer, 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) + }, + } + } +} + +#[entry] fn main(_hartid: usize) -> ! { - // let peripherals = Peripherals::take().unwrap(); - let peripherals = unsafe { Peripherals::steal() }; + let p = unsafe { Peripherals::steal() }; + + pac::ECLIC::set_threshold_level(eclic::Level::L0); + pac::ECLIC::set_level_priority_bits(eclic::LevelPriorityBits::L3P1); + + let t6 = p.TIMER6; + let mut rcu = p.RCU.configure().freeze(); + let mut timer = timer::Timer::::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 led = LED::new(&peripherals); + let led = LED::new(); + let mut state = State::ToggleLED(timer, Hertz(2), led); loop { - led.toggle(); - delay(); + state = state.next(); + unsafe { wfi() }; } } -fn delay() { - for _ in 1..100_000 {} +#[export_name="ExceptionHandler"] +fn exception_handler(_frame: &riscv_rt::TrapFrame) -> ! { + loop { unsafe { wfi() } }; +} + +#[export_name="DefaultHandler"] +fn default_handler() -> ! { + loop { unsafe { wfi() } }; +} + +#[export_name="MachineTimer"] +fn machine_timer() { + loop { unsafe { wfi() } }; } #[panic_handler] -- cgit v1.2.3