/
counter_mcux_rtc.c
288 lines (230 loc) · 7.31 KB
/
counter_mcux_rtc.c
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
* Copyright (c) 2018 blik GmbH
* Copyright (c) 2018,2024 NXP
*
* SPDX-License-Identifier: Apache-2.0
*/
#define DT_DRV_COMPAT nxp_kinetis_rtc
#include <zephyr/drivers/counter.h>
#include <zephyr/irq.h>
#include <zephyr/kernel.h>
#include <zephyr/sys_clock.h>
#include <fsl_rtc.h>
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(mcux_rtc, CONFIG_COUNTER_LOG_LEVEL);
struct mcux_rtc_data {
counter_alarm_callback_t alarm_callback;
counter_top_callback_t top_callback;
void *alarm_user_data;
void *top_user_data;
};
struct mcux_rtc_config {
struct counter_config_info info;
RTC_Type *base;
void (*irq_config_func)(const struct device *dev);
};
static int mcux_rtc_start(const struct device *dev)
{
const struct counter_config_info *info = dev->config;
const struct mcux_rtc_config *config =
CONTAINER_OF(info, struct mcux_rtc_config, info);
RTC_StartTimer(config->base);
RTC_EnableInterrupts(config->base,
kRTC_AlarmInterruptEnable |
kRTC_TimeOverflowInterruptEnable |
kRTC_TimeInvalidInterruptEnable);
return 0;
}
static int mcux_rtc_stop(const struct device *dev)
{
const struct counter_config_info *info = dev->config;
const struct mcux_rtc_config *config =
CONTAINER_OF(info, struct mcux_rtc_config, info);
RTC_DisableInterrupts(config->base,
kRTC_AlarmInterruptEnable |
kRTC_TimeOverflowInterruptEnable |
kRTC_TimeInvalidInterruptEnable);
RTC_StopTimer(config->base);
/* clear out any set alarms */
config->base->TAR = 0;
return 0;
}
static uint32_t mcux_rtc_read(const struct device *dev)
{
const struct counter_config_info *info = dev->config;
const struct mcux_rtc_config *config =
CONTAINER_OF(info, struct mcux_rtc_config, info);
uint32_t ticks = config->base->TSR;
/*
* Read TSR seconds twice in case it glitches during an update.
* This can happen when a read occurs at the time the register is
* incrementing.
*/
if (config->base->TSR == ticks) {
return ticks;
}
ticks = config->base->TSR;
return ticks;
}
static int mcux_rtc_get_value(const struct device *dev, uint32_t *ticks)
{
*ticks = mcux_rtc_read(dev);
return 0;
}
static int mcux_rtc_set_alarm(const struct device *dev, uint8_t chan_id,
const struct counter_alarm_cfg *alarm_cfg)
{
const struct counter_config_info *info = dev->config;
const struct mcux_rtc_config *config =
CONTAINER_OF(info, struct mcux_rtc_config, info);
struct mcux_rtc_data *data = dev->data;
uint32_t ticks = alarm_cfg->ticks;
uint32_t current = mcux_rtc_read(dev);
LOG_DBG("Current time is %d ticks", current);
if (chan_id != 0U) {
LOG_ERR("Invalid channel id");
return -EINVAL;
}
if (data->alarm_callback != NULL) {
return -EBUSY;
}
if ((alarm_cfg->flags & COUNTER_ALARM_CFG_ABSOLUTE) == 0) {
ticks += current;
}
if (ticks < current) {
LOG_ERR("Alarm cannot be earlier than current time");
return -EINVAL;
}
data->alarm_callback = alarm_cfg->callback;
data->alarm_user_data = alarm_cfg->user_data;
config->base->TAR = ticks;
LOG_DBG("Alarm set to %d ticks", ticks);
return 0;
}
static int mcux_rtc_cancel_alarm(const struct device *dev, uint8_t chan_id)
{
struct mcux_rtc_data *data = dev->data;
if (chan_id != 0U) {
LOG_ERR("Invalid channel id");
return -EINVAL;
}
data->alarm_callback = NULL;
return 0;
}
static int mcux_rtc_set_top_value(const struct device *dev,
const struct counter_top_cfg *cfg)
{
const struct counter_config_info *info = dev->config;
const struct mcux_rtc_config *config =
CONTAINER_OF(info, struct mcux_rtc_config, info);
struct mcux_rtc_data *data = dev->data;
if (cfg->ticks != info->max_top_value) {
LOG_ERR("Wrap can only be set to 0x%x.", info->max_top_value);
return -ENOTSUP;
}
if (!(cfg->flags & COUNTER_TOP_CFG_DONT_RESET)) {
RTC_StopTimer(config->base);
config->base->TSR = 0;
RTC_StartTimer(config->base);
}
data->top_callback = cfg->callback;
data->top_user_data = cfg->user_data;
return 0;
}
static uint32_t mcux_rtc_get_pending_int(const struct device *dev)
{
const struct counter_config_info *info = dev->config;
const struct mcux_rtc_config *config =
CONTAINER_OF(info, struct mcux_rtc_config, info);
return RTC_GetStatusFlags(config->base) & RTC_SR_TAF_MASK;
}
static uint32_t mcux_rtc_get_top_value(const struct device *dev)
{
const struct counter_config_info *info = dev->config;
return info->max_top_value;
}
static void mcux_rtc_isr(const struct device *dev)
{
const struct counter_config_info *info = dev->config;
const struct mcux_rtc_config *config =
CONTAINER_OF(info, struct mcux_rtc_config, info);
struct mcux_rtc_data *data = dev->data;
counter_alarm_callback_t cb;
uint32_t current = mcux_rtc_read(dev);
LOG_DBG("Current time is %d ticks", current);
if ((RTC_GetStatusFlags(config->base) & RTC_SR_TAF_MASK) &&
(data->alarm_callback)) {
cb = data->alarm_callback;
data->alarm_callback = NULL;
cb(dev, 0, current, data->alarm_user_data);
}
if ((RTC_GetStatusFlags(config->base) & RTC_SR_TOF_MASK) &&
(data->top_callback)) {
data->top_callback(dev, data->top_user_data);
}
/*
* Clear any conditions to ack the IRQ
*
* callback may have already reset the alarm flag if a new
* alarm value was programmed to the TAR
*/
RTC_StopTimer(config->base);
if (RTC_GetStatusFlags(config->base) & RTC_SR_TAF_MASK) {
RTC_ClearStatusFlags(config->base, kRTC_AlarmFlag);
} else if (RTC_GetStatusFlags(config->base) & RTC_SR_TIF_MASK) {
RTC_ClearStatusFlags(config->base, kRTC_TimeInvalidFlag);
} else if (RTC_GetStatusFlags(config->base) & RTC_SR_TOF_MASK) {
RTC_ClearStatusFlags(config->base, kRTC_TimeOverflowFlag);
}
RTC_StartTimer(config->base);
}
static int mcux_rtc_init(const struct device *dev)
{
const struct counter_config_info *info = dev->config;
const struct mcux_rtc_config *config =
CONTAINER_OF(info, struct mcux_rtc_config, info);
rtc_config_t rtc_config;
RTC_GetDefaultConfig(&rtc_config);
RTC_Init(config->base, &rtc_config);
#if !(defined(FSL_FEATURE_RTC_HAS_NO_CR_OSCE) && FSL_FEATURE_RTC_HAS_NO_CR_OSCE)
/* Enable 32kHz oscillator and wait for 1ms to settle */
RTC_SetClockSource(config->base);
k_busy_wait(USEC_PER_MSEC);
#endif /* !FSL_FEATURE_RTC_HAS_NO_CR_OSCE */
config->irq_config_func(dev);
return 0;
}
static const struct counter_driver_api mcux_rtc_driver_api = {
.start = mcux_rtc_start,
.stop = mcux_rtc_stop,
.get_value = mcux_rtc_get_value,
.set_alarm = mcux_rtc_set_alarm,
.cancel_alarm = mcux_rtc_cancel_alarm,
.set_top_value = mcux_rtc_set_top_value,
.get_pending_int = mcux_rtc_get_pending_int,
.get_top_value = mcux_rtc_get_top_value,
};
static struct mcux_rtc_data mcux_rtc_data_0;
static void mcux_rtc_irq_config_0(const struct device *dev);
static struct mcux_rtc_config mcux_rtc_config_0 = {
.base = (RTC_Type *)DT_INST_REG_ADDR(0),
.irq_config_func = mcux_rtc_irq_config_0,
.info = {
.max_top_value = UINT32_MAX,
.freq = DT_INST_PROP(0, clock_frequency) /
DT_INST_PROP(0, prescaler),
.flags = COUNTER_CONFIG_INFO_COUNT_UP,
.channels = 1,
},
};
DEVICE_DT_INST_DEFINE(0, &mcux_rtc_init, NULL,
&mcux_rtc_data_0, &mcux_rtc_config_0.info,
POST_KERNEL, CONFIG_COUNTER_INIT_PRIORITY,
&mcux_rtc_driver_api);
static void mcux_rtc_irq_config_0(const struct device *dev)
{
IRQ_CONNECT(DT_INST_IRQN(0),
DT_INST_IRQ(0, priority),
mcux_rtc_isr, DEVICE_DT_INST_GET(0), 0);
irq_enable(DT_INST_IRQN(0));
}