My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MarlinSerial.cpp 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 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 <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * MarlinSerial_Due.cpp - Hardware serial library for Arduino DUE
  24. * Copyright (c) 2017 Eduardo José Tagle. All right reserved
  25. * Based on MarlinSerial for AVR, copyright (c) 2006 Nicholas Zambetti. All right reserved.
  26. */
  27. #ifdef ARDUINO_ARCH_SAM
  28. #include "../../inc/MarlinConfig.h"
  29. #include "MarlinSerial.h"
  30. #include "InterruptVectors.h"
  31. #include "../../MarlinCore.h"
  32. template<typename Cfg> typename MarlinSerial<Cfg>::ring_buffer_r MarlinSerial<Cfg>::rx_buffer = { 0, 0, { 0 } };
  33. template<typename Cfg> typename MarlinSerial<Cfg>::ring_buffer_t MarlinSerial<Cfg>::tx_buffer = { 0 };
  34. template<typename Cfg> bool MarlinSerial<Cfg>::_written = false;
  35. template<typename Cfg> uint8_t MarlinSerial<Cfg>::xon_xoff_state = MarlinSerial<Cfg>::XON_XOFF_CHAR_SENT | MarlinSerial<Cfg>::XON_CHAR;
  36. template<typename Cfg> uint8_t MarlinSerial<Cfg>::rx_dropped_bytes = 0;
  37. template<typename Cfg> uint8_t MarlinSerial<Cfg>::rx_buffer_overruns = 0;
  38. template<typename Cfg> uint8_t MarlinSerial<Cfg>::rx_framing_errors = 0;
  39. template<typename Cfg> typename MarlinSerial<Cfg>::ring_buffer_pos_t MarlinSerial<Cfg>::rx_max_enqueued = 0;
  40. // A SW memory barrier, to ensure GCC does not overoptimize loops
  41. #define sw_barrier() asm volatile("": : :"memory");
  42. #include "../../feature/e_parser.h"
  43. // (called with RX interrupts disabled)
  44. template<typename Cfg>
  45. FORCE_INLINE void MarlinSerial<Cfg>::store_rxd_char() {
  46. static EmergencyParser::State emergency_state; // = EP_RESET
  47. // Get the tail - Nothing can alter its value while we are at this ISR
  48. const ring_buffer_pos_t t = rx_buffer.tail;
  49. // Get the head pointer
  50. ring_buffer_pos_t h = rx_buffer.head;
  51. // Get the next element
  52. ring_buffer_pos_t i = (ring_buffer_pos_t)(h + 1) & (ring_buffer_pos_t)(Cfg::RX_SIZE - 1);
  53. // Read the character from the USART
  54. uint8_t c = HWUART->UART_RHR;
  55. if (Cfg::EMERGENCYPARSER) emergency_parser.update(emergency_state, c);
  56. // If the character is to be stored at the index just before the tail
  57. // (such that the head would advance to the current tail), the RX FIFO is
  58. // full, so don't write the character or advance the head.
  59. if (i != t) {
  60. rx_buffer.buffer[h] = c;
  61. h = i;
  62. }
  63. else if (Cfg::DROPPED_RX && !++rx_dropped_bytes)
  64. --rx_dropped_bytes;
  65. const ring_buffer_pos_t rx_count = (ring_buffer_pos_t)(h - t) & (ring_buffer_pos_t)(Cfg::RX_SIZE - 1);
  66. // Calculate count of bytes stored into the RX buffer
  67. // Keep track of the maximum count of enqueued bytes
  68. if (Cfg::MAX_RX_QUEUED) NOLESS(rx_max_enqueued, rx_count);
  69. if (Cfg::XONOFF) {
  70. // If the last char that was sent was an XON
  71. if ((xon_xoff_state & XON_XOFF_CHAR_MASK) == XON_CHAR) {
  72. // Bytes stored into the RX buffer
  73. const ring_buffer_pos_t rx_count = (ring_buffer_pos_t)(h - t) & (ring_buffer_pos_t)(Cfg::RX_SIZE - 1);
  74. // If over 12.5% of RX buffer capacity, send XOFF before running out of
  75. // RX buffer space .. 325 bytes @ 250kbits/s needed to let the host react
  76. // and stop sending bytes. This translates to 13mS propagation time.
  77. if (rx_count >= (Cfg::RX_SIZE) / 8) {
  78. // At this point, definitely no TX interrupt was executing, since the TX isr can't be preempted.
  79. // Don't enable the TX interrupt here as a means to trigger the XOFF char, because if it happens
  80. // to be in the middle of trying to disable the RX interrupt in the main program, eventually the
  81. // enabling of the TX interrupt could be undone. The ONLY reliable thing this can do to ensure
  82. // the sending of the XOFF char is to send it HERE AND NOW.
  83. // About to send the XOFF char
  84. xon_xoff_state = XOFF_CHAR | XON_XOFF_CHAR_SENT;
  85. // Wait until the TX register becomes empty and send it - Here there could be a problem
  86. // - While waiting for the TX register to empty, the RX register could receive a new
  87. // character. This must also handle that situation!
  88. uint32_t status;
  89. while (!((status = HWUART->UART_SR) & UART_SR_TXRDY)) {
  90. if (status & UART_SR_RXRDY) {
  91. // We received a char while waiting for the TX buffer to be empty - Receive and process it!
  92. i = (ring_buffer_pos_t)(h + 1) & (ring_buffer_pos_t)(Cfg::RX_SIZE - 1);
  93. // Read the character from the USART
  94. c = HWUART->UART_RHR;
  95. if (Cfg::EMERGENCYPARSER) emergency_parser.update(emergency_state, c);
  96. // If the character is to be stored at the index just before the tail
  97. // (such that the head would advance to the current tail), the FIFO is
  98. // full, so don't write the character or advance the head.
  99. if (i != t) {
  100. rx_buffer.buffer[h] = c;
  101. h = i;
  102. }
  103. else if (Cfg::DROPPED_RX && !++rx_dropped_bytes)
  104. --rx_dropped_bytes;
  105. }
  106. sw_barrier();
  107. }
  108. HWUART->UART_THR = XOFF_CHAR;
  109. // At this point there could be a race condition between the write() function
  110. // and this sending of the XOFF char. This interrupt could happen between the
  111. // wait to be empty TX buffer loop and the actual write of the character. Since
  112. // the TX buffer is full because it's sending the XOFF char, the only way to be
  113. // sure the write() function will succeed is to wait for the XOFF char to be
  114. // completely sent. Since an extra character could be received during the wait
  115. // it must also be handled!
  116. while (!((status = HWUART->UART_SR) & UART_SR_TXRDY)) {
  117. if (status & UART_SR_RXRDY) {
  118. // A char arrived while waiting for the TX buffer to be empty - Receive and process it!
  119. i = (ring_buffer_pos_t)(h + 1) & (ring_buffer_pos_t)(Cfg::RX_SIZE - 1);
  120. // Read the character from the USART
  121. c = HWUART->UART_RHR;
  122. if (Cfg::EMERGENCYPARSER) emergency_parser.update(emergency_state, c);
  123. // If the character is to be stored at the index just before the tail
  124. // (such that the head would advance to the current tail), the FIFO is
  125. // full, so don't write the character or advance the head.
  126. if (i != t) {
  127. rx_buffer.buffer[h] = c;
  128. h = i;
  129. }
  130. else if (Cfg::DROPPED_RX && !++rx_dropped_bytes)
  131. --rx_dropped_bytes;
  132. }
  133. sw_barrier();
  134. }
  135. // At this point everything is ready. The write() function won't
  136. // have any issues writing to the UART TX register if it needs to!
  137. }
  138. }
  139. }
  140. // Store the new head value
  141. rx_buffer.head = h;
  142. }
  143. template<typename Cfg>
  144. FORCE_INLINE void MarlinSerial<Cfg>::_tx_thr_empty_irq() {
  145. if (Cfg::TX_SIZE > 0) {
  146. // Read positions
  147. uint8_t t = tx_buffer.tail;
  148. const uint8_t h = tx_buffer.head;
  149. if (Cfg::XONOFF) {
  150. // If an XON char is pending to be sent, do it now
  151. if (xon_xoff_state == XON_CHAR) {
  152. // Send the character
  153. HWUART->UART_THR = XON_CHAR;
  154. // Remember we sent it.
  155. xon_xoff_state = XON_CHAR | XON_XOFF_CHAR_SENT;
  156. // If nothing else to transmit, just disable TX interrupts.
  157. if (h == t) HWUART->UART_IDR = UART_IDR_TXRDY;
  158. return;
  159. }
  160. }
  161. // If nothing to transmit, just disable TX interrupts. This could
  162. // happen as the result of the non atomicity of the disabling of RX
  163. // interrupts that could end reenabling TX interrupts as a side effect.
  164. if (h == t) {
  165. HWUART->UART_IDR = UART_IDR_TXRDY;
  166. return;
  167. }
  168. // There is something to TX, Send the next byte
  169. const uint8_t c = tx_buffer.buffer[t];
  170. t = (t + 1) & (Cfg::TX_SIZE - 1);
  171. HWUART->UART_THR = c;
  172. tx_buffer.tail = t;
  173. // Disable interrupts if there is nothing to transmit following this byte
  174. if (h == t) HWUART->UART_IDR = UART_IDR_TXRDY;
  175. }
  176. }
  177. template<typename Cfg>
  178. void MarlinSerial<Cfg>::UART_ISR() {
  179. const uint32_t status = HWUART->UART_SR;
  180. // Data received?
  181. if (status & UART_SR_RXRDY) store_rxd_char();
  182. if (Cfg::TX_SIZE > 0) {
  183. // Something to send, and TX interrupts are enabled (meaning something to send)?
  184. if ((status & UART_SR_TXRDY) && (HWUART->UART_IMR & UART_IMR_TXRDY)) _tx_thr_empty_irq();
  185. }
  186. // Acknowledge errors
  187. if ((status & UART_SR_OVRE) || (status & UART_SR_FRAME)) {
  188. if (Cfg::DROPPED_RX && (status & UART_SR_OVRE) && !++rx_dropped_bytes) --rx_dropped_bytes;
  189. if (Cfg::RX_OVERRUNS && (status & UART_SR_OVRE) && !++rx_buffer_overruns) --rx_buffer_overruns;
  190. if (Cfg::RX_FRAMING_ERRORS && (status & UART_SR_FRAME) && !++rx_framing_errors) --rx_framing_errors;
  191. // TODO: error reporting outside ISR
  192. HWUART->UART_CR = UART_CR_RSTSTA;
  193. }
  194. }
  195. // Public Methods
  196. template<typename Cfg>
  197. void MarlinSerial<Cfg>::begin(const long baud_setting) {
  198. // Disable UART interrupt in NVIC
  199. NVIC_DisableIRQ( HWUART_IRQ );
  200. // We NEED memory barriers to ensure Interrupts are actually disabled!
  201. // ( https://dzone.com/articles/nvic-disabling-interrupts-on-arm-cortex-m-and-the )
  202. __DSB();
  203. __ISB();
  204. // Disable clock
  205. pmc_disable_periph_clk( HWUART_IRQ_ID );
  206. // Configure PMC
  207. pmc_enable_periph_clk( HWUART_IRQ_ID );
  208. // Disable PDC channel
  209. HWUART->UART_PTCR = UART_PTCR_RXTDIS | UART_PTCR_TXTDIS;
  210. // Reset and disable receiver and transmitter
  211. HWUART->UART_CR = UART_CR_RSTRX | UART_CR_RSTTX | UART_CR_RXDIS | UART_CR_TXDIS;
  212. // Configure mode: 8bit, No parity, 1 bit stop
  213. HWUART->UART_MR = UART_MR_CHMODE_NORMAL | US_MR_CHRL_8_BIT | US_MR_NBSTOP_1_BIT | UART_MR_PAR_NO;
  214. // Configure baudrate (asynchronous, no oversampling)
  215. HWUART->UART_BRGR = (SystemCoreClock / (baud_setting << 4));
  216. // Configure interrupts
  217. HWUART->UART_IDR = 0xFFFFFFFF;
  218. HWUART->UART_IER = UART_IER_RXRDY | UART_IER_OVRE | UART_IER_FRAME;
  219. // Install interrupt handler
  220. install_isr(HWUART_IRQ, UART_ISR);
  221. // Configure priority. We need a very high priority to avoid losing characters
  222. // and we need to be able to preempt the Stepper ISR and everything else!
  223. // (this could probably be fixed by using DMA with the Serial port)
  224. NVIC_SetPriority(HWUART_IRQ, 1);
  225. // Enable UART interrupt in NVIC
  226. NVIC_EnableIRQ(HWUART_IRQ);
  227. // Enable receiver and transmitter
  228. HWUART->UART_CR = UART_CR_RXEN | UART_CR_TXEN;
  229. if (Cfg::TX_SIZE > 0) _written = false;
  230. }
  231. template<typename Cfg>
  232. void MarlinSerial<Cfg>::end() {
  233. // Disable UART interrupt in NVIC
  234. NVIC_DisableIRQ( HWUART_IRQ );
  235. // We NEED memory barriers to ensure Interrupts are actually disabled!
  236. // ( https://dzone.com/articles/nvic-disabling-interrupts-on-arm-cortex-m-and-the )
  237. __DSB();
  238. __ISB();
  239. pmc_disable_periph_clk( HWUART_IRQ_ID );
  240. }
  241. template<typename Cfg>
  242. int MarlinSerial<Cfg>::peek() {
  243. const int v = rx_buffer.head == rx_buffer.tail ? -1 : rx_buffer.buffer[rx_buffer.tail];
  244. return v;
  245. }
  246. template<typename Cfg>
  247. int MarlinSerial<Cfg>::read() {
  248. const ring_buffer_pos_t h = rx_buffer.head;
  249. ring_buffer_pos_t t = rx_buffer.tail;
  250. if (h == t) return -1;
  251. int v = rx_buffer.buffer[t];
  252. t = (ring_buffer_pos_t)(t + 1) & (Cfg::RX_SIZE - 1);
  253. // Advance tail
  254. rx_buffer.tail = t;
  255. if (Cfg::XONOFF) {
  256. // If the XOFF char was sent, or about to be sent...
  257. if ((xon_xoff_state & XON_XOFF_CHAR_MASK) == XOFF_CHAR) {
  258. // Get count of bytes in the RX buffer
  259. const ring_buffer_pos_t rx_count = (ring_buffer_pos_t)(h - t) & (ring_buffer_pos_t)(Cfg::RX_SIZE - 1);
  260. // When below 10% of RX buffer capacity, send XON before running out of RX buffer bytes
  261. if (rx_count < (Cfg::RX_SIZE) / 10) {
  262. if (Cfg::TX_SIZE > 0) {
  263. // Signal we want an XON character to be sent.
  264. xon_xoff_state = XON_CHAR;
  265. // Enable TX isr.
  266. HWUART->UART_IER = UART_IER_TXRDY;
  267. }
  268. else {
  269. // If not using TX interrupts, we must send the XON char now
  270. xon_xoff_state = XON_CHAR | XON_XOFF_CHAR_SENT;
  271. while (!(HWUART->UART_SR & UART_SR_TXRDY)) sw_barrier();
  272. HWUART->UART_THR = XON_CHAR;
  273. }
  274. }
  275. }
  276. }
  277. return v;
  278. }
  279. template<typename Cfg>
  280. typename MarlinSerial<Cfg>::ring_buffer_pos_t MarlinSerial<Cfg>::available() {
  281. const ring_buffer_pos_t h = rx_buffer.head, t = rx_buffer.tail;
  282. return (ring_buffer_pos_t)(Cfg::RX_SIZE + h - t) & (Cfg::RX_SIZE - 1);
  283. }
  284. template<typename Cfg>
  285. void MarlinSerial<Cfg>::flush() {
  286. rx_buffer.tail = rx_buffer.head;
  287. if (Cfg::XONOFF) {
  288. if ((xon_xoff_state & XON_XOFF_CHAR_MASK) == XOFF_CHAR) {
  289. if (Cfg::TX_SIZE > 0) {
  290. // Signal we want an XON character to be sent.
  291. xon_xoff_state = XON_CHAR;
  292. // Enable TX isr.
  293. HWUART->UART_IER = UART_IER_TXRDY;
  294. }
  295. else {
  296. // If not using TX interrupts, we must send the XON char now
  297. xon_xoff_state = XON_CHAR | XON_XOFF_CHAR_SENT;
  298. while (!(HWUART->UART_SR & UART_SR_TXRDY)) sw_barrier();
  299. HWUART->UART_THR = XON_CHAR;
  300. }
  301. }
  302. }
  303. }
  304. template<typename Cfg>
  305. void MarlinSerial<Cfg>::write(const uint8_t c) {
  306. _written = true;
  307. if (Cfg::TX_SIZE == 0) {
  308. while (!(HWUART->UART_SR & UART_SR_TXRDY)) sw_barrier();
  309. HWUART->UART_THR = c;
  310. }
  311. else {
  312. // If the TX interrupts are disabled and the data register
  313. // is empty, just write the byte to the data register and
  314. // be done. This shortcut helps significantly improve the
  315. // effective datarate at high (>500kbit/s) bitrates, where
  316. // interrupt overhead becomes a slowdown.
  317. // Yes, there is a race condition between the sending of the
  318. // XOFF char at the RX isr, but it is properly handled there
  319. if (!(HWUART->UART_IMR & UART_IMR_TXRDY) && (HWUART->UART_SR & UART_SR_TXRDY)) {
  320. HWUART->UART_THR = c;
  321. return;
  322. }
  323. const uint8_t i = (tx_buffer.head + 1) & (Cfg::TX_SIZE - 1);
  324. // If global interrupts are disabled (as the result of being called from an ISR)...
  325. if (!ISRS_ENABLED()) {
  326. // Make room by polling if it is possible to transmit, and do so!
  327. while (i == tx_buffer.tail) {
  328. // If we can transmit another byte, do it.
  329. if (HWUART->UART_SR & UART_SR_TXRDY) _tx_thr_empty_irq();
  330. // Make sure compiler rereads tx_buffer.tail
  331. sw_barrier();
  332. }
  333. }
  334. else {
  335. // Interrupts are enabled, just wait until there is space
  336. while (i == tx_buffer.tail) sw_barrier();
  337. }
  338. // Store new char. head is always safe to move
  339. tx_buffer.buffer[tx_buffer.head] = c;
  340. tx_buffer.head = i;
  341. // Enable TX isr - Non atomic, but it will eventually enable TX isr
  342. HWUART->UART_IER = UART_IER_TXRDY;
  343. }
  344. }
  345. template<typename Cfg>
  346. void MarlinSerial<Cfg>::flushTX() {
  347. // TX
  348. if (Cfg::TX_SIZE == 0) {
  349. // No bytes written, no need to flush. This special case is needed since there's
  350. // no way to force the TXC (transmit complete) bit to 1 during initialization.
  351. if (!_written) return;
  352. // Wait until everything was transmitted
  353. while (!(HWUART->UART_SR & UART_SR_TXEMPTY)) sw_barrier();
  354. // At this point nothing is queued anymore (DRIE is disabled) and
  355. // the hardware finished transmission (TXC is set).
  356. }
  357. else {
  358. // If we have never written a byte, no need to flush. This special
  359. // case is needed since there is no way to force the TXC (transmit
  360. // complete) bit to 1 during initialization
  361. if (!_written) return;
  362. // If global interrupts are disabled (as the result of being called from an ISR)...
  363. if (!ISRS_ENABLED()) {
  364. // Wait until everything was transmitted - We must do polling, as interrupts are disabled
  365. while (tx_buffer.head != tx_buffer.tail || !(HWUART->UART_SR & UART_SR_TXEMPTY)) {
  366. // If there is more space, send an extra character
  367. if (HWUART->UART_SR & UART_SR_TXRDY) _tx_thr_empty_irq();
  368. sw_barrier();
  369. }
  370. }
  371. else {
  372. // Wait until everything was transmitted
  373. while (tx_buffer.head != tx_buffer.tail || !(HWUART->UART_SR & UART_SR_TXEMPTY)) sw_barrier();
  374. }
  375. // At this point nothing is queued anymore (DRIE is disabled) and
  376. // the hardware finished transmission (TXC is set).
  377. }
  378. }
  379. /**
  380. * Imports from print.h
  381. */
  382. template<typename Cfg>
  383. void MarlinSerial<Cfg>::print(char c, int base) {
  384. print((long)c, base);
  385. }
  386. template<typename Cfg>
  387. void MarlinSerial<Cfg>::print(unsigned char b, int base) {
  388. print((unsigned long)b, base);
  389. }
  390. template<typename Cfg>
  391. void MarlinSerial<Cfg>::print(int n, int base) {
  392. print((long)n, base);
  393. }
  394. template<typename Cfg>
  395. void MarlinSerial<Cfg>::print(unsigned int n, int base) {
  396. print((unsigned long)n, base);
  397. }
  398. template<typename Cfg>
  399. void MarlinSerial<Cfg>::print(long n, int base) {
  400. if (base == 0) write(n);
  401. else if (base == 10) {
  402. if (n < 0) { print('-'); n = -n; }
  403. printNumber(n, 10);
  404. }
  405. else
  406. printNumber(n, base);
  407. }
  408. template<typename Cfg>
  409. void MarlinSerial<Cfg>::print(unsigned long n, int base) {
  410. if (base == 0) write(n);
  411. else printNumber(n, base);
  412. }
  413. template<typename Cfg>
  414. void MarlinSerial<Cfg>::print(double n, int digits) {
  415. printFloat(n, digits);
  416. }
  417. template<typename Cfg>
  418. void MarlinSerial<Cfg>::println() {
  419. print('\r');
  420. print('\n');
  421. }
  422. template<typename Cfg>
  423. void MarlinSerial<Cfg>::println(const String& s) {
  424. print(s);
  425. println();
  426. }
  427. template<typename Cfg>
  428. void MarlinSerial<Cfg>::println(const char c[]) {
  429. print(c);
  430. println();
  431. }
  432. template<typename Cfg>
  433. void MarlinSerial<Cfg>::println(char c, int base) {
  434. print(c, base);
  435. println();
  436. }
  437. template<typename Cfg>
  438. void MarlinSerial<Cfg>::println(unsigned char b, int base) {
  439. print(b, base);
  440. println();
  441. }
  442. template<typename Cfg>
  443. void MarlinSerial<Cfg>::println(int n, int base) {
  444. print(n, base);
  445. println();
  446. }
  447. template<typename Cfg>
  448. void MarlinSerial<Cfg>::println(unsigned int n, int base) {
  449. print(n, base);
  450. println();
  451. }
  452. template<typename Cfg>
  453. void MarlinSerial<Cfg>::println(long n, int base) {
  454. print(n, base);
  455. println();
  456. }
  457. template<typename Cfg>
  458. void MarlinSerial<Cfg>::println(unsigned long n, int base) {
  459. print(n, base);
  460. println();
  461. }
  462. template<typename Cfg>
  463. void MarlinSerial<Cfg>::println(double n, int digits) {
  464. print(n, digits);
  465. println();
  466. }
  467. // Private Methods
  468. template<typename Cfg>
  469. void MarlinSerial<Cfg>::printNumber(unsigned long n, uint8_t base) {
  470. if (n) {
  471. unsigned char buf[8 * sizeof(long)]; // Enough space for base 2
  472. int8_t i = 0;
  473. while (n) {
  474. buf[i++] = n % base;
  475. n /= base;
  476. }
  477. while (i--)
  478. print((char)(buf[i] + (buf[i] < 10 ? '0' : 'A' - 10)));
  479. }
  480. else
  481. print('0');
  482. }
  483. template<typename Cfg>
  484. void MarlinSerial<Cfg>::printFloat(double number, uint8_t digits) {
  485. // Handle negative numbers
  486. if (number < 0.0) {
  487. print('-');
  488. number = -number;
  489. }
  490. // Round correctly so that print(1.999, 2) prints as "2.00"
  491. double rounding = 0.5;
  492. LOOP_L_N(i, digits) rounding *= 0.1;
  493. number += rounding;
  494. // Extract the integer part of the number and print it
  495. unsigned long int_part = (unsigned long)number;
  496. double remainder = number - (double)int_part;
  497. print(int_part);
  498. // Print the decimal point, but only if there are digits beyond
  499. if (digits) {
  500. print('.');
  501. // Extract digits from the remainder one at a time
  502. while (digits--) {
  503. remainder *= 10.0;
  504. int toPrint = int(remainder);
  505. print(toPrint);
  506. remainder -= toPrint;
  507. }
  508. }
  509. }
  510. // If not using the USB port as serial port
  511. #if SERIAL_PORT >= 0
  512. template class MarlinSerial<MarlinSerialCfg<SERIAL_PORT>>; // Define
  513. MarlinSerial<MarlinSerialCfg<SERIAL_PORT>> customizedSerial1; // Instantiate
  514. #endif
  515. #if defined(SERIAL_PORT_2) && SERIAL_PORT_2 >= 0
  516. template class MarlinSerial<MarlinSerialCfg<SERIAL_PORT_2>>; // Define
  517. MarlinSerial<MarlinSerialCfg<SERIAL_PORT_2>> customizedSerial2; // Instantiate
  518. #endif
  519. #endif // ARDUINO_ARCH_SAM