2022-03-08 12:03:06 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2006-2022, RT-Thread Development Team
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2022-03-04 stevetong459 first version
|
2022-07-22 15:05:14 +08:00
|
|
|
* 2022-07-15 Aligagago add apm32F4 serie MCU support
|
2023-01-05 14:15:02 +08:00
|
|
|
* 2022-12-26 luobeihai add apm32F0 serie MCU support
|
2022-03-08 12:03:06 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "board.h"
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
|
|
|
#ifdef BSP_USING_ONCHIP_RTC
|
|
|
|
|
2023-01-05 14:15:02 +08:00
|
|
|
#define DBG_TAG "drv.rtc"
|
2022-03-08 12:03:06 +08:00
|
|
|
#define DBG_LVL DBG_INFO
|
|
|
|
#include <rtdbg.h>
|
|
|
|
|
|
|
|
#ifndef LSI_VALUE
|
|
|
|
#define LSI_VALUE ((uint32_t)40000)
|
|
|
|
#endif
|
|
|
|
#ifndef LSE_VALUE
|
|
|
|
#define LSE_VALUE ((uint32_t)32768)
|
|
|
|
#endif
|
|
|
|
|
2023-01-05 14:15:02 +08:00
|
|
|
#define DRV_RTC_TIME_OUT 0xFFFFF
|
2022-03-08 12:03:06 +08:00
|
|
|
|
|
|
|
static rt_rtc_dev_t apm32_rtc_dev;
|
2022-07-22 15:05:14 +08:00
|
|
|
static rt_uint8_t rtc_init_flag = RESET;
|
2022-03-08 12:03:06 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief This function will initialize the rtc on chip.
|
|
|
|
*
|
|
|
|
* @return RT_EOK indicates successful initialize, other value indicates failed;
|
|
|
|
*/
|
2023-01-05 14:15:02 +08:00
|
|
|
static rt_err_t apm32_rtc_init(void)
|
2022-03-08 12:03:06 +08:00
|
|
|
{
|
|
|
|
volatile rt_uint32_t counter = 0;
|
2023-01-05 14:15:02 +08:00
|
|
|
|
2022-03-08 12:03:06 +08:00
|
|
|
/* Enable RTC Clock */
|
2023-01-05 14:15:02 +08:00
|
|
|
#if defined(SOC_SERIES_APM32F1)
|
2022-03-08 12:03:06 +08:00
|
|
|
RCM_EnableAPB1PeriphClock(RCM_APB1_PERIPH_PMU | RCM_APB1_PERIPH_BAKR);
|
2023-01-05 14:15:02 +08:00
|
|
|
#elif defined(SOC_SERIES_APM32F0) || defined(SOC_SERIES_APM32F4)
|
2022-07-22 15:05:14 +08:00
|
|
|
RCM_EnableAPB1PeriphClock(RCM_APB1_PERIPH_PMU);
|
|
|
|
#endif
|
2023-01-05 14:15:02 +08:00
|
|
|
|
2022-03-08 12:03:06 +08:00
|
|
|
PMU_EnableBackupAccess();
|
|
|
|
|
|
|
|
/* Config RTC clock */
|
|
|
|
#ifdef BSP_RTC_USING_LSI
|
|
|
|
RCM_EnableLSI();
|
|
|
|
while (!RCM_ReadStatusFlag(RCM_FLAG_LSIRDY))
|
|
|
|
{
|
|
|
|
if (++counter > DRV_RTC_TIME_OUT)
|
|
|
|
{
|
|
|
|
return RT_ETIMEOUT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
RCM_ConfigRTCCLK(RCM_RTCCLK_LSI);
|
|
|
|
#else
|
2023-01-05 14:15:02 +08:00
|
|
|
RCM_DisableLSI();
|
2022-03-08 12:03:06 +08:00
|
|
|
RCM_ConfigLSE(RCM_LSE_OPEN);
|
|
|
|
while (!RCM_ReadStatusFlag(RCM_FLAG_LSERDY))
|
|
|
|
{
|
|
|
|
if (++counter > DRV_RTC_TIME_OUT)
|
|
|
|
{
|
|
|
|
return RT_ETIMEOUT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
RCM_ConfigRTCCLK(RCM_RTCCLK_LSE);
|
2023-01-05 14:15:02 +08:00
|
|
|
#endif /* BSP_RTC_USING_LSI */
|
2022-03-08 12:03:06 +08:00
|
|
|
|
|
|
|
RCM_EnableRTCCLK();
|
2022-07-22 15:05:14 +08:00
|
|
|
RTC_WaitForSynchro();
|
2022-03-08 12:03:06 +08:00
|
|
|
|
2023-01-05 14:15:02 +08:00
|
|
|
#if defined(SOC_SERIES_APM32F1)
|
2022-03-08 12:03:06 +08:00
|
|
|
counter = 0;
|
|
|
|
while (!RTC_ReadStatusFlag(RTC_FLAG_OC))
|
|
|
|
{
|
|
|
|
if (++counter > DRV_RTC_TIME_OUT)
|
|
|
|
{
|
|
|
|
return RT_ETIMEOUT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RTC_EnableConfigMode();
|
|
|
|
|
|
|
|
RTC_ClearStatusFlag(RTC_FLAG_OVR | RTC_FLAG_ALR | RTC_FLAG_SEC);
|
|
|
|
|
2022-07-25 10:21:18 +08:00
|
|
|
#ifdef BSP_RTC_USING_LSI
|
|
|
|
RTC_ConfigPrescaler(LSI_VALUE - 1);
|
|
|
|
#else
|
|
|
|
RTC_ConfigPrescaler(LSE_VALUE - 1);
|
2023-01-05 14:15:02 +08:00
|
|
|
#endif /* BSP_RTC_USING_LSI */
|
|
|
|
|
|
|
|
#elif defined(SOC_SERIES_APM32F4)
|
2022-07-22 15:05:14 +08:00
|
|
|
RTC_EnableInit();
|
|
|
|
RTC_Config_T rtcConfig;
|
|
|
|
RTC_ConfigStructInit(&rtcConfig);
|
|
|
|
RTC_Config(&rtcConfig);
|
2023-01-05 14:15:02 +08:00
|
|
|
|
|
|
|
#elif defined(SOC_SERIES_APM32F0)
|
|
|
|
RTC_EnableInit();
|
|
|
|
RTC_Config_T rtcConfig;
|
|
|
|
RTC_ConfigStructInit(&rtcConfig);
|
|
|
|
|
|
|
|
#ifdef BSP_RTC_USING_LSI
|
|
|
|
rtcConfig.AsynchPrediv = 0x63;
|
|
|
|
rtcConfig.SynchPrediv = 0x18F;
|
|
|
|
#else
|
|
|
|
rtcConfig.AsynchPrediv = 0x7F;
|
|
|
|
rtcConfig.SynchPrediv = 0x130;
|
|
|
|
#endif /* BSP_RTC_USING_LSI */
|
|
|
|
RTC_Config(&rtcConfig);
|
|
|
|
|
|
|
|
#endif /* SOC_SERIES_APM32F1 */
|
2022-03-08 12:03:06 +08:00
|
|
|
|
2022-07-25 10:21:18 +08:00
|
|
|
if (!rtc_init_flag)
|
2022-07-22 15:05:14 +08:00
|
|
|
{
|
|
|
|
rtc_init_flag = SET;
|
|
|
|
}
|
2022-03-08 12:03:06 +08:00
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
2023-01-05 14:15:02 +08:00
|
|
|
#if defined(SOC_SERIES_APM32F1)
|
2022-03-08 12:03:06 +08:00
|
|
|
/**
|
|
|
|
* @brief This function will initialize the rtc on chip.
|
|
|
|
*
|
|
|
|
* @return RT_EOK indicates successful initialize, other value indicates failed;
|
|
|
|
*/
|
2023-01-05 14:15:02 +08:00
|
|
|
static rt_err_t apm32_rtc_get_secs(time_t *sec)
|
2022-03-08 12:03:06 +08:00
|
|
|
{
|
|
|
|
volatile rt_uint32_t counter = 0;
|
|
|
|
|
|
|
|
while (!RTC_ReadStatusFlag(RTC_FLAG_OC))
|
|
|
|
{
|
|
|
|
if (++counter > DRV_RTC_TIME_OUT)
|
|
|
|
{
|
|
|
|
return RT_ETIMEOUT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-05 14:15:02 +08:00
|
|
|
*(timer_t *) sec = RTC_ReadCounter();
|
2022-03-08 12:03:06 +08:00
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
2023-01-05 14:15:02 +08:00
|
|
|
static rt_err_t apm32_rtc_set_secs(time_t *sec)
|
2022-03-08 12:03:06 +08:00
|
|
|
{
|
|
|
|
volatile rt_uint32_t counter = 0;
|
|
|
|
|
2022-07-25 10:21:18 +08:00
|
|
|
if (!rtc_init_flag)
|
2022-07-22 15:05:14 +08:00
|
|
|
{
|
2023-01-05 14:15:02 +08:00
|
|
|
apm32_rtc_init();
|
2022-07-22 15:05:14 +08:00
|
|
|
}
|
|
|
|
|
2022-03-08 12:03:06 +08:00
|
|
|
while (!RTC_ReadStatusFlag(RTC_FLAG_OC))
|
|
|
|
{
|
|
|
|
if (++counter > DRV_RTC_TIME_OUT)
|
|
|
|
{
|
|
|
|
return RT_ETIMEOUT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-05 14:15:02 +08:00
|
|
|
RTC_ConfigCounter(*(rt_uint32_t *)sec);
|
2022-03-08 12:03:06 +08:00
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
2023-01-05 14:15:02 +08:00
|
|
|
#elif defined(SOC_SERIES_APM32F0) || defined(SOC_SERIES_APM32F4)
|
|
|
|
static rt_err_t apm32_rtc_get_timeval(struct timeval *tv)
|
2022-07-22 15:05:14 +08:00
|
|
|
{
|
2023-01-05 14:15:02 +08:00
|
|
|
#if defined(SOC_SERIES_APM32F0)
|
|
|
|
RTC_TIME_T timeConfig;
|
|
|
|
RTC_DATE_T dateConfig;
|
|
|
|
#elif defined(SOC_SERIES_APM32F4)
|
2022-07-22 15:05:14 +08:00
|
|
|
RTC_TimeConfig_T timeConfig;
|
|
|
|
RTC_DateConfig_T dateConfig;
|
2023-01-05 14:15:02 +08:00
|
|
|
#endif
|
|
|
|
|
2022-07-22 15:05:14 +08:00
|
|
|
struct tm tm_new = {0};
|
|
|
|
|
|
|
|
RTC_ReadTime(RTC_FORMAT_BIN, &timeConfig);
|
|
|
|
RTC_ReadDate(RTC_FORMAT_BIN, &dateConfig);
|
|
|
|
|
|
|
|
tm_new.tm_sec = timeConfig.seconds;
|
|
|
|
tm_new.tm_min = timeConfig.minutes;
|
|
|
|
tm_new.tm_hour = timeConfig.hours;
|
|
|
|
tm_new.tm_mday = dateConfig.date;
|
|
|
|
tm_new.tm_mon = dateConfig.month - 1;
|
|
|
|
tm_new.tm_year = dateConfig.year + 100;
|
2023-01-05 14:15:02 +08:00
|
|
|
|
2022-07-22 15:05:14 +08:00
|
|
|
tv->tv_sec = timegm(&tm_new);
|
2023-01-05 14:15:02 +08:00
|
|
|
|
|
|
|
return RT_EOK;
|
2022-07-22 15:05:14 +08:00
|
|
|
}
|
|
|
|
|
2023-01-05 14:15:02 +08:00
|
|
|
static rt_err_t set_rtc_time_stamp(time_t time_stamp)
|
2022-07-22 15:05:14 +08:00
|
|
|
{
|
2023-01-05 14:15:02 +08:00
|
|
|
#if defined(SOC_SERIES_APM32F0)
|
|
|
|
RTC_TIME_T timeConfig;
|
|
|
|
RTC_DATE_T dateConfig;
|
|
|
|
#elif defined(SOC_SERIES_APM32F4)
|
2022-07-22 15:05:14 +08:00
|
|
|
RTC_TimeConfig_T timeConfig;
|
|
|
|
RTC_DateConfig_T dateConfig;
|
2023-01-05 14:15:02 +08:00
|
|
|
#endif
|
|
|
|
|
2022-07-22 15:05:14 +08:00
|
|
|
struct tm tm = {0};
|
|
|
|
|
2022-07-25 10:21:18 +08:00
|
|
|
if (!rtc_init_flag)
|
2022-07-22 15:05:14 +08:00
|
|
|
{
|
2023-01-05 14:15:02 +08:00
|
|
|
apm32_rtc_init();
|
2022-07-22 15:05:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
gmtime_r(&time_stamp, &tm);
|
|
|
|
if (tm.tm_year < 100)
|
|
|
|
{
|
|
|
|
return -RT_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
timeConfig.seconds = tm.tm_sec ;
|
|
|
|
timeConfig.minutes = tm.tm_min ;
|
|
|
|
timeConfig.hours = tm.tm_hour;
|
|
|
|
dateConfig.date = tm.tm_mday;
|
2023-01-05 14:15:02 +08:00
|
|
|
#if defined(SOC_SERIES_APM32F4)
|
|
|
|
dateConfig.month = (RTC_MONTH_T)(tm.tm_mon + 1);
|
|
|
|
dateConfig.weekday = (RTC_WEEKDAY_T)(tm.tm_wday + 1);
|
|
|
|
#else
|
2022-07-22 15:05:14 +08:00
|
|
|
dateConfig.month = tm.tm_mon + 1 ;
|
|
|
|
dateConfig.weekday = tm.tm_wday + 1;
|
2023-01-05 14:15:02 +08:00
|
|
|
#endif
|
|
|
|
dateConfig.year = tm.tm_year - 100;
|
|
|
|
|
2022-07-22 15:05:14 +08:00
|
|
|
|
2022-07-25 10:21:18 +08:00
|
|
|
RTC_ConfigTime(RTC_FORMAT_BIN, &timeConfig);
|
|
|
|
RTC_ConfigDate(RTC_FORMAT_BIN, &dateConfig);
|
2023-01-05 14:15:02 +08:00
|
|
|
|
|
|
|
/* wait for set time completed */
|
|
|
|
for (int i = 0; i < 0xFFFF; i++);
|
2022-03-08 12:03:06 +08:00
|
|
|
|
2022-07-22 15:05:14 +08:00
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief This function will initialize the rtc on chip.
|
|
|
|
*
|
|
|
|
* @return RT_EOK indicates successful initialize, other value indicates failed;
|
|
|
|
*/
|
2023-01-05 14:15:02 +08:00
|
|
|
static rt_err_t apm32_rtc_get_secs(time_t *sec)
|
2022-07-22 15:05:14 +08:00
|
|
|
{
|
|
|
|
struct timeval tv;
|
2023-01-05 14:15:02 +08:00
|
|
|
apm32_rtc_get_timeval(&tv);
|
|
|
|
*(time_t *) sec = tv.tv_sec;
|
2022-07-22 15:05:14 +08:00
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
2023-01-05 14:15:02 +08:00
|
|
|
static rt_err_t apm32_rtc_set_secs(time_t *sec)
|
2022-07-22 15:05:14 +08:00
|
|
|
{
|
|
|
|
rt_err_t result = RT_EOK;
|
|
|
|
|
2023-01-05 14:15:02 +08:00
|
|
|
if (set_rtc_time_stamp(*sec))
|
2022-07-22 15:05:14 +08:00
|
|
|
{
|
|
|
|
result = -RT_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
#endif
|
2022-03-08 12:03:06 +08:00
|
|
|
|
2023-01-05 14:15:02 +08:00
|
|
|
static const struct rt_rtc_ops apm32_rtc_ops =
|
2022-03-08 12:03:06 +08:00
|
|
|
{
|
2023-01-05 14:15:02 +08:00
|
|
|
apm32_rtc_init,
|
|
|
|
apm32_rtc_get_secs,
|
|
|
|
apm32_rtc_set_secs,
|
2022-03-08 12:03:06 +08:00
|
|
|
RT_NULL,
|
|
|
|
RT_NULL,
|
2023-01-05 14:15:02 +08:00
|
|
|
#if defined(SOC_SERIES_APM32F0) || defined(SOC_SERIES_APM32F4)
|
|
|
|
apm32_rtc_get_timeval,
|
|
|
|
#else
|
2022-03-08 12:03:06 +08:00
|
|
|
RT_NULL,
|
2023-01-05 14:15:02 +08:00
|
|
|
#endif
|
2022-03-08 12:03:06 +08:00
|
|
|
RT_NULL,
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief RTC initialization function.
|
|
|
|
*
|
|
|
|
* @return RT_EOK indicates successful initialization, other value indicates failed;
|
|
|
|
*/
|
|
|
|
static int rt_hw_rtc_init(void)
|
|
|
|
{
|
|
|
|
rt_err_t result = RT_EOK;
|
|
|
|
|
2023-01-05 14:15:02 +08:00
|
|
|
apm32_rtc_dev.ops = &apm32_rtc_ops;
|
2022-03-08 12:03:06 +08:00
|
|
|
|
|
|
|
if (rt_hw_rtc_register(&apm32_rtc_dev, "rtc", RT_DEVICE_FLAG_RDWR, RT_NULL) != RT_EOK)
|
|
|
|
{
|
|
|
|
LOG_E("rtc init failed");
|
|
|
|
result = RT_ERROR;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LOG_D("rtc init success");
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
INIT_DEVICE_EXPORT(rt_hw_rtc_init);
|
|
|
|
|
|
|
|
#endif /* BSP_USING_ONCHIP_RTC */
|