修复rtc驱动,实现掉电能保存时钟数据(硬件外接电池)

This commit is contained in:
xjy 2022-11-17 14:11:32 +08:00 committed by Man, Jianting (Meco)
parent 5338b1d5f2
commit 60b741520f

View File

@ -19,6 +19,7 @@
#include "drv_rtc.h"
#include "fsl_snvs_hp.h"
#include "fsl_snvs_lp.h"
#include <sys/time.h>
#if defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL
@ -29,7 +30,10 @@ static time_t imxrt_hp_get_timestamp(void)
{
struct tm tm_new = {0};
snvs_hp_rtc_datetime_t rtcDate = {0};
snvs_lp_srtc_datetime_t srtcDate = {0};
SNVS_LP_SRTC_GetDatetime(SNVS, &srtcDate);
SNVS_HP_RTC_TimeSynchronize(SNVS);
SNVS_HP_RTC_GetDatetime(SNVS, &rtcDate);
tm_new.tm_sec = rtcDate.second;
@ -46,41 +50,49 @@ static time_t imxrt_hp_get_timestamp(void)
static int imxrt_hp_set_timestamp(time_t timestamp)
{
struct tm now;
snvs_hp_rtc_datetime_t rtcDate = {0};
snvs_lp_srtc_datetime_t srtcDate = {0};
gmtime_r(&timestamp, &now);
rtcDate.second = now.tm_sec ;
rtcDate.minute = now.tm_min ;
rtcDate.hour = now.tm_hour;
srtcDate.second = now.tm_sec;
srtcDate.minute = now.tm_min;
srtcDate.hour = now.tm_hour;
rtcDate.day = now.tm_mday;
rtcDate.month = now.tm_mon + 1;
rtcDate.year = now.tm_year + 1900;
srtcDate.day = now.tm_mday;
srtcDate.month = now.tm_mon + 1;
srtcDate.year = now.tm_year + 1900;
if (SNVS_HP_RTC_SetDatetime(SNVS, &rtcDate) != kStatus_Success)
if (SNVS_LP_SRTC_SetDatetime(SNVS, &srtcDate) != kStatus_Success)
{
LOG_E("set rtc date time failed\n");
return -RT_ERROR;
}
SNVS_HP_RTC_TimeSynchronize(SNVS);
return RT_EOK;
}
static rt_err_t imxrt_hp_rtc_init(rt_device_t dev)
{
snvs_hp_rtc_config_t snvsRtcConfig;
snvs_lp_srtc_config_t snvsSrtcConfig;
/* Init SNVS_HP */
SNVS_HP_RTC_GetDefaultConfig(&snvsRtcConfig);
SNVS_HP_RTC_Init(SNVS, &snvsRtcConfig);
/* Init SNVS_LP */
SNVS_LP_SRTC_GetDefaultConfig(&snvsSrtcConfig);
SNVS_LP_SRTC_Init(SNVS, &snvsSrtcConfig);
return RT_EOK;
}
static rt_err_t imxrt_hp_rtc_open(rt_device_t dev, rt_uint16_t oflag)
{
SNVS_HP_RTC_StartTimer(SNVS);
SNVS_LP_SRTC_StartTimer(SNVS);
return RT_EOK;
}
@ -156,4 +168,58 @@ int rt_hw_rtc_init(void)
INIT_DEVICE_EXPORT(rt_hw_rtc_init);
#include <rtthread.h>
#include <rtdevice.h>
#define RTC_NAME "rtc"
static int rtc_sample(int argc, char *argv[])
{
rt_err_t ret = RT_EOK;
time_t now;
rt_device_t device = RT_NULL;
/*寻找设备*/
device = rt_device_find(RTC_NAME);
if (!device)
{
LOG_E("find %s failed!", RTC_NAME);
return RT_ERROR;
}
/*初始化RTC设备*/
if(rt_device_open(device, 0) != RT_EOK)
{
LOG_E("open %s failed!", RTC_NAME);
return RT_ERROR;
}
/* 设置日期 */
ret = set_date(2018, 12, 3);
if (ret != RT_EOK)
{
rt_kprintf("set RTC date failed\n");
return ret;
}
/* 设置时间 */
ret = set_time(11, 15, 50);
if (ret != RT_EOK)
{
rt_kprintf("set RTC time failed\n");
return ret;
}
/* 延时3秒 */
rt_thread_mdelay(3000);
/* 获取时间 */
now = time(RT_NULL);
rt_kprintf("%s\n", ctime(&now));
return ret;
}
/* 导出到 msh 命令列表中 */
MSH_CMD_EXPORT(rtc_sample, rtc sample);
#endif /* BSP_USING_RTC */