Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get the time stamp in a RTIC Rust Embedded application #940

Closed
Spago123 opened this issue May 23, 2024 · 1 comment
Closed

How to get the time stamp in a RTIC Rust Embedded application #940

Spago123 opened this issue May 23, 2024 · 1 comment

Comments

@Spago123
Copy link

Hello, I am trying to print the current time/tick of the program usingthe debugger console via the macro: hprintln!. I am having trouble taking the time of the monotonics timer. I want to be able to this:

    #[task(local = [led1])]
    fn blink(cx: blink::Context) {
        let now = mono.now();
        hprintln!("Blink 1: {}", now);

mono is defined in the init task as:

let mut mono: Systick<1000> = Systick::new(cx.core.SYST, 36_000_000);

I trined to make the mono a shared resource but the function signature of the init task must be as follows:
fn init(cx: init::Context) -> (Shared, Local, init::Monotonics)
I return the next tuple from the init task:

        (
            Shared {},
            Local { 
                led1, 
                led2
            },
            init::Monotonics(mono),
        )

And now I cannot access mono in my blink tasks.
The entire code is given below:

#![deny(unsafe_code)]
#![deny(warnings)]
#![no_main]
#![no_std]

use panic_rtt_target as _;
use rtic::app;
use rtt_target::{rprintln, rtt_init_print};
use stm32f3xx_hal::gpio::{Output, PushPull};
use stm32f3xx_hal::prelude::*;
use systick_monotonic::{fugit::Duration, Systick};

#[app(device = stm32f3xx_hal::pac, peripherals = true, dispatchers = [SPI1, SPI2])]
mod app {
    use stm32f3xx_hal::gpio::{PE8, PE9};
    use cortex_m_semihosting::hprintln;

    use super::*;

    #[shared]
    struct Shared {}

    #[local]
    struct Local {
        led1: PE8<Output<PushPull>>,
        led2: PE9<Output<PushPull>>,
    }

    #[monotonic(binds = SysTick, default = true)]
    type MonoTimer = Systick<1000>;

    const SLEEP_TIME: u64 = 1;

    #[init]
    fn init(cx: init::Context) -> (Shared, Local, init::Monotonics) {
        // Setup clocks
        let mut flash = cx.device.FLASH.constrain();
        let mut rcc = cx.device.RCC.constrain();

        let mut mono: Systick<1000> = Systick::new(cx.core.SYST, 36_000_000);

        rtt_init_print!();
        rprintln!("init");

        let now = mono.now();
        hprintln!("{}", now);

        let _clocks = rcc
            .cfgr
            .use_hse(8.MHz())
            .sysclk(36.MHz())
            .pclk1(36.MHz())
            .freeze(&mut flash.acr);

        // Setup LED
        let mut gpioe = cx.device.GPIOE.split(&mut rcc.ahb);
        let mut led1 = gpioe
            .pe8
            .into_push_pull_output(&mut gpioe.moder, &mut gpioe.otyper);
        led1.set_high().unwrap();
        let led2 = gpioe
            .pe9
            .into_push_pull_output(&mut gpioe.moder, &mut gpioe.otyper);

        // Schedule the blinking task
        blink::spawn_after(Duration::<u64, 1, 1000>::from_ticks(SLEEP_TIME)).unwrap();
        blink2::spawn_after(Duration::<u64, 1, 1000>::from_ticks(SLEEP_TIME)).unwrap();

        (
            Shared {},
            Local { 
                led1, 
                led2
            },
            init::Monotonics(mono),
        )
    }

    #[task(local = [led1])]
    fn blink(cx: blink::Context) {
        let now = mono.now();
        hprintln!("Blink 1: {}", now);
        cx.local.led1.toggle().unwrap();
        blink::spawn_after(Duration::<u64, 1, 1000>::from_ticks(SLEEP_TIME)).unwrap();
    }

    #[task(local = [led2], priority = 1)]
    fn blink2(cx: blink2::Context) {
        let now = mono.now(); /// not available in this scope
        hprintln!("Blink 1: {}", now);
        cx.local.led2.toggle().unwrap();
        blink2::spawn_after(Duration::<u64, 1, 1000>::from_ticks(SLEEP_TIME)).unwrap();
    }
}

Is there a way to accomplish this using mono, or should I use a different approach?

@Spago123 Spago123 changed the title How to get time step in a RTIC Rust Embedded application How to get the time stamp in a RTIC Rust Embedded application May 23, 2024
@antonok-edm
Copy link

@Spago123 based on your code it looks like you're using an older version of rtic-monotonics, but if you upgrade to the latest version you can call Systick::now() to get a timestamp.

let systick_mono_token = rtic_monotonics::create_systick_token!();
Systick::start(core.SYST, SYS_CLOCK_MHZ.MHz::<1, 1>().raw(), systick_mono_token);

let now = Systick::now();

In the latest version it's a static method so you can call it from anywhere, as long as it's been started previously.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants