STM32mp1内部已经集成了RTC模块,所以我们操作起来就非常方便了。在嵌入式系统中,RTC还是使用的非常广泛的。 STM32MP1系列芯片已经内置RTC功能,可以直接配置使用,需要硬件连接RTC的备份电池。Linux kernel代码中启用RTC功能就可以使用了。 1.连接到Cortex-A7 GIC的安全中断,未在OpenSTLinux发行版中使用。 这里有两个步骤非常关键: 2. 设备与驱动成功匹配; 等待Linux启动完成后,我们可以在uart console中查看TRC是否正常工作:
- root@pangu:~# dmesg| grep rtc
- [ 1.859046] stm32_rtc 5c004000.rtc: rtc core: registered 5c004000.rtc as rtc0
- [ 1.865008] stm32_rtc 5c004000.rtc: Date/time must be initialized
- [ 1.870915] stm32_rtc 5c004000.rtc: registered rev:1.2
- [ 2.327185] stm32_rtc 5c004000.rtc: setting system clock to 2000-01-01 05:19:06 UTC (946703946)
- root@pangu:~# ls -la /dev/rtc*
- lrwxrwxrwx 1 root root 4 Jul 16 06:58 /dev/rtc -> rtc0
- crw------- 1 root root 253, 0 Jul 16 06:58 /dev/rtc0
复制代码
此时,我们就可以编写应用程序。
应用程先贴一个demo上来,主要功能就是设置和读取内核态的RTC硬件时间。
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
-
- static const char default_rtc[] = "/dev/rtc0";
- static char *program_name;
- /**
- * @brief: print help message
- */
- static void help(void)
- {
- fprintf(stderr,
- "tUsage: %s [OPTION]...n"
- "t-h,--help helpn"
- "t-s,--set set date/time given with you.ntinput format:./rtc_test [hour] [minute] [second] [year] [month] [day]n"
- "t-r,--show read hardware clock and print resultn"
- "n", program_name);
- }
-
- /**
- * @brief: read RTC date and time
- * @Param: fd: the file descriptor of rtc device
- */
- void rtc_read_time(int fd)
- {
- int retval;
- struct rtc_time rtc_tm;
-
- /*read the RTC time/date*/
- retval = ioctl(fd, RTC_RD_TIME, &rtc_tm);
- if (retval == -1) {
- perror("RTC_RD_TIME ioctl");
- exit(errno);
- }
- fprintf(stderr, "ntCurrent RTC date/time is %d-%d-%d, %02d:%02d:%02d.nn",
- rtc_tm.tm_mday, rtc_tm.tm_mon + 1, rtc_tm.tm_year + 1900,
- rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec);
-
- }
- /**
- * @brief: set rtc date and time
- * @Param: fd: the file descriptor of rtc device
- * @Param: hour: the hour to set
- * @Param: minute: the minute to set
- * @Param: second: the hour to set
- * @Param: year: the year to set
- * @Param: month: the month to set
- * @Param: day: the day to set
- */
- void rtc_set_time(int fd, int hour, int minute, int second, int year, int month, int day)
- {
- int retval;
- struct rtc_time rtc_tm;
-
- rtc_tm.tm_mday = day;
- rtc_tm.tm_mon = month - 1;
- rtc_tm.tm_year = year - 1900;
- rtc_tm.tm_hour = hour;
- rtc_tm.tm_min = minute;
- rtc_tm.tm_wday = rtc_tm.tm_yday = rtc_tm.tm_isdst = 0;
- rtc_tm.tm_sec = second;
- /*set the RTC time/date*/
- retval = ioctl(fd, RTC_SET_TIME, &rtc_tm);
- if (retval == -1) {
- perror("RTC_SET_TIME ioctl");
- exit(errno);
- }
-
- fprintf(stderr, "nttdate/time is updated to: %d-%d-%d, %02d:%02d:%02d.nn",
- rtc_tm.tm_mday, rtc_tm.tm_mon + 1, rtc_tm.tm_year + 1900,
- rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec);
- }
- /**
- * @brief: main function
- * @Param: argc: number of parameters
- * @Param: argv: parameters list
- */
- int main(int argc, char *argv[])
- {
- int fd, retval, c;
- int set_flag = 0, read_flag = 0;
- struct rtc_time rtc_tm;
- const char *rtc = default_rtc;
-
- struct option long_option[] =
- {
- {"help", 0, NULL, 'h'},
- {"set", 1, NULL, 's'},
- {"show", 0, NULL, 'r'},
- {NULL, 0, NULL, 0},
- };
-
- program_name = argv[0];
-
- while (1) {
- if ((c = getopt_long(argc, argv, "hs:r", long_option, NULL)) < 0)
- break;
- switch (c) {
- case 'h':
- help();
- break;
- case 's':
- if (argc < 8) {
- fprintf(stderr, "ntttime format error!nn");
- exit(errno);
- }
- set_flag = 1;
- break;
- case 'r':
- read_flag = 1;
- break;
- case '?':
- help();
- break;
- default:
- abort ();
- }
- }
-
- /* open rtc device */
- fd = open(rtc, O_RDWR);
- if (fd == -1) {
- perror(rtc);
- exit(errno);
- }
-
- if (argc == 1) {
- rtc_read_time(fd);
- }
-
- if (set_flag)
- rtc_set_time(fd, atoi(argv[2]), atoi(argv[3]), atoi(argv[4]), atoi(argv[5]), atoi(argv[6]), atoi(argv[7]));
- if (read_flag)
- rtc_read_time(fd);
-
- close(fd);
- return 0;
- }
复制代码
OK了,期待next.
|