Quellcode durchsuchen

Emergency Parser for STM32 (#18095)

rudihorn vor 5 Jahren
Ursprung
Commit
38ccc769f7
Es ist kein Account mit der E-Mail-Adresse des Committers verbunden

+ 3
- 0
Marlin/src/HAL/STM32/HAL.cpp Datei anzeigen

@@ -23,6 +23,7 @@
23 23
 #if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC)
24 24
 
25 25
 #include "HAL.h"
26
+#include "usb_serial.h"
26 27
 
27 28
 #include "../../inc/MarlinConfig.h"
28 29
 #include "../shared/Delay.h"
@@ -79,6 +80,8 @@ void HAL_init() {
79 80
   #endif
80 81
 
81 82
   SetSoftwareSerialTimerInterruptPriority();
83
+
84
+  TERN_(EMERGENCY_PARSER, USB_Hook_init());
82 85
 }
83 86
 
84 87
 void HAL_clear_reset_source() { __HAL_RCC_CLEAR_RESET_FLAGS(); }

+ 19
- 18
Marlin/src/HAL/STM32/HAL.h Datei anzeigen

@@ -30,6 +30,7 @@
30 30
 #include "../shared/HAL_SPI.h"
31 31
 #include "fastio.h"
32 32
 #include "watchdog.h"
33
+#include "MarlinSerial.h"
33 34
 
34 35
 #include "../../inc/MarlinConfigPre.h"
35 36
 
@@ -48,17 +49,17 @@
48 49
 #elif SERIAL_PORT == -1
49 50
   #define MYSERIAL0 SerialUSB
50 51
 #elif SERIAL_PORT == 1
51
-  #define MYSERIAL0 Serial1
52
+  #define MYSERIAL0 MSerial1
52 53
 #elif SERIAL_PORT == 2
53
-  #define MYSERIAL0 Serial2
54
+  #define MYSERIAL0 MSerial2
54 55
 #elif SERIAL_PORT == 3
55
-  #define MYSERIAL0 Serial3
56
+  #define MYSERIAL0 MSerial3
56 57
 #elif SERIAL_PORT == 4
57
-  #define MYSERIAL0 Serial4
58
+  #define MYSERIAL0 MSerial4
58 59
 #elif SERIAL_PORT == 5
59
-  #define MYSERIAL0 Serial5
60
+  #define MYSERIAL0 MSerial5
60 61
 #elif SERIAL_PORT == 6
61
-  #define MYSERIAL0 Serial6
62
+  #define MYSERIAL0 MSerial6
62 63
 #else
63 64
   #error "SERIAL_PORT must be from -1 to 6. Please update your configuration."
64 65
 #endif
@@ -72,17 +73,17 @@
72 73
   #elif SERIAL_PORT_2 == -1
73 74
     #define MYSERIAL1 SerialUSB
74 75
   #elif SERIAL_PORT_2 == 1
75
-    #define MYSERIAL1 Serial1
76
+    #define MYSERIAL1 MSerial1
76 77
   #elif SERIAL_PORT_2 == 2
77
-    #define MYSERIAL1 Serial2
78
+    #define MYSERIAL1 MSerial2
78 79
   #elif SERIAL_PORT_2 == 3
79
-    #define MYSERIAL1 Serial3
80
+    #define MYSERIAL1 MSerial3
80 81
   #elif SERIAL_PORT_2 == 4
81
-    #define MYSERIAL1 Serial4
82
+    #define MYSERIAL1 MSerial4
82 83
   #elif SERIAL_PORT_2 == 5
83
-    #define MYSERIAL1 Serial5
84
+    #define MYSERIAL1 MSerial5
84 85
   #elif SERIAL_PORT_2 == 6
85
-    #define MYSERIAL1 Serial6
86
+    #define MYSERIAL1 MSerial6
86 87
   #else
87 88
     #error "SERIAL_PORT_2 must be from -1 to 6. Please update your configuration."
88 89
   #endif
@@ -100,17 +101,17 @@
100 101
   #elif DGUS_SERIAL_PORT == -1
101 102
     #define DGUS_SERIAL SerialUSB
102 103
   #elif DGUS_SERIAL_PORT == 1
103
-    #define DGUS_SERIAL Serial1
104
+    #define DGUS_SERIAL MSerial1
104 105
   #elif DGUS_SERIAL_PORT == 2
105
-    #define DGUS_SERIAL Serial2
106
+    #define DGUS_SERIAL MSerial2
106 107
   #elif DGUS_SERIAL_PORT == 3
107
-    #define DGUS_SERIAL Serial3
108
+    #define DGUS_SERIAL MSerial3
108 109
   #elif DGUS_SERIAL_PORT == 4
109
-    #define DGUS_SERIAL Serial4
110
+    #define DGUS_SERIAL MSerial4
110 111
   #elif DGUS_SERIAL_PORT == 5
111
-    #define DGUS_SERIAL Serial5
112
+    #define DGUS_SERIAL MSerial5
112 113
   #elif DGUS_SERIAL_PORT == 6
113
-    #define DGUS_SERIAL Serial6
114
+    #define DGUS_SERIAL MSerial6
114 115
   #else
115 116
     #error "DGUS_SERIAL_PORT must be from -1 to 6. Please update your configuration."
116 117
   #endif

+ 80
- 0
Marlin/src/HAL/STM32/MarlinSerial.cpp Datei anzeigen

@@ -0,0 +1,80 @@
1
+/**
2
+ * Marlin 3D Printer Firmware
3
+ * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4
+ *
5
+ * This program is free software: you can redistribute it and/or modify
6
+ * it under the terms of the GNU General Public License as published by
7
+ * the Free Software Foundation, either version 3 of the License, or
8
+ * (at your option) any later version.
9
+ *
10
+ * This program is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
+ * GNU General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU General Public License
16
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
+ *
18
+ */
19
+
20
+#if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC)
21
+
22
+#include "../../inc/MarlinConfig.h"
23
+#include "MarlinSerial.h"
24
+
25
+#if ENABLED(EMERGENCY_PARSER)
26
+  #include "../../feature/e_parser.h"
27
+#endif
28
+
29
+#define DECLARE_SERIAL_PORT(ser_num) \
30
+  void _rx_complete_irq_ ## ser_num (serial_t * obj); \
31
+  MarlinSerial MSerial ## ser_num (USART ## ser_num, &_rx_complete_irq_ ## ser_num); \
32
+  void _rx_complete_irq_ ## ser_num (serial_t * obj) { MSerial ## ser_num ._rx_complete_irq(obj); }
33
+
34
+#define DECLARE_SERIAL_PORT_EXP(ser_num) DECLARE_SERIAL_PORT(ser_num)
35
+
36
+#if defined(SERIAL_PORT) && SERIAL_PORT >= 0
37
+  DECLARE_SERIAL_PORT_EXP(SERIAL_PORT)
38
+#endif
39
+
40
+#if defined(SERIAL_PORT_2) && SERIAL_PORT_2 >= 0
41
+  DECLARE_SERIAL_PORT_EXP(SERIAL_PORT_2)
42
+#endif
43
+
44
+#if defined(DGUS_SERIAL_PORT) && DGUS_SERIAL_PORT >= 0
45
+  DECLARE_SERIAL_PORT_EXP(DGUS_SERIAL_PORT)
46
+#endif
47
+
48
+void MarlinSerial::begin(unsigned long baud, uint8_t config) {
49
+  HardwareSerial::begin(baud, config);
50
+  // replace the IRQ callback with the one we have defined
51
+  #if ENABLED(EMERGENCY_PARSER)
52
+    _serial.rx_callback = _rx_callback;
53
+  #endif
54
+}
55
+
56
+// This function is Copyright (c) 2006 Nicholas Zambetti.
57
+void MarlinSerial::_rx_complete_irq(serial_t *obj) {
58
+  // No Parity error, read byte and store it in the buffer if there is room
59
+  unsigned char c;
60
+
61
+  if (uart_getc(obj, &c) == 0) {
62
+
63
+    rx_buffer_index_t i = (unsigned int)(obj->rx_head + 1) % SERIAL_RX_BUFFER_SIZE;
64
+
65
+    // if we should be storing the received character into the location
66
+    // just before the tail (meaning that the head would advance to the
67
+    // current location of the tail), we're about to overflow the buffer
68
+    // and so we don't write the character or advance the head.
69
+    if (i != obj->rx_tail) {
70
+      obj->rx_buff[obj->rx_head] = c;
71
+      obj->rx_head = i;
72
+    }
73
+
74
+    #if ENABLED(EMERGENCY_PARSER)
75
+      emergency_parser.update(emergency_state, c);
76
+    #endif
77
+  }
78
+}
79
+
80
+#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC

+ 54
- 0
Marlin/src/HAL/STM32/MarlinSerial.h Datei anzeigen

@@ -0,0 +1,54 @@
1
+/**
2
+ * Marlin 3D Printer Firmware
3
+ * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4
+ *
5
+ * This program is free software: you can redistribute it and/or modify
6
+ * it under the terms of the GNU General Public License as published by
7
+ * the Free Software Foundation, either version 3 of the License, or
8
+ * (at your option) any later version.
9
+ *
10
+ * This program is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
+ * GNU General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU General Public License
16
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
+ *
18
+ */
19
+#pragma once
20
+
21
+#include "../../inc/MarlinConfigPre.h"
22
+
23
+#if ENABLED(EMERGENCY_PARSER)
24
+  #include "../../feature/e_parser.h"
25
+#endif
26
+
27
+typedef void (*usart_rx_callback_t)(serial_t * obj);
28
+
29
+class MarlinSerial : public HardwareSerial {
30
+public:
31
+  MarlinSerial(void* peripheral, usart_rx_callback_t rx_callback) :
32
+      HardwareSerial(peripheral), _rx_callback(rx_callback)
33
+      #if ENABLED(EMERGENCY_PARSER)
34
+        , emergency_state(EmergencyParser::State::EP_RESET)
35
+      #endif
36
+  { }
37
+
38
+  void begin(unsigned long baud, uint8_t config);
39
+  inline void begin(unsigned long baud) { begin(baud, SERIAL_8N1); }
40
+
41
+  void _rx_complete_irq(serial_t* obj);
42
+
43
+protected:
44
+  usart_rx_callback_t _rx_callback;
45
+  #if ENABLED(EMERGENCY_PARSER)
46
+    EmergencyParser::State emergency_state;
47
+  #endif
48
+};
49
+
50
+extern MarlinSerial MSerial1;
51
+extern MarlinSerial MSerial2;
52
+extern MarlinSerial MSerial3;
53
+extern MarlinSerial MSerial4;
54
+extern MarlinSerial MSerial5;

+ 0
- 4
Marlin/src/HAL/STM32/inc/SanityCheck.h Datei anzeigen

@@ -28,10 +28,6 @@
28 28
 //  #error "SPINDLE_LASER_PWM_PIN must use SERVO0, SERVO1 or SERVO3 connector"
29 29
 //#endif
30 30
 
31
-#if ENABLED(EMERGENCY_PARSER)
32
-  #error "EMERGENCY_PARSER is not yet implemented for STM32. Disable EMERGENCY_PARSER to continue."
33
-#endif
34
-
35 31
 #if ENABLED(FAST_PWM_FAN)
36 32
   #error "FAST_PWM_FAN is not yet implemented for this platform."
37 33
 #endif

+ 55
- 0
Marlin/src/HAL/STM32/usb_serial.cpp Datei anzeigen

@@ -0,0 +1,55 @@
1
+/**
2
+ * Marlin 3D Printer Firmware
3
+ * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4
+ *
5
+ * This program is free software: you can redistribute it and/or modify
6
+ * it under the terms of the GNU General Public License as published by
7
+ * the Free Software Foundation, either version 3 of the License, or
8
+ * (at your option) any later version.
9
+ *
10
+ * This program is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
+ * GNU General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU General Public License
16
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
+ *
18
+ */
19
+
20
+#if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC)
21
+
22
+#include "../../inc/MarlinConfigPre.h"
23
+
24
+#if ENABLED(EMERGENCY_PARSER)
25
+
26
+#include "usb_serial.h"
27
+#include "../../feature/e_parser.h"
28
+
29
+EmergencyParser::State emergency_state = EmergencyParser::State::EP_RESET;
30
+
31
+int8_t (*USBD_CDC_Receive_original) (uint8_t *Buf, uint32_t *Len) = nullptr;
32
+
33
+static int8_t USBD_CDC_Receive_hook(uint8_t *Buf, uint32_t *Len) {
34
+  for (uint32_t i = 0; i < *Len; i++)
35
+    emergency_parser.update(emergency_state, Buf[i]);
36
+  return USBD_CDC_Receive_original(Buf, Len);
37
+}
38
+
39
+typedef struct _USBD_CDC_Itf {
40
+  int8_t (* Init)(void);
41
+  int8_t (* DeInit)(void);
42
+  int8_t (* Control)(uint8_t cmd, uint8_t *pbuf, uint16_t length);
43
+  int8_t (* Receive)(uint8_t *Buf, uint32_t *Len);
44
+  int8_t (* Transferred)(void);
45
+} USBD_CDC_ItfTypeDef;
46
+
47
+extern USBD_CDC_ItfTypeDef USBD_CDC_fops;
48
+
49
+void USB_Hook_init() {
50
+  USBD_CDC_Receive_original = USBD_CDC_fops.Receive;
51
+  USBD_CDC_fops.Receive = USBD_CDC_Receive_hook;
52
+}
53
+
54
+#endif // EMERGENCY_PARSER
55
+#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC

+ 21
- 0
Marlin/src/HAL/STM32/usb_serial.h Datei anzeigen

@@ -0,0 +1,21 @@
1
+/**
2
+ * Marlin 3D Printer Firmware
3
+ * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4
+ *
5
+ * This program is free software: you can redistribute it and/or modify
6
+ * it under the terms of the GNU General Public License as published by
7
+ * the Free Software Foundation, either version 3 of the License, or
8
+ * (at your option) any later version.
9
+ *
10
+ * This program is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
+ * GNU General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU General Public License
16
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
+ *
18
+ */
19
+#pragma once
20
+
21
+void USB_Hook_init();

Laden…
Abbrechen
Speichern