Navigation

    全志在线开发者论坛

    • Register
    • Login
    • Search
    • Categories
    • Tags
    • 在线文档
    • 社区主页

    如何在R128的M33核上使用DWT实现代码块耗时统计或无中断环境下的延迟

    A Series
    2
    2
    803
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • H
      haaland LV 6 last edited by

      请教一下各位大佬,有没有可以在ARM Cortex-M33 处理器上使用DWT组件实现代码块耗时统计或无中断环境下延迟的方法

      livpo 1 Reply Last reply Reply Quote Share 0
      • livpo
        livpo LV 6 @haaland last edited by

        @haaland DWT组件全称是Data Watchpoint and Trace,M33处理器中有硬件支持,开发者可以使用它提供的watchpoint比较器以及周期计数器进行代码调试、性能分析等工作。

        可以借助DWT的计数器获取处理器的时间信息,计数器的精度是(1 / cpu频率):
        (1)使能DWT模块并清0计数器;
        (2)使能计数器;
        (3)读取计数器值。

        统计短代码块耗时示例:

        #define  DWT_CR      *(volatile uint32_t *)0xE0001000
        #define  DWT_CYCCNT  *(volatile uint32_t *)0xE0001004
        #define  DEM_CR      *(volatile uint32_t *)0xE000EDFC
        
        #define  DEM_CR_TRCENA                   (1 << 24)
        #define  DWT_CR_CYCCNTENA                (1 <<  0)
        
        int dwt_init(void)
        {
            DEM_CR |= (uint32_t)DEM_CR_TRCENA;        # 使能DWT追踪
            DWT_CYCCNT = (uint32_t)0u;                # 计数器清0
            DWT_CR |= (uint32_t)DWT_CR_CYCCNTENA;     # 使能计数器
        
            return 0;
        }
        
        void main(void)
        {
            uint32_t ticks;
            uint32_t told,tnow;
            uint32_t cpu_freq = get_cpu_freq();
            float time_ms;
        
            dwt_init();
        
            tcnt = 0;
            told = (uint32_t)DWT_CYCCNT;
        
            代码块
        
            tnow = (uint32_t)DWT_CYCCNT;
        
            /* 计算间隔 */
            if(tnow > told)
                tcnt += tnow - told;
            else
                tcnt += UINT32_MAX - told + tnow;
        
            time_ms = (float)(tcnt) / (cpufreq / 1000) # 假设经过的时间以ms为单位
        }
        

        实现延迟:
        根据计算时间的公式,也可以根据期望时间换算出所需要的tcnt计数值,在while中循环读取计数器值直至其增加的计数值达到tcnt,以实现延迟。

        1 Reply Last reply Reply Quote Share 0
        • 1 / 1
        • First post
          Last post

        Copyright © 2024 深圳全志在线有限公司 粤ICP备2021084185号 粤公网安备44030502007680号

        行为准则 | 用户协议 | 隐私权政策