Browse Source

Fix STM32F1 emergency parser (#21011)

Arjan Mels 3 years ago
parent
commit
1e726fe405
No account linked to committer's email address
2 changed files with 50 additions and 11 deletions
  1. 28
    1
      Marlin/src/HAL/STM32F1/HAL.cpp
  2. 22
    10
      Marlin/src/HAL/STM32F1/msc_sd.cpp

+ 28
- 1
Marlin/src/HAL/STM32F1/HAL.cpp View File

@@ -84,7 +84,32 @@
84 84
 
85 85
 #if defined(SERIAL_USB) && !HAS_SD_HOST_DRIVE
86 86
   USBSerial SerialUSB;
87
-  DefaultSerial MSerial(false, SerialUSB);
87
+  DefaultSerial MSerial(true, SerialUSB);
88
+
89
+  #if ENABLED(EMERGENCY_PARSER)
90
+    #include "../libmaple/usb/stm32f1/usb_reg_map.h"
91
+    #include "libmaple/usb_cdcacm.h"
92
+    // The original callback is not called (no way to retrieve address).
93
+    // That callback detects a special STM32 reset sequence: this functionality is not essential
94
+    // as M997 achieves the same.
95
+    void my_rx_callback(unsigned int, void*) {
96
+      // max length of 16 is enough to contain all emergency commands
97
+      uint8 buf[16];
98
+
99
+      //rx is usbSerialPart.endpoints[2]
100
+      uint16 len = usb_get_ep_rx_count(USB_CDCACM_RX_ENDP);
101
+      uint32 total = usb_cdcacm_data_available();
102
+
103
+      if (len == 0 || total == 0 || !WITHIN(total, len, COUNT(buf)))
104
+        return;
105
+
106
+      // cannot get character by character due to bug in composite_cdcacm_peek_ex
107
+      len = usb_cdcacm_peek(buf, total);
108
+
109
+      for (uint32 i = 0; i < len; i++)
110
+        emergency_parser.update(MSerial.emergency_state, buf[i + total - len]);
111
+    }
112
+  #endif
88 113
 #endif
89 114
 
90 115
 uint16_t HAL_adc_result;
@@ -254,6 +279,8 @@ void HAL_init() {
254 279
   #endif
255 280
   #if HAS_SD_HOST_DRIVE
256 281
     MSC_SD_init();
282
+  #elif BOTH(SERIAL_USB, EMERGENCY_PARSER)
283
+    usb_cdcacm_set_hooks(USB_CDCACM_HOOK_RX, my_rx_callback);
257 284
   #endif
258 285
   #if PIN_EXISTS(USB_CONNECT)
259 286
     OUT_WRITE(USB_CONNECT_PIN, !USB_CONNECT_INVERTING);  // USB clear connection

+ 22
- 10
Marlin/src/HAL/STM32F1/msc_sd.cpp View File

@@ -19,6 +19,7 @@
19 19
 
20 20
 #include "msc_sd.h"
21 21
 #include "SPI.h"
22
+#include "usb_reg_map.h"
22 23
 
23 24
 #define PRODUCT_ID 0x29
24 25
 
@@ -41,14 +42,27 @@ Serial0Type<USBCompositeSerial> MarlinCompositeSerial(true);
41 42
 #endif
42 43
 
43 44
 #if ENABLED(EMERGENCY_PARSER)
44
-  void (*real_rx_callback)(void);
45 45
 
46
-  void my_rx_callback(void) {
47
-    real_rx_callback();
48
-    int len = MarlinCompositeSerial.available();
49
-    while (len-- > 0) // >0 because available() may return a negative value
50
-      emergency_parser.update(MarlinCompositeSerial.emergency_state, MarlinCompositeSerial.peek());
51
-  }
46
+// The original callback is not called (no way to retrieve address).
47
+// That callback detects a special STM32 reset sequence: this functionality is not essential
48
+// as M997 achieves the same.
49
+void my_rx_callback(unsigned int, void*) {
50
+  // max length of 16 is enough to contain all emergency commands
51
+  uint8 buf[16];
52
+
53
+  //rx is usbSerialPart.endpoints[2]
54
+  uint16 len = usb_get_ep_rx_count(usbSerialPart.endpoints[2].address);
55
+  uint32 total = composite_cdcacm_data_available();
56
+
57
+  if (len == 0 || total == 0 || !WITHIN(total, len, COUNT(buf)))
58
+    return;
59
+
60
+  // cannot get character by character due to bug in composite_cdcacm_peek_ex
61
+  len = composite_cdcacm_peek(buf, total);
62
+
63
+  for (uint32 i = 0; i < len; i++)
64
+    emergency_parser.update(MarlinCompositeSerial.emergency_state, buf[i+total-len]);
65
+}
52 66
 #endif
53 67
 
54 68
 void MSC_SD_init() {
@@ -73,9 +87,7 @@ void MSC_SD_init() {
73 87
   MarlinCompositeSerial.registerComponent();
74 88
   USBComposite.begin();
75 89
   #if ENABLED(EMERGENCY_PARSER)
76
-    //rx is usbSerialPart.endpoints[2]
77
-    real_rx_callback = usbSerialPart.endpoints[2].callback;
78
-    usbSerialPart.endpoints[2].callback = my_rx_callback;
90
+  	composite_cdcacm_set_hooks(USBHID_CDCACM_HOOK_RX, my_rx_callback);
79 91
   #endif
80 92
 }
81 93
 

Loading…
Cancel
Save