소스 검색

Add serial XON/XOFF handshaking

From @ejtagle, originally #7459
Scott Lahteine 7 년 전
부모
커밋
508d764d63
6개의 변경된 파일279개의 추가작업 그리고 104개의 파일을 삭제
  1. 2
    1
      Marlin/Configuration.h
  2. 23
    1
      Marlin/Configuration_adv.h
  3. 198
    60
      Marlin/src/HAL/HAL_AVR/MarlinSerial.cpp
  4. 40
    33
      Marlin/src/HAL/HAL_AVR/MarlinSerial.h
  5. 1
    0
      Marlin/src/core/macros.h
  6. 15
    9
      Marlin/src/gcode/queue.cpp

+ 2
- 1
Marlin/Configuration.h 파일 보기

@@ -108,8 +108,9 @@
108 108
  *
109 109
  * 250000 works in most cases, but you might try a lower speed if
110 110
  * you commonly experience drop-outs during host printing.
111
+ * You may try up to 1000000 to speed up SD file transfer.
111 112
  *
112
- * :[2400, 9600, 19200, 38400, 57600, 115200, 250000]
113
+ * :[2400, 9600, 19200, 38400, 57600, 115200, 250000, 500000, 1000000]
113 114
  */
114 115
 #define BAUDRATE 250000
115 116
 

+ 23
- 1
Marlin/Configuration_adv.h 파일 보기

@@ -751,7 +751,7 @@
751 751
 #define MAX_CMD_SIZE 96
752 752
 #define BUFSIZE 4
753 753
 
754
-// Transfer Buffer Size
754
+// Transmission to Host Buffer Size
755 755
 // To save 386 bytes of PROGMEM (and TX_BUFFER_SIZE+3 bytes of RAM) set to 0.
756 756
 // To buffer a simple "ok" you need 4 bytes.
757 757
 // For ADVANCED_OK (M105) you need 32 bytes.
@@ -760,6 +760,28 @@
760 760
 // :[0, 2, 4, 8, 16, 32, 64, 128, 256]
761 761
 #define TX_BUFFER_SIZE 0
762 762
 
763
+// Host Receive Buffer Size
764
+// Without XON/XOFF flow control (see SERIAL_XON_XOFF below) 32 bytes should be enough.
765
+// To use flow control, set this buffer size to at least 1024 bytes.
766
+// :[0, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048]
767
+//#define RX_BUFFER_SIZE 1024
768
+
769
+#if RX_BUFFER_SIZE >= 1024
770
+  // Enable to have the controller send XON/XOFF control characters to
771
+  // the host to signal the RX buffer is becoming full.
772
+  //#define SERIAL_XON_XOFF
773
+#endif
774
+
775
+#if ENABLED(SDSUPPORT)
776
+  // Enable this option to collect and display the maximum
777
+  // RX queue usage after transferring a file to SD.
778
+  //#define SERIAL_STATS_MAX_RX_QUEUED
779
+
780
+  // Enable this option to collect and display the number
781
+  // of dropped bytes after a file transfer to SD.
782
+  //#define SERIAL_STATS_DROPPED_RX
783
+#endif
784
+
763 785
 // Enable an emergency-command parser to intercept certain commands as they
764 786
 // enter the serial receive buffer, so they cannot be blocked.
765 787
 // Currently handles M108, M112, M410

+ 198
- 60
Marlin/src/HAL/HAL_AVR/MarlinSerial.cpp 파일 보기

@@ -27,16 +27,31 @@
27 27
  * Modified 23 November 2006 by David A. Mellis
28 28
  * Modified 28 September 2010 by Mark Sproul
29 29
  * Modified 14 February 2016 by Andreas Hardtung (added tx buffer)
30
+ * Modified 01 October 2017 by Eduardo José Tagle (added XON/XOFF)
30 31
  */
31 32
 #ifdef __AVR__
32 33
 
