Browse Source

Emergency Parser for STM32F1 (#19279)

Victor Oliveira 3 years ago
parent
commit
b6ab163814
No account linked to committer's email address

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

@@ -46,6 +46,8 @@
46 46
   #include "msc_sd.h"
47 47
 #endif
48 48
 
49
+#include "MarlinSerial.h"
50
+
49 51
 // ------------------------
50 52
 // Defines
51 53
 // ------------------------
@@ -64,17 +66,6 @@
64 66
   #else
65 67
     #define UsbSerial MarlinCompositeSerial
66 68
   #endif
67
-  #define MSerial1  Serial1
68
-  #define MSerial2  Serial2
69
-  #define MSerial3  Serial3
70
-  #define MSerial4  Serial4
71
-  #define MSerial5  Serial5
72
-#else
73
-  #define MSerial1  Serial
74
-  #define MSerial2  Serial1
75
-  #define MSerial3  Serial2
76
-  #define MSerial4  Serial3
77
-  #define MSerial5  Serial4
78 69
 #endif
79 70
 
80 71
 #if SERIAL_PORT == 0

+ 93
- 0
Marlin/src/HAL/STM32F1/MarlinSerial.cpp View File

@@ -0,0 +1,93 @@
1
+/**
2
+ * Marlin 3D Printer Firmware
3
+ * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4
+ *
5
+ * Based on Sprinter and grbl.
6
+ * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
7
+ *
8
+ * This program is free software: you can redistribute it and/or modify
9
+ * it under the terms of the GNU General Public License as published by
10
+ * the Free Software Foundation, either version 3 of the License, or
11
+ * (at your option) any later version.
12
+ *
13
+ * This program is distributed in the hope that it will be useful,
14
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
+ * GNU General Public License for more details.
17
+ *
18
+ * You should have received a copy of the GNU General Public License
19
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20
+ *
21
+ */
22
+
23
+#include "../../inc/MarlinConfigPre.h"
24
+#include "MarlinSerial.h"
25
+#include <libmaple/usart.h>
26
+
27
+// Copied from ~/.platformio/packages/framework-arduinoststm32-maple/STM32F1/system/libmaple/usart_private.h
28
+// Changed to handle Emergency Parser
29
+static inline __always_inline void my_usart_irq(ring_buffer *rb, ring_buffer *wb, usart_reg_map *regs, MarlinSerial &serial) {
30
+   /* Handle RXNEIE and TXEIE interrupts.
31
+    * RXNE signifies availability of a byte in DR.
32
+    *
33
+    * See table 198 (sec 27.4, p809) in STM document RM0008 rev 15.
34
+    * We enable RXNEIE.
35
+    */
36
+  if ((regs->CR1 & USART_CR1_RXNEIE) && (regs->SR & USART_SR_RXNE)) {
37
+    uint8_t c = (uint8)regs->DR;
38
+    #ifdef USART_SAFE_INSERT
39
+      // If the buffer is full and the user defines USART_SAFE_INSERT,
40
+      // ignore new bytes.
41
+      rb_safe_insert(rb, c);
42
+    #else
43
+      // By default, push bytes around in the ring buffer.
44
+      rb_push_insert(rb, c);
45
+    #endif
46
+    #if ENABLED(EMERGENCY_PARSER)
47
+      emergency_parser.update(serial.emergency_state, c);
48
+    #endif
49
+  }
50
+  // TXE signifies readiness to send a byte to DR.
51
+  if ((regs->CR1 & USART_CR1_TXEIE) && (regs->SR & USART_SR_TXE)) {
52
+    if (!rb_is_empty(wb))
53
+      regs->DR=rb_remove(wb);
54
+    else
55
+      regs->CR1 &= ~((uint32)USART_CR1_TXEIE); // disable TXEIE
56
+  }
57
+}
58
+
59
+#define DEFINE_HWSERIAL_MARLIN(name, n)  \
60
+  MarlinSerial name(USART##n,            \
61
+            BOARD_USART##n##_TX_PIN,     \
62
+            BOARD_USART##n##_RX_PIN);    \
63
+  extern "C" void __irq_usart##n(void) { \
64
+    my_usart_irq(USART##n->rb, USART##n->wb, USART##n##_BASE, MSerial##n); \
65
+  }
66
+
67
+#define DEFINE_HWSERIAL_UART_MARLIN(name, n) \
68
+  MarlinSerial name(UART##n,                 \
69
+            BOARD_USART##n##_TX_PIN,         \
70
+            BOARD_USART##n##_RX_PIN);        \
71
+  extern "C" void __irq_usart##n(void) {     \
72
+    my_usart_irq(USART##n->rb, USART##n->wb, USART##n##_BASE, MSerial##n); \
73
+  }
74
+
75
+#if SERIAL_PORT == 1 || SERIAL_PORT_2 == 1 || DGUS_SERIAL_PORT == 1
76
+  DEFINE_HWSERIAL_MARLIN(MSerial1, 1);
77
+#endif
78
+
79
+#if SERIAL_PORT == 2 || SERIAL_PORT_2 == 2 || DGUS_SERIAL_PORT == 2
80
+  DEFINE_HWSERIAL_MARLIN(MSerial2, 2);
81
+#endif
82
+
83
+#if SERIAL_PORT == 3 || SERIAL_PORT_2 == 3 || DGUS_SERIAL_PORT == 3
84
+  DEFINE_HWSERIAL_MARLIN(MSerial3, 3);
85
+#endif
86
+
87
+#if SERIAL_PORT == 4 || SERIAL_PORT_2 == 4 || DGUS_SERIAL_PORT == 4
88
+  DEFINE_HWSERIAL_UART_MARLIN(MSerial4, 4);
89
+#endif
90
+
91
+#if SERIAL_PORT == 5 || SERIAL_PORT_2 == 5 || DGUS_SERIAL_PORT == 5
92
+  DEFINE_HWSERIAL_UART_MARLIN(MSerial5, 5);
93
+#endif

+ 50
- 0
Marlin/src/HAL/STM32F1/MarlinSerial.h View File

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

+ 0
- 4
Marlin/src/HAL/STM32F1/inc/SanityCheck.h View File

@@ -25,10 +25,6 @@
25 25
  * Test STM32F1-specific configuration values for errors at compile-time.
26 26
  */
27 27
 
28
-#if ENABLED(EMERGENCY_PARSER)
29
-  #error "EMERGENCY_PARSER is not yet implemented for STM32F1. Disable EMERGENCY_PARSER to continue."
30
-#endif
31
-
32 28
 #if ENABLED(FAST_PWM_FAN) || SPINDLE_LASER_FREQUENCY
33 29
   #error "Features requiring Hardware PWM (FAST_PWM_FAN, SPINDLE_LASER_FREQUENCY) are not yet supported on STM32F1."
34 30
 #endif

Loading…
Cancel
Save