Browse Source

🚸 Prevent M42 unintended pin change to output (#22493)

Tanguy Pruvot 3 years ago
parent
commit
f35e0b9382
No account linked to committer's email address
1 changed files with 32 additions and 4 deletions
  1. 32
    4
      Marlin/src/gcode/control/M42.cpp

+ 32
- 4
Marlin/src/gcode/control/M42.cpp View File

31
   #include "../../module/temperature.h"
31
   #include "../../module/temperature.h"
32
 #endif
32
 #endif
33
 
33
 
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
+
34
 void protected_pin_err() {
41
 void protected_pin_err() {
35
   SERIAL_ERROR_MSG(STR_ERR_PROTECTED_PIN);
42
   SERIAL_ERROR_MSG(STR_ERR_PROTECTED_PIN);
36
 }
43
 }
55
 
62
 
56
   if (!parser.boolval('I') && pin_is_protected(pin)) return protected_pin_err();
63
   if (!parser.boolval('I') && pin_is_protected(pin)) return protected_pin_err();
57
 
64
 
65
+  bool avoidWrite = false;
58
   if (parser.seenval('M')) {
66
   if (parser.seenval('M')) {
59
     switch (parser.value_byte()) {
67
     switch (parser.value_byte()) {
60
-      case 0: pinMode(pin, INPUT); break;
68
+      case 0: pinMode(pin, INPUT); avoidWrite = true; break;
61
       case 1: pinMode(pin, OUTPUT); break;
69
       case 1: pinMode(pin, OUTPUT); break;
62
-      case 2: pinMode(pin, INPUT_PULLUP); break;
70
+      case 2: pinMode(pin, INPUT_PULLUP); avoidWrite = true; break;
63
       #ifdef INPUT_PULLDOWN
71
       #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;
65
       #endif
79
       #endif
66
       default: SERIAL_ECHOLNPGM("Invalid Pin Mode"); return;
80
       default: SERIAL_ECHOLNPGM("Invalid Pin Mode"); return;
67
     }
81
     }
99
     }
113
     }
100
   #endif
114
   #endif
101
 
115
 
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
103
   extDigitalWrite(pin, pin_status);
126
   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
104
   analogWrite(pin, pin_status);
132
   analogWrite(pin, pin_status);
105
 }
133
 }
106
 
134
 

Loading…
Cancel
Save