chiark / gitweb /
Big update here.
[wiringPi.git] / examples / delayTest.c
1
2 #include <stdio.h>
3 #include <unistd.h>
4 #include <wiringPi.h>
5
6 #include <sys/time.h>
7
8 #define CYCLES  1000
9
10 int main()
11 {
12   int x ;
13   struct timeval t1, t2 ;
14   int t ;
15   int max, min ;
16   int del ;
17   int underRuns, overRuns, exactRuns, total ;
18   int descheds ;
19
20   if (wiringPiSetup () == -1)
21     return 1 ;
22
23   piHiPri (10) ; sleep (1) ;
24
25 // Baseline test
26
27   gettimeofday (&t1, NULL) ;
28   gettimeofday (&t2, NULL) ;
29
30   t = t2.tv_usec - t1.tv_usec ;
31   printf ("Baseline test: %d\n", t);
32
33   for (del = 1 ; del < 200 ; ++del)
34   {
35     underRuns = overRuns = exactRuns = total = 0 ;
36     descheds = 0 ;
37     max = del ;
38     min = del ;
39
40     for (x = 0 ; x < CYCLES ; ++x)
41     {
42       for (;;)                          // Repeat this if we get a delay over 999uS
43       {                                 // -> High probability Linux has deschedulled us
44         gettimeofday (&t1, NULL) ;
45           delayMicroseconds (del) ;
46         gettimeofday (&t2, NULL) ;
47
48         if (t2.tv_usec < t1.tv_usec)    // Counter wrapped
49           t = (1000000 + t2.tv_usec) - t1.tv_usec;
50         else
51           t = t2.tv_usec - t1.tv_usec ;
52         if (t > 999)
53         {
54           ++descheds ;
55           continue ;
56         }
57         else
58           break ;
59       }
60
61       if (t > max)
62       {
63         max = t ;
64         ++overRuns ;
65       }
66       else if (t < min)
67       {
68         min = t ;
69         ++underRuns ;
70       }
71       else
72         ++exactRuns ;
73
74       total += t ;
75     }
76     printf ("Delay: %3d. Min: %3d, Max: %3d, Unders: %3d, Overs: %3d, Exacts: %3d, Average: %3d,  Descheds: %2d\n",
77         del, min, max, underRuns, overRuns, exactRuns, total / CYCLES,  descheds) ;
78     fflush (stdout) ;
79     delay (1) ;
80   }
81
82   return 0 ;
83 }