Skip to content

Commit d1db782

Browse files
authored
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()`.
1 parent 8613b9a commit d1db782

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

ports/esp32s2/common-hal/pwmio/PWMOut.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ pwmout_result_t common_hal_pwmio_pwmout_construct(pwmio_pwmout_obj_t *self,
6363
uint16_t duty,
6464
uint32_t frequency,
6565
bool variable_frequency) {
66+
67+
// check the frequency (avoid divide by zero below)
68+
if (frequency == 0) {
69+
return PWMOUT_INVALID_FREQUENCY;
70+
}
71+
6672
// Calculate duty cycle
6773
uint32_t duty_bits = 0;
6874
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,
129135
return PWMOUT_INITIALIZATION_ERROR;
130136
}
131137

132-
// check the frequency -- 0 is not valid
133-
// maybe also want to reject frequency > system_clock / 2 ??
134-
if (frequency == 0) {
135-
return PWMOUT_INVALID_FREQUENCY;
136-
}
137-
138138
// Make reservations
139139
reserved_timer_freq[timer_index] = frequency;
140140
reserved_channels[channel_index] = timer_index;

0 commit comments

Comments
 (0)