1 | /* |
---|
2 | * Nanoseconds accuracy timestamp test |
---|
3 | */ |
---|
4 | |
---|
5 | /* COPYRIGHT (c) 1989-2011. |
---|
6 | * On-Line Applications Research Corporation (OAR). |
---|
7 | * |
---|
8 | * The license and distribution terms for this file may be |
---|
9 | * found in the file LICENSE in this distribution or at |
---|
10 | * http://www.rtems.com/license/LICENSE. |
---|
11 | * |
---|
12 | * $Id$ |
---|
13 | */ |
---|
14 | |
---|
15 | #define CONFIGURE_INIT |
---|
16 | |
---|
17 | #ifdef HAVE_CONFIG_H |
---|
18 | #include "config.h" |
---|
19 | #endif |
---|
20 | |
---|
21 | #include <rtems.h> |
---|
22 | #include <inttypes.h> |
---|
23 | #include <stdio.h> |
---|
24 | #include <string.h> |
---|
25 | #include <stdlib.h> |
---|
26 | #include <unistd.h> |
---|
27 | #include <sys/time.h> |
---|
28 | #include <rtems/score/timespec.h> /* _Timespec_Substract */ |
---|
29 | #include "tmacros.h" |
---|
30 | |
---|
31 | char *my_ctime( time_t t ) |
---|
32 | { |
---|
33 | static char b[32]; |
---|
34 | ctime_r(&t, b); |
---|
35 | b[ strlen(b) - 1] = '\0'; |
---|
36 | return b; |
---|
37 | } |
---|
38 | |
---|
39 | void subtract_em( |
---|
40 | struct timespec *start, |
---|
41 | struct timespec *stop, |
---|
42 | struct timespec *t |
---|
43 | ) |
---|
44 | { |
---|
45 | t->tv_sec = 0; |
---|
46 | t->tv_nsec = 0; |
---|
47 | _Timespec_Subtract( start, stop, t ); |
---|
48 | } |
---|
49 | |
---|
50 | /* body below .. hoping it isn't inlined */ |
---|
51 | extern void dummy_function_empty_body_to_force_call(void); |
---|
52 | |
---|
53 | rtems_task Init( |
---|
54 | rtems_task_argument argument |
---|
55 | ) |
---|
56 | { |
---|
57 | rtems_status_code status; |
---|
58 | rtems_time_of_day time; |
---|
59 | int index; |
---|
60 | |
---|
61 | puts( "\n\n*** NANOSECOND CLOCK TEST ***" ); |
---|
62 | |
---|
63 | time.year = 2007; |
---|
64 | time.month = 03; |
---|
65 | time.day = 24; |
---|
66 | time.hour = 11; |
---|
67 | time.minute = 15; |
---|
68 | time.second = 0; |
---|
69 | time.ticks = 0; |
---|
70 | |
---|
71 | status = rtems_clock_set( &time ); |
---|
72 | directive_failed( status, "clock set" ); |
---|
73 | |
---|
74 | /* |
---|
75 | * Iterate 10 times showing difference in TOD |
---|
76 | */ |
---|
77 | printf( "10 iterations of getting TOD\n" ); |
---|
78 | for (index=0 ; index <10 ; index++ ) { |
---|
79 | struct timespec start, stop; |
---|
80 | struct timespec diff; |
---|
81 | #if 0 |
---|
82 | clock_gettime( CLOCK_REALTIME, &start ); |
---|
83 | clock_gettime( CLOCK_REALTIME, &stop ); |
---|
84 | #else |
---|
85 | _TOD_Get( &start ); |
---|
86 | _TOD_Get( &stop ); |
---|
87 | #endif |
---|
88 | |
---|
89 | subtract_em( &start, &stop, &diff ); |
---|
90 | printf( "Start: %s:%ld\nStop : %s:%ld", |
---|
91 | my_ctime(start.tv_sec), start.tv_nsec, |
---|
92 | my_ctime(stop.tv_sec), stop.tv_nsec |
---|
93 | ); |
---|
94 | |
---|
95 | printf( " --> %" PRItime_t ":%ld\n", diff.tv_sec, diff.tv_nsec ); |
---|
96 | } |
---|
97 | |
---|
98 | /* |
---|
99 | * Iterate 10 times showing difference in Uptime |
---|
100 | */ |
---|
101 | printf( "\n10 iterations of getting Uptime\n" ); |
---|
102 | for (index=0 ; index <10 ; index++ ) { |
---|
103 | struct timespec start, stop; |
---|
104 | struct timespec diff; |
---|
105 | rtems_clock_get_uptime( &start ); |
---|
106 | rtems_clock_get_uptime( &stop ); |
---|
107 | |
---|
108 | subtract_em( &start, &stop, &diff ); |
---|
109 | printf( "%" PRItime_t ":%ld %" PRItime_t ":%ld --> %" PRItime_t ":%ld\n", |
---|
110 | start.tv_sec, start.tv_nsec, |
---|
111 | stop.tv_sec, stop.tv_nsec, |
---|
112 | diff.tv_sec, diff.tv_nsec |
---|
113 | ); |
---|
114 | } |
---|
115 | |
---|
116 | /* |
---|
117 | * Iterate 10 times showing difference in Uptime with different counts |
---|
118 | */ |
---|
119 | printf( "\n10 iterations of getting Uptime with different loop values\n" ); |
---|
120 | for (index=1 ; index <=10 ; index++ ) { |
---|
121 | struct timespec start, stop; |
---|
122 | struct timespec diff; |
---|
123 | int j, max = (index * 10000); |
---|
124 | rtems_clock_get_uptime( &start ); |
---|
125 | for (j=0 ; j<max ; j++ ) |
---|
126 | dummy_function_empty_body_to_force_call(); |
---|
127 | rtems_clock_get_uptime( &stop ); |
---|
128 | |
---|
129 | subtract_em( &start, &stop, &diff ); |
---|
130 | printf( "loop of %d %" PRItime_t ":%ld %" PRItime_t ":%ld --> %" PRItime_t ":%ld\n", |
---|
131 | max, |
---|
132 | start.tv_sec, start.tv_nsec, |
---|
133 | stop.tv_sec, stop.tv_nsec, |
---|
134 | diff.tv_sec, diff.tv_nsec |
---|
135 | ); |
---|
136 | } |
---|
137 | |
---|
138 | sleep(1); |
---|
139 | |
---|
140 | puts( "*** END OF NANOSECOND CLOCK TEST ***" ); |
---|
141 | exit(0); |
---|
142 | } |
---|
143 | |
---|
144 | |
---|
145 | #include <bsp.h> /* for device driver prototypes */ |
---|
146 | |
---|
147 | #define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER |
---|
148 | #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER |
---|
149 | |
---|
150 | #define CONFIGURE_MICROSECONDS_PER_TICK 1000 |
---|
151 | #define CONFIGURE_MAXIMUM_TASKS 1 |
---|
152 | #define CONFIGURE_RTEMS_INIT_TASKS_TABLE |
---|
153 | |
---|
154 | #include <rtems/confdefs.h> |
---|
155 | |
---|