Browse Source

[2.0.x] Emergency parser for multiple serial ports (#10524)

Scott Lahteine 6 years ago
parent
commit
2578996631
No account linked to committer's email address

+ 5
- 1
Marlin/src/HAL/HAL_AVR/MarlinSerial.cpp View File

85
 
85
 
86
   FORCE_INLINE void store_rxd_char() {
86
   FORCE_INLINE void store_rxd_char() {
87
 
87
 
88
+    #if ENABLED(EMERGENCY_PARSER)
89
+      static EmergencyParser::State emergency_state; // = EP_RESET
90
+    #endif
91
+
88
     const ring_buffer_pos_t h = rx_buffer.head,
92
     const ring_buffer_pos_t h = rx_buffer.head,
89
                             i = (ring_buffer_pos_t)(h + 1) & (ring_buffer_pos_t)(RX_BUFFER_SIZE - 1);
93
                             i = (ring_buffer_pos_t)(h + 1) & (ring_buffer_pos_t)(RX_BUFFER_SIZE - 1);
90
 
94
 
160
     #endif // SERIAL_XON_XOFF
164
     #endif // SERIAL_XON_XOFF
161
 
165
 
162
     #if ENABLED(EMERGENCY_PARSER)
166
     #if ENABLED(EMERGENCY_PARSER)
163
-      emergency_parser.update(c);
167
+      emergency_parser.update(emergency_state, c);
164
     #endif
168
     #endif
165
   }
169
   }
166
 
170
 

+ 5
- 1
Marlin/src/HAL/HAL_DUE/MarlinSerial_Due.cpp View File

112
 
112
 
113
   FORCE_INLINE void store_rxd_char() {
113
   FORCE_INLINE void store_rxd_char() {
114
 
114
 
115
+    #if ENABLED(EMERGENCY_PARSER)
116
+      static EmergencyParser::State emergency_state; // = EP_RESET
117
+    #endif
118
+
115
     const ring_buffer_pos_t h = rx_buffer.head,
119
     const ring_buffer_pos_t h = rx_buffer.head,
116
                             i = (ring_buffer_pos_t)(h + 1) & (ring_buffer_pos_t)(RX_BUFFER_SIZE - 1);
120
                             i = (ring_buffer_pos_t)(h + 1) & (ring_buffer_pos_t)(RX_BUFFER_SIZE - 1);
117
 
121
 
182
     #endif // SERIAL_XON_XOFF
186
     #endif // SERIAL_XON_XOFF
183
 
187
 
184
     #if ENABLED(EMERGENCY_PARSER)
188
     #if ENABLED(EMERGENCY_PARSER)
185
-      emergency_parser.update(c);
189
+      emergency_parser.update(emergency_state, c);
186
     #endif
190
     #endif
187
   }
191
   }
188
 
192
 

+ 1
- 7
Marlin/src/HAL/HAL_LPC1768/HardwareSerial.cpp View File

24
 
24
 
25
 #include "HardwareSerial.h"
25
 #include "HardwareSerial.h"
26
 
26
 
27
-#include "../../inc/MarlinConfigPre.h"
28
-
29
-#if ENABLED(EMERGENCY_PARSER)
30
-  #include "../../feature/emergency_parser.h"
31
-#endif
32
-
33
 #if SERIAL_PORT == 0 || SERIAL_PORT_2 == 0
27
 #if SERIAL_PORT == 0 || SERIAL_PORT_2 == 0
34
   HardwareSerial Serial = HardwareSerial(LPC_UART0);
28
   HardwareSerial Serial = HardwareSerial(LPC_UART0);
35
 #elif SERIAL_PORT == 1 || SERIAL_PORT_2 == 1
29
 #elif SERIAL_PORT == 1 || SERIAL_PORT_2 == 1
254
     // Clear the FIFO
248
     // Clear the FIFO
255
     while (UART_Receive(UARTx, &byte, 1, NONE_BLOCKING)) {
249
     while (UART_Receive(UARTx, &byte, 1, NONE_BLOCKING)) {
256
       #if ENABLED(EMERGENCY_PARSER)
250
       #if ENABLED(EMERGENCY_PARSER)
257
-        emergency_parser.update(byte);
251
+        emergency_parser.update(emergency_state, byte);
258
       #endif
252
       #endif
259
       if ((RxQueueWritePos + 1) % RX_BUFFER_SIZE != RxQueueReadPos) {
253
       if ((RxQueueWritePos + 1) % RX_BUFFER_SIZE != RxQueueReadPos) {
260
         RxBuffer[RxQueueWritePos] = byte;
254
         RxBuffer[RxQueueWritePos] = byte;

+ 11
- 2
Marlin/src/HAL/HAL_LPC1768/HardwareSerial.h View File

23
 #ifndef HARDWARE_SERIAL_H_
23
 #ifndef HARDWARE_SERIAL_H_
24
 #define HARDWARE_SERIAL_H_
24
 #define HARDWARE_SERIAL_H_
25
 
25
 
26
+#include "../../inc/MarlinConfigPre.h"
27
+#if ENABLED(EMERGENCY_PARSER)
28
+  #include "../../feature/emergency_parser.h"
29
+#endif
30
+
26
 #include <stdarg.h>
31
 #include <stdarg.h>
27
 #include <stdio.h>
32
 #include <stdio.h>
28
 #include <Stream.h>
33
 #include <Stream.h>
32
   #include "lpc17xx_pinsel.h"
37
   #include "lpc17xx_pinsel.h"
33
 }
38
 }
34
 
39
 
35
-#include "../../inc/MarlinConfigPre.h"
36
-
37
 class HardwareSerial : public Stream {
40
 class HardwareSerial : public Stream {
38
 private:
41
 private:
39
   LPC_UART_TypeDef *UARTx;
42
   LPC_UART_TypeDef *UARTx;
48
     uint32_t TxQueueWritePos;
51
     uint32_t TxQueueWritePos;
49
     uint32_t TxQueueReadPos;
52
     uint32_t TxQueueReadPos;
50
   #endif
53
   #endif
54
+  #if ENABLED(EMERGENCY_PARSER)
55
+    EmergencyParser::State emergency_state;
56
+  #endif
51
 
57
 
52
 public:
58
 public:
53
   HardwareSerial(LPC_UART_TypeDef *UARTx)
59
   HardwareSerial(LPC_UART_TypeDef *UARTx)
59
       , TxQueueWritePos(0)
65
       , TxQueueWritePos(0)
60
       , TxQueueReadPos(0)
66
       , TxQueueReadPos(0)
61
     #endif
67
     #endif
68
+    #if ENABLED(EMERGENCY_PARSER)
69
+      , emergency_state(EmergencyParser::State::EP_RESET)
70
+    #endif
62
   {
71
   {
63
   }
72
   }
64
 
73
 

+ 10
- 0
Marlin/src/HAL/HAL_LPC1768/include/serial.h View File

23
 #ifndef _HAL_SERIAL_H_
23
 #ifndef _HAL_SERIAL_H_
24
 #define _HAL_SERIAL_H_
24
 #define _HAL_SERIAL_H_
25
 
25
 
26
+#include "../../../inc/MarlinConfigPre.h"
27
+#if ENABLED(EMERGENCY_PARSER)
28
+  #include "../../../feature/emergency_parser.h"
29
+#endif
30
+
26
 #include <stdarg.h>
31
 #include <stdarg.h>
27
 #include <stdio.h>
32
 #include <stdio.h>
28
 
33
 
73
 
78
 
74
 class HalSerial {
79
 class HalSerial {
75
 public:
80
 public:
81
+
82
+  #if ENABLED(EMERGENCY_PARSER)
83
+    EmergencyParser::State emergency_state;
84
+  #endif
85
+
76
   HalSerial() { host_connected = false; }
86
   HalSerial() { host_connected = false; }
77
 
87
 
78
   void begin(int32_t baud) {
88
   void begin(int32_t baud) {

+ 2
- 84
Marlin/src/feature/emergency_parser.cpp View File

30
 
30
 
31
 #include "emergency_parser.h"
31
 #include "emergency_parser.h"
32
 
32
 
33
-extern volatile bool wait_for_user, wait_for_heatup;
34
-void quickstop_stepper();
35
-
36
-EmergencyParser::State EmergencyParser::state = EP_RESET;
33
+// Static data members
37
 bool EmergencyParser::killed_by_M112; // = false
34
 bool EmergencyParser::killed_by_M112; // = false
38
 
35
 
36
+// Global instance
39
 EmergencyParser emergency_parser;
37
 EmergencyParser emergency_parser;
40
 
38
 
41
-void EmergencyParser::update(const uint8_t c) {
42
-
43
-  switch (state) {
44
-    case EP_RESET:
45
-      switch (c) {
46
-        case ' ': break;
47
-        case 'N': state = EP_N;      break;
48
-        case 'M': state = EP_M;      break;
49
-        default: state  = EP_IGNORE;
50
-      }
51
-      break;
52
-
53
-    case EP_N:
54
-      switch (c) {
55
-        case '0': case '1': case '2':
56
-        case '3': case '4': case '5':
57
-        case '6': case '7': case '8':
58
-        case '9': case '-': case ' ':   break;
59
-        case 'M': state = EP_M;      break;
60
-        default:  state = EP_IGNORE;
61
-      }
62
-      break;
63
-
64
-    case EP_M:
65
-      switch (c) {
66
-        case ' ': break;
67
-        case '1': state = EP_M1;     break;
68
-        case '4': state = EP_M4;     break;
69
-        default: state  = EP_IGNORE;
70
-      }
71
-      break;
72
-
73
-    case EP_M1:
74
-      switch (c) {
75
-        case '0': state = EP_M10;    break;
76
-        case '1': state = EP_M11;    break;
77
-        default: state  = EP_IGNORE;
78
-      }
79
-      break;
80
-
81
-    case EP_M10:
82
-      state = (c == '8') ? EP_M108 : EP_IGNORE;
83
-      break;
84
-
85
-    case EP_M11:
86
-      state = (c == '2') ? EP_M112 : EP_IGNORE;
87
-      break;
88
-
89
-    case EP_M4:
90
-      state = (c == '1') ? EP_M41 : EP_IGNORE;
91
-      break;
92
-
93
-    case EP_M41:
94
-      state = (c == '0') ? EP_M410 : EP_IGNORE;
95
-      break;
96
-
97
-    case EP_IGNORE:
98
-      if (c == '\n') state = EP_RESET;
99
-      break;
100
-
101
-    default:
102
-      if (c == '\n') {
103
-        switch (state) {
104
-          case EP_M108:
105
-            wait_for_user = wait_for_heatup = false;
106
-            break;
107
-          case EP_M112:
108
-            killed_by_M112 = true;
109
-            break;
110
-          case EP_M410:
111
-            quickstop_stepper();
112
-            break;
113
-          default:
114
-            break;
115
-        }
116
-        state = EP_RESET;
117
-      }
118
-  }
119
-}
120
-
121
 #endif // EMERGENCY_PARSER
39
 #endif // EMERGENCY_PARSER

+ 86
- 4
Marlin/src/feature/emergency_parser.h View File

27
 #ifndef _EMERGENCY_PARSER_H_
27
 #ifndef _EMERGENCY_PARSER_H_
28
 #define _EMERGENCY_PARSER_H_
28
 #define _EMERGENCY_PARSER_H_
29
 
29
 
30
+// External references
31
+extern volatile bool wait_for_user, wait_for_heatup;
32
+void quickstop_stepper();
33
+
30
 class EmergencyParser {
34
 class EmergencyParser {
31
 
35
 
36
+public:
37
+
32
   // Currently looking for: M108, M112, M410
38
   // Currently looking for: M108, M112, M410
33
   enum State : char {
39
   enum State : char {
34
     EP_RESET,
40
     EP_RESET,
45
     EP_IGNORE // to '\n'
51
     EP_IGNORE // to '\n'
46
   };
52
   };
47
 
53
 
48
-public:
49
-
50
-  static EmergencyParser::State state;
51
   static bool killed_by_M112;
54
   static bool killed_by_M112;
52
 
55
 
53
   EmergencyParser() {}
56
   EmergencyParser() {}
54
 
57
 
55
-  static void update(const uint8_t c);
58
+  __attribute__((always_inline)) inline
59
+  static void update(State &state, const uint8_t c) {
60
+
61
+    switch (state) {
62
+      case EP_RESET:
63
+        switch (c) {
64
+          case ' ': break;
65
+          case 'N': state = EP_N;      break;
66
+          case 'M': state = EP_M;      break;
67
+          default: state  = EP_IGNORE;
68
+        }
69
+        break;
70
+
71
+      case EP_N:
72
+        switch (c) {
73
+          case '0': case '1': case '2':
74
+          case '3': case '4': case '5':
75
+          case '6': case '7': case '8':
76
+          case '9': case '-': case ' ':   break;
77
+          case 'M': state = EP_M;      break;
78
+          default:  state = EP_IGNORE;
79
+        }
80
+        break;
81
+
82
+      case EP_M:
83
+        switch (c) {
84
+          case ' ': break;
85
+          case '1': state = EP_M1;     break;
86
+          case '4': state = EP_M4;     break;
87
+          default: state  = EP_IGNORE;
88
+        }
89
+        break;
90
+
91
+      case EP_M1:
92
+        switch (c) {
93
+          case '0': state = EP_M10;    break;
94
+          case '1': state = EP_M11;    break;
95
+          default: state  = EP_IGNORE;
96
+        }
97
+        break;
98
+
99
+      case EP_M10:
100
+        state = (c == '8') ? EP_M108 : EP_IGNORE;
101
+        break;
102
+
103
+      case EP_M11:
104
+        state = (c == '2') ? EP_M112 : EP_IGNORE;
105
+        break;
106
+
107
+      case EP_M4:
108
+        state = (c == '1') ? EP_M41 : EP_IGNORE;
109
+        break;
110
+
111
+      case EP_M41:
112
+        state = (c == '0') ? EP_M410 : EP_IGNORE;
113
+        break;
114
+
115
+      case EP_IGNORE:
116
+        if (c == '\n') state = EP_RESET;
117
+        break;
118
+
119
+      default:
120
+        if (c == '\n') {
121
+          switch (state) {
122
+            case EP_M108:
123
+              wait_for_user = wait_for_heatup = false;
124
+              break;
125
+            case EP_M112:
126
+              killed_by_M112 = true;
127
+              break;
128
+            case EP_M410:
129
+              quickstop_stepper();
130
+              break;
131
+            default:
132
+              break;
133
+          }
134
+          state = EP_RESET;
135
+        }
136
+    }
137
+  }
56
 
138
 
57
 };
139
 };
58
 
140
 

+ 1
- 7
frameworks/CMSIS/LPC1768/lib/usb/cdcuser.cpp View File

39
 unsigned short CDC_LineState = 0;
39
 unsigned short CDC_LineState = 0;
40
 unsigned short CDC_SerialState = 0;
40
 unsigned short CDC_SerialState = 0;
41
 
41
 
42
-#include "../../../../../Marlin/src/inc/MarlinConfigPre.h"
43
-
44
-#if ENABLED(EMERGENCY_PARSER)
45
-  #include "../../../../../Marlin/src/feature/emergency_parser.h"
46
-#endif
47
-
48
 extern HalSerial usb_serial;
42
 extern HalSerial usb_serial;
49
 /*----------------------------------------------------------------------------
43
 /*----------------------------------------------------------------------------
50
  write data to CDC_OutBuf
44
  write data to CDC_OutBuf
58
 
52
 
59
   while (bytesToWrite) {
53
   while (bytesToWrite) {
60
     #if ENABLED(EMERGENCY_PARSER)
54
     #if ENABLED(EMERGENCY_PARSER)
61
-      emergency_parser.update(*buffer);
55
+      emergency_parser.update(usb_serial.emergency_state, *buffer);
62
     #endif
56
     #endif
63
     usb_serial.receive_buffer.write(*buffer++);           // Copy Data to buffer
57
     usb_serial.receive_buffer.write(*buffer++);           // Copy Data to buffer
64
     bytesToWrite--;
58
     bytesToWrite--;

Loading…
Cancel
Save