可以利用ARM 内核中的Performance Monitor Unit(PMU,性能检测单元)来得到耗时的tick count。
转载别人的代码如下:
修改XTime_GetTime代码:
/****************************************************************************
*
* Get the time from the Global Timer Counter Register.
*
* @param Pointer to the location to be updated with the time.
*
* @return None.
*
* @note None.
*
****************************************************************************/
void XTime_GetTime(XTime *Xtime)
{
u32 low;
u32 high;
/* Reading Global Timer Counter Register */
do
{
high = Xil_In32(GLOBAL_TMR_BASEADDR + GTIMER_COUNTER_UPPER_OFFSET);
low = Xil_In32(GLOBAL_TMR_BASEADDR + GTIMER_COUNTER_LOWER_OFFSET);
} while(Xil_In32(GLOBAL_TMR_BASEADDR + GTIMER_COUNTER_UPPER_OFFSET) != high);
*Xtime = (((XTime) high) << 32) | (XTime) low;
}
测试代码:
#include <stdio.h>
#include "sleep.h"
#include "xil_io.h"
#include "xtime_l.h"
#include "xil_printf.h"
#include "xpm_counter.h"
#include "xparameters.h"
#define COUNTS_PER_SECOND (XPAR_CPU_CORTEXA9_CORE_CLOCK_FREQ_HZ / 2)
int main()
{
XTime tEnd, tCur;
u32 tUsed;
XTime_GetTime(&tCur);
usleep(1345);
XTime_GetTime(&tEnd);
tUsed = ((tEnd-tCur)*1000000)/(COUNTS_PER_SECOND);
xil_printf("time elapsed is %d us\r\n",tUsed);
while(1); //等待
return 0;
}