Move validation code to the right spot.

As MicroDev1 pointed out the problem is a divide by zero when calculating the duty cycle.
Maybe need to check again in `common_hal_pwmio_pwmout_set_frequency()`.
This commit is contained in:
Randall Bohn 2021-05-03 09:16:46 -06:00 committed by GitHub
parent 8613b9a9fe
commit d1db782760
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -63,6 +63,12 @@ pwmout_result_t common_hal_pwmio_pwmout_construct(pwmio_pwmout_obj_t *self,
uint16_t duty,
uint32_t frequency,
bool variable_frequency) {
// check the frequency (avoid divide by zero below)
if (frequency == 0) {
return PWMOUT_INVALID_FREQUENCY;
}
// Calculate duty cycle
uint32_t duty_bits = 0;
uint32_t interval = LEDC_APB_CLK_HZ / frequency;
@ -129,12 +135,6 @@ pwmout_result_t common_hal_pwmio_pwmout_construct(pwmio_pwmout_obj_t *self,
return PWMOUT_INITIALIZATION_ERROR;
}
// check the frequency -- 0 is not valid
// maybe also want to reject frequency > system_clock / 2 ??
if (frequency == 0) {
return PWMOUT_INVALID_FREQUENCY;
}
// Make reservations
reserved_timer_freq[timer_index] = frequency;
reserved_channels[channel_index] = timer_index;