RTC也就是实时时钟,用于记录当前系统时间,对于Linux系统而言时间是非常重要的,就和我们使用Windows电脑或手机查看时间一样,我们在使用Linux设备的时候也需要查看时间。
Linux内核RTC驱动简介
RTC设备驱动是一个标准的字符设备驱动,应用程序通过open、release、read、write和ioctl等函数完成对RTC设备的操作。

原理详讲
1、6U 内部自带到了一个 RTC 外设,确切的说是 SRTC。 6U 和 6ULL 的 RTC 内容在 SNVS
章节。6U 的 RTC 分为 LP 和 HP。LP 叫做 SRTC,HP 是 RTC,但是 HP 的 RTC 掉电以后数据
就丟失了,即使用了纽扣电池也没用。所以必须要使用 LP,也就是 SRTC。
SNVS 章节有些是跟加密有关的,需要与 NXP 签订 NDA 协议才可以拿到。
RTC 分为 SNVS_LP 和 SNVS_HP,4
如果做产品,建议使用外置 RTC 芯片,PCF8563。
RTC 很类似定时器,外接 32.768KHz 的晶振,然后就开始计时,RTC 使用两个寄存器来
保存计数值。
RTC 使用很简单,打开 RTC,然后 RTC 就开始工作,我们要做的就是不断地读取 RTC
计数寄存器,获取时间值,或者向 RTC 计数器写入时间值,也就是调整时间。
SNVS HPCOMR 的 bit31 置 1,表示所有的软件都可以访问 SNVS 所有寄存器。Bit8 也
是和安全有关的,我们置 1,也可以不置 1.4
SNVS_ LPCR 寄存器, bit0 置 1,开始 SRTC 功能。
SNVS_LPSRTCMR 是高 32 为 RTC 计数寄存器
SNVS_LPSRTCLR 是低 32 为 RTC 计数器,与 LPSRTCMR 共同组成了 SRTC 计数器,,每
1 秒数据加 1。
6U 的 RTC模式从 1970年1月1日0时0点0分0秒。
主要代码:

