Skip to content

Commit f042d7a

Browse files
committed
stmhal: Fix edge case for timer PWM of 100%.
Also improve precision of calculating PWM percent in integer mode. Also update teensy with edge case fix.
1 parent 8537087 commit f042d7a

File tree

2 files changed

+6
-7
lines changed

2 files changed

+6
-7
lines changed

stmhal/timer.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -326,19 +326,18 @@ STATIC uint32_t compute_pwm_value_from_percent(uint32_t period, mp_obj_t percent
326326
STATIC mp_obj_t compute_percent_from_pwm_value(uint32_t period, uint32_t cmp) {
327327
#if MICROPY_PY_BUILTINS_FLOAT
328328
float percent;
329-
if (cmp > period) {
329+
if (cmp >= period) {
330330
percent = 100.0;
331331
} else {
332332
percent = (float)cmp * 100.0 / ((float)period);
333333
}
334334
return mp_obj_new_float(percent);
335335
#else
336336
mp_int_t percent;
337-
if (cmp > period) {
337+
if (cmp >= period) {
338338
percent = 100;
339-
} else if (period > MAX_PERIOD_DIV_100) {
340-
// We divide the top and bottom by 128, and then do the math.
341-
percent = (cmp / 128) * 100 / (period / 128);
339+
} else if (cmp > MAX_PERIOD_DIV_100) {
340+
percent = cmp / (period / 100);
342341
} else {
343342
percent = cmp * 100 / period;
344343
}

teensy/timer.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,15 +175,15 @@ STATIC uint32_t compute_pwm_value_from_percent(uint32_t period, mp_obj_t percent
175175
STATIC mp_obj_t compute_percent_from_pwm_value(uint32_t period, uint32_t cmp) {
176176
#if MICROPY_PY_BUILTINS_FLOAT
177177
float percent = (float)cmp * 100.0 / (float)period;
178-
if (cmp > period) {
178+
if (cmp >= period) {
179179
percent = 100.0;
180180
} else {
181181
percent = (float)cmp * 100.0 / (float)period;
182182
}
183183
return mp_obj_new_float(percent);
184184
#else
185185
mp_int_t percent;
186-
if (cmp > period) {
186+
if (cmp >= period) {
187187
percent = 100;
188188
} else {
189189
percent = cmp * 100 / period;

0 commit comments

Comments
 (0)