Sfoglia il codice sorgente

Minor serial code cleanup

Scott Lahteine 3 anni fa
parent
commit
fd2477923c

+ 1
- 1
Marlin/src/HAL/AVR/MarlinSerial.cpp Vedi File

566
   MarlinSerial<MarlinSerialCfg<SERIAL_PORT>>::_tx_udr_empty_irq();
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
 template class MarlinSerial< MarlinSerialCfg<SERIAL_PORT> >;
570
 template class MarlinSerial< MarlinSerialCfg<SERIAL_PORT> >;
571
 MSerialT customizedSerial1(MSerialT::HasEmergencyParser);
571
 MSerialT customizedSerial1(MSerialT::HasEmergencyParser);
572
 
572
 

+ 3
- 0
Marlin/src/core/serial.h Vedi File

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

+ 6
- 6
Marlin/src/core/serial_base.h Vedi File

29
 #endif
29
 #endif
30
 
30
 
31
 // flushTX is not implemented in all HAL, so use SFINAE to call the method where it is.
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
 CALL_IF_EXISTS_IMPL(bool, connected, true);
33
 CALL_IF_EXISTS_IMPL(bool, connected, true);
34
 
34
 
35
 // In order to catch usage errors in code, we make the base to encode number explicit
35
 // In order to catch usage errors in code, we make the base to encode number explicit
42
   Bin = 2
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
 // double or float. For double or float, a conversion exists so the call will be transparent
46
 // double or float. For double or float, a conversion exists so the call will be transparent
47
 struct EnsureDouble {
47
 struct EnsureDouble {
48
   double a;
48
   double a;
49
   FORCE_INLINE operator double() { return a; }
49
   FORCE_INLINE operator double() { return a; }
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
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
   // 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:
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
   FORCE_INLINE EnsureDouble(double a) : a(a) {}
53
   FORCE_INLINE EnsureDouble(double a) : a(a) {}
54
   FORCE_INLINE EnsureDouble(float a) : a(a) {}
54
   FORCE_INLINE EnsureDouble(float a) : a(a) {}
55
 };
55
 };
147
     else write('0');
147
     else write('0');
148
   }
148
   }
149
   void printNumber(signed long n, const uint8_t base) {
149
   void printNumber(signed long n, const uint8_t base) {
150
-    if (base == 10 && n < 0) { 
150
+    if (base == 10 && n < 0) {
151
       n = -n; // This works because all platforms Marlin's builds on are using 2-complement encoding for negative number
151
       n = -n; // This works because all platforms Marlin's builds on are using 2-complement encoding for negative number
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
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
               // -n = 0x7FFFFFFF + 1 => 0x80000000 = 2147483648 (if interpreted as unsigned) or -2147483648 if interpreted as signed.
153
               // -n = 0x7FFFFFFF + 1 => 0x80000000 = 2147483648 (if interpreted as unsigned) or -2147483648 if interpreted as signed.
154
               // On non 2-complement CPU, there would be no possible representation for 2147483648.
154
               // On non 2-complement CPU, there would be no possible representation for 2147483648.
155
-      write('-'); 
156
-    } 
155
+      write('-');
156
+    }
157
     printNumber((unsigned long)n , base);
157
     printNumber((unsigned long)n , base);
158
   }
158
   }
159
 
159
 

+ 1
- 5
Marlin/src/gcode/host/M118.cpp Vedi File

53
   }
53
   }
54
 
54
 
55
   #if HAS_MULTI_SERIAL
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
   #endif
57
   #endif
60
 
58
 
61
   if (hasE) SERIAL_ECHO_START();
59
   if (hasE) SERIAL_ECHO_START();
62
   if (hasA) SERIAL_ECHOPGM("//");
60
   if (hasA) SERIAL_ECHOPGM("//");
63
   SERIAL_ECHOLN(p);
61
   SERIAL_ECHOLN(p);
64
-
65
-  TERN_(HAS_MULTI_SERIAL, multiSerial.portMask = old_serial);
66
 }
62
 }

+ 2
- 1
Marlin/src/libs/autoreport.h Vedi File

42
     const millis_t ms = millis();
42
     const millis_t ms = millis();
43
     if (ELAPSED(ms, next_report_ms)) {
43
     if (ELAPSED(ms, next_report_ms)) {
44
       next_report_ms = ms + SEC_TO_MS(report_interval);
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
       Helper::report();
46
       Helper::report();
47
+      //PORT_RESTORE();
47
     }
48
     }
48
   }
49
   }
49
 };
50
 };

+ 3
- 4
docs/Serial.md Vedi File

1
 # Serial port architecture in Marlin
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
 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.
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
 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.
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
 Starting with version `2.0.9`, Marlin provides a common interface for its serial needs.
8
 Starting with version `2.0.9`, Marlin provides a common interface for its serial needs.
10
 
9
 
11
 ## Common interface
10
 ## Common interface
16
 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.
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
 Instead, the Curiously Recurring Template Pattern (**CRTP**) is used so that, upon compilation, the interface abstraction does not incur a performance cost.
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
 ## Composing the desired feature
20
 ## Composing the desired feature
22
 The different specificities for each architecture are provided by composing the serial type based on desired functionality.
21
 The different specificities for each architecture are provided by composing the serial type based on desired functionality.
32
 This is easily done via type definition of the feature.
31
 This is easily done via type definition of the feature.
33
 
32
 
34
 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:
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
 typedef MultiSerial< RuntimeSerial<Serial>, ConditionalSerial<TelnetClient> > Serial0Type;
35
 typedef MultiSerial< RuntimeSerial<Serial>, ConditionalSerial<TelnetClient> > Serial0Type;
37
 ```
36
 ```
38
 
37
 

Loading…
Annulla
Salva