2 * Real Time Clock Driver Test/Example Program
5 * gcc -s -Wall -Wstrict-prototypes rtctest.c -o rtctest
7 * Copyright (C) 1996, Paul Gortmaker.
9 * Released under the GNU General Public License, version 2,
10 * included herein by reference.
15 #include <linux/rtc.h>
16 #include <sys/ioctl.h>
18 #include <sys/types.h>
25 # define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
29 * This expects the new RTC class driver framework, working with
30 * clocks that will often not be clones of what the PC-AT had.
31 * Use the command line to specify another RTC if you need one.
33 static const char default_rtc
[] = "/dev/rtc0";
35 static struct rtc_time cutoff_dates
[] = {
37 .tm_year
= 70, /* 1970 -1900 */
40 /* signed time_t 19/01/2038 3:14:08 */
50 .tm_year
= 199, /* 2099 -1900 */
54 .tm_year
= 200, /* 2100 -1900 */
57 /* unsigned time_t 07/02/2106 7:28:15*/
68 /* signed time on 64bit in nanoseconds 12/04/2262 01:47:16*/
75 .tm_year
= 362, /* 2262 -1900 */
81 static int compare_dates(struct rtc_time
*a
, struct rtc_time
*b
)
83 if (a
->tm_year
!= b
->tm_year
||
84 a
->tm_mon
!= b
->tm_mon
||
85 a
->tm_mday
!= b
->tm_mday
||
86 a
->tm_hour
!= b
->tm_hour
||
87 a
->tm_min
!= b
->tm_min
||
88 ((b
->tm_sec
- a
->tm_sec
) > 1))
94 int main(int argc
, char **argv
)
96 int i
, fd
, retval
, irqcount
= 0, dangerous
= 0;
97 unsigned long tmp
, data
;
98 struct rtc_time rtc_tm
;
99 const char *rtc
= default_rtc
;
100 struct timeval start
, end
, diff
;
112 fprintf(stderr
, "usage: rtctest [rtcdev] [d]\n");
116 fd
= open(rtc
, O_RDONLY
);
123 fprintf(stderr
, "\n\t\t\tRTC Driver Test Example.\n\n");
125 /* Turn on update interrupts (one per second) */
126 retval
= ioctl(fd
, RTC_UIE_ON
, 0);
128 if (errno
== EINVAL
) {
130 "\n...Update IRQs not supported.\n");
133 perror("RTC_UIE_ON ioctl");
137 fprintf(stderr
, "Counting 5 update (1/sec) interrupts from reading %s:",
140 for (i
=1; i
<6; i
++) {
141 /* This read will block */
142 retval
= read(fd
, &data
, sizeof(unsigned long));
147 fprintf(stderr
, " %d",i
);
152 fprintf(stderr
, "\nAgain, from using select(2) on /dev/rtc:");
154 for (i
=1; i
<6; i
++) {
155 struct timeval tv
= {5, 0}; /* 5 second timeout on select */
159 FD_SET(fd
, &readfds
);
160 /* The select will wait until an RTC interrupt happens. */
161 retval
= select(fd
+1, &readfds
, NULL
, NULL
, &tv
);
166 /* This read won't block unlike the select-less case above. */
167 retval
= read(fd
, &data
, sizeof(unsigned long));
172 fprintf(stderr
, " %d",i
);
177 /* Turn off update interrupts */
178 retval
= ioctl(fd
, RTC_UIE_OFF
, 0);
180 perror("RTC_UIE_OFF ioctl");
185 /* Read the RTC time/date */
186 retval
= ioctl(fd
, RTC_RD_TIME
, &rtc_tm
);
188 perror("RTC_RD_TIME ioctl");
192 fprintf(stderr
, "\n\nCurrent RTC date/time is %d-%d-%d, %02d:%02d:%02d.\n",
193 rtc_tm
.tm_mday
, rtc_tm
.tm_mon
+ 1, rtc_tm
.tm_year
+ 1900,
194 rtc_tm
.tm_hour
, rtc_tm
.tm_min
, rtc_tm
.tm_sec
);
196 /* Set the alarm to 5 sec in the future, and check for rollover */
198 if (rtc_tm
.tm_sec
>= 60) {
202 if (rtc_tm
.tm_min
== 60) {
206 if (rtc_tm
.tm_hour
== 24)
209 retval
= ioctl(fd
, RTC_ALM_SET
, &rtc_tm
);
211 if (errno
== EINVAL
) {
213 "\n...Alarm IRQs not supported.\n");
217 perror("RTC_ALM_SET ioctl");
221 /* Read the current alarm settings */
222 retval
= ioctl(fd
, RTC_ALM_READ
, &rtc_tm
);
224 if (errno
== EINVAL
) {
226 "\n...EINVAL reading current alarm setting.\n");
229 perror("RTC_ALM_READ ioctl");
233 fprintf(stderr
, "Alarm time now set to %02d:%02d:%02d.\n",
234 rtc_tm
.tm_hour
, rtc_tm
.tm_min
, rtc_tm
.tm_sec
);
236 /* Enable alarm interrupts */
237 retval
= ioctl(fd
, RTC_AIE_ON
, 0);
239 if (errno
== EINVAL
|| errno
== EIO
) {
241 "\n...Alarm IRQs not supported.\n");
245 perror("RTC_AIE_ON ioctl");
249 fprintf(stderr
, "Waiting 5 seconds for alarm...");
251 /* This blocks until the alarm ring causes an interrupt */
252 retval
= read(fd
, &data
, sizeof(unsigned long));
258 fprintf(stderr
, " okay. Alarm rang.\n");
260 /* Disable alarm interrupts */
261 retval
= ioctl(fd
, RTC_AIE_OFF
, 0);
263 perror("RTC_AIE_OFF ioctl");
268 /* Read periodic IRQ rate */
269 retval
= ioctl(fd
, RTC_IRQP_READ
, &tmp
);
271 /* not all RTCs support periodic IRQs */
272 if (errno
== EINVAL
) {
273 fprintf(stderr
, "\nNo periodic IRQ support\n");
276 perror("RTC_IRQP_READ ioctl");
279 fprintf(stderr
, "\nPeriodic IRQ rate is %ldHz.\n", tmp
);
281 fprintf(stderr
, "Counting 20 interrupts at:");
284 /* The frequencies 128Hz, 256Hz, ... 8192Hz are only allowed for root. */
285 for (tmp
=2; tmp
<=64; tmp
*=2) {
287 retval
= ioctl(fd
, RTC_IRQP_SET
, tmp
);
289 /* not all RTCs can change their periodic IRQ rate */
290 if (errno
== EINVAL
) {
292 "\n...Periodic IRQ rate is fixed\n");
295 perror("RTC_IRQP_SET ioctl");
299 fprintf(stderr
, "\n%ldHz:\t", tmp
);
302 /* Enable periodic interrupts */
303 retval
= ioctl(fd
, RTC_PIE_ON
, 0);
305 perror("RTC_PIE_ON ioctl");
309 for (i
=1; i
<21; i
++) {
310 gettimeofday(&start
, NULL
);
312 retval
= read(fd
, &data
, sizeof(unsigned long));
317 gettimeofday(&end
, NULL
);
318 timersub(&end
, &start
, &diff
);
319 if (diff
.tv_sec
> 0 ||
320 diff
.tv_usec
> ((1000000L / tmp
) * 1.10)) {
321 fprintf(stderr
, "\nPIE delta error: %ld.%06ld should be close to 0.%06ld\n",
322 diff
.tv_sec
, diff
.tv_usec
,
328 fprintf(stderr
, " %d",i
);
333 /* Disable periodic interrupts */
334 retval
= ioctl(fd
, RTC_PIE_OFF
, 0);
336 perror("RTC_PIE_OFF ioctl");
345 fprintf(stderr
, "\nTesting problematic dates\n");
347 for (i
= 0; i
< ARRAY_SIZE(cutoff_dates
); i
++) {
348 struct rtc_time current
;
350 /* Write the new date in RTC */
351 retval
= ioctl(fd
, RTC_SET_TIME
, &cutoff_dates
[i
]);
353 perror("RTC_SET_TIME ioctl");
359 retval
= ioctl(fd
, RTC_RD_TIME
, ¤t
);
361 perror("RTC_RD_TIME ioctl");
365 if(compare_dates(&cutoff_dates
[i
], ¤t
)) {
366 fprintf(stderr
,"Setting date %d failed\n",
367 cutoff_dates
[i
].tm_year
+ 1900);
371 cutoff_dates
[i
].tm_sec
+= 5;
373 /* Write the new alarm in RTC */
374 retval
= ioctl(fd
, RTC_ALM_SET
, &cutoff_dates
[i
]);
376 perror("RTC_ALM_SET ioctl");
382 retval
= ioctl(fd
, RTC_ALM_READ
, ¤t
);
384 perror("RTC_ALM_READ ioctl");
388 if(compare_dates(&cutoff_dates
[i
], ¤t
)) {
389 fprintf(stderr
,"Setting alarm %d failed\n",
390 cutoff_dates
[i
].tm_year
+ 1900);
394 fprintf(stderr
, "Setting year %d is OK \n",
395 cutoff_dates
[i
].tm_year
+ 1900);
398 fprintf(stderr
, "\n\n\t\t\t *** Test complete ***\n");