1:RTC介绍
1.1 RTC基础功能介绍
参考《S32K3xx Reference Manual》,S32K328芯片内部自带RTC功能,并且支持从低功耗状态下唤醒设备;
1.2 RTC电源介绍
由以下三张图可知
bash
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时间设置和获取就比较简单了,只要初始化正确就行;
c
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计数,需要自己进行单位转**
c
// 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的配置如下, 未实测过
c
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);
}