瀏覽代碼

Move temp errors calling kill() out of ISR (#21832)

Scott Lahteine 4 年之前
父節點
當前提交
42a2b5c3ec
共有 2 個文件被更改,包括 94 次插入91 次删除
  1. 84
    89
      Marlin/src/module/temperature.cpp
  2. 10
    2
      Marlin/src/module/temperature.h

+ 84
- 89
Marlin/src/module/temperature.cpp 查看文件

@@ -576,8 +576,7 @@ volatile bool Temperature::raw_temps_ready = false;
576 576
 
577 577
       const millis_t ms = millis();
578 578
 
579
-      if (raw_temps_ready) { // temp sample ready
580
-        updateTemperaturesFromRawValues();
579
+      if (updateTemperaturesIfReady()) { // temp sample ready
581 580
 
582 581
         // Get the current temperature and constrain it
583 582
         current_temp = GHV(degChamber(), degBed(), degHotend(heater_id));
@@ -1212,9 +1211,7 @@ void Temperature::manage_heater() {
1212 1211
     }
1213 1212
   #endif
1214 1213
 
1215
-  if (!raw_temps_ready) return;
1216
-
1217
-  updateTemperaturesFromRawValues(); // also resets the watchdog
1214
+  if (!updateTemperaturesIfReady()) return; // Will also reset the watchdog if temperatures are ready
1218 1215
 
1219 1216
   #if DISABLED(IGNORE_THERMOCOUPLE_ERRORS)
1220 1217
     #if TEMP_SENSOR_0_IS_MAX_TC
@@ -1890,29 +1887,88 @@ void Temperature::manage_heater() {
1890 1887
 #endif // HAS_TEMP_PROBE
1891 1888
 
1892 1889
 /**
1893
- * Get the raw values into the actual temperatures.
1894
- * The raw values are created in interrupt context,
1895
- * and this function is called from normal context
1896
- * as it would block the stepper routine.
1890
+ * Convert the raw sensor readings into actual Celsius temperatures and
1891
+ * validate raw temperatures. Bad readings generate min/maxtemp errors.
1892
+ *
1893
+ * The raw values are generated entirely in interrupt context, and this
1894
+ * method is called from normal context once 'raw_temps_ready' has been
1895
+ * set by update_raw_temperatures().
1896
+ *
1897
+ * The watchdog is dependent on this method. If 'raw_temps_ready' stops
1898
+ * being set by the interrupt so that this method is not called for over
1899
+ * 4 seconds then something has gone afoul and the machine will be reset.
1897 1900
  */
1898 1901
 void Temperature::updateTemperaturesFromRawValues() {
1902
+
1903
+  watchdog_refresh(); // Reset because raw_temps_ready was set by the interrupt
1904
+
1899 1905
   TERN_(TEMP_SENSOR_0_IS_MAX_TC, temp_hotend[0].raw = READ_MAX_TC(0));
1900 1906
   TERN_(TEMP_SENSOR_1_IS_MAX_TC, TERN(TEMP_SENSOR_1_AS_REDUNDANT, temp_redundant, temp_hotend[1]).raw = READ_MAX_TC(1));
1901 1907
   #if HAS_HOTEND
1902 1908
     HOTEND_LOOP() temp_hotend[e].celsius = analog_to_celsius_hotend(temp_hotend[e].raw, e);
1903 1909
   #endif
1904 1910
   TERN_(TEMP_SENSOR_1_AS_REDUNDANT, temp_redundant.celsius = analog_to_celsius_hotend(temp_redundant.raw, 1));
1905
-  TERN_(HAS_HEATED_BED, temp_bed.celsius = analog_to_celsius_bed(temp_bed.raw));
1911
+
1912
+  TERN_(HAS_HEATED_BED,   temp_bed.celsius     = analog_to_celsius_bed(temp_bed.raw));
1906 1913
   TERN_(HAS_TEMP_CHAMBER, temp_chamber.celsius = analog_to_celsius_chamber(temp_chamber.raw));
1907
-  TERN_(HAS_TEMP_COOLER, temp_cooler.celsius = analog_to_celsius_cooler(temp_cooler.raw));
1908
-  TERN_(HAS_TEMP_PROBE, temp_probe.celsius = analog_to_celsius_probe(temp_probe.raw));
1914
+  TERN_(HAS_TEMP_COOLER,  temp_cooler.celsius  = analog_to_celsius_cooler(temp_cooler.raw));
1915
+  TERN_(HAS_TEMP_PROBE,   temp_probe.celsius   = analog_to_celsius_probe(temp_probe.raw));
1916
+
1909 1917
   TERN_(FILAMENT_WIDTH_SENSOR, filwidth.update_measured_mm());
1910
-  TERN_(HAS_POWER_MONITOR, power_monitor.capture_values());
1918
+  TERN_(HAS_POWER_MONITOR,     power_monitor.capture_values());
1911 1919
 
1912
-  // Reset the watchdog on good temperature measurement
1913
-  watchdog_refresh();
1920
+  #if HAS_HOTEND
1921
+
1922
+    static constexpr int8_t temp_dir[] = {
1923
+      TERN(TEMP_SENSOR_0_IS_MAX_TC, 0, TEMPDIR(0))
1924
+      #if HAS_MULTI_HOTEND
1925
+        , TERN(TEMP_SENSOR_1_IS_MAX_TC, 0, TEMPDIR(1))
1926
+        #if HOTENDS > 2
1927
+          #define _TEMPDIR(N) , TEMPDIR(N)
1928
+          REPEAT_S(2, HOTENDS, _TEMPDIR)
1929
+        #endif
1930
+      #endif
1931
+    };
1932
+
1933
+    LOOP_L_N(e, COUNT(temp_dir)) {
1934
+      const int8_t tdir = temp_dir[e];
1935
+      if (tdir) {
1936
+        const int16_t rawtemp = temp_hotend[e].raw * tdir; // normal direction, +rawtemp, else -rawtemp
1937
+        if (rawtemp > temp_range[e].raw_max * tdir) max_temp_error((heater_id_t)e);
1914 1938
 
1915
-  raw_temps_ready = false;
1939
+        const bool heater_on = temp_hotend[e].target > 0;
1940
+        if (heater_on && rawtemp < temp_range[e].raw_min * tdir && !is_preheating(e)) {
1941
+          #if MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED > 1
1942
+            if (++consecutive_low_temperature_error[e] >= MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED)
1943
+          #endif
1944
+              min_temp_error((heater_id_t)e);
1945
+        }
1946
+        #if MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED > 1
1947
+          else
1948
+            consecutive_low_temperature_error[e] = 0;
1949
+        #endif
1950
+      }
1951
+    }
1952
+
1953
+  #endif // HAS_HOTEND
1954
+
1955
+  #if ENABLED(THERMAL_PROTECTION_BED)
1956
+    #define BEDCMP(A,B) (TEMPDIR(BED) < 0 ? ((A)<(B)) : ((A)>(B)))
1957
+    if (BEDCMP(temp_bed.raw, maxtemp_raw_BED)) max_temp_error(H_BED);
1958
+    if (temp_bed.target > 0 && BEDCMP(mintemp_raw_BED, temp_bed.raw)) min_temp_error(H_BED);
1959
+  #endif
1960
+
1961
+  #if BOTH(HAS_HEATED_CHAMBER, THERMAL_PROTECTION_CHAMBER)
1962
+    #define CHAMBERCMP(A,B) (TEMPDIR(CHAMBER) < 0 ? ((A)<(B)) : ((A)>(B)))
1963
+    if (CHAMBERCMP(temp_chamber.raw, maxtemp_raw_CHAMBER)) max_temp_error(H_CHAMBER);
1964
+    if (temp_chamber.target > 0 && CHAMBERCMP(mintemp_raw_CHAMBER, temp_chamber.raw)) min_temp_error(H_CHAMBER);
1965
+  #endif
1966
+
1967
+  #if BOTH(HAS_COOLER, THERMAL_PROTECTION_COOLER)
1968
+    #define COOLERCMP(A,B) (TEMPDIR(COOLER) < 0 ? ((A)<(B)) : ((A)>(B)))
1969
+    if (cutter.unitPower > 0 && COOLERCMP(temp_cooler.raw, maxtemp_raw_COOLER)) max_temp_error(H_COOLER);
1970
+    if (COOLERCMP(mintemp_raw_COOLER, temp_cooler.raw)) min_temp_error(H_COOLER);
1971
+  #endif
1916 1972
 }
1917 1973
 
1918 1974
 #if THERMO_SEPARATE_SPI
@@ -2657,6 +2713,9 @@ void Temperature::disable_all_heaters() {
2657 2713
 
2658 2714
 /**
2659 2715
  * Update raw temperatures
2716
+ *
2717
+ * Called by ISR => readings_ready when new temperatures have been set by updateTemperaturesFromRawValues.
2718
+ * Applies all the accumulators to the current raw temperatures.
2660 2719
  */
2661 2720
 void Temperature::update_raw_temperatures() {
2662 2721
 
@@ -2686,14 +2745,19 @@ void Temperature::update_raw_temperatures() {
2686 2745
   TERN_(HAS_JOY_ADC_X, joystick.x.update());
2687 2746
   TERN_(HAS_JOY_ADC_Y, joystick.y.update());
2688 2747
   TERN_(HAS_JOY_ADC_Z, joystick.z.update());
2689
-
2690
-  raw_temps_ready = true;
2691 2748
 }
2692 2749
 
2750
+/**
2751
+ * Called by the Temperature ISR when all the ADCs have been processed.
2752
+ * Reset all the ADC accumulators for another round of updates.
2753
+ */
2693 2754
 void Temperature::readings_ready() {
2694 2755
 
2695
-  // Update the raw values if they've been read. Else we could be updating them during reading.
2696
-  if (!raw_temps_ready) update_raw_temperatures();
2756
+  // Update raw values only if they're not already set.
2757
+  if (!raw_temps_ready) {
2758
+    update_raw_temperatures();
2759
+    raw_temps_ready = true;
2760
+  }
2697 2761
 
2698 2762
   // Filament Sensor - can be read any time since IIR filtering is used
2699 2763
   TERN_(FILAMENT_WIDTH_SENSOR, filwidth.reading_ready());
@@ -2711,75 +2775,6 @@ void Temperature::readings_ready() {
2711 2775
   TERN_(HAS_JOY_ADC_X, joystick.x.reset());
2712 2776
   TERN_(HAS_JOY_ADC_Y, joystick.y.reset());
2713 2777
   TERN_(HAS_JOY_ADC_Z, joystick.z.reset());
2714
-
2715
-  #if HAS_HOTEND
2716
-
2717
-    static constexpr int8_t temp_dir[] = {
2718
-      TERN(TEMP_SENSOR_0_IS_MAX_TC, 0, TEMPDIR(0))
2719
-      #if HAS_MULTI_HOTEND
2720
-        , TERN(TEMP_SENSOR_1_IS_MAX_TC, 0, TEMPDIR(1))
2721
-        #if HOTENDS > 2
2722
-          #define _TEMPDIR(N) , TEMPDIR(N)
2723
-          REPEAT_S(2, HOTENDS, _TEMPDIR)
2724
-        #endif
2725
-      #endif
2726
-    };
2727
-
2728
-    LOOP_L_N(e, COUNT(temp_dir)) {
2729
-      const int8_t tdir = temp_dir[e];
2730
-      if (tdir) {
2731
-        const int16_t rawtemp = temp_hotend[e].raw * tdir; // normal direction, +rawtemp, else -rawtemp
2732
-        if (rawtemp > temp_range[e].raw_max * tdir) max_temp_error((heater_id_t)e);
2733
-
2734
-        const bool heater_on = (temp_hotend[e].target > 0 || TERN0(PIDTEMP, temp_hotend[e].soft_pwm_amount > 0));
2735
-        if (heater_on && rawtemp < temp_range[e].raw_min * tdir && !is_preheating(e)) {
2736
-          #if MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED > 1
2737
-            if (++consecutive_low_temperature_error[e] >= MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED)
2738
-          #endif
2739
-              min_temp_error((heater_id_t)e);
2740
-        }
2741
-        #if MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED > 1
2742
-          else
2743
-            consecutive_low_temperature_error[e] = 0;
2744
-        #endif
2745
-      }
2746
-    }
2747
-
2748
-  #endif // HAS_HOTEND
2749
-
2750
-  #if ENABLED(THERMAL_PROTECTION_BED)
2751
-    #if TEMPDIR(BED) < 0
2752
-      #define BEDCMP(A,B) ((A)<(B))
2753
-    #else
2754
-      #define BEDCMP(A,B) ((A)>(B))
2755
-    #endif
2756
-    const bool bed_on = (temp_bed.target > 0) || TERN0(PIDTEMPBED, temp_bed.soft_pwm_amount > 0);
2757
-    if (BEDCMP(temp_bed.raw, maxtemp_raw_BED)) max_temp_error(H_BED);
2758
-    if (bed_on && BEDCMP(mintemp_raw_BED, temp_bed.raw)) min_temp_error(H_BED);
2759
-  #endif
2760
-
2761
-  #if BOTH(HAS_HEATED_CHAMBER, THERMAL_PROTECTION_CHAMBER)
2762
-    #if TEMPDIR(CHAMBER) < 0
2763
-      #define CHAMBERCMP(A,B) ((A)<(B))
2764
-    #else
2765
-      #define CHAMBERCMP(A,B) ((A)>(B))
2766
-    #endif
2767
-    const bool chamber_on = (temp_chamber.target > 0);
2768
-    if (CHAMBERCMP(temp_chamber.raw, maxtemp_raw_CHAMBER)) max_temp_error(H_CHAMBER);
2769
-    if (chamber_on && CHAMBERCMP(mintemp_raw_CHAMBER, temp_chamber.raw)) min_temp_error(H_CHAMBER);
2770
-  #endif
2771
-
2772
-  #if BOTH(HAS_COOLER, THERMAL_PROTECTION_COOLER)
2773
-    #if TEMPDIR(COOLER) < 0
2774
-      #define COOLERCMP(A,B) ((A)<(B))
2775
-    #else
2776
-      #define COOLERCMP(A,B) ((A)>(B))
2777
-    #endif
2778
-    if (cutter.unitPower > 0) {
2779
-      if (COOLERCMP(temp_cooler.raw, maxtemp_raw_COOLER)) max_temp_error(H_COOLER);
2780
-    }
2781
-    if (COOLERCMP(mintemp_raw_COOLER, temp_cooler.raw)) min_temp_error(H_COOLER);
2782
-  #endif
2783 2778
 }
2784 2779
 
2785 2780
 /**

+ 10
- 2
Marlin/src/module/temperature.h 查看文件

@@ -419,8 +419,6 @@ class Temperature {
419 419
 
420 420
   private:
421 421
 
422
-    static volatile bool raw_temps_ready;
423
-
424 422
     #if ENABLED(WATCH_HOTENDS)
425 423
       static hotend_watch_t watch_hotend[HOTENDS];
426 424
     #endif
@@ -880,9 +878,19 @@ class Temperature {
880 878
     #endif
881 879
 
882 880
   private:
881
+
882
+    // Reading raw temperatures and converting to Celsius when ready
883
+    static volatile bool raw_temps_ready;
883 884
     static void update_raw_temperatures();
884 885
     static void updateTemperaturesFromRawValues();
886
+    static inline bool updateTemperaturesIfReady() {
887
+      if (!raw_temps_ready) return false;
888
+      updateTemperaturesFromRawValues();
889
+      raw_temps_ready = false;
890
+      return true;
891
+    }
885 892
 
893
+    // MAX Thermocouples
886 894
     #if HAS_MAX_TC
887 895
       #define MAX_TC_COUNT 1 + BOTH(TEMP_SENSOR_0_IS_MAX_TC, TEMP_SENSOR_1_IS_MAX_TC)
888 896
       #if MAX_TC_COUNT > 1

Loading…
取消
儲存