1:RTC介紹
1.1 RTC基礎功能介紹
參考《S32K3xx Reference Manual》,S32K328芯片內部自帶RTC功能,并且支持從低功耗狀態下喚醒設備;
1.2 RTC電源介紹
由以下三張圖可知
1:RTC由V11供電,V11依賴外部V15供電;
2:MCU外部只需要輸入3.3V或5V + 1.5V;
3:待機模式下,RTC任然保持供電在;
電源轉換框圖
外部電源輸入圖,也就是說 外部電源只需要輸入3.3V和1.5V即可
詳細電源介紹
2:RTC的mex配置
2.1 RTC模塊配置
基礎通道配置
硬件中斷配置
喚醒配置
2.2 WKPU 模塊配置
參考文檔可知,RTC喚醒源,默認為bit0或bit1
添加以下中斷配置表
配置以下喚醒通道
喚醒通道基礎配置
2.3 clock配置
兩個模式狀態均需要使能;不然可能在待機模式下,RTC不會繼續計數
3:軟件代碼
3.1 RTC時間設置和讀取
RTC時間設置和獲取就比較簡單了,只要初始化正確就行;
int main (void)
{static Rtc_Ip_TimedateType lasttime;Rtc_Ip_TimedateType nowtime;/* 時鐘初始化 */Clock_Ip_Init(&Clock_Ip_aClockConfig[0]);/* 失能和清除API中斷,否則上電啟動進入中斷 */Rtc_Ip_DisableInterrupt(RTC_INST, RTC_IP_API_INTERRUPT);/* 初始化中斷控制器 */IntCtrl_Ip_InstallHandler(RTC_IRQn, RTC_0_Ch_0_ISR, NULL_PTR);IntCtrl_Ip_EnableIrq(RTC_IRQn);/* 初始化RTC */Rtc_Ip_Init(RTC_INST, &RTC_0_InitConfig_PB);/* 配置中斷,API匹配中斷 */ // 注意這里要設置不同的參數Rtc_Ip_EnableInterrupt(RTC_INST, RTC_IP_API_INTERRUPT);/* 定時器啟動,并配置API比較值 */Rtc_Ip_StartTimer(RTC_INST,RTC_PERIOD);/* 設置當前時間 ,實則配置rtc比較值 */Rtc_Ip_SetTimeDate(RTC_INST, &Rtc_DateTimeCfg_0);while(1){/* RTC獲取當前時間 */Rtc_Ip_GetTimeDate(RTC_INST, &nowtime);/* 對比時間,判斷時間是否更新 */
// if(false == DataTimeCompare(&lasttime, &nowtime))if(memcmp(&lasttime, &nowtime, sizeof(Rtc_Ip_TimedateType))){printf("%d-%d-%d %d:%d:%d\r\n",nowtime.year, nowtime.month, nowtime.day,\nowtime.hour, nowtime.minutes, nowtime.seconds);}/* 記錄值用于比較 */lasttime = nowtime;}return 0;
}
這里存在兩個問題
1:如果發現 Rtc_Ip_GetTimeDate 的時間數據不更新,那么參考以下初始化RTC的三個函數,缺一不可;
2:RTC記錄的時間,MCU重啟就會丟失,通過示波器測量V11電源穩定無變化;實測不能實現掉主電(MCU_3V3)持續更新時間功能
跟蹤了一下RTD生成的源代碼,發現 “Rtc_Ip_GetTimeDate” 獲取的參數,初始化會設置為0;
3.2 RTC喚醒
通過 "figure 189"可知,RTC可以配置兩個喚醒源;
初始化都設置好了之后,在休眠前執行如下喚醒函數;配置 wakeup source bit[0];
參考 Rtc_Ip.h 定義–“Rtc_Ip_StartTimer”, reads the RTC counter register,這里是tick計數,需要自己進行單位轉**
// Rtc_Ip.h 定義
/**
* @brief Function for starting the Rtc timer channel.
* @details This function:
* - disables the API functionality
* - sets the timeout value (in RTCC - APIVAL)
* - enables API functionality (RTCC - APIEN).
* - reads the RTC counter register and stores the value of the channel start time
*
* @param[in] value channel timeout value
* @return void
* @pre The data structure including the configuration set required for initializing the GPT driver
*
*/
void Rtc_Ip_StartTimer(uint8 instance, uint32 value);
/*************************************/// 應用實現,配置一下定時時間和喚醒中斷0
void wkup_config(void)
{/* RTC configuration */Rtc_Ip_StopTimer(RTC_INST);Rtc_Ip_StartTimer(RTC_INST, 30*32768);/* WKPU configuration */Wkpu_Ip_Init(WKPU_INST, &Wkpu_Ip_Config_PB);Wkpu_Ip_EnableInterrupt(WKPU_INST, 0);/*enter sleep*/Power_Ip_SetMode(Power_Ip_aModeConfigPB);
}
休眠之后實現喚醒
喚醒中斷1的配置如下, 未實測過
void Wkup_Config(void)
{/* WKPU configuration */Wkpu_Ip_Init(WKPU_INST, &Wkpu_Ip_Config_PB_BOARD_InitPeripherals);Wkpu_Ip_EnableInterrupt(WKPU_INST, 1);/* Init Rtc and RTC_0_InitConfig_PB is config tool generated */Rtc_Ip_Init(RTC_INST, &RTC_0_InitConfig_PB);/* Stop the Rtc counter */Rtc_Ip_StopCounter(RTC_INST);/* Sets the timeout value (in RTCC - RTCVAL)Start the Rtc counter. */Wkup_SetRtcCompareValue(RTC_TIME);Rtc_Ip_StartCounter(RTC_INST);
}