首先设置RTC时间的节点名称,此次使用的是海思的RTC时间节点“hi_rtc”。
const char *dev_name = “/dev/hi_rtc”;以下是设置时间的函数,包括RTC时间和系统时间:
int set_hwtime(rtc_time_t *rtc_time) { struct timeval val_time; struct tm tm_time; int fd = -1, ret = -1; fd = open(dev_name, O_RDWR); if (fd < 0) { printf(“open %s failed\n”, dev_name); return -1; } printf(“set_hwtime: year=%d, month=%d, day=%d, hour=%d, min=%d, sec=%d\n”,\ rtc_time->year, rtc_time->month, rtc_time->date, rtc_time->hour, rtc_time->minute, rtc_time->second); ret = ioctl(fd, HI_RTC_SET_TIME, rtc_time); if (ret < 0) { printf(“ioctl: HI_RTC_SET_TIME failed\n”); return -1; } tm_time.tm_year = rtc_time->year – 1900; tm_time.tm_mon = rtc_time->month – 1; tm_time.tm_mday = rtc_time->date; tm_time.tm_hour = rtc_time->hour; tm_time.tm_min = rtc_time->minute; tm_time.tm_sec = rtc_time->second; tm_time.tm_wday = rtc_time->weekday; printf(“set_systime: year=%d, month=%d, day=%d, hour=%d, min=%d, sec=%d\n”,\ rtc_time->year, rtc_time->month, rtc_time->date, rtc_time->hour, rtc_time->minute, rtc_time->second); val_time.tv_sec = mktime(&tm_time); val_time.tv_usec = 0; settimeofday(&val_time,NULL); close(fd); return 0; }<
rtc时间通过ioctl的方式,将时间写入。系统时间通过settimeofday函数,将时间写入。最后完成需确认打开的句柄是否关闭close(fd)。
免责声明:文章内容来自互联网,本站不对其真实性负责,也不承担任何法律责任,如有侵权等情况,请与本站联系删除。
转载请注明出处:linux c 设置rtc以及系统时间 https://www.yhzz.com.cn/a/13861.html