summaryrefslogtreecommitdiffstatshomepage
path: root/stmhal/timer.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2014-09-29 14:15:01 +0100
committerDamien George <damien.p.george@gmail.com>2014-09-29 14:15:01 +0100
commitf042d7a4d7197d87798bc9727e9df094a3edd7c1 (patch)
tree1bd316e2f4911a4c7ae49df2906c08a5e74dbadc /stmhal/timer.c
parent853708738eee3437c3cb10e4bedf2760c541766b (diff)
downloadmicropython-f042d7a4d7197d87798bc9727e9df094a3edd7c1.tar.gz
micropython-f042d7a4d7197d87798bc9727e9df094a3edd7c1.zip
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.
Diffstat (limited to 'stmhal/timer.c')
-rw-r--r--stmhal/timer.c9
1 files changed, 4 insertions, 5 deletions
diff --git a/stmhal/timer.c b/stmhal/timer.c
index 026d11c815..14ef3cc4a9 100644
--- a/stmhal/timer.c
+++ b/stmhal/timer.c
@@ -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) {
#if MICROPY_PY_BUILTINS_FLOAT
float percent;
- if (cmp > period) {
+ if (cmp >= period) {
percent = 100.0;
} else {
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);
#else
mp_int_t percent;
- if (cmp > period) {
+ if (cmp >= period) {
percent = 100;
- } else if (period > MAX_PERIOD_DIV_100) {
- // We divide the top and bottom by 128, and then do the math.
- percent = (cmp / 128) * 100 / (period / 128);
+ } else if (cmp > MAX_PERIOD_DIV_100) {
+ percent = cmp / (period / 100);
} else {
percent = cmp * 100 / period;
}