forked from zephyrproject-rtos/zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpwm_mcux_pwt.c
340 lines (274 loc) · 7.93 KB
/
pwm_mcux_pwt.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) 2021 Vestas Wind Systems A/S
*
* SPDX-License-Identifier: Apache-2.0
*/
#define DT_DRV_COMPAT nxp_kinetis_pwt
#include <drivers/clock_control.h>
#include <errno.h>
#include <drivers/pwm.h>
#include <soc.h>
#include <fsl_pwt.h>
#include <fsl_clock.h>
#include <logging/log.h>
LOG_MODULE_REGISTER(pwm_mcux_pwt, CONFIG_PWM_LOG_LEVEL);
/* Number of PWT input ports */
#define PWT_INPUTS 4U
struct mcux_pwt_config {
PWT_Type *base;
const struct device *clock_dev;
clock_control_subsys_t clock_subsys;
pwt_clock_source_t pwt_clock_source;
pwt_clock_prescale_t prescale;
void (*irq_config_func)(const struct device *dev);
};
struct mcux_pwt_data {
uint32_t clock_freq;
uint32_t period_cycles;
uint32_t high_overflows;
uint32_t low_overflows;
pwm_capture_callback_handler_t callback;
void *user_data;
pwt_config_t pwt_config;
bool continuous : 1;
bool inverted : 1;
bool overflowed : 1;
};
static inline bool mcux_pwt_is_active(const struct device *dev)
{
const struct mcux_pwt_config *config = dev->config;
return !!(config->base->CS & PWT_CS_PWTEN_MASK);
}
static int mcux_pwt_pin_set(const struct device *dev, uint32_t pwm,
uint32_t period_cycles, uint32_t pulse_cycles,
pwm_flags_t flags)
{
ARG_UNUSED(dev);
ARG_UNUSED(pwm);
ARG_UNUSED(period_cycles);
ARG_UNUSED(pulse_cycles);
ARG_UNUSED(flags);
LOG_ERR("pwt only supports pwm capture");
return -ENOTSUP;
}
static int mcux_pwt_pin_configure_capture(const struct device *dev,
uint32_t pwm,
pwm_flags_t flags,
pwm_capture_callback_handler_t cb,
void *user_data)
{
const struct mcux_pwt_config *config = dev->config;
struct mcux_pwt_data *data = dev->data;
if (pwm >= PWT_INPUTS) {
LOG_ERR("invalid channel %d", pwm);
return -EINVAL;
}
if (mcux_pwt_is_active(dev)) {
LOG_ERR("pwm capture in progress");
return -EBUSY;
}
data->callback = cb;
data->user_data = user_data;
data->pwt_config.inputSelect = pwm;
data->continuous =
(flags & PWM_CAPTURE_MODE_MASK) == PWM_CAPTURE_MODE_CONTINUOUS;
data->inverted =
(flags & PWM_POLARITY_MASK) == PWM_POLARITY_INVERTED;
PWT_Init(config->base, &data->pwt_config);
PWT_EnableInterrupts(config->base,
kPWT_PulseWidthReadyInterruptEnable |
kPWT_CounterOverflowInterruptEnable);
return 0;
}
static int mcux_pwt_pin_enable_capture(const struct device *dev, uint32_t pwm)
{
const struct mcux_pwt_config *config = dev->config;
struct mcux_pwt_data *data = dev->data;
if (pwm >= PWT_INPUTS) {
LOG_ERR("invalid channel %d", pwm);
return -EINVAL;
}
if (!data->callback) {
LOG_ERR("PWM capture not configured");
return -EINVAL;
}
if (mcux_pwt_is_active(dev)) {
LOG_ERR("PWM capture already enabled");
return -EBUSY;
}
data->overflowed = false;
data->high_overflows = 0;
data->low_overflows = 0;
PWT_StartTimer(config->base);
return 0;
}
static int mcux_pwt_pin_disable_capture(const struct device *dev, uint32_t pwm)
{
const struct mcux_pwt_config *config = dev->config;
if (pwm >= PWT_INPUTS) {
LOG_ERR("invalid channel %d", pwm);
return -EINVAL;
}
PWT_StopTimer(config->base);
return 0;
}
static int mcux_pwt_calc_period(uint16_t ppw, uint16_t npw,
uint32_t high_overflows,
uint32_t low_overflows,
uint32_t *result)
{
uint32_t period;
/* Calculate sum of overflow counters */
if (u32_add_overflow(high_overflows, low_overflows, &period)) {
return -ERANGE;
}
/* Calculate cycles from sum of overflow counters */
if (u32_mul_overflow(period, 0xFFFFU, &period)) {
return -ERANGE;
}
/* Add positive pulse width */
if (u32_add_overflow(period, ppw, &period)) {
return -ERANGE;
}
/* Add negative pulse width */
if (u32_add_overflow(period, npw, &period)) {
return -ERANGE;
}
*result = period;
return 0;
}
static int mcux_pwt_calc_pulse(uint16_t pw, uint32_t overflows,
uint32_t *result)
{
uint32_t pulse;
/* Calculate cycles from overflow counter */
if (u32_mul_overflow(overflows, 0xFFFFU, &pulse)) {
return -ERANGE;
}
/* Add pulse width */
if (u32_add_overflow(pulse, pw, &pulse)) {
return -ERANGE;
}
*result = pulse;
return 0;
}
static void mcux_pwt_isr(const struct device *dev)
{
const struct mcux_pwt_config *config = dev->config;
struct mcux_pwt_data *data = dev->data;
uint32_t period = 0;
uint32_t pulse = 0;
uint32_t flags;
uint16_t ppw;
uint16_t npw;
int err;
flags = PWT_GetStatusFlags(config->base);
if (flags & kPWT_CounterOverflowFlag) {
if (config->base->CR & PWT_CR_LVL_MASK) {
data->overflowed |= u32_add_overflow(1,
data->high_overflows, &data->high_overflows);
} else {
data->overflowed |= u32_add_overflow(1,
data->low_overflows, &data->low_overflows);
}
PWT_ClearStatusFlags(config->base, kPWT_CounterOverflowFlag);
}
if (flags & kPWT_PulseWidthValidFlag) {
ppw = PWT_ReadPositivePulseWidth(config->base);
npw = PWT_ReadNegativePulseWidth(config->base);
if (!data->continuous) {
PWT_StopTimer(config->base);
}
if (data->inverted) {
err = mcux_pwt_calc_pulse(npw, data->low_overflows,
&pulse);
} else {
err = mcux_pwt_calc_pulse(ppw, data->high_overflows,
&pulse);
}
if (err == 0) {
err = mcux_pwt_calc_period(ppw, npw,
data->high_overflows,
data->low_overflows,
&period);
}
if (data->overflowed) {
err = -ERANGE;
}
LOG_DBG("period = %d, pulse = %d, err = %d", period, pulse,
err);
if (data->callback) {
data->callback(dev, data->pwt_config.inputSelect,
period, pulse, err, data->user_data);
}
data->overflowed = false;
data->high_overflows = 0;
data->low_overflows = 0;
PWT_ClearStatusFlags(config->base, kPWT_PulseWidthValidFlag);
}
}
static int mcux_pwt_get_cycles_per_sec(const struct device *dev, uint32_t pwm,
uint64_t *cycles)
{
const struct mcux_pwt_config *config = dev->config;
struct mcux_pwt_data *data = dev->data;
ARG_UNUSED(pwm);
*cycles = data->clock_freq >> config->prescale;
return 0;
}
static int mcux_pwt_init(const struct device *dev)
{
const struct mcux_pwt_config *config = dev->config;
struct mcux_pwt_data *data = dev->data;
pwt_config_t *pwt_config = &data->pwt_config;
if (clock_control_get_rate(config->clock_dev, config->clock_subsys,
&data->clock_freq)) {
LOG_ERR("could not get clock frequency");
return -EINVAL;
}
PWT_GetDefaultConfig(pwt_config);
pwt_config->clockSource = config->pwt_clock_source;
pwt_config->prescale = config->prescale;
pwt_config->enableFirstCounterLoad = true;
PWT_Init(config->base, pwt_config);
config->irq_config_func(dev);
return 0;
}
static const struct pwm_driver_api mcux_pwt_driver_api = {
.pin_set = mcux_pwt_pin_set,
.get_cycles_per_sec = mcux_pwt_get_cycles_per_sec,
.pin_configure_capture = mcux_pwt_pin_configure_capture,
.pin_enable_capture = mcux_pwt_pin_enable_capture,
.pin_disable_capture = mcux_pwt_pin_disable_capture,
};
#define TO_PWT_PRESCALE_DIVIDE(val) _DO_CONCAT(kPWT_Prescale_Divide_, val)
#define PWT_DEVICE(n) \
static void mcux_pwt_config_func_##n(const struct device *dev); \
\
static const struct mcux_pwt_config mcux_pwt_config_##n = { \
.base = (PWT_Type *)DT_INST_REG_ADDR(n), \
.clock_dev = DEVICE_DT_GET(DT_INST_CLOCKS_CTLR(n)), \
.clock_subsys = \
(clock_control_subsys_t)DT_INST_CLOCKS_CELL(n, name), \
.pwt_clock_source = kPWT_BusClock, \
.prescale = \
TO_PWT_PRESCALE_DIVIDE(DT_INST_PROP(n, prescaler)), \
.irq_config_func = mcux_pwt_config_func_##n, \
}; \
\
static struct mcux_pwt_data mcux_pwt_data_##n; \
\
DEVICE_DT_INST_DEFINE(n, &mcux_pwt_init, \
NULL, &mcux_pwt_data_##n, \
&mcux_pwt_config_##n, \
POST_KERNEL, \
CONFIG_KERNEL_INIT_PRIORITY_DEVICE, \
&mcux_pwt_driver_api); \
\
static void mcux_pwt_config_func_##n(const struct device *dev) \
{ \
IRQ_CONNECT(DT_INST_IRQN(n), DT_INST_IRQ(n, priority), \
mcux_pwt_isr, DEVICE_DT_INST_GET(n), 0); \
irq_enable(DT_INST_IRQN(n)); \
}
DT_INST_FOREACH_STATUS_OKAY(PWT_DEVICE)