Skip to content

Commit f3204b7

Browse files
RuibinChangcarlescufi
authored andcommitted
ITE drivers/pwm: don't divide-by-zero
Make sure cxcprs isn't zero, or we will have divide-by-zero on calculating actual_freq. Test: 1.tests/drivers/pwm/pwm_api pattern 2.GPA0(pwm0) output 79201Hz, 324Hz, 100Hz, 1Hz waveform Signed-off-by: Ruibin Chang <[email protected]>
1 parent 23225c6 commit f3204b7

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

drivers/pwm/pwm_ite_it8xxx2.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,18 @@ static int pwm_it8xxx2_set_cycles(const struct device *dev,
157157
* CTRx[7:0] value FFh results in a divisor 256
158158
*/
159159
for (ctr = 0xFF; ctr >= PWM_CTRX_MIN; ctr--) {
160-
cxcprs = (((uint32_t) pwm_clk_src) / (ctr + 1) / target_freq) - 1;
161-
if (cxcprs >= 0) {
162-
actual_freq = ((uint32_t) pwm_clk_src) / (ctr + 1) / (cxcprs + 1);
163-
if (abs(actual_freq - target_freq) < deviation)
160+
cxcprs = (((uint32_t) pwm_clk_src) / (ctr + 1) / target_freq);
161+
/*
162+
* Make sure cxcprs isn't zero, or we will have
163+
* divide-by-zero on calculating actual_freq.
164+
*/
165+
if (cxcprs != 0) {
166+
actual_freq = ((uint32_t) pwm_clk_src) / (ctr + 1) / cxcprs;
167+
if (abs(actual_freq - target_freq) < deviation) {
168+
/* CxCPRS[15:0] = cxcprs - 1 */
169+
cxcprs--;
164170
break;
171+
}
165172
}
166173
}
167174

0 commit comments

Comments
 (0)