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

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

问题描述

我正在FreeRTOS中开发STM32F302R8.我正在此处来描述执行时间.我的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);

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

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天全站免登陆