使用 STM32 和 HAL 将 DWT 周期计数转换为时间 [英] Convert DWT cycle count to time using STM32 and HAL

查看:30
本文介绍了使用 STM32 和 HAL 将 DWT 周期计数转换为时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 FreeRTOS 中的 STM32F302R8 上进行开发.我正在使用 here 来分析执行时间.我的 DWT 周期计数似乎有效,但我不确定如何将其转换为秒.从我在网上收集的信息来看,循环计数似乎是基于 CPU 频率的.哪个 HAL 函数将为我返回正确的 CPU 频率?我认为这是以下之一

I am developing on STM32F302R8 in FreeRTOS. I am using the following DWT code from here to profile execution time. My DWT cycle count seems to be working, but I am unsure how to convert it into seconds. From what I gathered online, it seems like the cycle count is based on the CPU frequency. Which HAL function will return the correct CPU frequency for me? I am thinking that it's one of the following

uint32_t          HAL_RCC_GetSysClockFreq(void);
uint32_t          HAL_RCC_GetHCLKFreq(void);
uint32_t          HAL_RCC_GetPCLK1Freq(void);
uint32_t          HAL_RCC_GetPCLK2Freq(void);

此外,我尝试通过在 1kHz 滴答中断中插入 DWT 代码来收集一些经验证据,如下所示:

Furthermore, I tried gathering some empirical evidence by inserting the DWT code inside the 1kHz tick interrupt like so:

void xPortSysTickHandler( void )
{
    /* The SysTick runs at the lowest interrupt priority, so when this interrupt
    executes all interrupts must be unmasked.  There is therefore no need to
    save and then restore the interrupt mask value as its value is already
    known. */
    portDISABLE_INTERRUPTS();
    {
      /* MY CODE START */
      static uint32_t cycles;
      cycles = KIN1_GetCycleCounter();
      KIN1_ResetCycleCounter();
      /* MY CODE END */

        /* Increment the RTOS tick. */
        if( xTaskIncrementTick() != pdFALSE )
        {
            /* A context switch is required.  Context switching is performed in
            the PendSV interrupt.  Pend the PendSV interrupt. */
            portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT;
        }
    }
    portENABLE_INTERRUPTS();
}

周期最终平均每次约为 71300.这是否意味着我的时钟运行在 71300 * 1000 = 71.3MHz?这似乎接近我的 HAL_RCC_GetSysClockFreq() 返回 72MHz.是什么导致了 71.3MHz 和 72Mhz 之间的轻微误差(假设我的计算是正确的)?

Cycles end up averaging about ~71300 each time. Does that mean my clock is running at 71300 * 1000 = 71.3MHz? This seems close to my HAL_RCC_GetSysClockFreq() which returns 72MHz. What's causing the slight error between 71.3MHz and 72Mhz though (assuming my calculation is correct)?

推荐答案

HAL_RCC_GetSysClockFreq() 对于 CPU 时钟是正确的.您看到的测量周期数与理论时钟速度之间的差异可能是由多种因素造成的;测量错误是其中大部分的可能来源(对 KIN1_GetCycleCounter()KIN1_ResetCycleCounter 的调用会有延迟),并且您没有提到您的时钟源,但可能不准确.

HAL_RCC_GetSysClockFreq() is correct for the CPU clock. The discrepancy you're seeing between the measured number of cycles and the theoretical clock speed could be due to a number of factors; measurement error is the likely source of most of it (there will be latency in the calls to KIN1_GetCycleCounter() and KIN1_ResetCycleCounter), and you don't mention the nature of your clock source but it's possible that it's inaccurate.

这篇关于使用 STM32 和 HAL 将 DWT 周期计数转换为时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