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

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