/
counter_mcux_qtmr.c
340 lines (279 loc) · 9.73 KB
/
counter_mcux_qtmr.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*
* Copyright (c) 2022 KT-Elektronik, Klaucke und Partner GmbH
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @file
*
* Counter driver for the Quad Timer through the MCUxpresso SDK. Based mainly on counter_mcux_gpt.c
*
* Each quad timer module has four channels (0-3) that can operate independently, but the Zephyr
* counter-API does not support starting or stopping different channels independently. Hence, each
* channel is represented as an independent counter device.
*/
#include <zephyr/drivers/counter.h>
#include <zephyr/drivers/clock_control.h>
#include <zephyr/irq.h>
#include <fsl_qtmr.h>
#include <zephyr/logging/log.h>
#include <zephyr/sys/barrier.h>
LOG_MODULE_REGISTER(mcux_qtmr, CONFIG_COUNTER_LOG_LEVEL);
struct mcux_qtmr_config {
/* info must be first element */
struct counter_config_info info;
const struct device *clock_dev;
clock_control_subsys_t clock_subsys;
TMR_Type *base;
clock_name_t clock_source;
qtmr_channel_selection_t channel;
qtmr_config_t qtmr_config;
qtmr_counting_mode_t mode;
};
struct mcux_qtmr_data {
counter_alarm_callback_t alarm_callback;
counter_top_callback_t top_callback;
void *alarm_user_data;
void *top_user_data;
qtmr_status_flags_t interrupt_mask;
uint32_t freq;
};
/* Only one interrupt per QTMR module. Each of which has four timers. */
#define DT_DRV_COMPAT nxp_imx_qtmr
/**
* @brief ISR for a specific timer channel
*
* @param dev timer channel device
*/
void mcux_qtmr_timer_handler(const struct device *dev, uint32_t status)
{
const struct mcux_qtmr_config *config = dev->config;
struct mcux_qtmr_data *data = dev->data;
uint32_t current = QTMR_GetCurrentTimerCount(config->base, config->channel);
QTMR_ClearStatusFlags(config->base, config->channel, status);
barrier_dsync_fence_full();
if ((status & kQTMR_Compare1Flag) && data->alarm_callback) {
QTMR_DisableInterrupts(config->base, config->channel,
kQTMR_Compare1InterruptEnable);
data->interrupt_mask &= ~kQTMR_Compare1InterruptEnable;
counter_alarm_callback_t alarm_cb = data->alarm_callback;
data->alarm_callback = NULL;
alarm_cb(dev, config->channel, current, data->alarm_user_data);
}
if ((status & kQTMR_OverflowFlag) && data->top_callback) {
data->top_callback(dev, data->top_user_data);
}
}
/**
* @brief ISR for the QTMR
*
* @param timers array containing the counter devices for each channel of the timer module
*/
static void mcux_qtmr_isr(const struct device *timers[])
{
/* the interrupt can be triggered by any of the four channels of the QTMR. Check status
* of all channels and trigger the ISR for the channel(s) that has/have triggered the
* interrupt.
*/
for (qtmr_channel_selection_t ch = kQTMR_Channel_0; ch <= kQTMR_Channel_3 ; ch++) {
if (timers[ch] != NULL) {
const struct mcux_qtmr_config *config = timers[ch]->config;
struct mcux_qtmr_data *data = timers[ch]->data;
uint32_t channel_status = QTMR_GetStatus(config->base, ch);
if ((channel_status & data->interrupt_mask) != 0) {
mcux_qtmr_timer_handler(timers[ch], channel_status);
}
}
}
}
#define INIT_TIMER(node_id) [DT_PROP(node_id, channel)] = DEVICE_DT_GET(node_id),
#define QTMR_DEVICE_INIT_MCUX(n) \
static const struct device *const timers_##n[4] = { \
DT_FOREACH_CHILD_STATUS_OKAY(DT_DRV_INST(n), INIT_TIMER) \
}; \
static int init_irq_##n(void) \
{ \
IRQ_CONNECT(DT_INST_IRQN(n), DT_INST_IRQ(n, priority), mcux_qtmr_isr, \
timers_##n, 0); \
irq_enable(DT_INST_IRQN(n)); \
return 0; \
} \
\
SYS_INIT(init_irq_##n, POST_KERNEL, CONFIG_COUNTER_INIT_PRIORITY);
DT_INST_FOREACH_STATUS_OKAY(QTMR_DEVICE_INIT_MCUX)
#undef DT_DRV_COMPAT
#define DT_DRV_COMPAT nxp_imx_tmr
static int mcux_qtmr_start(const struct device *dev)
{
const struct mcux_qtmr_config *config = dev->config;
QTMR_StartTimer(config->base, config->channel, config->mode);
return 0;
}
static int mcux_qtmr_stop(const struct device *dev)
{
const struct mcux_qtmr_config *config = dev->config;
QTMR_StopTimer(config->base, config->channel);
return 0;
}
static int mcux_qtmr_get_value(const struct device *dev, uint32_t *ticks)
{
const struct mcux_qtmr_config *config = dev->config;
*ticks = QTMR_GetCurrentTimerCount(config->base, config->channel);
return 0;
}
static int mcux_qtmr_set_alarm(const struct device *dev, uint8_t chan_id,
const struct counter_alarm_cfg *alarm_cfg)
{
const struct mcux_qtmr_config *config = dev->config;
struct mcux_qtmr_data *data = dev->data;
uint32_t current;
uint32_t ticks;
if (chan_id != 0) {
LOG_ERR("Invalid channel id");
return -EINVAL;
}
if (data->alarm_callback) {
return -EBUSY;
}
data->alarm_callback = alarm_cfg->callback;
data->alarm_user_data = alarm_cfg->user_data;
current = QTMR_GetCurrentTimerCount(config->base, config->channel);
ticks = alarm_cfg->ticks;
if ((alarm_cfg->flags & COUNTER_ALARM_CFG_ABSOLUTE) == 0) {
ticks += current;
}
/* this timer always counts up. */
config->base->CHANNEL[config->channel].COMP1 = ticks;
data->interrupt_mask |= kQTMR_Compare1InterruptEnable;
QTMR_EnableInterrupts(config->base, config->channel, data->interrupt_mask);
return 0;
}
static int mcux_qtmr_cancel_alarm(const struct device *dev, uint8_t chan_id)
{
const struct mcux_qtmr_config *config = dev->config;
struct mcux_qtmr_data *data = dev->data;
if (chan_id != 0) {
LOG_ERR("Invalid channel id");
return -EINVAL;
}
QTMR_DisableInterrupts(config->base, config->channel, data->interrupt_mask);
data->interrupt_mask &= ~kQTMR_Compare1InterruptEnable;
data->alarm_callback = NULL;
return 0;
}
static uint32_t mcux_qtmr_get_pending_int(const struct device *dev)
{
const struct mcux_qtmr_config *config = dev->config;
return QTMR_GetStatus(config->base, config->channel);
}
static int mcux_qtmr_set_top_value(const struct device *dev,
const struct counter_top_cfg *cfg)
{
const struct mcux_qtmr_config *config = dev->config;
struct mcux_qtmr_data *data = dev->data;
if (cfg->ticks != config->info.max_top_value) {
LOG_ERR("Wrap can only be set to 0x%x",
config->info.max_top_value);
return -ENOTSUP;
}
if ((cfg->flags & COUNTER_TOP_CFG_DONT_RESET) == 0) {
if ((config->base->CHANNEL[config->channel].CTRL & TMR_CTRL_DIR_MASK) != 0U) {
/* counting down, reset to UINT16MAX */
config->base->CHANNEL[config->channel].CNTR = UINT16_MAX;
} else {
/* counting up, reset to 0 */
config->base->CHANNEL[config->channel].CNTR = 0;
}
}
if (cfg->callback != NULL) {
data->top_callback = cfg->callback;
data->top_user_data = cfg->user_data;
data->interrupt_mask |= kQTMR_OverflowInterruptEnable;
QTMR_EnableInterrupts(config->base, config->channel, kQTMR_OverflowInterruptEnable);
}
return 0;
}
static uint32_t mcux_qtmr_get_top_value(const struct device *dev)
{
const struct mcux_qtmr_config *config = dev->config;
return config->info.max_top_value;
}
static uint32_t mcux_qtmr_get_freq(const struct device *dev)
{
struct mcux_qtmr_data *data = dev->data;
return data->freq;
}
/**
* @brief look up table for dividers when using internal clock sources kQTMR_ClockDivide_1 to
* kQTMR_ClockDivide_128
*/
static const uint8_t qtmr_primary_source_divider[] = {1, 2, 4, 8, 16, 32, 64, 128};
static int mcux_qtmr_init(const struct device *dev)
{
const struct mcux_qtmr_config *config = dev->config;
struct mcux_qtmr_data *data = dev->data;
if (config->qtmr_config.primarySource < kQTMR_ClockDivide_1) {
/* for external sources, use the value from the dts (if given) */
data->freq = config->info.freq;
} else {
/* bus clock with divider */
if (!device_is_ready(config->clock_dev)) {
LOG_ERR("clock control device not ready");
return -ENODEV;
}
if (clock_control_get_rate(config->clock_dev, config->clock_subsys,
&data->freq)) {
return -EINVAL;
}
data->freq /= qtmr_primary_source_divider[config->qtmr_config.primarySource -
kQTMR_ClockDivide_1];
}
QTMR_Init(config->base, config->channel, &config->qtmr_config);
return 0;
}
static const struct counter_driver_api mcux_qtmr_driver_api = {
.start = mcux_qtmr_start,
.stop = mcux_qtmr_stop,
.get_value = mcux_qtmr_get_value,
.set_alarm = mcux_qtmr_set_alarm,
.cancel_alarm = mcux_qtmr_cancel_alarm,
.set_top_value = mcux_qtmr_set_top_value,
.get_pending_int = mcux_qtmr_get_pending_int,
.get_top_value = mcux_qtmr_get_top_value,
.get_freq = mcux_qtmr_get_freq,
};
#define TMR_DEVICE_INIT_MCUX(n) \
static struct mcux_qtmr_data mcux_qtmr_data_ ## n; \
\
static const struct mcux_qtmr_config mcux_qtmr_config_ ## n = { \
.base = (void *)DT_REG_ADDR(DT_INST_PARENT(n)), \
.clock_dev = DEVICE_DT_GET(DT_CLOCKS_CTLR(DT_INST_PARENT(n))), \
.clock_subsys = \
(clock_control_subsys_t)DT_CLOCKS_CELL(DT_INST_PARENT(n), name), \
.info = { \
.max_top_value = UINT16_MAX, \
.freq = DT_INST_PROP_OR(n, freq, 0), \
.channels = 1, \
.flags = COUNTER_CONFIG_INFO_COUNT_UP, \
}, \
.channel = DT_INST_PROP(n, channel), \
.qtmr_config = { \
.debugMode = kQTMR_RunNormalInDebug, \
.enableExternalForce = false, \
.enableMasterMode = false, \
.faultFilterCount = DT_INST_PROP_OR(n, filter_count, 0), \
.faultFilterPeriod = DT_INST_PROP_OR(n, filter_count, 0), \
.primarySource = DT_INST_ENUM_IDX(n, primary_source), \
.secondarySource = DT_INST_ENUM_IDX_OR(n, secondary_source, 0), \
}, \
.mode = DT_INST_ENUM_IDX(n, mode), \
}; \
\
DEVICE_DT_INST_DEFINE(n, \
mcux_qtmr_init, \
NULL, \
&mcux_qtmr_data_ ## n, \
&mcux_qtmr_config_ ## n, \
POST_KERNEL, \
CONFIG_COUNTER_INIT_PRIORITY, \
&mcux_qtmr_driver_api); \
DT_INST_FOREACH_STATUS_OKAY(TMR_DEVICE_INIT_MCUX)