//* RTC driver test code *

// // Waring: // This program might be (c) http://download.analog.com/27516/trackeritem/1/2/4/1244/rtc_test.c // However, no copyright statement is posted on the Web site mentioned above, credit duly given // #include <linux/rtc.h> #include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <unistd.h> #include <sys/ioctl.h> int main() { int rtc_fd, fd1; unsigned long data; int ret, i; struct rtc_time rtc_tm; fprintf(stdout,"====== RTC Test ====\n"); fprintf(stdout,"0. open and release\n"); rtc_fd = open("/dev/rtc", O_RDONLY,0); if (rtc_fd == -1) { fprintf(stdout,"/dev/rtc open error\n"); exit(1); } else fprintf(stdout,"open success fd = %d \n",rtc_fd); fprintf(stdout,"1. ioctl RTC_UIE_ON\r\n"); ret = ioctl(rtc_fd, RTC_UIE_ON, 0); if (ret == -1) { fprintf(stdout,"rtc ioctl RTC_UIE_ON error\r\n"); } fprintf(stdout,"2. RTC read 6 times\r\n"); for( i = 1; i <2; i++){ /* This read will block */ ret = read(rtc_fd, &data, sizeof(unsigned long)); if (ret == -1) { fprintf(stdout,"rtc read error\r\n"); } fprintf(stdout,"RTC read %d\r\n", i); } fprintf(stdout,stderr, "%s", "AFTER READ"); fflush(stderr); fprintf(stdout,"3. ioctl RTC_UIE_OFF\r\n"); ret = ioctl(rtc_fd, RTC_UIE_OFF, 0); if (ret == -1) { fprintf(stdout,"rtc ioctl RTC_UIE_OFF error\r\n"); } fprintf(stdout,"4. Get RTC Time\r\n"); ret = ioctl(rtc_fd, RTC_RD_TIME, &rtc_tm); if (ret == -1) { fprintf(stdout,"rtc ioctl RTC_RD_TIME error\r\n"); } fprintf(stdout,"Current RTC date/time is %d-%d-%d, %02d:%02d:%02d.\r\n", rtc_tm.tm_mday, rtc_tm.tm_mon + 1, rtc_tm.tm_year, rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec); /* Set the RTC time/date */ rtc_tm.tm_mday = 31; rtc_tm.tm_mon = 4;/* for example Sep. 8 */ rtc_tm.tm_year = 104; rtc_tm.tm_hour = 2; rtc_tm.tm_min = 30; rtc_tm.tm_sec = 0; fprintf(stdout,"5. Set RTC Time\r\n"); ret = ioctl(rtc_fd, RTC_SET_TIME, &rtc_tm); if (ret == -1) { fprintf(stdout,"rtc ioctl RTC_SET_TIME error\r\n"); } fprintf(stdout,"Set Current RTC date/time to %d-%d-%d, %02d:%02d:%02d.\r\n", rtc_tm.tm_mday, rtc_tm.tm_mon + 1, rtc_tm.tm_year, rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec); fprintf(stdout,"Get RTC time\r\n"); ret = ioctl(rtc_fd, RTC_RD_TIME, &rtc_tm); if (ret == -1) { fprintf(stdout,"rtc ioctl RTC_RD_TIME error\r\n"); } fprintf(stdout,"Current RTC date/time is %d-%d-%d, %02d:%02d:%02d.\r\n", rtc_tm.tm_mday, rtc_tm.tm_mon + 1, rtc_tm.tm_year, rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec); rtc_tm.tm_sec += 50; if (rtc_tm.tm_sec >= 60) { rtc_tm.tm_sec %= 60; rtc_tm.tm_min++; } if (rtc_tm.tm_min == 60) { rtc_tm.tm_min = 0; rtc_tm.tm_hour++; } if (rtc_tm.tm_hour == 24) rtc_tm.tm_hour = 0; fprintf(stdout,"6. Set alarm Time\r\n"); ret = ioctl(rtc_fd, RTC_ALM_SET, &rtc_tm); if (ret == -1) { fprintf(stdout,"rtc ioctl RTC_ALM_SET error\r\n"); } /* Read the current alarm settings */ fprintf(stdout,"7. Get alarm Time\r\n"); ret = ioctl(rtc_fd, RTC_ALM_READ, &rtc_tm); if (ret == -1) { fprintf(stdout,"rtc ioctl RTC_ALM_READ error\r\n"); } fprintf(stdout,"Alarm time now set to %02d:%02d:%02d.\r\n", rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec); /* Enable alarm interrupts */ ret = ioctl(rtc_fd, RTC_AIE_ON, 0); if (ret == -1) { fprintf(stdout,"rtc ioctl RTC_ALE_ON error\r\n"); } fprintf(stdout,"Waiting 50 seconds for alarm...\r\n"); /* This blocks until the alarm ring causes an interrupt */ ret = read(rtc_fd, &data, sizeof(unsigned long)); if (ret == -1) { fprintf(stdout,"rtc read errot\r\n"); } fprintf(stdout," Okay. Alarm rang.\r\n"); ret = ioctl(rtc_fd, RTC_RD_TIME, &rtc_tm); if (ret == -1) { fprintf(stdout,"rtc ioctl RTC_RD_TIME error\r\n"); } fprintf(stdout,"Current RTC date/time is %d-%d-%d, %02d:%02d:%02d.\r\n", rtc_tm.tm_mday, rtc_tm.tm_mon + 1, rtc_tm.tm_year, rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec); /* Disable alarm interrupts */ fprintf(stdout,"8. ioctl RTC_AIE_OFF\r\n"); ret = ioctl(rtc_fd, RTC_AIE_OFF, 0); if (ret == -1) { fprintf(stdout,"ioctl RTC_ALE_OFF error\r\n"); } #if 0 fprintf(stdout,"9. ioctl RTC_SWCNT_ON\r\n"); ret = ioctl(rtc_fd, RTC_SWCNT_ON, 0); if (ret == -1) { fprintf(stdout,"ioctl RTC_SWCNT_ON error\r\n"); } ret = ioctl(rtc_fd, RTC_RD_TIME, &rtc_tm); if (ret == -1) { fprintf(stdout,"rtc ioctl RTC_RD_TIME error\r\n"); } fprintf(stdout,"Current RTC date/time is %d-%d-%d, %02d:%02d:%02d.\r\n", rtc_tm.tm_mday, rtc_tm.tm_mon + 1, rtc_tm.tm_year, rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec); fprintf(stdout,"10. ioctl RTC_SWCNT_SET\r\n"); fprintf(stdout," Enabling the SWCNT interrupt\n"); ret = ioctl(rtc_fd, RTC_SWCNT_SET, 10); if (ret == -1) { fprintf(stdout,"ioctl RTC_SWCNT_SET error\r\n"); } fprintf(stdout,"\n Before Reading ....\n"); ret = read(rtc_fd, &data, sizeof(unsigned long)); if (ret == -1) { fprintf(stdout,"rtc read errot\r\n"); } fprintf(stdout,stderr, "\n After Reading ....\n"); fflush(stderr); ret = ioctl(rtc_fd, RTC_RD_TIME, &rtc_tm); if (ret == -1) { fprintf(stdout,"rtc ioctl RTC_RD_TIME error\r\n"); } fprintf(stdout,"Current RTC date/time is %d-%d-%d, %02d:%02d:%02d.\r\n", rtc_tm.tm_mday, rtc_tm.tm_mon + 1, rtc_tm.tm_year, rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec); fprintf(stdout,"11. ioctl RTC_SWCNT_OFF\r\n"); ret = ioctl(rtc_fd, RTC_SWCNT_OFF, 0); if (ret == -1) { fprintf(stdout,"ioctl RTC_SWCNT_OFF error\r\n"); } fprintf(stdout,"12. ioctl RTC_EPOCH_READ\r\n"); ret = ioctl(rtc_fd, RTC_EPOCH_READ, &data); if (ret == -1) { fprintf(stdout,"ioctl RTC_EPOCH_READ error\r\n"); } fprintf(stdout,"Current epoch is %ld\r\n",data); fprintf(stdout,"13. ioctl RTC_EPOCH_SET\r\n"); ret = ioctl(rtc_fd, RTC_EPOCH_SET, 2310); if (ret == -1) { fprintf(stdout,"ioctl RTC_EPOCH_SET error\r\n"); } ret = ioctl(rtc_fd, RTC_RD_TIME, &rtc_tm); if (ret == -1) { fprintf(stdout,"rtc ioctl RTC_RD_TIME error\r\n"); } fprintf(stdout,"Current RTC date/time is %d-%d-%d, %02d:%02d:%02d.\r\n", rtc_tm.tm_mday, rtc_tm.tm_mon + 1, rtc_tm.tm_year, rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec); #endif close(rtc_fd); return 0; }