ソースを参照

⚡️ Fix noisy ADC - 16x oversampling with 12-bit ADC (#23867)

tombrazier 3年前
コミット
631e35bfd6
コミッターのメールアドレスに関連付けられたアカウントが存在しません

+ 1
- 0
Marlin/src/core/types.h ファイルの表示

@@ -128,6 +128,7 @@ typedef float feedRate_t;
128 128
 // celsius_t is the native unit of temperature. Signed to handle a disconnected thermistor value (-14).
129 129
 // For more resolition (e.g., for a chocolate printer) this may later be changed to Celsius x 100
130 130
 //
131
+typedef uint16_t raw_adc_t;
131 132
 typedef int16_t celsius_t;
132 133
 typedef float celsius_float_t;
133 134
 

+ 11
- 11
Marlin/src/feature/joystick.cpp ファイルの表示

@@ -68,13 +68,13 @@ Joystick joystick;
68 68
   void Joystick::report() {
69 69
     SERIAL_ECHOPGM("Joystick");
70 70
     #if HAS_JOY_ADC_X
71
-      SERIAL_ECHOPGM_P(SP_X_STR, JOY_X(x.raw));
71
+      SERIAL_ECHOPGM_P(SP_X_STR, JOY_X(x.getraw()));
72 72
     #endif
73 73
     #if HAS_JOY_ADC_Y
74
-      SERIAL_ECHOPGM_P(SP_Y_STR, JOY_Y(y.raw));
74
+      SERIAL_ECHOPGM_P(SP_Y_STR, JOY_Y(y.getraw()));
75 75
     #endif
76 76
     #if HAS_JOY_ADC_Z
77
-      SERIAL_ECHOPGM_P(SP_Z_STR, JOY_Z(z.raw));
77
+      SERIAL_ECHOPGM_P(SP_Z_STR, JOY_Z(z.getraw()));
78 78
     #endif
79 79
     #if HAS_JOY_ADC_EN
80 80
       SERIAL_ECHO_TERNARY(READ(JOY_EN_PIN), " EN=", "HIGH (dis", "LOW (en", "abled)");
@@ -91,29 +91,29 @@ Joystick joystick;
91 91
       if (READ(JOY_EN_PIN)) return;
92 92
     #endif
93 93
 
94
-    auto _normalize_joy = [](float &axis_jog, const int16_t raw, const int16_t (&joy_limits)[4]) {
94
+    auto _normalize_joy = [](float &axis_jog, const raw_adc_t raw, const raw_adc_t (&joy_limits)[4]) {
95 95
       if (WITHIN(raw, joy_limits[0], joy_limits[3])) {
96 96
         // within limits, check deadzone
97 97
         if (raw > joy_limits[2])
98 98
           axis_jog = (raw - joy_limits[2]) / float(joy_limits[3] - joy_limits[2]);
99 99
         else if (raw < joy_limits[1])
100
-          axis_jog = (raw - joy_limits[1]) / float(joy_limits[1] - joy_limits[0]);  // negative value
100
+          axis_jog = int16_t(raw - joy_limits[1]) / float(joy_limits[1] - joy_limits[0]);  // negative value
101 101
         // Map normal to jog value via quadratic relationship
102 102
         axis_jog = SIGN(axis_jog) * sq(axis_jog);
103 103
       }
104 104
     };
105 105
 
106 106
     #if HAS_JOY_ADC_X
107
-      static constexpr int16_t joy_x_limits[4] = JOY_X_LIMITS;
108
-      _normalize_joy(norm_jog.x, JOY_X(x.raw), joy_x_limits);
107
+      static constexpr raw_adc_t joy_x_limits[4] = JOY_X_LIMITS;
108
+      _normalize_joy(norm_jog.x, JOY_X(x.getraw()), joy_x_limits);
109 109
     #endif
110 110
     #if HAS_JOY_ADC_Y
111
-      static constexpr int16_t joy_y_limits[4] = JOY_Y_LIMITS;
112
-      _normalize_joy(norm_jog.y, JOY_Y(y.raw), joy_y_limits);
111
+      static constexpr raw_adc_t joy_y_limits[4] = JOY_Y_LIMITS;
112
+      _normalize_joy(norm_jog.y, JOY_Y(y.getraw()), joy_y_limits);
113 113
     #endif
114 114
     #if HAS_JOY_ADC_Z
115
-      static constexpr int16_t joy_z_limits[4] = JOY_Z_LIMITS;
116
-      _normalize_joy(norm_jog.z, JOY_Z(z.raw), joy_z_limits);
115
+      static constexpr raw_adc_t joy_z_limits[4] = JOY_Z_LIMITS;
116
+      _normalize_joy(norm_jog.z, JOY_Z(z.getraw()), joy_z_limits);
117 117
     #endif
118 118
   }
119 119
 

+ 7
- 7
Marlin/src/lcd/marlinui.cpp ファイルの表示

@@ -1176,7 +1176,7 @@ void MarlinUI::init() {
1176 1176
   #if HAS_ADC_BUTTONS
1177 1177
 
1178 1178
     typedef struct {
1179
-      uint16_t ADCKeyValueMin, ADCKeyValueMax;
1179
+      raw_adc_t ADCKeyValueMin, ADCKeyValueMax;
1180 1180
       uint8_t  ADCKeyNo;
1181 1181
     } _stADCKeypadTable_;
1182 1182
 
@@ -1203,10 +1203,10 @@ void MarlinUI::init() {
1203 1203
     #endif
1204 1204
 
1205 1205
     // Calculate the ADC value for the voltage divider with specified pull-down resistor value
1206
-    #define ADC_BUTTON_VALUE(r)  int(HAL_ADC_RANGE * (ADC_BUTTONS_VALUE_SCALE) * r / (r + ADC_BUTTONS_R_PULLUP))
1206
+    #define ADC_BUTTON_VALUE(r)  raw_adc_t(HAL_ADC_RANGE * (ADC_BUTTONS_VALUE_SCALE) * r / (r + ADC_BUTTONS_R_PULLUP))
1207 1207
 
1208
-    static constexpr uint16_t adc_button_tolerance = HAL_ADC_RANGE *   25 / 1024,
1209
-                                  adc_other_button = HAL_ADC_RANGE * 1000 / 1024;
1208
+    static constexpr raw_adc_t adc_button_tolerance = HAL_ADC_RANGE *   25 / 1024,
1209
+                                   adc_other_button = HAL_ADC_RANGE * 1000 / 1024;
1210 1210
     static const _stADCKeypadTable_ stADCKeyTable[] PROGMEM = {
1211 1211
       // VALUE_MIN, VALUE_MAX, KEY
1212 1212
       { adc_other_button, HAL_ADC_RANGE, 1 + BLEN_KEYPAD_F1     }, // F1
@@ -1226,13 +1226,13 @@ void MarlinUI::init() {
1226 1226
 
1227 1227
     uint8_t get_ADC_keyValue() {
1228 1228
       if (thermalManager.ADCKey_count >= 16) {
1229
-        const uint16_t currentkpADCValue = thermalManager.current_ADCKey_raw;
1229
+        const raw_adc_t currentkpADCValue = thermalManager.current_ADCKey_raw;
1230 1230
         thermalManager.current_ADCKey_raw = HAL_ADC_RANGE;
1231 1231
         thermalManager.ADCKey_count = 0;
1232 1232
         if (currentkpADCValue < adc_other_button)
1233 1233
           LOOP_L_N(i, ADC_KEY_NUM) {
1234
-            const uint16_t lo = pgm_read_word(&stADCKeyTable[i].ADCKeyValueMin),
1235
-                           hi = pgm_read_word(&stADCKeyTable[i].ADCKeyValueMax);
1234
+            const raw_adc_t lo = pgm_read_word(&stADCKeyTable[i].ADCKeyValueMin),
1235
+                            hi = pgm_read_word(&stADCKeyTable[i].ADCKeyValueMax);
1236 1236
             if (WITHIN(currentkpADCValue, lo, hi)) return pgm_read_byte(&stADCKeyTable[i].ADCKeyNo);
1237 1237
           }
1238 1238
       }

+ 63
- 63
Marlin/src/module/temperature.cpp ファイルの表示

@@ -437,8 +437,8 @@ PGMSTR(str_t_heating_failed, STR_T_HEATING_FAILED);
437 437
 #if HAS_HEATED_BED
438 438
   bed_info_t Temperature::temp_bed; // = { 0 }
439 439
   // Init min and max temp with extreme values to prevent false errors during startup
440
-  int16_t Temperature::mintemp_raw_BED = TEMP_SENSOR_BED_RAW_LO_TEMP,
441
-          Temperature::maxtemp_raw_BED = TEMP_SENSOR_BED_RAW_HI_TEMP;
440
+  raw_adc_t Temperature::mintemp_raw_BED = TEMP_SENSOR_BED_RAW_LO_TEMP,
441
+            Temperature::maxtemp_raw_BED = TEMP_SENSOR_BED_RAW_HI_TEMP;
442 442
   TERN_(WATCH_BED, bed_watch_t Temperature::watch_bed); // = { 0 }
443 443
   IF_DISABLED(PIDTEMPBED, millis_t Temperature::next_bed_check_ms);
444 444
 #endif
@@ -448,8 +448,8 @@ PGMSTR(str_t_heating_failed, STR_T_HEATING_FAILED);
448 448
   #if HAS_HEATED_CHAMBER
449 449
     millis_t next_cool_check_ms_2 = 0;
450 450
     celsius_float_t old_temp = 9999;
451
-    int16_t Temperature::mintemp_raw_CHAMBER = TEMP_SENSOR_CHAMBER_RAW_LO_TEMP,
452
-            Temperature::maxtemp_raw_CHAMBER = TEMP_SENSOR_CHAMBER_RAW_HI_TEMP;
451
+    raw_adc_t Temperature::mintemp_raw_CHAMBER = TEMP_SENSOR_CHAMBER_RAW_LO_TEMP,
452
+              Temperature::maxtemp_raw_CHAMBER = TEMP_SENSOR_CHAMBER_RAW_HI_TEMP;
453 453
     TERN_(WATCH_CHAMBER, chamber_watch_t Temperature::watch_chamber{0});
454 454
     IF_DISABLED(PIDTEMPCHAMBER, millis_t Temperature::next_chamber_check_ms);
455 455
   #endif
@@ -461,8 +461,8 @@ PGMSTR(str_t_heating_failed, STR_T_HEATING_FAILED);
461 461
     bool flag_cooler_state;
462 462
     //bool flag_cooler_excess = false;
463 463
     celsius_float_t previous_temp = 9999;
464
-    int16_t Temperature::mintemp_raw_COOLER = TEMP_SENSOR_COOLER_RAW_LO_TEMP,
465
-            Temperature::maxtemp_raw_COOLER = TEMP_SENSOR_COOLER_RAW_HI_TEMP;
464
+    raw_adc_t Temperature::mintemp_raw_COOLER = TEMP_SENSOR_COOLER_RAW_LO_TEMP,
465
+              Temperature::maxtemp_raw_COOLER = TEMP_SENSOR_COOLER_RAW_HI_TEMP;
466 466
     #if WATCH_COOLER
467 467
       cooler_watch_t Temperature::watch_cooler{0};
468 468
     #endif
@@ -477,8 +477,8 @@ PGMSTR(str_t_heating_failed, STR_T_HEATING_FAILED);
477 477
 #if HAS_TEMP_BOARD
478 478
   board_info_t Temperature::temp_board; // = { 0 }
479 479
   #if ENABLED(THERMAL_PROTECTION_BOARD)
480
-    int16_t Temperature::mintemp_raw_BOARD = TEMP_SENSOR_BOARD_RAW_LO_TEMP,
481
-            Temperature::maxtemp_raw_BOARD = TEMP_SENSOR_BOARD_RAW_HI_TEMP;
480
+    raw_adc_t Temperature::mintemp_raw_BOARD = TEMP_SENSOR_BOARD_RAW_LO_TEMP,
481
+              Temperature::maxtemp_raw_BOARD = TEMP_SENSOR_BOARD_RAW_HI_TEMP;
482 482
   #endif
483 483
 #endif
484 484
 
@@ -508,6 +508,7 @@ volatile bool Temperature::raw_temps_ready = false;
508 508
 #endif
509 509
 
510 510
 #define TEMPDIR(N) ((TEMP_SENSOR_##N##_RAW_LO_TEMP) < (TEMP_SENSOR_##N##_RAW_HI_TEMP) ? 1 : -1)
511
+#define TP_CMP(S,A,B) (TEMPDIR(S) < 0 ? ((A)<(B)) : ((A)>(B)))
511 512
 
512 513
 #if HAS_HOTEND
513 514
   // Init mintemp and maxtemp with extreme values to prevent false errors during startup
@@ -1689,8 +1690,8 @@ void Temperature::manage_heater() {
1689 1690
     m = (l + r) >> 1;                                                     \
1690 1691
     if (!m) return celsius_t(pgm_read_word(&TBL[0].celsius));             \
1691 1692
     if (m == l || m == r) return celsius_t(pgm_read_word(&TBL[LEN-1].celsius)); \
1692
-    int16_t v00 = pgm_read_word(&TBL[m-1].value),                         \
1693
-            v10 = pgm_read_word(&TBL[m-0].value);                         \
1693
+    raw_adc_t v00 = pgm_read_word(&TBL[m-1].value),                       \
1694
+              v10 = pgm_read_word(&TBL[m-0].value);                       \
1694 1695
          if (raw < v00) r = m;                                            \
1695 1696
     else if (raw > v10) l = m;                                            \
1696 1697
     else {                                                                \
@@ -1784,7 +1785,7 @@ void Temperature::manage_heater() {
1784 1785
     SERIAL_EOL();
1785 1786
   }
1786 1787
 
1787
-  celsius_float_t Temperature::user_thermistor_to_deg_c(const uint8_t t_index, const int16_t raw) {
1788
+  celsius_float_t Temperature::user_thermistor_to_deg_c(const uint8_t t_index, const raw_adc_t raw) {
1788 1789
 
1789 1790
     if (!WITHIN(t_index, 0, COUNT(user_thermistor) - 1)) return 25;
1790 1791
 
@@ -1799,8 +1800,8 @@ void Temperature::manage_heater() {
1799 1800
     }
1800 1801
 
1801 1802
     // maximum adc value .. take into account the over sampling
1802
-    const int adc_max = MAX_RAW_THERMISTOR_VALUE,
1803
-              adc_raw = constrain(raw, 1, adc_max - 1); // constrain to prevent divide-by-zero
1803
+    constexpr raw_adc_t adc_max = MAX_RAW_THERMISTOR_VALUE;
1804
+    const raw_adc_t adc_raw = constrain(raw, 1, adc_max - 1); // constrain to prevent divide-by-zero
1804 1805
 
1805 1806
     const float adc_inverse = (adc_max - adc_raw) - 0.5f,
1806 1807
                 resistance = t.series_res * (adc_raw + 0.5f) / adc_inverse,
@@ -1820,7 +1821,7 @@ void Temperature::manage_heater() {
1820 1821
 #if HAS_HOTEND
1821 1822
   // Derived from RepRap FiveD extruder::getTemperature()
1822 1823
   // For hot end temperature measurement.
1823
-  celsius_float_t Temperature::analog_to_celsius_hotend(const int16_t raw, const uint8_t e) {
1824
+  celsius_float_t Temperature::analog_to_celsius_hotend(const raw_adc_t raw, const uint8_t e) {
1824 1825
     if (e >= HOTENDS) {
1825 1826
       SERIAL_ERROR_START();
1826 1827
       SERIAL_ECHO(e);
@@ -1836,11 +1837,11 @@ void Temperature::manage_heater() {
1836 1837
         #elif TEMP_SENSOR_0_IS_MAX_TC
1837 1838
           #if TEMP_SENSOR_0_IS_MAX31865
1838 1839
             return TERN(LIB_INTERNAL_MAX31865,
1839
-              max31865_0.temperature((uint16_t)raw),
1840
+              max31865_0.temperature(raw),
1840 1841
               max31865_0.temperature(MAX31865_SENSOR_OHMS_0, MAX31865_CALIBRATION_OHMS_0)
1841 1842
             );
1842 1843
           #else
1843
-            return raw * 0.25;
1844
+            return (int16_t)raw * 0.25;
1844 1845
           #endif
1845 1846
         #elif TEMP_SENSOR_0_IS_AD595
1846 1847
           return TEMP_AD595(raw);
@@ -1855,11 +1856,11 @@ void Temperature::manage_heater() {
1855 1856
         #elif TEMP_SENSOR_1_IS_MAX_TC
1856 1857
           #if TEMP_SENSOR_0_IS_MAX31865
1857 1858
             return TERN(LIB_INTERNAL_MAX31865,
1858
-              max31865_1.temperature((uint16_t)raw),
1859
+              max31865_1.temperature(raw),
1859 1860
               max31865_1.temperature(MAX31865_SENSOR_OHMS_1, MAX31865_CALIBRATION_OHMS_1)
1860 1861
             );
1861 1862
           #else
1862
-            return raw * 0.25;
1863
+            return (int16_t)raw * 0.25;
1863 1864
           #endif
1864 1865
         #elif TEMP_SENSOR_1_IS_AD595
1865 1866
           return TEMP_AD595(raw);
@@ -1943,7 +1944,7 @@ void Temperature::manage_heater() {
1943 1944
 
1944 1945
 #if HAS_HEATED_BED
1945 1946
   // For bed temperature measurement.
1946
-  celsius_float_t Temperature::analog_to_celsius_bed(const int16_t raw) {
1947
+  celsius_float_t Temperature::analog_to_celsius_bed(const raw_adc_t raw) {
1947 1948
     #if TEMP_SENSOR_BED_IS_CUSTOM
1948 1949
       return user_thermistor_to_deg_c(CTI_BED, raw);
1949 1950
     #elif TEMP_SENSOR_BED_IS_THERMISTOR
@@ -1961,7 +1962,7 @@ void Temperature::manage_heater() {
1961 1962
 
1962 1963
 #if HAS_TEMP_CHAMBER
1963 1964
   // For chamber temperature measurement.
1964
-  celsius_float_t Temperature::analog_to_celsius_chamber(const int16_t raw) {
1965
+  celsius_float_t Temperature::analog_to_celsius_chamber(const raw_adc_t raw) {
1965 1966
     #if TEMP_SENSOR_CHAMBER_IS_CUSTOM
1966 1967
       return user_thermistor_to_deg_c(CTI_CHAMBER, raw);
1967 1968
     #elif TEMP_SENSOR_CHAMBER_IS_THERMISTOR
@@ -1979,7 +1980,7 @@ void Temperature::manage_heater() {
1979 1980
 
1980 1981
 #if HAS_TEMP_COOLER
1981 1982
   // For cooler temperature measurement.
1982
-  celsius_float_t Temperature::analog_to_celsius_cooler(const int16_t raw) {
1983
+  celsius_float_t Temperature::analog_to_celsius_cooler(const raw_adc_t raw) {
1983 1984
     #if TEMP_SENSOR_COOLER_IS_CUSTOM
1984 1985
       return user_thermistor_to_deg_c(CTI_COOLER, raw);
1985 1986
     #elif TEMP_SENSOR_COOLER_IS_THERMISTOR
@@ -1997,7 +1998,7 @@ void Temperature::manage_heater() {
1997 1998
 
1998 1999
 #if HAS_TEMP_PROBE
1999 2000
   // For probe temperature measurement.
2000
-  celsius_float_t Temperature::analog_to_celsius_probe(const int16_t raw) {
2001
+  celsius_float_t Temperature::analog_to_celsius_probe(const raw_adc_t raw) {
2001 2002
     #if TEMP_SENSOR_PROBE_IS_CUSTOM
2002 2003
       return user_thermistor_to_deg_c(CTI_PROBE, raw);
2003 2004
     #elif TEMP_SENSOR_PROBE_IS_THERMISTOR
@@ -2015,7 +2016,7 @@ void Temperature::manage_heater() {
2015 2016
 
2016 2017
 #if HAS_TEMP_BOARD
2017 2018
   // For motherboard temperature measurement.
2018
-  celsius_float_t Temperature::analog_to_celsius_board(const int16_t raw) {
2019
+  celsius_float_t Temperature::analog_to_celsius_board(const raw_adc_t raw) {
2019 2020
     #if TEMP_SENSOR_BOARD_IS_CUSTOM
2020 2021
       return user_thermistor_to_deg_c(CTI_BOARD, raw);
2021 2022
     #elif TEMP_SENSOR_BOARD_IS_THERMISTOR
@@ -2033,13 +2034,13 @@ void Temperature::manage_heater() {
2033 2034
 
2034 2035
 #if HAS_TEMP_REDUNDANT
2035 2036
   // For redundant temperature measurement.
2036
-  celsius_float_t Temperature::analog_to_celsius_redundant(const int16_t raw) {
2037
+  celsius_float_t Temperature::analog_to_celsius_redundant(const raw_adc_t raw) {
2037 2038
     #if TEMP_SENSOR_REDUNDANT_IS_CUSTOM
2038 2039
       return user_thermistor_to_deg_c(CTI_REDUNDANT, raw);
2039 2040
     #elif TEMP_SENSOR_REDUNDANT_IS_MAX_TC && REDUNDANT_TEMP_MATCH(SOURCE, E0)
2040
-      return TERN(TEMP_SENSOR_REDUNDANT_IS_MAX31865, max31865_0.temperature((uint16_t)raw), raw * 0.25);
2041
+      return TERN(TEMP_SENSOR_REDUNDANT_IS_MAX31865, max31865_0.temperature(raw), (int16_t)raw * 0.25);
2041 2042
     #elif TEMP_SENSOR_REDUNDANT_IS_MAX_TC && REDUNDANT_TEMP_MATCH(SOURCE, E1)
2042
-      return TERN(TEMP_SENSOR_REDUNDANT_IS_MAX31865, max31865_1.temperature((uint16_t)raw), raw * 0.25);
2043
+      return TERN(TEMP_SENSOR_REDUNDANT_IS_MAX31865, max31865_1.temperature(raw), (int16_t)raw * 0.25);
2043 2044
     #elif TEMP_SENSOR_REDUNDANT_IS_THERMISTOR
2044 2045
       SCAN_THERMISTOR_TABLE(TEMPTABLE_REDUNDANT, TEMPTABLE_REDUNDANT_LEN);
2045 2046
     #elif TEMP_SENSOR_REDUNDANT_IS_AD595
@@ -2069,20 +2070,20 @@ void Temperature::updateTemperaturesFromRawValues() {
2069 2070
 
2070 2071
   watchdog_refresh(); // Reset because raw_temps_ready was set by the interrupt
2071 2072
 
2072
-  TERN_(TEMP_SENSOR_0_IS_MAX_TC, temp_hotend[0].raw = READ_MAX_TC(0));
2073
-  TERN_(TEMP_SENSOR_1_IS_MAX_TC, temp_hotend[1].raw = READ_MAX_TC(1));
2074
-  TERN_(TEMP_SENSOR_REDUNDANT_IS_MAX_TC, temp_redundant.raw = READ_MAX_TC(HEATER_ID(TEMP_SENSOR_REDUNDANT_SOURCE)));
2073
+  TERN_(TEMP_SENSOR_0_IS_MAX_TC, temp_hotend[0].setraw(READ_MAX_TC(0)));
2074
+  TERN_(TEMP_SENSOR_1_IS_MAX_TC, temp_hotend[1].setraw(READ_MAX_TC(1)));
2075
+  TERN_(TEMP_SENSOR_REDUNDANT_IS_MAX_TC, temp_redundant.setraw(READ_MAX_TC(HEATER_ID(TEMP_SENSOR_REDUNDANT_SOURCE))));
2075 2076
 
2076 2077
   #if HAS_HOTEND
2077
-    HOTEND_LOOP() temp_hotend[e].celsius = analog_to_celsius_hotend(temp_hotend[e].raw, e);
2078
+    HOTEND_LOOP() temp_hotend[e].celsius = analog_to_celsius_hotend(temp_hotend[e].getraw(), e);
2078 2079
   #endif
2079 2080
 
2080
-  TERN_(HAS_HEATED_BED,     temp_bed.celsius       = analog_to_celsius_bed(temp_bed.raw));
2081
-  TERN_(HAS_TEMP_CHAMBER,   temp_chamber.celsius   = analog_to_celsius_chamber(temp_chamber.raw));
2082
-  TERN_(HAS_TEMP_COOLER,    temp_cooler.celsius    = analog_to_celsius_cooler(temp_cooler.raw));
2083
-  TERN_(HAS_TEMP_PROBE,     temp_probe.celsius     = analog_to_celsius_probe(temp_probe.raw));
2084
-  TERN_(HAS_TEMP_BOARD,     temp_board.celsius     = analog_to_celsius_board(temp_board.raw));
2085
-  TERN_(HAS_TEMP_REDUNDANT, temp_redundant.celsius = analog_to_celsius_redundant(temp_redundant.raw));
2081
+  TERN_(HAS_HEATED_BED,     temp_bed.celsius       = analog_to_celsius_bed(temp_bed.getraw()));
2082
+  TERN_(HAS_TEMP_CHAMBER,   temp_chamber.celsius   = analog_to_celsius_chamber(temp_chamber.getraw()));
2083
+  TERN_(HAS_TEMP_COOLER,    temp_cooler.celsius    = analog_to_celsius_cooler(temp_cooler.getraw()));
2084
+  TERN_(HAS_TEMP_PROBE,     temp_probe.celsius     = analog_to_celsius_probe(temp_probe.getraw()));
2085
+  TERN_(HAS_TEMP_BOARD,     temp_board.celsius     = analog_to_celsius_board(temp_board.getraw()));
2086
+  TERN_(HAS_TEMP_REDUNDANT, temp_redundant.celsius = analog_to_celsius_redundant(temp_redundant.getraw()));
2086 2087
 
2087 2088
   TERN_(FILAMENT_WIDTH_SENSOR, filwidth.update_measured_mm());
2088 2089
   TERN_(HAS_POWER_MONITOR,     power_monitor.capture_values());
@@ -2108,46 +2109,45 @@ void Temperature::updateTemperaturesFromRawValues() {
2108 2109
     };
2109 2110
 
2110 2111
     LOOP_L_N(e, COUNT(temp_dir)) {
2111
-      const int8_t tdir = temp_dir[e];
2112
-      if (tdir) {
2113
-        const int16_t rawtemp = temp_hotend[e].raw * tdir; // normal direction, +rawtemp, else -rawtemp
2114
-        if (rawtemp > temp_range[e].raw_max * tdir) max_temp_error((heater_id_t)e);
2115
-
2116
-        const bool heater_on = temp_hotend[e].target > 0;
2117
-        if (heater_on && rawtemp < temp_range[e].raw_min * tdir && !is_preheating(e)) {
2118
-          #if MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED > 1
2119
-            if (++consecutive_low_temperature_error[e] >= MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED)
2120
-          #endif
2121
-              min_temp_error((heater_id_t)e);
2122
-        }
2112
+      const raw_adc_t r = temp_hotend[e].getraw();
2113
+      const bool neg = temp_dir[e] < 0, pos = temp_dir[e] > 0;
2114
+      if ((neg && r < temp_range[e].raw_max) || (pos && r > temp_range[e].raw_max))
2115
+        max_temp_error((heater_id_t)e);
2116
+
2117
+      const bool heater_on = temp_hotend[e].target > 0;
2118
+      if (heater_on && ((neg && r > temp_range[e].raw_min) || (pos && r < temp_range[e].raw_min))) {
2123 2119
         #if MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED > 1
2124
-          else
2125
-            consecutive_low_temperature_error[e] = 0;
2120
+          if (++consecutive_low_temperature_error[e] >= MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED)
2126 2121
         #endif
2122
+            min_temp_error((heater_id_t)e);
2127 2123
       }
2124
+      #if MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED > 1
2125
+        else
2126
+          consecutive_low_temperature_error[e] = 0;
2127
+      #endif
2128 2128
     }
2129 2129
 
2130 2130
   #endif // HAS_HOTEND
2131 2131
 
2132 2132
   #define TP_CMP(S,A,B) (TEMPDIR(S) < 0 ? ((A)<(B)) : ((A)>(B)))
2133 2133
   #if ENABLED(THERMAL_PROTECTION_BED)
2134
-    if (TP_CMP(BED, temp_bed.raw, maxtemp_raw_BED)) max_temp_error(H_BED);
2135
-    if (temp_bed.target > 0 && TP_CMP(BED, mintemp_raw_BED, temp_bed.raw)) min_temp_error(H_BED);
2134
+    if (TP_CMP(BED, temp_bed.getraw(), maxtemp_raw_BED)) max_temp_error(H_BED);
2135
+    if (temp_bed.target > 0 && TP_CMP(BED, mintemp_raw_BED, temp_bed.getraw())) min_temp_error(H_BED);
2136 2136
   #endif
2137 2137
 
2138 2138
   #if BOTH(HAS_HEATED_CHAMBER, THERMAL_PROTECTION_CHAMBER)
2139
-    if (TP_CMP(CHAMBER, temp_chamber.raw, maxtemp_raw_CHAMBER)) max_temp_error(H_CHAMBER);
2140
-    if (temp_chamber.target > 0 && TP_CMP(CHAMBER, mintemp_raw_CHAMBER, temp_chamber.raw)) min_temp_error(H_CHAMBER);
2139
+    if (TP_CMP(CHAMBER, temp_chamber.getraw(), maxtemp_raw_CHAMBER)) max_temp_error(H_CHAMBER);
2140
+    if (temp_chamber.target > 0 && TP_CMP(CHAMBER, mintemp_raw_CHAMBER, temp_chamber.getraw())) min_temp_error(H_CHAMBER);
2141 2141
   #endif
2142 2142
 
2143 2143
   #if BOTH(HAS_COOLER, THERMAL_PROTECTION_COOLER)
2144
-    if (cutter.unitPower > 0 && TP_CMP(COOLER, temp_cooler.raw, maxtemp_raw_COOLER)) max_temp_error(H_COOLER);
2145
-    if (TP_CMP(COOLER, mintemp_raw_COOLER, temp_cooler.raw)) min_temp_error(H_COOLER);
2144
+    if (cutter.unitPower > 0 && TP_CMP(COOLER, temp_cooler.getraw(), maxtemp_raw_COOLER)) max_temp_error(H_COOLER);
2145
+    if (TP_CMP(COOLER, mintemp_raw_COOLER, temp_cooler.getraw())) min_temp_error(H_COOLER);
2146 2146
   #endif
2147 2147
 
2148 2148
   #if BOTH(HAS_TEMP_BOARD, THERMAL_PROTECTION_BOARD)
2149
-    if (TP_CMP(BOARD, temp_board.raw, maxtemp_raw_BOARD)) max_temp_error(H_BOARD);
2150
-    if (TP_CMP(BOARD, mintemp_raw_BOARD, temp_board.raw)) min_temp_error(H_BOARD);
2149
+    if (TP_CMP(BOARD, temp_board.getraw(), maxtemp_raw_BOARD)) max_temp_error(H_BOARD);
2150
+    if (TP_CMP(BOARD, mintemp_raw_BOARD, temp_board.getraw())) min_temp_error(H_BOARD);
2151 2151
   #endif
2152 2152
   #undef TP_CMP
2153 2153
 
@@ -2731,7 +2731,7 @@ void Temperature::disable_all_heaters() {
2731 2731
    * @param  hindex  the hotend we're referencing (if MULTI_MAX_TC)
2732 2732
    * @return         integer representing the board's buffer, to be converted later if needed
2733 2733
    */
2734
-  int16_t Temperature::read_max_tc(TERN_(HAS_MULTI_MAX_TC, const uint8_t hindex/*=0*/)) {
2734
+  raw_adc_t Temperature::read_max_tc(TERN_(HAS_MULTI_MAX_TC, const uint8_t hindex/*=0*/)) {
2735 2735
     #define MAXTC_HEAT_INTERVAL 250UL
2736 2736
 
2737 2737
     #if HAS_MAX31855
@@ -2750,7 +2750,7 @@ void Temperature::disable_all_heaters() {
2750 2750
 
2751 2751
     #if HAS_MULTI_MAX_TC
2752 2752
       // Needed to return the correct temp when this is called between readings
2753
-      static int16_t max_tc_temp_previous[MAX_TC_COUNT] = { 0 };
2753
+      static raw_adc_t max_tc_temp_previous[MAX_TC_COUNT] = { 0 };
2754 2754
       #define THERMO_TEMP(I) max_tc_temp_previous[I]
2755 2755
       #define THERMO_SEL(A,B) (hindex ? (B) : (A))
2756 2756
       #define MAXTC_CS_WRITE(V) do{ switch (hindex) { case 1: WRITE(TEMP_1_CS_PIN, V); break; default: WRITE(TEMP_0_CS_PIN, V); } }while(0)
@@ -2779,7 +2779,7 @@ void Temperature::disable_all_heaters() {
2779 2779
     // Return last-read value between readings
2780 2780
     millis_t ms = millis();
2781 2781
     if (PENDING(ms, next_max_tc_ms[hindex]))
2782
-      return (int16_t)THERMO_TEMP(hindex);
2782
+      return THERMO_TEMP(hindex);
2783 2783
 
2784 2784
     next_max_tc_ms[hindex] = ms + MAXTC_HEAT_INTERVAL;
2785 2785
 
@@ -2876,7 +2876,7 @@ void Temperature::disable_all_heaters() {
2876 2876
 
2877 2877
     THERMO_TEMP(hindex) = max_tc_temp;
2878 2878
 
2879
-    return (int16_t)max_tc_temp;
2879
+    return max_tc_temp;
2880 2880
   }
2881 2881
 
2882 2882
 #endif // HAS_MAX_TC
@@ -3017,7 +3017,7 @@ void Temperature::isr() {
3017 3017
   uint8_t pwm_count_tmp = pwm_count;
3018 3018
 
3019 3019
   #if HAS_ADC_BUTTONS
3020
-    static unsigned int raw_ADCKey_value = 0;
3020
+    static raw_adc_t raw_ADCKey_value = 0;
3021 3021
     static bool ADCKey_pressed = false;
3022 3022
   #endif
3023 3023
 

+ 29
- 28
Marlin/src/module/temperature.h ファイルの表示

@@ -192,12 +192,16 @@ enum ADCSensorState : char {
192 192
 
193 193
 // A temperature sensor
194 194
 typedef struct TempInfo {
195
-  uint16_t acc;
196
-  int16_t raw;
195
+private:
196
+  raw_adc_t acc;
197
+  raw_adc_t raw;
198
+public:
197 199
   celsius_float_t celsius;
198 200
   inline void reset() { acc = 0; }
199
-  inline void sample(const uint16_t s) { acc += s; }
201
+  inline void sample(const raw_adc_t s) { acc += s; }
200 202
   inline void update() { raw = acc; }
203
+  void setraw(const raw_adc_t r) { raw = r; }
204
+  raw_adc_t getraw() { return raw; }
201 205
 } temp_info_t;
202 206
 
203 207
 #if HAS_TEMP_REDUNDANT
@@ -287,9 +291,7 @@ struct HeaterWatch {
287 291
 #endif
288 292
 
289 293
 // Temperature sensor read value ranges
290
-typedef struct { int16_t raw_min, raw_max; } raw_range_t;
291
-typedef struct { celsius_t mintemp, maxtemp; } celsius_range_t;
292
-typedef struct { int16_t raw_min, raw_max; celsius_t mintemp, maxtemp; } temp_range_t;
294
+typedef struct { raw_adc_t raw_min, raw_max; celsius_t mintemp, maxtemp; } temp_range_t;
293 295
 
294 296
 #define THERMISTOR_ABS_ZERO_C           -273.15f  // bbbbrrrrr cold !
295 297
 #define THERMISTOR_RESISTANCE_NOMINAL_C 25.0f     // mmmmm comfortable
@@ -492,7 +494,7 @@ class Temperature {
492 494
         static bed_watch_t watch_bed;
493 495
       #endif
494 496
       IF_DISABLED(PIDTEMPBED, static millis_t next_bed_check_ms);
495
-      static int16_t mintemp_raw_BED, maxtemp_raw_BED;
497
+      static raw_adc_t mintemp_raw_BED, maxtemp_raw_BED;
496 498
     #endif
497 499
 
498 500
     #if HAS_HEATED_CHAMBER
@@ -500,7 +502,7 @@ class Temperature {
500 502
         static chamber_watch_t watch_chamber;
501 503
       #endif
502 504
       TERN(PIDTEMPCHAMBER,,static millis_t next_chamber_check_ms);
503
-      static int16_t mintemp_raw_CHAMBER, maxtemp_raw_CHAMBER;
505
+      static raw_adc_t mintemp_raw_CHAMBER, maxtemp_raw_CHAMBER;
504 506
     #endif
505 507
 
506 508
     #if HAS_COOLER
@@ -508,11 +510,11 @@ class Temperature {
508 510
         static cooler_watch_t watch_cooler;
509 511
       #endif
510 512
       static millis_t next_cooler_check_ms, cooler_fan_flush_ms;
511
-      static int16_t mintemp_raw_COOLER, maxtemp_raw_COOLER;
513
+      static raw_adc_t mintemp_raw_COOLER, maxtemp_raw_COOLER;
512 514
     #endif
513 515
 
514 516
     #if HAS_TEMP_BOARD && ENABLED(THERMAL_PROTECTION_BOARD)
515
-      static int16_t mintemp_raw_BOARD, maxtemp_raw_BOARD;
517
+      static raw_adc_t mintemp_raw_BOARD, maxtemp_raw_BOARD;
516 518
     #endif
517 519
 
518 520
     #if MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED > 1
@@ -566,7 +568,7 @@ class Temperature {
566 568
       static user_thermistor_t user_thermistor[USER_THERMISTORS];
567 569
       static void M305_report(const uint8_t t_index, const bool forReplay=true);
568 570
       static void reset_user_thermistors();
569
-      static celsius_float_t user_thermistor_to_deg_c(const uint8_t t_index, const int16_t raw);
571
+      static celsius_float_t user_thermistor_to_deg_c(const uint8_t t_index, const raw_adc_t raw);
570 572
       static bool set_pull_up_res(int8_t t_index, float value) {
571 573
         //if (!WITHIN(t_index, 0, USER_THERMISTORS - 1)) return false;
572 574
         if (!WITHIN(value, 1, 1000000)) return false;
@@ -594,25 +596,25 @@ class Temperature {
594 596
     #endif
595 597
 
596 598
     #if HAS_HOTEND
597
-      static celsius_float_t analog_to_celsius_hotend(const int16_t raw, const uint8_t e);
599
+      static celsius_float_t analog_to_celsius_hotend(const raw_adc_t raw, const uint8_t e);
598 600
     #endif
599 601
     #if HAS_HEATED_BED
600
-      static celsius_float_t analog_to_celsius_bed(const int16_t raw);
602
+      static celsius_float_t analog_to_celsius_bed(const raw_adc_t raw);
601 603
     #endif
602 604
     #if HAS_TEMP_CHAMBER
603
-      static celsius_float_t analog_to_celsius_chamber(const int16_t raw);
605
+      static celsius_float_t analog_to_celsius_chamber(const raw_adc_t raw);
604 606
     #endif
605 607
     #if HAS_TEMP_PROBE
606
-      static celsius_float_t analog_to_celsius_probe(const int16_t raw);
608
+      static celsius_float_t analog_to_celsius_probe(const raw_adc_t raw);
607 609
     #endif
608 610
     #if HAS_TEMP_COOLER
609
-      static celsius_float_t analog_to_celsius_cooler(const int16_t raw);
611
+      static celsius_float_t analog_to_celsius_cooler(const raw_adc_t raw);
610 612
     #endif
611 613
     #if HAS_TEMP_BOARD
612
-      static celsius_float_t analog_to_celsius_board(const int16_t raw);
614
+      static celsius_float_t analog_to_celsius_board(const raw_adc_t raw);
613 615
     #endif
614 616
     #if HAS_TEMP_REDUNDANT
615
-      static celsius_float_t analog_to_celsius_redundant(const int16_t raw);
617
+      static celsius_float_t analog_to_celsius_redundant(const raw_adc_t raw);
616 618
     #endif
617 619
 
618 620
     #if HAS_FAN
@@ -707,8 +709,8 @@ class Temperature {
707 709
     }
708 710
 
709 711
     #if ENABLED(SHOW_TEMP_ADC_VALUES)
710
-      static int16_t rawHotendTemp(const uint8_t E_NAME) {
711
-        return TERN0(HAS_HOTEND, temp_hotend[HOTEND_INDEX].raw);
712
+      static raw_adc_t rawHotendTemp(const uint8_t E_NAME) {
713
+        return TERN0(HAS_HOTEND, temp_hotend[HOTEND_INDEX].getraw());
712 714
       }
713 715
     #endif
714 716
 
@@ -770,7 +772,7 @@ class Temperature {
770 772
     #if HAS_HEATED_BED
771 773
 
772 774
       #if ENABLED(SHOW_TEMP_ADC_VALUES)
773
-        static int16_t rawBedTemp()    { return temp_bed.raw; }
775
+        static raw_adc_t rawBedTemp()  { return temp_bed.getraw(); }
774 776
       #endif
775 777
       static celsius_float_t degBed()  { return temp_bed.celsius; }
776 778
       static celsius_t wholeDegBed()   { return static_cast<celsius_t>(degBed() + 0.5f); }
@@ -801,7 +803,7 @@ class Temperature {
801 803
 
802 804
     #if HAS_TEMP_PROBE
803 805
       #if ENABLED(SHOW_TEMP_ADC_VALUES)
804
-        static int16_t rawProbeTemp()    { return temp_probe.raw; }
806
+        static raw_adc_t rawProbeTemp()  { return temp_probe.getraw(); }
805 807
       #endif
806 808
       static celsius_float_t degProbe()  { return temp_probe.celsius; }
807 809
       static celsius_t wholeDegProbe()   { return static_cast<celsius_t>(degProbe() + 0.5f); }
@@ -812,7 +814,7 @@ class Temperature {
812 814
 
813 815
     #if HAS_TEMP_CHAMBER
814 816
       #if ENABLED(SHOW_TEMP_ADC_VALUES)
815
-        static int16_t rawChamberTemp()      { return temp_chamber.raw; }
817
+        static raw_adc_t rawChamberTemp()    { return temp_chamber.getraw(); }
816 818
       #endif
817 819
       static celsius_float_t degChamber()    { return temp_chamber.celsius; }
818 820
       static celsius_t wholeDegChamber()     { return static_cast<celsius_t>(degChamber() + 0.5f); }
@@ -835,7 +837,7 @@ class Temperature {
835 837
 
836 838
     #if HAS_TEMP_COOLER
837 839
       #if ENABLED(SHOW_TEMP_ADC_VALUES)
838
-        static int16_t rawCoolerTemp()     { return temp_cooler.raw; }
840
+        static raw_adc_t rawCoolerTemp()   { return temp_cooler.getraw(); }
839 841
       #endif
840 842
       static celsius_float_t degCooler()   { return temp_cooler.celsius; }
841 843
       static celsius_t wholeDegCooler()    { return static_cast<celsius_t>(temp_cooler.celsius + 0.5f); }
@@ -849,7 +851,7 @@ class Temperature {
849 851
 
850 852
     #if HAS_TEMP_BOARD
851 853
       #if ENABLED(SHOW_TEMP_ADC_VALUES)
852
-        static int16_t rawBoardTemp()    { return temp_board.raw; }
854
+        static raw_adc_t rawBoardTemp()  { return temp_board.getraw(); }
853 855
       #endif
854 856
       static celsius_float_t degBoard()  { return temp_board.celsius; }
855 857
       static celsius_t wholeDegBoard()   { return static_cast<celsius_t>(temp_board.celsius + 0.5f); }
@@ -857,8 +859,7 @@ class Temperature {
857 859
 
858 860
     #if HAS_TEMP_REDUNDANT
859 861
       #if ENABLED(SHOW_TEMP_ADC_VALUES)
860
-        static int16_t rawRedundantTemp()         { return temp_redundant.raw; }
861
-        static int16_t rawRedundanTargetTemp()    { return (*temp_redundant.target).raw; }
862
+        static raw_adc_t rawRedundantTemp()       { return temp_redundant.getraw(); }
862 863
       #endif
863 864
       static celsius_float_t degRedundant()       { return temp_redundant.celsius; }
864 865
       static celsius_float_t degRedundantTarget() { return (*temp_redundant.target).celsius; }
@@ -991,7 +992,7 @@ class Temperature {
991 992
       #else
992 993
         #define READ_MAX_TC(N) read_max_tc()
993 994
       #endif
994
-      static int16_t read_max_tc(TERN_(HAS_MULTI_MAX_TC, const uint8_t hindex=0));
995
+      static raw_adc_t read_max_tc(TERN_(HAS_MULTI_MAX_TC, const uint8_t hindex=0));
995 996
     #endif
996 997
 
997 998
     #if HAS_AUTO_FAN

+ 7
- 9
Marlin/src/module/thermistor/thermistors.h ファイルの表示

@@ -27,22 +27,20 @@
27 27
 #define THERMISTOR_TABLE_SCALE (HAL_ADC_RANGE / _BV(THERMISTOR_TABLE_ADC_RESOLUTION))
28 28
 #if ENABLED(HAL_ADC_FILTERED)
29 29
   #define OVERSAMPLENR 1
30
-#elif HAL_ADC_RESOLUTION > 10
31
-  #define OVERSAMPLENR (20 - HAL_ADC_RESOLUTION)
32 30
 #else
33 31
   #define OVERSAMPLENR 16
34 32
 #endif
35
-#define MAX_RAW_THERMISTOR_VALUE (HAL_ADC_RANGE * (OVERSAMPLENR) - 1)
36 33
 
37
-// Currently Marlin stores all oversampled ADC values as int16_t, make sure the HAL settings do not overflow 15bit
38
-#if MAX_RAW_THERMISTOR_VALUE > ((1 << 15) - 1)
39
-  #error "MAX_RAW_THERMISTOR_VALUE is too large for int16_t. Reduce OVERSAMPLENR or HAL_ADC_RESOLUTION."
34
+// Currently Marlin stores all oversampled ADC values as uint16_t, make sure the HAL settings do not overflow 16 bit
35
+#if (HAL_ADC_RANGE) * (OVERSAMPLENR) > 1 << 16
36
+  #error "MAX_RAW_THERMISTOR_VALUE is too large for uint16_t. Reduce OVERSAMPLENR or HAL_ADC_RESOLUTION."
40 37
 #endif
38
+#define MAX_RAW_THERMISTOR_VALUE (uint16_t(HAL_ADC_RANGE) * (OVERSAMPLENR) - 1)
41 39
 
42
-#define OV_SCALE(N) (N)
43
-#define OV(N) int16_t(OV_SCALE(N) * (OVERSAMPLENR) * (THERMISTOR_TABLE_SCALE))
40
+#define OV_SCALE(N) float(N)
41
+#define OV(N) raw_adc_t(OV_SCALE(N) * (OVERSAMPLENR) * (THERMISTOR_TABLE_SCALE))
44 42
 
45
-typedef struct { int16_t value; celsius_t celsius; } temp_entry_t;
43
+typedef struct { raw_adc_t value; celsius_t celsius; } temp_entry_t;
46 44
 
47 45
 // Pt1000 and Pt100 handling
48 46
 //

読み込み中…
キャンセル
保存