文章目錄
- 一.RTC介紹
- 二.IMX6ull RTC介紹
- 1.SNVS_HP (high power domain)
- 2.SNVS_LP (low power domain)
- 3.SNVS interrupts and alarms
- 三. SNVS重點寄存器介紹
- 1.SNVS_HP Command(HPCOMR)
- 2.SNVS_HP/SNVS_LP Control register (SNVS_HPCR/SNVS_LPCR)
- 3.SNVS_HP/SNVS_LP 狀態寄存器(SNVS_HPSR/SNVS_LPSR)
- 4.SNVS_HP/SNVS_LP 實時計數器高字節寄存器(HPRTCMR/LPSRTCMR)
- 5.SNVS_HP/SNVS_LP 實時計數器低字節寄存器(SNVS_HPRTCLR/SNVS_LPSRTCLR)
- 6.SNVS_HP 時間報警高字節寄存器(SNVS_HPTAMR)
- 四.獲取RTC時間
- 1.編程思路
- 2.日期時間與秒時間之間轉換
- (1)daetime.h
- (2)datetime.c
- 3.代碼
- 五.Alarm 中斷
- 1. 編程思路
- RTC 鬧鐘功能操作步驟
- 2.代碼
一.RTC介紹
實時時鐘(Real Time Clock, RTC):可在系統斷電時用備用電池工作,斷電時備用電池能存儲秒、分、小時、周、日、月、年時間數據,單元外接32.768 kHz晶振,有定時報警功能 。
二.IMX6ull RTC介紹
- 硬件功能:imx6ull芯片的Secure Non - Volatile Storage (SNVS)提供RTC功能 ,其低功耗(電池支持)部分含安全實時計數器、單調計數器、通用寄存器,由電池供電,芯片斷電時電池保持SNVS_LP寄存器狀態 。
- 專業術語:Secure Non - Volatile Storage (SNVS)(安全非易失性存儲)、RTC(Real Time Clock,實時時鐘 ) 、monotonic counter(單調計數器 ) 、SNVS_LP registers(SNVS低功耗寄存器 ) 。
- SNVS_HP部分功能:實現啟用系統通信和SNVS_LP部分分配配置的所有功能
- SNVS_LP部分功能:提供能夠安全存儲和保護敏感數據的硬件,其內部有SRTC(定時器 ),核心板為其提供32.768KHz時鐘信號即可工作 。
1.SNVS_HP (high power domain)
- 功能單元:SNVS_HP分為IP總線接口、SNVS_LP接口、帶報警功能的實時計數器、控制和狀態寄存器這些功能單元 。
- 供電與接口:SNVS_HP位于芯片電源域,與芯片其余部分一同供電;是SNVS_LP和系統其余部分間的接口,訪問SNVS_LP寄存器須經SNVS_HP且其需上電,通過寄存器訪問權限策略決定是否允許訪問特定寄存器 。
2.SNVS_LP (low power domain)
功能單元:SNVS_LP(低功耗域)的功能單元有非翻轉單調計數器、通用寄存器、控制和狀態寄存器 。
子系統與電源:SNVS_LP是數據存儲子系統,用于存儲和保護系統數據,不受主系統電源狀態影響;處于始終上電域,為單獨電源域,有自身電源 。
3.SNVS interrupts and alarms
- 時間報警寄存器功能:
- SNVS_HP非安全RTC有自身時間報警寄存器,應用程序可更新。
- 能生成中斷提醒主機處理器,可從低功耗模式喚醒主機處理器;系統斷電時無法喚醒整個系統(因報警也斷電 )。
- 周期性中斷功能:
- SNVS_HP非安全RTC含周期性中斷,RTC選定位0 - 1或1 - 0轉換時產生。
- 中斷源依HP控制寄存器PI_FREQ字段,從HP RTC 16位中選,位選擇定義中斷頻率 。
三. SNVS重點寄存器介紹
1.SNVS_HP Command(HPCOMR)
2.SNVS_HP/SNVS_LP Control register (SNVS_HPCR/SNVS_LPCR)
3.SNVS_HP/SNVS_LP 狀態寄存器(SNVS_HPSR/SNVS_LPSR)
4.SNVS_HP/SNVS_LP 實時計數器高字節寄存器(HPRTCMR/LPSRTCMR)
5.SNVS_HP/SNVS_LP 實時計數器低字節寄存器(SNVS_HPRTCLR/SNVS_LPSRTCLR)
6.SNVS_HP 時間報警高字節寄存器(SNVS_HPTAMR)
四.獲取RTC時間
1.編程思路
- 操作步驟類:
- 使能SNVS時鐘
- 設置寄存器訪問不受權限限制
- 停止RTC計數器,等待RTC計數器停止成功
- 設置日期
- 開啟RTC計數器,等待RTC計數器開啟成功
- 每隔1秒讀取RTC數據,然后輸出
2.日期時間與秒時間之間轉換
(1)daetime.h
#ifndef _DATETIME_HEAD_H
#define _DATETIME_HEAD_H#include <stdint.h>#define SECONDS_IN_A_DAY (86400U)
#define SECONDS_IN_A_HOUR (3600U)
#define SECONDS_IN_A_MINUTE (60U)
#define DAYS_IN_A_YEAR (365U)
#define YEAR_RANGE_START (1970U)
#define YEAR_RANGE_END (2099U)typedef struct rtc_datetime
{uint16_t year; /*!< Range from 1970 to 2099. */uint8_t month; /*!< Range from 1 to 12. */uint8_t day; /*!< Range from 1 to 31 (depending on month). */uint8_t hour; /*!< Range from 0 to 23. */uint8_t minute; /*!< Range from 0 to 59. */uint8_t second; /*!< Range from 0 to 59. */
} rtc_datetime_t;extern uint32_t convert_datetime_to_seconds(const rtc_datetime_t *datetime);
extern void convert_seconds_to_datetime(uint32_t seconds, rtc_datetime_t *datetime);#endif /* _DATETIME_HEAD_H */
(2)datetime.c
#include "datetime.h"uint32_t convert_datetime_to_seconds(const rtc_datetime_t *datetime)
{/* Number of days from begin of the non Leap-year*/uint16_t monthDays[] = {0U, 31U, 59U, 90U, 120U, 151U, 181U, 212U, 243U, 273U, 304U, 334U};uint32_t seconds = 0;/* Compute number of days from 1970 till given year*/seconds = (datetime->year - 1970U) * DAYS_IN_A_YEAR;/* Add leap year number of days */seconds += ((datetime->year / 4) - (1970U / 4));/* Add number of days till given month*/seconds += monthDays[datetime->month];/* Add days in given month. We subtract the current day as it is * represented in the hours, minutes and seconds field*/seconds += (datetime->day - 1);/* For leap year if month less than or equal to February, decrement day counter*/if ((!(datetime->year & 3U)) && (datetime->month <= 2U)){seconds--;}seconds = (seconds * SECONDS_IN_A_DAY) + (datetime->hour * SECONDS_IN_A_HOUR) + (datetime->minute * SECONDS_IN_A_MINUTE) + datetime->second;return seconds;
}void convert_seconds_to_datetime(uint32_t seconds, rtc_datetime_t *datetime)
{uint32_t x;uint32_t secondsRemaining, days;uint16_t daysInYear;/* Table of days in a month for a non leap year. First entry in the table is not used,* valid months start from 1*/uint8_t daysPerMonth[] = {0U, 31U, 28U, 31U, 30U, 31U, 30U, 31U, 31U, 30U, 31U, 30U, 31U};/* Start with the seconds value that is passed in to be converted to * date time format*/secondsRemaining = seconds;/* Calculate the number of days, we add 1 for the current day which is represented in * the hours and seconds field*/days = secondsRemaining / SECONDS_IN_A_DAY + 1;/* Update seconds left*/secondsRemaining = secondsRemaining % SECONDS_IN_A_DAY;/* Calculate the datetime hour, minute and second fields */datetime->hour = secondsRemaining / SECONDS_IN_A_HOUR;secondsRemaining = secondsRemaining % SECONDS_IN_A_HOUR;datetime->minute = secondsRemaining / 60U;datetime->second = secondsRemaining % SECONDS_IN_A_MINUTE;/* Calculate year */daysInYear = DAYS_IN_A_YEAR;datetime->year = YEAR_RANGE_START;while (days > daysInYear){/* Decrease day count by a year and increment year by 1 */days -= daysInYear;datetime->year++;/* Adjust the number of days for a leap year */if (datetime->year & 3U){daysInYear = DAYS_IN_A_YEAR;}else{daysInYear = DAYS_IN_A_YEAR + 1;}}/* Adjust the days in February for a leap year */if (!(datetime->year & 3U)){daysPerMonth[2] = 29U;}for (x = 1U; x <= 12U; x++){if (days <= daysPerMonth[x]){datetime->month = x;break;}else{days -= daysPerMonth[x];}}datetime->day = days;return;
}
3.代碼
#include "imx6ull.h"
#include "datetime.h"
void rtc_init(void)
{/*Enable Clock*/CCM_CCGR5 |= (0x3 << 18);/*NPSWA_EN [31] 1 Any software can accsee*/SNVS->HPCOMR |= (1 << 31);
}void rtc_stop(void)
{/*RTC_EN [0] 0b clear Enable*/SNVS->HPCR &= ~(1 << 0);while(SNVS->HPCR & (1 << 0)){}
}void rtc_start(void)
{/*RTC_EN [0] 1b Enable*/SNVS->HPCR |= (1 << 0);while((SNVS->HPCR & (1 << 0)) == 0){}
}void rtc_set_datetime(rtc_datetime_t *datetime)
{uint32_t seconds;rtc_stop();seconds = convert_datetime_to_seconds(datetime);SNVS->HPRTCMR = seconds >> 17;//[31:18][17:0]SNVS->HPRTCLR = seconds << 15;rtc_start();return;}void rtc_get_datetime(rtc_datetime_t *datetime)
{uint32_t tmp = 0;
uint32_t seconds = 0;do {tmp = seconds;seconds = (SNVS->HPRTCMR << 17) | (SNVS->HPRTCLR >> 15);
} while (seconds != tmp);convert_seconds_to_datetime(seconds, datetime);return;
}void rtc_test(void)
{rtc_datetime_t datetime;rtc_init();datetime.year = 2025;
datetime.month = 1;
datetime.day = 1;
datetime.hour = 21;
datetime.minute = 40;
datetime.second = 30;rtc_set_datetime(&datetime);while(1) {rtc_get_datetime(&datetime);uart_printf("%d-%d-%d %d:%d:%d\r\n", datetime.year, datetime.month, datetime.day, datetime.hour, datetime.minute, datetime.second);gpt_delay_sec(1);
}return;
}
五.Alarm 中斷
1. 編程思路
RTC 鬧鐘功能操作步驟
-
注冊 RTC 中斷
需編寫代碼完成 RTC 中斷的注冊,使系統能響應 RTC 相關中斷事件(如鬧鐘觸發 )。 -
設置 RTC alarm 日期
- Disable RTC alarm:先禁用 RTC 鬧鐘,避免設置過程中誤觸發。
- 設置日期:配置鬧鐘觸發的具體日期、時間等參數(如年、月、日、時、分、秒 )。
- Enable RTC alarm:啟用 RTC 鬧鐘,使配置的鬧鐘參數生效,到指定時間觸發中斷。
-
中斷處理函數邏輯
- 判斷是否是 RTC alarm 中斷:在中斷處理函數中,通過檢測中斷標志等方式,識別是否由 RTC 鬧鐘觸發中斷。
- 輸出提醒信息:若確認是 RTC alarm 中斷,輸出“鬧鐘時間到達”等提示信息(可通過串口、顯示屏等方式輸出 )。
- 清除中斷標志:執行寫 1 清 0 操作,清除 RTC alarm 中斷標志,確保后續中斷能正常觸發。
2.代碼
int rtc_interrupt_handler(int id)
{if(SNVS->HPSR & (1 << 0)){uart_printf("RTC Alarm\r\n");//clear interruptSNVS->HPSR |= (1 << 0);while(SNVS->HPSR & (1 << 0)){}}return 0;
}void rtc_set_alarm(rtc_datetime_t *datetime)
{int seconds;// 請求 RTC 中斷:注冊中斷請求,綁定中斷處理函數
request_irq(SNVS_Consolidated_IRQn, rtc_interrupt_handler);// 禁用鬧鐘:通過操作 HPCR 寄存器,等待禁用完成
SNVS->HPCR &= ~(1 << 1);
while (SNVS->HPCR & (1 << 1)) {// 等待寄存器位清除,確保鬧鐘已禁用
}// 設置鬧鐘時間:將 datetime 轉換為秒數,拆分后寫入寄存器
seconds = convert_datetime_to_seconds(datetime);
// 高 15 位寫入 HPRTCMR([31:17] 存儲秒數高段)
SNVS->HPRTCMR = seconds >> 17;
// 低 17 位寫入 HPRTCLR([16:0] 存儲秒數低段,通過左移 15 位對齊)
SNVS->HPRTCLR = seconds << 15;//Enable alarmSNVS->HPCR |= (1 << 1);while((SNVS->HPCR & (1 << 1)) == 0){}
}