Cortex-M中的DWT计数器 Cortex-M3处理器系统框图分析

dwt跟踪组件
cortex-m3 权威指南:
16.2 trace components: dwt
the rest of the dwt counters are typically used for profiling the application codes. they can beprogrammed to emit events (in the form of trace packets) when the counter overflows. one typicalapplication is to use the cyccnt register to count the number of clock cycles required for a specifictask, for benchmarking purposes.
16.2 跟踪组件:数据观察点与跟踪(dwt)
dwt 中有剩余的计数器,它们典型地用于程序代码的“性能速写”(profiling)。通过编程它们,就可以让它们在计数器溢出时发出事件(以跟踪数据包的形式)。最典型地,就是使用 cyccnt寄存器来测量执行某个任务所花的周期数,这也可以用作时间基准相关的目的(操作系统中统计 cpu使用率可以用到它)。
cortex-m中的dwt
在cortex-m里面有一个外设叫dwt(data watchpoint and trace),是用于系统调试及跟踪,
它有一个32位的寄存器叫cyccnt,它是一个向上的计数器,记录的是内核时钟运行的个数,内核时钟跳动一次,该计数器就加1,精度非常高,如果内核时钟是72m,那精度就是1/72m = 14ns,而程序的运行时间都是微秒级别的,所以14ns的精度是远远够的。
最长能记录的时间为:59.65s。计算方法为2的32次方/72000000。
当cyccnt溢出之后,会清0重新开始向上计数。
使用方法
要实现延时的功能,总共涉及到三个寄存器:demcr 、dwt_ctrl、dwt_cyccnt,分别用于开启dwt功能、开启cyccnt及获得系统时钟计数值。
demcr
想要使能dwt外设,需要由另外的内核调试寄存器demcr的位24控制,写1使能(划重点啦,要考试!!)。demcr的地址是0xe000 edfc
关于dwt_cyccnt
使能dwt_cyccnt寄存器之前,先清0。让我们看看dwt_cyccnt的基地址,从arm-cortex-m手册中可以看到其基地址是0xe000 1004,复位默认值是0,而且它的类型是可读可写的,我们往0xe000 1004这个地址写0就将dwt_cyccnt清0了。
关于cyccntena
cyccntena enable the cyccnt counter. if not enabled, the counter does not count and no event is generated for ps sampling or cyccntena. in normal use, the debugger must initialize the cyccnt counter to 0.它是dwt控制寄存器的第一位,写1使能,则启用cyccnt计数器,否则cyccnt计数器将不会工作。
【https://developer.arm.com/documentation/ddi0337/e/system-debug/dwt/summary-and-description-of-the-dwt-registers?lang=en】
综上所述
想要使用dwt的cyccnt步骤:
先使能dwt外设,这个由另外内核调试寄存器demcr的位24控制,写1使能
使能cyccnt寄存器之前,先清0。
使能cyccnt寄存器,这个由dwt的cyccntena 控制,也就是dwt控制寄存器的位0控制,写1使能
寄存器定义:
//0xe000edfc demcr rw debug exception and monitor control register.  //使能dwt模块的功能位#define demcr           ( *(unsigned int *)0xe000edfc )  #define trcena          ( 0x01 << 24) // demcr的dwt使能位    //0xe0001000 dwt_ctrl rw the debug watchpoint and trace (dwt) unit  //使能cyccnt计数器开始计数#define dwt_ctrl        ( *(unsigned int *)0xe0001000 )  #define cyccntena       ( 0x01 << 0 ) // dwt的syccnt使能位 //0xe0001004 dwt_cyccnt rw cycle count register,   //cyccnt计数器的内部值(32位无符号)#define dwt_cyccnt      ( *(unsigned int *)0xe0001004) //显示或设置处理器的周期计数值   用法示例:
vvolatile unsigned int *dwt_cyccnt  ;volatile unsigned int *dwt_control ;volatile unsigned int *scb_demcr   ; void reset_timer(){    dwt_cyccnt   = (int *)0xe0001004; //address of the register    dwt_control  = (int *)0xe0001000; //address of the register    scb_demcr    = (int *)0xe000edfc; //address of the register    *scb_demcr   = *scb_demcr | 0x01000000;    *dwt_cyccnt  = 0; // reset the counter    *dwt_control = 0; } void start_timer(){    *dwt_control = *dwt_control | 1 ; // enable the counter} void stop_timer(){    *dwt_control = *dwt_control | 0 ; // disable the counter    } unsigned int getcycles(){    return *dwt_cyccnt;} main(){    ....    reset_timer(); //reset timer    start_timer(); //start timer    //code to profile    ...    myfunction();    ...    stop_timer(); //stop timer    numcycles = getcycles(); //read number of cycles     ...} 示例2:
#define start_timer()    *((volatile uint32_t*)0xe0001000) = 0x40000001  // enable cyccnt register#define stop_timer()   *((volatile uint32_t*)0xe0001000) = 0x40000000  // disable cyccnt register#define get_timer()   *((volatile uint32_t*)0xe0001004)               // get value from cyccnt register /************ how to use:*       uint32_t it1, it2;      // start and stop flag                                                    start_timer();          // start the timer.        it1 = get_timer();      // store current cycle-count in a local        // do something        it2 = get_timer() - it1;    // derive the cycle-count difference        stop_timer();               // if timer is not needed any more, stopprint_int(it2);                 // display the difference****/ 示例3:


变压器杀手?三相不平衡!
格如灵宣布完成了数千万元A轮融资,全面发力虚拟化教育市场
干电池内部构造(图)
电流互感器的型号表示及作用
日本NTT Docomo与东武铁道和华为合作完成5G毫米波外场试验
Cortex-M中的DWT计数器 Cortex-M3处理器系统框图分析
区块链是一种流行还是一个大事件
加密市场如此脆弱的原因的是什么
工业控制中PLC的无线远程控制
利用余压监控系统来探测与控制余压值的原因是什么
维信诺引领电子信息产业率先布局“未来市场”
什么是无线插座?对无线插座方便性而言还需要做更多改进
广汽埃安东南亚出口集散中心启运,泰国工厂首批货柜顺利发运
单端A类放大器及场效应管(3)
索尼的PS5和微软的Xbox Series X都将配备基于RDNA2的GPU
Linux 内核维护者需要更好的协作工具来改变贡献方式
微软宣布将于2020年1月关闭Remix3D
LTC3622双路1A同步单片式降压型稳压器介绍及应用分析
运算放大器组成阶梯波发生器电路图
M5156-000005-250BG压力传感器故障处理诊断方法