#include "drv_dwt.h"
#include "drv_clk.h"
 
#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 drv_dwt_start(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 drv_dwt_end(void)
{
    return (uint64_t)DWT_CYCCNT * 1000000 / CLOCK_SYS_FREQ;
} 
