<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[如何在R128的M33核上使用DWT实现代码块耗时统计或无中断环境下的延迟]]></title><description><![CDATA[<p dir="auto">请教一下各位大佬，有没有可以在ARM Cortex-M33 处理器上使用DWT组件实现代码块耗时统计或无中断环境下延迟的方法</p>
]]></description><link>https://bbs.aw-ol.com/topic/4384/如何在r128的m33核上使用dwt实现代码块耗时统计或无中断环境下的延迟</link><generator>RSS for Node</generator><lastBuildDate>Tue, 19 May 2026 10:12:01 GMT</lastBuildDate><atom:link href="https://bbs.aw-ol.com/topic/4384.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 11 Oct 2023 02:15:47 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to 如何在R128的M33核上使用DWT实现代码块耗时统计或无中断环境下的延迟 on Wed, 11 Oct 2023 02:25:20 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://bbs.aw-ol.com/uid/1849">@haaland</a> DWT组件全称是Data Watchpoint and Trace，M33处理器中有硬件支持，开发者可以使用它提供的watchpoint比较器以及周期计数器进行代码调试、性能分析等工作。</p>
<p dir="auto">可以借助DWT的计数器获取处理器的时间信息，计数器的精度是（1 / cpu频率）：<br />
（1）使能DWT模块并清0计数器；<br />
（2）使能计数器；<br />
（3）读取计数器值。</p>
<p dir="auto">统计短代码块耗时示例：</p>
<pre><code>#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 &lt;&lt; 24)
#define  DWT_CR_CYCCNTENA                (1 &lt;&lt;  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 &gt; told)
        tcnt += tnow - told;
    else
        tcnt += UINT32_MAX - told + tnow;

    time_ms = (float)(tcnt) / (cpufreq / 1000) # 假设经过的时间以ms为单位
}
</code></pre>
<p dir="auto">实现延迟：<br />
根据计算时间的公式，也可以根据期望时间换算出所需要的tcnt计数值，在while中循环读取计数器值直至其增加的计数值达到tcnt，以实现延迟。</p>
]]></description><link>https://bbs.aw-ol.com/post/19128</link><guid isPermaLink="true">https://bbs.aw-ol.com/post/19128</guid><dc:creator><![CDATA[livpo]]></dc:creator><pubDate>Wed, 11 Oct 2023 02:25:20 GMT</pubDate></item></channel></rss>