From 2aed66a955f023d36c3230aa7477e9d384a82f90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Br=C3=BCns?= Date: Sun, 12 Mar 2017 22:10:21 +0100 Subject: [PATCH 1/5] temperature: Fix SOFT_PWM off by one MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A 128 step PWM has 127 intervals (0/127 ... 127/127 duty). Currently, a PWM setting of 1/127 is active for 2/128, i.e. double the expected time, or, in general n+1/128 instead of n/127. Fixes issue#6003. Signed-off-by: Stefan Brüns --- Marlin/temperature.cpp | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/Marlin/temperature.cpp b/Marlin/temperature.cpp index 883133806..66390d8c7 100644 --- a/Marlin/temperature.cpp +++ b/Marlin/temperature.cpp @@ -1547,7 +1547,8 @@ void Temperature::isr() { /** * Standard PWM modulation */ - if (pwm_count == 0) { + if (pwm_count >= 127) { + pwm_count = 0; soft_pwm_0 = soft_pwm[0]; WRITE_HEATER_0(soft_pwm_0 > 0 ? HIGH : LOW); #if HOTENDS > 1 @@ -1584,30 +1585,30 @@ void Temperature::isr() { #endif } - if (soft_pwm_0 < pwm_count) WRITE_HEATER_0(0); + if (soft_pwm_0 <= pwm_count) WRITE_HEATER_0(0); #if HOTENDS > 1 - if (soft_pwm_1 < pwm_count) WRITE_HEATER_1(0); + if (soft_pwm_1 <= pwm_count) WRITE_HEATER_1(0); #if HOTENDS > 2 - if (soft_pwm_2 < pwm_count) WRITE_HEATER_2(0); + if (soft_pwm_2 <= pwm_count) WRITE_HEATER_2(0); #if HOTENDS > 3 - if (soft_pwm_3 < pwm_count) WRITE_HEATER_3(0); + if (soft_pwm_3 <= pwm_count) 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) 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) WRITE_FAN(0); #endif #if HAS_FAN1 - if (soft_pwm_fan[1] < pwm_count) WRITE_FAN1(0); + if (soft_pwm_fan[1] <= pwm_count) WRITE_FAN1(0); #endif #if HAS_FAN2 - if (soft_pwm_fan[2] < pwm_count) WRITE_FAN2(0); + if (soft_pwm_fan[2] <= pwm_count) WRITE_FAN2(0); #endif #endif @@ -1620,7 +1621,6 @@ void Temperature::isr() { // 4: / 8 = 122.0703 Hz // 5: / 4 = 244.1406 Hz pwm_count += _BV(SOFT_PWM_SCALE); - pwm_count &= 0x7F; #else // SLOW_PWM_HEATERS @@ -1694,7 +1694,8 @@ void Temperature::isr() { #endif #if ENABLED(FAN_SOFT_PWM) - if (pwm_count == 0) { + if (pwm_count >= 127) { + pwm_count = 0; #if HAS_FAN0 soft_pwm_fan[0] = fanSpeedSoftPwm[0] >> 1; WRITE_FAN(soft_pwm_fan[0] > 0 ? HIGH : LOW); @@ -1709,13 +1710,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) WRITE_FAN(0); #endif #if HAS_FAN1 - if (soft_pwm_fan[1] < pwm_count) WRITE_FAN1(0); + if (soft_pwm_fan[1] <= pwm_count) WRITE_FAN1(0); #endif #if HAS_FAN2 - if (soft_pwm_fan[2] < pwm_count) WRITE_FAN2(0); + if (soft_pwm_fan[2] <= pwm_count) WRITE_FAN2(0); #endif #endif //FAN_SOFT_PWM @@ -1728,10 +1729,10 @@ void Temperature::isr() { // 4: / 8 = 122.0703 Hz // 5: / 4 = 244.1406 Hz pwm_count += _BV(SOFT_PWM_SCALE); - pwm_count &= 0x7F; - // increment slow_pwm_count only every 64 pwm_count (e.g., every 8s) - if ((pwm_count % 64) == 0) { + // increment slow_pwm_count only every 64th pwm_count, + // i.e. yielding a PWM frequency of 16/128 Hz (8s). + if (((pwm_count >> SOFT_PWM_SCALE) % 64) == 0) { slow_pwm_count++; slow_pwm_count &= 0x7f; From 35a55d5757c1ed415cd5c9b4225f5c2011f0fbf1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Br=C3=83=C2=BCns?= Date: Sun, 12 Mar 2017 06:00:26 +0100 Subject: [PATCH 2/5] SOFT_PWM: Implement dithering if SOFT_PWM_SCALE is 1 or more MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If dithering is enabled, the remainder of the soft_pwm_X duty value at turnoff time is added to the next cycle. If e.g. the duty is set to 9 and SCALE is set to 2, the PWM will be active for 8 counts for 3 cycles and 12 counts on each fourth cycle, i.e. the average is 9 cycles. This compensates the resolution loss at higher scales and allows running fans with SOFT_PWM with significantly reduced noise. Signed-off-by: Stefan Brüns --- Marlin/Configuration.h | 6 ++++++ Marlin/temperature.cpp | 44 +++++++++++++++++++++++++----------------- 2 files changed, 32 insertions(+), 18 deletions(-) diff --git a/Marlin/Configuration.h b/Marlin/Configuration.h index 96b1ededc..d290af575 100644 --- a/Marlin/Configuration.h +++ b/Marlin/Configuration.h @@ -1424,6 +1424,12 @@ // at zero value, there are 128 effective control positions. #define SOFT_PWM_SCALE 0 +// If SOFT_PWM_SCALE is set to a value higher than 0, dithering can +// be used to mitigate the associated resolution loss. If enabled, +// some of the PWM cycles are stretched so on average the wanted +// duty cycle is attained. +//#define SOFT_PWM_DITHER + // Temperature status LEDs that display the hotend and bed temperature. // If all hotends and bed temperature and temperature setpoint are < 54C then the BLUE led is on. // Otherwise the RED led is on. There is 1C hysteresis. diff --git a/Marlin/temperature.cpp b/Marlin/temperature.cpp index 66390d8c7..0cec6fc7c 100644 --- a/Marlin/temperature.cpp +++ b/Marlin/temperature.cpp @@ -1521,7 +1521,7 @@ void Temperature::isr() { static uint8_t state_heater_ ## n = 0; \ static uint8_t state_timer_heater_ ## n = 0 #else - #define ISR_STATICS(n) static uint8_t soft_pwm_ ## n + #define ISR_STATICS(n) static uint8_t soft_pwm_ ## n = 0 #endif // Statics per heater @@ -1544,43 +1544,51 @@ void Temperature::isr() { #endif #if DISABLED(SLOW_PWM_HEATERS) + constexpr uint8_t pwm_mask = + #if ENABLED(SOFT_PWM_DITHER) + _BV(SOFT_PWM_SCALE) - 1 + #else + 0 + #endif + ; + /** * Standard PWM modulation */ if (pwm_count >= 127) { - pwm_count = 0; - soft_pwm_0 = soft_pwm[0]; - WRITE_HEATER_0(soft_pwm_0 > 0 ? HIGH : LOW); + pwm_count -= 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 - soft_pwm_1 = soft_pwm[1]; - WRITE_HEATER_1(soft_pwm_1 > 0 ? HIGH : LOW); + soft_pwm_1 = (soft_pwm_1 & pwm_mask) + soft_pwm[1]; + WRITE_HEATER_1(soft_pwm_1 > pwm_mask ? HIGH : LOW); #if HOTENDS > 2 - soft_pwm_2 = soft_pwm[2]; - WRITE_HEATER_2(soft_pwm_2 > 0 ? HIGH : LOW); + soft_pwm_2 = (soft_pwm_2 & pwm_mask) + soft_pwm[2]; + WRITE_HEATER_2(soft_pwm_2 > pwm_mask ? HIGH : LOW); #if HOTENDS > 3 - soft_pwm_3 = soft_pwm[3]; - WRITE_HEATER_3(soft_pwm_3 > 0 ? HIGH : LOW); + soft_pwm_3 = (soft_pwm_3 & pwm_mask) + soft_pwm[3]; + WRITE_HEATER_3(soft_pwm_3 > pwm_mask ? HIGH : LOW); #endif #endif #endif #if HAS_HEATER_BED - soft_pwm_BED = soft_pwm_bed; - WRITE_HEATER_BED(soft_pwm_BED > 0 ? HIGH : LOW); + soft_pwm_BED = (soft_pwm_BED & pwm_mask) + soft_pwm_bed; + WRITE_HEATER_BED(soft_pwm_BED > pwm_mask ? HIGH : LOW); #endif #if ENABLED(FAN_SOFT_PWM) #if HAS_FAN0 - soft_pwm_fan[0] = fanSpeedSoftPwm[0] >> 1; - WRITE_FAN(soft_pwm_fan[0] > 0 ? HIGH : LOW); + soft_pwm_fan[0] = (soft_pwm_fan[0] & pwm_mask) + fanSpeedSoftPwm[0] >> 1; + WRITE_FAN(soft_pwm_fan[0] > pwm_mask ? HIGH : LOW); #endif #if HAS_FAN1 - soft_pwm_fan[1] = fanSpeedSoftPwm[1] >> 1; - WRITE_FAN1(soft_pwm_fan[1] > 0 ? HIGH : LOW); + soft_pwm_fan[1] = (soft_pwm_fan[1] & pwm_mask) + fanSpeedSoftPwm[1] >> 1; + WRITE_FAN1(soft_pwm_fan[1] > pwm_mask ? HIGH : LOW); #endif #if HAS_FAN2 - soft_pwm_fan[2] = fanSpeedSoftPwm[2] >> 1; - WRITE_FAN2(soft_pwm_fan[2] > 0 ? HIGH : LOW); + soft_pwm_fan[2] = (soft_pwm_fan[2] & pwm_mask) + fanSpeedSoftPwm[2] >> 1; + WRITE_FAN2(soft_pwm_fan[2] > pwm_mask ? HIGH : LOW); #endif #endif } 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 3/5] 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 0cec6fc7c..33ef2ccda 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). From 6a040a69670e66e1e0122c8849fbc87b9df68da0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Br=C3=BCns?= Date: Sat, 18 Mar 2017 20:28:57 +0100 Subject: [PATCH 4/5] SOFT_PWM: Do not switch off heaters twice on pwm_count wraparound MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit After wraparound, pwm_count <= pwm_mask holds, thus soft_pwm_X <= pwm_count guarantees soft_pwm_X < pwm_mask is true, and the heater will be switched off in the first branch. Do not evaluate the pwm conditions a second time, this reduces the instruction count (4 instructions per PWM) and text size (6 byte). Signed-off-by: Stefan Brüns --- Marlin/temperature.cpp | 43 +++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/Marlin/temperature.cpp b/Marlin/temperature.cpp index 33ef2ccda..83c5e7176 100644 --- a/Marlin/temperature.cpp +++ b/Marlin/temperature.cpp @@ -1594,33 +1594,34 @@ void Temperature::isr() { #endif #endif } - - if (soft_pwm_0 <= pwm_count_tmp) WRITE_HEATER_0(0); - #if HOTENDS > 1 - if (soft_pwm_1 <= pwm_count_tmp) WRITE_HEATER_1(0); + else { + if (soft_pwm_0 <= pwm_count_tmp) WRITE_HEATER_0(0); + #if HOTENDS > 1 + if (soft_pwm_1 <= pwm_count_tmp) WRITE_HEATER_1(0); + #endif #if HOTENDS > 2 if (soft_pwm_2 <= pwm_count_tmp) WRITE_HEATER_2(0); - #if HOTENDS > 3 - if (soft_pwm_3 <= pwm_count_tmp) WRITE_HEATER_3(0); - #endif #endif - #endif - - #if HAS_HEATER_BED - 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_tmp) WRITE_FAN(0); + #if HOTENDS > 3 + if (soft_pwm_3 <= pwm_count_tmp) WRITE_HEATER_3(0); #endif - #if HAS_FAN1 - if (soft_pwm_fan[1] <= pwm_count_tmp) WRITE_FAN1(0); + + #if HAS_HEATER_BED + if (soft_pwm_BED <= pwm_count_tmp) WRITE_HEATER_BED(0); #endif - #if HAS_FAN2 - if (soft_pwm_fan[2] <= pwm_count_tmp) WRITE_FAN2(0); + + #if ENABLED(FAN_SOFT_PWM) + #if HAS_FAN0 + if (soft_pwm_fan[0] <= pwm_count_tmp) WRITE_FAN(0); + #endif + #if HAS_FAN1 + if (soft_pwm_fan[1] <= pwm_count_tmp) WRITE_FAN1(0); + #endif + #if HAS_FAN2 + if (soft_pwm_fan[2] <= pwm_count_tmp) WRITE_FAN2(0); + #endif #endif - #endif + } // SOFT_PWM_SCALE to frequency: // From 043be2856b22e8209009c78e0a29b56d8d23bfb1 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Thu, 23 Mar 2017 04:20:25 -0500 Subject: [PATCH 5/5] Use "& 0x3F" instead of "% 64" --- Marlin/temperature.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Marlin/temperature.cpp b/Marlin/temperature.cpp index 83c5e7176..50a8f7b1d 100644 --- a/Marlin/temperature.cpp +++ b/Marlin/temperature.cpp @@ -1729,7 +1729,7 @@ void Temperature::isr() { #if HAS_FAN2 if (soft_pwm_fan[2] <= pwm_count_tmp) WRITE_FAN2(0); #endif - #endif //FAN_SOFT_PWM + #endif // FAN_SOFT_PWM // SOFT_PWM_SCALE to frequency: // @@ -1743,9 +1743,9 @@ void Temperature::isr() { // increment slow_pwm_count only every 64th pwm_count, // i.e. yielding a PWM frequency of 16/128 Hz (8s). - if (((pwm_count >> SOFT_PWM_SCALE) % 64) == 0) { + if (((pwm_count >> SOFT_PWM_SCALE) & 0x3F) == 0) { slow_pwm_count++; - slow_pwm_count &= 0x7f; + slow_pwm_count &= 0x7F; // EXTRUDER 0 if (state_timer_heater_0 > 0) state_timer_heater_0--; @@ -1761,7 +1761,7 @@ void Temperature::isr() { #if HAS_HEATER_BED if (state_timer_heater_BED > 0) state_timer_heater_BED--; #endif - } // (pwm_count % 64) == 0 + } // ((pwm_count >> SOFT_PWM_SCALE) & 0x3F) == 0 #endif // SLOW_PWM_HEATERS