DWT跟蹤組件
跟蹤組件:數據觀察點與跟蹤(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, stop
print_int(it2); // Display the difference
****/
示例3:
#define DWT_CR *(uint32_t *)0xE0001000
#define DWT_CYCCNT *(uint32_t *)0xE0001004
#define DEM_CR *(uint32_t *)0xE000EDFC
#define DEM_CR_TRCENA (1 < < 24)
#define DWT_CR_CYCCNTENA (1 < < 0)
/* 初始化時間戳 */
void CPU_TS_TmrInit(void)
{
/* 使能DWT外設 */
DEM_CR |= (uint32_t)DEM_CR_TRCENA;
/* DWT CYCCNT寄存器計數清0 */
DWT_CYCCNT = (uint32_t)0u;
/* 使能Cortex-M3 DWT CYCCNT寄存器 */
DWT_CR |= (uint32_t)DWT_CR_CYCCNTENA;
}
uint32_t OS_TS_GET(void)
{
return ((uint32_t)DWT_CYCCNT);
}
-
寄存器
+關注
關注
31文章
5363瀏覽量
121162 -
cpu
+關注
關注
68文章
10902瀏覽量
213015 -
DWT
+關注
關注
0文章
20瀏覽量
11166
發布評論請先 登錄
相關推薦
評論