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 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  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.cpp - Hardware serial library for Wiring
  24. * Copyright (c) 2006 Nicholas Zambetti. All right reserved.
  25. *
  26. * Modified 23 November 2006 by David A. Mellis
  27. * Modified 28 September 2010 by Mark Sproul
  28. * Modified 14 February 2016 by Andreas Hardtung (added tx buffer)
  29. * Modified 01 October 2017 by Eduardo José Tagle (added XON/XOFF)
  30. * Modified 10 June 2018 by Eduardo José Tagle (See #10991)
  31. * Templatized 01 October 2018 by Eduardo José Tagle to allow multiple instances
  32. */
  33. #ifdef __AVR__
  34. // Disable HardwareSerial.cpp to support chips without a UART (Attiny, etc.)
  35. #include "../../inc/MarlinConfig.h"
  36. #if !defined(USBCON) && (defined(UBRRH) || defined(UBRR0H) || defined(UBRR1H) || defined(UBRR2H) || defined(UBRR3H))
  37. #include "MarlinSerial.h"
  38. #include "../../MarlinCore.h"
  39. #if ENABLED(DIRECT_STEPPING)
  40. #include "../../feature/direct_stepping.h"
  41. #endif
  42. template<typename Cfg> typename MarlinSerial<Cfg>::ring_buffer_r MarlinSerial<Cfg>::rx_buffer = { 0, 0, { 0 } };
  43. template<typename Cfg> typename MarlinSerial<Cfg>::ring_buffer_t MarlinSerial<Cfg>::tx_buffer = { 0 };
  44. template<typename Cfg> bool MarlinSerial<Cfg>::_written = false;
  45. template<typename Cfg> uint8_t MarlinSerial<Cfg>::xon_xoff_state = MarlinSerial<Cfg>::XON_XOFF_CHAR_SENT | MarlinSerial<Cfg>::XON_CHAR;
  46. template<typename Cfg> uint8_t MarlinSerial<Cfg>::rx_dropped_bytes = 0;
  47. template<typename Cfg> uint8_t MarlinSerial<Cfg>::rx_buffer_overruns = 0;
  48. template<typename Cfg> uint8_t MarlinSerial<Cfg>::rx_framing_errors = 0;
  49. template<typename Cfg> typename MarlinSerial<Cfg>::ring_buffer_pos_t MarlinSerial<Cfg>::rx_max_enqueued = 0;
  50. // A SW memory barrier, to ensure GCC does not overoptimize loops
  51. #define sw_barrier() asm volatile("": : :"memory");
  52. #include "../../feature/e_parser.h"
  53. // "Atomically" read the RX head index value without disabling interrupts:
  54. // This MUST be called with RX interrupts enabled, and CAN'T be called
  55. // from the RX ISR itself!
  56. template<typename Cfg>
  57. FORCE_INLINE typename MarlinSerial<Cfg>::ring_buffer_pos_t MarlinSerial<Cfg>::atomic_read_rx_head() {
  58. if (Cfg::RX_SIZE > 256) {
  59. // Keep reading until 2 consecutive reads return the same value,
  60. // meaning there was no update in-between caused by an interrupt.
  61. // This works because serial RX interrupts happen at a slower rate
  62. // than successive reads of a variable, so 2 consecutive reads with
  63. // the same value means no interrupt updated it.
  64. ring_buffer_pos_t vold, vnew = rx_buffer.head;
  65. sw_barrier();
  66. do {
  67. vold = vnew;
  68. vnew = rx_buffer.head;
  69. sw_barrier();
  70. } while (vold != vnew);
  71. return vnew;
  72. }
  73. else {
  74. // With an 8bit index, reads are always atomic. No need for special handling
  75. return rx_buffer.head;
  76. }
  77. }
  78. template<typename Cfg>
  79. volatile bool MarlinSerial<Cfg>::rx_tail_value_not_stable = false;
  80. template<typename Cfg>
  81. volatile uint16_t MarlinSerial<Cfg>::rx_tail_value_backup = 0;
  82. // Set RX tail index, taking into account the RX ISR could interrupt
  83. // the write to this variable in the middle - So a backup strategy
  84. // is used to ensure reads of the correct values.
  85. // -Must NOT be called from the RX ISR -
  86. template<typename Cfg>
  87. FORCE_INLINE void MarlinSerial<Cfg>::atomic_set_rx_tail(typename MarlinSerial<Cfg>::ring_buffer_pos_t value) {
  88. if (Cfg::RX_SIZE > 256) {
  89. // Store the new value in the backup
  90. rx_tail_value_backup = value;
  91. sw_barrier();
  92. // Flag we are about to change the true value
  93. rx_tail_value_not_stable = true;
  94. sw_barrier();
  95. // Store the new value
  96. rx_buffer.tail = value;
  97. sw_barrier();
  98. // Signal the new value is completely stored into the value
  99. rx_tail_value_not_stable = false;
  100. sw_barrier();
  101. }
  102. else
  103. rx_buffer.tail = value;
  104. }
  105. // Get the RX tail index, taking into account the read could be
  106. // interrupting in the middle of the update of that index value
  107. // -Called from the RX ISR -
  108. template<typename Cfg>
  109. FORCE_INLINE typename MarlinSerial<Cfg>::ring_buffer_pos_t MarlinSerial<Cfg>::atomic_read_rx_tail() {
  110. if (Cfg::RX_SIZE > 256) {
  111. // If the true index is being modified, return the backup value
  112. if (rx_tail_value_not_stable) return rx_tail_value_backup;
  113. }
  114. // The true index is stable, return it
  115. return rx_buffer.tail;
  116. }
  117. // (called with RX interrupts disabled)
  118. template<typename Cfg>
  119. FORCE_INLINE void MarlinSerial<Cfg>::store_rxd_char() {
  120. static EmergencyParser::State emergency_state; // = EP_RESET
  121. // This must read the R_UCSRA register before reading the received byte to detect error causes
  122. if (Cfg::DROPPED_RX && B_DOR && !++rx_dropped_bytes) --rx_dropped_bytes;
  123. if (Cfg::RX_OVERRUNS && B_DOR && !++rx_buffer_overruns) --rx_buffer_overruns;
  124. if (Cfg::RX_FRAMING_ERRORS && B_FE && !++rx_framing_errors) --rx_framing_errors;
  125. // Read the character from the USART
  126. uint8_t c = R_UDR;
  127. #if ENABLED(DIRECT_STEPPING)
  128. if (page_manager.maybe_store_rxd_char(c)) return;
  129. #endif
  130. // Get the tail - Nothing can alter its value while this ISR is executing, but there's
  131. // a chance that this ISR interrupted the main process while it was updating the index.
  132. // The backup mechanism ensures the correct value is always returned.
  133. const ring_buffer_pos_t t = atomic_read_rx_tail();
  134. // Get the head pointer - This ISR is the only one that modifies its value, so it's safe to read here
  135. ring_buffer_pos_t h = rx_buffer.head;
  136. // Get the next element
  137. ring_buffer_pos_t i = (ring_buffer_pos_t)(h + 1) & (ring_buffer_pos_t)(Cfg::RX_SIZE - 1);
  138. if (Cfg::EMERGENCYPARSER) emergency_parser.update(emergency_state, c);
  139. // If the character is to be stored at the index just before the tail
  140. // (such that the head would advance to the current tail), the RX FIFO is
  141. // full, so don't write the character or advance the head.
  142. if (i != t) {
  143. rx_buffer.buffer[h] = c;
  144. h = i;
  145. }
  146. else if (Cfg::DROPPED_RX && !++rx_dropped_bytes)
  147. --rx_dropped_bytes;
  148. if (Cfg::MAX_RX_QUEUED) {
  149. // Calculate count of bytes stored into the RX buffer
  150. const ring_buffer_pos_t rx_count = (ring_buffer_pos_t)(h - t) & (ring_buffer_pos_t)(Cfg::RX_SIZE - 1);
  151. // Keep track of the maximum count of enqueued bytes
  152. NOLESS(rx_max_enqueued, rx_count);
  153. }
  154. if (Cfg::XONOFF) {
  155. // If the last char that was sent was an XON
  156. if ((xon_xoff_state & XON_XOFF_CHAR_MASK) == XON_CHAR) {
  157. // Bytes stored into the RX buffer
  158. const ring_buffer_pos_t rx_count = (ring_buffer_pos_t)(h - t) & (ring_buffer_pos_t)(Cfg::RX_SIZE - 1);
  159. // If over 12.5% of RX buffer capacity, send XOFF before running out of
  160. // RX buffer space .. 325 bytes @ 250kbits/s needed to let the host react
  161. // and stop sending bytes. This translates to 13mS propagation time.
  162. if (rx_count >= (Cfg::RX_SIZE) / 8) {
  163. // At this point, definitely no TX interrupt was executing, since the TX ISR can't be preempted.
  164. // Don't enable the TX interrupt here as a means to trigger the XOFF char, because if it happens
  165. // to be in the middle of trying to disable the RX interrupt in the main program, eventually the
  166. // enabling of the TX interrupt could be undone. The ONLY reliable thing this can do to ensure
  167. // the sending of the XOFF char is to send it HERE AND NOW.
  168. // About to send the XOFF char
  169. xon_xoff_state = XOFF_CHAR | XON_XOFF_CHAR_SENT;
  170. // Wait until the TX register becomes empty and send it - Here there could be a problem
  171. // - While waiting for the TX register to empty, the RX register could receive a new
  172. // character. This must also handle that situation!
  173. while (!B_UDRE) {
  174. if (B_RXC) {
  175. // A char arrived while waiting for the TX buffer to be empty - Receive and process it!
  176. i = (ring_buffer_pos_t)(h + 1) & (ring_buffer_pos_t)(Cfg::RX_SIZE - 1);
  177. // Read the character from the USART
  178. c = R_UDR;
  179. if (Cfg::EMERGENCYPARSER) emergency_parser.update(emergency_state, c);
  180. // If the character is to be stored at the index just before the tail
  181. // (such that the head would advance to the current tail), the FIFO is
  182. // full, so don't write the character or advance the head.
  183. if (i != t) {
  184. rx_buffer.buffer[h] = c;
  185. h = i;
  186. }
  187. else if (Cfg::DROPPED_RX && !++rx_dropped_bytes)
  188. --rx_dropped_bytes;
  189. }
  190. sw_barrier();
  191. }
  192. R_UDR = XOFF_CHAR;
  193. // Clear the TXC bit -- "can be cleared by writing a one to its bit
  194. // location". This makes sure flush() won't return until the bytes
  195. // actually got written
  196. B_TXC = 1;
  197. // At this point there could be a race condition between the write() function
  198. // and this sending of the XOFF char. This interrupt could happen between the
  199. // wait to be empty TX buffer loop and the actual write of the character. Since
  200. // the TX buffer is full because it's sending the XOFF char, the only way to be
  201. // sure the write() function will succeed is to wait for the XOFF char to be
  202. // completely sent. Since an extra character could be received during the wait
  203. // it must also be handled!
  204. while (!B_UDRE) {
  205. if (B_RXC) {
  206. // A char arrived while waiting for the TX buffer to be empty - Receive and process it!
  207. i = (ring_buffer_pos_t)(h + 1) & (ring_buffer_pos_t)(Cfg::RX_SIZE - 1);
  208. // Read the character from the USART
  209. c = R_UDR;
  210. if (Cfg::EMERGENCYPARSER)
  211. emergency_parser.update(emergency_state, c);
  212. // If the character is to be stored at the index just before the tail
  213. // (such that the head would advance to the current tail), the FIFO is
  214. // full, so don't write the character or advance the head.
  215. if (i != t) {
  216. rx_buffer.buffer[h] = c;
  217. h = i;
  218. }
  219. else if (Cfg::DROPPED_RX && !++rx_dropped_bytes)
  220. --rx_dropped_bytes;
  221. }
  222. sw_barrier();
  223. }
  224. // At this point everything is ready. The write() function won't
  225. // have any issues writing to the UART TX register if it needs to!
  226. }
  227. }
  228. }
  229. // Store the new head value - The main loop will retry until the value is stable
  230. rx_buffer.head = h;
  231. }
  232. // (called with TX irqs disabled)
  233. template<typename Cfg>
  234. FORCE_INLINE void MarlinSerial<Cfg>::_tx_udr_empty_irq() {
  235. if (Cfg::TX_SIZE > 0) {
  236. // Read positions
  237. uint8_t t = tx_buffer.tail;
  238. const uint8_t h = tx_buffer.head;
  239. if (Cfg::XONOFF) {
  240. // If an XON char is pending to be sent, do it now
  241. if (xon_xoff_state == XON_CHAR) {
  242. // Send the character
  243. R_UDR = XON_CHAR;
  244. // clear the TXC bit -- "can be cleared by writing a one to its bit
  245. // location". This makes sure flush() won't return until the bytes
  246. // actually got written
  247. B_TXC = 1;
  248. // Remember we sent it.
  249. xon_xoff_state = XON_CHAR | XON_XOFF_CHAR_SENT;
  250. // If nothing else to transmit, just disable TX interrupts.
  251. if (h == t) B_UDRIE = 0; // (Non-atomic, could be reenabled by the main program, but eventually this will succeed)
  252. return;
  253. }
  254. }
  255. // If nothing to transmit, just disable TX interrupts. This could
  256. // happen as the result of the non atomicity of the disabling of RX
  257. // interrupts that could end reenabling TX interrupts as a side effect.
  258. if (h == t) {
  259. B_UDRIE = 0; // (Non-atomic, could be reenabled by the main program, but eventually this will succeed)
  260. return;
  261. }
  262. // There is something to TX, Send the next byte
  263. const uint8_t c = tx_buffer.buffer[t];
  264. t = (t + 1) & (Cfg::TX_SIZE - 1);
  265. R_UDR = c;
  266. tx_buffer.tail = t;
  267. // Clear the TXC bit (by writing a one to its bit location).
  268. // Ensures flush() won't return until the bytes are actually written/
  269. B_TXC = 1;
  270. // Disable interrupts if there is nothing to transmit following this byte
  271. if (h == t) B_UDRIE = 0; // (Non-atomic, could be reenabled by the main program, but eventually this will succeed)
  272. }
  273. }
  274. // Public Methods
  275. template<typename Cfg>
  276. void MarlinSerial<Cfg>::begin(const long baud) {
  277. uint16_t baud_setting;
  278. bool useU2X = true;
  279. #if F_CPU == 16000000UL && SERIAL_PORT == 0
  280. // Hard-coded exception for compatibility with the bootloader shipped
  281. // with the Duemilanove and previous boards, and the firmware on the
  282. // 8U2 on the Uno and Mega 2560.
  283. if (baud == 57600) useU2X = false;
  284. #endif
  285. R_UCSRA = 0;
  286. if (useU2X) {
  287. B_U2X = 1;
  288. baud_setting = (F_CPU / 4 / baud - 1) / 2;
  289. }
  290. else
  291. baud_setting = (F_CPU / 8 / baud - 1) / 2;
  292. // assign the baud_setting, a.k.a. ubbr (USART Baud Rate Register)
  293. R_UBRRH = baud_setting >> 8;
  294. R_UBRRL = baud_setting;
  295. B_RXEN = 1;
  296. B_TXEN = 1;
  297. B_RXCIE = 1;
  298. if (Cfg::TX_SIZE > 0) B_UDRIE = 0;
  299. _written = false;
  300. }
  301. template<typename Cfg>
  302. void MarlinSerial<Cfg>::end() {
  303. B_RXEN = 0;
  304. B_TXEN = 0;
  305. B_RXCIE = 0;
  306. B_UDRIE = 0;
  307. }
  308. template<typename Cfg>
  309. int MarlinSerial<Cfg>::peek() {
  310. const ring_buffer_pos_t h = atomic_read_rx_head(), t = rx_buffer.tail;
  311. return h == t ? -1 : rx_buffer.buffer[t];
  312. }
  313. template<typename Cfg>
  314. int MarlinSerial<Cfg>::read() {
  315. const ring_buffer_pos_t h = atomic_read_rx_head();
  316. // Read the tail. Main thread owns it, so it is safe to directly read it
  317. ring_buffer_pos_t t = rx_buffer.tail;
  318. // If nothing to read, return now
  319. if (h == t) return -1;
  320. // Get the next char
  321. const int v = rx_buffer.buffer[t];
  322. t = (ring_buffer_pos_t)(t + 1) & (Cfg::RX_SIZE - 1);
  323. // Advance tail - Making sure the RX ISR will always get an stable value, even
  324. // if it interrupts the writing of the value of that variable in the middle.
  325. atomic_set_rx_tail(t);
  326. if (Cfg::XONOFF) {
  327. // If the XOFF char was sent, or about to be sent...
  328. if ((xon_xoff_state & XON_XOFF_CHAR_MASK) == XOFF_CHAR) {
  329. // Get count of bytes in the RX buffer
  330. const ring_buffer_pos_t rx_count = (ring_buffer_pos_t)(h - t) & (ring_buffer_pos_t)(Cfg::RX_SIZE - 1);
  331. if (rx_count < (Cfg::RX_SIZE) / 10) {
  332. if (Cfg::TX_SIZE > 0) {
  333. // Signal we want an XON character to be sent.
  334. xon_xoff_state = XON_CHAR;
  335. // Enable TX ISR. Non atomic, but it will eventually enable them
  336. B_UDRIE = 1;
  337. }
  338. else {
  339. // If not using TX interrupts, we must send the XON char now
  340. xon_xoff_state = XON_CHAR | XON_XOFF_CHAR_SENT;
  341. while (!B_UDRE) sw_barrier();
  342. R_UDR = XON_CHAR;
  343. }
  344. }
  345. }
  346. }
  347. return v;
  348. }
  349. template<typename Cfg>
  350. typename MarlinSerial<Cfg>::ring_buffer_pos_t MarlinSerial<Cfg>::available() {
  351. const ring_buffer_pos_t h = atomic_read_rx_head(), t = rx_buffer.tail;
  352. return (ring_buffer_pos_t)(Cfg::RX_SIZE + h - t) & (Cfg::RX_SIZE - 1);
  353. }
  354. template<typename Cfg>
  355. void MarlinSerial<Cfg>::flush() {
  356. // Set the tail to the head:
  357. // - Read the RX head index in a safe way. (See atomic_read_rx_head.)
  358. // - Set the tail, making sure the RX ISR will always get a stable value, even
  359. // if it interrupts the writing of the value of that variable in the middle.
  360. atomic_set_rx_tail(atomic_read_rx_head());
  361. if (Cfg::XONOFF) {
  362. // If the XOFF char was sent, or about to be sent...
  363. if ((xon_xoff_state & XON_XOFF_CHAR_MASK) == XOFF_CHAR) {
  364. if (Cfg::TX_SIZE > 0) {
  365. // Signal we want an XON character to be sent.
  366. xon_xoff_state = XON_CHAR;
  367. // Enable TX ISR. Non atomic, but it will eventually enable it.
  368. B_UDRIE = 1;
  369. }
  370. else {
  371. // If not using TX interrupts, we must send the XON char now
  372. xon_xoff_state = XON_CHAR | XON_XOFF_CHAR_SENT;
  373. while (!B_UDRE) sw_barrier();
  374. R_UDR = XON_CHAR;
  375. }
  376. }
  377. }
  378. }
  379. template<typename Cfg>
  380. void MarlinSerial<Cfg>::write(const uint8_t c) {
  381. if (Cfg::TX_SIZE == 0) {
  382. _written = true;
  383. while (!B_UDRE) sw_barrier();
  384. R_UDR = c;
  385. }
  386. else {
  387. _written = true;
  388. // If the TX interrupts are disabled and the data register
  389. // is empty, just write the byte to the data register and
  390. // be done. This shortcut helps significantly improve the
  391. // effective datarate at high (>500kbit/s) bitrates, where
  392. // interrupt overhead becomes a slowdown.
  393. // Yes, there is a race condition between the sending of the
  394. // XOFF char at the RX ISR, but it is properly handled there
  395. if (!B_UDRIE && B_UDRE) {
  396. R_UDR = c;
  397. // clear the TXC bit -- "can be cleared by writing a one to its bit
  398. // location". This makes sure flush() won't return until the bytes
  399. // actually got written
  400. B_TXC = 1;
  401. return;
  402. }
  403. const uint8_t i = (tx_buffer.head + 1) & (Cfg::TX_SIZE - 1);
  404. // If global interrupts are disabled (as the result of being called from an ISR)...
  405. if (!ISRS_ENABLED()) {
  406. // Make room by polling if it is possible to transmit, and do so!
  407. while (i == tx_buffer.tail) {
  408. // If we can transmit another byte, do it.
  409. if (B_UDRE) _tx_udr_empty_irq();
  410. // Make sure compiler rereads tx_buffer.tail
  411. sw_barrier();
  412. }
  413. }
  414. else {
  415. // Interrupts are enabled, just wait until there is space
  416. while (i == tx_buffer.tail) sw_barrier();
  417. }
  418. // Store new char. head is always safe to move
  419. tx_buffer.buffer[tx_buffer.head] = c;
  420. tx_buffer.head = i;
  421. // Enable TX ISR - Non atomic, but it will eventually enable TX ISR
  422. B_UDRIE = 1;
  423. }
  424. }
  425. template<typename Cfg>
  426. void MarlinSerial<Cfg>::flushTX() {
  427. if (Cfg::TX_SIZE == 0) {
  428. // No bytes written, no need to flush. This special case is needed since there's
  429. // no way to force the TXC (transmit complete) bit to 1 during initialization.
  430. if (!_written) return;
  431. // Wait until everything was transmitted
  432. while (!B_TXC) sw_barrier();
  433. // At this point nothing is queued anymore (DRIE is disabled) and
  434. // the hardware finished transmission (TXC is set).
  435. }
  436. else {
  437. // No bytes written, no need to flush. This special case is needed since there's
  438. // no way to force the TXC (transmit complete) bit to 1 during initialization.
  439. if (!_written) return;
  440. // If global interrupts are disabled (as the result of being called from an ISR)...
  441. if (!ISRS_ENABLED()) {
  442. // Wait until everything was transmitted - We must do polling, as interrupts are disabled
  443. while (tx_buffer.head != tx_buffer.tail || !B_TXC) {
  444. // If there is more space, send an extra character
  445. if (B_UDRE) _tx_udr_empty_irq();
  446. sw_barrier();
  447. }
  448. }
  449. else {
  450. // Wait until everything was transmitted
  451. while (tx_buffer.head != tx_buffer.tail || !B_TXC) sw_barrier();
  452. }
  453. // At this point nothing is queued anymore (DRIE is disabled) and
  454. // the hardware finished transmission (TXC is set).
  455. }
  456. }
  457. /**
  458. * Imports from print.h
  459. */
  460. template<typename Cfg>
  461. void MarlinSerial<Cfg>::print(char c, int base) {
  462. print((long)c, base);
  463. }
  464. template<typename Cfg>
  465. void MarlinSerial<Cfg>::print(unsigned char b, int base) {
  466. print((unsigned long)b, base);
  467. }
  468. template<typename Cfg>
  469. void MarlinSerial<Cfg>::print(int n, int base) {
  470. print((long)n, base);
  471. }
  472. template<typename Cfg>
  473. void MarlinSerial<Cfg>::print(unsigned int n, int base) {
  474. print((unsigned long)n, base);
  475. }
  476. template<typename Cfg>
  477. void MarlinSerial<Cfg>::print(long n, int base) {
  478. if (base == 0) write(n);
  479. else if (base == 10) {
  480. if (n < 0) { print('-'); n = -n; }
  481. printNumber(n, 10);
  482. }
  483. else
  484. printNumber(n, base);
  485. }
  486. template<typename Cfg>
  487. void MarlinSerial<Cfg>::print(unsigned long n, int base) {
  488. if (base == 0) write(n);
  489. else printNumber(n, base);
  490. }
  491. template<typename Cfg>
  492. void MarlinSerial<Cfg>::print(double n, int digits) {
  493. printFloat(n, digits);
  494. }
  495. template<typename Cfg>
  496. void MarlinSerial<Cfg>::println() {
  497. print('\r');
  498. print('\n');
  499. }
  500. template<typename Cfg>
  501. void MarlinSerial<Cfg>::println(const String& s) {
  502. print(s);
  503. println();
  504. }
  505. template<typename Cfg>
  506. void MarlinSerial<Cfg>::println(const char c[]) {
  507. print(c);
  508. println();
  509. }
  510. template<typename Cfg>
  511. void MarlinSerial<Cfg>::println(char c, int base) {
  512. print(c, base);
  513. println();
  514. }
  515. template<typename Cfg>
  516. void MarlinSerial<Cfg>::println(unsigned char b, int base) {
  517. print(b, base);
  518. println();
  519. }
  520. template<typename Cfg>
  521. void MarlinSerial<Cfg>::println(int n, int base) {
  522. print(n, base);
  523. println();
  524. }
  525. template<typename Cfg>
  526. void MarlinSerial<Cfg>::println(unsigned int n, int base) {
  527. print(n, base);
  528. println();
  529. }
  530. template<typename Cfg>
  531. void MarlinSerial<Cfg>::println(long n, int base) {
  532. print(n, base);
  533. println();
  534. }
  535. template<typename Cfg>
  536. void MarlinSerial<Cfg>::println(unsigned long n, int base) {
  537. print(n, base);
  538. println();
  539. }
  540. template<typename Cfg>
  541. void MarlinSerial<Cfg>::println(double n, int digits) {
  542. print(n, digits);
  543. println();
  544. }
  545. // Private Methods
  546. template<typename Cfg>
  547. void MarlinSerial<Cfg>::printNumber(unsigned long n, uint8_t base) {
  548. if (n) {
  549. unsigned char buf[8 * sizeof(long)]; // Enough space for base 2
  550. int8_t i = 0;
  551. while (n) {
  552. buf[i++] = n % base;
  553. n /= base;
  554. }
  555. while (i--)
  556. print((char)(buf[i] + (buf[i] < 10 ? '0' : 'A' - 10)));
  557. }
  558. else
  559. print('0');
  560. }
  561. template<typename Cfg>
  562. void MarlinSerial<Cfg>::printFloat(double number, uint8_t digits) {
  563. // Handle negative numbers
  564. if (number < 0.0) {
  565. print('-');
  566. number = -number;
  567. }
  568. // Round correctly so that print(1.999, 2) prints as "2.00"
  569. double rounding = 0.5;
  570. LOOP_L_N(i, digits) rounding *= 0.1;
  571. number += rounding;
  572. // Extract the integer part of the number and print it
  573. unsigned long int_part = (unsigned long)number;
  574. double remainder = number - (double)int_part;
  575. print(int_part);
  576. // Print the decimal point, but only if there are digits beyond
  577. if (digits) {
  578. print('.');
  579. // Extract digits from the remainder one at a time
  580. while (digits--) {
  581. remainder *= 10.0;
  582. int toPrint = int(remainder);
  583. print(toPrint);
  584. remainder -= toPrint;
  585. }
  586. }
  587. }
  588. // Hookup ISR handlers
  589. ISR(SERIAL_REGNAME(USART,SERIAL_PORT,_RX_vect)) {
  590. MarlinSerial<MarlinSerialCfg<SERIAL_PORT>>::store_rxd_char();
  591. }
  592. ISR(SERIAL_REGNAME(USART,SERIAL_PORT,_UDRE_vect)) {
  593. MarlinSerial<MarlinSerialCfg<SERIAL_PORT>>::_tx_udr_empty_irq();
  594. }
  595. // Preinstantiate
  596. template class MarlinSerial<MarlinSerialCfg<SERIAL_PORT>>;
  597. // Instantiate
  598. MarlinSerial<MarlinSerialCfg<SERIAL_PORT>> customizedSerial1;
  599. #ifdef SERIAL_PORT_2
  600. // Hookup ISR handlers
  601. ISR(SERIAL_REGNAME(USART,SERIAL_PORT_2,_RX_vect)) {
  602. MarlinSerial<MarlinSerialCfg<SERIAL_PORT_2>>::store_rxd_char();
  603. }
  604. ISR(SERIAL_REGNAME(USART,SERIAL_PORT_2,_UDRE_vect)) {
  605. MarlinSerial<MarlinSerialCfg<SERIAL_PORT_2>>::_tx_udr_empty_irq();
  606. }
  607. // Preinstantiate
  608. template class MarlinSerial<MarlinSerialCfg<SERIAL_PORT_2>>;
  609. // Instantiate
  610. MarlinSerial<MarlinSerialCfg<SERIAL_PORT_2>> customizedSerial2;
  611. #endif
  612. #endif // !USBCON && (UBRRH || UBRR0H || UBRR1H || UBRR2H || UBRR3H)
  613. #ifdef INTERNAL_SERIAL_PORT
  614. ISR(SERIAL_REGNAME(USART,INTERNAL_SERIAL_PORT,_RX_vect)) {
  615. MarlinSerial<MarlinInternalSerialCfg<INTERNAL_SERIAL_PORT>>::store_rxd_char();
  616. }
  617. ISR(SERIAL_REGNAME(USART,INTERNAL_SERIAL_PORT,_UDRE_vect)) {
  618. MarlinSerial<MarlinInternalSerialCfg<INTERNAL_SERIAL_PORT>>::_tx_udr_empty_irq();
  619. }
  620. // Preinstantiate
  621. template class MarlinSerial<MarlinInternalSerialCfg<INTERNAL_SERIAL_PORT>>;
  622. // Instantiate
  623. MarlinSerial<MarlinInternalSerialCfg<INTERNAL_SERIAL_PORT>> internalSerial;
  624. #endif
  625. #ifdef DGUS_SERIAL_PORT
  626. template<typename Cfg>
  627. typename MarlinSerial<Cfg>::ring_buffer_pos_t MarlinSerial<Cfg>::get_tx_buffer_free() {
  628. const ring_buffer_pos_t t = tx_buffer.tail, // next byte to send.
  629. h = tx_buffer.head; // next pos for queue.
  630. int ret = t - h - 1;
  631. if (ret < 0) ret += Cfg::TX_SIZE + 1;
  632. return ret;
  633. }
  634. ISR(SERIAL_REGNAME(USART,DGUS_SERIAL_PORT,_RX_vect)) {
  635. MarlinSerial<MarlinInternalSerialCfg<DGUS_SERIAL_PORT>>::store_rxd_char();
  636. }
  637. ISR(SERIAL_REGNAME(USART,DGUS_SERIAL_PORT,_UDRE_vect)) {
  638. MarlinSerial<MarlinInternalSerialCfg<DGUS_SERIAL_PORT>>::_tx_udr_empty_irq();
  639. }
  640. // Preinstantiate
  641. template class MarlinSerial<MarlinInternalSerialCfg<DGUS_SERIAL_PORT>>;
  642. // Instantiate
  643. MarlinSerial<MarlinInternalSerialCfg<DGUS_SERIAL_PORT>> internalDgusSerial;
  644. #endif
  645. // For AT90USB targets use the UART for BT interfacing
  646. #if defined(USBCON) && ENABLED(BLUETOOTH)
  647. HardwareSerial bluetoothSerial;
  648. #endif
  649. #endif // __AVR__