GithubHelp home page GithubHelp logo

Comments (2)

lulf avatar lulf commented on September 27, 2024

Which chip/hal are you using? You can configure the system tick rate setting a feature on the embassy-time-driver crate: https://docs.embassy.dev/embassy-time-driver/git/default/index.html#feature-flags

If you want to use a timer directly (i.e. you want a higher/lower freq timer compared to the system one), you can usually do that, but that depends on the HAL you are using. Example for stm32: https://docs.embassy.dev/embassy-stm32/git/stm32c011d6/timer/low_level/struct.Timer.html

from embassy.

ckellar33 avatar ckellar33 commented on September 27, 2024

I am using a STM32F427VI chip/hal. I currently have the tick rate configured to 1MHz. However, I did notice I had a println!() call within the task. This did slow it down quite a bit.

I modified the task and now my code looks like this. I am still only seeing about a 63.299us accuracy on a 5us toggle.

#[interrupt]
unsafe fn TIM6_DAC() {
    EXECUTOR_HIGH.on_interrupt();
}

#[embassy_executor::task]
async fn blinker(mut led: Output<'static, AnyPin>) {
    println!("blinker launched");
    loop {
        if led.is_set_high() {
            led.set_low();
        } else {
            led.set_high();
        }
        Timer::after_micros(5).await;
    }
}

#[embassy_executor::main]
async fn main(spawner: Spawner) {
    defmt::debug!("debug");

    // Special eth config
    let mut config = Config::default();
    {
        use embassy_stm32::rcc::*;
        config.rcc.hse = Some(Hse {
            freq: Hertz(8_000_000),
            mode: HseMode::Oscillator,
        });
        config.rcc.pll_src = PllSource::HSE;
        config.rcc.pll = Some(Pll {
            prediv: PllPreDiv::DIV4,
            mul: PllMul::MUL168,
            divp: Some(PllPDiv::DIV2), // 8mhz / 4 * 180 / 2 = 180Mhz.
            divq: Some(PllQDiv::DIV4),
            divr: None,
        });
        config.rcc.ahb_pre = AHBPrescaler::DIV1;
        config.rcc.apb1_pre = APBPrescaler::DIV4;
        config.rcc.apb2_pre = APBPrescaler::DIV2;
        config.rcc.sys = Sysclk::PLL1_P;
    }
    let p = embassy_stm32::init(config);
    
    // Driving a GPIO with a high priority timer
    let led = Output::new(p.PE15, Level::Low, Speed::VeryHigh);
    interrupt::TIM6_DAC.set_priority(Priority::P6);
    let timer_spawner = EXECUTOR_HIGH.start(interrupt::TIM6_DAC);
    unwrap!(timer_spawner.spawn(blinker(led.degrade())));

    loop {
        Timer::after(Duration::from_secs(1)).await;
    }
}

I could be going about this incorrectly though. I want to have a highly accurate timer (1us) running and controllable with start/stop routines. Should I use a low-level? I tried the following code, but the IRQ does not fire.

    #[interrupt]
    unsafe fn TIM6_DAC() {
        if led.is_set_high() {
            led.set_low();
        } else {
            led.set_high();
        }
    }

    let mut timer = p.TIM6;
    interrupt::TIM6_DAC.set_priority(Priority::P6);
    unsafe { interrupt::TIM6_DAC.enable() };
    timer.set_frequency(Hertz(1));
    timer.set_autoreload_preload(true);
    timer.enable_update_interrupt(true);
    timer.start();

Any additional guidance would be appreciated.

Thank you,

from embassy.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.