浏览代码

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

Scott Lahteine 6 年前
父节点
当前提交
2578996631
没有帐户链接到提交者的电子邮件

+ 5
- 1
Marlin/src/HAL/HAL_AVR/MarlinSerial.cpp 查看文件

@@ -85,6 +85,10 @@
85 85
 
86 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 92
     const ring_buffer_pos_t h = rx_buffer.head,
89 93
                             i = (ring_buffer_pos_t)(h + 1) & (ring_buffer_pos_t)(RX_BUFFER_SIZE - 1);
90 94
 
@@ -160,7 +164,7 @@
160 164
     #endif // SERIAL_XON_XOFF
161 165
 
162 166
     #if ENABLED(EMERGENCY_PARSER)
163
-      emergency_parser.update(c);
167
+      emergency_parser.update(emergency_state, c);
164 168
     #endif
165 169
   }
166 170
 

+ 5
- 1
Marlin/src/HAL/HAL_DUE/MarlinSerial_Due.cpp 查看文件

@@ -112,6 +112,10 @@
112 112
 
113 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 119
     const ring_buffer_pos_t h = rx_buffer.head,
116 120
                             i = (ring_buffer_pos_t)(h + 1) & (ring_buffer_pos_t)(RX_BUFFER_SIZE - 1);
117 121
 
@@ -182,7 +186,7 @@
182 186
     #endif // SERIAL_XON_XOFF
183 187
 
184 188
     #if ENABLED(EMERGENCY_PARSER)
185
-      emergency_parser.update(c);
189
+      emergency_parser.update(emergency_state, c);
186 190
     #endif
187 191
   }
188 192
 

+ 1
- 7
Marlin/src/HAL/HAL_LPC1768/HardwareSerial.cpp 查看文件

@@ -24,12 +24,6 @@
24 24
 
25 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 27
 #if SERIAL_PORT == 0 || SERIAL_PORT_2 == 0
34 28
   HardwareSerial Serial = HardwareSerial(LPC_UART0);
35 29
 #elif SERIAL_PORT == 1 || SERIAL_PORT_2 == 1
@@ -254,7 +248,7 @@ void HardwareSerial::IRQHandler() {
254 248
     // Clear the FIFO
255 249
     while (UART_Receive(UARTx, &byte, 1, NONE_BLOCKING)) {
256 250
       #if ENABLED(EMERGENCY_PARSER)
257
-        emergency_parser.update(byte);
251
+        emergency_parser.update(emergency_state, byte);
258 252
       #endif
259 253
       if ((RxQueueWritePos + 1) % RX_BUFFER_SIZE != RxQueueReadPos) {
260 254
         RxBuffer[RxQueueWritePos] = byte;

+ 11
- 2
Marlin/src/HAL/HAL_LPC1768/HardwareSerial.h 查看文件

@@ -23,6 +23,11 @@
23 23
 #ifndef HARDWARE_SERIAL_H_
24 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 31
 #include <stdarg.h>
27 32
 #include <stdio.h>
28 33
 #include <Stream.h>
@@ -32,8 +37,6 @@ extern "C" {
32 37
   #include "lpc17xx_pinsel.h"
33 38
 }
34 39
 
35
-#include "../../inc/MarlinConfigPre.h"
36
-
37 40
 class HardwareSerial : public Stream {
38 41
 private:
39 42
   LPC_UART_TypeDef *UARTx;
@@ -48,6 +51,9 @@ private:
48 51
     uint32_t TxQueueWritePos;
49 52
     uint32_t TxQueueReadPos;
50 53
   #endif
54
+  #if ENABLED(EMERGENCY_PARSER)
55
+    EmergencyParser::State emergency_state;
56
+  #endif
51 57
 
52 58
 public:
53 59
   HardwareSerial(LPC_UART_TypeDef *UARTx)
@@ -59,6 +65,9 @@ public:
59 65
       , TxQueueWritePos(0)
60 66
       , TxQueueReadPos(0)
61 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 查看文件

@@ -23,6 +23,11 @@
23 23
 #ifndef _HAL_SERIAL_H_
24 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 31
 #include <stdarg.h>
27 32
 #include <stdio.h>
28 33
 
@@ -73,6 +78,11 @@ private:
73 78
 
74 79
 class HalSerial {
75 80
 public:
81
+
82
+  #if ENABLED(EMERGENCY_PARSER)
83
+    EmergencyParser::State emergency_state;
84
+  #endif
85
+
76 86
   HalSerial() { host_connected = false; }
77 87
 
78 88
   void begin(int32_t baud) {

+ 2
- 84
Marlin/src/feature/emergency_parser.cpp 查看文件

@@ -30,92 +30,10 @@
30 30
 
31 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 34
 bool EmergencyParser::killed_by_M112; // = false
38 35
 
36
+// Global instance
39 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 39
 #endif // EMERGENCY_PARSER

+ 86
- 4
Marlin/src/feature/emergency_parser.h 查看文件

@@ -27,8 +27,14 @@
27 27
 #ifndef _EMERGENCY_PARSER_H_
28 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 34
 class EmergencyParser {
31 35
 
36
+public:
37
+
32 38
   // Currently looking for: M108, M112, M410
33 39
   enum State : char {
34 40
     EP_RESET,
@@ -45,14 +51,90 @@ class EmergencyParser {
45 51
     EP_IGNORE // to '\n'
46 52
   };
47 53
 
48
-public:
49
-
50
-  static EmergencyParser::State state;
51 54
   static bool killed_by_M112;
52 55
 
53 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 查看文件

@@ -39,12 +39,6 @@ unsigned short CDC_DepInEmpty = 1;                   // Data IN EP is empty
39 39
 unsigned short CDC_LineState = 0;
40 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 42
 extern HalSerial usb_serial;
49 43
 /*----------------------------------------------------------------------------
50 44
  write data to CDC_OutBuf
@@ -58,7 +52,7 @@ uint32_t CDC_WrOutBuf(const char *buffer, uint32_t *length) {
58 52
 
59 53
   while (bytesToWrite) {
60 54
     #if ENABLED(EMERGENCY_PARSER)
61
-      emergency_parser.update(*buffer);
55
+      emergency_parser.update(usb_serial.emergency_state, *buffer);
62 56
     #endif
63 57
     usb_serial.receive_buffer.write(*buffer++);           // Copy Data to buffer
64 58
     bytesToWrite--;

正在加载...
取消
保存