Nios II 系统时钟timestamp的应用
软件:Quartus II 9.0, Nios II 9.0
图 1 调用Interval Timer核
各参数说明请参照Quartus II Handbook Version 9.0 Volume 5: Embedded Peripherals, Chapter 24: Interval Timer Core.请根据需要进行设置。
#include <unistd.h> //usleep()
#include "system.h"
#include <sys/alt_timestamp.h> //timestamp
#include "alt_types.h"
{
usleep(m);
}
void Delay1(unsigned long n)
{
for(;n>0;n--);
}
{
while(--p);
}
{
alt_u32 t0,t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12,t13,t14,t15,t16,
t17,t18,t19,t20,t21,t22,t23,t24,t25,t26,t27,t28,t29,t30,t31,t32,t33,
t34,t35,t36,t37,t38,t39,t40,t41,t42,t43,t44,t45,t46,t47,t48,t49,t50,
freq;
{
printf("No timestamp device available\n");
}
else
{
//usleep
t0 = alt_timestamp(); // 测量时间戳t0
Delay0(1);
t1 = alt_timestamp(); // 测量时间戳t1
Delay0(4);
t2 = alt_timestamp(); // 测量时间戳t2
Delay0(7);
t3 = alt_timestamp(); // 测量时间戳t3
Delay0(10);
t4 = alt_timestamp(); // 测量时间戳t4
Delay0(40);
t5 = alt_timestamp(); // 测量时间戳t5
Delay0(70);
t6 = alt_timestamp(); // 测量时间戳t6
Delay0(100);
t7 = alt_timestamp(); // 测量时间戳t7
Delay0(400);
t8 = alt_timestamp(); // 测量时间戳t8
Delay0(700);
t9 = alt_timestamp(); // 测量时间戳t9
Delay0(1000);
t10 = alt_timestamp(); // 测量时间戳t10
Delay0(4000);
t11 = alt_timestamp(); // 测量时间戳t11
Delay0(7000);
t12 = alt_timestamp(); // 测量时间戳t12
Delay0(10000);
t13 = alt_timestamp(); // 测量时间戳t13
Delay0(40000);
t14 = alt_timestamp(); // 测量时间戳t14
Delay0(70000);
t15 = alt_timestamp(); // 测量时间戳t15
Delay0(100000);
t16 = alt_timestamp(); // 测量时间戳t16
//for
t17 = alt_timestamp(); // 测量时间戳t17
Delay1(1);
t18 = alt_timestamp(); // 测量时间戳t18
Delay1(4);
t19 = alt_timestamp(); // 测量时间戳t19
Delay1(7);
t20 = alt_timestamp(); // 测量时间戳t20
Delay1(10);
t21 = alt_timestamp(); // 测量时间戳t21
Delay1(40);
t22 = alt_timestamp(); // 测量时间戳t22
Delay1(70);
t23 = alt_timestamp(); // 测量时间戳t23
Delay1(100);
t24 = alt_timestamp(); // 测量时间戳t24
Delay1(400);
t25 = alt_timestamp(); // 测量时间戳t25
Delay1(700);
t26 = alt_timestamp(); // 测量时间戳t26
Delay1(1000);
t27 = alt_timestamp(); // 测量时间戳t27
Delay1(4000);
t28 = alt_timestamp(); // 测量时间戳t28
Delay1(7000);
t29 = alt_timestamp(); // 测量时间戳t29
Delay1(10000);
t30 = alt_timestamp(); // 测量时间戳t30
Delay1(40000);
t31 = alt_timestamp(); // 测量时间戳t31
Delay1(70000);
t32 = alt_timestamp(); // 测量时间戳t32
Delay1(100000);
t33 = alt_timestamp(); // 测量时间戳t33
//while
t34 = alt_timestamp(); // 测量时间戳t34
Delay2(1);
t35 = alt_timestamp(); // 测量时间戳t35
Delay2(4);
t36 = alt_timestamp(); // 测量时间戳t36
Delay2(7);
t37 = alt_timestamp(); // 测量时间戳t37
Delay2(10);
t38 = alt_timestamp(); // 测量时间戳t38
Delay2(40);
t39 = alt_timestamp(); // 测量时间戳t39
Delay2(70);
t40 = alt_timestamp(); // 测量时间戳t40
Delay2(100);
t41 = alt_timestamp(); // 测量时间戳t41
Delay2(400);
t42 = alt_timestamp(); // 测量时间戳t42
Delay2(700);
t43 = alt_timestamp(); // 测量时间戳t43
Delay2(1000);
t44 = alt_timestamp(); // 测量时间戳t44
Delay2(4000);
t45 = alt_timestamp(); // 测量时间戳t45
Delay2(7000);
t46 = alt_timestamp(); // 测量时间戳t46
Delay2(10000);
t47 = alt_timestamp(); // 测量时间戳t47
Delay2(40000);
t48 = alt_timestamp(); // 测量时间戳t48
Delay2(70000);
t49 = alt_timestamp(); // 测量时间戳t49
Delay2(100000);
t50 = alt_timestamp(); // 测量时间戳t50
freq=alt_timestamp_freq();
printf("系统时钟频率是 %ld Hz\n", freq); //freq可以写成ALT_CPU_FREQ
printf("\n");
printf("usleep(1)= %ld us\n",(t1-t0)/(ALT_CPU_FREQ/1000000));
printf("usleep(4)= %ld us\n",(t2-t1)/(ALT_CPU_FREQ/1000000));
printf("usleep(7)= %ld us\n",(t3-t2)/(ALT_CPU_FREQ/1000000));
printf("usleep(10)= %ld us\n",(t4-t3)/(ALT_CPU_FREQ/1000000));
printf("usleep(40)= %ld us\n",(t5-t4)/(ALT_CPU_FREQ/1000000));
printf("usleep(70)= %ld us\n",(t6-t5)/(ALT_CPU_FREQ/1000000));
printf("usleep(100)= %ld us\n",(t7-t6)/(ALT_CPU_FREQ/1000000));
printf("usleep(400)= %ld us\n",(t8-t7)/(ALT_CPU_FREQ/1000000));
printf("usleep(700)= %ld us\n",(t9-t8)/(ALT_CPU_FREQ/1000000));
printf("usleep(1000)= %ld us\n",(t10-t9)/(ALT_CPU_FREQ/1000000));
printf("usleep(4000)= %ld us\n",(t11-t10)/(ALT_CPU_FREQ/1000000));
printf("usleep(7000)= %ld us\n",(t12-t11)/(ALT_CPU_FREQ/1000000));
printf("usleep(10000)= %ld us\n",(t13-t12)/(ALT_CPU_FREQ/1000000));
printf("usleep(40000)= %ld us\n",(t14-t13)/(ALT_CPU_FREQ/1000000));
printf("usleep(70000)= %ld us\n",(t15-t14)/(ALT_CPU_FREQ/1000000));
printf("usleep(100000)= %ld us\n",(t16-t15)/(ALT_CPU_FREQ/1000000));
printf("\n");
printf("for(1)= %ld us\n",(t18-t17)/(ALT_CPU_FREQ/1000000));
printf("for(4)= %ld us\n",(t19-t18)/(ALT_CPU_FREQ/1000000));
printf("for(7)= %ld us\n",(t20-t19)/(ALT_CPU_FREQ/1000000));
printf("for(10)= %ld us\n",(t21-t20)/(ALT_CPU_FREQ/1000000));
printf("for(40)= %ld us\n",(t22-t21)/(ALT_CPU_FREQ/1000000));
printf("for(70)= %ld us\n",(t23-t22)/(ALT_CPU_FREQ/1000000));
printf("for(100)= %ld us\n",(t24-t23)/(ALT_CPU_FREQ/1000000));
printf("for(400)= %ld us\n",(t25-t24)/(ALT_CPU_FREQ/1000000));
printf("for(700)= %ld us\n",(t26-t25)/(ALT_CPU_FREQ/1000000));
printf("for(1000)= %ld us\n",(t27-t6)/(ALT_CPU_FREQ/1000000));
printf("for(4000)= %ld us\n",(t27-t27)/(ALT_CPU_FREQ/1000000));
printf("for(7000)= %ld us\n",(t29-t28)/(ALT_CPU_FREQ/1000000));
printf("for(10000)= %ld us\n",(t30-t29)/(ALT_CPU_FREQ/1000000));
printf("for(40000)= %ld us\n",(t31-t30)/(ALT_CPU_FREQ/1000000));
printf("for(70000)= %ld us\n",(t32-t31)/(ALT_CPU_FREQ/1000000));
printf("for(100000)= %ld us\n",(t33-t32)/(ALT_CPU_FREQ/1000000));
printf("\n");
printf("while(1)= %ld us\n",(t35-t34)/(ALT_CPU_FREQ/1000000));
printf("while(4)= %ld us\n",(t36-t35)/(ALT_CPU_FREQ/1000000));
printf("while(7)= %ld us\n",(t37-t36)/(ALT_CPU_FREQ/1000000));
printf("while(10)= %ld us\n",(t38-t37)/(ALT_CPU_FREQ/1000000));
printf("while(40)= %ld us\n",(t39-t38)/(ALT_CPU_FREQ/1000000));
printf("while(70)= %ld us\n",(t40-t39)/(ALT_CPU_FREQ/1000000));
printf("while(100)= %ld us\n",(t41-t40)/(ALT_CPU_FREQ/1000000));
printf("while(400)= %ld us\n",(t42-t41)/(ALT_CPU_FREQ/1000000));
printf("while(700)= %ld us\n",(t43-t42)/(ALT_CPU_FREQ/1000000));
printf("while(1000)= %ld us\n",(t44-t43)/(ALT_CPU_FREQ/1000000));
printf("while(4000)= %ld us\n",(t45-t44)/(ALT_CPU_FREQ/1000000));
printf("while(7000)= %ld us\n",(t46-t45)/(ALT_CPU_FREQ/1000000));
printf("while(10000)= %ld us\n",(t47-t46)/(ALT_CPU_FREQ/1000000));
printf("while(40000)= %ld us\n",(t48-t47)/(ALT_CPU_FREQ/1000000));
printf("while(70000)= %ld us\n",(t49-t48)/(ALT_CPU_FREQ/1000000));
printf("while(100000)= %ld us\n",(t50-t49)/(ALT_CPU_FREQ/1000000));
}
return 0;
}
系统时钟频率是 50000000 Hz
usleep(1)= 42 us
usleep(4)= 38 us
usleep(7)= 40 us
usleep(10)= 44 us
usleep(40)= 72 us
usleep(70)= 102 us
usleep(100)= 130 us
usleep(400)= 418 us
usleep(700)= 706 us
usleep(1000)= 994 us
usleep(4000)= 3874 us
usleep(7000)= 6754 us
usleep(10000)= 9634 us
usleep(40000)= 38435 us
usleep(70000)= 67234 us
usleep(100000)= 96035 us
for(1)= 9 us
for(4)= 11 us
for(7)= 15 us
for(10)= 18 us
for(40)= 53 us
for(70)= 87 us
for(100)= 122 us
for(400)= 465 us
for(700)= 809 us
for(1000)= 226971 us
for(4000)= 0 us
for(7000)= 8018 us
for(10000)= 11452 us
for(40000)= 45782 us
for(70000)= 80115 us
for(100000)= 114447 us
while(1)= 7 us
while(4)= 10 us
while(7)= 11 us
while(10)= 14 us
while(40)= 35 us
while(70)= 58 us
while(100)= 79 us
while(400)= 298 us
while(700)= 516 us
while(1000)= 734 us
while(4000)= 2916 us
while(7000)= 5099 us
while(10000)= 7281 us
while(40000)= 29104 us
while(70000)= 50927 us
while(100000)= 72751 us
在ALT_CPU_FREQ处右击,选中Open Declaration,可以找到ALT_CPU_FREQ在system.h中的定义。其值与函数alt_timestamp_freq()返回的值相同,都是在SOPC中设置的。如图5所示。
3、(2011.5.31)
4、(2011.6.15)对于时序比较复杂要求严格的控制,比如DS18B20,不适用于Nios II完全控制它的操作。先用硬件逻辑实现,再用Nios II读取温度数据的办法较好。