Browse Source

⚡️ Optimize Sensitive Pins array (except STM32) (#22080)

Scott Lahteine 4 years ago
parent
commit
77496c8235
No account linked to committer's email address

+ 1
- 1
Marlin/src/HAL/AVR/HAL.h View File

186
 #define GET_PIN_MAP_INDEX(pin) pin
186
 #define GET_PIN_MAP_INDEX(pin) pin
187
 #define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval)
187
 #define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval)
188
 
188
 
189
-#define HAL_SENSITIVE_PINS 0, 1
189
+#define HAL_SENSITIVE_PINS 0, 1,
190
 
190
 
191
 #ifdef __AVR_AT90USB1286__
191
 #ifdef __AVR_AT90USB1286__
192
   #define JTAG_DISABLE() do{ MCUCR = 0x80; MCUCR = 0x80; }while(0)
192
   #define JTAG_DISABLE() do{ MCUCR = 0x80; MCUCR = 0x80; }while(0)

+ 0
- 37
Marlin/src/HAL/LINUX/include/pinmapping.cpp View File

25
 
25
 
26
 #include "../../../gcode/parser.h"
26
 #include "../../../gcode/parser.h"
27
 
27
 
28
-uint8_t analog_offset = NUM_DIGITAL_PINS - NUM_ANALOG_INPUTS;
29
-
30
-// Get the digital pin for an analog index
31
-pin_t analogInputToDigitalPin(const int8_t p) {
32
-  return (WITHIN(p, 0, NUM_ANALOG_INPUTS) ? analog_offset + p : P_NC);
33
-}
34
-
35
-// Return the index of a pin number
36
-int16_t GET_PIN_MAP_INDEX(const pin_t pin) {
37
-  return pin;
38
-}
39
-
40
-// Test whether the pin is valid
41
-bool VALID_PIN(const pin_t p) {
42
-  return WITHIN(p, 0, NUM_DIGITAL_PINS);
43
-}
44
-
45
-// Get the analog index for a digital pin
46
-int8_t DIGITAL_PIN_TO_ANALOG_PIN(const pin_t p) {
47
-  return (WITHIN(p, analog_offset, NUM_DIGITAL_PINS) ? p - analog_offset : P_NC);
48
-}
49
-
50
-// Test whether the pin is PWM
51
-bool PWM_PIN(const pin_t p) {
52
-  return false;
53
-}
54
-
55
-// Test whether the pin is interruptable
56
-bool INTERRUPT_PIN(const pin_t p) {
57
-  return false;
58
-}
59
-
60
-// Get the pin number at the given index
61
-pin_t GET_PIN_MAP_PIN(const int16_t ind) {
62
-  return ind;
63
-}
64
-
65
 int16_t PARSED_PIN_INDEX(const char code, const int16_t dval) {
28
 int16_t PARSED_PIN_INDEX(const char code, const int16_t dval) {
66
   return parser.intval(code, dval);
29
   return parser.intval(code, dval);
67
 }
30
 }

+ 15
- 9
Marlin/src/HAL/LINUX/include/pinmapping.h View File

34
 
34
 
35
 #define HAL_SENSITIVE_PINS
35
 #define HAL_SENSITIVE_PINS
36
 
36
 
37
+constexpr uint8_t analog_offset = NUM_DIGITAL_PINS - NUM_ANALOG_INPUTS;
38
+
37
 // Get the digital pin for an analog index
39
 // Get the digital pin for an analog index
38
-pin_t analogInputToDigitalPin(const int8_t p);
40
+constexpr pin_t analogInputToDigitalPin(const int8_t p) {
41
+  return (WITHIN(p, 0, NUM_ANALOG_INPUTS) ? analog_offset + p : P_NC);
42
+}
43
+
44
+// Get the analog index for a digital pin
45
+constexpr int8_t DIGITAL_PIN_TO_ANALOG_PIN(const pin_t p) {
46
+  return (WITHIN(p, analog_offset, NUM_DIGITAL_PINS) ? p - analog_offset : P_NC);
47
+}
39
 
48
 
40
 // Return the index of a pin number
49
 // Return the index of a pin number
41
-int16_t GET_PIN_MAP_INDEX(const pin_t pin);
50
+constexpr int16_t GET_PIN_MAP_INDEX(const pin_t pin) { return pin; }
42
 
51
 
43
 // Test whether the pin is valid
52
 // Test whether the pin is valid
44
-bool VALID_PIN(const pin_t p);
45
-
46
-// Get the analog index for a digital pin
47
-int8_t DIGITAL_PIN_TO_ANALOG_PIN(const pin_t p);
53
+constexpr bool VALID_PIN(const pin_t p) { return WITHIN(p, 0, NUM_DIGITAL_PINS); }
48
 
54
 
49
 // Test whether the pin is PWM
55
 // Test whether the pin is PWM
50
-bool PWM_PIN(const pin_t p);
56
+constexpr bool PWM_PIN(const pin_t p) { return false; }
51
 
57
 
52
 // Test whether the pin is interruptable
58
 // Test whether the pin is interruptable
53
-bool INTERRUPT_PIN(const pin_t p);
59
+constexpr bool INTERRUPT_PIN(const pin_t p) { return false; }
54
 
60
 
55
 // Get the pin number at the given index
61
 // Get the pin number at the given index
56
-pin_t GET_PIN_MAP_PIN(const int16_t ind);
62
+constexpr pin_t GET_PIN_MAP_PIN(const int16_t ind) { return ind; }
57
 
63
 
58
 // Parse a G-code word into a pin index
64
 // Parse a G-code word into a pin index
59
 int16_t PARSED_PIN_INDEX(const char code, const int16_t dval);
65
 int16_t PARSED_PIN_INDEX(const char code, const int16_t dval);

+ 1
- 1
Marlin/src/HAL/LPC1768/HAL.h View File

198
 // Parse a G-code word into a pin index
198
 // Parse a G-code word into a pin index
199
 int16_t PARSED_PIN_INDEX(const char code, const int16_t dval);
199
 int16_t PARSED_PIN_INDEX(const char code, const int16_t dval);
200
 // P0.6 thru P0.9 are for the onboard SD card
200
 // P0.6 thru P0.9 are for the onboard SD card
201
-#define HAL_SENSITIVE_PINS P0_06, P0_07, P0_08, P0_09
201
+#define HAL_SENSITIVE_PINS P0_06, P0_07, P0_08, P0_09,
202
 
202
 
203
 #define HAL_IDLETASK 1
203
 #define HAL_IDLETASK 1
204
 void HAL_idletask();
204
 void HAL_idletask();

+ 3
- 0
Marlin/src/HAL/STM32/inc/Conditionals_adv.h View File

30
   #undef F_CPU
30
   #undef F_CPU
31
   #define F_CPU BOARD_F_CPU
31
   #define F_CPU BOARD_F_CPU
32
 #endif
32
 #endif
33
+
34
+// The Sensitive Pins array is not optimizable
35
+#define RUNTIME_ONLY_ANALOG_TO_DIGITAL

+ 8
- 1
Marlin/src/MarlinCore.cpp View File

282
 #pragma GCC diagnostic push
282
 #pragma GCC diagnostic push
283
 #pragma GCC diagnostic ignored "-Wnarrowing"
283
 #pragma GCC diagnostic ignored "-Wnarrowing"
284
 
284
 
285
+#ifdef RUNTIME_ONLY_ANALOG_TO_DIGITAL
286
+  static const pin_t sensitive_pins[] PROGMEM = { SENSITIVE_PINS };
287
+#else
288
+  template <pin_t ...D>
289
+  constexpr pin_t OnlyPins<-2, D...>::table[sizeof...(D)];
290
+  #define sensitive_pins OnlyPins<SENSITIVE_PINS>::table
291
+#endif
292
+
285
 bool pin_is_protected(const pin_t pin) {
293
 bool pin_is_protected(const pin_t pin) {
286
-  static const pin_t sensitive_pins[] PROGMEM = SENSITIVE_PINS;
287
   LOOP_L_N(i, COUNT(sensitive_pins)) {
294
   LOOP_L_N(i, COUNT(sensitive_pins)) {
288
     pin_t sensitive_pin;
295
     pin_t sensitive_pin;
289
     memcpy_P(&sensitive_pin, &sensitive_pins[i], sizeof(pin_t));
296
     memcpy_P(&sensitive_pin, &sensitive_pins[i], sizeof(pin_t));

+ 53
- 19
Marlin/src/pins/sensitive_pins.h View File

187
   #else
187
   #else
188
     #define _I_MS3
188
     #define _I_MS3
189
   #endif
189
   #endif
190
+  #if PIN_EXISTS(I_ENABLE)
191
+    #define _I_ENABLE_PIN I_ENABLE_PIN,
192
+  #else
193
+    #define _I_ENABLE_PIN
194
+  #endif
190
 
195
 
191
-  #define _I_PINS I_STEP_PIN, I_DIR_PIN, I_ENABLE_PIN, _I_MIN _I_MAX _I_MS1 _I_MS2 _I_MS3 _I_CS
196
+  #define _I_PINS I_STEP_PIN, I_DIR_PIN, _I_ENABLE_PIN _I_MIN _I_MAX _I_MS1 _I_MS2 _I_MS3 _I_CS
192
 
197
 
193
 #else
198
 #else
194
 
199
 
228
   #else
233
   #else
229
     #define _J_MS3
234
     #define _J_MS3
230
   #endif
235
   #endif
236
+  #if PIN_EXISTS(J_ENABLE)
237
+    #define _J_ENABLE_PIN J_ENABLE_PIN,
238
+  #else
239
+    #define _J_ENABLE_PIN
240
+  #endif
231
 
241
 
232
-  #define _J_PINS J_STEP_PIN, J_DIR_PIN, J_ENABLE_PIN, _J_MIN _J_MAX _J_MS1 _J_MS2 _J_MS3 _J_CS
242
+  #define _J_PINS J_STEP_PIN, J_DIR_PIN, _J_ENABLE_PIN _J_MIN _J_MAX _J_MS1 _J_MS2 _J_MS3 _J_CS
233
 
243
 
234
 #else
244
 #else
235
 
245
 
269
   #else
279
   #else
270
     #define _K_MS3
280
     #define _K_MS3
271
   #endif
281
   #endif
282
+  #if PIN_EXISTS(K_ENABLE)
283
+    #define _K_ENABLE_PIN K_ENABLE_PIN,
284
+  #else
285
+    #define _K_ENABLE_PIN
286
+  #endif
272
 
287
 
273
-  #define _K_PINS K_STEP_PIN, K_DIR_PIN, K_ENABLE_PIN, _K_MIN _K_MAX _K_MS1 _K_MS2 _K_MS3 _K_CS
288
+  #define _K_PINS K_STEP_PIN, K_DIR_PIN, _K_ENABLE_PIN _K_MIN _K_MAX _K_MS1 _K_MS2 _K_MS3 _K_CS
274
 
289
 
275
 #else
290
 #else
276
 
291
 
577
 #define _H6_PINS
592
 #define _H6_PINS
578
 #define _H7_PINS
593
 #define _H7_PINS
579
 
594
 
595
+#define DIO_PIN(P) TERN(TARGET_LPC1768, P, analogInputToDigitalPin(P))
596
+
580
 #if HAS_HOTEND
597
 #if HAS_HOTEND
581
   #undef _H0_PINS
598
   #undef _H0_PINS
582
-  #define _H0_PINS HEATER_0_PIN, E0_AUTO_FAN_PIN, analogInputToDigitalPin(TEMP_0_PIN),
599
+  #define _H0_PINS HEATER_0_PIN, E0_AUTO_FAN_PIN, DIO_PIN(TEMP_0_PIN),
583
   #if HAS_MULTI_HOTEND
600
   #if HAS_MULTI_HOTEND
584
     #undef _H1_PINS
601
     #undef _H1_PINS
585
-    #define _H1_PINS HEATER_1_PIN, E1_AUTO_FAN_PIN, analogInputToDigitalPin(TEMP_1_PIN),
602
+    #define _H1_PINS HEATER_1_PIN, E1_AUTO_FAN_PIN, DIO_PIN(TEMP_1_PIN),
586
     #if HOTENDS > 2
603
     #if HOTENDS > 2
587
       #undef _H2_PINS
604
       #undef _H2_PINS
588
-      #define _H2_PINS HEATER_2_PIN, E2_AUTO_FAN_PIN, analogInputToDigitalPin(TEMP_2_PIN),
605
+      #define _H2_PINS HEATER_2_PIN, E2_AUTO_FAN_PIN, DIO_PIN(TEMP_2_PIN),
589
       #if HOTENDS > 3
606
       #if HOTENDS > 3
590
         #undef _H3_PINS
607
         #undef _H3_PINS
591
-        #define _H3_PINS HEATER_3_PIN, E3_AUTO_FAN_PIN, analogInputToDigitalPin(TEMP_3_PIN),
608
+        #define _H3_PINS HEATER_3_PIN, E3_AUTO_FAN_PIN, DIO_PIN(TEMP_3_PIN),
592
         #if HOTENDS > 4
609
         #if HOTENDS > 4
593
           #undef _H4_PINS
610
           #undef _H4_PINS
594
-          #define _H4_PINS HEATER_4_PIN, E4_AUTO_FAN_PIN, analogInputToDigitalPin(TEMP_4_PIN),
611
+          #define _H4_PINS HEATER_4_PIN, E4_AUTO_FAN_PIN, DIO_PIN(TEMP_4_PIN),
595
           #if HOTENDS > 5
612
           #if HOTENDS > 5
596
             #undef _H5_PINS
613
             #undef _H5_PINS
597
-            #define _H5_PINS HEATER_5_PIN, E5_AUTO_FAN_PIN, analogInputToDigitalPin(TEMP_5_PIN),
614
+            #define _H5_PINS HEATER_5_PIN, E5_AUTO_FAN_PIN, DIO_PIN(TEMP_5_PIN),
598
             #if HOTENDS > 6
615
             #if HOTENDS > 6
599
               #undef _H6_PINS
616
               #undef _H6_PINS
600
-              #define _H6_PINS HEATER_6_PIN, E6_AUTO_FAN_PIN, analogInputToDigitalPin(TEMP_6_PIN),
617
+              #define _H6_PINS HEATER_6_PIN, E6_AUTO_FAN_PIN, DIO_PIN(TEMP_6_PIN),
601
               #if HOTENDS > 7
618
               #if HOTENDS > 7
602
                 #undef _H7_PINS
619
                 #undef _H7_PINS
603
-                #define _H7_PINS HEATER_7_PIN, E7_AUTO_FAN_PIN, analogInputToDigitalPin(TEMP_7_PIN),
620
+                #define _H7_PINS HEATER_7_PIN, E7_AUTO_FAN_PIN, DIO_PIN(TEMP_7_PIN),
604
               #endif // HOTENDS > 7
621
               #endif // HOTENDS > 7
605
             #endif // HOTENDS > 6
622
             #endif // HOTENDS > 6
606
           #endif // HOTENDS > 5
623
           #endif // HOTENDS > 5
809
 #endif
826
 #endif
810
 
827
 
811
 #if TEMP_SENSOR_BED && PINS_EXIST(TEMP_BED, HEATER_BED)
828
 #if TEMP_SENSOR_BED && PINS_EXIST(TEMP_BED, HEATER_BED)
812
-  #define _BED_PINS HEATER_BED_PIN, analogInputToDigitalPin(TEMP_BED_PIN),
829
+  #define _BED_PINS HEATER_BED_PIN, DIO_PIN(TEMP_BED_PIN),
813
 #else
830
 #else
814
   #define _BED_PINS
831
   #define _BED_PINS
815
 #endif
832
 #endif
816
 
833
 
817
 #if TEMP_SENSOR_CHAMBER && PIN_EXISTS(TEMP_CHAMBER)
834
 #if TEMP_SENSOR_CHAMBER && PIN_EXISTS(TEMP_CHAMBER)
818
-  #define _CHAMBER_TEMP analogInputToDigitalPin(TEMP_CHAMBER_PIN),
835
+  #define _CHAMBER_TEMP DIO_PIN(TEMP_CHAMBER_PIN),
819
 #else
836
 #else
820
   #define _CHAMBER_TEMP
837
   #define _CHAMBER_TEMP
821
 #endif
838
 #endif
831
 #endif
848
 #endif
832
 
849
 
833
 #if TEMP_SENSOR_COOLER && PIN_EXISTS(TEMP_COOLER)
850
 #if TEMP_SENSOR_COOLER && PIN_EXISTS(TEMP_COOLER)
834
-  #define _COOLER_TEMP analogInputToDigitalPin(TEMP_COOLER_PIN),
851
+  #define _COOLER_TEMP DIO_PIN(TEMP_COOLER_PIN),
835
 #else
852
 #else
836
   #define _COOLER_TEMP
853
   #define _COOLER_TEMP
837
 #endif
854
 #endif
838
-
839
 #if TEMP_SENSOR_COOLER && PIN_EXISTS(COOLER)
855
 #if TEMP_SENSOR_COOLER && PIN_EXISTS(COOLER)
840
   #define _COOLER COOLER_PIN,
856
   #define _COOLER COOLER_PIN,
841
 #else
857
 #else
842
   #define _COOLER
858
   #define _COOLER
843
 #endif
859
 #endif
844
-
845
 #if TEMP_SENSOR_COOLER && PINS_EXIST(TEMP_COOLER, COOLER_AUTO_FAN)
860
 #if TEMP_SENSOR_COOLER && PINS_EXIST(TEMP_COOLER, COOLER_AUTO_FAN)
846
   #define _COOLER_FAN COOLER_AUTO_FAN_PIN,
861
   #define _COOLER_FAN COOLER_AUTO_FAN_PIN,
847
 #else
862
 #else
852
   #define HAL_SENSITIVE_PINS
867
   #define HAL_SENSITIVE_PINS
853
 #endif
868
 #endif
854
 
869
 
855
-#define SENSITIVE_PINS { \
870
+#ifdef RUNTIME_ONLY_ANALOG_TO_DIGITAL
871
+  #define _SP_END
872
+#else
873
+  #define _SP_END -2
874
+
875
+  // Move a regular pin in front to the end
876
+  template<pin_t F, pin_t ...D>
877
+  struct OnlyPins : OnlyPins<D..., F> { };
878
+
879
+  // Remove a -1 from the front
880
+  template<pin_t ...D>
881
+  struct OnlyPins<-1, D...> : OnlyPins<D...> { };
882
+
883
+  // Remove -2 from the front, emit the rest, cease propagation
884
+  template<pin_t ...D>
885
+  struct OnlyPins<_SP_END, D...> { static constexpr pin_t table[sizeof...(D)] PROGMEM = { D... }; };
886
+#endif
887
+
888
+#define SENSITIVE_PINS \
856
   _X_PINS _Y_PINS _Z_PINS _I_PINS _J_PINS _K_PINS \
889
   _X_PINS _Y_PINS _Z_PINS _I_PINS _J_PINS _K_PINS \
857
   _X2_PINS _Y2_PINS _Z2_PINS _Z3_PINS _Z4_PINS _Z_PROBE \
890
   _X2_PINS _Y2_PINS _Z2_PINS _Z3_PINS _Z4_PINS _Z_PROBE \
858
   _E0_PINS _E1_PINS _E2_PINS _E3_PINS _E4_PINS _E5_PINS _E6_PINS _E7_PINS \
891
   _E0_PINS _E1_PINS _E2_PINS _E3_PINS _E4_PINS _E5_PINS _E6_PINS _E7_PINS \
859
   _H0_PINS _H1_PINS _H2_PINS _H3_PINS _H4_PINS _H5_PINS _H6_PINS _H7_PINS \
892
   _H0_PINS _H1_PINS _H2_PINS _H3_PINS _H4_PINS _H5_PINS _H6_PINS _H7_PINS \
860
   _PS_ON _FAN0 _FAN1 _FAN2 _FAN3 _FAN4 _FAN5 _FAN6 _FAN7 _FANC \
893
   _PS_ON _FAN0 _FAN1 _FAN2 _FAN3 _FAN4 _FAN5 _FAN6 _FAN7 _FANC \
861
-  _BED_PINS _COOLER _CHAMBER_TEMP _CHAMBER_HEATER _CHAMBER_FAN HAL_SENSITIVE_PINS \
862
-}
894
+  _BED_PINS _CHAMBER_TEMP _CHAMBER_HEATER _CHAMBER_FAN \
895
+  _COOLER_TEMP _COOLER _COOLER_FAN HAL_SENSITIVE_PINS \
896
+  _SP_END

Loading…
Cancel
Save