33
-#include "MarlinSerial.h"
34
-#include "../../Marlin.h"
35
-
36 34
 // Disable HardwareSerial.cpp to support chips without a UART (Attiny, etc.)
37 35
 
36
+#include "../../inc/MarlinConfig.h"
37
+
38 38
 #if !defined(USBCON) && (defined(UBRRH) || defined(UBRR0H) || defined(UBRR1H) || defined(UBRR2H) || defined(UBRR3H))
39 39
 
40
+  #include "MarlinSerial.h"
41
+  #include "../../Marlin.h"
42
+
43
+  struct ring_buffer_r {
44
+    unsigned char buffer[RX_BUFFER_SIZE];
45
+    volatile ring_buffer_pos_t head, tail;
46
+  };
47
+
48
+  #if TX_BUFFER_SIZE > 0
49
+    struct ring_buffer_t {
50
+      unsigned char buffer[TX_BUFFER_SIZE];
51
+      volatile uint8_t head, tail;
52
+    };
53
+  #endif
54
+
40 55
   #if UART_PRESENT(SERIAL_PORT)
41 56
     ring_buffer_r rx_buffer = { { 0 }, 0, 0 };
42 57
     #if TX_BUFFER_SIZE > 0
@@ -45,6 +60,23 @@
45 60
     #endif
46 61
   #endif
47 62
 
63
+  #if ENABLED(SERIAL_XON_XOFF)
64
+    uint8_t xon_xoff_state = XON_XOFF_CHAR_SENT | XON_CHAR;
65
+    constexpr uint8_t XON_XOFF_CHAR_SENT = 0x80;  // XON / XOFF Character was sent
66
+    constexpr uint8_t XON_XOFF_CHAR_MASK = 0x1F;  // XON / XOFF character to send
67
+    // XON / XOFF character definitions
68
+    constexpr uint8_t XON_CHAR  = 17;
69
+    constexpr uint8_t XOFF_CHAR = 19;
70
+  #endif
71
+
72
+  #if ENABLED(SERIAL_STATS_DROPPED_RX)
73
+    uint8_t rx_dropped_bytes = 0;
74
+  #endif
75
+
76
+  #if ENABLED(SERIAL_STATS_MAX_RX_QUEUED)
77
+    ring_buffer_pos_t rx_max_enqueued = 0;
78
+  #endif
79
+
48 80
   #if ENABLED(EMERGENCY_PARSER)
49 81
 
50 82
     #include "../../module/stepper.h"
@@ -136,20 +168,78 @@
136 168
 
137 169
   #endif // EMERGENCY_PARSER
138 170
 
