VSF Documented
rtc_template.inc
Go to the documentation of this file.
1/*****************************************************************************
2 * Copyright(C)2009-2022 by VSF Team *
3 * *
4 * Licensed under the Apache License, Version 2.0 (the "License"); *
5 * you may not use this file except in compliance with the License. *
6 * You may obtain a copy of the License at *
7 * *
8 * http://www.apache.org/licenses/LICENSE-2.0 *
9 * *
10 * Unless required by applicable law or agreed to in writing, software *
11 * distributed under the License is distributed on an "AS IS" BASIS, *
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
13 * See the License for the specific language governing permissions and *
14 * limitations under the License. *
15 * *
16 ****************************************************************************/
17
18#if VSF_HAL_USE_RTC == ENABLED
19
20/*============================ INCLUDES ======================================*/
21/*============================ MACROS ========================================*/
22
23#if VSF_RTC_CFG_GET_API_TEMPLATE == ENABLED && VSF_RTC_CFG_GET_TIME_API_TEMPLATE == ENABLED
24# error "VSF_RTC_CFG_GET_API_TEMPLATE and VSF_RTC_CFG_GET_TIME_API_TEMPLATE Only one can be defined at a time"
25#endif
26
27#if VSF_RTC_CFG_SET_API_TEMPLATE == ENABLED && VSF_RTC_CFG_SET_TIME_API_TEMPLATE == ENABLED
28# error "VSF_RTC_CFG_SET_API_TEMPLATE and VSF_RTC_CFG_SET_TIME_API_TEMPLATE Only one can be defined at a time"
29#endif
30
31#ifndef VSF_RTC_CFG_REIMPLEMENT_API_CAPABILITY
32# define VSF_RTC_CFG_REIMPLEMENT_API_CAPABILITY DISABLED
33#endif
34
35#ifdef VSF_RTC_CFG_IMP_REMAP_PREFIX
36# undef VSF_RTC_CFG_REIMPLEMENT_API_CAPABILITY
37# define VSF_RTC_CFG_REIMPLEMENT_API_CAPABILITY ENABLED
38#endif
39
40#if VSF_RTC_CFG_REIMPLEMENT_API_CAPABILITY == DISABLED
41# ifndef VSF_RTC_CFG_CAPABILITY_T_IRQ_MASK
42# define VSF_RTC_CFG_CAPABILITY_T_IRQ_MASK VSF_RTC_IRQ_ALL_BITS_MASK
43# endif
44#endif
45
46#define vsf_real_rtc_t VSF_MCONNECT(VSF_RTC_CFG_IMP_PREFIX, _rtc_t)
47#define vsf_real_rtc_capability VSF_MCONNECT(VSF_RTC_CFG_IMP_PREFIX, _rtc_capability)
48#define vsf_real_rtc_get VSF_MCONNECT(VSF_RTC_CFG_IMP_PREFIX, _rtc_get)
49#define vsf_real_rtc_set VSF_MCONNECT(VSF_RTC_CFG_IMP_PREFIX, _rtc_set)
50#define vsf_real_rtc_get_time VSF_MCONNECT(VSF_RTC_CFG_IMP_PREFIX, _rtc_get_time)
51#define vsf_real_rtc_set_time VSF_MCONNECT(VSF_RTC_CFG_IMP_PREFIX, _rtc_set_time)
52
53/*============================ MACROFIED FUNCTIONS ===========================*/
54/*============================ TYPES =========================================*/
55/*============================ GLOBAL VARIABLES ==============================*/
56/*============================ LOCAL VARIABLES ===============================*/
57/*============================ PROTOTYPES ====================================*/
58/*============================ IMPLEMENTATION ================================*/
59
60#if VSF_RTC_CFG_GET_API_TEMPLATE == ENABLED
61vsf_err_t vsf_real_rtc_get(vsf_real_rtc_t *rtc_ptr, vsf_rtc_tm_t *rtc_tm)
62{
63 VSF_HAL_ASSERT(rtc_ptr != NULL);
64 VSF_HAL_ASSERT(rtc_tm != NULL);
65
66 time_t second, milliseconds;
67 vsf_err_t result = vsf_real_rtc_get_time(rtc_ptr, &second, &milliseconds);
68 if (result != VSF_ERR_NONE) {
69 return result;
70 }
71
72 struct tm *t = gmtime(&second);
73 if (t == NULL) {
74 return VSF_ERR_FAIL;
75 }
76
77 rtc_tm->tm_sec = t->tm_sec;
78 rtc_tm->tm_min = t->tm_min;
79 rtc_tm->tm_hour = t->tm_hour;
80 rtc_tm->tm_mday = t->tm_mday;
81 rtc_tm->tm_wday = t->tm_wday + 1;
82 rtc_tm->tm_mon = t->tm_mon + 1;
83 rtc_tm->tm_year = t->tm_year + 1900;
84 rtc_tm->tm_ms = milliseconds;
85
86 return VSF_ERR_NONE;
87}
88#endif
89
90#if VSF_RTC_CFG_SET_API_TEMPLATE == ENABLED
91vsf_err_t vsf_real_rtc_set(vsf_real_rtc_t *rtc_ptr, const vsf_rtc_tm_t *rtc_tm)
92{
93 VSF_HAL_ASSERT(rtc_ptr != NULL);
94 VSF_HAL_ASSERT(rtc_tm != NULL);
95
96 struct tm t = {
97 .tm_sec = rtc_tm->tm_sec,
98 .tm_min = rtc_tm->tm_min,
99 .tm_hour = rtc_tm->tm_hour,
100 .tm_mday = rtc_tm->tm_mday,
101 .tm_mon = rtc_tm->tm_mon - 1,
102 .tm_year = rtc_tm->tm_year - 1900,
103 };
104
105 time_t second = mktime(&t);
106 return vsf_real_rtc_set_time(rtc_ptr, second, rtc_tm->tm_ms);
107}
108#endif
109
110#if VSF_RTC_CFG_GET_TIME_API_TEMPLATE == ENABLED
111vsf_err_t vsf_real_rtc_get_time(vsf_hw_rtc_t *rtc_ptr, vsf_rtc_time_t *second_ptr, vsf_rtc_time_t *milliseconds_ptr)
112{
113 VSF_HAL_ASSERT(rtc_ptr != NULL);
114 VSF_HAL_ASSERT((second_ptr != NULL) || (milliseconds_ptr != NULL));
115
116 vsf_rtc_tm_t rtc_tm;
117 vsf_err_t result = vsf_real_rtc_get(rtc_ptr, &rtc_tm);
118 if (result != VSF_ERR_NONE) {
119 return result;
120 }
121
122 if (second_ptr != NULL) {
123 struct tm t = {
124 .tm_sec = rtc_tm.tm_sec,
125 .tm_min = rtc_tm.tm_min,
126 .tm_hour = rtc_tm.tm_hour,
127 .tm_mday = rtc_tm.tm_mday,
128 .tm_mon = rtc_tm.tm_mon - 1,
129 .tm_year = rtc_tm.tm_year - 1900,
130 };
131 *second_ptr = mktime(&t);
132 }
133
134 if (milliseconds_ptr != NULL) {
135 *milliseconds_ptr = rtc_tm.tm_ms;
136 }
137
138 return VSF_ERR_NONE;
139}
140#endif
141
142#if VSF_RTC_CFG_SET_TIME_API_TEMPLATE == ENABLED
143vsf_err_t vsf_real_rtc_set_time(vsf_real_rtc_t *rtc_ptr, vsf_rtc_time_t second, vsf_rtc_time_t milliseconds)
144{
145 VSF_HAL_ASSERT(rtc_ptr != NULL);
146
147 struct tm *t = gmtime((const time_t *)&second);
148 if (NULL == t) {
149 return VSF_ERR_FAIL;
150 }
151
152 vsf_rtc_tm_t rtc_tm = {
153 .tm_sec = t->tm_sec,
154 .tm_min = t->tm_min,
155 .tm_hour = t->tm_hour,
156 .tm_mday = t->tm_mday,
157 .tm_mon = t->tm_mon + 1,
158 .tm_year = t->tm_year + 1900,
159 .tm_ms = milliseconds,
160 };
161
162 return vsf_real_rtc_set(rtc_ptr, &rtc_tm);
163}
164#endif
165
166#if VSF_RTC_CFG_REIMPLEMENT_API_CAPABILITY == DISABLED
167vsf_rtc_capability_t vsf_real_rtc_capability(vsf_real_rtc_t *rtc_ptr)
168{
169 vsf_rtc_capability_t rtc_capability = {
171 };
172
173 return rtc_capability;
174}
175#endif
176
177/*============================ MACROS ========================================*/
178
179#undef VSF_RTC_CFG_REIMPLEMENT_API_CAPABILITY
180#undef VSF_RTC_CFG_CAPABILITY_T_IRQ_MASK
181#undef vsf_real_rtc_t
182#undef vsf_real_rtc_capability
183
184/*============================ MACROS ========================================*/
185
186#ifdef VSF_RTC_CFG_IMP_REMAP_PREFIX
187
188# define vsf_imp_rtc_t VSF_MCONNECT(VSF_RTC_CFG_IMP_PREFIX, _rtc_t)
189# define vsf_imp_rtc_init VSF_MCONNECT(VSF_RTC_CFG_IMP_PREFIX, _rtc_init)
190# define vsf_imp_rtc_enable VSF_MCONNECT(VSF_RTC_CFG_IMP_PREFIX, _rtc_enable)
191# define vsf_imp_rtc_disable VSF_MCONNECT(VSF_RTC_CFG_IMP_PREFIX, _rtc_disable)
192# define vsf_imp_rtc_capability VSF_MCONNECT(VSF_RTC_CFG_IMP_PREFIX, _rtc_capability)
193# define vsf_imp_rtc_get VSF_MCONNECT(VSF_RTC_CFG_IMP_PREFIX, _rtc_get)
194# define vsf_imp_rtc_set VSF_MCONNECT(VSF_RTC_CFG_IMP_PREFIX, _rtc_set)
195# define vsf_imp_rtc_get_time VSF_MCONNECT(VSF_RTC_CFG_IMP_PREFIX, _rtc_get_time)
196# define vsf_imp_rtc_set_time VSF_MCONNECT(VSF_RTC_CFG_IMP_PREFIX, _rtc_set_time)
197
198# define vsf_remap_rtc_t VSF_MCONNECT(VSF_RTC_CFG_IMP_REMAP_PREFIX, _rtc_t)
199# define vsf_remap_rtc_init VSF_MCONNECT(VSF_RTC_CFG_IMP_REMAP_PREFIX, _rtc_init)
200# define vsf_remap_rtc_enable VSF_MCONNECT(VSF_RTC_CFG_IMP_REMAP_PREFIX, _rtc_enable)
201# define vsf_remap_rtc_disable VSF_MCONNECT(VSF_RTC_CFG_IMP_REMAP_PREFIX, _rtc_disable)
202# define vsf_remap_rtc_capability VSF_MCONNECT(VSF_RTC_CFG_IMP_REMAP_PREFIX, _rtc_capability)
203# define vsf_remap_rtc_get VSF_MCONNECT(VSF_RTC_CFG_IMP_REMAP_PREFIX, _rtc_get)
204# define vsf_remap_rtc_set VSF_MCONNECT(VSF_RTC_CFG_IMP_REMAP_PREFIX, _rtc_set)
205# define vsf_remap_rtc_get_time VSF_MCONNECT(VSF_RTC_CFG_IMP_REMAP_PREFIX, _rtc_get_time)
206# define vsf_remap_rtc_set_time VSF_MCONNECT(VSF_RTC_CFG_IMP_REMAP_PREFIX, _rtc_set_time)
207
208# define VSF_RTC_CFG_IMP_REMAP_FUNCTIONS \
209 vsf_err_t vsf_imp_rtc_init(vsf_imp_rtc_t *rtc_ptr, vsf_rtc_cfg_t *cfg_ptr) \
210 { \
211 VSF_HAL_ASSERT(rtc_ptr != NULL); \
212 return vsf_remap_rtc_init(rtc_ptr, cfg_ptr); \
213 } \
214 void vsf_imp_rtc_fini(vsf_imp_rtc_t *rtc_ptr) \
215 { \
216 VSF_HAL_ASSERT(rtc_ptr != NULL); \
217 vsf_remap_rtc_fini(rtc_ptr); \
218 } \
219 fsm_rt_t vsf_imp_rtc_enable(vsf_imp_rtc_t *rtc_ptr) \
220 { \
221 VSF_HAL_ASSERT(rtc_ptr != NULL); \
222 return vsf_remap_rtc_enable(rtc_ptr); \
223 } \
224 fsm_rt_t vsf_imp_rtc_disable(vsf_imp_rtc_t *rtc_ptr) \
225 { \
226 VSF_HAL_ASSERT(rtc_ptr != NULL); \
227 return vsf_remap_rtc_disable(rtc_ptr); \
228 } \
229 vsf_err_t vsf_imp_rtc_get(vsf_imp_rtc_t *rtc_ptr, vsf_imp_rtc_tm_t *rtc_tm) \
230 { \
231 VSF_HAL_ASSERT(rtc_ptr != NULL); \
232 return vsf_remap_rtc_get(rtc_ptr, rtc_tm); \
233 } \
234 vsf_err_t vsf_imp_rtc_set(vsf_imp_rtc_t *rtc_ptr, const vsf_imp_rtc_tm_t *rtc_tm) \
235 { \
236 VSF_HAL_ASSERT(rtc_ptr != NULL); \
237 return vsf_remap_rtc_set(rtc_ptr, rtc_tm); \
238 } \
239 vsf_err_t vsf_imp_rtc_get_time(vsf_imp_rtc_t *rtc_ptr, vsf_imp_rtc_time_t *second_ptr,\
240 vsf_imp_rtc_time_t *millisecond_ptr) \
241 { \
242 VSF_HAL_ASSERT(rtc_ptr != NULL); \
243 return vsf_remap_rtc_get_time(rtc_ptr, second_ptr, millisecond_ptr); \
244 } \
245 vsf_err_t vsf_imp_rtc_set_time(vsf_imp_rtc_t *rtc_ptr, vsf_imp_rtc_time_t second, \
246 vsf_imp_rtc_time_t millisecond) \
247 { \
248 VSF_HAL_ASSERT(rtc_ptr != NULL); \
249 return vsf_remap_rtc_set_time(rtc_ptr, second, millisecond); \
250 } \
251 vsf_rtc_capability_t vsf_imp_rtc_capability(vsf_imp_rtc_t *rtc_ptr) \
252 { \
253 VSF_HAL_ASSERT(rtc_ptr != NULL); \
254 return vsf_remap_rtc_capability(rtc_ptr); \
255 }
256#endif
257
258/*============================ GLOBAL VARIABLES ==============================*/
259
260#define VSF_HAL_TEMPLATE_IMP_NAME _rtc
261#define VSF_HAL_TEMPLATE_IMP_UPCASE_NAME _RTC
262
263#ifndef VSF_RTC_CFG_IMP_PREFIX
264# error "Please define VSF_RTC_CFG_IMP_PREFIX in rtc driver"
265#endif
266
267#ifndef VSF_RTC_CFG_IMP_UPCASE_PREFIX
268# error "Please define VSF_RTC_CFG_IMP_UPCASE_PREFIX in rtc driver"
269#endif
270
271#ifndef VSF_RTC_CFG_IMP_COUNT_MASK_PREFIX
272# define VSF_RTC_CFG_IMP_COUNT_MASK_PREFIX VSF_RTC_CFG_IMP_UPCASE_PREFIX
273#endif
274
275#ifdef VSF_RTC_CFG_IMP_REMAP_FUNCTIONS
276# define VSF_HAL_CFG_IMP_REMAP_FUNCTIONS VSF_RTC_CFG_IMP_REMAP_FUNCTIONS
277#endif
278
280
281#undef VSF_RTC_CFG_IMP_PREFIX
282#undef VSF_RTC_CFG_IMP_COUNT_MASK_PREFIX
283#undef VSF_RTC_CFG_IMP_UPCASE_PREFIX
284#undef VSF_RTC_CFG_IMP_LV0
285#undef VSF_RTC_CFG_IMP_REMAP_FUNCTIONS
286#undef VSF_RTC_CFG_IMP_HAS_OP
287#undef VSF_RTC_CFG_IMP_EXTERN_OP
288
289#undef vsf_imp_rtc_t
290#undef vsf_imp_rtc_init
291#undef vsf_imp_rtc_enable
292#undef vsf_imp_rtc_disable
293#undef vsf_imp_rtc_capability
294#undef vsf_imp_rtc_get
295#undef vsf_imp_rtc_set
296#undef vsf_imp_rtc_get_time
297#undef vsf_imp_rtc_set_time
298
299#undef vsf_remap_rtc_t
300#undef vsf_remap_rtc_init
301#undef vsf_remap_rtc_enable
302#undef vsf_remap_rtc_disable
303#undef vsf_remap_rtc_capability
304#undef vsf_remap_rtc_get
305#undef vsf_remap_rtc_set
306#undef vsf_remap_rtc_get_time
307#undef vsf_remap_rtc_set_time
308
309#endif /* VSF_HAL_USE_RTC */
310
vsf_err_t
Definition __type.h:42
@ VSF_ERR_NONE
none error
Definition __type.h:44
@ VSF_ERR_FAIL
failed
Definition __type.h:51
#define VSF_RTC_CFG_CAPABILITY_IRQ_MASK
Definition rtc.c:148
#define NULL
Definition stddef.h:52
__TIME_T time_t
Definition types.h:122
struct tm * gmtime(const time_t *t)
Definition vsf_linux_glibc_time.c:338
time_t mktime(struct tm *tm)
Definition vsf_linux_glibc_time.c:70
Definition time.h:52
int tm_mon
Definition time.h:57
int tm_year
Definition time.h:58
int tm_hour
Definition time.h:55
int tm_sec
Definition time.h:53
int tm_mday
Definition time.h:56
int tm_min
Definition time.h:54
int tm_wday
Definition time.h:59
Definition rtc.c:35
Definition vsf_template_rtc.h:143
Definition vsf_template_rtc.h:114
uint16_t tm_ms
Definition vsf_template_rtc.h:122
uint8_t tm_sec
Definition vsf_template_rtc.h:115
uint16_t tm_year
Definition vsf_template_rtc.h:121
uint8_t tm_mon
Definition vsf_template_rtc.h:120
uint8_t tm_wday
Definition vsf_template_rtc.h:119
uint8_t tm_mday
Definition vsf_template_rtc.h:118
uint8_t tm_hour
Definition vsf_template_rtc.h:117
uint8_t tm_min
Definition vsf_template_rtc.h:116
#define VSF_HAL_ASSERT(__CON)
all hal modules use this configuration file
Definition vsf_hal_cfg.h:36
VSF_RTC_CFG_TIME_TYPE vsf_rtc_time_t
Definition vsf_template_rtc.h:112