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 * 2018-12-10 Zohar_Lee first version
9 * 2020-07-10 lik format file
10 */
11
12 #include "drv_rtc.h"
13
14 #ifdef RT_USING_RTC
15 #ifdef BSP_USING_RTC
16
17 //#define DRV_DEBUG
18 #define LOG_TAG "drv.rtc"
19 #include <drv_log.h>
20
21 static rt_rtc_dev_t swm_rtc_device;
22
calcWeekDay(uint32_t year,uint32_t month,uint32_t date)23 static uint32_t calcWeekDay(uint32_t year, uint32_t month, uint32_t date)
24 {
25 uint32_t i, cnt = 0;
26 const uint32_t daysOfMonth[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
27
28 for (i = 1; i < month; i++)
29 cnt += daysOfMonth[i];
30
31 cnt += date;
32
33 if ((year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0)) && (month >= 3))
34 cnt += 1;
35
36 cnt += (year - 1901) * 365;
37
38 for (i = 1901; i < year; i++)
39 {
40 if ((i % 4 == 0) && ((i % 100 != 0) || (i % 400 == 0)))
41 cnt += 1;
42 }
43
44 return (cnt + 1) % 7;
45 }
46
swm_get_rtc_time_stamp(void)47 static time_t swm_get_rtc_time_stamp(void)
48 {
49 RTC_DateTime get_datetime = {0};
50 struct tm tm_new;
51
52 RTC_GetDateTime(RTC, &get_datetime);
53
54 tm_new.tm_sec = get_datetime.Second;
55 tm_new.tm_min = get_datetime.Minute;
56 tm_new.tm_hour = get_datetime.Hour;
57 tm_new.tm_mday = get_datetime.Date;
58 tm_new.tm_mon = get_datetime.Month - 1;
59 tm_new.tm_year = get_datetime.Year - 1900;
60
61 LOG_D("get rtc time.");
62 return mktime(&tm_new);
63 }
swm_set_rtc_time_stamp(time_t time_stamp)64 static rt_err_t swm_set_rtc_time_stamp(time_t time_stamp)
65 {
66 RTC_DateTime set_datetime = {0};
67 struct tm now;
68
69 gmtime_r(&time_stamp, &now);
70 set_datetime.Second = now.tm_sec;
71 set_datetime.Minute = now.tm_min;
72 set_datetime.Hour = now.tm_hour;
73 set_datetime.Date = now.tm_mday;
74 set_datetime.Month = now.tm_mon + 1;
75 set_datetime.Year = now.tm_year + 1900;
76 // set_datetime.Day = now.tm_wday;
77
78 RTC_Stop(RTC);
79 while (RTC->CFGABLE == 0)
80 ;
81 RTC->MINSEC = (set_datetime.Second << RTC_MINSEC_SEC_Pos) |
82 (set_datetime.Minute << RTC_MINSEC_MIN_Pos);
83 RTC->DATHUR = (set_datetime.Hour << RTC_DATHUR_HOUR_Pos) |
84 ((set_datetime.Date) << RTC_DATHUR_DATE_Pos);
85 RTC->MONDAY = (calcWeekDay(set_datetime.Year, set_datetime.Month, set_datetime.Date)
86 << RTC_MONDAY_DAY_Pos) |
87 ((set_datetime.Month) << RTC_MONDAY_MON_Pos);
88 RTC->YEAR = set_datetime.Year;
89 RTC->LOAD = 1 << RTC_LOAD_TIME_Pos;
90 RTC_Start(RTC);
91
92 LOG_D("set rtc time.");
93 return RT_EOK;
94 }
95
swm_rtc_configure(void)96 static rt_err_t swm_rtc_configure(void)
97 {
98 RTC_InitStructure rtc_initstruct;
99
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