-
-
- #include "bsp_rtc.h"
- #include "stdio.h"
-
-
- void rtc_init(void)
- {
-
- SNVS->HPCOMR |= (1 << 31) | (1 << 8);
-
- #if 0
- struct rtc_datetime rtcdate;
-
- rtcdate.year = 2018U;
- rtcdate.month = 12U;
- rtcdate.day = 13U;
- rtcdate.hour = 14U;
- rtcdate.minute = 52;
- rtcdate.second = 0;
- rtc_setDatetime(&rtcdate); //初始化时间和日期
- #endif
-
- rtc_enable(); //使能RTC
-
- }
-
-
- void rtc_enable(void)
- {
- /*
- * LPCR寄存器bit0置1,使能RTC
- */
- SNVS->LPCR |= 1 << 0;
- while(!(SNVS->LPCR & 0X01));//等待使能完成
-
- }
-
-
- void rtc_disable(void)
- {
-
- SNVS->LPCR &= ~(1 << 0);
- while(SNVS->LPCR & 0X01);//等待关闭完成
- }
-
-
- unsigned char rtc_isleapyear(unsigned short year)
- {
- unsigned char value=0;
-
- if(year % 400 == 0)
- value = 1;
- else
- {
- if((year % 4 == 0) && (year % 100 != 0))
- value = 1;
- else
- value = 0;
- }
- return value;
- }
-
-
- unsigned int rtc_coverdate_to_seconds(struct rtc_datetime *datetime)
- {
- unsigned short i = 0;
- unsigned int seconds = 0;
- unsigned int days = 0;
- unsigned short monthdays[] = {0U, 0U, 31U, 59U, 90U, 120U, 151U, 181U, 212U, 243U, 273U, 304U, 334U};
-
- for(i = 1970; i < datetime->year; i++)
- {
- days += DAYS_IN_A_YEAR; /* 平年,每年365天 */
- if(rtc_isleapyear(i)) days += 1;/* 闰年多加一天 */
- }
-
- days += monthdays[datetime->month];
- if(rtc_isleapyear(i) && (datetime->month >= 3)) days += 1;/* 闰年,并且当前月份大于等于3月的话加一天 */
-
- days += datetime->day - 1;
-
- seconds = days * SECONDS_IN_A_DAY +
- datetime->hour * SECONDS_IN_A_HOUR +
- datetime->minute * SECONDS_IN_A_MINUTE +
- datetime->second;
-
- return seconds;
- }
-
-
- void rtc_setdatetime(struct rtc_datetime *datetime)
- {
-
- unsigned int seconds = 0;
- unsigned int tmp = SNVS->LPCR;
-
- rtc_disable(); /* 设置寄存器HPRTCMR和HPRTCLR的时候一定要先关闭RTC */
-
-
- /* 先将时间转换为秒 */
- seconds = rtc_coverdate_to_seconds(datetime);
-
- SNVS->LPSRTCMR = (unsigned int)(seconds >> 17); /* 设置高16位 */
- SNVS->LPSRTCLR = (unsigned int)(seconds << 15); /* 设置地16位 */
-
- /* 如果此前RTC是打开的在设置完RTC时间以后需要重新打开RTC */
- if (tmp & 0x1)
- rtc_enable();
- }
-
-
- void rtc_convertseconds_to_datetime(u64 seconds, struct rtc_datetime *datetime)
- {
- u64 x;
- u64 secondsRemaining, days;
- unsigned short daysInYear;
-
- /* 每个月的天数 */
- unsigned char daysPerMonth[] = {0U, 31U, 28U, 31U, 30U, 31U, 30U, 31U, 31U, 30U, 31U, 30U, 31U};
-
- secondsRemaining = seconds; /* 剩余秒数初始化 */
- days = secondsRemaining / SECONDS_IN_A_DAY + 1; /* 根据秒数计算天数,加1是当前天数 */
- secondsRemaining = secondsRemaining % SECONDS_IN_A_DAY; /*计算天数以后剩余的秒数 */
-
- /* 计算时、分、秒 */
- datetime->hour = secondsRemaining / SECONDS_IN_A_HOUR;
- secondsRemaining = secondsRemaining % SECONDS_IN_A_HOUR;
- datetime->minute = secondsRemaining / 60;
- datetime->second = secondsRemaining % SECONDS_IN_A_MINUTE;
-
- /* 计算年 */
- daysInYear = DAYS_IN_A_YEAR;
- datetime->year = YEAR_RANGE_START;
- while(days > daysInYear)
- {
- /* 根据天数计算年 */
- days -= daysInYear;
- datetime->year++;
-
- /* 处理闰年 */
- if (!rtc_isleapyear(datetime->year))
- daysInYear = DAYS_IN_A_YEAR;
- else /*闰年,天数加一 */
- daysInYear = DAYS_IN_A_YEAR + 1;
- }
- /*根据剩余的天数计算月份 */
- if(rtc_isleapyear(datetime->year)) /* 如果是闰年的话2月加一天 */
- daysPerMonth[2] = 29;
-
- for(x = 1; x <= 12; x++)
- {
- if (days <= daysPerMonth[x])
- {
- datetime->month = x;
- break;
- }
- else
- {
- days -= daysPerMonth[x];
- }
- }
-
- datetime->day = days;
-
- }
-
- unsigned int rtc_getseconds(void)
- {
- unsigned int seconds = 0;
- seconds = (SNVS->LPSRTCMR << 17) | (SNVS->LPSRTCLR >> 15);
- return seconds;
- }
-
- void rtc_getdatetime(struct rtc_datetime *datetime)
- {
- //unsigned int seconds = 0;
- u64 seconds;
- seconds = rtc_getseconds();
- rtc_convertseconds_to_datetime(seconds, datetime);
- }
-
-
-