日本a√视频在线,久久青青亚洲国产,亚洲一区欧美二区,免费g片在线观看网站

        <style id="k3y6c"><u id="k3y6c"></u></style>
        <s id="k3y6c"></s>
        <mark id="k3y6c"></mark>
          
          

          <mark id="k3y6c"></mark>

          "); //-->

          博客專欄

          EEPW首頁 > 博客 > 一個超級實(shí)用的單片機(jī)調(diào)試技巧!DWT組件

          一個超級實(shí)用的單片機(jī)調(diào)試技巧!DWT組件

          發(fā)布人:xiaomaidashu 時(shí)間:2022-10-11 來源:工程師 發(fā)布文章
          DWT跟蹤組件

           跟蹤組件:數(shù)據(jù)觀察點(diǎn)與跟蹤(DWT)

          圖片

          DWT 中有剩余的計(jì)數(shù)器,它們典型地用于程序代碼的“性能速寫”(profiling)。通過編程它們,就可以讓它們在計(jì)數(shù)器溢出時(shí)發(fā)出事件(以跟蹤數(shù)據(jù)包的形式)。

          最典型地,就是使用 CYCCNT寄存器來測量執(zhí)行某個任務(wù)所花的周期數(shù),這也可以用作時(shí)間基準(zhǔn)相關(guān)的目的(操作系統(tǒng)中統(tǒng)計(jì) CPU使用率可以用到它)。

          Cortex-M中的DWT

          在Cortex-M里面有一個外設(shè)叫DWT(Data Watchpoint and Trace),是用于系統(tǒng)調(diào)試及跟蹤。

          圖片

          它有一個32位的寄存器叫CYCCNT,它是一個向上的計(jì)數(shù)器,記錄的是內(nèi)核時(shí)鐘運(yùn)行的個數(shù),內(nèi)核時(shí)鐘跳動一次,該計(jì)數(shù)器就加1,精度非常高,如果內(nèi)核時(shí)鐘是72M,那精度就是1/72M = 14ns,而程序的運(yùn)行時(shí)間都是微秒級別的,所以14ns的精度是遠(yuǎn)遠(yuǎn)夠的。

          最長能記錄的時(shí)間為:59.65s。計(jì)算方法為2的32次方/72000000。

          當(dāng)CYCCNT溢出之后,會清0重新開始向上計(jì)數(shù)。

          使用方法

          要實(shí)現(xiàn)延時(shí)的功能,總共涉及到三個寄存器:DEMCR 、DWT_CTRL、DWT_CYCCNT,分別用于開啟DWT功能、開啟CYCCNT及獲得系統(tǒng)時(shí)鐘計(jì)數(shù)值。

          DEMCR

          想要使能DWT外設(shè),需要由另外的內(nèi)核調(diào)試寄存器DEMCR的位24控制,寫1使能(劃重點(diǎn)啦,要考試?。。?。DEMCR的地址是0xE000 EDFC

          圖片圖片

          關(guān)于DWT_CYCCNT

          使能DWT_CYCCNT寄存器之前,先清0。讓我們看看DWT_CYCCNT的基地址,從ARM-Cortex-M手冊中可以看到其基地址是0xE000 1004,復(fù)位默認(rèn)值是0,而且它的類型是可讀可寫的,我們往0xE000 1004這個地址寫0就將DWT_CYCCNT清0了。

          圖片關(guān)于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計(jì)數(shù)器,否則CYCCNT計(jì)數(shù)器將不會工作。

          【https://developer.arm.com/documentation/ddi0337/e/system-debug/dwt/summary-and-description-of-the-dwt-registers?lang=en】

          圖片綜上所述

          想要使用DWT的CYCCNT步驟:

          1. 先使能DWT外設(shè),這個由另外內(nèi)核調(diào)試寄存器DEMCR的位24控制,寫1使能

          2. 使能CYCCNT寄存器之前,先清0。

          3. 使能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計(jì)數(shù)器開始計(jì)數(shù)
          #define DWT_CTRL        ( *(unsigned int *)0xE0001000 )  
          #define CYCCNTENA       ( 0x01 << 0 ) // DWT的SYCCNT使能位
           
          //0xE0001004 DWT_CYCCNT RW Cycle Count register,   
          //CYCCNT計(jì)數(shù)器的內(nèi)部值(32位無符號)
          #define DWT_CYCCNT      ( *(unsigned int *)0xE0001004) //顯示或設(shè)置處理器的周期計(jì)數(shù)值

          用法示例:

          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)
           
          /* 初始化時(shí)間戳 */
           
          void CPU_TS_TmrInit(void)
           
          {
           
                  /* 使能DWT外設(shè) */
                  DEM_CR |= (uint32_t)DEM_CR_TRCENA;               
           
                  /* DWT CYCCNT寄存器計(jì)數(shù)清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);
           
          }



          原文地址:https://blog.csdn.net/booksyhay/article/details/109028712

          轉(zhuǎn)自公眾號:技術(shù)讓夢想更偉大


          *博客內(nèi)容為網(wǎng)友個人發(fā)布,僅代表博主個人觀點(diǎn),如有侵權(quán)請聯(lián)系工作人員刪除。



          關(guān)鍵詞: 單片機(jī)

          相關(guān)推薦

          技術(shù)專區(qū)

          關(guān)閉