1 /*
2 * Copyright (c) 2021 Kent Hall.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #define DT_DRV_COMPAT st_stm32_counter
8
9 #include <zephyr/drivers/counter.h>
10 #include <zephyr/drivers/clock_control/stm32_clock_control.h>
11 #include <zephyr/drivers/reset.h>
12 #include <zephyr/irq.h>
13 #include <zephyr/sys/atomic.h>
14
15 #include <stm32_ll_tim.h>
16 #include <stm32_ll_rcc.h>
17
18 #include <zephyr/logging/log.h>
19 LOG_MODULE_REGISTER(counter_timer_stm32, CONFIG_COUNTER_LOG_LEVEL);
20
21 /* L0 series MCUs only have 16-bit timers and don't have below macro defined */
22 #ifndef IS_TIM_32B_COUNTER_INSTANCE
23 #define IS_TIM_32B_COUNTER_INSTANCE(INSTANCE) (0)
24 #endif
25
26 /** Maximum number of timer channels. */
27 #define TIMER_MAX_CH 4U
28
29 /** Number of channels for timer by index. */
30 #define NUM_CH(timx) \
31 (IS_TIM_CCX_INSTANCE(timx, TIM_CHANNEL_4) ? 4U : \
32 (IS_TIM_CCX_INSTANCE(timx, TIM_CHANNEL_3) ? 3U : \
33 (IS_TIM_CCX_INSTANCE(timx, TIM_CHANNEL_2) ? 2U : \
34 (IS_TIM_CCX_INSTANCE(timx, TIM_CHANNEL_1) ? 1U : \
35 0))))
36
37 /** Channel to compare set function mapping. */
38 static void(*const set_timer_compare[TIMER_MAX_CH])(TIM_TypeDef *,
39 uint32_t) = {
40 LL_TIM_OC_SetCompareCH1, LL_TIM_OC_SetCompareCH2,
41 LL_TIM_OC_SetCompareCH3, LL_TIM_OC_SetCompareCH4,
42 };
43
44 /** Channel to compare get function mapping. */
45 #if !defined(CONFIG_SOC_SERIES_STM32F4X) && \
46 !defined(CONFIG_SOC_SERIES_STM32G4X) && \
47 !defined(CONFIG_SOC_SERIES_STM32MP1X)
48 static uint32_t(*const get_timer_compare[TIMER_MAX_CH])(const TIM_TypeDef *) = {
49 LL_TIM_OC_GetCompareCH1, LL_TIM_OC_GetCompareCH2,
50 LL_TIM_OC_GetCompareCH3, LL_TIM_OC_GetCompareCH4,
51 };
52 #else
53 static uint32_t(*const get_timer_compare[TIMER_MAX_CH])(TIM_TypeDef *) = {
54 LL_TIM_OC_GetCompareCH1, LL_TIM_OC_GetCompareCH2,
55 LL_TIM_OC_GetCompareCH3, LL_TIM_OC_GetCompareCH4,
56 };
57 #endif
58 /** Channel to interrupt enable function mapping. */
59 static void(*const enable_it[TIMER_MAX_CH])(TIM_TypeDef *) = {
60 LL_TIM_EnableIT_CC1, LL_TIM_EnableIT_CC2,
61 LL_TIM_EnableIT_CC3, LL_TIM_EnableIT_CC4,
62 };
63
64 /** Channel to interrupt enable function mapping. */
65 static void(*const disable_it[TIMER_MAX_CH])(TIM_TypeDef *) = {
66 LL_TIM_DisableIT_CC1, LL_TIM_DisableIT_CC2,
67 LL_TIM_DisableIT_CC3, LL_TIM_DisableIT_CC4,
68 };
69
70 #ifdef CONFIG_ASSERT
71 /** Channel to interrupt enable check function mapping. */
72 #if !defined(CONFIG_SOC_SERIES_STM32F4X) && \
73 !defined(CONFIG_SOC_SERIES_STM32G4X) && \
74 !defined(CONFIG_SOC_SERIES_STM32MP1X)
75 static uint32_t(*const check_it_enabled[TIMER_MAX_CH])(const TIM_TypeDef *) = {
76 LL_TIM_IsEnabledIT_CC1, LL_TIM_IsEnabledIT_CC2,
77 LL_TIM_IsEnabledIT_CC3, LL_TIM_IsEnabledIT_CC4,
78 };
79 #else
80 static uint32_t(*const check_it_enabled[TIMER_MAX_CH])(TIM_TypeDef *) = {
81 LL_TIM_IsEnabledIT_CC1, LL_TIM_IsEnabledIT_CC2,
82 LL_TIM_IsEnabledIT_CC3, LL_TIM_IsEnabledIT_CC4,
83 };
84 #endif
85 #endif
86
87 /** Channel to interrupt flag clear function mapping. */
88 static void(*const clear_it_flag[TIMER_MAX_CH])(TIM_TypeDef *) = {
89 LL_TIM_ClearFlag_CC1, LL_TIM_ClearFlag_CC2,
90 LL_TIM_ClearFlag_CC3, LL_TIM_ClearFlag_CC4,
91 };
92
93 struct counter_stm32_data {
94 counter_top_callback_t top_cb;
95 void *top_user_data;
96 uint32_t guard_period;
97 atomic_t cc_int_pending;
98 uint32_t freq;
99 /* Reset controller device configuration */
100 const struct reset_dt_spec reset;
101 };
102
103 struct counter_stm32_ch_data {
104 counter_alarm_callback_t callback;
105 void *user_data;
106 };
107
108 struct counter_stm32_config {
109 struct counter_config_info info;
110 struct counter_stm32_ch_data *ch_data;
111 TIM_TypeDef *timer;
112 uint32_t prescaler;
113 struct stm32_pclken pclken;
114 void (*irq_config_func)(const struct device *dev);
115 uint32_t irqn;
116
117 LOG_INSTANCE_PTR_DECLARE(log);
118 };
119
counter_stm32_start(const struct device * dev)120 static int counter_stm32_start(const struct device *dev)
121 {
122 const struct counter_stm32_config *config = dev->config;
123 TIM_TypeDef *timer = config->timer;
124
125 /* enable counter */
126 LL_TIM_EnableCounter(timer);
127
128 return 0;
129 }
130
counter_stm32_stop(const struct device * dev)131 static int counter_stm32_stop(const struct device *dev)
132 {
133 const struct counter_stm32_config *config = dev->config;
134 TIM_TypeDef *timer = config->timer;
135
136 /* disable counter */
137 LL_TIM_DisableCounter(timer);
138
139 return 0;
140 }
141
counter_stm32_get_top_value(const struct device * dev)142 static uint32_t counter_stm32_get_top_value(const struct device *dev)
143 {
144 const struct counter_stm32_config *config = dev->config;
145
146 return LL_TIM_GetAutoReload(config->timer);
147 }
148
counter_stm32_read(const struct device * dev)149 static uint32_t counter_stm32_read(const struct device *dev)
150 {
151 const struct counter_stm32_config *config = dev->config;
152
153 return LL_TIM_GetCounter(config->timer);
154 }
155
counter_stm32_get_value(const struct device * dev,uint32_t * ticks)156 static int counter_stm32_get_value(const struct device *dev, uint32_t *ticks)
157 {
158 *ticks = counter_stm32_read(dev);
159 return 0;
160 }
161
counter_stm32_ticks_add(uint32_t val1,uint32_t val2,uint32_t top)162 static uint32_t counter_stm32_ticks_add(uint32_t val1, uint32_t val2, uint32_t top)
163 {
164 uint32_t to_top;
165
166 if (likely(IS_BIT_MASK(top))) {
167 return (val1 + val2) & top;
168 }
169
170 to_top = top - val1;
171
172 return (val2 <= to_top) ? val1 + val2 : val2 - to_top - 1U;
173 }
174
counter_stm32_ticks_sub(uint32_t val,uint32_t old,uint32_t top)175 static uint32_t counter_stm32_ticks_sub(uint32_t val, uint32_t old, uint32_t top)
176 {
177 if (likely(IS_BIT_MASK(top))) {
178 return (val - old) & top;
179 }
180
181 /* if top is not 2^n-1 */
182 return (val >= old) ? (val - old) : val + top + 1U - old;
183 }
184
counter_stm32_counter_stm32_set_cc_int_pending(const struct device * dev,uint8_t chan)185 static void counter_stm32_counter_stm32_set_cc_int_pending(const struct device *dev, uint8_t chan)
186 {
187 const struct counter_stm32_config *config = dev->config;
188 struct counter_stm32_data *data = dev->data;
189
190 atomic_or(&data->cc_int_pending, BIT(chan));
191 NVIC_SetPendingIRQ(config->irqn);
192 }
193
counter_stm32_set_cc(const struct device * dev,uint8_t id,const struct counter_alarm_cfg * alarm_cfg)194 static int counter_stm32_set_cc(const struct device *dev, uint8_t id,
195 const struct counter_alarm_cfg *alarm_cfg)
196 {
197 const struct counter_stm32_config *config = dev->config;
198 struct counter_stm32_data *data = dev->data;
199
200 __ASSERT_NO_MSG(data->guard_period < counter_stm32_get_top_value(dev));
201 uint32_t val = alarm_cfg->ticks;
202 uint32_t flags = alarm_cfg->flags;
203 bool absolute = flags & COUNTER_ALARM_CFG_ABSOLUTE;
204 bool irq_on_late;
205 TIM_TypeDef *timer = config->timer;
206 uint32_t top = counter_stm32_get_top_value(dev);
207 int err = 0;
208 uint32_t prev_val;
209 uint32_t now;
210 uint32_t diff;
211 uint32_t max_rel_val;
212
213 __ASSERT(!check_it_enabled[id](timer),
214 "Expected that CC interrupt is disabled.");
215
216 /* First take care of a risk of an event coming from CC being set to
217 * next tick. Reconfigure CC to future (now tick is the furthest
218 * future).
219 */
220 now = counter_stm32_read(dev);
221 prev_val = get_timer_compare[id](timer);
222 set_timer_compare[id](timer, now);
223 clear_it_flag[id](timer);
224
225 if (absolute) {
226 max_rel_val = top - data->guard_period;
227 irq_on_late = flags & COUNTER_ALARM_CFG_EXPIRE_WHEN_LATE;
228 } else {
229 /* If relative value is smaller than half of the counter range
230 * it is assumed that there is a risk of setting value too late
231 * and late detection algorithm must be applied. When late
232 * setting is detected, interrupt shall be triggered for
233 * immediate expiration of the timer. Detection is performed
234 * by limiting relative distance between CC and counter.
235 *
236 * Note that half of counter range is an arbitrary value.
237 */
238 irq_on_late = val < (top / 2U);
239 /* limit max to detect short relative being set too late. */
240 max_rel_val = irq_on_late ? top / 2U : top;
241 val = counter_stm32_ticks_add(now, val, top);
242 }
243
244 set_timer_compare[id](timer, val);
245
246 /* decrement value to detect also case when val == counter_stm32_read(dev). Otherwise,
247 * condition would need to include comparing diff against 0.
248 */
249 diff = counter_stm32_ticks_sub(val - 1U, counter_stm32_read(dev), top);
250 if (diff > max_rel_val) {
251 if (absolute) {
252 err = -ETIME;
253 }
254
255 /* Interrupt is triggered always for relative alarm and
256 * for absolute depending on the flag.
257 */
258 if (irq_on_late) {
259 counter_stm32_counter_stm32_set_cc_int_pending(dev, id);
260 } else {
261 config->ch_data[id].callback = NULL;
262 }
263 } else {
264 enable_it[id](timer);
265 }
266
267 return err;
268 }
269
counter_stm32_set_alarm(const struct device * dev,uint8_t chan,const struct counter_alarm_cfg * alarm_cfg)270 static int counter_stm32_set_alarm(const struct device *dev, uint8_t chan,
271 const struct counter_alarm_cfg *alarm_cfg)
272 {
273 const struct counter_stm32_config *config = dev->config;
274 struct counter_stm32_ch_data *chdata = &config->ch_data[chan];
275
276 if (alarm_cfg->ticks > counter_stm32_get_top_value(dev)) {
277 return -EINVAL;
278 }
279
280 if (chdata->callback) {
281 return -EBUSY;
282 }
283
284 chdata->callback = alarm_cfg->callback;
285 chdata->user_data = alarm_cfg->user_data;
286
287 return counter_stm32_set_cc(dev, chan, alarm_cfg);
288 }
289
counter_stm32_cancel_alarm(const struct device * dev,uint8_t chan)290 static int counter_stm32_cancel_alarm(const struct device *dev, uint8_t chan)
291 {
292 const struct counter_stm32_config *config = dev->config;
293
294 disable_it[chan](config->timer);
295 config->ch_data[chan].callback = NULL;
296
297 return 0;
298 }
299
counter_stm32_set_top_value(const struct device * dev,const struct counter_top_cfg * cfg)300 static int counter_stm32_set_top_value(const struct device *dev,
301 const struct counter_top_cfg *cfg)
302 {
303 const struct counter_stm32_config *config = dev->config;
304 TIM_TypeDef *timer = config->timer;
305 struct counter_stm32_data *data = dev->data;
306 int err = 0;
307
308 for (int i = 0; i < counter_get_num_of_channels(dev); i++) {
309 /* Overflow can be changed only when all alarms are
310 * disabled.
311 */
312 if (config->ch_data[i].callback) {
313 return -EBUSY;
314 }
315 }
316
317 LL_TIM_DisableIT_UPDATE(timer);
318 LL_TIM_SetAutoReload(timer, cfg->ticks);
319 LL_TIM_ClearFlag_UPDATE(timer);
320
321 data->top_cb = cfg->callback;
322 data->top_user_data = cfg->user_data;
323
324 if (!(cfg->flags & COUNTER_TOP_CFG_DONT_RESET)) {
325 LL_TIM_SetCounter(timer, 0);
326 } else if (counter_stm32_read(dev) >= cfg->ticks) {
327 err = -ETIME;
328 if (cfg->flags & COUNTER_TOP_CFG_RESET_WHEN_LATE) {
329 LL_TIM_SetCounter(timer, 0);
330 }
331 }
332
333 if (cfg->callback) {
334 LL_TIM_EnableIT_UPDATE(timer);
335 }
336
337 return err;
338 }
339
counter_stm32_get_pending_int(const struct device * dev)340 static uint32_t counter_stm32_get_pending_int(const struct device *dev)
341 {
342 const struct counter_stm32_config *cfg = dev->config;
343 uint32_t pending = 0;
344
345 switch (counter_get_num_of_channels(dev)) {
346 case 4U:
347 pending |= LL_TIM_IsActiveFlag_CC4(cfg->timer);
348 __fallthrough;
349 case 3U:
350 pending |= LL_TIM_IsActiveFlag_CC3(cfg->timer);
351 __fallthrough;
352 case 2U:
353 pending |= LL_TIM_IsActiveFlag_CC2(cfg->timer);
354 __fallthrough;
355 case 1U:
356 pending |= LL_TIM_IsActiveFlag_CC1(cfg->timer);
357 }
358
359 return !!pending;
360 }
361
362 /**
363 * Obtain timer clock speed.
364 *
365 * @param pclken Timer clock control subsystem.
366 * @param tim_clk Where computed timer clock will be stored.
367 *
368 * @return 0 on success, error code otherwise.
369 *
370 * This function is ripped from the PWM driver; TODO handle code duplication.
371 */
counter_stm32_get_tim_clk(const struct stm32_pclken * pclken,uint32_t * tim_clk)372 static int counter_stm32_get_tim_clk(const struct stm32_pclken *pclken, uint32_t *tim_clk)
373 {
374 int r;
375 const struct device *clk;
376 uint32_t bus_clk, apb_psc;
377
378 clk = DEVICE_DT_GET(STM32_CLOCK_CONTROL_NODE);
379
380 if (!device_is_ready(clk)) {
381 return -ENODEV;
382 }
383
384 r = clock_control_get_rate(clk, (clock_control_subsys_t)pclken,
385 &bus_clk);
386 if (r < 0) {
387 return r;
388 }
389
390 #if defined(CONFIG_SOC_SERIES_STM32H7X)
391 if (pclken->bus == STM32_CLOCK_BUS_APB1) {
392 apb_psc = STM32_D2PPRE1;
393 } else {
394 apb_psc = STM32_D2PPRE2;
395 }
396 #else
397 if (pclken->bus == STM32_CLOCK_BUS_APB1) {
398 #if defined(CONFIG_SOC_SERIES_STM32MP1X)
399 apb_psc = (uint32_t)(READ_BIT(RCC->APB1DIVR, RCC_APB1DIVR_APB1DIV));
400 #else
401 apb_psc = STM32_APB1_PRESCALER;
402 #endif
403 }
404 #if !defined(CONFIG_SOC_SERIES_STM32F0X) && !defined(CONFIG_SOC_SERIES_STM32G0X)
405 else {
406 #if defined(CONFIG_SOC_SERIES_STM32MP1X)
407 apb_psc = (uint32_t)(READ_BIT(RCC->APB2DIVR, RCC_APB2DIVR_APB2DIV));
408 #else
409 apb_psc = STM32_APB2_PRESCALER;
410 #endif
411 }
412 #endif
413 #endif
414
415 #if defined(RCC_DCKCFGR_TIMPRE) || defined(RCC_DCKCFGR1_TIMPRE) || \
416 defined(RCC_CFGR_TIMPRE)
417 /*
418 * There are certain series (some F4, F7 and H7) that have the TIMPRE
419 * bit to control the clock frequency of all the timers connected to
420 * APB1 and APB2 domains.
421 *
422 * Up to a certain threshold value of APB{1,2} prescaler, timer clock
423 * equals to HCLK. This threshold value depends on TIMPRE setting
424 * (2 if TIMPRE=0, 4 if TIMPRE=1). Above threshold, timer clock is set
425 * to a multiple of the APB domain clock PCLK{1,2} (2 if TIMPRE=0, 4 if
426 * TIMPRE=1).
427 */
428
429 if (LL_RCC_GetTIMPrescaler() == LL_RCC_TIM_PRESCALER_TWICE) {
430 /* TIMPRE = 0 */
431 if (apb_psc <= 2u) {
432 LL_RCC_ClocksTypeDef clocks;
433
434 LL_RCC_GetSystemClocksFreq(&clocks);
435 *tim_clk = clocks.HCLK_Frequency;
436 } else {
437 *tim_clk = bus_clk * 2u;
438 }
439 } else {
440 /* TIMPRE = 1 */
441 if (apb_psc <= 4u) {
442 LL_RCC_ClocksTypeDef clocks;
443
444 LL_RCC_GetSystemClocksFreq(&clocks);
445 *tim_clk = clocks.HCLK_Frequency;
446 } else {
447 *tim_clk = bus_clk * 4u;
448 }
449 }
450 #else
451 /*
452 * If the APB prescaler equals 1, the timer clock frequencies
453 * are set to the same frequency as that of the APB domain.
454 * Otherwise, they are set to twice (×2) the frequency of the
455 * APB domain.
456 */
457 if (apb_psc == 1u) {
458 *tim_clk = bus_clk;
459 } else {
460 *tim_clk = bus_clk * 2u;
461 }
462 #endif
463
464 return 0;
465 }
466
counter_stm32_init_timer(const struct device * dev)467 static int counter_stm32_init_timer(const struct device *dev)
468 {
469 const struct counter_stm32_config *cfg = dev->config;
470 struct counter_stm32_data *data = dev->data;
471 TIM_TypeDef *timer = cfg->timer;
472 LL_TIM_InitTypeDef init;
473 uint32_t tim_clk;
474 int r;
475
476 /* initialize clock and check its speed */
477 r = clock_control_on(DEVICE_DT_GET(STM32_CLOCK_CONTROL_NODE),
478 (clock_control_subsys_t)&cfg->pclken);
479 if (r < 0) {
480 LOG_ERR("Could not initialize clock (%d)", r);
481 return r;
482 }
483 r = counter_stm32_get_tim_clk(&cfg->pclken, &tim_clk);
484 if (r < 0) {
485 LOG_ERR("Could not obtain timer clock (%d)", r);
486 return r;
487 }
488 data->freq = tim_clk / (cfg->prescaler + 1U);
489
490 if (!device_is_ready(data->reset.dev)) {
491 LOG_ERR("reset controller not ready");
492 return -ENODEV;
493 }
494
495 /* Reset timer to default state using RCC */
496 (void)reset_line_toggle_dt(&data->reset);
497
498 /* config/enable IRQ */
499 cfg->irq_config_func(dev);
500
501 /* initialize timer */
502 LL_TIM_StructInit(&init);
503
504 init.Prescaler = cfg->prescaler;
505 init.CounterMode = LL_TIM_COUNTERMODE_UP;
506 init.Autoreload = counter_get_max_top_value(dev);
507 init.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
508
509 if (LL_TIM_Init(timer, &init) != SUCCESS) {
510 LOG_ERR("Could not initialize timer");
511 return -EIO;
512 }
513
514 return 0;
515 }
516
counter_stm32_get_guard_period(const struct device * dev,uint32_t flags)517 static uint32_t counter_stm32_get_guard_period(const struct device *dev, uint32_t flags)
518 {
519 struct counter_stm32_data *data = dev->data;
520
521 ARG_UNUSED(flags);
522 return data->guard_period;
523 }
524
counter_stm32_set_guard_period(const struct device * dev,uint32_t guard,uint32_t flags)525 static int counter_stm32_set_guard_period(const struct device *dev, uint32_t guard,
526 uint32_t flags)
527 {
528 struct counter_stm32_data *data = dev->data;
529
530 ARG_UNUSED(flags);
531 __ASSERT_NO_MSG(guard < counter_stm32_get_top_value(dev));
532
533 data->guard_period = guard;
534 return 0;
535 }
536
counter_stm32_get_freq(const struct device * dev)537 static uint32_t counter_stm32_get_freq(const struct device *dev)
538 {
539 struct counter_stm32_data *data = dev->data;
540
541 return data->freq;
542 }
543
counter_stm32_top_irq_handle(const struct device * dev)544 static void counter_stm32_top_irq_handle(const struct device *dev)
545 {
546 struct counter_stm32_data *data = dev->data;
547
548 counter_top_callback_t cb = data->top_cb;
549
550 __ASSERT(cb != NULL, "top event enabled - expecting callback");
551 cb(dev, data->top_user_data);
552 }
553
counter_stm32_alarm_irq_handle(const struct device * dev,uint32_t id)554 static void counter_stm32_alarm_irq_handle(const struct device *dev, uint32_t id)
555 {
556 const struct counter_stm32_config *config = dev->config;
557 struct counter_stm32_data *data = dev->data;
558 TIM_TypeDef *timer = config->timer;
559
560 struct counter_stm32_ch_data *chdata;
561 counter_alarm_callback_t cb;
562
563 atomic_and(&data->cc_int_pending, ~BIT(id));
564 disable_it[id](timer);
565
566 chdata = &config->ch_data[id];
567 cb = chdata->callback;
568 chdata->callback = NULL;
569
570 if (cb) {
571 uint32_t cc_val = get_timer_compare[id](timer);
572
573 cb(dev, id, cc_val, chdata->user_data);
574 }
575 }
576
577 static const struct counter_driver_api counter_stm32_driver_api = {
578 .start = counter_stm32_start,
579 .stop = counter_stm32_stop,
580 .get_value = counter_stm32_get_value,
581 .set_alarm = counter_stm32_set_alarm,
582 .cancel_alarm = counter_stm32_cancel_alarm,
583 .set_top_value = counter_stm32_set_top_value,
584 .get_pending_int = counter_stm32_get_pending_int,
585 .get_top_value = counter_stm32_get_top_value,
586 .get_guard_period = counter_stm32_get_guard_period,
587 .set_guard_period = counter_stm32_set_guard_period,
588 .get_freq = counter_stm32_get_freq,
589 };
590
591 #define TIM_IRQ_HANDLE_CC(timx, cc) \
592 do { \
593 bool hw_irq = LL_TIM_IsActiveFlag_CC##cc(timer) && \
594 LL_TIM_IsEnabledIT_CC##cc(timer); \
595 if (hw_irq || (data->cc_int_pending & BIT(cc - 1U))) { \
596 if (hw_irq) { \
597 LL_TIM_ClearFlag_CC##cc(timer); \
598 } \
599 counter_stm32_alarm_irq_handle(dev, cc - 1U); \
600 } \
601 } while (0)
602
counter_stm32_irq_handler(const struct device * dev)603 void counter_stm32_irq_handler(const struct device *dev)
604 {
605 const struct counter_stm32_config *config = dev->config;
606 struct counter_stm32_data *data = dev->data;
607 TIM_TypeDef *timer = config->timer;
608
609 /* Capture compare events */
610 switch (counter_get_num_of_channels(dev)) {
611 case 4U:
612 TIM_IRQ_HANDLE_CC(timer, 4);
613 __fallthrough;
614 case 3U:
615 TIM_IRQ_HANDLE_CC(timer, 3);
616 __fallthrough;
617 case 2U:
618 TIM_IRQ_HANDLE_CC(timer, 2);
619 __fallthrough;
620 case 1U:
621 TIM_IRQ_HANDLE_CC(timer, 1);
622 }
623
624 /* TIM Update event */
625 if (LL_TIM_IsActiveFlag_UPDATE(timer) && LL_TIM_IsEnabledIT_UPDATE(timer)) {
626 LL_TIM_ClearFlag_UPDATE(timer);
627 counter_stm32_top_irq_handle(dev);
628 }
629 }
630
631 #define TIMER(idx) DT_INST_PARENT(idx)
632
633 /** TIMx instance from DT */
634 #define TIM(idx) ((TIM_TypeDef *)DT_REG_ADDR(TIMER(idx)))
635
636 #define COUNTER_DEVICE_INIT(idx) \
637 BUILD_ASSERT(DT_PROP(TIMER(idx), st_prescaler) <= 0xFFFF, \
638 "TIMER prescaler out of range"); \
639 BUILD_ASSERT(NUM_CH(TIM(idx)) <= TIMER_MAX_CH, \
640 "TIMER too many channels"); \
641 \
642 static struct counter_stm32_data counter##idx##_data = { \
643 .reset = RESET_DT_SPEC_GET(TIMER(idx)), \
644 }; \
645 static struct counter_stm32_ch_data counter##idx##_ch_data[TIMER_MAX_CH]; \
646 \
647 static void counter_##idx##_stm32_irq_config(const struct device *dev) \
648 { \
649 IRQ_CONNECT(DT_IRQN(TIMER(idx)), \
650 DT_IRQ(TIMER(idx), priority), \
651 counter_stm32_irq_handler, \
652 DEVICE_DT_INST_GET(idx), \
653 0); \
654 irq_enable(DT_IRQN(TIMER(idx))); \
655 } \
656 \
657 static const struct counter_stm32_config counter##idx##_config = { \
658 .info = { \
659 .max_top_value = \
660 IS_TIM_32B_COUNTER_INSTANCE(TIM(idx)) ? \
661 0xFFFFFFFF : 0x0000FFFF, \
662 .flags = COUNTER_CONFIG_INFO_COUNT_UP, \
663 .channels = NUM_CH(TIM(idx)), \
664 }, \
665 .ch_data = counter##idx##_ch_data, \
666 .timer = TIM(idx), \
667 .prescaler = DT_PROP(TIMER(idx), st_prescaler), \
668 .pclken = { \
669 .bus = DT_CLOCKS_CELL(TIMER(idx), bus), \
670 .enr = DT_CLOCKS_CELL(TIMER(idx), bits) \
671 }, \
672 .irq_config_func = counter_##idx##_stm32_irq_config, \
673 .irqn = DT_IRQN(TIMER(idx)), \
674 }; \
675 \
676 DEVICE_DT_INST_DEFINE(idx, \
677 counter_stm32_init_timer, \
678 NULL, \
679 &counter##idx##_data, \
680 &counter##idx##_config, \
681 PRE_KERNEL_1, CONFIG_COUNTER_INIT_PRIORITY, \
682 &counter_stm32_driver_api);
683
684 DT_INST_FOREACH_STATUS_OKAY(COUNTER_DEVICE_INIT)
685