Skip to content

Commit f35e0b9

Browse files
authored
🚸 Prevent M42 unintended pin change to output (#22493)
1 parent 5cb961e commit f35e0b9

File tree

1 file changed

+32
-4
lines changed

1 file changed

+32
-4
lines changed

Marlin/src/gcode/control/M42.cpp

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@
3131
#include "../../module/temperature.h"
3232
#endif
3333

34+
#ifdef MAPLE_STM32F1
35+
// these are enums on the F1...
36+
#define INPUT_PULLDOWN INPUT_PULLDOWN
37+
#define INPUT_ANALOG INPUT_ANALOG
38+
#define OUTPUT_OPEN_DRAIN OUTPUT_OPEN_DRAIN
39+
#endif
40+
3441
void protected_pin_err() {
3542
SERIAL_ERROR_MSG(STR_ERR_PROTECTED_PIN);
3643
}
@@ -55,13 +62,20 @@ void GcodeSuite::M42() {
5562

5663
if (!parser.boolval('I') && pin_is_protected(pin)) return protected_pin_err();
5764

65+
bool avoidWrite = false;
5866
if (parser.seenval('M')) {
5967
switch (parser.value_byte()) {
60-
case 0: pinMode(pin, INPUT); break;
68+
case 0: pinMode(pin, INPUT); avoidWrite = true; break;
6169
case 1: pinMode(pin, OUTPUT); break;
62-
case 2: pinMode(pin, INPUT_PULLUP); break;
70+
case 2: pinMode(pin, INPUT_PULLUP); avoidWrite = true; break;
6371
#ifdef INPUT_PULLDOWN
64-
case 3: pinMode(pin, INPUT_PULLDOWN); break;
72+
case 3: pinMode(pin, INPUT_PULLDOWN); avoidWrite = true; break;
73+
#endif
74+
#ifdef INPUT_ANALOG
75+
case 4: pinMode(pin, INPUT_ANALOG); avoidWrite = true; break;
76+
#endif
77+
#ifdef OUTPUT_OPEN_DRAIN
78+
case 5: pinMode(pin, OUTPUT_OPEN_DRAIN); break;
6579
#endif
6680
default: SERIAL_ECHOLNPGM("Invalid Pin Mode"); return;
6781
}
@@ -99,8 +113,22 @@ void GcodeSuite::M42() {
99113
}
100114
#endif
101115

102-
pinMode(pin, OUTPUT);
116+
if (avoidWrite) {
117+
SERIAL_ECHOLNPGM("?Cannot write to INPUT");
118+
return;
119+
}
120+
121+
// An OUTPUT_OPEN_DRAIN should not be changed to normal OUTPUT (STM32)
122+
// Use M42 Px M1/5 S0/1 to set the output type and then set value
123+
#ifndef OUTPUT_OPEN_DRAIN
124+
pinMode(pin, OUTPUT);
125+
#endif
103126
extDigitalWrite(pin, pin_status);
127+
128+
#ifdef ARDUINO_ARCH_STM32
129+
// A simple I/O will be set to 0 by analogWrite()
130+
if (pin_status <= 1) return;
131+
#endif
104132
analogWrite(pin, pin_status);
105133
}
106134

0 commit comments

Comments
 (0)