📄 timestampt.c
字号:
/* timestampTest.c - Timestamp Test Library *//* Copyright 1997-2004 DY4 Systems Inc. *//*modification history--------------------03a,17feb04,mb rewrote timestampTest(), added timestampInterval, added timestampRollover, removed tickCount02a,26jul01,aak added timestampTest()01a,03jun98,jcc written*//* * DESCRIPTION * * This library contains the routines to be used in testing the timestamp * driver. * * The routines included are : * * timestampTest - main routine to be called for the timestamp test * timestampInterval - routine used as a task to display timestamp data at certain intervals * timestampRollover - routine called when an interrupt occurs during the timestamp roll-over * */#include "stdio.h"#include "math.h"#include "logLib.h"#include "semLib.h"#include "sysLib.h"#include "tickLib.h"#include "taskLib.h"SEM_ID dataSemId; /* Given by the ISR when a datum is available */int timeInterval = 10;int testLength = 20;int rollover;int timeTaken;int firstTime;/***************************************************************************** timestampInterval - This routine is called when the task "tIntervalTask"* is spawned from the main routine.* * This routine's purpose is to check the time interval between two given * timestamps and ensure that this time is accurate according to the time* interval given by the user. It also adjusts for when a rollover occurs * so that there are no discrepencies with the time intervals. */void timestampInterval (void){ int tickTime = timeInterval*sysClkRateGet(); /*time interval in ticks to delay the task*/ int timeDeltaTicks=0;/*delta between timestamps*/ float accuracy, timeDeltaSec; int i,j; int rollCount =0; /*counter for the number of roll-overs*/ int oldTime = 0;/*previous timestamp count*/ int newTime = sysTimestamp();/*current timestamp count*/ firstTime = tickGet();/*initial number of ticks - for rollover purposes*/ rollover = 0; /*Initial Settings*/ printf("\nInitial timestamp:\t\t%u ticks\n",newTime); printf("System timestamp period:\t%u ticks\n",sysTimestampPeriod()); printf("System timestamp frequency:\t%u Hz\n",sysTimestampFreq()); printf("________________________________________________________\n\n"); taskDelay(15);/*create a delay of 1/4 second to prepare the test*/ /* loop through the test */ for(i=0;i<testLength;i++) { if (i==0)/* initial run through with a fresh timestamp*/ { oldTime = sysTimestamp(); } else { oldTime = newTime; } taskDelay(tickTime); /*wait for a period of tickTime*/ newTime = sysTimestamp(); if(rollover == 1)/* if there has been a roll-over*/ { rollover =0; rollCount ++; timeDeltaTicks = sysTimestampPeriod() - oldTime + newTime; /* calculation accounts for roll-over time*/ timeDeltaSec = (float)timeDeltaTicks/(float)sysTimestampFreq(); accuracy = 100 - (fabs(timeDeltaSec - timeInterval)*100 / timeDeltaSec); printf("________________________________________________________\n\n"); printf("A roll-over has occurred at %d seconds.\n",timeTaken); printf("________________________________________________________\n\n\n"); } else { timeDeltaTicks = newTime - oldTime; timeDeltaSec = (float)timeDeltaTicks/(float)sysTimestampFreq(); accuracy = 100 - (fabs(timeDeltaSec - timeInterval)*100); } printf("Delta between ticks: \t %1.6f seconds\n",timeDeltaSec); printf("Accuracy of delta: \t%1.6f %%\n\n",accuracy); } printf("________________________________________________________\n\n"); /* final results summary */ printf("Time to run the test:\t%d seconds\n",timeInterval*testLength); printf("Number of rollovers:\t%d\n",rollCount); for(j=0;j<rollCount;j++){ printf("Timestamp rollover %d:\t%u ticks\n",(j+1),sysTimestampPeriod()); } printf("Final timestamp:\t%u ticks",newTime); /* release the semaphore*/ semGive(dataSemId);} /**************************************************************************** * timestampRollover - This routine is called when an interrupt occurs * * It calculates the time it has taken for the interrupt to occur since the * start of the test. */void timestampRollover(void) { int currentTime = tickGet();/* current number of ticks */ rollover =1; timeTaken = (currentTime - firstTime)/sysClkRateGet();/* time taken for the rollover to occur*/} /**************************************************************************** * * timestampTest - main routine to be called * This routine installs the timestamp ISR, spawns a task that calls the * timestampInterval routine and waits for the semaphore from the ISR. * Once the number of iterations (given by the user) have completed, the * semaphore is released. */void timestampTest(void){ int taskId; /* Wait for user input for the time interval */ printf("\nEnter desired Time Interval(between 1 and 40 seconds): "); scanf("%d",&timeInterval); while (timeInterval > 40 || timeInterval < 1) { printf("\n\nThe time interval is not within the required range.\nPlease choose another interval (between 1 and 40 seconds)\n\n"); printf("Enter desired time Interval: "); scanf("%d",&timeInterval); } /* Wait for user input for the number of iterations */ printf("Enter desired number of Iterations: "); scanf("%d",&testLength); /* start of timestamp test */ printf("\n\n********************************************************\n"); printf("* Timestamp Test Started *"); printf("\n********************************************************\n\n"); /* Creation of the semaphore */ dataSemId = semBCreate (SEM_Q_FIFO, SEM_EMPTY); /* Install the ISR to be called at each timestamp timer interrupt */ sysTimestampConnect((FUNCPTR)timestampRollover, 0); /* Enable the timetamp device */ sysTimestampEnable(); printf("The test will run %d times at %d second intervals...\n\n",testLength, timeInterval); /* Spawn task */ taskId = taskSpawn("tIntervalTask", 20, 0, 4096, (FUNCPTR)timestampInterval,0,0,0,0,0,0,0,0,0,0); /* Take semaphore*/ semTake(dataSemId, -1); /* Cleanup*/ sysTimestampDisable(); taskDelete(taskId); semDelete (dataSemId); printf("\n\n********************************************************\n"); printf("* Timestamp Test Completed *"); printf("\n********************************************************\n\n");}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -