Skip to content

Blink an LED using an interrupt (no example) #112

@cjdell

Description

@cjdell

I have a ch32v003f4p6 and I just wanted to get interrupt functions working. The closest I have to an example is the implementation of Embassy:

https://github.com/ch32-rs/ch32-hal/blob/main/src/embassy/time_driver_tim.rs

It is however complex and so I'm really shooting in the dark. The following code compiles but the TIM1 function is never invoked.

#![no_std]
#![no_main]

use ch32_hal::gpio::{Level, Output};
use ch32_hal::pac::timer::vals::FilterValue;
use ch32_hal::peripherals::SPI1;
use ch32_hal::println;
use ch32_hal::time::Hertz;
use ch32_hal::timer::low_level::{InputTISelection, Timer};
use ch32_hal::timer::Channel;
use core::ptr::{self, null_mut};
use panic_halt as _;
use qingke::riscv;
use qingke_rt::interrupt;

struct UnsafeSync<T>(T);
unsafe impl<T> Sync for UnsafeSync<T> {}

static mut LED: UnsafeSync<*mut Output<'_>> = UnsafeSync(ptr::null_mut());

#[qingke_rt::interrupt]
fn TIM1() {
    unsafe {
        if !LED.0.is_null() {
            // Never called?
            (*LED.0).toggle();
        }
    }
}

#[qingke_rt::entry]
fn main() -> ! {
    riscv::asm::delay(20_000_000);

    let mut config = ch32_hal::Config::default();
    config.rcc = ch32_hal::rcc::Config::SYSCLK_FREQ_48MHZ_HSI;
    let p = ch32_hal::init(config);

    let mut led = Output::new(p.PD2, Level::Low, Default::default());
    unsafe { LED.0 = &mut led };

    let ch = Channel::Ch1;

    let timer = Timer::new(p.TIM1);
    timer.set_frequency(Hertz(1));
    timer.set_input_ti_selection(ch, InputTISelection::TRC);
    timer.set_input_capture_prescaler(ch, 0);
    timer.set_input_capture_filter(ch, FilterValue::FCK_INT_N2);
    timer.enable_channel(ch, true);
    timer.enable_input_interrupt(ch, true);
    timer.start();

    loop {
        // Just to test the GPIO works...
        // led.toggle();
    }
}

I'll be especially grateful if someone can show me how to get this example to work.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions