|
| 1 | +/* |
| 2 | + * Copyright (c) 2006-2022, RT-Thread Development Team |
| 3 | + * Copyright (c) 2022, xiaoxiaolisunny |
| 4 | + * |
| 5 | + * SPDX-License-Identifier: Apache-2.0 |
| 6 | + * |
| 7 | + * Change Logs: |
| 8 | + * Date Author Notes |
| 9 | + * 2022-06-10 xiaoxiaolisunny first version |
| 10 | + */ |
| 11 | + |
| 12 | +#include <board.h> |
| 13 | +#include <rtdbg.h> |
| 14 | +#include <rtthread.h> |
| 15 | +#include <rtdevice.h> |
| 16 | +#include <sys/time.h> |
| 17 | + |
| 18 | +#ifdef BSP_USING_RTC |
| 19 | + |
| 20 | +static rt_rtc_dev_t hc32_rtc_dev; |
| 21 | + |
| 22 | +static rt_err_t hc32_rtc_get_time_stamp(struct timeval *tv) |
| 23 | +{ |
| 24 | + stc_rtc_time_t stcRtcTime = {0}; |
| 25 | + stc_rtc_date_t stcRtcDate = {0}; |
| 26 | + struct tm tm_new = {0}; |
| 27 | + |
| 28 | + RTC_GetTime(RTC_DATA_FMT_DEC, &stcRtcTime); |
| 29 | + RTC_GetDate(RTC_DATA_FMT_DEC, &stcRtcDate); |
| 30 | + |
| 31 | + tm_new.tm_sec = stcRtcTime.u8Second; |
| 32 | + tm_new.tm_min = stcRtcTime.u8Minute; |
| 33 | + tm_new.tm_hour = stcRtcTime.u8Hour; |
| 34 | + if(stcRtcDate.u8Month == 0) |
| 35 | + { |
| 36 | + tm_new.tm_mday = stcRtcDate.u8Day + 1; |
| 37 | + tm_new.tm_mon = stcRtcDate.u8Month; |
| 38 | + } |
| 39 | + else |
| 40 | + { |
| 41 | + tm_new.tm_mday = stcRtcDate.u8Day ; |
| 42 | + tm_new.tm_mon = stcRtcDate.u8Month - 1; |
| 43 | + } |
| 44 | + tm_new.tm_year = stcRtcDate.u8Year + 100; |
| 45 | + |
| 46 | + tv->tv_sec = timegm(&tm_new); |
| 47 | + |
| 48 | + return RT_EOK; |
| 49 | +} |
| 50 | + |
| 51 | +static rt_err_t hc32_rtc_set_time_stamp(time_t time_stamp) |
| 52 | +{ |
| 53 | + stc_rtc_time_t stcRtcTime = {0}; |
| 54 | + stc_rtc_date_t stcRtcDate = {0}; |
| 55 | + struct tm p_tm = {0}; |
| 56 | + |
| 57 | + gmtime_r(&time_stamp, &p_tm); |
| 58 | + |
| 59 | + if (p_tm.tm_year < 100) |
| 60 | + { |
| 61 | + return -RT_ERROR; |
| 62 | + } |
| 63 | + |
| 64 | + stcRtcTime.u8Second = p_tm.tm_sec ; |
| 65 | + stcRtcTime.u8Minute = p_tm.tm_min ; |
| 66 | + stcRtcTime.u8Hour = p_tm.tm_hour; |
| 67 | + stcRtcDate.u8Day = p_tm.tm_mday; |
| 68 | + stcRtcDate.u8Month = p_tm.tm_mon + 1; |
| 69 | + stcRtcDate.u8Year = p_tm.tm_year - 100; |
| 70 | + stcRtcDate.u8Weekday = p_tm.tm_wday; |
| 71 | + |
| 72 | + if (LL_OK != RTC_SetTime(RTC_DATA_FMT_DEC, &stcRtcTime)) |
| 73 | + { |
| 74 | + return -RT_ERROR; |
| 75 | + } |
| 76 | + if (LL_OK != RTC_SetDate(RTC_DATA_FMT_DEC, &stcRtcDate)) |
| 77 | + { |
| 78 | + return -RT_ERROR; |
| 79 | + } |
| 80 | + |
| 81 | + LOG_D("set rtc time."); |
| 82 | + return RT_EOK; |
| 83 | +} |
| 84 | + |
| 85 | +static rt_err_t hc32_rtc_init(void) |
| 86 | +{ |
| 87 | + stc_rtc_init_t stcRtcInit; |
| 88 | + |
| 89 | +#ifdef BSP_RTC_USING_XTAL32 |
| 90 | + stc_clock_xtal32_init_t stcXtal32Init; |
| 91 | + /* Xtal32 config */ |
| 92 | + stcXtal32Init.u8State = CLK_XTAL32_ON; |
| 93 | + stcXtal32Init.u8Drv = CLK_XTAL32_DRV_HIGH; |
| 94 | + stcXtal32Init.u8Filter = CLK_XTAL32_FILTER_RUN_MD; |
| 95 | + (void)CLK_Xtal32Init(&stcXtal32Init); |
| 96 | + /* Waiting for XTAL32 stabilization */ |
| 97 | + rt_thread_delay(100); |
| 98 | +#endif |
| 99 | + |
| 100 | + /* RTC stopped */ |
| 101 | + if (DISABLE == RTC_GetCounterState()) |
| 102 | + { |
| 103 | + /* Reset RTC counter */ |
| 104 | + if (LL_ERR_TIMEOUT == RTC_DeInit()) |
| 105 | + { |
| 106 | + return -RT_ERROR; |
| 107 | + } |
| 108 | + else |
| 109 | + { |
| 110 | + /* Configure structure initialization */ |
| 111 | + (void)RTC_StructInit(&stcRtcInit); |
| 112 | + /* Configuration RTC structure */ |
| 113 | + #ifdef BSP_RTC_USING_XTAL32 |
| 114 | + stcRtcInit.u8ClockSrc = RTC_CLK_SRC_XTAL32; |
| 115 | + #else |
| 116 | + stcRtcInit.u8ClockSrc = RTC_CLK_SRC_LRC; |
| 117 | + #endif |
| 118 | + stcRtcInit.u8HourFormat = RTC_HOUR_FMT_24H; |
| 119 | + (void)RTC_Init(&stcRtcInit); |
| 120 | + /* Startup RTC count */ |
| 121 | + RTC_Cmd(ENABLE); |
| 122 | + } |
| 123 | + } |
| 124 | + return RT_EOK; |
| 125 | +} |
| 126 | + |
| 127 | +static rt_err_t hc32_rtc_get_secs(time_t *sec) |
| 128 | +{ |
| 129 | + struct timeval tv; |
| 130 | + |
| 131 | + hc32_rtc_get_time_stamp(&tv); |
| 132 | + *(time_t *) sec = tv.tv_sec; |
| 133 | + LOG_D("RTC: get rtc_time %d", *sec); |
| 134 | + |
| 135 | + return RT_EOK; |
| 136 | +} |
| 137 | + |
| 138 | +static rt_err_t hc32_rtc_set_secs(time_t *sec) |
| 139 | +{ |
| 140 | + rt_err_t result = RT_EOK; |
| 141 | + |
| 142 | + if (hc32_rtc_set_time_stamp(*sec)) |
| 143 | + { |
| 144 | + result = -RT_ERROR; |
| 145 | + } |
| 146 | + LOG_D("RTC: set rtc_time %d", *sec); |
| 147 | + |
| 148 | + return result; |
| 149 | +} |
| 150 | + |
| 151 | +const static struct rt_rtc_ops hc32_rtc_ops = |
| 152 | +{ |
| 153 | + hc32_rtc_init, |
| 154 | + hc32_rtc_get_secs, |
| 155 | + hc32_rtc_set_secs, |
| 156 | + RT_NULL, |
| 157 | + RT_NULL, |
| 158 | + hc32_rtc_get_time_stamp, |
| 159 | + RT_NULL |
| 160 | +}; |
| 161 | + |
| 162 | +int rt_hw_rtc_init(void) |
| 163 | +{ |
| 164 | + rt_err_t result; |
| 165 | + |
| 166 | + hc32_rtc_dev.ops = &hc32_rtc_ops; |
| 167 | + result = rt_hw_rtc_register(&hc32_rtc_dev, "rtc", RT_DEVICE_FLAG_RDWR, RT_NULL); |
| 168 | + if (result != RT_EOK) |
| 169 | + { |
| 170 | + LOG_E("rtc register err code: %d", result); |
| 171 | + return result; |
| 172 | + } |
| 173 | + LOG_D("rtc init success"); |
| 174 | + return RT_EOK; |
| 175 | +} |
| 176 | +INIT_DEVICE_EXPORT(rt_hw_rtc_init); |
| 177 | + |
| 178 | +#endif /* BSP_USING_RTC */ |
0 commit comments