Browse Source

Adding XON/XOFF and STATISTICS implementation

etagle 8 years ago
parent
commit
534bbb81ff
4 changed files with 239 additions and 23 deletions
  1. 2
    1
      Marlin/Configuration.h
  2. 194
    16
      Marlin/MarlinSerial.cpp
  3. 37
    6
      Marlin/MarlinSerial.h
  4. 6
    0
      Marlin/Marlin_main.cpp

+ 2
- 1
Marlin/Configuration.h View File

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

+ 194
- 16
Marlin/MarlinSerial.cpp View File

@@ -44,6 +44,17 @@
44 44
     #endif
45 45
   #endif
46 46
 
47
+  #if ENABLED(SERIAL_XON_XOFF)
48
+	uint8_t xon_xoff_state = XON_XOFF_CHAR_SENT | XON_CHAR;
49
+  #endif
50
+  
51
+  #if ENABLED(SERIAL_STATS_DROPPED_RX)
52
+	uint8_t rx_dropped_bytes = 0;
53
+  #endif
54
+  #if ENABLED(SERIAL_STATS_MAX_RX_QUEUED)
55
+	ring_buffer_pos_t rx_max_enqueued = 0;
56
+  #endif  
57
+
47 58
   #if ENABLED(EMERGENCY_PARSER)
48 59
 
49 60
     #include "stepper.h"
@@ -136,20 +147,94 @@
136 147
 
137 148
   #endif // EMERGENCY_PARSER
