本帖最后由 yjvijfhvk 于 2022-10-8 11:31 编辑
[C] 纯文本查看 复制代码 #include<stdio.h>
static unsigned int s_u32Speed; //DEBUG Stopwatch stop cycle counter value
#define DEMCR_TRCENA 0x01000000
/* Core Debug registers */
#define DEMCR (*((volatile unsigned int *)0xE000EDFC))
#define DWT_CTRL (*(volatile unsigned int *)0xe0001000)
#define CYCCNTENA (1<<0)
#define DWT_CYCCNT ((volatile unsigned int *)0xE0001004)
#define CPU_CYCLES *DWT_CYCCNT
//#define CLK_SPEED 72000000 // EXAMPLE for CortexM4, EDIT as needed
/*****************************************************************************
Prototype : i32DWT_IfSupport
Description : 检测芯片是否支持DWT
Input : void
Output : None
Return Value :
Calls :
Called By :
History :
1.Date : 2022/10/7
Author : liusuchao
Modification : Created function
*****************************************************************************/
int i32DWT_IfSupport(void){
if(DWT_CTRL == 0x00)
return -1;
return 0;
}
/*****************************************************************************
Prototype : u32DWT_Getticks
Description : 获取失踪周期
Input : void
Output : None
Return Value : unsigned
Calls :
Called By :
History :
1.Date : 2022/10/7
Author : liusuchao
Modification : Created function
*****************************************************************************/
unsigned int u32DWT_Getticks(void)
{
return CPU_CYCLES;
}
/*****************************************************************************
Prototype : vDWT_Init
Description : 初始化DWT
Input : unsigned int u32M
Output : None
Return Value :
Calls :
Called By :
History :
1.Date : 2022/10/7
Author : liusuchao
Modification : Created function
*****************************************************************************/
void vDWT_Init(unsigned int u32M)
{
s_u32Speed = u32M *1000000;
/* Enable DWT */
DEMCR |= DEMCR_TRCENA;
*DWT_CYCCNT = 0;
/* Enable CPU cycle counter */
DWT_CTRL |= CYCCNTENA;
}
/*****************************************************************************
Prototype : vDWT_Delay
Description : 延时函数,我一般会用示波器测试一下 72M时钟 vDWT_Delay(300) = 5us
Input : unsigned int ticks
Output : None
Return Value :
Calls :
Called By :
History :
1.Date : 2022/10/7
Author : liusuchao
Modification : Created function
*****************************************************************************/
void vDWT_Delay(unsigned int ticks)
{
unsigned int end_ticks = ticks + u32DWT_Getticks();
while(1)
{
if (u32DWT_Getticks() >= end_ticks)
break;
}
}
/*****************************************************************************
Prototype : u32DWT_CalcNanosecondsFromStopwatch
Description : 用于计算程序执行时间
Input : unsigned int u32StopCount
unsigned int u32StartCount
Output : None
Return Value : unsigned
Calls :
Called By :
History :
1.Date : 2022/10/7
Author : liusuchao
Modification : Created function
*****************************************************************************/
unsigned int u32DWT_CalcNanosecondsFromStopwatch(unsigned int u32StopCount, unsigned int u32StartCount)
{
unsigned int nDiffTicks;
unsigned int nSystemCoreTicksPerMicrosec;
// Convert (clk speed per sec) to (clk speed per microsec)
nSystemCoreTicksPerMicrosec = s_u32Speed / 1000000;
// Elapsed ticks
nDiffTicks = u32StopCount - u32StartCount;
// Elapsed nanosec = 1000 * (ticks-elapsed / clock-ticks in a microsec)
return 1000 * nDiffTicks / nSystemCoreTicksPerMicrosec;
}
|