Browse Source

soft_pwm: avoid useless refetches of pwm_count

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 <stefan.bruens@rwth-aachen.de>
Stefan Brüns 7 years ago
parent
commit
0a74774af1
1 changed files with 19 additions and 17 deletions
  1. 19
    17
      Marlin/temperature.cpp

+ 19
- 17
Marlin/temperature.cpp View File

@@ -1512,6 +1512,8 @@ void Temperature::isr() {
1512 1512
   static uint8_t temp_count = 0;
1513 1513
   static TempState temp_state = StartupDelay;
1514 1514
   static uint8_t pwm_count = _BV(SOFT_PWM_SCALE);
1515
+  // avoid multiple loads of pwm_count
1516
+  uint8_t pwm_count_tmp = pwm_count;
1515 1517
 
1516 1518
   // Static members for each heater
1517 1519
   #if ENABLED(SLOW_PWM_HEATERS)
@@ -1555,8 +1557,8 @@ void Temperature::isr() {
1555 1557
     /**
1556 1558
      * Standard PWM modulation
1557 1559
      */
1558
-    if (pwm_count >= 127) {
1559
-      pwm_count -= 127;
1560
+    if (pwm_count_tmp >= 127) {
1561
+      pwm_count_tmp -= 127;
1560 1562
       soft_pwm_0 = (soft_pwm_0 & pwm_mask) + soft_pwm[0];
1561 1563
       WRITE_HEATER_0(soft_pwm_0 > pwm_mask ? HIGH : LOW);
1562 1564
       #if HOTENDS > 1
@@ -1593,30 +1595,30 @@ void Temperature::isr() {
1593 1595
       #endif
1594 1596
     }
1595 1597
 
1596
-    if (soft_pwm_0 <= pwm_count) WRITE_HEATER_0(0);
1598
+    if (soft_pwm_0 <= pwm_count_tmp) WRITE_HEATER_0(0);
1597 1599
     #if HOTENDS > 1
1598
-      if (soft_pwm_1 <= pwm_count) WRITE_HEATER_1(0);
1600
+      if (soft_pwm_1 <= pwm_count_tmp) WRITE_HEATER_1(0);
1599 1601
       #if HOTENDS > 2
1600
-        if (soft_pwm_2 <= pwm_count) WRITE_HEATER_2(0);
1602
+        if (soft_pwm_2 <= pwm_count_tmp) WRITE_HEATER_2(0);
1601 1603
         #if HOTENDS > 3
1602
-          if (soft_pwm_3 <= pwm_count) WRITE_HEATER_3(0);
1604
+          if (soft_pwm_3 <= pwm_count_tmp) WRITE_HEATER_3(0);
1603 1605
         #endif
1604 1606
       #endif
1605 1607
     #endif
1606 1608
 
1607 1609
     #if HAS_HEATER_BED
1608
-      if (soft_pwm_BED <= pwm_count) WRITE_HEATER_BED(0);
1610
+      if (soft_pwm_BED <= pwm_count_tmp) WRITE_HEATER_BED(0);
1609 1611
     #endif
1610 1612
 
1611 1613
     #if ENABLED(FAN_SOFT_PWM)
1612 1614
       #if HAS_FAN0
1613
-        if (soft_pwm_fan[0] <= pwm_count) WRITE_FAN(0);
1615
+        if (soft_pwm_fan[0] <= pwm_count_tmp) WRITE_FAN(0);
1614 1616
       #endif
1615 1617
       #if HAS_FAN1
1616
-        if (soft_pwm_fan[1] <= pwm_count) WRITE_FAN1(0);
1618
+        if (soft_pwm_fan[1] <= pwm_count_tmp) WRITE_FAN1(0);
1617 1619
       #endif
1618 1620
       #if HAS_FAN2
1619
-        if (soft_pwm_fan[2] <= pwm_count) WRITE_FAN2(0);
1621
+        if (soft_pwm_fan[2] <= pwm_count_tmp) WRITE_FAN2(0);
1620 1622
       #endif
1621 1623
     #endif
1622 1624
 
@@ -1628,7 +1630,7 @@ void Temperature::isr() {
1628 1630
     // 3:                / 16 =  61.0352 Hz
1629 1631
     // 4:                /  8 = 122.0703 Hz
1630 1632
     // 5:                /  4 = 244.1406 Hz
1631
-    pwm_count += _BV(SOFT_PWM_SCALE);
1633
+    pwm_count = pwm_count_tmp + _BV(SOFT_PWM_SCALE);
1632 1634
 
1633 1635
   #else // SLOW_PWM_HEATERS
1634 1636
 
@@ -1702,8 +1704,8 @@ void Temperature::isr() {
1702 1704
     #endif
1703 1705
 
1704 1706
     #if ENABLED(FAN_SOFT_PWM)
1705
-      if (pwm_count >= 127) {
1706
-        pwm_count = 0;
1707
+      if (pwm_count_tmp >= 127) {
1708
+        pwm_count_tmp = 0;
1707 1709
         #if HAS_FAN0
1708 1710
           soft_pwm_fan[0] = fanSpeedSoftPwm[0] >> 1;
1709 1711
           WRITE_FAN(soft_pwm_fan[0] > 0 ? HIGH : LOW);
@@ -1718,13 +1720,13 @@ void Temperature::isr() {
1718 1720
         #endif
1719 1721
       }
1720 1722
       #if HAS_FAN0
1721
-        if (soft_pwm_fan[0] <= pwm_count) WRITE_FAN(0);
1723
+        if (soft_pwm_fan[0] <= pwm_count_tmp) WRITE_FAN(0);
1722 1724
       #endif
1723 1725
       #if HAS_FAN1
1724
-        if (soft_pwm_fan[1] <= pwm_count) WRITE_FAN1(0);
1726
+        if (soft_pwm_fan[1] <= pwm_count_tmp) WRITE_FAN1(0);
1725 1727
       #endif
1726 1728
       #if HAS_FAN2
1727
-        if (soft_pwm_fan[2] <= pwm_count) WRITE_FAN2(0);
1729
+        if (soft_pwm_fan[2] <= pwm_count_tmp) WRITE_FAN2(0);
1728 1730
       #endif
1729 1731
     #endif //FAN_SOFT_PWM
1730 1732
 
@@ -1736,7 +1738,7 @@ void Temperature::isr() {
1736 1738
     // 3:                / 16 =  61.0352 Hz
1737 1739
     // 4:                /  8 = 122.0703 Hz
1738 1740
     // 5:                /  4 = 244.1406 Hz
1739
-    pwm_count += _BV(SOFT_PWM_SCALE);
1741
+    pwm_count = pwm_count_tmp + _BV(SOFT_PWM_SCALE);
1740 1742
 
1741 1743
     // increment slow_pwm_count only every 64th pwm_count,
1742 1744
     // i.e. yielding a PWM frequency of 16/128 Hz (8s).

Loading…
Cancel
Save