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_GET_CONFIGURATION
32# define VSF_RTC_CFG_REIMPLEMENT_API_GET_CONFIGURATION DISABLED
33#endif
34
35#ifndef VSF_RTC_CFG_REIMPLEMENT_API_CAPABILITY
36# define VSF_RTC_CFG_REIMPLEMENT_API_CAPABILITY DISABLED
37#endif
38
39#ifdef VSF_RTC_CFG_IMP_REMAP_PREFIX
40# undef VSF_RTC_CFG_REIMPLEMENT_API_CAPABILITY
41# define VSF_RTC_CFG_REIMPLEMENT_API_CAPABILITY ENABLED
42# undef VSF_RTC_CFG_REIMPLEMENT_API_GET_CONFIGURATION
43# define VSF_RTC_CFG_REIMPLEMENT_API_GET_CONFIGURATION ENABLED
44#endif
45
46#if VSF_RTC_CFG_REIMPLEMENT_API_CAPABILITY == DISABLED
47# ifndef VSF_RTC_CFG_CAPABILITY_T_IRQ_MASK
48# define VSF_RTC_CFG_CAPABILITY_T_IRQ_MASK VSF_RTC_IRQ_ALL_BITS_MASK
49# endif
50#endif
51
52#define vsf_real_rtc_t VSF_MCONNECT(VSF_RTC_CFG_IMP_PREFIX, _rtc_t)
53#define vsf_real_rtc_capability VSF_MCONNECT(VSF_RTC_CFG_IMP_PREFIX, _rtc_capability)
54#define vsf_real_rtc_get_configuration VSF_MCONNECT(VSF_RTC_CFG_IMP_PREFIX, _rtc_get_configuration)
55#define vsf_real_rtc_get VSF_MCONNECT(VSF_RTC_CFG_IMP_PREFIX, _rtc_get)
56#define vsf_real_rtc_set VSF_MCONNECT(VSF_RTC_CFG_IMP_PREFIX, _rtc_set)
57#define vsf_real_rtc_get_time VSF_MCONNECT(VSF_RTC_CFG_IMP_PREFIX, _rtc_get_time)
58#define vsf_real_rtc_set_time VSF_MCONNECT(VSF_RTC_CFG_IMP_PREFIX, _rtc_set_time)
59
60/*============================ MACROFIED FUNCTIONS ===========================*/
61/*============================ TYPES =========================================*/
62/*============================ GLOBAL VARIABLES ==============================*/
63/*============================ LOCAL VARIABLES ===============================*/
64/*============================ PROTOTYPES ====================================*/
65/*============================ IMPLEMENTATION ================================*/
66
67#if VSF_RTC_CFG_GET_API_TEMPLATE == ENABLED
68vsf_err_t vsf_real_rtc_get(vsf_real_rtc_t *rtc_ptr, vsf_rtc_tm_t *rtc_tm)
69{
70 VSF_HAL_ASSERT(rtc_ptr != NULL);
71 VSF_HAL_ASSERT(rtc_tm != NULL);
72
73 time_t second, milliseconds;
74 vsf_err_t result = vsf_real_rtc_get_time(rtc_ptr, &second, &milliseconds);
75 if (result != VSF_ERR_NONE) {
76 return result;
77 }
78
79 struct tm *t = gmtime(&second);
80 if (t == NULL) {
81 return VSF_ERR_FAIL;
82 }
83
84 rtc_tm->tm_sec = t->tm_sec;
85 rtc_tm->tm_min = t->tm_min;
86 rtc_tm->tm_hour = t->tm_hour;
87 rtc_tm->tm_mday = t->tm_mday;
88 rtc_tm->tm_wday = t->tm_wday + 1;
89 rtc_tm->tm_mon = t->tm_mon + 1;
90 rtc_tm->tm_year = t->tm_year + 1900;
91 rtc_tm->tm_ms = milliseconds;
92
93 return VSF_ERR_NONE;
94}
95#endif
96
97#if VSF_RTC_CFG_SET_API_TEMPLATE == ENABLED
98vsf_err_t vsf_real_rtc_set(vsf_real_rtc_t *rtc_ptr, const vsf_rtc_tm_t *rtc_tm)
99{
100 VSF_HAL_ASSERT(rtc_ptr != NULL);
101 VSF_HAL_ASSERT(rtc_tm != NULL);
102
103 struct tm t = {
104 .tm_sec = rtc_tm->tm_sec,
105 .tm_min = rtc_tm->tm_min,
106 .tm_hour = rtc_tm->tm_hour,
107 .tm_mday = rtc_tm->tm_mday,
108 .tm_mon = rtc_tm->tm_mon - 1,
109 .tm_year = rtc_tm->tm_year - 1900,
110 };
111
112 time_t second = mktime(&t);
113 return vsf_real_rtc_set_time(rtc_ptr, second, rtc_tm->tm_ms);
114}
115#endif
116
117#if VSF_RTC_CFG_GET_TIME_API_TEMPLATE == ENABLED
118vsf_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)
119{
120 VSF_HAL_ASSERT(rtc_ptr != NULL);
121 VSF_HAL_ASSERT((second_ptr != NULL) || (milliseconds_ptr != NULL));
122
123 vsf_rtc_tm_t rtc_tm;
124 vsf_err_t result = vsf_real_rtc_get(rtc_ptr, &rtc_tm);
125 if (result != VSF_ERR_NONE) {
126 return result;
127 }
128
129 if (second_ptr != NULL) {
130 struct tm t = {
131 .tm_sec = rtc_tm.tm_sec,
132 .tm_min = rtc_tm.tm_min,
133 .tm_hour = rtc_tm.tm_hour,
134 .tm_mday = rtc_tm.tm_mday,
135 .tm_mon = rtc_tm.tm_mon - 1,
136 .tm_year = rtc_tm.tm_year - 1900,
137 };
138 *second_ptr = mktime(&t);
139 }
140
141 if (milliseconds_ptr != NULL) {
142 *milliseconds_ptr = rtc_tm.tm_ms;
143 }
144
145 return VSF_ERR_NONE;
146}
147#endif
148
149#if VSF_RTC_CFG_SET_TIME_API_TEMPLATE == ENABLED
150vsf_err_t vsf_real_rtc_set_time(vsf_real_rtc_t *rtc_ptr, vsf_rtc_time_t second, vsf_rtc_time_t milliseconds)
151{
152 VSF_HAL_ASSERT(rtc_ptr != NULL);
153
154 struct tm *t = gmtime((const time_t *)&second);
155 if (NULL == t) {
156 return VSF_ERR_FAIL;
157 }
158
159 vsf_rtc_tm_t rtc_tm = {
160 .tm_sec = t->tm_sec,
161 .tm_min = t->tm_min,
162 .tm_hour = t->tm_hour,
163 .tm_mday = t->tm_mday,
164 .tm_mon = t->tm_mon + 1,
165 .tm_year = t->tm_year + 1900,
166 .tm_ms = milliseconds,
167 };
168
169 return vsf_real_rtc_set(rtc_ptr, &rtc_tm);
170}
171#endif
172
173#if VSF_RTC_CFG_REIMPLEMENT_API_CAPABILITY == DISABLED
174vsf_rtc_capability_t vsf_real_rtc_capability(vsf_real_rtc_t *rtc_ptr)
175{
176 vsf_rtc_capability_t rtc_capability = {
178 };
179
180 return rtc_capability;
181}
182#endif
183
184#if VSF_RTC_CFG_REIMPLEMENT_API_GET_CONFIGURATION == DISABLED
185vsf_err_t vsf_real_rtc_get_configuration(vsf_real_rtc_t *rtc_ptr, vsf_rtc_cfg_t *cfg_ptr)
186{
187 VSF_HAL_ASSERT(NULL != rtc_ptr);
188 VSF_HAL_ASSERT(NULL != cfg_ptr);
189 VSF_HAL_ASSERT(0); // Default implementation: not supported, trigger assertion
190 return VSF_ERR_NOT_SUPPORT;
191}
192#endif
193
194/*============================ MACROS ========================================*/
195
196#undef VSF_RTC_CFG_REIMPLEMENT_API_CAPABILITY
197#undef VSF_RTC_CFG_REIMPLEMENT_API_GET_CONFIGURATION
198#undef VSF_RTC_CFG_CAPABILITY_T_IRQ_MASK
199#undef vsf_real_rtc_t
200#undef vsf_real_rtc_capability
201#undef vsf_real_rtc_get_configuration
202
203/*============================ MACROS ========================================*/
204
205#ifdef VSF_RTC_CFG_IMP_REMAP_PREFIX
206
207# define vsf_imp_rtc_t VSF_MCONNECT(VSF_RTC_CFG_IMP_PREFIX, _rtc_t)
208# define vsf_imp_rtc_init VSF_MCONNECT(VSF_RTC_CFG_IMP_PREFIX, _rtc_init)
209# define vsf_imp_rtc_enable VSF_MCONNECT(VSF_RTC_CFG_IMP_PREFIX, _rtc_enable)
210# define vsf_imp_rtc_disable VSF_MCONNECT(VSF_RTC_CFG_IMP_PREFIX, _rtc_disable)
211# define vsf_imp_rtc_capability VSF_MCONNECT(VSF_RTC_CFG_IMP_PREFIX, _rtc_capability)
212# define vsf_imp_rtc_get VSF_MCONNECT(VSF_RTC_CFG_IMP_PREFIX, _rtc_get)
213# define vsf_imp_rtc_set VSF_MCONNECT(VSF_RTC_CFG_IMP_PREFIX, _rtc_set)
214# define vsf_imp_rtc_get_time VSF_MCONNECT(VSF_RTC_CFG_IMP_PREFIX, _rtc_get_time)
215# define vsf_imp_rtc_set_time VSF_MCONNECT(VSF_RTC_CFG_IMP_PREFIX, _rtc_set_time)
216# define vsf_imp_rtc_get_configuration VSF_MCONNECT(VSF_RTC_CFG_IMP_PREFIX, _rtc_get_configuration)
217
218# define vsf_remap_rtc_t VSF_MCONNECT(VSF_RTC_CFG_IMP_REMAP_PREFIX, _rtc_t)
219# define vsf_remap_rtc_init VSF_MCONNECT(VSF_RTC_CFG_IMP_REMAP_PREFIX, _rtc_init)
220# define vsf_remap_rtc_enable VSF_MCONNECT(VSF_RTC_CFG_IMP_REMAP_PREFIX, _rtc_enable)
221# define vsf_remap_rtc_disable VSF_MCONNECT(VSF_RTC_CFG_IMP_REMAP_PREFIX, _rtc_disable)
222# define vsf_remap_rtc_capability VSF_MCONNECT(VSF_RTC_CFG_IMP_REMAP_PREFIX, _rtc_capability)
223# define vsf_remap_rtc_get VSF_MCONNECT(VSF_RTC_CFG_IMP_REMAP_PREFIX, _rtc_get)
224# define vsf_remap_rtc_set VSF_MCONNECT(VSF_RTC_CFG_IMP_REMAP_PREFIX, _rtc_set)
225# define vsf_remap_rtc_get_time VSF_MCONNECT(VSF_RTC_CFG_IMP_REMAP_PREFIX, _rtc_get_time)
226# define vsf_remap_rtc_set_time VSF_MCONNECT(VSF_RTC_CFG_IMP_REMAP_PREFIX, _rtc_set_time)
227# define vsf_remap_rtc_get_configuration VSF_MCONNECT(VSF_RTC_CFG_IMP_REMAP_PREFIX, _rtc_get_configuration)
228
229# define VSF_RTC_CFG_IMP_REMAP_FUNCTIONS \
230 vsf_err_t vsf_imp_rtc_init(vsf_imp_rtc_t *rtc_ptr, vsf_rtc_cfg_t *cfg_ptr) \
231 { \
232 VSF_HAL_ASSERT(rtc_ptr != NULL); \
233 return vsf_remap_rtc_init(rtc_ptr, cfg_ptr); \
234 } \
235 void vsf_imp_rtc_fini(vsf_imp_rtc_t *rtc_ptr) \
236 { \
237 VSF_HAL_ASSERT(rtc_ptr != NULL); \
238 vsf_remap_rtc_fini(rtc_ptr); \
239 } \
240 fsm_rt_t vsf_imp_rtc_enable(vsf_imp_rtc_t *rtc_ptr) \
241 { \
242 VSF_HAL_ASSERT(rtc_ptr != NULL); \
243 return vsf_remap_rtc_enable(rtc_ptr); \
244 } \
245 fsm_rt_t vsf_imp_rtc_disable(vsf_imp_rtc_t *rtc_ptr) \
246 { \
247 VSF_HAL_ASSERT(rtc_ptr != NULL); \
248 return vsf_remap_rtc_disable(rtc_ptr); \
249 } \
250 vsf_err_t vsf_imp_rtc_get(vsf_imp_rtc_t *rtc_ptr, vsf_imp_rtc_tm_t *rtc_tm) \
251 { \
252 VSF_HAL_ASSERT(rtc_ptr != NULL); \
253 return vsf_remap_rtc_get(rtc_ptr, rtc_tm); \
254 } \
255 vsf_err_t vsf_imp_rtc_set(vsf_imp_rtc_t *rtc_ptr, const vsf_imp_rtc_tm_t *rtc_tm) \
256 { \
257 VSF_HAL_ASSERT(rtc_ptr != NULL); \
258 return vsf_remap_rtc_set(rtc_ptr, rtc_tm); \
259 } \
260 vsf_err_t vsf_imp_rtc_get_time(vsf_imp_rtc_t *rtc_ptr, vsf_imp_rtc_time_t *second_ptr,\
261 vsf_imp_rtc_time_t *millisecond_ptr) \
262 { \
263 VSF_HAL_ASSERT(rtc_ptr != NULL); \
264 return vsf_remap_rtc_get_time(rtc_ptr, second_ptr, millisecond_ptr); \
265 } \
266 vsf_err_t vsf_imp_rtc_set_time(vsf_imp_rtc_t *rtc_ptr, vsf_imp_rtc_time_t second, \
267 vsf_imp_rtc_time_t millisecond) \
268 { \
269 VSF_HAL_ASSERT(rtc_ptr != NULL); \
270 return vsf_remap_rtc_set_time(rtc_ptr, second, millisecond); \
271 } \
272 vsf_rtc_capability_t vsf_imp_rtc_capability(vsf_imp_rtc_t *rtc_ptr) \
273 { \
274 VSF_HAL_ASSERT(rtc_ptr != NULL); \
275 return vsf_remap_rtc_capability(rtc_ptr); \
276 } \
277 \
278 vsf_err_t vsf_imp_rtc_get_configuration(vsf_imp_rtc_t *rtc_ptr, vsf_rtc_cfg_t *cfg_ptr) \
279 { \
280 VSF_HAL_ASSERT(rtc_ptr != NULL); \
281 return vsf_remap_rtc_get_configuration(rtc_ptr, cfg_ptr); \
282 }
283#endif
284
285/*============================ GLOBAL VARIABLES ==============================*/
286
287#define VSF_HAL_TEMPLATE_IMP_NAME _rtc
288#define VSF_HAL_TEMPLATE_IMP_UPCASE_NAME _RTC
289
290#if !defined(VSF_RTC_CFG_IMP_PREFIX) && !defined(VSF_RTC_CFG_IMP_DEVICE_PREFIX)
291# error "Please define VSF_RTC_CFG_IMP_PREFIX in rtc driver"
292#endif
293
294#if !defined(VSF_RTC_CFG_IMP_UPCASE_PREFIX) && !defined(VSF_RTC_CFG_IMP_DEVICE_UPCASE_PREFIX)
295# error "Please define VSF_RTC_CFG_IMP_UPCASE_PREFIX in rtc driver"
296#endif
297
298#ifndef VSF_RTC_CFG_IMP_COUNT_MASK_PREFIX
299# define VSF_RTC_CFG_IMP_COUNT_MASK_PREFIX VSF_RTC_CFG_IMP_UPCASE_PREFIX
300#endif
301
302#ifdef VSF_RTC_CFG_IMP_REMAP_FUNCTIONS
303# define VSF_HAL_CFG_IMP_REMAP_FUNCTIONS VSF_RTC_CFG_IMP_REMAP_FUNCTIONS
304#endif
305
307
308#undef VSF_RTC_CFG_IMP_PREFIX
309#undef VSF_RTC_CFG_IMP_COUNT_MASK_PREFIX
310#undef VSF_RTC_CFG_IMP_UPCASE_PREFIX
311#undef VSF_RTC_CFG_IMP_DEVICE_PREFIX
312#undef VSF_RTC_CFG_IMP_DEVICE_UPCASE_PREFIX
313#undef VSF_RTC_CFG_IMP_LV0
314#undef VSF_RTC_CFG_IMP_REMAP_FUNCTIONS
315#undef VSF_RTC_CFG_IMP_HAS_OP
316#undef VSF_RTC_CFG_IMP_EXTERN_OP
317
318#undef vsf_imp_rtc_t
319#undef vsf_imp_rtc_init
320#undef vsf_imp_rtc_enable
321#undef vsf_imp_rtc_disable
322#undef vsf_imp_rtc_capability
323#undef vsf_imp_rtc_get
324#undef vsf_imp_rtc_set
325#undef vsf_imp_rtc_get_time
326#undef vsf_imp_rtc_set_time
327#undef vsf_imp_rtc_get_configuration
328
329#undef vsf_remap_rtc_t
330#undef vsf_remap_rtc_init
331#undef vsf_remap_rtc_enable
332#undef vsf_remap_rtc_disable
333#undef vsf_remap_rtc_capability
334#undef vsf_remap_rtc_get
335#undef vsf_remap_rtc_set
336#undef vsf_remap_rtc_get_time
337#undef vsf_remap_rtc_set_time
338#undef vsf_remap_rtc_get_configuration
339
340#endif /* VSF_HAL_USE_RTC */
341
vsf_err_t
Definition __type.h:42
@ VSF_ERR_NOT_SUPPORT
function not supported
Definition __type.h:46
@ 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
__TIME_T time_t
Definition types.h:130
#define NULL
Definition lvgl.h:26
struct tm * gmtime(const time_t *t)
Definition vsf_linux_glibc_time.c:347
time_t mktime(struct tm *tm)
Definition vsf_linux_glibc_time.c:74
Definition time.h:53
int tm_mon
Definition time.h:58
int tm_year
Definition time.h:59
int tm_hour
Definition time.h:56
int tm_sec
Definition time.h:54
int tm_mday
Definition time.h:57
int tm_min
Definition time.h:55
int tm_wday
Definition time.h:60
Definition rtc.c:35
RTC capability structure. Describes the features and capabilities supported by the RTC hardware.
Definition vsf_template_rtc.h:364
RTC configuration structure.
Definition vsf_template_rtc.h:350
Time structure for RTC operations. Used to represent date and time information in a standardized form...
Definition vsf_template_rtc.h:240
uint16_t tm_ms
Milliseconds component of time [0-999]. Provides sub-second precision.
Definition vsf_template_rtc.h:311
uint8_t tm_sec
Seconds component of time [0-59].
Definition vsf_template_rtc.h:247
uint16_t tm_year
Year value [0-65535].
Definition vsf_template_rtc.h:301
uint8_t tm_mon
Month [1-12]. January=1, February=2, ..., December=12.
Definition vsf_template_rtc.h:293
uint8_t tm_wday
Day of week [1-7]. Sunday=1, Monday=2, ..., Saturday=7.
Definition vsf_template_rtc.h:283
uint8_t tm_mday
Day of month [1-31].
Definition vsf_template_rtc.h:273
uint8_t tm_hour
Hours component of time [0-23]. Uses 24-hour format.
Definition vsf_template_rtc.h:265
uint8_t tm_min
Minutes component of time [0-59].
Definition vsf_template_rtc.h:255
#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
RTC time type definition.
Definition vsf_template_rtc.h:230
Generated from commit: vsfteam/vsf@74aa6ce