1 /*
2  * Copyright (c) 2006-2022, Synwit Technology Co.,Ltd.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2021-07-01     lik          first version
9  */
10 
11 #include "drv_rtc.h"
12 
13 #ifdef RT_USING_RTC
14 #ifdef BSP_USING_RTC
15 
16 //#define DRV_DEBUG
17 #define LOG_TAG "drv.rtc"
18 #include <drv_log.h>
19 
20 static rt_rtc_dev_t swm_rtc_device;
21 
calcWeekDay(uint32_t year,uint32_t month,uint32_t date)22 static uint32_t calcWeekDay(uint32_t year, uint32_t month, uint32_t date)
23 {
24     uint32_t i, cnt = 0;
25     const uint32_t daysOfMonth[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
26 
27     for (i = 1; i < month; i++)
28         cnt += daysOfMonth[i];
29 
30     cnt += date;
31 
32     if ((year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0)) && (month >= 3))
33         cnt += 1;
34 
35     cnt += (year - 1901) * 365;
36 
37     for (i = 1901; i < year; i++)
38     {
39         if ((i % 4 == 0) && ((i % 100 != 0) || (i % 400 == 0)))
40             cnt += 1;
41     }
42 
43     return (cnt + 1) % 7;
44 }
45 
swm_get_rtc_time_stamp(void)46 static time_t swm_get_rtc_time_stamp(void)
47 {
48     RTC_DateTime get_datetime = {0};
49     struct tm tm_new;
50 
51     RTC_GetDateTime(RTC, &get_datetime);
52 
53     tm_new.tm_sec = get_datetime.Second;
54     tm_new.tm_min = get_datetime.Minute;
55     tm_new.tm_hour = get_datetime.Hour;
56     tm_new.tm_mday = get_datetime.Date;
57     tm_new.tm_mon = get_datetime.Month - 1;
58     tm_new.tm_year = get_datetime.Year - 1900;
59 
60     LOG_D("get rtc time.");
61     return mktime(&tm_new);
62 }
swm_set_rtc_time_stamp(time_t time_stamp)63 static rt_err_t swm_set_rtc_time_stamp(time_t time_stamp)
64 {
65     RTC_DateTime set_datetime = {0};
66     struct tm now;
67 
68     gmtime_r(&time_stamp, &now);
69     set_datetime.Second = now.tm_sec;
70     set_datetime.Minute = now.tm_min;
71     set_datetime.Hour = now.tm_hour;
72     set_datetime.Date = now.tm_mday;
73     set_datetime.Month = now.tm_mon + 1;
74     set_datetime.Year = now.tm_year + 1900;
75     // set_datetime.Day = now.tm_wday;
76 
77     RTC_Stop(RTC);
78     while (RTC->CFGABLE == 0)
79         ;
80     RTC->MINSEC = (set_datetime.Second << RTC_MINSEC_SEC_Pos) |
81                   (set_datetime.Minute << RTC_MINSEC_MIN_Pos);
82     RTC->DATHUR = (set_datetime.Hour << RTC_DATHUR_HOUR_Pos) |
83                   ((set_datetime.Date) << RTC_DATHUR_DATE_Pos);
84     RTC->MONDAY = (calcWeekDay(set_datetime.Year, set_datetime.Month, set_datetime.Date)
85                    << RTC_MONDAY_DAY_Pos) |
86                   ((set_datetime.Month) << RTC_MONDAY_MON_Pos);
87     RTC->YEAR = set_datetime.Year;
88     RTC->LOAD = 1 << RTC_LOAD_TIME_Pos;
89     RTC_Start(RTC);
90 
91     LOG_D("set rtc time.");
92     return RT_EOK;
93 }
94 
swm_rtc_configure(void)95 static rt_err_t swm_rtc_configure(void)
96 {
97     RTC_InitStructure rtc_initstruct;
98 
99     rtc_initstruct.clksrc = RTC_CLKSRC_XTAL32K;
100     rtc_initstruct.Year = 2020;
101     rtc_initstruct.Month = 2;
102     rtc_initstruct.Date = 28;
103     rtc_initstruct.Hour = 23;
104     rtc_initstruct.Minute = 59;
105     rtc_initstruct.Second = 55;
106     rtc_initstruct.SecondIEn = 0;
107     rtc_initstruct.MinuteIEn = 0;
108     RTC_Init(RTC, &rtc_initstruct);
109     RTC_Start(RTC);
110 
111     return RT_EOK;
112 }
113 
swm_rtc_get_secs(time_t * args)114 static rt_err_t swm_rtc_get_secs(time_t *args)
115 {
116     *args = swm_get_rtc_time_stamp();
117     LOG_D("RTC: get rtc_time %x\n", *args);
118 
119     return RT_EOK;
120 }
121 
swm_rtc_set_secs(time_t * args)122 static rt_err_t swm_rtc_set_secs(time_t *args)
123 {
124     rt_err_t result = RT_EOK;
125 
126     if (swm_set_rtc_time_stamp(*args))
127     {
128         result = -RT_ERROR;
129     }
130     LOG_D("RTC: set rtc_time %x\n", *args);
131 
132     return result;
133 }
134 
135 static const struct rt_rtc_ops swm_rtc_ops =
136 {
137     .init = swm_rtc_configure,
138     .get_secs = swm_rtc_get_secs,
139     .set_secs = swm_rtc_set_secs,
140     RT_NULL,
141     RT_NULL,
142     RT_NULL,
143     RT_NULL,
144 };
145 
swm_rtc_init(void)146 int swm_rtc_init(void)
147 {
148     rt_err_t result;
149 
150     swm_rtc_configure();
151 
152     swm_rtc_device.ops = &swm_rtc_ops;
153     result = rt_hw_rtc_register(&swm_rtc_device, "rtc", RT_DEVICE_FLAG_RDWR,RT_NULL);
154     if (result != RT_EOK)
155     {
156         LOG_E("rtc register err code: %d", result);
157     }
158     else
159     {
160         LOG_D("rtc register success.");
161     }
162     return result;
163 }
164 INIT_DEVICE_EXPORT(swm_rtc_init);
165 
166 #endif /* BSP_USING_RTC */
167 #endif /* RT_USING_RTC */
168