Skip to content

Commit 24dcfb2

Browse files
gautierg-stnashif
authored andcommitted
drivers: pwm: stm32: remork complementary channel logic
Align the definition of the complementary channels to the normal channels. That way, it is consistent for all arrays, we use channel-1 as index, and no other operation is necessary. Signed-off-by: Guillaume Gautier <[email protected]>
1 parent c82a2b2 commit 24dcfb2

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

drivers/pwm/pwm_stm32.c

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ enum capture_state {
4848
/** Return the complementary channel number
4949
* that is used to capture the end of the pulse.
5050
*/
51-
static const uint32_t complementary_channel[] = {0, 2, 1, 4, 3};
51+
static const uint32_t complementary_channel[] = {2, 1, 4, 3};
5252

5353
struct pwm_stm32_capture_data {
5454
pwm_capture_callback_handler_t callback;
@@ -325,7 +325,7 @@ static void init_capture_channels(const struct device *dev, uint32_t channel,
325325
TIM_TypeDef *timer = cfg->timer;
326326
bool is_inverted = (flags & PWM_POLARITY_MASK) == PWM_POLARITY_INVERTED;
327327
uint32_t ll_channel = ch2ll[channel - 1];
328-
uint32_t ll_complementary_channel = ch2ll[complementary_channel[channel] - 1];
328+
uint32_t ll_complementary_channel = ch2ll[complementary_channel[channel - 1] - 1];
329329

330330

331331
/* Setup main channel */
@@ -469,7 +469,7 @@ static int pwm_stm32_enable_capture(const struct device *dev, uint32_t channel)
469469
enable_capture_interrupt[channel - 1](timer);
470470

471471
LL_TIM_CC_EnableChannel(timer, ch2ll[channel - 1]);
472-
LL_TIM_CC_EnableChannel(timer, ch2ll[complementary_channel[channel] - 1]);
472+
LL_TIM_CC_EnableChannel(timer, ch2ll[complementary_channel[channel - 1] - 1]);
473473
LL_TIM_GenerateEvent_UPDATE(timer);
474474

475475
return 0;
@@ -498,7 +498,7 @@ static int pwm_stm32_disable_capture(const struct device *dev, uint32_t channel)
498498

499499
LL_TIM_DisableIT_UPDATE(timer);
500500
LL_TIM_CC_DisableChannel(timer, ch2ll[channel - 1]);
501-
LL_TIM_CC_DisableChannel(timer, ch2ll[complementary_channel[channel] - 1]);
501+
LL_TIM_CC_DisableChannel(timer, ch2ll[complementary_channel[channel - 1] - 1]);
502502

503503
return 0;
504504
}
@@ -509,6 +509,7 @@ static void pwm_stm32_isr(const struct device *dev)
509509
TIM_TypeDef *timer = cfg->timer;
510510
struct pwm_stm32_data *data = dev->data;
511511
struct pwm_stm32_capture_data *cpt = &data->capture;
512+
uint8_t channel = cpt->channel;
512513
int status = 0;
513514

514515
if (cpt->skip_irq != 0u) {
@@ -542,19 +543,19 @@ static void pwm_stm32_isr(const struct device *dev)
542543
}
543544

544545
if (!cfg->four_channel_capture_support) {
545-
if (is_capture_active[cpt->channel - 1](timer) ||
546-
is_capture_active[complementary_channel[cpt->channel] - 1](timer)) {
547-
clear_capture_interrupt[cpt->channel - 1](timer);
546+
if (is_capture_active[channel - 1](timer) ||
547+
is_capture_active[complementary_channel[channel - 1] - 1](timer)) {
548+
clear_capture_interrupt[channel - 1](timer);
548549
clear_capture_interrupt
549-
[complementary_channel[cpt->channel] - 1](timer);
550+
[complementary_channel[channel - 1] - 1](timer);
550551

551-
cpt->period = get_channel_capture[cpt->channel - 1](timer);
552+
cpt->period = get_channel_capture[channel - 1](timer);
552553
cpt->pulse = get_channel_capture
553-
[complementary_channel[cpt->channel] - 1](timer);
554+
[complementary_channel[channel - 1] - 1](timer);
554555
}
555556
} else {
556557
if (cpt->state == CAPTURE_STATE_WAIT_FOR_PULSE_START &&
557-
is_capture_active[cpt->channel - 1](timer)) {
558+
is_capture_active[channel - 1](timer)) {
558559
/* Reset the counter manually instead of automatically by HW
559560
* This sets the pulse-start at 0 and makes the pulse-end
560561
* and period related to that number. Sure we loose some
@@ -569,19 +570,19 @@ static void pwm_stm32_isr(const struct device *dev)
569570

570571
} else if ((cpt->state == CAPTURE_STATE_WAIT_FOR_UPDATE_EVENT ||
571572
cpt->state == CAPTURE_STATE_WAIT_FOR_PERIOD_END) &&
572-
is_capture_active[cpt->channel - 1](timer)) {
573+
is_capture_active[channel - 1](timer)) {
573574
cpt->state = CAPTURE_STATE_IDLE;
574575
/* The end of the period. Both capture channels should now contain
575576
* the timer value when the pulse and period ended respectively.
576577
*/
577-
cpt->pulse = get_channel_capture[complementary_channel[cpt->channel] - 1]
578+
cpt->pulse = get_channel_capture[complementary_channel[channel - 1] - 1]
578579
(timer);
579-
cpt->period = get_channel_capture[cpt->channel - 1](timer);
580+
cpt->period = get_channel_capture[channel - 1](timer);
580581
/* Reset the counter manually for next cycle */
581582
LL_TIM_GenerateEvent_UPDATE(timer);
582583
}
583584

584-
clear_capture_interrupt[cpt->channel - 1](timer);
585+
clear_capture_interrupt[channel - 1](timer);
585586

586587
if (cpt->state != CAPTURE_STATE_IDLE) {
587588
/* Still waiting for a complete capture */
@@ -594,14 +595,14 @@ static void pwm_stm32_isr(const struct device *dev)
594595
}
595596

596597
if (!cpt->continuous) {
597-
pwm_stm32_disable_capture(dev, cpt->channel);
598+
pwm_stm32_disable_capture(dev, channel);
598599
} else {
599600
cpt->overflows = 0u;
600601
cpt->state = CAPTURE_STATE_WAIT_FOR_PERIOD_END;
601602
}
602603

603604
if (cpt->callback != NULL) {
604-
cpt->callback(dev, cpt->channel, cpt->capture_period ? cpt->period : 0u,
605+
cpt->callback(dev, channel, cpt->capture_period ? cpt->period : 0u,
605606
cpt->capture_pulse ? cpt->pulse : 0u, status, cpt->user_data);
606607
}
607608
}

0 commit comments

Comments
 (0)