当前位置:网站首页>Accurate calculation of time delay detailed explanation of VxWorks timestamp
Accurate calculation of time delay detailed explanation of VxWorks timestamp
2022-07-24 06:00:00 【Joey Boye o (* ^ ^ ^ *) o】
Project scenario :
vxWorks Accurate to ms Time keeping method of , Use a timestamp sysTimeStamp() function
Method of accurately calculating time delay Demo
void timeAccurate()
{
int t2,t3,tt2,tt3;
int time1,time2,freq;
int a=0;
while(a<10)
{
freq = (sysTimestampFreq()/1000);
t2=sysTimestamp(); /* Get the timestamp */
tt2=tickGet();
//msDelay(1);
taskDelay(2);
t3=sysTimestamp(); /* Get the timestamp */
tt3=tickGet();
//logMsg("is : t2=%d t3=%d tt2=%d tt3=%d t3-t2=%d tt3-tt2=%d\n",t2,t3,tt2,tt3,(t3-t2),(tt3-tt2));
//logMsg("miao :%d ms %d\n",((t3-t2)/399783),a,0,0,0,0);
//logMsg("freq is :%d\n",freq,0,0,0,0,0);
//logMsg("time is : %d\n",((t3-t2)/freq),0,0,0,0,0);
abb[a]=(tt3-tt2);
acc[a]=((t3-t2)/freq);
a++;
}
}
void printacc() /* Print uses the timestamp difference to calculate the delay time */
{
for(int i=0;i<10;i++)
{
logMsg("time is : %d ms\n",acc[i],0,0,0,0,0);
}
}
void printabb() /* To print using tick Difference calculation delay time */
{
for(int i=0;i<10;i++)
{
logMsg("tick time is : %d ms\n",abb[i],0,0,0,0,0);
}
}
void msDelay(int ms) // Delay function encapsulation Use timestamp to delay
{
int t,t1,t2;
t1 = sysTimestamp(); /* Record the timestamp of the last round */
while(ms--)
{
t = 0; /* Counter reset */
while(t < sysTimestampFreq()/1000)
{
t2 = sysTimestamp(); /* Read the current timestamp */
if(t2 > t1)
t += (t2-t1);
else
t += t2;
t1 = t2;
}
}
}
CPU Time stamp :
sysTimeStamp() Also known as “ Time stamp ”. It is realized through the system clock . At first, I also felt puzzled , The timing cycle of the system clock is tick, How to realize high-precision clock ? By reading BSP Underlying code discovery ,sysTimeStamp In fact, high-precision timing is obtained by reading the current count value of the timer . adopt sysTimestampFreq() Function can get the frequency of the system timestamp , It often reflects CPU The reference frequency of the timer . Of course , Such a high resolution can only be an ideal value , Different systems may not be able to achieve . After all, the implementation of this timestamp has a fatal weakness : Through query . The system clock timing interrupt is based on ticb: Unit , Further improve the resolution and read the count value of the timer (CPU A special function register of ), It can only be realized by query .
To get the precise time interval of milliseconds , And frequent calls do not cause the system CPU High occupancy .
I suggest taking adopt CPU Machine cycle solution .
Basically, most mainstream CPU All one. 64 Bit registers are used to record CPU After power on Number of machine cycles .
You can write an assembly function to get this 64 Bit count .
Then you start the program , Make a 10 Second timing , Get it once before and after the timing CPU Machine cycle .
And then subtract , You can get every second CPU Machine cycle ,
Divide this number by 1000 It's every millisecond CPU Machine cycle .
This value can be used as a constant in operation , Use us to name it Millisecond factor
Then you take the previous execution and the next execution when you perform the last execution ,
In the next execution, take the previous execution and the next execution .
Subtract twice to get Spaced CPU Machine cycle . Then use the above millisecond factor to convert it into milliseconds .、
Using this method, you just want to be accurate to nanosecond It's OK, too .
Tick And timestamp difference
vxWorks Under the tick and timestamp There's no correlation . Nor is it the relationship between seconds and minutes that you said .
tick In fact, it can be regarded as the frequency of kernel scheduling , It's also a time slice 、watchdog Overtime 、 Unit of task delay , So it is system clock.timestamp Can get high-precision timing , Follow RTOS Scheduling doesn't matter . For example, the count of reading registers at a certain time is A, Read again after a period of time as B, The main frequency is f. that B and A The time distance between them is :T = (B-A)*f.( Suppose it is used as timestamp Of timer No, rollover) Corresponding to hardware ,tick as long as timer Capable of producing periodic The interruption is That's ok , Used as a timestamp Of timer Ask for a little more .
Timestamp common functions
If the support Timestamp,BSP The following functions should be provided
sysTimestampConnect() - Connection timestamp interrupted
sysTimestampEnable() - Enable timestamp
sysTimestampDisable() - No timestamps
sysTimestampFreq() - Frequency of obtaining timestamp
sysTimestampPeriod() - Get timestamp period
sysTimestamp() - Get a timestamp
sysTimestampLock() - No interruptions , Get a timestamp
sysTimestampInt() - Optional timestamp ISR
边栏推荐
- json.dumps()函数解析
- 用指针访问二维数组
- Vscode multiline comments always expand automatically
- Signals and systems: Hilbert transform
- PLSQL query data garbled
- Could not load library cudnn_ cnn_ infer64_ 8.dll. Error code 126Please make sure cudnn_ cnn_ infer64_ eight
- Points for attention in adding spp module to the network
- systemctl + journalctl
- [activiti] activiti environment configuration
- 学习率余弦退火衰减之后的loss
猜你喜欢
随机推荐
String methods and instances
HAL_ Delay() delay error about 1ms
Numpy array broadcast rule memory method array broadcast broadcast principle broadcast mechanism
[activiti] gateway
如何在网页上下载视频
Native JS magnifying glass effect
精确计算时间延迟VxWorks 时间戳 详解
删除分类网络预训练权重的的head部分的权重以及修改权重名称
"Statistical learning methods (2nd Edition)" Li Hang Chapter 16 principal component analysis PCA mind map notes and after-school exercise answers (detailed steps) PCA matrix singular value Chapter 16
Machine learning (zhouzhihua) Chapter 2 model selection and evaluation notes learning experience
Could not load library cudnn_ cnn_ infer64_ 8.dll. Error code 126Please make sure cudnn_ cnn_ infer64_ eight
JUC并发编程基础(8)--读写锁
[activiti] activiti system table description
Loss after cosine annealing decay of learning rate
[MYCAT] Introduction to MYCAT
【数据库系统原理】第四章 高级数据库模型:统一建模语言UML、对象定义语言ODL
绘制轮廓 cv2.findContours函数及参数解释
Deepsort summary
jestson安装ibus输入法
JSON. Dumps() function parsing



![[activiti] personal task](/img/bc/80ac4067f6c58785acb4273f9507ee.png)





