From 0a74774af1690461872ca4db2bfef52fe97474c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Br=C3=BCns?= Date: Tue, 14 Mar 2017 20:24:17 +0100 Subject: [PATCH] soft_pwm: avoid useless refetches of pwm_count MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The compiler is not able to reuse the value of pwm_count, but reloads it on every evaluation, if is stored in a static variable, as it cannot prove it will be unchanged. A variable with local scope may not be modified from the outside, so its value can be reused. Doing so reduces text size and instruction count. Signed-off-by: Stefan BrĂ¼ns --- Marlin/temperature.cpp | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/Marlin/temperature.cpp b/Marlin/temperature.cpp index 0cec6fc7ca..33ef2ccda5 100644 --- a/Marlin/temperature.cpp +++ b/Marlin/temperature.cpp @@ -1512,6 +1512,8 @@ void Temperature::isr() { static uint8_t temp_count = 0; static TempState temp_state = StartupDelay; static uint8_t pwm_count = _BV(SOFT_PWM_SCALE); + // avoid multiple loads of pwm_count + uint8_t pwm_count_tmp = pwm_count; // Static members for each heater #if ENABLED(SLOW_PWM_HEATERS) @@ -1555,8 +1557,8 @@ void Temperature::isr() { /** * Standard PWM modulation */ - if (pwm_count >= 127) { - pwm_count -= 127; + if (pwm_count_tmp >= 127) { + pwm_count_tmp -= 127; soft_pwm_0 = (soft_pwm_0 & pwm_mask) + soft_pwm[0]; WRITE_HEATER_0(soft_pwm_0 > pwm_mask ? HIGH : LOW); #if HOTENDS > 1 @@ -1593,30 +1595,30 @@ void Temperature::isr() { #endif } - if (soft_pwm_0 <= pwm_count) WRITE_HEATER_0(0); + if (soft_pwm_0 <= pwm_count_tmp) WRITE_HEATER_0(0); #if HOTENDS > 1 - if (soft_pwm_1 <= pwm_count) WRITE_HEATER_1(0); + if (soft_pwm_1 <= pwm_count_tmp) WRITE_HEATER_1(0); #if HOTENDS > 2 - if (soft_pwm_2 <= pwm_count) WRITE_HEATER_2(0); + if (soft_pwm_2 <= pwm_count_tmp) WRITE_HEATER_2(0); #if HOTENDS > 3 - if (soft_pwm_3 <= pwm_count) WRITE_HEATER_3(0); + if (soft_pwm_3 <= pwm_count_tmp) WRITE_HEATER_3(0); #endif #endif #endif #if HAS_HEATER_BED - if (soft_pwm_BED <= pwm_count) WRITE_HEATER_BED(0); + if (soft_pwm_BED <= pwm_count_tmp) WRITE_HEATER_BED(0); #endif #if ENABLED(FAN_SOFT_PWM) #if HAS_FAN0 - if (soft_pwm_fan[0] <= pwm_count) WRITE_FAN(0); + if (soft_pwm_fan[0] <= pwm_count_tmp) WRITE_FAN(0); #endif #if HAS_FAN1 - if (soft_pwm_fan[1] <= pwm_count) WRITE_FAN1(0); + if (soft_pwm_fan[1] <= pwm_count_tmp) WRITE_FAN1(0); #endif #if HAS_FAN2 - if (soft_pwm_fan[2] <= pwm_count) WRITE_FAN2(0); + if (soft_pwm_fan[2] <= pwm_count_tmp) WRITE_FAN2(0); #endif #endif @@ -1628,7 +1630,7 @@ void Temperature::isr() { // 3: / 16 = 61.0352 Hz // 4: / 8 = 122.0703 Hz // 5: / 4 = 244.1406 Hz - pwm_count += _BV(SOFT_PWM_SCALE); + pwm_count = pwm_count_tmp + _BV(SOFT_PWM_SCALE); #else // SLOW_PWM_HEATERS @@ -1702,8 +1704,8 @@ void Temperature::isr() { #endif #if ENABLED(FAN_SOFT_PWM) - if (pwm_count >= 127) { - pwm_count = 0; + if (pwm_count_tmp >= 127) { + pwm_count_tmp = 0; #if HAS_FAN0 soft_pwm_fan[0] = fanSpeedSoftPwm[0] >> 1; WRITE_FAN(soft_pwm_fan[0] > 0 ? HIGH : LOW); @@ -1718,13 +1720,13 @@ void Temperature::isr() { #endif } #if HAS_FAN0 - if (soft_pwm_fan[0] <= pwm_count) WRITE_FAN(0); + if (soft_pwm_fan[0] <= pwm_count_tmp) WRITE_FAN(0); #endif #if HAS_FAN1 - if (soft_pwm_fan[1] <= pwm_count) WRITE_FAN1(0); + if (soft_pwm_fan[1] <= pwm_count_tmp) WRITE_FAN1(0); #endif #if HAS_FAN2 - if (soft_pwm_fan[2] <= pwm_count) WRITE_FAN2(0); + if (soft_pwm_fan[2] <= pwm_count_tmp) WRITE_FAN2(0); #endif #endif //FAN_SOFT_PWM @@ -1736,7 +1738,7 @@ void Temperature::isr() { // 3: / 16 = 61.0352 Hz // 4: / 8 = 122.0703 Hz // 5: / 4 = 244.1406 Hz - pwm_count += _BV(SOFT_PWM_SCALE); + pwm_count = pwm_count_tmp + _BV(SOFT_PWM_SCALE); // increment slow_pwm_count only every 64th pwm_count, // i.e. yielding a PWM frequency of 16/128 Hz (8s).