Skip to content

Commit 9679424

Browse files
committed
⚡️ Optimize Sensitive Pins array (except STM32) (MarlinFirmware#22080)
1 parent bfa2579 commit 9679424

File tree

7 files changed

+81
-68
lines changed

7 files changed

+81
-68
lines changed

Marlin/src/HAL/AVR/HAL.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ inline void HAL_adc_init() {
186186
#define GET_PIN_MAP_INDEX(pin) pin
187187
#define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval)
188188

189-
#define HAL_SENSITIVE_PINS 0, 1
189+
#define HAL_SENSITIVE_PINS 0, 1,
190190

191191
#ifdef __AVR_AT90USB1286__
192192
#define JTAG_DISABLE() do{ MCUCR = 0x80; MCUCR = 0x80; }while(0)

Marlin/src/HAL/LINUX/include/pinmapping.cpp

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -25,43 +25,6 @@
2525

2626
#include "../../../gcode/parser.h"
2727

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-
6528
int16_t PARSED_PIN_INDEX(const char code, const int16_t dval) {
6629
return parser.intval(code, dval);
6730
}

Marlin/src/HAL/LINUX/include/pinmapping.h

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,26 +34,32 @@ constexpr uint8_t NUM_ANALOG_INPUTS = 16;
3434

3535
#define HAL_SENSITIVE_PINS
3636

37+
constexpr uint8_t analog_offset = NUM_DIGITAL_PINS - NUM_ANALOG_INPUTS;
38+
3739
// 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+
}
3948

4049
// 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; }
4251

4352
// 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); }
4854

4955
// 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; }
5157

5258
// 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; }
5460

5561
// 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; }
5763

5864
// Parse a G-code word into a pin index
5965
int16_t PARSED_PIN_INDEX(const char code, const int16_t dval);

Marlin/src/HAL/LPC1768/HAL.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ constexpr pin_t GET_PIN_MAP_PIN(const int16_t index) {
198198
// Parse a G-code word into a pin index
199199
int16_t PARSED_PIN_INDEX(const char code, const int16_t dval);
200200
// 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,
202202

203203
#define HAL_IDLETASK 1
204204
void HAL_idletask();

Marlin/src/HAL/STM32/inc/Conditionals_adv.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,6 @@
3030
#undef F_CPU
3131
#define F_CPU BOARD_F_CPU
3232
#endif
33+
34+
// The Sensitive Pins array is not optimizable
35+
#define RUNTIME_ONLY_ANALOG_TO_DIGITAL

Marlin/src/MarlinCore.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,15 @@ bool wait_for_heatup = true;
282282
#pragma GCC diagnostic push
283283
#pragma GCC diagnostic ignored "-Wnarrowing"
284284

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+
285293
bool pin_is_protected(const pin_t pin) {
286-
static const pin_t sensitive_pins[] PROGMEM = SENSITIVE_PINS;
287294
LOOP_L_N(i, COUNT(sensitive_pins)) {
288295
pin_t sensitive_pin;
289296
memcpy_P(&sensitive_pin, &sensitive_pins[i], sizeof(pin_t));

Marlin/src/pins/sensitive_pins.h

Lines changed: 53 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,13 @@
187187
#else
188188
#define _I_MS3
189189
#endif
190+
#if PIN_EXISTS(I_ENABLE)
191+
#define _I_ENABLE_PIN I_ENABLE_PIN,
192+
#else
193+
#define _I_ENABLE_PIN
194+
#endif
190195

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
192197

193198
#else
194199

@@ -228,8 +233,13 @@
228233
#else
229234
#define _J_MS3
230235
#endif
236+
#if PIN_EXISTS(J_ENABLE)
237+
#define _J_ENABLE_PIN J_ENABLE_PIN,
238+
#else
239+
#define _J_ENABLE_PIN
240+
#endif
231241

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
233243

234244
#else
235245

@@ -269,8 +279,13 @@
269279
#else
270280
#define _K_MS3
271281
#endif
282+
#if PIN_EXISTS(K_ENABLE)
283+
#define _K_ENABLE_PIN K_ENABLE_PIN,
284+
#else
285+
#define _K_ENABLE_PIN
286+
#endif
272287

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
274289

275290
#else
276291

@@ -577,30 +592,32 @@
577592
#define _H6_PINS
578593
#define _H7_PINS
579594

595+
#define DIO_PIN(P) TERN(TARGET_LPC1768, P, analogInputToDigitalPin(P))
596+
580597
#if HAS_HOTEND
581598
#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),
583600
#if HAS_MULTI_HOTEND
584601
#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),
586603
#if HOTENDS > 2
587604
#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),
589606
#if HOTENDS > 3
590607
#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),
592609
#if HOTENDS > 4
593610
#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),
595612
#if HOTENDS > 5
596613
#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),
598615
#if HOTENDS > 6
599616
#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),
601618
#if HOTENDS > 7
602619
#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),
604621
#endif // HOTENDS > 7
605622
#endif // HOTENDS > 6
606623
#endif // HOTENDS > 5
@@ -809,13 +826,13 @@
809826
#endif
810827

811828
#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),
813830
#else
814831
#define _BED_PINS
815832
#endif
816833

817834
#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),
819836
#else
820837
#define _CHAMBER_TEMP
821838
#endif
@@ -831,17 +848,15 @@
831848
#endif
832849

833850
#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),
835852
#else
836853
#define _COOLER_TEMP
837854
#endif
838-
839855
#if TEMP_SENSOR_COOLER && PIN_EXISTS(COOLER)
840856
#define _COOLER COOLER_PIN,
841857
#else
842858
#define _COOLER
843859
#endif
844-
845860
#if TEMP_SENSOR_COOLER && PINS_EXIST(TEMP_COOLER, COOLER_AUTO_FAN)
846861
#define _COOLER_FAN COOLER_AUTO_FAN_PIN,
847862
#else
@@ -852,11 +867,30 @@
852867
#define HAL_SENSITIVE_PINS
853868
#endif
854869

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 \
856889
_X_PINS _Y_PINS _Z_PINS _I_PINS _J_PINS _K_PINS \
857890
_X2_PINS _Y2_PINS _Z2_PINS _Z3_PINS _Z4_PINS _Z_PROBE \
858891
_E0_PINS _E1_PINS _E2_PINS _E3_PINS _E4_PINS _E5_PINS _E6_PINS _E7_PINS \
859892
_H0_PINS _H1_PINS _H2_PINS _H3_PINS _H4_PINS _H5_PINS _H6_PINS _H7_PINS \
860893
_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

0 commit comments

Comments
 (0)