139
-  FORCE_INLINE void store_char(unsigned char c) {
140
-    CRITICAL_SECTION_START;
141
-      const uint8_t h = rx_buffer.head,
142
-                    i = (uint8_t)(h + 1) & (RX_BUFFER_SIZE - 1);
143
-
144
-      // if we should be storing the received character into the location
145
-      // just before the tail (meaning that the head would advance to the
146
-      // current location of the tail), we're about to overflow the buffer
147
-      // and so we don't write the character or advance the head.
148
-      if (i != rx_buffer.tail) {
149
-        rx_buffer.buffer[h] = c;
150
-        rx_buffer.head = i;
171
+  FORCE_INLINE void store_rxd_char() {
172
+    const ring_buffer_pos_t h = rx_buffer.head,
173
+                            i = (ring_buffer_pos_t)(h + 1) & (ring_buffer_pos_t)(RX_BUFFER_SIZE - 1);
174
+
175
+    // If the character is to be stored at the index just before the tail
176
+    // (such that the head would advance to the current tail), the buffer is
177
+    // critical, so don't write the character or advance the head.
178
+    if (i != rx_buffer.tail) {
179
+      rx_buffer.buffer[h] = M_UDRx;
180
+      rx_buffer.head = i;
181
+    }
182
+    else {
183
+      (void)M_UDRx;
184
+      #if ENABLED(SERIAL_STATS_DROPPED_RX)
185
+        if (!++rx_dropped_bytes) ++rx_dropped_bytes;
186
+      #endif
187
+    }
188
+
189
+    #if ENABLED(SERIAL_STATS_MAX_RX_QUEUED)
190
+      // calculate count of bytes stored into the RX buffer
191
+      ring_buffer_pos_t rx_count = (ring_buffer_pos_t)(rx_buffer.head - rx_buffer.tail) & (ring_buffer_pos_t)(RX_BUFFER_SIZE - 1);
192
+      // Keep track of the maximum count of enqueued bytes
193
+      NOLESS(rx_max_enqueued, rx_count);
194
+    #endif
195
+
196
+    #if ENABLED(SERIAL_XON_XOFF)
197
+
198
+      // for high speed transfers, we can use XON/XOFF protocol to do
199
+      // software handshake and avoid overruns.
200
+      if ((xon_xoff_state & XON_XOFF_CHAR_MASK) == XON_CHAR) {
201
+
202
+        // calculate count of bytes stored into the RX buffer
203
+        ring_buffer_pos_t rx_count = (ring_buffer_pos_t)(rx_buffer.head - rx_buffer.tail) & (ring_buffer_pos_t)(RX_BUFFER_SIZE - 1);
204
+
205
+        // if we are above 12.5% of RX buffer capacity, send XOFF before
206
+        // we run out of RX buffer space .. We need 325 bytes @ 250kbits/s to
207
+        // let the host react and stop sending bytes. This translates to 13mS
208
+        // propagation time.
209
+        if (rx_count >= (RX_BUFFER_SIZE) / 8) {
210
+          // If TX interrupts are disabled and data register is empty,
211
+          // just write the byte to the data register and be done. This
212
+          // shortcut helps significantly improve the effective datarate
213
+          // at high (>500kbit/s) bitrates, where interrupt overhead
214
+          // becomes a slowdown.
215
+          if (!TEST(M_UCSRxB, M_UDRIEx) && TEST(M_UCSRxA, M_UDREx)) {
216
+            // Send an XOFF character
217
+            M_UDRx = XOFF_CHAR;
218
+            // clear the TXC bit -- "can be cleared by writing a one to its bit
219
+            // location". This makes sure flush() won't return until the bytes
220
+            // actually got written
221
+            SBI(M_UCSRxA, M_TXCx);
222
+            // And remember it was sent
223
+            xon_xoff_state = XOFF_CHAR | XON_XOFF_CHAR_SENT;
224
+          }
225
+          else {
226
+            // TX interrupts disabled, but buffer still not empty ... or
227
+            // TX interrupts enabled. Reenable TX ints and schedule XOFF
228
+            // character to be sent
229
+            #if TX_BUFFER_SIZE > 0
230
+              SBI(M_UCSRxB, M_UDRIEx);
231
+              xon_xoff_state = XOFF_CHAR;
232
+            #else
233
+              // We are not using TX interrupts, we will have to send this manually
234
+              while (!TEST(M_UCSRxA, M_UDREx)) { /* nada */ };
235
+              M_UDRx = XOFF_CHAR;
236
+              // And remember we already sent it
237
+              xon_xoff_state = XOFF_CHAR | XON_XOFF_CHAR_SENT;
238
+            #endif
239
+          }
240
+        }
151 241
       }
152
-    CRITICAL_SECTION_END;
242
+    #endif // SERIAL_XON_XOFF
153 243
 
154 244
     #if ENABLED(EMERGENCY_PARSER)
155 245
       emergency_parser(c);
@@ -160,37 +250,41 @@
160 250
 
161 251
     FORCE_INLINE void _tx_udr_empty_irq(void) {
162 252
       // If interrupts are enabled, there must be more data in the output
163
-      // buffer. Send the next byte
164
-      const uint8_t t = tx_buffer.tail,
165
-                    c = tx_buffer.buffer[t];
166
-      tx_buffer.tail = (t + 1) & (TX_BUFFER_SIZE - 1);
167
-
168
-      M_UDRx = c;
253
+      // buffer.
254
+
255
+      #if ENABLED(SERIAL_XON_XOFF)
256
+        // Do a priority insertion of an XON/XOFF char, if needed.
257
+        const uint8_t state = xon_xoff_state;
258
+        if (!(state & XON_XOFF_CHAR_SENT)) {
259
+          M_UDRx = state & XON_XOFF_CHAR_MASK;
260
+          xon_xoff_state = state | XON_XOFF_CHAR_SENT;
261
+        }
262
+        else
263
+      #endif
264
+      { // Send the next byte
265
+        const uint8_t t = tx_buffer.tail, c = tx_buffer.buffer[t];
266
+        tx_buffer.tail = (t + 1) & (TX_BUFFER_SIZE - 1);
267
+        M_UDRx = c;
268
+      }
169 269
 
170 270
       // clear the TXC bit -- "can be cleared by writing a one to its bit
171 271
       // location". This makes sure flush() won't return until the bytes
172 272
       // actually got written
173 273
       SBI(M_UCSRxA, M_TXCx);
174 274
 
175
-      if (tx_buffer.head == tx_buffer.tail) {
176
-        // Buffer empty, so disable interrupts
275
+      // Disable interrupts if the buffer is empty
276
+      if (tx_buffer.head == tx_buffer.tail)
177 277
         CBI(M_UCSRxB, M_UDRIEx);
178
-      }
179 278
     }
180 279
 
181 280
     #ifdef M_USARTx_UDRE_vect
182
-      ISR(M_USARTx_UDRE_vect) {
183
-        _tx_udr_empty_irq();
184
-      }
281
+      ISR(M_USARTx_UDRE_vect) { _tx_udr_empty_irq(); }
185 282
     #endif
186 283
 
187 284
   #endif // TX_BUFFER_SIZE
188 285
 
189 286
   #ifdef M_USARTx_RX_vect
190
-    ISR(M_USARTx_RX_vect) {
191
-      const unsigned char c = M_UDRx;
192
-      store_char(c);
193
-    }
287
+    ISR(M_USARTx_RX_vect) { store_rxd_char(); }
194 288
   #endif
195 289
 
196 290
   // Public Methods
@@ -200,9 +294,9 @@
200 294
     bool useU2X = true;
201 295
 
202 296
     #if F_CPU == 16000000UL && SERIAL_PORT == 0
203
-      // hard-coded exception for compatibility with the bootloader shipped
204
-      // with the Duemilanove and previous boards and the firmware on the 8U2
205
-      // on the Uno and Mega 2560.
297
+      // Hard-coded exception for compatibility with the bootloader shipped
298
+      // with the Duemilanove and previous boards, and the firmware on the
299
+      // 8U2 on the Uno and Mega 2560.
206 300
       if (baud == 57600) useU2X = false;
207 301
     #endif
208 302
 
@@ -237,8 +331,9 @@
237 331
 
238 332
   void MarlinSerial::checkRx(void) {
239 333
     if (TEST(M_UCSRxA, M_RXCx)) {
240
-      const uint8_t c = M_UDRx;
241
-      store_char(c);
334
+      CRITICAL_SECTION_START;
335
+        store_rxd_char();
336
+      CRITICAL_SECTION_END;
242 337
     }
243 338
   }
244 339
 
@@ -252,47 +347,76 @@
252 347
   int MarlinSerial::read(void) {
253 348
     int v;
254 349
     CRITICAL_SECTION_START;
255
-      const uint8_t t = rx_buffer.tail;
350
+      const ring_buffer_pos_t t = rx_buffer.tail;
256 351
       if (rx_buffer.head == t)
257 352
         v = -1;
258 353
       else {
259 354
         v = rx_buffer.buffer[t];
260
-        rx_buffer.tail = (uint8_t)(t + 1) & (RX_BUFFER_SIZE - 1);
355
+        rx_buffer.tail = (ring_buffer_pos_t)(t + 1) & (RX_BUFFER_SIZE - 1);
356
+
357
+        #if ENABLED(SERIAL_XON_XOFF)
358
+          if ((xon_xoff_state & XON_XOFF_CHAR_MASK) == XOFF_CHAR) {
359
+            // Get count of bytes in the RX buffer
360
+            ring_buffer_pos_t rx_count = (ring_buffer_pos_t)(rx_buffer.head - rx_buffer.tail) & (ring_buffer_pos_t)(RX_BUFFER_SIZE - 1);
361
+            // When below 10% of RX buffer capacity, send XON before
362
+            // running out of RX buffer bytes
363
+            if (rx_count < (RX_BUFFER_SIZE) / 10) {
364
+              xon_xoff_state = XON_CHAR | XON_XOFF_CHAR_SENT;
365
+              CRITICAL_SECTION_END;       // End critical section before returning!
366
+              writeNoHandshake(XON_CHAR);
367
+              return v;
368
+            }
369
+          }
370
+        #endif
261 371
       }
262 372
     CRITICAL_SECTION_END;
263 373
     return v;
264 374
   }
265 375
 
266
-  uint8_t MarlinSerial::available(void) {
376
+  ring_buffer_pos_t MarlinSerial::available(void) {
267 377
     CRITICAL_SECTION_START;
268
-      const uint8_t h = rx_buffer.head,
269
-                    t = rx_buffer.tail;
378
+      const ring_buffer_pos_t h = rx_buffer.head, t = rx_buffer.tail;
270 379
     CRITICAL_SECTION_END;
271
-    return (uint8_t)(RX_BUFFER_SIZE + h - t) & (RX_BUFFER_SIZE - 1);
380
+    return (ring_buffer_pos_t)(RX_BUFFER_SIZE + h - t) & (RX_BUFFER_SIZE - 1);
272 381
   }
273 382
 
274 383
   void MarlinSerial::flush(void) {
275
-    // RX
276
-    // don't reverse this or there may be problems if the RX interrupt
277
-    // occurs after reading the value of rx_buffer_head but before writing
278
-    // the value to rx_buffer_tail; the previous value of rx_buffer_head
279
-    // may be written to rx_buffer_tail, making it appear as if the buffer
280
-    // were full, not empty.
384
+    // Don't change this order of operations. If the RX interrupt occurs between
385
+    // reading rx_buffer_head and updating rx_buffer_tail, the previous rx_buffer_head
386
+    // may be written to rx_buffer_tail, making the buffer appear full rather than empty.
281 387
     CRITICAL_SECTION_START;
282 388
       rx_buffer.head = rx_buffer.tail;
283 389
     CRITICAL_SECTION_END;
390
+
391
+    #if ENABLED(SERIAL_XON_XOFF)
392
+      if ((xon_xoff_state & XON_XOFF_CHAR_MASK) == XOFF_CHAR) {
393
+        xon_xoff_state = XON_CHAR | XON_XOFF_CHAR_SENT;
394
+        writeNoHandshake(XON_CHAR);
395
+      }
396
+    #endif
284 397
   }
285 398
 
286 399
   #if TX_BUFFER_SIZE > 0
287 400
     uint8_t MarlinSerial::availableForWrite(void) {
288 401
       CRITICAL_SECTION_START;
289
-        const uint8_t h = tx_buffer.head,
290
-                      t = tx_buffer.tail;
402
+        const uint8_t h = tx_buffer.head, t = tx_buffer.tail;
291 403
       CRITICAL_SECTION_END;
292 404
       return (uint8_t)(TX_BUFFER_SIZE + h - t) & (TX_BUFFER_SIZE - 1);
293 405
     }
294 406
 
295 407
     void MarlinSerial::write(const uint8_t c) {
408
+      #if ENABLED(SERIAL_XON_XOFF)
409
+        const uint8_t state = xon_xoff_state;
410
+        if (!(state & XON_XOFF_CHAR_SENT)) {
411
+          // Send 2 chars: XON/XOFF, then a user-specified char
412
+          writeNoHandshake(state & XON_XOFF_CHAR_MASK);
413
+          xon_xoff_state = state | XON_XOFF_CHAR_SENT;
414
+        }
415
+      #endif
416
+      writeNoHandshake(c);
417
+    }
418
+
419
+    void MarlinSerial::writeNoHandshake(const uint8_t c) {
296 420
       _written = true;
297 421
       CRITICAL_SECTION_START;
298 422
         bool emty = (tx_buffer.head == tx_buffer.tail);
@@ -353,20 +477,34 @@
353 477
       }
354 478
       // If we get here, nothing is queued anymore (DRIE is disabled) and
355 479
       // the hardware finished tranmission (TXC is set).
356
-  }
480
+    }
481
+
482
+  #else // TX_BUFFER_SIZE == 0
357 483
 
358
-  #else
359
-    void MarlinSerial::write(uint8_t c) {
360
-      while (!TEST(M_UCSRxA, M_UDREx))
361
-        ;
484
+    void MarlinSerial::write(const uint8_t c) {
485
+      while (!TEST(M_UCSRxA, M_UDREx)) { /* nada */ }
362 486
       M_UDRx = c;
487
+      #if ENABLED(SERIAL_XON_XOFF)
488
+        // Do a priority insertion of an XON/XOFF char, if needed.
489
+        const uint8_t state = xon_xoff_state;
490
+        if (!(state & XON_XOFF_CHAR_SENT)) {
491
+          writeNoHandshake(state & XON_XOFF_CHAR_MASK);
492
+          xon_xoff_state = state | XON_XOFF_CHAR_SENT;
493
+        }
494
+      #endif
495
+      writeNoHandshake(c);
363 496
     }
364
-  #endif
365 497
 
366
-  // end NEW
498
+    void MarlinSerial::writeNoHandshake(const uint8_t c) {
499
+      while (!TEST(M_UCSRxA, M_UDREx)) ;
500
+      M_UDRx = c;
501
+    }
367 502
 
368
-  /// imports from print.h
503
+  #endif // TX_BUFFER_SIZE == 0
369 504
 
505
+  /**
506
+   * Imports from print.h
507
+   */
370 508
 
371 509
   void MarlinSerial::print(char c, int base) {
372 510
     print((long)c, base);
@@ -516,4 +654,4 @@
516 654
   HardwareSerial bluetoothSerial;
517 655
 #endif
518 656
 
519
-#endif
657
+#endif // __AVR__

+ 40
- 33
Marlin/src/HAL/HAL_AVR/MarlinSerial.h 파일 보기

@@ -21,13 +21,13 @@
21 21
  */
22 22
 
23 23
 /**
24
-  MarlinSerial.h - Hardware serial library for Wiring
25
-  Copyright (c) 2006 Nicholas Zambetti.  All right reserved.
26
-
27
-  Modified 28 September 2010 by Mark Sproul
28
-  Modified 14 February 2016 by Andreas Hardtung (added tx buffer)
29
-
30
-*/
24
+ * MarlinSerial.h - Hardware serial library for Wiring
25
+ * Copyright (c) 2006 Nicholas Zambetti.  All right reserved.
26
+ *
27
+ * Modified 28 September 2010 by Mark Sproul
28
+ * Modified 14 February 2016 by Andreas Hardtung (added tx buffer)
29
+ * Modified 01 October 2017 by Eduardo José Tagle (added XON/XOFF)
30
+ */
31 31
 
32 32
 #ifndef MARLINSERIAL_H
33 33
 #define MARLINSERIAL_H
@@ -89,34 +89,33 @@
89 89
   #ifndef TX_BUFFER_SIZE
90 90
     #define TX_BUFFER_SIZE 32
91 91
   #endif
92
-  #if !((RX_BUFFER_SIZE == 256) ||(RX_BUFFER_SIZE == 128) ||(RX_BUFFER_SIZE == 64) ||(RX_BUFFER_SIZE == 32) ||(RX_BUFFER_SIZE == 16) ||(RX_BUFFER_SIZE == 8) ||(RX_BUFFER_SIZE == 4) ||(RX_BUFFER_SIZE == 2))
93
-    #error "RX_BUFFER_SIZE has to be a power of 2 and >= 2"
94
-  #endif
95
-  #if !((TX_BUFFER_SIZE == 256) ||(TX_BUFFER_SIZE == 128) ||(TX_BUFFER_SIZE == 64) ||(TX_BUFFER_SIZE == 32) ||(TX_BUFFER_SIZE == 16) ||(TX_BUFFER_SIZE == 8) ||(TX_BUFFER_SIZE == 4) ||(TX_BUFFER_SIZE == 2) ||(TX_BUFFER_SIZE == 0))
96
-    #error TX_BUFFER_SIZE has to be a power of 2 or 0
92
+
93
+  #if ENABLED(SERIAL_XON_XOFF) && RX_BUFFER_SIZE < 1024
94
+    #error "XON/XOFF requires RX_BUFFER_SIZE >= 1024 for reliable transfers without drops."
97 95
   #endif
98 96
 
99
-  struct ring_buffer_r {
100
-    unsigned char buffer[RX_BUFFER_SIZE];
101
-    volatile uint8_t head;
102
-    volatile uint8_t tail;
103
-  };
97
+  #if !IS_POWER_OF_2(RX_BUFFER_SIZE) || RX_BUFFER_SIZE < 2
98
+    #error "RX_BUFFER_SIZE must be a power of 2 greater than 1."
99
+  #endif
104 100
 
105
-  #if TX_BUFFER_SIZE > 0
106
-    struct ring_buffer_t {
107
-      unsigned char buffer[TX_BUFFER_SIZE];
108
-      volatile uint8_t head;
109
-      volatile uint8_t tail;
110
-    };
101
+  #if TX_BUFFER_SIZE && (TX_BUFFER_SIZE < 2 || TX_BUFFER_SIZE > 256 || !IS_POWER_OF_2(TX_BUFFER_SIZE))
102
+    #error "TX_BUFFER_SIZE must be 0 or a power of 2 greater than 1."
111 103
   #endif
112 104
 
113
-  #if UART_PRESENT(SERIAL_PORT)
114
-    extern ring_buffer_r rx_buffer;
115
-    #if TX_BUFFER_SIZE > 0
116
-      extern ring_buffer_t tx_buffer;
117
-    #endif
105
+  #if RX_BUFFER_SIZE > 256
106
+    typedef uint16_t ring_buffer_pos_t;
107
+  #else
108
+    typedef uint8_t ring_buffer_pos_t;
109
+  #endif
110
+  
111
+  #if ENABLED(SERIAL_STATS_DROPPED_RX)
112
+    extern uint8_t rx_dropped_bytes;
118 113
   #endif
119 114
 
115
+  #if ENABLED(SERIAL_STATS_MAX_RX_QUEUED)
116
+    extern ring_buffer_pos_t rx_max_enqueued;
117
+  #endif  
118
+  
120 119
   class MarlinSerial { //: public Stream
121 120
 
122 121
     public:
@@ -126,19 +125,23 @@
126 125
       static int peek(void);
127 126
       static int read(void);
128 127
       static void flush(void);
129
-      static uint8_t available(void);
128
+      static ring_buffer_pos_t available(void);
130 129
       static void checkRx(void);
131 130
       static void write(const uint8_t c);
132 131
       #if TX_BUFFER_SIZE > 0
133 132
         static uint8_t availableForWrite(void);
134 133
         static void flushTX(void);
135 134
       #endif
135
+      static void writeNoHandshake(const uint8_t c);
136 136
 
137
-    private:
138
-      static void printNumber(unsigned long, const uint8_t);
139
-      static void printFloat(double, uint8_t);
137
+      #if ENABLED(SERIAL_STATS_DROPPED_RX)
138
+        FORCE_INLINE static uint32_t dropped() { return rx_dropped_bytes; }
139
+      #endif
140
+
141
+      #if ENABLED(SERIAL_STATS_MAX_RX_QUEUED)
142
+        FORCE_INLINE static ring_buffer_pos_t rxMaxEnqueued() { return rx_max_enqueued; }
143
+      #endif  
140 144
 
141
-    public:
142 145
       static FORCE_INLINE void write(const char* str) { while (*str) write(*str++); }
143 146
       static FORCE_INLINE void write(const uint8_t* buffer, size_t size) { while (size--) write(*buffer++); }
144 147
       static FORCE_INLINE void print(const String& s) { for (int i = 0; i < (int)s.length(); i++) write(s[i]); }
@@ -163,6 +166,10 @@
163 166
       static void println(double, int = 2);
164 167
       static void println(void);
165 168
       operator bool() { return true; }
169
+
170
+    private:
171
+      static void printNumber(unsigned long, const uint8_t);
172
+      static void printFloat(double, uint8_t);
166 173
   };
167 174
 
168 175
   extern MarlinSerial customizedSerial;

+ 1
- 0
Marlin/src/core/macros.h 파일 보기

@@ -106,6 +106,7 @@
106 106
 #define CIRCLE_CIRC(R) (2.0 * M_PI * (R))
107 107
 
108 108
 #define SIGN(a) ((a>0)-(a<0))
109
+#define IS_POWER_OF_2(x) ((x) && !((x) & ((x) - 1)))
109 110
 
110 111
 // Macros to contrain values
111 112
 #define NOLESS(v,n) do{ if (v < n) v = n; }while(0)

+ 15
- 9
Marlin/src/gcode/queue.cpp 파일 보기

@@ -221,9 +221,9 @@ inline void get_serial_commands() {
221 221
   /**
222 222
    * Loop while serial characters are incoming and the queue is not full
223 223
    */
224
-  while (commands_in_queue < BUFSIZE && MYSERIAL.available() > 0) {
225
-
226
-    char serial_char = MYSERIAL.read();
224
+  int c;
225
+  while (commands_in_queue < BUFSIZE && (c = MYSERIAL.read()) >= 0) {
226
+    char serial_char = c;
227 227
 
228 228
     /**
229 229
      * If the character ends the line
@@ -323,12 +323,9 @@ inline void get_serial_commands() {
323 323
       // The command will be injected when EOL is reached
324 324
     }
325 325
     else if (serial_char == '\\') {  // Handle escapes
326
-      if (MYSERIAL.available() > 0) {
327
-        // if we have one more character, copy it over
328
-        serial_char = MYSERIAL.read();
329
-        if (!serial_comment_mode) serial_line_buffer[serial_count++] = serial_char;
330
-      }
331
-      // otherwise do nothing
326
+      // if we have one more character, copy it over
327
+      if ((c = MYSERIAL.read()) >= 0 && !serial_comment_mode)
328
+        serial_line_buffer[serial_count++] = serial_char;
332 329
     }
333 330
     else { // it's not a newline, carriage return or escape char
334 331
       if (serial_char == ';') serial_comment_mode = true;
@@ -448,6 +445,15 @@ void advance_command_queue() {
448 445
         // M29 closes the file
449 446
         card.closefile();
450 447
         SERIAL_PROTOCOLLNPGM(MSG_FILE_SAVED);
448
+
449
+        #if ENABLED(SERIAL_STATS_DROPPED_RX)
450
+          SERIAL_ECHOLNPAIR("Dropped bytes: ", customizedSerial.dropped());
451
+        #endif
452
+
453
+        #if ENABLED(SERIAL_STATS_MAX_RX_QUEUED)
454
+          SERIAL_ECHOLNPAIR("Max RX Queue Size: ", customizedSerial.rxMaxEnqueued());
455
+        #endif
456
+
451 457
         ok_to_send();
452 458
       }
453 459
       else {

Loading…
취소
저장