Parcourir la source

Minor serial code cleanup

Scott Lahteine il y a 3 ans
Parent
révision
fd2477923c

+ 1
- 1
Marlin/src/HAL/AVR/MarlinSerial.cpp Voir le fichier

@@ -566,7 +566,7 @@ ISR(SERIAL_REGNAME(USART, SERIAL_PORT, _UDRE_vect)) {
566 566
   MarlinSerial<MarlinSerialCfg<SERIAL_PORT>>::_tx_udr_empty_irq();
567 567
 }
568 568
 
569
-// Because of the template definition above, it's required to instantiate the template to have all method generated
569
+// Because of the template definition above, it's required to instantiate the template to have all methods generated
570 570
 template class MarlinSerial< MarlinSerialCfg<SERIAL_PORT> >;
571 571
 MSerialT customizedSerial1(MSerialT::HasEmergencyParser);
572 572
 

+ 3
- 0
Marlin/src/core/serial.h Voir le fichier

@@ -62,6 +62,7 @@ typedef int8_t serial_index_t;
62 62
 #define SERIAL_ALL 0x7F
63 63
 #if HAS_MULTI_SERIAL
64 64
   #define _PORT_REDIRECT(n,p)   REMEMBER(n,multiSerial.portMask,p)
65
+  #define _PORT_RESTORE(n,p)    RESTORE(n)
65 66
   #define SERIAL_ASSERT(P)      if(multiSerial.portMask!=(P)){ debugger(); }
66 67
   #ifdef SERIAL_CATCHALL
67 68
     typedef MultiSerial<decltype(MYSERIAL), decltype(SERIAL_CATCHALL), 0> SerialOutputT;
@@ -72,6 +73,7 @@ typedef int8_t serial_index_t;
72 73
   #define SERIAL_IMPL           multiSerial
73 74
 #else
74 75
   #define _PORT_REDIRECT(n,p)   NOOP
76
+  #define _PORT_RESTORE(n)      NOOP
75 77
   #define SERIAL_ASSERT(P)      NOOP
76 78
   #define SERIAL_IMPL           MYSERIAL0
77 79
 #endif
@@ -79,6 +81,7 @@ typedef int8_t serial_index_t;
79 81
 #define SERIAL_OUT(WHAT, V...)  (void)SERIAL_IMPL.WHAT(V)
80 82
 
81 83
 #define PORT_REDIRECT(p)        _PORT_REDIRECT(1,p)
84
+#define PORT_RESTORE()          _PORT_RESTORE(1)
82 85
 #define SERIAL_PORTMASK(P)      _BV(P)
83 86
 
84 87
 //

+ 6
- 6
Marlin/src/core/serial_base.h Voir le fichier

@@ -29,7 +29,7 @@
29 29
 #endif
30 30
 
31 31
 // flushTX is not implemented in all HAL, so use SFINAE to call the method where it is.
32
-CALL_IF_EXISTS_IMPL(void, flushTX );
32
+CALL_IF_EXISTS_IMPL(void, flushTX);
33 33
 CALL_IF_EXISTS_IMPL(bool, connected, true);
34 34
 
35 35
 // In order to catch usage errors in code, we make the base to encode number explicit
@@ -42,14 +42,14 @@ enum class PrintBase {
42 42
   Bin = 2
43 43
 };
44 44
 
45
-// A simple forward struct that prevent the compiler to select print(double, int) as a default overload for any type different than 
45
+// A simple forward struct that prevent the compiler to select print(double, int) as a default overload for any type different than
46 46
 // double or float. For double or float, a conversion exists so the call will be transparent
47 47
 struct EnsureDouble {
48 48
   double a;
49 49
   FORCE_INLINE operator double() { return a; }
50 50
   // If the compiler breaks on ambiguity here, it's likely because you're calling print(X, base) with X not a double or a float, and a
51 51
   // base that's not one of PrintBase's value. This exact code is made to detect such error, you NEED to set a base explicitely like this:
52
-  // SERIAL_PRINT(v, PrintBase::Hex) 
52
+  // SERIAL_PRINT(v, PrintBase::Hex)
53 53
   FORCE_INLINE EnsureDouble(double a) : a(a) {}
54 54
   FORCE_INLINE EnsureDouble(float a) : a(a) {}
55 55
 };
@@ -147,13 +147,13 @@ struct SerialBase {
147 147
     else write('0');
148 148
   }