138 149
 
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);
150
+  FORCE_INLINE void store_rxd_char() {
151
+    const ring_buffer_pos_t h = rx_buffer.head,
152
+							i = (ring_buffer_pos_t)(h + 1) & (ring_buffer_pos_t)(RX_BUFFER_SIZE - 1);
143 153
 
144 154
       // if we should be storing the received character into the location
145 155
       // just before the tail (meaning that the head would advance to the
146 156
       // current location of the tail), we're about to overflow the buffer
147 157
       // and so we don't write the character or advance the head.
148 158
       if (i != rx_buffer.tail) {
149
-        rx_buffer.buffer[h] = c;
159
+      rx_buffer.buffer[h] = M_UDRx;
150 160
         rx_buffer.head = i;
151 161
       }
152
-    CRITICAL_SECTION_END;
162
+	else {
163
+		(void)M_UDRx;
164
+  #if ENABLED(SERIAL_STATS_DROPPED_RX)		
165
+	  if (!++rx_dropped_bytes)
166
+		  ++rx_dropped_bytes;
167
+  #endif	  
168
+	}
169
+  #if ENABLED(SERIAL_STATS_MAX_RX_QUEUED)
170
+	{
171
+	  // calculate count of bytes stored into the RX buffer
172
+	  ring_buffer_pos_t rx_count = (ring_buffer_pos_t)(rx_buffer.head - rx_buffer.tail) & (ring_buffer_pos_t)(RX_BUFFER_SIZE - 1);
173
+	  
174
+	  // Keep track of the maximum count of enqueued bytes
175
+	  if (rx_max_enqueued < rx_count)
176
+	    rx_max_enqueued = rx_count;
177
+	}
178
+  #endif  
179
+	  
180
+  #if ENABLED(SERIAL_XON_XOFF)
181
+  
182
+    // for high speed transfers, we can use XON/XOFF protocol to do 
183
+    // software handshake and avoid overruns.
184
+    if ((xon_xoff_state & XON_XOFF_CHAR_MASK) == XON_CHAR) {
185
+	
186
+	  // calculate count of bytes stored into the RX buffer
187
+	  ring_buffer_pos_t rx_count = (ring_buffer_pos_t)(rx_buffer.head - rx_buffer.tail) & (ring_buffer_pos_t)(RX_BUFFER_SIZE - 1);
188
+	
189
+	  // if we are above 12.5% of RX buffer capacity, send XOFF before
190
+	  // 	we run out of RX buffer space .. We need 325 bytes @ 250kbits/s to
191
+	  //  let the host react and stop sending bytes. This translates to 13mS
192
+	  //  propagation time.
193
+	  if (rx_count >= (RX_BUFFER_SIZE/8)) {
194
+		
195
+	    // If TX interrupts are disabled and data register is empty,
196
+	    // just write the byte to the data register and be done. This 
197
+	    // shortcut helps significantly improve the effective datarate
198
+	    // at high (>500kbit/s) bitrates, where interrupt overhead 
199
+	    // becomes a slowdown.
200
+	    if (!TEST(M_UCSRxB, M_UDRIEx) && TEST(M_UCSRxA, M_UDREx)) {
201
+		
202
+		  // Send an XOFF character
203
+		  M_UDRx = XOFF_CHAR;
204
+		
205
+		  // clear the TXC bit -- "can be cleared by writing a one to its bit
206
+		  // location". This makes sure flush() won't return until the bytes
207
+		  // actually got written
208
+		  SBI(M_UCSRxA, M_TXCx);
209
+		  
210
+		  // And remember we already sent it
211
+		  xon_xoff_state = XOFF_CHAR | XON_XOFF_CHAR_SENT;
212
+		  
213
+	    } else {
214
+
215
+		  // TX interrupts disabled, but buffer still not empty ... or 
216
+		  // TX interrupts enabled. Reenable TX ints and schedule XOFF 
217
+		  // character to be sent
218
+	  #if TX_BUFFER_SIZE > 0		
219
+		
220
+		  SBI(M_UCSRxB, M_UDRIEx);
221
+		  xon_xoff_state = XOFF_CHAR;
222
+		
223
+	  #else
224
+		  // We are not using TX interrupts, we will have to send this manually
225
+		  while (!TEST(M_UCSRxA, M_UDREx))
226
+		    ;
227
+		  M_UDRx = XOFF_CHAR;
228
+		
229
+		  // And remember we already sent it
230
+		  xon_xoff_state = XOFF_CHAR | XON_XOFF_CHAR_SENT;
231
+
232
+	  #endif
233
+		}
234
+
235
+	  }
236
+	}
237
+  #endif
153 238
 
154 239
     #if ENABLED(EMERGENCY_PARSER)
155 240
       emergency_parser(c);
@@ -160,13 +245,31 @@
160 245
 
161 246
     FORCE_INLINE void _tx_udr_empty_irq(void) {
162 247
       // If interrupts are enabled, there must be more data in the output
163
-      // buffer. Send the next byte
248
+      // buffer.
249
+
250
+    #if ENABLED(SERIAL_XON_XOFF)
251
+	
252
+	  // If we must do a priority insertion of an XON/XOFF char, 
253
+	  //  do it now
254
+	  uint8_t state = xon_xoff_state;
255
+	  if (!(state & XON_XOFF_CHAR_SENT)) {
256
+		M_UDRx = state & XON_XOFF_CHAR_MASK;
257
+		xon_xoff_state = state | XON_XOFF_CHAR_SENT;
258
+		
259
+	  } else {
260
+	#endif
261
+	  
262
+		// Send the next byte
164 263
       const uint8_t t = tx_buffer.tail,
165 264
                     c = tx_buffer.buffer[t];
166 265
       tx_buffer.tail = (t + 1) & (TX_BUFFER_SIZE - 1);
167 266
 
168 267
       M_UDRx = c;
169 268
 
269
+    #if ENABLED(SERIAL_XON_XOFF)
270
+	  }
271
+	#endif
272
+	
170 273
       // clear the TXC bit -- "can be cleared by writing a one to its bit
171 274
       // location". This makes sure flush() won't return until the bytes
172 275
       // actually got written
@@ -188,8 +291,7 @@
188 291
 
189 292
   #ifdef M_USARTx_RX_vect
190 293
     ISR(M_USARTx_RX_vect) {
191
-      const unsigned char c = M_UDRx;
192
-      store_char(c);
294
+      store_rxd_char();
193 295
     }
194 296
   #endif
195 297
 
@@ -237,8 +339,9 @@
237 339
 
238 340
   void MarlinSerial::checkRx(void) {
239 341
     if (TEST(M_UCSRxA, M_RXCx)) {
240
-      const uint8_t c = M_UDRx;
241
-      store_char(c);
342
+	  CRITICAL_SECTION_START;
343
+        store_rxd_char();
344
+	  CRITICAL_SECTION_END;
242 345
     }
243 346
   }
244 347
 
@@ -252,23 +355,52 @@
252 355
   int MarlinSerial::read(void) {
253 356
     int v;
254 357
     CRITICAL_SECTION_START;
255
-      const uint8_t t = rx_buffer.tail;
358
+      const ring_buffer_pos_t t = rx_buffer.tail;
256 359
       if (rx_buffer.head == t)
257 360
         v = -1;
258 361
       else {
259 362
         v = rx_buffer.buffer[t];
260
-        rx_buffer.tail = (uint8_t)(t + 1) & (RX_BUFFER_SIZE - 1);
363
+        rx_buffer.tail = (ring_buffer_pos_t)(t + 1) & (RX_BUFFER_SIZE - 1);
364
+		
365
+	  #if ENABLED(SERIAL_XON_XOFF)
366
+	  
367
+		// for high speed transfers, we can use XON/XOFF protocol to do 
368
+		// software handshake and avoid overruns.
369
+		if ((xon_xoff_state & XON_XOFF_CHAR_MASK) == XOFF_CHAR) {
370
+			
371
+		  // calculate count of bytes stored into the RX buffer
372
+		  ring_buffer_pos_t rx_count = (ring_buffer_pos_t)(rx_buffer.head - rx_buffer.tail) & (ring_buffer_pos_t)(RX_BUFFER_SIZE - 1);
373
+			
374
+		  // if we are below 10% of RX buffer capacity, send XON before
375
+		  // 	we run out of RX buffer bytes
376
+		  if (rx_count < (RX_BUFFER_SIZE/10)) {
377
+				
378
+			// Send an XON character
379
+			xon_xoff_state = XON_CHAR | XON_XOFF_CHAR_SENT;
380
+	
381
+			// End critical section
382
+			CRITICAL_SECTION_END;	
383
+			 
384
+			// Transmit the XON character
385
+			writeNoHandshake(XON_CHAR);
386
+			
387
+			// Done
388
+			return v;
389
+		  }
390
+		}
391
+	  #endif
392
+		
261 393
       }
262 394
     CRITICAL_SECTION_END;
263 395
     return v;
264 396
   }
265 397
 
266
-  uint8_t MarlinSerial::available(void) {
398
+  ring_buffer_pos_t MarlinSerial::available(void) {
267 399
     CRITICAL_SECTION_START;
268
-      const uint8_t h = rx_buffer.head,
400
+      const ring_buffer_pos_t h = rx_buffer.head,
269 401
                     t = rx_buffer.tail;
270 402
     CRITICAL_SECTION_END;
271
-    return (uint8_t)(RX_BUFFER_SIZE + h - t) & (RX_BUFFER_SIZE - 1);
403
+    return (ring_buffer_pos_t)(RX_BUFFER_SIZE + h - t) & (RX_BUFFER_SIZE - 1);
272 404
   }
273 405
 
274 406
   void MarlinSerial::flush(void) {
@@ -281,6 +413,20 @@
281 413
     CRITICAL_SECTION_START;
282 414
       rx_buffer.head = rx_buffer.tail;
283 415
     CRITICAL_SECTION_END;
416
+	
417
+  #if ENABLED(SERIAL_XON_XOFF)
418
+  
419
+	// for high speed transfers, we can use XON/XOFF protocol to do 
420
+	// software handshake and avoid overruns.
421
+	if ((xon_xoff_state & XON_XOFF_CHAR_MASK) == XOFF_CHAR) {
422
+		
423
+		// Send an XON character
424
+		xon_xoff_state = XON_CHAR | XON_XOFF_CHAR_SENT;
425
+
426
+		// Transmit the XON character
427
+		writeNoHandshake(XON_CHAR);
428
+	}
429
+  #endif
284 430
   }
285 431
 
286 432
   #if TX_BUFFER_SIZE > 0
@@ -293,10 +439,26 @@
293 439
     }
294 440
 
295 441
     void MarlinSerial::write(const uint8_t c) {
442
+	
443
+	#if ENABLED(SERIAL_XON_XOFF)
444
+	  uint8_t state = xon_xoff_state;
445
+  	  if (!(state & XON_XOFF_CHAR_SENT)) {
446
+		// 2 characters to send: The XON/XOFF character and the user
447
+		// specified char. 
448
+		writeNoHandshake(state & XON_XOFF_CHAR_MASK);
449
+		xon_xoff_state = state | XON_XOFF_CHAR_SENT;		
450
+	  }
451
+	#endif
452
+	  writeNoHandshake(c);
453
+    }
454
+	
455
+	void MarlinSerial::writeNoHandshake(uint8_t c) {
456
+		
296 457
       _written = true;
297 458
       CRITICAL_SECTION_START;
298 459
         bool emty = (tx_buffer.head == tx_buffer.tail);
299 460
       CRITICAL_SECTION_END;
461
+	  
300 462
       // If the buffer and the data register is empty, just write the byte
301 463
       // to the data register and be done. This shortcut helps
302 464
       // significantly improve the effective datarate at high (>
@@ -335,6 +497,7 @@
335 497
       return;
336 498
     }
337 499
 
500
+	
338 501
     void MarlinSerial::flushTX(void) {
339 502
       // TX
340 503
       // If we have never written a byte, no need to flush. This special
@@ -357,6 +520,21 @@
357 520
 
358 521
   #else
359 522
     void MarlinSerial::write(uint8_t c) {
523
+		
524
+	#if ENABLED(SERIAL_XON_XOFF)
525
+	  // If we must do a priority insertion of an XON/XOFF char, do it now
526
+	  uint8_t state = xon_xoff_state;
527
+	  if (!(state & XON_XOFF_CHAR_SENT)) {
528
+		  
529
+		writeNoHandshake(state & XON_XOFF_CHAR_MASK);
530
+		xon_xoff_state = state | XON_XOFF_CHAR_SENT;
531
+	  }
532
+	#endif
533
+
534
+	  writeNoHandshake(c);
535
+    }
536
+	
537
+    void MarlinSerial::writeNoHandshake(uint8_t c) {
360 538
       while (!TEST(M_UCSRxA, M_UDREx))
361 539
         ;
362 540
       M_UDRx = c;

+ 37
- 6
Marlin/MarlinSerial.h View File

@@ -90,17 +90,22 @@
90 90
   #ifndef TX_BUFFER_SIZE
91 91
     #define TX_BUFFER_SIZE 32
92 92
   #endif
93
-  #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
+  #if !IS_POWEROF2(RX_BUFFER_SIZE) || (RX_BUFFER_SIZE < 2)
94 94
     #error "RX_BUFFER_SIZE has to be a power of 2 and >= 2"
95 95
   #endif
96
-  #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))
97
-    #error TX_BUFFER_SIZE has to be a power of 2 or 0
96
+  #if TX_BUFFER_SIZE != 0 && (TX_BUFFER_SIZE < 2 || TX_BUFFER_SIZE > 256 || !IS_POWEROF2(TX_BUFFER_SIZE))
97
+    #error "TX_BUFFER_SIZE has to be a power of 2 or 0"
98
+  #endif
99
+  #if RX_BUFFER_SIZE > 256
100
+	typedef uint16_t ring_buffer_pos_t;
101
+  #else
102
+	typedef uint8_t ring_buffer_pos_t;
98 103
   #endif
99 104
 
100 105
   struct ring_buffer_r {
101 106
     unsigned char buffer[RX_BUFFER_SIZE];
102
-    volatile uint8_t head;
103
-    volatile uint8_t tail;
107
+    volatile ring_buffer_pos_t head;
108
+    volatile ring_buffer_pos_t tail;
104 109
   };
105 110
 
106 111
   #if TX_BUFFER_SIZE > 0
@@ -118,6 +123,24 @@
118 123
     #endif
119 124
   #endif
120 125
 
126
+  #if ENABLED(SERIAL_XON_XOFF)
127
+	#define XON_XOFF_CHAR_SENT 	(uint8_t)0x80	/* XON / XOFF Character was sent */
128
+	#define XON_XOFF_CHAR_MASK  (uint8_t)0x1F	/* XON / XOFF character to send */
129
+	
130
+	extern uint8_t xon_xoff_state;
131
+
132
+	// XON / XOFF character definitions
133
+	#define XON_CHAR  (uint8_t)17
134
+	#define XOFF_CHAR (uint8_t)19
135
+  #endif
136
+  
137
+  #if ENABLED(SERIAL_STATS_DROPPED_RX)
138
+	extern uint8_t rx_dropped_bytes;
139
+  #endif
140
+  #if ENABLED(SERIAL_STATS_MAX_RX_QUEUED)
141
+	extern ring_buffer_pos_t rx_max_enqueued;
142
+  #endif  
143
+  
121 144
   class MarlinSerial { //: public Stream
122 145
 
123 146
     public:
@@ -127,13 +150,21 @@
127 150
       static int peek(void);
128 151
       static int read(void);
129 152
       static void flush(void);
130
-      static uint8_t available(void);
153
+      static ring_buffer_pos_t available(void);
131 154
       static void checkRx(void);
132 155
       static void write(const uint8_t c);
133 156
       #if TX_BUFFER_SIZE > 0
134 157
         static uint8_t availableForWrite(void);
135 158
         static void flushTX(void);
136 159
       #endif
160
+	  static void writeNoHandshake(uint8_t c);
161
+
162
+  #if ENABLED(SERIAL_STATS_DROPPED_RX)
163
+	  static uint32_t dropped() { return rx_dropped_bytes; }
164
+  #endif
165
+  #if ENABLED(SERIAL_STATS_MAX_RX_QUEUED)
166
+	  static ring_buffer_pos_t rxMaxEnqueued() { return rx_max_enqueued; }
167
+  #endif  
137 168
 
138 169
     private:
139 170
       static void printNumber(unsigned long, const uint8_t);

+ 6
- 0
Marlin/Marlin_main.cpp View File

@@ -13329,6 +13329,12 @@ void loop() {
13329 13329
           // M29 closes the file
13330 13330
           card.closefile();
13331 13331
           SERIAL_PROTOCOLLNPGM(MSG_FILE_SAVED);
13332
+		#if ENABLED(SERIAL_STATS_DROPPED_RX)
13333
+		  SERIAL_ECHOLNPAIR("Dropped bytes: ", MarlinSerial::dropped());
13334
+		#endif
13335
+		#if ENABLED(SERIAL_STATS_MAX_RX_QUEUED)
13336
+		  SERIAL_ECHOLNPAIR("Max RX Queue Size: ", MarlinSerial::rxMaxEnqueued());
13337
+		#endif  
13332 13338
           ok_to_send();
13333 13339
         }
13334 13340
         else {

Loading…
Cancel
Save