|
|
@@ -25,14 +25,22 @@
|
|
|
#ifndef RTC_BKP_DR1
|
|
|
#define RTC_BKP_DR1 RT_NULL
|
|
|
#endif
|
|
|
-
|
|
|
-//#define DRV_DEBUG
|
|
|
+#define DBG_ENABLE
|
|
|
+#define DBG_COLOR
|
|
|
#define LOG_TAG "drv.rtc"
|
|
|
+#define DBG_LVL DBG_LOG
|
|
|
#include <drv_log.h>
|
|
|
|
|
|
#define BKUP_REG_DATA 0xA0A0
|
|
|
|
|
|
static struct rt_device rtc;
|
|
|
+static struct rt_rtc_device rtc_device;
|
|
|
+
|
|
|
+#ifdef RT_USING_ALARM
|
|
|
+static rt_err_t rtc_alarm_time_set(struct rt_rtc_device* p_dev);
|
|
|
+static int rt_rtc_alarm_init(void);
|
|
|
+static RTC_AlarmTypeDef salarmstructure;
|
|
|
+#endif
|
|
|
|
|
|
static RTC_HandleTypeDef RTC_Handler;
|
|
|
uint32_t RTC_ReadTimeCounter(RTC_HandleTypeDef *hrtc);
|
|
|
@@ -54,8 +62,7 @@ static time_t get_rtc_timestamp(void)
|
|
|
// tm_new.tm_year = RTC_DateStruct.Year + 100;
|
|
|
//
|
|
|
// LOG_D("get rtc time.");
|
|
|
-// RTC_ReadTimeCounter(&RTC_Handler);
|
|
|
- //return mktime(&tm_new);
|
|
|
+// return mktime(&tm_new);
|
|
|
return RTC_ReadTimeCounter(&RTC_Handler);
|
|
|
}
|
|
|
|
|
|
@@ -79,18 +86,20 @@ static rt_err_t set_rtc_time_stamp(time_t time_stamp)
|
|
|
// RTC_DateStruct.Year = p_tm->tm_year - 100;
|
|
|
// RTC_DateStruct.WeekDay = p_tm->tm_wday + 1;
|
|
|
//
|
|
|
-// if (HAL_RTC_SetTime(&RTC_Handler, &RTC_TimeStruct, RTC_FORMAT_BIN) != HAL_OK)
|
|
|
+// if (HAL_RTC_SetDate(&RTC_Handler, &RTC_DateStruct, RTC_FORMAT_BIN) != HAL_OK)
|
|
|
// {
|
|
|
// return -RT_ERROR;
|
|
|
// }
|
|
|
-// if (HAL_RTC_SetDate(&RTC_Handler, &RTC_DateStruct, RTC_FORMAT_BIN) != HAL_OK)
|
|
|
+//
|
|
|
+// if (HAL_RTC_SetTime(&RTC_Handler, &RTC_TimeStruct, RTC_FORMAT_BIN) != HAL_OK)
|
|
|
// {
|
|
|
// return -RT_ERROR;
|
|
|
// }
|
|
|
//
|
|
|
+//
|
|
|
// LOG_D("set rtc time.");
|
|
|
- HAL_RTCEx_BKUPWrite(&RTC_Handler, RTC_BKP_DR1, BKUP_REG_DATA);
|
|
|
-// return RT_EOK;
|
|
|
+// HAL_RTCEx_BKUPWrite(&RTC_Handler, RTC_BKP_DR1, BKUP_REG_DATA);
|
|
|
+
|
|
|
RTC_WriteTimeCounter(&RTC_Handler,time_stamp);
|
|
|
return RT_EOK;
|
|
|
}
|
|
|
@@ -181,6 +190,9 @@ static rt_err_t rt_rtc_config(struct rt_device *dev)
|
|
|
static rt_err_t rt_rtc_control(rt_device_t dev, int cmd, void *args)
|
|
|
{
|
|
|
rt_err_t result = RT_EOK;
|
|
|
+#ifdef RT_USING_ALARM
|
|
|
+ struct rt_rtc_wkalarm *p_wkalarm = RT_NULL;
|
|
|
+#endif
|
|
|
RT_ASSERT(dev != RT_NULL);
|
|
|
switch (cmd)
|
|
|
{
|
|
|
@@ -190,12 +202,43 @@ static rt_err_t rt_rtc_control(rt_device_t dev, int cmd, void *args)
|
|
|
break;
|
|
|
|
|
|
case RT_DEVICE_CTRL_RTC_SET_TIME:
|
|
|
- if (set_rtc_time_stamp(*(rt_uint32_t *)args))
|
|
|
- {
|
|
|
+ if (set_rtc_time_stamp(*(rt_uint32_t *)args) != RT_EOK) {
|
|
|
+ // 错误处理
|
|
|
result = -RT_ERROR;
|
|
|
}
|
|
|
+#ifdef RT_USING_ALARM
|
|
|
+ rt_alarm_update(&rtc_device.parent, 1);
|
|
|
+#endif
|
|
|
+
|
|
|
LOG_D("RTC: set rtc_time %x\n", *(rt_uint32_t *)args);
|
|
|
break;
|
|
|
+#ifdef RT_USING_ALARM
|
|
|
+ case RT_DEVICE_CTRL_RTC_GET_ALARM:
|
|
|
+ args = &rtc_device.wkalarm;
|
|
|
+ LOG_D("GET_ALARM %d:%d:%d",rtc_device.wkalarm.tm_hour,
|
|
|
+ rtc_device.wkalarm.tm_min,rtc_device.wkalarm.tm_sec);
|
|
|
+ break;
|
|
|
+
|
|
|
+ case RT_DEVICE_CTRL_RTC_SET_ALARM:
|
|
|
+ LOG_D("RT_DEVICE_CTRL_RTC_SET_ALARM");
|
|
|
+ p_wkalarm = (struct rt_rtc_wkalarm *)args;
|
|
|
+ if (p_wkalarm != RT_NULL)
|
|
|
+ {
|
|
|
+ rtc_device.wkalarm.enable = p_wkalarm->enable;
|
|
|
+ rtc_device.wkalarm.tm_hour = p_wkalarm->tm_hour;
|
|
|
+ rtc_device.wkalarm.tm_min = p_wkalarm->tm_min;
|
|
|
+ rtc_device.wkalarm.tm_sec = p_wkalarm->tm_sec;
|
|
|
+ rtc_alarm_time_set(&rtc_device);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ result = -RT_ERROR;
|
|
|
+ LOG_E("RT_DEVICE_CTRL_RTC_SET_ALARM error!!");
|
|
|
+ }
|
|
|
+ LOG_D("SET_ALARM %d:%d:%d",p_wkalarm->tm_hour,
|
|
|
+ p_wkalarm->tm_min,p_wkalarm->tm_sec);
|
|
|
+ break;
|
|
|
+#endif
|
|
|
}
|
|
|
|
|
|
return result;
|
|
|
@@ -250,9 +293,68 @@ int rt_hw_rtc_init(void)
|
|
|
LOG_E("rtc register err code: %d", result);
|
|
|
return result;
|
|
|
}
|
|
|
+#ifdef RT_USING_ALARM
|
|
|
+ rt_rtc_alarm_init();
|
|
|
+#endif
|
|
|
LOG_D("rtc init success");
|
|
|
return RT_EOK;
|
|
|
}
|
|
|
+
|
|
|
+#ifdef RT_USING_ALARM
|
|
|
+void rt_rtc_alarm_enable(void)
|
|
|
+{
|
|
|
+ HAL_RTC_SetAlarm_IT(&RTC_Handler,&salarmstructure,RTC_FORMAT_BIN);
|
|
|
+ HAL_RTC_GetAlarm(&RTC_Handler,&salarmstructure,RTC_ALARM_A,RTC_FORMAT_BIN);
|
|
|
+ LOG_D("alarm read:%d:%d:%d", salarmstructure.AlarmTime.Hours,
|
|
|
+ salarmstructure.AlarmTime.Minutes,
|
|
|
+ salarmstructure.AlarmTime.Seconds);
|
|
|
+ HAL_NVIC_SetPriority(RTC_Alarm_IRQn, 0x02, 0);
|
|
|
+ HAL_NVIC_EnableIRQ(RTC_Alarm_IRQn);
|
|
|
+}
|
|
|
+
|
|
|
+void rt_rtc_alarm_disable(void)
|
|
|
+{
|
|
|
+ HAL_RTC_DeactivateAlarm(&RTC_Handler, RTC_ALARM_A);
|
|
|
+ HAL_NVIC_DisableIRQ(RTC_Alarm_IRQn);
|
|
|
+}
|
|
|
+
|
|
|
+static int rt_rtc_alarm_init(void)
|
|
|
+{
|
|
|
+ return RT_EOK;
|
|
|
+}
|
|
|
+
|
|
|
+static rt_err_t rtc_alarm_time_set(struct rt_rtc_device* p_dev)
|
|
|
+{
|
|
|
+ if (p_dev->wkalarm.enable)
|
|
|
+ {
|
|
|
+ salarmstructure.Alarm = RTC_ALARM_A;
|
|
|
+ salarmstructure.AlarmTime.Hours = p_dev->wkalarm.tm_hour;
|
|
|
+ salarmstructure.AlarmTime.Minutes = p_dev->wkalarm.tm_min;
|
|
|
+ salarmstructure.AlarmTime.Seconds = p_dev->wkalarm.tm_sec;
|
|
|
+ LOG_D("alarm set:%d:%d:%d", salarmstructure.AlarmTime.Hours,
|
|
|
+ salarmstructure.AlarmTime.Minutes,
|
|
|
+ salarmstructure.AlarmTime.Seconds);
|
|
|
+ rt_rtc_alarm_enable();
|
|
|
+ }
|
|
|
+
|
|
|
+ return RT_EOK;
|
|
|
+}
|
|
|
+
|
|
|
+void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
|
|
|
+{
|
|
|
+ //LOG_D("rtc alarm isr.\n");
|
|
|
+ rt_alarm_update(&rtc_device.parent, 1);
|
|
|
+}
|
|
|
+
|
|
|
+void RTC_Alarm_IRQHandler(void)
|
|
|
+{
|
|
|
+ rt_interrupt_enter();
|
|
|
+ HAL_RTC_AlarmIRQHandler(&RTC_Handler);
|
|
|
+ rt_interrupt_leave();
|
|
|
+}
|
|
|
+
|
|
|
+#endif
|
|
|
+
|
|
|
INIT_APP_EXPORT(rt_hw_rtc_init);
|
|
|
|
|
|
#endif /* BSP_USING_ONCHIP_RTC */
|