Parcourir la source

Raise STM32F1 UART IRQ Priority, add error handling (#19301)

(Error handling for Overrun, Framing and Parity.)
Victor Oliveira il y a 4 ans
Parent
révision
b98946b5c1
Aucun compte lié à l'adresse e-mail de l'auteur

+ 2
- 4
Marlin/src/HAL/STM32/MarlinSerial.cpp Voir le fichier

@@ -55,10 +55,8 @@
55 55
 
56 56
 void MarlinSerial::begin(unsigned long baud, uint8_t config) {
57 57
   HardwareSerial::begin(baud, config);
58
-  // replace the IRQ callback with the one we have defined
59
-  #if ENABLED(EMERGENCY_PARSER)
60
-    _serial.rx_callback = _rx_callback;
61
-  #endif
58
+  // Replace the IRQ callback with the one we have defined
59
+  TERN_(EMERGENCY_PARSER, _serial.rx_callback = _rx_callback);
62 60
 }
63 61
 
64 62
 // This function is Copyright (c) 2006 Nicholas Zambetti.

+ 35
- 20
Marlin/src/HAL/STM32F1/MarlinSerial.cpp Voir le fichier

@@ -29,28 +29,43 @@
29 29
 // Copied from ~/.platformio/packages/framework-arduinoststm32-maple/STM32F1/system/libmaple/usart_private.h
30 30
 // Changed to handle Emergency Parser
31 31
 static inline __always_inline void my_usart_irq(ring_buffer *rb, ring_buffer *wb, usart_reg_map *regs, MarlinSerial &serial) {
32
-   /* Handle RXNEIE and TXEIE interrupts.
33
-    * RXNE signifies availability of a byte in DR.
34
-    *
35
-    * See table 198 (sec 27.4, p809) in STM document RM0008 rev 15.
36
-    * We enable RXNEIE.
37
-    */
38
-  if ((regs->CR1 & USART_CR1_RXNEIE) && (regs->SR & USART_SR_RXNE)) {
39
-    uint8_t c = (uint8)regs->DR;
40
-    #ifdef USART_SAFE_INSERT
41
-      // If the buffer is full and the user defines USART_SAFE_INSERT,
42
-      // ignore new bytes.
43
-      rb_safe_insert(rb, c);
44
-    #else
45
-      // By default, push bytes around in the ring buffer.
46
-      rb_push_insert(rb, c);
47
-    #endif
48
-    #if ENABLED(EMERGENCY_PARSER)
49
-      emergency_parser.update(serial.emergency_state, c);
50
-    #endif
32
+ /* Handle RXNEIE and TXEIE interrupts.
33
+  * RXNE signifies availability of a byte in DR.
34
+  *
35
+  * See table 198 (sec 27.4, p809) in STM document RM0008 rev 15.
36
+  * We enable RXNEIE.
37
+  */
38
+  uint32_t srflags = regs->SR, cr1its = regs->CR1;
39
+
40
+  if ((cr1its & USART_CR1_RXNEIE) && (srflags & USART_SR_RXNE)) {
41
+    if (srflags & USART_SR_FE || srflags & USART_SR_PE ) {
42
+      // framing error or parity error
43
+      regs->DR; // Read and throw away the data, which also clears FE and PE
44
+    }
45
+    else {
46
+      uint8_t c = (uint8)regs->DR;
47
+      #ifdef USART_SAFE_INSERT
48
+        // If the buffer is full and the user defines USART_SAFE_INSERT,
49
+        // ignore new bytes.
50
+        rb_safe_insert(rb, c);
51
+      #else
52
+        // By default, push bytes around in the ring buffer.
53
+        rb_push_insert(rb, c);
54
+      #endif
55
+      #if ENABLED(EMERGENCY_PARSER)
56
+        emergency_parser.update(serial.emergency_state, c);
57
+      #endif
58
+    }
59
+  }
60
+  else if (srflags & USART_SR_ORE) {
61
+    // overrun and empty data, just do a dummy read to clear ORE
62
+    // and prevent a raise condition where a continous interrupt stream (due to ORE set) occurs
63
+    // (see chapter "Overrun error" ) in STM32 reference manual
64
+    regs->DR;
51 65
   }
66
+
52 67
   // TXE signifies readiness to send a byte to DR.
53
-  if ((regs->CR1 & USART_CR1_TXEIE) && (regs->SR & USART_SR_TXE)) {
68
+  if ((cr1its & USART_CR1_TXEIE) && (srflags & USART_SR_TXE)) {
54 69
     if (!rb_is_empty(wb))
55 70
       regs->DR=rb_remove(wb);
56 71
     else

+ 15
- 0
Marlin/src/HAL/STM32F1/MarlinSerial.h Voir le fichier

@@ -22,6 +22,7 @@
22 22
 #pragma once
23 23
 
24 24
 #include <HardwareSerial.h>
25
+#include <libmaple/usart.h>
25 26
 #include <WString.h>
26 27
 
27 28
 #include "../../inc/MarlinConfigPre.h"
@@ -29,6 +30,8 @@
29 30
   #include "../../feature/e_parser.h"
30 31
 #endif
31 32
 
33
+#define UART_IRQ_PRIO 1
34
+
32 35
 class MarlinSerial : public HardwareSerial {
33 36
 public:
34 37
   MarlinSerial(struct usart_dev *usart_device, uint8 tx_pin, uint8 rx_pin) :
@@ -38,6 +41,18 @@ public:
38 41
     #endif
39 42
     { }
40 43
 
44
+  #ifdef UART_IRQ_PRIO
45
+    // shadow the parent methods to set irq priority after the begin
46
+    void begin(uint32 baud) {
47
+      MarlinSerial::begin(baud, SERIAL_8N1);
48
+    }
49
+
50
+    void begin(uint32 baud, uint8_t config) {
51
+      HardwareSerial::begin(baud, config);
52
+      nvic_irq_set_priority(c_dev()->irq_num, UART_IRQ_PRIO);
53
+    }
54
+  #endif
55
+
41 56
   #if ENABLED(EMERGENCY_PARSER)
42 57
     EmergencyParser::State emergency_state;
43 58
   #endif

Chargement…
Annuler
Enregistrer