AMD MicroBlaze中通过AXI Timer获取时间戳

作者:付汉杰 hankf@amd.com,来源:博客园

AMD MicroBlaze中通过AXI Timer获取时间戳

1. 硬件的Vivado Block design设计中,添加AXI Timer

2. 包含AXI Timer的头文件

#include "xtmrctr.h"

3. 定义timer的参数和全局变量

/*
* The following constants map to the XPAR parameters created in the
* xparameters.h file. They are only defined here such that a user can easily
* change all the needed parameters in one place.
*/
#define TMRCTR_DEVICE_ID              XPAR_TMRCTR_0_DEVICE_ID

/*
 * This example only uses the 1st of the 2 timer counters contained in a
 * single timer counter hardware device
 */
#define TIMER_COUNTER_0	 0

// #define XPAR_AXI_TIMER_0_CLOCK_FREQ_HZ 100000000U  100 000 000U
#define TMRCTR_DEVICE_FREQ_MS              (XPAR_TMRCTR_0_CLOCK_FREQ_HZ/1000)
#define TMRCTR_DEVICE_FREQ_US              (TMRCTR_DEVICE_FREQ_MS/1000)

#define TMRCTR_DEVICE_PERIOD_NS ( (1000*1000*1000)/XPAR_AXI_TIMER_0_CLOCK_FREQ_HZ )

XTmrCtr TimerCounter; /* The instance of the Tmrctr Device */

4. 初始化AXI Timer
调用TmrCtrPolledInit()初始化AXI Timer, 也初始化了全局变量TimerCounter。

 TmrCtrPolledInit(TMRCTR_DEVICE_ID, TIMER_COUNTER_0);

其中TmrCtrPolledInit()来自于xtmrctr_polled_example.c中的TmrCtrPolledExample(),可以在BSP中从AXI Timer的“Import Example”中导入。

5. 通过XTmrCtr_GetValue获取时间戳
通过XTmrCtr_GetValue获取时间戳,是以时钟周期为单位的。可以转化成微妙、毫秒、秒等单位。

time_begin = XTmrCtr_GetValue(TmrCtrInstancePtr, TmrCtrNumber);
for (i=0; i<100000; i++ ) {
g_iLoopTest++;
}
time_end = XTmrCtr_GetValue(TmrCtrInstancePtr, TIMER_COUNTER_0);

if( time_end < time_begin ){
time_diff = 0xffffffff - time_begin;
time_diff = time_diff + time_end + 1;
} else {
time_diff = time_end - time_begin;
}
time_diff_us = time_diff/TMRCTR_DEVICE_FREQ_US;
xil_printf("100K loop: %x : Timer diff: 0x%08x = %d Cycles, %d us, %d ms at %s : %d\r\n",
g_iLoopTest, time_diff, time_diff, time_diff_us, time_diff_us/1000, __func__, __LINE__ );

最新文章

最新文章