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