获取自嵌入式 Rust 中某一时刻以来的经过时间

huangapple go评论58阅读模式
英文:

Get elapsed time since some instant in embedded rust

问题

I started making a program in rust, and I want to be able to get the elapsed time since some instant. Here's how the std library does it.

use std::time::{Duration, Instant};
use std::thread::sleep;

fn main() {
   let now = Instant::now();

   // we sleep for 2 seconds
   sleep(Duration::new(2, 0));
   // it prints '2'
   println!("{}", now.elapsed().as_secs());
}

I don't have std in embedded rust so how can I get around this? I'm using the stm32f1xx hal with an stm32f103c8t6. I've tried to have a look at various timer related modules in the hal, as well as the embedded time crate. But I'm having no luck getting anything to work. Any help would be appreciated.

I've scraped together some random lines of code from things other people have done that are loosely related.

let mut counter = Timer::new(dp.TIM2, &clocks).counter_us();
counter.start(1_000_000_u32.micros()).unwrap();
let mut timer = Timer::syst(cp.SYST, &clocks);
let mut counter = Timer::counter_us(timer);

But I can't even initialize a counter without errors, even though relevant functions, structs, etc. Should all be imported.

error[E0599]: no function or associated item named `new` found for struct `Timer` in the current scope
  --> src/main.rs:87:30
   |
87 |     let mut counter = Timer::new(dp.TIM2, &clocks).counter_us();
   |                              ^^^ function or associated item not found in `Timer<_>`
use panic_halt as _;

use cortex_m_rt::entry;
use embedded_hal::digital::v2::OutputPin;
use stm32f1xx_hal as hal;
use hal::{pac, TIM2, timer::Timer, delay::Delay, prelude::*};

use rtt_target::{rprintln, rtt_init_print};
英文:

I started making a program in rust, and I want to be able to get the elapsed time since some instant. Here's how the std library does it.

use std::time::{Duration, Instant};
use std::thread::sleep;

fn main() {
   let now = Instant::now();

   // we sleep for 2 seconds
   sleep(Duration::new(2, 0));
   // it prints &#39;2&#39;
   println!(&quot;{}&quot;, now.elapsed().as_secs());
}

I don't have std in embedded rust so how can I get around this? I'm using the stm32f1xx hal with an stm32f103c8t6. I've tried to have a look at various timer related modules in the hal, aswell as the embedded time crate. But I'm having no luck getting anything to work. Any help would be appreciated.

I've scraped together some random lines of code from things other people have done that are loosely related.

let mut counter = Timer::new(dp.TIM2, &amp;clocks).counter_us();
counter.start(1_000_000_u32.micros()).unwrap();
let mut timer = Timer::syst(cp.SYST, &amp;clocks);
let mut counter = Timer::counter_us(timer);

But I can't even initialize a counter without errors, even though relevant functions, structs, etc. Should all be imported.

error[E0599]: no function or associated item named `new` found for struct `Timer` in the current scope
  --&gt; src/main.rs:87:30
   |
87 |     let mut counter = Timer::new(dp.TIM2, &amp;clocks).counter_us();
   |                              ^^^ function or associated item not found in `Timer&lt;_&gt;`

use panic_halt as _;

use cortex_m_rt::entry;
use embedded_hal::digital::v2::OutputPin;
use stm32f1xx_hal as hal;
use hal::{pac, TIM2, timer::Timer, delay::Delay, prelude::*};

use rtt_target::{rprintln, rtt_init_print};

答案1

得分: 1

这样的代码听起来怎么样?

let some_clock = SomeClock;
let start = some_clock.try_now().unwrap();

// 进行一些操作

let duration = some_clock.try_now().unwrap() - start;

这基本上就是文档中展示的内容。你有一个实现了embedded_time::Clock trait的结构体吗?

否则,考虑使用MonoTimer如何?

英文:

How does something like this sound?


let some_clock = SomeClock;
let start = some_clock.try_now().unwrap();

// Do something

let duration = some_clock.try_now().unwrap() - start;

This is pretty much what the documentation shows. Do you have some struct that implements the embedded_time::Clock trait?

Otherwise how about using the MonoTimer?

答案2

得分: 1

一开始查看stm32f1xx_hal中的各个模块和结构时,我不确定如何初始化需要传递给函数的结构。在查看从问题答案中提供的MonoTimer结构时,我发现了DWT。我可以使用DWT查看总计的CPU周期。我可以将其与MonoTimer(我认为)一起使用,以获得比我最初寻找的实现更接近的结果。但对于我的用例来说,获取CPU周期计数实际上更有益。

这是我用来获取CPU周期计数的相关代码:

// 获取设备和核心外设的访问权限
let dp = pac::Peripherals::take().unwrap();
let mut cp = cortex_m::Peripherals::take().unwrap();

// 启用循环计数器
cp.DCB.enable_trace();
cp.DWT.enable_cycle_counter();

// 输出周期
loop {
    let cycles = DWT::cycle_count();
    rprintln!("{:?}", cycles);
    delay.delay_ms(1000u16);
}
英文:

Initially looking at various modules and structs within the stm32f1xx_hal, I wasn't sure how to initialize structs that I needed to pass to functions. While looking at the MonoTimer struct provided from an answer to my question I found DWT. I can see the total elapsed CPU cycles with DWT. I could use this with MonoTimer (I think) to get an implementation closer than I was initially looking for. But having the CPU cycle count is actaully more beneficial for my use case.

Here's the relevant code I'm using to get CPU cycle count:

// Get access to device and core peripherals
let dp = pac::Peripherals::take().unwrap();
let mut cp = cortex_m::Peripherals::take().unwrap();

// Enabled cycle counter
cp.DCB.enable_trace();
cp.DWT.enable_cycle_counter();


// Output cycles
loop {
    let cycles = DWT::cycle_count();
    rprintln!(&quot;{:?}&quot;, cycles);
    delay.delay_ms(1000u16);
}

huangapple
  • 本文由 发表于 2023年5月15日 13:57:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76251222.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定