149 149
   void printNumber(signed long n, const uint8_t base) {
150
-    if (base == 10 && n < 0) { 
150
+    if (base == 10 && n < 0) {
151 151
       n = -n; // This works because all platforms Marlin's builds on are using 2-complement encoding for negative number
152 152
               // On such CPU, changing the sign of a number is done by inverting the bits and adding one, so if n = 0x80000000 = -2147483648 then
153 153
               // -n = 0x7FFFFFFF + 1 => 0x80000000 = 2147483648 (if interpreted as unsigned) or -2147483648 if interpreted as signed.
154 154
               // On non 2-complement CPU, there would be no possible representation for 2147483648.
155
-      write('-'); 
156
-    } 
155
+      write('-');
156
+    }
157 157
     printNumber((unsigned long)n , base);
158 158
   }
159 159
 

+ 1
- 5
Marlin/src/gcode/host/M118.cpp Voir le fichier

@@ -53,14 +53,10 @@ void GcodeSuite::M118() {
53 53
   }
54 54
 
55 55
   #if HAS_MULTI_SERIAL
56
-    const int8_t old_serial = multiSerial.portMask;
57
-    if (WITHIN(port, 0, NUM_SERIAL))
58
-      multiSerial.portMask = port ? _BV(port - 1) : SERIAL_ALL;
56
+    PORT_REDIRECT(WITHIN(port, 0, NUM_SERIAL) ? (port ? _BV(port - 1) : SERIAL_ALL) : multiSerial.portMask);
59 57
   #endif
60 58
 
61 59
   if (hasE) SERIAL_ECHO_START();
62 60
   if (hasA) SERIAL_ECHOPGM("//");
63 61
   SERIAL_ECHOLN(p);
64
-
65
-  TERN_(HAS_MULTI_SERIAL, multiSerial.portMask = old_serial);
66 62
 }

+ 2
- 1
Marlin/src/libs/autoreport.h Voir le fichier

@@ -42,8 +42,9 @@ struct AutoReporter {
42 42
     const millis_t ms = millis();
43 43
     if (ELAPSED(ms, next_report_ms)) {
44 44
       next_report_ms = ms + SEC_TO_MS(report_interval);
45
-      TERN_(HAS_MULTI_SERIAL, PORT_REDIRECT(report_port_mask));
45
+      PORT_REDIRECT(report_port_mask);
46 46
       Helper::report();
47
+      //PORT_RESTORE();
47 48
     }
48 49
   }
49 50
 };

+ 3
- 4
docs/Serial.md Voir le fichier

@@ -1,11 +1,10 @@
1 1
 # Serial port architecture in Marlin
2 2
 
3
-Marlin is targeting a pletora of different CPU architecture and platforms. Each of these platforms has its own serial interface.
3
+Marlin is targeting a plethora of different CPU architecture and platforms. Each of these platforms has its own serial interface.
4 4
 While many provide a Arduino-like Serial class, it's not all of them, and the differences in the existing API create a very complex brain teaser for writing code that works more or less on each platform.
5 5
 
6 6
 Moreover, many platform have intrinsic needs about serial port (like forwarding the output on multiple serial port, providing a *serial-like* telnet server, mixing USB-based serial port with SD card emulation) that are difficult to handle cleanly in the other platform serial logic.
7 7
 
8
-
9 8
 Starting with version `2.0.9`, Marlin provides a common interface for its serial needs.
10 9
 
11 10
 ## Common interface
@@ -16,7 +15,7 @@ Any implementation will need to follow this interface for being used transparent
16 15
 The implementation was written to prioritize performance over abstraction, so the base interface is not using virtual inheritance to avoid the cost of virtual dispatching while calling methods.
17 16
 Instead, the Curiously Recurring Template Pattern (**CRTP**) is used so that, upon compilation, the interface abstraction does not incur a performance cost.
18 17
 
19
-Because some platform do not follow the same interface, the missing method in the actual low-level implementation are detected via SFINAE and a wrapper is generated when such method are missing. See `CALL_IF_EXISTS` macro in `Marlin/src/core/macros.h` for the documentation of this technic.
18
+Because some platform do not follow the same interface, the missing method in the actual low-level implementation are detected via SFINAE and a wrapper is generated when such method are missing. See the `CALL_IF_EXISTS` macro in `Marlin/src/core/macros.h` for documentation of this technique.
20 19
 
21 20
 ## Composing the desired feature
22 21
 The different specificities for each architecture are provided by composing the serial type based on desired functionality.
@@ -32,7 +31,7 @@ Since all the types above are using CRTP, it's possible to combine them to get t
32 31
 This is easily done via type definition of the feature.
33 32
 
34 33
 For example, to present a serial interface that's outputting to 2 serial port, the first one being hooked at runtime and the second one connected to a runtime switchable telnet client, you'll declare the type to use as:
35
-```
34
+```cpp
36 35
 typedef MultiSerial< RuntimeSerial<Serial>, ConditionalSerial<TelnetClient> > Serial0Type;
37 36
 ```
38 37
 

Chargement…
Annuler
Enregistrer