如何在Stm32上运行定时器中断时获取微秒级的代码执行时间?

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

How do I get code execution time in MicroSecond while running timer interrupt on Stm32?

问题

如何在定时器中断执行期间以微秒为单位检查代码的运行时间?

我想知道如何在STM32 IDE上获取代码的执行时间。
谢谢。

英文:

How do I check the operating time of the code in MicroSecond units during timer interrupt execution?

I would like to know how to get the execution time of code on STM32 IDE.
Thank you.

答案1

得分: 0

为了测量微秒,您需要一些硬件定时器。最容易实现的是SysTick定时器,它内置在核心中,非常简单配置。您只需要设置计数器的重载值、当前值和时钟源。时钟源可以是CPU时钟,这意味着您可以计算任何事情所需的周期数。

将计数器值保存到(全局的话)变量中,运行函数,将新的计数器值保存到第二个变量中。两者之间的差异大致是经过的时钟周期数,具有相当高的精度。

定时器本身很精确,从计数器中读取/保存变量会带来一些额外开销,但如果您在没有中断的情况下直接从计数器中读取,那么所有这些操作将需要不到10个时钟周期,这可能是可以容忍的误差。

显然,如果您知道CPU频率以及运行某项任务所需的时钟周期数,您就基本上知道了时间。

有关STM32的SysTick配置在您的MCU(或其核心)的编程手册文档中有详细说明。它是一个非常简单的外设,只包含4个寄存器,只需要配置一对东西。

  1. 无需在RCC中激活其时钟,它始终具有时钟。
  2. 将计数器值设置为起始值(它是倒计数器)。可以是0xFFFFFF(它是24位的倒计数器)。
  3. 将重载值设置为计数器值(通常为了当计数器达到0时,它将从相同的计数器值重新开始计数)。
  4. 将SysTick时钟源设置为CPU时钟而不是外部时钟信号。
  5. 当您实际需要它时,启用计数器。
  6. 在测量完成后停止计数器。
  7. 再次读取计数器值。两个计数器值之间的差异就是经过的时钟周期数。

显然,如果您的函数所需的时间超过了完整的倒计数,那么它将不起作用,因此您将不得不实现一个SysTick中断,以获得一个变量,其中将包含经过的倒计数数量。这将增加每次中断的开销,约为30个周期(中断进入、中断退出、计数器增加)。因此,最终您将获得经过的完整倒计数数量以及剩余的计数器值。

如果您需要SysTick用于其他用途,完全可以使用任何其他硬件定时器来实现此操作,只需注意它们的时钟源频率通常与APBx或AHB频率相关(取决于定时器及其时钟源配置,这可以在定时器本身或RCC专用时钟配置寄存器中配置,如果它们存在的话)。

英文:

To measure microseconds, you need some hardware timer. The easiest to implement would be the SysTick timer, which is built into the core itself, and is extremely simple to configure. All you need to do is to set the counter reload value, counter current value, and clock source. Clock source can be the CPU clock, which means, you can literally count the number of cycles it takes for anything.

Save counter value into (global, if you wish) variable, run function, save new counter value into variable two. The difference between the two is roughly the number of clock cycles it took, with pretty good precision.

The timer itself is precise, it's a little overhead to read/save the counter variable, but if you do that without interrupt straight by reading from the counter, then all of that will take under 10 clock cycles, which is likely tolerable error.

Obviously, if you know CPU frequency and how many clock cycles it took for something to run, you basically have the time.

SysTick configuration of STM32 is covered in Programming Manual document for your MCU (or, rather, its core). It's an extremely simple peripheral consisting of only 4 registers, with literally only a pair of things that need to be configured there.

  1. No need to activate its clock in RCC, it always has clock.
  2. Set counter value to start value (it counts DOWN). Could be 0xFFFFFF (it's a 24-bit down counter)
  3. Set reload value to counter value (typically; so when counter reaches 0, it will start from the same counter value again).
  4. Set SysTick clock source to CPU clock rather than external clock signal
  5. When you actually need it, enable counter.
  6. Stop the counter when you're done measuring.
  7. Read the counter value again. The difference between counter values is the number of clock cycles passed.

Obviously, if your function takes more than full countdown, it won't work, so you'll have to implement a systick interrupt then to have a variable which will contain how many countdowns have passed. This will add an overhead of ~30 cycles per interrupt (interrupt entry, interrupt exit, counter increment). So at the end you will have a number of full countdowns passed plus the remainder counter value.

If you need SysTick for other purposes, you can totally do this with any other hardware timer, just bear in mind their source clock frequency is typically related to APBx or AHB frequency (depending on timer and its clock source configuration, which could be inside the timer itself or RCC dedicated clock configuration registers, if they're present)

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

发表评论

匿名网友

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

确定