|
@@ -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;
|