1
2
3
4
5
6
7
8
9
10
11
12
13
14#include <stdio.h>
15#include <linux/rtc.h>
16#include <sys/ioctl.h>
17#include <sys/time.h>
18#include <sys/types.h>
19#include <fcntl.h>
20#include <unistd.h>
21#include <stdlib.h>
22#include <errno.h>
23
24
25
26
27
28
29
30static const char default_rtc[] = "/dev/rtc0";
31
32
33int main(int argc, char **argv)
34{
35 int i, fd, retval, irqcount = 0;
36 unsigned long tmp, data;
37 struct rtc_time rtc_tm;
38 const char *rtc = default_rtc;
39 struct timeval start, end, diff;
40
41 switch (argc) {
42 case 2:
43 rtc = argv[1];
44
45 case 1:
46 break;
47 default:
48 fprintf(stderr, "usage: rtctest [rtcdev]\n");
49 return 1;
50 }
51
52 fd = open(rtc, O_RDONLY);
53
54 if (fd == -1) {
55 perror(rtc);
56 exit(errno);
57 }
58
59 fprintf(stderr, "\n\t\t\tRTC Driver Test Example.\n\n");
60
61
62 retval = ioctl(fd, RTC_UIE_ON, 0);
63 if (retval == -1) {
64 if (errno == EINVAL) {
65 fprintf(stderr,
66 "\n...Update IRQs not supported.\n");
67 goto test_READ;
68 }
69 perror("RTC_UIE_ON ioctl");
70 exit(errno);
71 }
72
73 fprintf(stderr, "Counting 5 update (1/sec) interrupts from reading %s:",
74 rtc);
75 fflush(stderr);
76 for (i=1; i<6; i++) {
77
78 retval = read(fd, &data, sizeof(unsigned long));
79 if (retval == -1) {
80 perror("read");
81 exit(errno);
82 }
83 fprintf(stderr, " %d",i);
84 fflush(stderr);
85 irqcount++;
86 }
87
88 fprintf(stderr, "\nAgain, from using select(2) on /dev/rtc:");
89 fflush(stderr);
90 for (i=1; i<6; i++) {
91 struct timeval tv = {5, 0};
92 fd_set readfds;
93
94 FD_ZERO(&readfds);
95 FD_SET(fd, &readfds);
96
97 retval = select(fd+1, &readfds, NULL, NULL, &tv);
98 if (retval == -1) {
99 perror("select");
100 exit(errno);
101 }
102
103 retval = read(fd, &data, sizeof(unsigned long));
104 if (retval == -1) {
105 perror("read");
106 exit(errno);
107 }
108 fprintf(stderr, " %d",i);
109 fflush(stderr);
110 irqcount++;
111 }
112
113
114 retval = ioctl(fd, RTC_UIE_OFF, 0);
115 if (retval == -1) {
116 perror("RTC_UIE_OFF ioctl");
117 exit(errno);
118 }
119
120test_READ:
121
122 retval = ioctl(fd, RTC_RD_TIME, &rtc_tm);
123 if (retval == -1) {
124 perror("RTC_RD_TIME ioctl");
125 exit(errno);
126 }
127
128 fprintf(stderr, "\n\nCurrent RTC date/time is %d-%d-%d, %02d:%02d:%02d.\n",
129 rtc_tm.tm_mday, rtc_tm.tm_mon + 1, rtc_tm.tm_year + 1900,
130 rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec);
131
132
133 rtc_tm.tm_sec += 5;
134 if (rtc_tm.tm_sec >= 60) {
135 rtc_tm.tm_sec %= 60;
136 rtc_tm.tm_min++;
137 }
138 if (rtc_tm.tm_min == 60) {
139 rtc_tm.tm_min = 0;
140 rtc_tm.tm_hour++;
141 }
142 if (rtc_tm.tm_hour == 24)
143 rtc_tm.tm_hour = 0;
144
145 retval = ioctl(fd, RTC_ALM_SET, &rtc_tm);
146 if (retval == -1) {
147 if (errno == EINVAL) {
148 fprintf(stderr,
149 "\n...Alarm IRQs not supported.\n");
150 goto test_PIE;
151 }
152
153 perror("RTC_ALM_SET ioctl");
154 exit(errno);
155 }
156
157
158 retval = ioctl(fd, RTC_ALM_READ, &rtc_tm);
159 if (retval == -1) {
160 perror("RTC_ALM_READ ioctl");
161 exit(errno);
162 }
163
164 fprintf(stderr, "Alarm time now set to %02d:%02d:%02d.\n",
165 rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec);
166
167
168 retval = ioctl(fd, RTC_AIE_ON, 0);
169 if (retval == -1) {
170 if (errno == EINVAL) {
171 fprintf(stderr,
172 "\n...Alarm IRQs not supported.\n");
173 goto test_PIE;
174 }
175
176 perror("RTC_AIE_ON ioctl");
177 exit(errno);
178 }
179
180 fprintf(stderr, "Waiting 5 seconds for alarm...");
181 fflush(stderr);
182
183 retval = read(fd, &data, sizeof(unsigned long));
184 if (retval == -1) {
185 perror("read");
186 exit(errno);
187 }
188 irqcount++;
189 fprintf(stderr, " okay. Alarm rang.\n");
190
191
192 retval = ioctl(fd, RTC_AIE_OFF, 0);
193 if (retval == -1) {
194 perror("RTC_AIE_OFF ioctl");
195 exit(errno);
196 }
197
198test_PIE:
199
200 retval = ioctl(fd, RTC_IRQP_READ, &tmp);
201 if (retval == -1) {
202
203 if (errno == EINVAL) {
204 fprintf(stderr, "\nNo periodic IRQ support\n");
205 goto done;
206 }
207 perror("RTC_IRQP_READ ioctl");
208 exit(errno);
209 }
210 fprintf(stderr, "\nPeriodic IRQ rate is %ldHz.\n", tmp);
211
212 fprintf(stderr, "Counting 20 interrupts at:");
213 fflush(stderr);
214
215
216 for (tmp=2; tmp<=64; tmp*=2) {
217
218 retval = ioctl(fd, RTC_IRQP_SET, tmp);
219 if (retval == -1) {
220
221 if (errno == EINVAL) {
222 fprintf(stderr,
223 "\n...Periodic IRQ rate is fixed\n");
224 goto done;
225 }
226 perror("RTC_IRQP_SET ioctl");
227 exit(errno);
228 }
229
230 fprintf(stderr, "\n%ldHz:\t", tmp);
231 fflush(stderr);
232
233
234 retval = ioctl(fd, RTC_PIE_ON, 0);
235 if (retval == -1) {
236 perror("RTC_PIE_ON ioctl");
237 exit(errno);
238 }
239
240 for (i=1; i<21; i++) {
241 gettimeofday(&start, NULL);
242
243 retval = read(fd, &data, sizeof(unsigned long));
244 if (retval == -1) {
245 perror("read");
246 exit(errno);
247 }
248 gettimeofday(&end, NULL);
249 timersub(&end, &start, &diff);
250 if (diff.tv_sec > 0 ||
251 diff.tv_usec > ((1000000L / tmp) * 1.10)) {
252 fprintf(stderr, "\nPIE delta error: %ld.%06ld should be close to 0.%06ld\n",
253 diff.tv_sec, diff.tv_usec,
254 (1000000L / tmp));
255 fflush(stdout);
256 exit(-1);
257 }
258
259 fprintf(stderr, " %d",i);
260 fflush(stderr);
261 irqcount++;
262 }
263
264
265 retval = ioctl(fd, RTC_PIE_OFF, 0);
266 if (retval == -1) {
267 perror("RTC_PIE_OFF ioctl");
268 exit(errno);
269 }
270 }
271
272done:
273 fprintf(stderr, "\n\n\t\t\t *** Test complete ***\n");
274
275 close(fd);
276
277 return 0;
278}
279