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.
This commit is contained in:
Damien George 2014-09-29 14:15:01 +01:00
parent 853708738e
commit f042d7a4d7
2 changed files with 6 additions and 7 deletions

View File

@ -326,7 +326,7 @@ STATIC uint32_t compute_pwm_value_from_percent(uint32_t period, mp_obj_t percent
STATIC mp_obj_t compute_percent_from_pwm_value(uint32_t period, uint32_t cmp) { STATIC mp_obj_t compute_percent_from_pwm_value(uint32_t period, uint32_t cmp) {
#if MICROPY_PY_BUILTINS_FLOAT #if MICROPY_PY_BUILTINS_FLOAT
float percent; float percent;
if (cmp > period) { if (cmp >= period) {
percent = 100.0; percent = 100.0;
} else { } else {
percent = (float)cmp * 100.0 / ((float)period); percent = (float)cmp * 100.0 / ((float)period);
@ -334,11 +334,10 @@ STATIC mp_obj_t compute_percent_from_pwm_value(uint32_t period, uint32_t cmp) {
return mp_obj_new_float(percent); return mp_obj_new_float(percent);
#else #else
mp_int_t percent; mp_int_t percent;
if (cmp > period) { if (cmp >= period) {
percent = 100; percent = 100;
} else if (period > MAX_PERIOD_DIV_100) { } else if (cmp > MAX_PERIOD_DIV_100) {
// We divide the top and bottom by 128, and then do the math. percent = cmp / (period / 100);
percent = (cmp / 128) * 100 / (period / 128);
} else { } else {
percent = cmp * 100 / period; percent = cmp * 100 / period;
} }

View File

@ -175,7 +175,7 @@ STATIC uint32_t compute_pwm_value_from_percent(uint32_t period, mp_obj_t percent
STATIC mp_obj_t compute_percent_from_pwm_value(uint32_t period, uint32_t cmp) { STATIC mp_obj_t compute_percent_from_pwm_value(uint32_t period, uint32_t cmp) {
#if MICROPY_PY_BUILTINS_FLOAT #if MICROPY_PY_BUILTINS_FLOAT
float percent = (float)cmp * 100.0 / (float)period; float percent = (float)cmp * 100.0 / (float)period;
if (cmp > period) { if (cmp >= period) {
percent = 100.0; percent = 100.0;
} else { } else {
percent = (float)cmp * 100.0 / (float)period; percent = (float)cmp * 100.0 / (float)period;
@ -183,7 +183,7 @@ STATIC mp_obj_t compute_percent_from_pwm_value(uint32_t period, uint32_t cmp) {
return mp_obj_new_float(percent); return mp_obj_new_float(percent);
#else #else
mp_int_t percent; mp_int_t percent;
if (cmp > period) { if (cmp >= period) {
percent = 100; percent = 100;
} else { } else {
percent = cmp * 100 / period; percent = cmp * 100 / period;