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>
26 * This expects the new RTC class driver framework, working with
27 * clocks that will often not be clones of what the PC-AT had.
28 * Use the command line to specify another RTC if you need one.
30 static const char default_rtc[] = "/dev/rtc0";
33 int main(int argc, char **argv)
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;
48 fprintf(stderr, "usage: rtctest [rtcdev]\n");
52 fd = open(rtc, O_RDONLY);
59 fprintf(stderr, "\n\t\t\tRTC Driver Test Example.\n\n");
61 /* Turn on update interrupts (one per second) */
62 retval = ioctl(fd, RTC_UIE_ON, 0);
64 if (errno == EINVAL) {
66 "\n...Update IRQs not supported.\n");
69 perror("RTC_UIE_ON ioctl");
73 fprintf(stderr, "Counting 5 update (1/sec) interrupts from reading %s:",
77 /* This read will block */
78 retval = read(fd, &data, sizeof(unsigned long));
83 fprintf(stderr, " %d",i);
88 fprintf(stderr, "\nAgain, from using select(2) on /dev/rtc:");
91 struct timeval tv = {5, 0}; /* 5 second timeout on select */
96 /* The select will wait until an RTC interrupt happens. */
97 retval = select(fd+1, &readfds, NULL, NULL, &tv);
102 /* This read won't block unlike the select-less case above. */
103 retval = read(fd, &data, sizeof(unsigned long));
108 fprintf(stderr, " %d",i);
113 /* Turn off update interrupts */
114 retval = ioctl(fd, RTC_UIE_OFF, 0);
116 perror("RTC_UIE_OFF ioctl");
121 /* Read the RTC time/date */
122 retval = ioctl(fd, RTC_RD_TIME, &rtc_tm);
124 perror("RTC_RD_TIME ioctl");
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);
132 /* Set the alarm to 5 sec in the future, and check for rollover */
134 if (rtc_tm.tm_sec >= 60) {
138 if (rtc_tm.tm_min == 60) {
142 if (rtc_tm.tm_hour == 24)
145 retval = ioctl(fd, RTC_ALM_SET, &rtc_tm);
147 if (errno == EINVAL) {
149 "\n...Alarm IRQs not supported.\n");
153 perror("RTC_ALM_SET ioctl");
157 /* Read the current alarm settings */
158 retval = ioctl(fd, RTC_ALM_READ, &rtc_tm);
160 perror("RTC_ALM_READ ioctl");
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);
167 /* Enable alarm interrupts */
168 retval = ioctl(fd, RTC_AIE_ON, 0);
170 if (errno == EINVAL) {
172 "\n...Alarm IRQs not supported.\n");
176 perror("RTC_AIE_ON ioctl");
180 fprintf(stderr, "Waiting 5 seconds for alarm...");
182 /* This blocks until the alarm ring causes an interrupt */
183 retval = read(fd, &data, sizeof(unsigned long));
189 fprintf(stderr, " okay. Alarm rang.\n");
191 /* Disable alarm interrupts */
192 retval = ioctl(fd, RTC_AIE_OFF, 0);
194 perror("RTC_AIE_OFF ioctl");
199 /* Read periodic IRQ rate */
200 retval = ioctl(fd, RTC_IRQP_READ, &tmp);
202 /* not all RTCs support periodic IRQs */
203 if (errno == EINVAL) {
204 fprintf(stderr, "\nNo periodic IRQ support\n");
207 perror("RTC_IRQP_READ ioctl");
210 fprintf(stderr, "\nPeriodic IRQ rate is %ldHz.\n", tmp);
212 fprintf(stderr, "Counting 20 interrupts at:");
215 /* The frequencies 128Hz, 256Hz, ... 8192Hz are only allowed for root. */
216 for (tmp=2; tmp<=64; tmp*=2) {
218 retval = ioctl(fd, RTC_IRQP_SET, tmp);
220 /* not all RTCs can change their periodic IRQ rate */
221 if (errno == EINVAL) {
223 "\n...Periodic IRQ rate is fixed\n");
226 perror("RTC_IRQP_SET ioctl");
230 fprintf(stderr, "\n%ldHz:\t", tmp);
233 /* Enable periodic interrupts */
234 retval = ioctl(fd, RTC_PIE_ON, 0);
236 perror("RTC_PIE_ON ioctl");
240 for (i=1; i<21; i++) {
241 gettimeofday(&start, NULL);
243 retval = read(fd, &data, sizeof(unsigned long));
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,
259 fprintf(stderr, " %d",i);
264 /* Disable periodic interrupts */
265 retval = ioctl(fd, RTC_PIE_OFF, 0);
267 perror("RTC_PIE_OFF ioctl");
273 fprintf(stderr, "\n\n\t\t\t *** Test complete ***\n");