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 <Ruibin.Chang@ite.com.tw>
This commit is contained in:
parent
23225c68c6
commit
f3204b7326
1 changed files with 11 additions and 4 deletions
|
@ -157,11 +157,18 @@ static int pwm_it8xxx2_set_cycles(const struct device *dev,
|
||||||
* CTRx[7:0] value FFh results in a divisor 256
|
* CTRx[7:0] value FFh results in a divisor 256
|
||||||
*/
|
*/
|
||||||
for (ctr = 0xFF; ctr >= PWM_CTRX_MIN; ctr--) {
|
for (ctr = 0xFF; ctr >= PWM_CTRX_MIN; ctr--) {
|
||||||
cxcprs = (((uint32_t) pwm_clk_src) / (ctr + 1) / target_freq) - 1;
|
cxcprs = (((uint32_t) pwm_clk_src) / (ctr + 1) / target_freq);
|
||||||
if (cxcprs >= 0) {
|
/*
|
||||||
actual_freq = ((uint32_t) pwm_clk_src) / (ctr + 1) / (cxcprs + 1);
|
* Make sure cxcprs isn't zero, or we will have
|
||||||
if (abs(actual_freq - target_freq) < deviation)
|
* divide-by-zero on calculating actual_freq.
|
||||||
|
*/
|
||||||
|
if (cxcprs != 0) {
|
||||||
|
actual_freq = ((uint32_t) pwm_clk_src) / (ctr + 1) / cxcprs;
|
||||||
|
if (abs(actual_freq - target_freq) < deviation) {
|
||||||
|
/* CxCPRS[15:0] = cxcprs - 1 */
|
||||||
|
cxcprs--;
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue