1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
|
/*
* Copyright (c) 2006-2024 RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2022-07-25 Rbb666 first version
* 2024-11-06 kurisaw add alarm function
*/
#include <rtthread.h>
#include <rtdevice.h>
#include <sys/time.h>
#include "drv_common.h"
#ifdef BSP_USING_RTC
/*#define DRV_DEBUG*/
#define LOG_TAG "drv.rtc"
#include <drv_log.h>
cyhal_rtc_t rtc_obj;
struct rtc_device_object
{
rt_rtc_dev_t rtc_dev;
#ifdef RT_USING_ALARM
struct rt_rtc_wkalarm wkalarm;
#endif
};
static struct rtc_device_object ifx32_rtc_dev;
static int get_day_of_week(int day, int month, int year)
{
int ret;
int k = 0;
int j = 0;
if (month < CY_RTC_MARCH)
{
month += CY_RTC_MONTHS_PER_YEAR;
year--;
}
k = (year % 100);
j = (year / 100);
ret = (day + (13 * (month + 1) / 5) + k + (k / 4) + (j / 4) + (5 * j)) % 7;
ret = ((ret + 6) % 7);
return ret;
}
static rt_err_t set_rtc_time_stamp(time_t time_stamp)
{
struct tm tm = {0};
struct tm new_time = {0};
gmtime_r(&time_stamp, &tm);
if (tm.tm_year < 100)
{
return -RT_ERROR;
}
new_time.tm_sec = tm.tm_sec ;
new_time.tm_min = tm.tm_min ;
new_time.tm_hour = tm.tm_hour;
new_time.tm_mday = tm.tm_mday;
new_time.tm_mon = tm.tm_mon;
new_time.tm_year = tm.tm_year;
new_time.tm_wday = get_day_of_week(tm.tm_mday, tm.tm_mon, tm.tm_year);
if (cyhal_rtc_write(&rtc_obj, &new_time) != RT_EOK)
{
return -RT_ERROR;
}
LOG_D("set rtc time.");
return RT_EOK;
}
static rt_err_t ifx_rtc_get_timeval(struct timeval *tv)
{
struct tm tm_new = {0};
struct tm date_time = {0};
cyhal_rtc_read(&rtc_obj, &date_time);
tm_new.tm_sec = date_time.tm_sec;
tm_new.tm_min = date_time.tm_min;
tm_new.tm_hour = date_time.tm_hour;
tm_new.tm_mday = date_time.tm_mday;
tm_new.tm_mon = date_time.tm_mon;
tm_new.tm_year = date_time.tm_year;
tv->tv_sec = timegm(&tm_new);
return RT_EOK;
}
#ifdef RT_USING_ALARM
void rtc_alarm_callback(void *callback_arg, cyhal_rtc_event_t event);
#endif
static rt_err_t _rtc_init(void)
{
#ifdef BSP_RTC_USING_LSE
Cy_RTC_SelectClockSource(CY_RTC_CLK_SELECT_WCO);
#else
Cy_RTC_SelectClockSource(CY_RTC_CLK_SELECT_ILO);
#endif /* BSP_RTC_USING_LSE */
if (cyhal_rtc_init(&rtc_obj) != RT_EOK)
{
LOG_E("rtc init failed.");
return -RT_ERROR;
}
#ifdef RT_USING_ALARM
cyhal_rtc_register_callback(&rtc_obj, rtc_alarm_callback, NULL);
cyhal_rtc_enable_event(&rtc_obj, CYHAL_RTC_ALARM, 3u, true);
#endif
return RT_EOK;
}
static rt_err_t _rtc_get_secs(time_t *sec)
{
struct timeval tv;
ifx_rtc_get_timeval(&tv);
*(time_t *) sec = tv.tv_sec;
LOG_D("RTC: get rtc_time %d", *sec);
return RT_EOK;
}
static rt_err_t _rtc_set_secs(time_t *sec)
{
rt_err_t result = RT_EOK;
if (set_rtc_time_stamp(*sec))
{
result = -RT_ERROR;
}
LOG_D("RTC: set rtc_time %d", *sec);
return result;
}
#if defined(RT_USING_ALARM)
static rt_err_t _rtc_get_alarm(struct rt_rtc_wkalarm *alarm)
{
#ifdef RT_USING_ALARM
*alarm = ifx32_rtc_dev.wkalarm;
LOG_D("GET_ALARM %d:%d:%d",ifx32_rtc_dev.wkalarm.tm_hour,
ifx32_rtc_dev.wkalarm.tm_min,ifx32_rtc_dev.wkalarm.tm_sec);
return RT_EOK;
#else
return -RT_ERROR;
#endif
}
static rt_err_t _rtc_set_alarm(struct rt_rtc_wkalarm *alarm)
{
#ifdef RT_USING_ALARM
LOG_D("RT_DEVICE_CTRL_RTC_SET_ALARM");
if (alarm != RT_NULL)
{
ifx32_rtc_dev.wkalarm.enable = alarm->enable;
ifx32_rtc_dev.wkalarm.tm_hour = alarm->tm_hour;
ifx32_rtc_dev.wkalarm.tm_min = alarm->tm_min;
ifx32_rtc_dev.wkalarm.tm_sec = alarm->tm_sec;
cyhal_rtc_set_alarm_by_seconds(&rtc_obj, 1);
}
else
{
LOG_E("RT_DEVICE_CTRL_RTC_SET_ALARM error!!");
return -RT_ERROR;
}
LOG_D("SET_ALARM %d:%d:%d",alarm->tm_hour,
alarm->tm_min, alarm->tm_sec);
return RT_EOK;
#else
return -RT_ERROR;
#endif
}
#ifdef RT_USING_ALARM
void rtc_alarm_callback(void *callback_arg, cyhal_rtc_event_t event)
{
RT_UNUSED(callback_arg);
RT_UNUSED(event);
rt_interrupt_enter();
rt_alarm_update(0, 0);
rt_interrupt_leave();
}
#endif
static const struct rt_rtc_ops _rtc_ops =
{
_rtc_init,
_rtc_get_secs,
_rtc_set_secs,
_rtc_get_alarm,
_rtc_set_alarm,
ifx_rtc_get_timeval,
RT_NULL,
};
/**
* @brief RTC initialization function.
*
* @return RT_EOK indicates successful initialization, other value indicates failed;
*/
static int rt_hw_rtc_init(void)
{
rt_err_t result = RT_EOK;
ifx32_rtc_dev.rtc_dev.ops = &_rtc_ops;
if (rt_hw_rtc_register(&(ifx32_rtc_dev.rtc_dev), "rtc", RT_DEVICE_FLAG_RDWR, RT_NULL) != RT_EOK)
{
LOG_E("rtc init failed");
result = -RT_ERROR;
}
else
{
LOG_D("rtc init success");
}
return result;
}
INIT_DEVICE_EXPORT(rt_hw_rtc_init);
#endif
#endif
|