|
@@ -0,0 +1,680 @@
|
|
1
|
+/**
|
|
2
|
+ * Marlin 3D Printer Firmware
|
|
3
|
+ * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
|
4
|
+ *
|
|
5
|
+ * Based on Sprinter and grbl.
|
|
6
|
+ * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
|
|
7
|
+ *
|
|
8
|
+ * This program is free software: you can redistribute it and/or modify
|
|
9
|
+ * it under the terms of the GNU General Public License as published by
|
|
10
|
+ * the Free Software Foundation, either version 3 of the License, or
|
|
11
|
+ * (at your option) any later version.
|
|
12
|
+ *
|
|
13
|
+ * This program is distributed in the hope that it will be useful,
|
|
14
|
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
15
|
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
16
|
+ * GNU General Public License for more details.
|
|
17
|
+ *
|
|
18
|
+ * You should have received a copy of the GNU General Public License
|
|
19
|
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
20
|
+ *
|
|
21
|
+ */
|
|
22
|
+
|
|
23
|
+/**
|
|
24
|
+ * MarlinSerial_Due.cpp - Hardware serial library for Arduino DUE
|
|
25
|
+ * Copyright (c) 2017 Eduardo José Tagle. All right reserved
|
|
26
|
+ * Based on MarlinSerial for AVR, copyright (c) 2006 Nicholas Zambetti. All right reserved.
|
|
27
|
+ */
|
|
28
|
+#ifdef ARDUINO_ARCH_SAM
|
|
29
|
+
|
|
30
|
+#include "../../inc/MarlinConfig.h"
|
|
31
|
+
|
|
32
|
+#include "MarlinSerial_Due.h"
|
|
33
|
+#include "InterruptVectors_Due.h"
|
|
34
|
+#include "../../Marlin.h"
|
|
35
|
+
|
|
36
|
+// Based on selected port, use the proper configuration
|
|
37
|
+#if SERIAL_PORT == 0
|
|
38
|
+ #define HWUART UART
|
|
39
|
+ #define HWUART_IRQ UART_IRQn
|
|
40
|
+ #define HWUART_IRQ_ID ID_UART
|
|
41
|
+#elif SERIAL_PORT == 1
|
|
42
|
+ #define HWUART USART0
|
|
43
|
+ #define HWUART_IRQ USART0_IRQn
|
|
44
|
+ #define HWUART_IRQ_ID ID_USART0
|
|
45
|
+#elif SERIAL_PORT == 2
|
|
46
|
+ #define HWUART USART1
|
|
47
|
+ #define HWUART_IRQ USART1_IRQn
|
|
48
|
+ #define HWUART_IRQ_ID ID_USART1
|
|
49
|
+#elif SERIAL_PORT == 3
|
|
50
|
+ #define HWUART USART3
|
|
51
|
+ #define HWUART_IRQ USART3_IRQn
|
|
52
|
+ #define HWUART_IRQ_ID ID_USART3
|
|
53
|
+#endif
|
|
54
|
+
|
|
55
|
+struct ring_buffer_r {
|
|
56
|
+ unsigned char buffer[RX_BUFFER_SIZE];
|
|
57
|
+ volatile ring_buffer_pos_t head, tail;
|
|
58
|
+};
|
|
59
|
+
|
|
60
|
+#if TX_BUFFER_SIZE > 0
|
|
61
|
+ struct ring_buffer_t {
|
|
62
|
+ unsigned char buffer[TX_BUFFER_SIZE];
|
|
63
|
+ volatile uint8_t head, tail;
|
|
64
|
+ };
|
|
65
|
+#endif
|
|
66
|
+
|
|
67
|
+ring_buffer_r rx_buffer = { { 0 }, 0, 0 };
|
|
68
|
+#if TX_BUFFER_SIZE > 0
|
|
69
|
+ ring_buffer_t tx_buffer = { { 0 }, 0, 0 };
|
|
70
|
+ static bool _written;
|
|
71
|
+#endif
|
|
72
|
+
|
|
73
|
+#if ENABLED(SERIAL_XON_XOFF)
|
|
74
|
+ constexpr uint8_t XON_XOFF_CHAR_SENT = 0x80; // XON / XOFF Character was sent
|
|
75
|
+ constexpr uint8_t XON_XOFF_CHAR_MASK = 0x1F; // XON / XOFF character to send
|
|
76
|
+ // XON / XOFF character definitions
|
|
77
|
+ constexpr uint8_t XON_CHAR = 17;
|
|
78
|
+ constexpr uint8_t XOFF_CHAR = 19;
|
|
79
|
+ uint8_t xon_xoff_state = XON_XOFF_CHAR_SENT | XON_CHAR;
|
|
80
|
+
|
|
81
|
+ // Validate that RX buffer size is at least 4096 bytes- According to several experiments, on
|
|
82
|
+ // the original Arduino Due that uses a ATmega16U2 as USB to serial bridge, due to the introduced
|
|
83
|
+ // latencies, at least 2959 bytes of RX buffering (when transmitting at 250kbits/s) are required
|
|
84
|
+ // to avoid overflows.
|
|
85
|
+
|
|
86
|
+ #if RX_BUFFER_SIZE < 4096
|
|
87
|
+ #error Arduino DUE requires at least 4096 bytes of RX buffer to avoid buffer overflows when using XON/XOFF handshake
|
|
88
|
+ #endif
|
|
89
|
+#endif
|
|
90
|
+
|
|
91
|
+#if ENABLED(SERIAL_STATS_DROPPED_RX)
|
|
92
|
+ uint8_t rx_dropped_bytes = 0;
|
|
93
|
+#endif
|
|
94
|
+
|
|
95
|
+#if ENABLED(SERIAL_STATS_MAX_RX_QUEUED)
|
|
96
|
+ ring_buffer_pos_t rx_max_enqueued = 0;
|
|
97
|
+#endif
|
|
98
|
+
|
|
99
|
+// A SW memory barrier, to ensure GCC does not overoptimize loops
|
|
100
|
+#define sw_barrier() asm volatile("": : :"memory");
|
|
101
|
+
|
|
102
|
+#if ENABLED(EMERGENCY_PARSER)
|
|
103
|
+
|
|
104
|
+ #include "../../module/stepper.h"
|
|
105
|
+
|
|
106
|
+ // Currently looking for: M108, M112, M410
|
|
107
|
+ // If you alter the parser please don't forget to update the capabilities in Conditionals_post.h
|
|
108
|
+
|
|
109
|
+ FORCE_INLINE void emergency_parser(const uint8_t c) {
|
|
110
|
+
|
|
111
|
+ static e_parser_state state = state_RESET;
|
|
112
|
+
|
|
113
|
+ switch (state) {
|
|
114
|
+ case state_RESET:
|
|
115
|
+ switch (c) {
|
|
116
|
+ case ' ': break;
|
|
117
|
+ case 'N': state = state_N; break;
|
|
118
|
+ case 'M': state = state_M; break;
|
|
119
|
+ default: state = state_IGNORE;
|
|
120
|
+ }
|
|
121
|
+ break;
|
|
122
|
+
|
|
123
|
+ case state_N:
|
|
124
|
+ switch (c) {
|
|
125
|
+ case '0': case '1': case '2':
|
|
126
|
+ case '3': case '4': case '5':
|
|
127
|
+ case '6': case '7': case '8':
|
|
128
|
+ case '9': case '-': case ' ': break;
|
|
129
|
+ case 'M': state = state_M; break;
|
|
130
|
+ default: state = state_IGNORE;
|
|
131
|
+ }
|
|
132
|
+ break;
|
|
133
|
+
|
|
134
|
+ case state_M:
|
|
135
|
+ switch (c) {
|
|
136
|
+ case ' ': break;
|
|
137
|
+ case '1': state = state_M1; break;
|
|
138
|
+ case '4': state = state_M4; break;
|
|
139
|
+ default: state = state_IGNORE;
|
|
140
|
+ }
|
|
141
|
+ break;
|
|
142
|
+
|
|
143
|
+ case state_M1:
|
|
144
|
+ switch (c) {
|
|
145
|
+ case '0': state = state_M10; break;
|
|
146
|
+ case '1': state = state_M11; break;
|
|
147
|
+ default: state = state_IGNORE;
|
|
148
|
+ }
|
|
149
|
+ break;
|
|
150
|
+
|
|
151
|
+ case state_M10:
|
|
152
|
+ state = (c == '8') ? state_M108 : state_IGNORE;
|
|
153
|
+ break;
|
|
154
|
+
|
|
155
|
+ case state_M11:
|
|
156
|
+ state = (c == '2') ? state_M112 : state_IGNORE;
|
|
157
|
+ break;
|
|
158
|
+
|
|
159
|
+ case state_M4:
|
|
160
|
+ state = (c == '1') ? state_M41 : state_IGNORE;
|
|
161
|
+ break;
|
|
162
|
+
|
|
163
|
+ case state_M41:
|
|
164
|
+ state = (c == '0') ? state_M410 : state_IGNORE;
|
|
165
|
+ break;
|
|
166
|
+
|
|
167
|
+ case state_IGNORE:
|
|
168
|
+ if (c == '\n') state = state_RESET;
|
|
169
|
+ break;
|
|
170
|
+
|
|
171
|
+ default:
|
|
172
|
+ if (c == '\n') {
|
|
173
|
+ switch (state) {
|
|
174
|
+ case state_M108:
|
|
175
|
+ wait_for_user = wait_for_heatup = false;
|
|
176
|
+ break;
|
|
177
|
+ case state_M112:
|
|
178
|
+ kill(PSTR(MSG_KILLED));
|
|
179
|
+ break;
|
|
180
|
+ case state_M410:
|
|
181
|
+ quickstop_stepper();
|
|
182
|
+ break;
|
|
183
|
+ default:
|
|
184
|
+ break;
|
|
185
|
+ }
|
|
186
|
+ state = state_RESET;
|
|
187
|
+ }
|
|
188
|
+ }
|
|
189
|
+ }
|
|
190
|
+
|
|
191
|
+#endif // EMERGENCY_PARSER
|
|
192
|
+
|
|
193
|
+FORCE_INLINE void store_rxd_char() {
|
|
194
|
+
|
|
195
|
+ const ring_buffer_pos_t h = rx_buffer.head,
|
|
196
|
+ i = (ring_buffer_pos_t)(h + 1) & (ring_buffer_pos_t)(RX_BUFFER_SIZE - 1);
|
|
197
|
+
|
|
198
|
+ // Read the character
|
|
199
|
+ const uint8_t c = HWUART->UART_RHR;
|
|
200
|
+
|
|
201
|
+ // If the character is to be stored at the index just before the tail
|
|
202
|
+ // (such that the head would advance to the current tail), the buffer is
|
|
203
|
+ // critical, so don't write the character or advance the head.
|
|
204
|
+ if (i != rx_buffer.tail) {
|
|
205
|
+ rx_buffer.buffer[h] = c;
|
|
206
|
+ rx_buffer.head = i;
|
|
207
|
+ }
|
|
208
|
+ #if ENABLED(SERIAL_STATS_DROPPED_RX)
|
|
209
|
+ else if (!++rx_dropped_bytes) ++rx_dropped_bytes;
|
|
210
|
+ #endif
|
|
211
|
+
|
|
212
|
+#if ENABLED(SERIAL_STATS_MAX_RX_QUEUED)
|
|
213
|
+ // calculate count of bytes stored into the RX buffer
|
|
214
|
+ ring_buffer_pos_t rx_count = (ring_buffer_pos_t)(rx_buffer.head - rx_buffer.tail) & (ring_buffer_pos_t)(RX_BUFFER_SIZE - 1);
|
|
215
|
+ // Keep track of the maximum count of enqueued bytes
|
|
216
|
+ NOLESS(rx_max_enqueued, rx_count);
|
|
217
|
+#endif
|
|
218
|
+
|
|
219
|
+#if ENABLED(SERIAL_XON_XOFF)
|
|
220
|
+
|
|
221
|
+ // for high speed transfers, we can use XON/XOFF protocol to do
|
|
222
|
+ // software handshake and avoid overruns.
|
|
223
|
+ if ((xon_xoff_state & XON_XOFF_CHAR_MASK) == XON_CHAR) {
|
|
224
|
+
|
|
225
|
+ // calculate count of bytes stored into the RX buffer
|
|
226
|
+ ring_buffer_pos_t rx_count = (ring_buffer_pos_t)(rx_buffer.head - rx_buffer.tail) & (ring_buffer_pos_t)(RX_BUFFER_SIZE - 1);
|
|
227
|
+
|
|
228
|
+ // if we are above 12.5% of RX buffer capacity, send XOFF before
|
|
229
|
+ // we run out of RX buffer space .. We need 325 bytes @ 250kbits/s to
|
|
230
|
+ // let the host react and stop sending bytes. This translates to 13mS
|
|
231
|
+ // propagation time.
|
|
232
|
+ if (rx_count >= (RX_BUFFER_SIZE) / 8) {
|
|
233
|
+ // If TX interrupts are disabled and data register is empty,
|
|
234
|
+ // just write the byte to the data register and be done. This
|
|
235
|
+ // shortcut helps significantly improve the effective datarate
|
|
236
|
+ // at high (>500kbit/s) bitrates, where interrupt overhead
|
|
237
|
+ // becomes a slowdown.
|
|
238
|
+ if (!(HWUART->UART_IMR & UART_IMR_TXRDY) && (HWUART->UART_SR & UART_SR_TXRDY)) {
|
|
239
|
+ // Send an XOFF character
|
|
240
|
+ HWUART->UART_THR = XOFF_CHAR;
|
|
241
|
+
|
|
242
|
+ // And remember it was sent
|
|
243
|
+ xon_xoff_state = XOFF_CHAR | XON_XOFF_CHAR_SENT;
|
|
244
|
+ }
|
|
245
|
+ else {
|
|
246
|
+ // TX interrupts disabled, but buffer still not empty ... or
|
|
247
|
+ // TX interrupts enabled. Reenable TX ints and schedule XOFF
|
|
248
|
+ // character to be sent
|
|
249
|
+ #if TX_BUFFER_SIZE > 0
|
|
250
|
+ HWUART->UART_IER = UART_IER_TXRDY;
|
|
251
|
+ xon_xoff_state = XOFF_CHAR;
|
|
252
|
+ #else
|
|
253
|
+ // We are not using TX interrupts, we will have to send this manually
|
|
254
|
+ while (!(HWUART->UART_SR & UART_SR_TXRDY)) { sw_barrier(); };
|
|
255
|
+ HWUART->UART_THR = XOFF_CHAR;
|
|
256
|
+ // And remember we already sent it
|
|
257
|
+ xon_xoff_state = XOFF_CHAR | XON_XOFF_CHAR_SENT;
|
|
258
|
+ #endif
|
|
259
|
+ }
|
|
260
|
+ }
|
|
261
|
+ }
|
|
262
|
+#endif // SERIAL_XON_XOFF
|
|
263
|
+
|
|
264
|
+#if ENABLED(EMERGENCY_PARSER)
|
|
265
|
+ emergency_parser(c);
|
|
266
|
+#endif
|
|
267
|
+}
|
|
268
|
+
|
|
269
|
+#if TX_BUFFER_SIZE > 0
|
|
270
|
+
|
|
271
|
+ FORCE_INLINE void _tx_thr_empty_irq(void) {
|
|
272
|
+ // If interrupts are enabled, there must be more data in the output
|
|
273
|
+ // buffer.
|
|
274
|
+
|
|
275
|
+ #if ENABLED(SERIAL_XON_XOFF)
|
|
276
|
+ // Do a priority insertion of an XON/XOFF char, if needed.
|
|
277
|
+ const uint8_t state = xon_xoff_state;
|
|
278
|
+ if (!(state & XON_XOFF_CHAR_SENT)) {
|
|
279
|
+ HWUART->UART_THR = state & XON_XOFF_CHAR_MASK;
|
|
280
|
+ xon_xoff_state = state | XON_XOFF_CHAR_SENT;
|
|
281
|
+ }
|
|
282
|
+ else
|
|
283
|
+ #endif
|
|
284
|
+ { // Send the next byte
|
|
285
|
+ const uint8_t t = tx_buffer.tail, c = tx_buffer.buffer[t];
|
|
286
|
+ tx_buffer.tail = (t + 1) & (TX_BUFFER_SIZE - 1);
|
|
287
|
+ HWUART->UART_THR = c;
|
|
288
|
+ }
|
|
289
|
+
|
|
290
|
+ // Disable interrupts if the buffer is empty
|
|
291
|
+ if (tx_buffer.head == tx_buffer.tail)
|
|
292
|
+ HWUART->UART_IDR = UART_IDR_TXRDY;
|
|
293
|
+ }
|
|
294
|
+
|
|
295
|
+#endif // TX_BUFFER_SIZE
|
|
296
|
+
|
|
297
|
+static void UART_ISR(void) {
|
|
298
|
+ uint32_t status = HWUART->UART_SR;
|
|
299
|
+
|
|
300
|
+ // Did we receive data?
|
|
301
|
+ if (status & UART_SR_RXRDY)
|
|
302
|
+ store_rxd_char();
|
|
303
|
+
|
|
304
|
+ #if TX_BUFFER_SIZE > 0
|
|
305
|
+ // Do we have something to send, and TX interrupts are enabled (meaning something to send) ?
|
|
306
|
+ if ((status & UART_SR_TXRDY) && (HWUART->UART_IMR & UART_IMR_TXRDY))
|
|
307
|
+ _tx_thr_empty_irq();
|
|
308
|
+ #endif
|
|
309
|
+
|
|
310
|
+ // Acknowledge errors
|
|
311
|
+ if ((status & UART_SR_OVRE) || (status & UART_SR_FRAME)) {
|
|
312
|
+ // TODO: error reporting outside ISR
|
|
313
|
+ HWUART->UART_CR = UART_CR_RSTSTA;
|
|
314
|
+ }
|
|
315
|
+}
|
|
316
|
+
|
|
317
|
+// Public Methods
|
|
318
|
+
|
|
319
|
+void MarlinSerial::begin(const long baud_setting) {
|
|
320
|
+
|
|
321
|
+ // Disable UART interrupt in NVIC
|
|
322
|
+ NVIC_DisableIRQ( HWUART_IRQ );
|
|
323
|
+
|
|
324
|
+ // Disable clock
|
|
325
|
+ pmc_disable_periph_clk( HWUART_IRQ_ID );
|
|
326
|
+
|
|
327
|
+ // Configure PMC
|
|
328
|
+ pmc_enable_periph_clk( HWUART_IRQ_ID );
|
|
329
|
+
|
|
330
|
+ // Disable PDC channel
|
|
331
|
+ HWUART->UART_PTCR = UART_PTCR_RXTDIS | UART_PTCR_TXTDIS;
|
|
332
|
+
|
|
333
|
+ // Reset and disable receiver and transmitter
|
|
334
|
+ HWUART->UART_CR = UART_CR_RSTRX | UART_CR_RSTTX | UART_CR_RXDIS | UART_CR_TXDIS;
|
|
335
|
+
|
|
336
|
+ // Configure mode: 8bit, No parity, 1 bit stop
|
|
337
|
+ HWUART->UART_MR = UART_MR_CHMODE_NORMAL | US_MR_CHRL_8_BIT | US_MR_NBSTOP_1_BIT | UART_MR_PAR_NO;
|
|
338
|
+
|
|
339
|
+ // Configure baudrate (asynchronous, no oversampling)
|
|
340
|
+ HWUART->UART_BRGR = (SystemCoreClock / (baud_setting << 4));
|
|
341
|
+
|
|
342
|
+ // Configure interrupts
|
|
343
|
+ HWUART->UART_IDR = 0xFFFFFFFF;
|
|
344
|
+ HWUART->UART_IER = UART_IER_RXRDY | UART_IER_OVRE | UART_IER_FRAME;
|
|
345
|
+
|
|
346
|
+ // Install interrupt handler
|
|
347
|
+ install_isr(HWUART_IRQ, UART_ISR);
|
|
348
|
+
|
|
349
|
+ // Enable UART interrupt in NVIC
|
|
350
|
+ NVIC_EnableIRQ(HWUART_IRQ);
|
|
351
|
+
|
|
352
|
+ // Enable receiver and transmitter
|
|
353
|
+ HWUART->UART_CR = UART_CR_RXEN | UART_CR_TXEN;
|
|
354
|
+
|
|
355
|
+ #if TX_BUFFER_SIZE > 0
|
|
356
|
+ _written = false;
|
|
357
|
+ #endif
|
|
358
|
+}
|
|
359
|
+
|
|
360
|
+void MarlinSerial::end() {
|
|
361
|
+ // Disable UART interrupt in NVIC
|
|
362
|
+ NVIC_DisableIRQ( HWUART_IRQ );
|
|
363
|
+
|
|
364
|
+ pmc_disable_periph_clk( HWUART_IRQ_ID );
|
|
365
|
+}
|
|
366
|
+
|
|
367
|
+void MarlinSerial::checkRx(void) {
|
|
368
|
+ if (HWUART->UART_SR & UART_SR_RXRDY) {
|
|
369
|
+ CRITICAL_SECTION_START;
|
|
370
|
+ store_rxd_char();
|
|
371
|
+ CRITICAL_SECTION_END;
|
|
372
|
+ }
|
|
373
|
+}
|
|
374
|
+
|
|
375
|
+int MarlinSerial::peek(void) {
|
|
376
|
+ CRITICAL_SECTION_START;
|
|
377
|
+ const int v = rx_buffer.head == rx_buffer.tail ? -1 : rx_buffer.buffer[rx_buffer.tail];
|
|
378
|
+ CRITICAL_SECTION_END;
|
|
379
|
+ return v;
|
|
380
|
+}
|
|
381
|
+
|
|
382
|
+int MarlinSerial::read(void) {
|
|
383
|
+ int v;
|
|
384
|
+ CRITICAL_SECTION_START;
|
|
385
|
+ const ring_buffer_pos_t t = rx_buffer.tail;
|
|
386
|
+ if (rx_buffer.head == t)
|
|
387
|
+ v = -1;
|
|
388
|
+ else {
|
|
389
|
+ v = rx_buffer.buffer[t];
|
|
390
|
+ rx_buffer.tail = (ring_buffer_pos_t)(t + 1) & (RX_BUFFER_SIZE - 1);
|
|
391
|
+
|
|
392
|
+ #if ENABLED(SERIAL_XON_XOFF)
|
|
393
|
+ if ((xon_xoff_state & XON_XOFF_CHAR_MASK) == XOFF_CHAR) {
|
|
394
|
+ // Get count of bytes in the RX buffer
|
|
395
|
+ ring_buffer_pos_t rx_count = (ring_buffer_pos_t)(rx_buffer.head - rx_buffer.tail) & (ring_buffer_pos_t)(RX_BUFFER_SIZE - 1);
|
|
396
|
+ // When below 10% of RX buffer capacity, send XON before
|
|
397
|
+ // running out of RX buffer bytes
|
|
398
|
+ if (rx_count < (RX_BUFFER_SIZE) / 10) {
|
|
399
|
+ xon_xoff_state = XON_CHAR | XON_XOFF_CHAR_SENT;
|
|
400
|
+ CRITICAL_SECTION_END; // End critical section before returning!
|
|
401
|
+ writeNoHandshake(XON_CHAR);
|
|
402
|
+ return v;
|
|
403
|
+ }
|
|
404
|
+ }
|
|
405
|
+ #endif
|
|
406
|
+ }
|
|
407
|
+ CRITICAL_SECTION_END;
|
|
408
|
+ return v;
|
|
409
|
+}
|
|
410
|
+
|
|
411
|
+ring_buffer_pos_t MarlinSerial::available(void) {
|
|
412
|
+ CRITICAL_SECTION_START;
|
|
413
|
+ const ring_buffer_pos_t h = rx_buffer.head, t = rx_buffer.tail;
|
|
414
|
+ CRITICAL_SECTION_END;
|
|
415
|
+ return (ring_buffer_pos_t)(RX_BUFFER_SIZE + h - t) & (RX_BUFFER_SIZE - 1);
|
|
416
|
+}
|
|
417
|
+
|
|
418
|
+void MarlinSerial::flush(void) {
|
|
419
|
+ // Don't change this order of operations. If the RX interrupt occurs between
|
|
420
|
+ // reading rx_buffer_head and updating rx_buffer_tail, the previous rx_buffer_head
|
|
421
|
+ // may be written to rx_buffer_tail, making the buffer appear full rather than empty.
|
|
422
|
+ CRITICAL_SECTION_START;
|
|
423
|
+ rx_buffer.head = rx_buffer.tail;
|
|
424
|
+ CRITICAL_SECTION_END;
|
|
425
|
+
|
|
426
|
+#if ENABLED(SERIAL_XON_XOFF)
|
|
427
|
+ if ((xon_xoff_state & XON_XOFF_CHAR_MASK) == XOFF_CHAR) {
|
|
428
|
+ xon_xoff_state = XON_CHAR | XON_XOFF_CHAR_SENT;
|
|
429
|
+ writeNoHandshake(XON_CHAR);
|
|
430
|
+ }
|
|
431
|
+#endif
|
|
432
|
+}
|
|
433
|
+
|
|
434
|
+#if TX_BUFFER_SIZE > 0
|
|
435
|
+ uint8_t MarlinSerial::availableForWrite(void) {
|
|
436
|
+ CRITICAL_SECTION_START;
|
|
437
|
+ const uint8_t h = tx_buffer.head, t = tx_buffer.tail;
|
|
438
|
+ CRITICAL_SECTION_END;
|
|
439
|
+ return (uint8_t)(TX_BUFFER_SIZE + h - t) & (TX_BUFFER_SIZE - 1);
|
|
440
|
+ }
|
|
441
|
+
|
|
442
|
+ void MarlinSerial::write(const uint8_t c) {
|
|
443
|
+ #if ENABLED(SERIAL_XON_XOFF)
|
|
444
|
+ const uint8_t state = xon_xoff_state;
|
|
445
|
+ if (!(state & XON_XOFF_CHAR_SENT)) {
|
|
446
|
+ // Send 2 chars: XON/XOFF, then a user-specified char
|
|
447
|
+ writeNoHandshake(state & XON_XOFF_CHAR_MASK);
|
|
448
|
+ xon_xoff_state = state | XON_XOFF_CHAR_SENT;
|
|
449
|
+ }
|
|
450
|
+ #endif
|
|
451
|
+ writeNoHandshake(c);
|
|
452
|
+ }
|
|
453
|
+
|
|
454
|
+ void MarlinSerial::writeNoHandshake(const uint8_t c) {
|
|
455
|
+ _written = true;
|
|
456
|
+ CRITICAL_SECTION_START;
|
|
457
|
+ bool emty = (tx_buffer.head == tx_buffer.tail);
|
|
458
|
+ CRITICAL_SECTION_END;
|
|
459
|
+ // If the buffer and the data register is empty, just write the byte
|
|
460
|
+ // to the data register and be done. This shortcut helps
|
|
461
|
+ // significantly improve the effective datarate at high (>
|
|
462
|
+ // 500kbit/s) bitrates, where interrupt overhead becomes a slowdown.
|
|
463
|
+ if (emty && (HWUART->UART_SR & UART_SR_TXRDY)) {
|
|
464
|
+ CRITICAL_SECTION_START;
|
|
465
|
+ HWUART->UART_THR = c;
|
|
466
|
+ HWUART->UART_IER = UART_IER_TXRDY;
|
|
467
|
+ CRITICAL_SECTION_END;
|
|
468
|
+ return;
|
|
469
|
+ }
|
|
470
|
+ const uint8_t i = (tx_buffer.head + 1) & (TX_BUFFER_SIZE - 1);
|
|
471
|
+
|
|
472
|
+ // If the output buffer is full, there's nothing for it other than to
|
|
473
|
+ // wait for the interrupt handler to empty it a bit
|
|
474
|
+ while (i == tx_buffer.tail) {
|
|
475
|
+ if (__get_PRIMASK()) {
|
|
476
|
+ // Interrupts are disabled, so we'll have to poll the data
|
|
477
|
+ // register empty flag ourselves. If it is set, pretend an
|
|
478
|
+ // interrupt has happened and call the handler to free up
|
|
479
|
+ // space for us.
|
|
480
|
+ if (HWUART->UART_SR & UART_SR_TXRDY)
|
|
481
|
+ _tx_thr_empty_irq();
|
|
482
|
+ }
|
|
483
|
+ else {
|
|
484
|
+ // nop, the interrupt handler will free up space for us
|
|
485
|
+ }
|
|
486
|
+ sw_barrier();
|
|
487
|
+ }
|
|
488
|
+
|
|
489
|
+ tx_buffer.buffer[tx_buffer.head] = c;
|
|
490
|
+ { CRITICAL_SECTION_START;
|
|
491
|
+ tx_buffer.head = i;
|
|
492
|
+ HWUART->UART_IER = UART_IER_TXRDY;
|
|
493
|
+ CRITICAL_SECTION_END;
|
|
494
|
+ }
|
|
495
|
+ return;
|
|
496
|
+ }
|
|
497
|
+
|
|
498
|
+ void MarlinSerial::flushTX(void) {
|
|
499
|
+ // TX
|
|
500
|
+ // If we have never written a byte, no need to flush.
|
|
501
|
+ if (!_written)
|
|
502
|
+ return;
|
|
503
|
+
|
|
504
|
+ while ((HWUART->UART_IMR & UART_IMR_TXRDY) || !(HWUART->UART_SR & UART_SR_TXEMPTY)) {
|
|
505
|
+ if (__get_PRIMASK())
|
|
506
|
+ if ((HWUART->UART_SR & UART_SR_TXRDY))
|
|
507
|
+ _tx_thr_empty_irq();
|
|
508
|
+ sw_barrier();
|
|
509
|
+ }
|
|
510
|
+ // If we get here, nothing is queued anymore (TX interrupts are disabled) and
|
|
511
|
+ // the hardware finished tranmission (TXEMPTY is set).
|
|
512
|
+ }
|
|
513
|
+
|
|
514
|
+#else // TX_BUFFER_SIZE == 0
|
|
515
|
+
|
|
516
|
+ void MarlinSerial::write(const uint8_t c) {
|
|
517
|
+ #if ENABLED(SERIAL_XON_XOFF)
|
|
518
|
+ // Do a priority insertion of an XON/XOFF char, if needed.
|
|
519
|
+ const uint8_t state = xon_xoff_state;
|
|
520
|
+ if (!(state & XON_XOFF_CHAR_SENT)) {
|
|
521
|
+ writeNoHandshake(state & XON_XOFF_CHAR_MASK);
|
|
522
|
+ xon_xoff_state = state | XON_XOFF_CHAR_SENT;
|
|
523
|
+ }
|
|
524
|
+ #endif
|
|
525
|
+ writeNoHandshake(c);
|
|
526
|
+ }
|
|
527
|
+
|
|
528
|
+ void MarlinSerial::writeNoHandshake(const uint8_t c) {
|
|
529
|
+ while (!(HWUART->UART_SR & UART_SR_TXRDY)) { sw_barrier(); };
|
|
530
|
+ HWUART->UART_THR = c;
|
|
531
|
+ }
|
|
532
|
+
|
|
533
|
+#endif // TX_BUFFER_SIZE == 0
|
|
534
|
+
|
|
535
|
+/**
|
|
536
|
+* Imports from print.h
|
|
537
|
+*/
|
|
538
|
+
|
|
539
|
+void MarlinSerial::print(char c, int base) {
|
|
540
|
+ print((long)c, base);
|
|
541
|
+}
|
|
542
|
+
|
|
543
|
+void MarlinSerial::print(unsigned char b, int base) {
|
|
544
|
+ print((unsigned long)b, base);
|
|
545
|
+}
|
|
546
|
+
|
|
547
|
+void MarlinSerial::print(int n, int base) {
|
|
548
|
+ print((long)n, base);
|
|
549
|
+}
|
|
550
|
+
|
|
551
|
+void MarlinSerial::print(unsigned int n, int base) {
|
|
552
|
+ print((unsigned long)n, base);
|
|
553
|
+}
|
|
554
|
+
|
|
555
|
+void MarlinSerial::print(long n, int base) {
|
|
556
|
+ if (base == 0)
|
|
557
|
+ write(n);
|
|
558
|
+ else if (base == 10) {
|
|
559
|
+ if (n < 0) {
|
|
560
|
+ print('-');
|
|
561
|
+ n = -n;
|
|
562
|
+ }
|
|
563
|
+ printNumber(n, 10);
|
|
564
|
+ }
|
|
565
|
+ else
|
|
566
|
+ printNumber(n, base);
|
|
567
|
+}
|
|
568
|
+
|
|
569
|
+void MarlinSerial::print(unsigned long n, int base) {
|
|
570
|
+ if (base == 0) write(n);
|
|
571
|
+ else printNumber(n, base);
|
|
572
|
+}
|
|
573
|
+
|
|
574
|
+void MarlinSerial::print(double n, int digits) {
|
|
575
|
+ printFloat(n, digits);
|
|
576
|
+}
|
|
577
|
+
|
|
578
|
+void MarlinSerial::println(void) {
|
|
579
|
+ print('\r');
|
|
580
|
+ print('\n');
|
|
581
|
+}
|
|
582
|
+
|
|
583
|
+void MarlinSerial::println(const String& s) {
|
|
584
|
+ print(s);
|
|
585
|
+ println();
|
|
586
|
+}
|
|
587
|
+
|
|
588
|
+void MarlinSerial::println(const char c[]) {
|
|
589
|
+ print(c);
|
|
590
|
+ println();
|
|
591
|
+}
|
|
592
|
+
|
|
593
|
+void MarlinSerial::println(char c, int base) {
|
|
594
|
+ print(c, base);
|
|
595
|
+ println();
|
|
596
|
+}
|
|
597
|
+
|
|
598
|
+void MarlinSerial::println(unsigned char b, int base) {
|
|
599
|
+ print(b, base);
|
|
600
|
+ println();
|
|
601
|
+}
|
|
602
|
+
|
|
603
|
+void MarlinSerial::println(int n, int base) {
|
|
604
|
+ print(n, base);
|
|
605
|
+ println();
|
|
606
|
+}
|
|
607
|
+
|
|
608
|
+void MarlinSerial::println(unsigned int n, int base) {
|
|
609
|
+ print(n, base);
|
|
610
|
+ println();
|
|
611
|
+}
|
|
612
|
+
|
|
613
|
+void MarlinSerial::println(long n, int base) {
|
|
614
|
+ print(n, base);
|
|
615
|
+ println();
|
|
616
|
+}
|
|
617
|
+
|
|
618
|
+void MarlinSerial::println(unsigned long n, int base) {
|
|
619
|
+ print(n, base);
|
|
620
|
+ println();
|
|
621
|
+}
|
|
622
|
+
|
|
623
|
+void MarlinSerial::println(double n, int digits) {
|
|
624
|
+ print(n, digits);
|
|
625
|
+ println();
|
|
626
|
+}
|
|
627
|
+
|
|
628
|
+// Private Methods
|
|
629
|
+
|
|
630
|
+void MarlinSerial::printNumber(unsigned long n, uint8_t base) {
|
|
631
|
+ if (n) {
|
|
632
|
+ unsigned char buf[8 * sizeof(long)]; // Enough space for base 2
|
|
633
|
+ int8_t i = 0;
|
|
634
|
+ while (n) {
|
|
635
|
+ buf[i++] = n % base;
|
|
636
|
+ n /= base;
|
|
637
|
+ }
|
|
638
|
+ while (i--)
|
|
639
|
+ print((char)(buf[i] + (buf[i] < 10 ? '0' : 'A' - 10)));
|
|
640
|
+ }
|
|
641
|
+ else
|
|
642
|
+ print('0');
|
|
643
|
+}
|
|
644
|
+
|
|
645
|
+void MarlinSerial::printFloat(double number, uint8_t digits) {
|
|
646
|
+ // Handle negative numbers
|
|
647
|
+ if (number < 0.0) {
|
|
648
|
+ print('-');
|
|
649
|
+ number = -number;
|
|
650
|
+ }
|
|
651
|
+
|
|
652
|
+ // Round correctly so that print(1.999, 2) prints as "2.00"
|
|
653
|
+ double rounding = 0.5;
|
|
654
|
+ for (uint8_t i = 0; i < digits; ++i)
|
|
655
|
+ rounding *= 0.1;
|
|
656
|
+
|
|
657
|
+ number += rounding;
|
|
658
|
+
|
|
659
|
+ // Extract the integer part of the number and print it
|
|
660
|
+ unsigned long int_part = (unsigned long)number;
|
|
661
|
+ double remainder = number - (double)int_part;
|
|
662
|
+ print(int_part);
|
|
663
|
+
|
|
664
|
+ // Print the decimal point, but only if there are digits beyond
|
|
665
|
+ if (digits) {
|
|
666
|
+ print('.');
|
|
667
|
+ // Extract digits from the remainder one at a time
|
|
668
|
+ while (digits--) {
|
|
669
|
+ remainder *= 10.0;
|
|
670
|
+ int toPrint = int(remainder);
|
|
671
|
+ print(toPrint);
|
|
672
|
+ remainder -= toPrint;
|
|
673
|
+ }
|
|
674
|
+ }
|
|
675
|
+}
|
|
676
|
+
|
|
677
|
+// Preinstantiate
|
|
678
|
+MarlinSerial customizedSerial;
|
|
679
|
+
|
|
680
|
+#endif // ARDUINO_ARCH_SAM
|