rtctest.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * Real Time Clock Driver Test/Example Program
  3. *
  4. * Compile with:
  5. * gcc -s -Wall -Wstrict-prototypes rtctest.c -o rtctest
  6. *
  7. * Copyright (C) 1996, Paul Gortmaker.
  8. *
  9. * Released under the GNU General Public License, version 2,
  10. * included herein by reference.
  11. *
  12. */
  13. #include <stdio.h>
  14. #include <linux/rtc.h>
  15. #include <sys/ioctl.h>
  16. #include <sys/time.h>
  17. #include <sys/types.h>
  18. #include <fcntl.h>
  19. #include <unistd.h>
  20. #include <stdlib.h>
  21. #include <errno.h>
  22. /*
  23. * This expects the new RTC class driver framework, working with
  24. * clocks that will often not be clones of what the PC-AT had.
  25. * Use the command line to specify another RTC if you need one.
  26. */
  27. static const char default_rtc[] = "/dev/rtc0";
  28. int main(int argc, char **argv)
  29. {
  30. int i, fd, retval, irqcount = 0;
  31. unsigned long tmp, data;
  32. struct rtc_time rtc_tm;
  33. const char *rtc = default_rtc;
  34. struct timeval start, end, diff;
  35. switch (argc) {
  36. case 2:
  37. rtc = argv[1];
  38. /* FALLTHROUGH */
  39. case 1:
  40. break;
  41. default:
  42. fprintf(stderr, "usage: rtctest [rtcdev]\n");
  43. return 1;
  44. }
  45. fd = open(rtc, O_RDONLY);
  46. if (fd == -1) {
  47. perror(rtc);
  48. exit(errno);
  49. }
  50. fprintf(stderr, "\n\t\t\tRTC Driver Test Example.\n\n");
  51. /* Turn on update interrupts (one per second) */
  52. retval = ioctl(fd, RTC_UIE_ON, 0);
  53. if (retval == -1) {
  54. if (errno == EINVAL) {
  55. fprintf(stderr,
  56. "\n...Update IRQs not supported.\n");
  57. goto test_READ;
  58. }
  59. perror("RTC_UIE_ON ioctl");
  60. exit(errno);
  61. }
  62. fprintf(stderr, "Counting 5 update (1/sec) interrupts from reading %s:",
  63. rtc);
  64. fflush(stderr);
  65. for (i=1; i<6; i++) {
  66. /* This read will block */
  67. retval = read(fd, &data, sizeof(unsigned long));
  68. if (retval == -1) {
  69. perror("read");
  70. exit(errno);
  71. }
  72. fprintf(stderr, " %d",i);
  73. fflush(stderr);
  74. irqcount++;
  75. }
  76. fprintf(stderr, "\nAgain, from using select(2) on /dev/rtc:");
  77. fflush(stderr);
  78. for (i=1; i<6; i++) {
  79. struct timeval tv = {5, 0}; /* 5 second timeout on select */
  80. fd_set readfds;
  81. FD_ZERO(&readfds);
  82. FD_SET(fd, &readfds);
  83. /* The select will wait until an RTC interrupt happens. */
  84. retval = select(fd+1, &readfds, NULL, NULL, &tv);
  85. if (retval == -1) {
  86. perror("select");
  87. exit(errno);
  88. }
  89. /* This read won't block unlike the select-less case above. */
  90. retval = read(fd, &data, sizeof(unsigned long));
  91. if (retval == -1) {
  92. perror("read");
  93. exit(errno);
  94. }
  95. fprintf(stderr, " %d",i);
  96. fflush(stderr);
  97. irqcount++;
  98. }
  99. /* Turn off update interrupts */
  100. retval = ioctl(fd, RTC_UIE_OFF, 0);
  101. if (retval == -1) {
  102. perror("RTC_UIE_OFF ioctl");
  103. exit(errno);
  104. }
  105. test_READ:
  106. /* Read the RTC time/date */
  107. retval = ioctl(fd, RTC_RD_TIME, &rtc_tm);
  108. if (retval == -1) {
  109. perror("RTC_RD_TIME ioctl");
  110. exit(errno);
  111. }
  112. fprintf(stderr, "\n\nCurrent RTC date/time is %d-%d-%d, %02d:%02d:%02d.\n",
  113. rtc_tm.tm_mday, rtc_tm.tm_mon + 1, rtc_tm.tm_year + 1900,
  114. rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec);
  115. /* Set the alarm to 5 sec in the future, and check for rollover */
  116. rtc_tm.tm_sec += 5;
  117. if (rtc_tm.tm_sec >= 60) {
  118. rtc_tm.tm_sec %= 60;
  119. rtc_tm.tm_min++;
  120. }
  121. if (rtc_tm.tm_min == 60) {
  122. rtc_tm.tm_min = 0;
  123. rtc_tm.tm_hour++;
  124. }
  125. if (rtc_tm.tm_hour == 24)
  126. rtc_tm.tm_hour = 0;
  127. retval = ioctl(fd, RTC_ALM_SET, &rtc_tm);
  128. if (retval == -1) {
  129. if (errno == EINVAL) {
  130. fprintf(stderr,
  131. "\n...Alarm IRQs not supported.\n");
  132. goto test_PIE;
  133. }
  134. perror("RTC_ALM_SET ioctl");
  135. exit(errno);
  136. }
  137. /* Read the current alarm settings */
  138. retval = ioctl(fd, RTC_ALM_READ, &rtc_tm);
  139. if (retval == -1) {
  140. perror("RTC_ALM_READ ioctl");
  141. exit(errno);
  142. }
  143. fprintf(stderr, "Alarm time now set to %02d:%02d:%02d.\n",
  144. rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec);
  145. /* Enable alarm interrupts */
  146. retval = ioctl(fd, RTC_AIE_ON, 0);
  147. if (retval == -1) {
  148. if (errno == EINVAL) {
  149. fprintf(stderr,
  150. "\n...Alarm IRQs not supported.\n");
  151. goto test_PIE;
  152. }
  153. perror("RTC_AIE_ON ioctl");
  154. exit(errno);
  155. }
  156. fprintf(stderr, "Waiting 5 seconds for alarm...");
  157. fflush(stderr);
  158. /* This blocks until the alarm ring causes an interrupt */
  159. retval = read(fd, &data, sizeof(unsigned long));
  160. if (retval == -1) {
  161. perror("read");
  162. exit(errno);
  163. }
  164. irqcount++;
  165. fprintf(stderr, " okay. Alarm rang.\n");
  166. /* Disable alarm interrupts */
  167. retval = ioctl(fd, RTC_AIE_OFF, 0);
  168. if (retval == -1) {
  169. perror("RTC_AIE_OFF ioctl");
  170. exit(errno);
  171. }
  172. test_PIE:
  173. /* Read periodic IRQ rate */
  174. retval = ioctl(fd, RTC_IRQP_READ, &tmp);
  175. if (retval == -1) {
  176. /* not all RTCs support periodic IRQs */
  177. if (errno == EINVAL) {
  178. fprintf(stderr, "\nNo periodic IRQ support\n");
  179. goto done;
  180. }
  181. perror("RTC_IRQP_READ ioctl");
  182. exit(errno);
  183. }
  184. fprintf(stderr, "\nPeriodic IRQ rate is %ldHz.\n", tmp);
  185. fprintf(stderr, "Counting 20 interrupts at:");
  186. fflush(stderr);
  187. /* The frequencies 128Hz, 256Hz, ... 8192Hz are only allowed for root. */
  188. for (tmp=2; tmp<=64; tmp*=2) {
  189. retval = ioctl(fd, RTC_IRQP_SET, tmp);
  190. if (retval == -1) {
  191. /* not all RTCs can change their periodic IRQ rate */
  192. if (errno == EINVAL) {
  193. fprintf(stderr,
  194. "\n...Periodic IRQ rate is fixed\n");
  195. goto done;
  196. }
  197. perror("RTC_IRQP_SET ioctl");
  198. exit(errno);
  199. }
  200. fprintf(stderr, "\n%ldHz:\t", tmp);
  201. fflush(stderr);
  202. /* Enable periodic interrupts */
  203. retval = ioctl(fd, RTC_PIE_ON, 0);
  204. if (retval == -1) {
  205. perror("RTC_PIE_ON ioctl");
  206. exit(errno);
  207. }
  208. for (i=1; i<21; i++) {
  209. gettimeofday(&start, NULL);
  210. /* This blocks */
  211. retval = read(fd, &data, sizeof(unsigned long));
  212. if (retval == -1) {
  213. perror("read");
  214. exit(errno);
  215. }
  216. gettimeofday(&end, NULL);
  217. timersub(&end, &start, &diff);
  218. if (diff.tv_sec > 0 ||
  219. diff.tv_usec > ((1000000L / tmp) * 1.10)) {
  220. fprintf(stderr, "\nPIE delta error: %ld.%06ld should be close to 0.%06ld\n",
  221. diff.tv_sec, diff.tv_usec,
  222. (1000000L / tmp));
  223. fflush(stdout);
  224. exit(-1);
  225. }
  226. fprintf(stderr, " %d",i);
  227. fflush(stderr);
  228. irqcount++;
  229. }
  230. /* Disable periodic interrupts */
  231. retval = ioctl(fd, RTC_PIE_OFF, 0);
  232. if (retval == -1) {
  233. perror("RTC_PIE_OFF ioctl");
  234. exit(errno);
  235. }
  236. }
  237. done:
  238. fprintf(stderr, "\n\n\t\t\t *** Test complete ***\n");
  239. close(fd);
  240. return 0;
  241. }