drivers: pwm: pwm_mcux: Round up PWM frequency to prevent overflow

The HAL function PWM_SetupPwm does the inverse division to get the
number of pulses from the frequency.
In the case of a prescaler of 1 and a period of UINT16_MAX cycles
this would result in an uint16_t overflow.

Signed-off-by: Pieter De Gendt <pieter.degendt@basalte.be>
This commit is contained in:
Pieter De Gendt 2023-04-26 11:12:55 +02:00 committed by Fabio Baltieri
parent 7ea422af84
commit bab23bbe6c

View File

@ -88,7 +88,7 @@ static int mcux_pwm_set_cycles(const struct device *dev, uint32_t channel,
return -EINVAL;
}
pwm_freq = (clock_freq >> config->prescale) / period_cycles;
pwm_freq = DIV_ROUND_UP(clock_freq >> config->prescale, period_cycles);
if (pwm_freq == 0) {
LOG_ERR("Could not set up pwm_freq=%d", pwm_freq);