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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  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. * 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. */
  31. #ifdef __AVR__
  32. // Disable HardwareSerial.cpp to support chips without a UART (Attiny, etc.)
  33. #include "../../inc/MarlinConfig.h"
  34. #if !defined(USBCON) && (defined(UBRRH) || defined(UBRR0H) || defined(UBRR1H) || defined(UBRR2H) || defined(UBRR3H))
  35. #include "MarlinSerial.h"
  36. #include "../../Marlin.h"
  37. struct ring_buffer_r {
  38. unsigned char buffer[RX_BUFFER_SIZE];
  39. volatile ring_buffer_pos_t head, tail;
  40. };
  41. #if TX_BUFFER_SIZE > 0
  42. struct ring_buffer_t {
  43. unsigned char buffer[TX_BUFFER_SIZE];
  44. volatile uint8_t head, tail;
  45. };
  46. #endif
  47. #if UART_PRESENT(SERIAL_PORT)
  48. ring_buffer_r rx_buffer = { { 0 }, 0, 0 };
  49. #if TX_BUFFER_SIZE > 0
  50. ring_buffer_t tx_buffer = { { 0 }, 0, 0 };
  51. #endif
  52. static bool _written;
  53. #endif
  54. #if ENABLED(SERIAL_XON_XOFF)
  55. constexpr uint8_t XON_XOFF_CHAR_SENT = 0x80, // XON / XOFF Character was sent
  56. XON_XOFF_CHAR_MASK = 0x1F; // XON / XOFF character to send
  57. // XON / XOFF character definitions
  58. constexpr uint8_t XON_CHAR = 17, XOFF_CHAR = 19;
  59. uint8_t xon_xoff_state = XON_XOFF_CHAR_SENT | XON_CHAR;
  60. #endif
  61. #if ENABLED(SERIAL_STATS_DROPPED_RX)
  62. uint8_t rx_dropped_bytes = 0;
  63. #endif
  64. #if ENABLED(SERIAL_STATS_MAX_RX_QUEUED)
  65. ring_buffer_pos_t rx_max_enqueued = 0;
  66. #endif
  67. // A SW memory barrier, to ensure GCC does not overoptimize loops
  68. #define sw_barrier() asm volatile("": : :"memory");
  69. #if ENABLED(EMERGENCY_PARSER)
  70. #include "../../feature/emergency_parser.h"
  71. #endif
  72. // (called with RX interrupts disabled)
  73. FORCE_INLINE void store_rxd_char() {
  74. #if ENABLED(EMERGENCY_PARSER)
  75. static EmergencyParser::State emergency_state; // = EP_RESET
  76. #endif
  77. // Get the tail - Nothing can alter its value while we are at this ISR
  78. const ring_buffer_pos_t t = rx_buffer.tail;
  79. // Get the head pointer
  80. ring_buffer_pos_t h = rx_buffer.head;
  81. // Get the next element
  82. ring_buffer_pos_t i = (ring_buffer_pos_t)(h + 1) & (ring_buffer_pos_t)(RX_BUFFER_SIZE - 1);
  83. // Read the character from the USART
  84. uint8_t c = M_UDRx;
  85. #if ENABLED(EMERGENCY_PARSER)
  86. emergency_parser.update(emergency_state, c);
  87. #endif
  88. // If the character is to be stored at the index just before the tail
  89. // (such that the head would advance to the current tail), the RX FIFO is
  90. // full, so don't write the character or advance the head.
  91. if (i != t) {
  92. rx_buffer.buffer[h] = c;
  93. h = i;
  94. }
  95. #if ENABLED(SERIAL_STATS_DROPPED_RX)
  96. else if (!++rx_dropped_bytes) --rx_dropped_bytes;
  97. #endif
  98. #if ENABLED(SERIAL_STATS_MAX_RX_QUEUED)
  99. // Calculate count of bytes stored into the RX buffer
  100. const ring_buffer_pos_t rx_count = (ring_buffer_pos_t)(h - t) & (ring_buffer_pos_t)(RX_BUFFER_SIZE - 1);
  101. // Keep track of the maximum count of enqueued bytes
  102. NOLESS(rx_max_enqueued, rx_count);
  103. #endif
  104. #if ENABLED(SERIAL_XON_XOFF)
  105. // If the last char that was sent was an XON
  106. if ((xon_xoff_state & XON_XOFF_CHAR_MASK) == XON_CHAR) {
  107. // Bytes stored into the RX buffer
  108. const ring_buffer_pos_t rx_count = (ring_buffer_pos_t)(h - t) & (ring_buffer_pos_t)(RX_BUFFER_SIZE - 1);
  109. // If over 12.5% of RX buffer capacity, send XOFF before running out of
  110. // RX buffer space .. 325 bytes @ 250kbits/s needed to let the host react
  111. // and stop sending bytes. This translates to 13mS propagation time.
  112. if (rx_count >= (RX_BUFFER_SIZE) / 8) {
  113. // At this point, definitely no TX interrupt was executing, since the TX isr can't be preempted.
  114. // Don't enable the TX interrupt here as a means to trigger the XOFF char, because if it happens
  115. // to be in the middle of trying to disable the RX interrupt in the main program, eventually the
  116. // enabling of the TX interrupt could be undone. The ONLY reliable thing this can do to ensure
  117. // the sending of the XOFF char is to send it HERE AND NOW.
  118. // About to send the XOFF char
  119. xon_xoff_state = XOFF_CHAR | XON_XOFF_CHAR_SENT;
  120. // Wait until the TX register becomes empty and send it - Here there could be a problem
  121. // - While waiting for the TX register to empty, the RX register could receive a new
  122. // character. This must also handle that situation!
  123. while (!TEST(M_UCSRxA, M_UDREx)) {
  124. if (TEST(M_UCSRxA,M_RXCx)) {
  125. // A char arrived while waiting for the TX buffer to be empty - Receive and process it!
  126. i = (ring_buffer_pos_t)(h + 1) & (ring_buffer_pos_t)(RX_BUFFER_SIZE - 1);
  127. // Read the character from the USART
  128. c = M_UDRx;
  129. #if ENABLED(EMERGENCY_PARSER)
  130. emergency_parser.update(emergency_state, c);
  131. #endif
  132. // If the character is to be stored at the index just before the tail
  133. // (such that the head would advance to the current tail), the FIFO is
  134. // full, so don't write the character or advance the head.
  135. if (i != t) {
  136. rx_buffer.buffer[h] = c;
  137. h = i;
  138. }
  139. #if ENABLED(SERIAL_STATS_DROPPED_RX)
  140. else if (!++rx_dropped_bytes) --rx_dropped_bytes;
  141. #endif
  142. }
  143. sw_barrier();
  144. }
  145. M_UDRx = XOFF_CHAR;
  146. // Clear the TXC bit -- "can be cleared by writing a one to its bit
  147. // location". This makes sure flush() won't return until the bytes
  148. // actually got written
  149. SBI(M_UCSRxA, M_TXCx);
  150. // At this point there could be a race condition between the write() function
  151. // and this sending of the XOFF char. This interrupt could happen between the
  152. // wait to be empty TX buffer loop and the actual write of the character. Since
  153. // the TX buffer is full because it's sending the XOFF char, the only way to be
  154. // sure the write() function will succeed is to wait for the XOFF char to be
  155. // completely sent. Since an extra character could be received during the wait
  156. // it must also be handled!
  157. while (!TEST(M_UCSRxA, M_UDREx)) {
  158. if (TEST(M_UCSRxA,M_RXCx)) {
  159. // A char arrived while waiting for the TX buffer to be empty - Receive and process it!
  160. i = (ring_buffer_pos_t)(h + 1) & (ring_buffer_pos_t)(RX_BUFFER_SIZE - 1);
  161. // Read the character from the USART
  162. c = M_UDRx;
  163. #if ENABLED(EMERGENCY_PARSER)
  164. emergency_parser.update(emergency_state, c);
  165. #endif
  166. // If the character is to be stored at the index just before the tail
  167. // (such that the head would advance to the current tail), the FIFO is
  168. // full, so don't write the character or advance the head.
  169. if (i != t) {
  170. rx_buffer.buffer[h] = c;
  171. h = i;
  172. }
  173. #if ENABLED(SERIAL_STATS_DROPPED_RX)
  174. else if (!++rx_dropped_bytes) --rx_dropped_bytes;
  175. #endif
  176. }
  177. sw_barrier();
  178. }
  179. // At this point everything is ready. The write() function won't
  180. // have any issues writing to the UART TX register if it needs to!
  181. }
  182. }
  183. #endif // SERIAL_XON_XOFF
  184. // Store the new head value
  185. rx_buffer.head = h;
  186. }
  187. #if TX_BUFFER_SIZE > 0
  188. // (called with TX irqs disabled)
  189. FORCE_INLINE void _tx_udr_empty_irq(void) {
  190. // Read positions
  191. uint8_t t = tx_buffer.tail;
  192. const uint8_t h = tx_buffer.head;
  193. #if ENABLED(SERIAL_XON_XOFF)
  194. // If an XON char is pending to be sent, do it now
  195. if (xon_xoff_state == XON_CHAR) {
  196. // Send the character
  197. M_UDRx = XON_CHAR;
  198. // clear the TXC bit -- "can be cleared by writing a one to its bit
  199. // location". This makes sure flush() won't return until the bytes
  200. // actually got written
  201. SBI(M_UCSRxA, M_TXCx);
  202. // Remember we sent it.
  203. xon_xoff_state = XON_CHAR | XON_XOFF_CHAR_SENT;
  204. // If nothing else to transmit, just disable TX interrupts.
  205. if (h == t) CBI(M_UCSRxB, M_UDRIEx); // (Non-atomic, could be reenabled by the main program, but eventually this will succeed)
  206. return;
  207. }
  208. #endif
  209. // If nothing to transmit, just disable TX interrupts. This could
  210. // happen as the result of the non atomicity of the disabling of RX
  211. // interrupts that could end reenabling TX interrupts as a side effect.
  212. if (h == t) {
  213. CBI(M_UCSRxB, M_UDRIEx); // (Non-atomic, could be reenabled by the main program, but eventually this will succeed)
  214. return;
  215. }
  216. // There is something to TX, Send the next byte
  217. const uint8_t c = tx_buffer.buffer[t];
  218. t = (t + 1) & (TX_BUFFER_SIZE - 1);
  219. M_UDRx = c;
  220. tx_buffer.tail = t;
  221. // Clear the TXC bit (by writing a one to its bit location).
  222. // Ensures flush() won't return until the bytes are actually written/
  223. SBI(M_UCSRxA, M_TXCx);
  224. // Disable interrupts if there is nothing to transmit following this byte
  225. if (h == t) CBI(M_UCSRxB, M_UDRIEx); // (Non-atomic, could be reenabled by the main program, but eventually this will succeed)
  226. }
  227. #ifdef M_USARTx_UDRE_vect
  228. ISR(M_USARTx_UDRE_vect) { _tx_udr_empty_irq(); }
  229. #endif
  230. #endif // TX_BUFFER_SIZE
  231. #ifdef M_USARTx_RX_vect
  232. ISR(M_USARTx_RX_vect) { store_rxd_char(); }
  233. #endif
  234. // Public Methods
  235. void MarlinSerial::begin(const long baud) {
  236. uint16_t baud_setting;
  237. bool useU2X = true;
  238. #if F_CPU == 16000000UL && SERIAL_PORT == 0
  239. // Hard-coded exception for compatibility with the bootloader shipped
  240. // with the Duemilanove and previous boards, and the firmware on the
  241. // 8U2 on the Uno and Mega 2560.
  242. if (baud == 57600) useU2X = false;
  243. #endif
  244. if (useU2X) {
  245. M_UCSRxA = _BV(M_U2Xx);
  246. baud_setting = (F_CPU / 4 / baud - 1) / 2;
  247. }
  248. else {
  249. M_UCSRxA = 0;
  250. baud_setting = (F_CPU / 8 / baud - 1) / 2;
  251. }
  252. // assign the baud_setting, a.k.a. ubbr (USART Baud Rate Register)
  253. M_UBRRxH = baud_setting >> 8;
  254. M_UBRRxL = baud_setting;
  255. SBI(M_UCSRxB, M_RXENx);
  256. SBI(M_UCSRxB, M_TXENx);
  257. SBI(M_UCSRxB, M_RXCIEx);
  258. #if TX_BUFFER_SIZE > 0
  259. CBI(M_UCSRxB, M_UDRIEx);
  260. #endif
  261. _written = false;
  262. }
  263. void MarlinSerial::end() {
  264. CBI(M_UCSRxB, M_RXENx);
  265. CBI(M_UCSRxB, M_TXENx);
  266. CBI(M_UCSRxB, M_RXCIEx);
  267. CBI(M_UCSRxB, M_UDRIEx);
  268. }
  269. int MarlinSerial::peek(void) {
  270. #if RX_BUFFER_SIZE > 256
  271. // Disable RX interrupts, but only if non atomic reads
  272. const bool isr_enabled = TEST(M_UCSRxB, M_RXCIEx);
  273. CBI(M_UCSRxB, M_RXCIEx);
  274. #endif
  275. const int v = rx_buffer.head == rx_buffer.tail ? -1 : rx_buffer.buffer[rx_buffer.tail];
  276. #if RX_BUFFER_SIZE > 256
  277. // Reenable RX interrupts if they were enabled
  278. if (isr_enabled) SBI(M_UCSRxB, M_RXCIEx);
  279. #endif
  280. return v;
  281. }
  282. int MarlinSerial::read(void) {
  283. #if RX_BUFFER_SIZE > 256
  284. // Disable RX interrupts to ensure atomic reads - This could reenable TX interrupts,
  285. // but this situation is explicitly handled at the TX isr, so no problems there
  286. bool isr_enabled = TEST(M_UCSRxB, M_RXCIEx);
  287. CBI(M_UCSRxB, M_RXCIEx);
  288. #endif
  289. const ring_buffer_pos_t h = rx_buffer.head;
  290. #if RX_BUFFER_SIZE > 256
  291. // End critical section
  292. if (isr_enabled) SBI(M_UCSRxB, M_RXCIEx);
  293. #endif
  294. ring_buffer_pos_t t = rx_buffer.tail;
  295. // If nothing to read, return now
  296. if (h == t) return -1;
  297. // Get the next char
  298. const int v = rx_buffer.buffer[t];
  299. t = (ring_buffer_pos_t)(t + 1) & (RX_BUFFER_SIZE - 1);
  300. #if RX_BUFFER_SIZE > 256
  301. // Disable RX interrupts to ensure atomic write to tail, so
  302. // the RX isr can't read partially updated values - This could
  303. // reenable TX interrupts, but this situation is explicitly
  304. // handled at the TX isr, so no problems there
  305. isr_enabled = TEST(M_UCSRxB, M_RXCIEx);
  306. CBI(M_UCSRxB, M_RXCIEx);
  307. #endif
  308. // Advance tail
  309. rx_buffer.tail = t;
  310. #if RX_BUFFER_SIZE > 256
  311. // End critical section
  312. if (isr_enabled) SBI(M_UCSRxB, M_RXCIEx);
  313. #endif
  314. #if ENABLED(SERIAL_XON_XOFF)
  315. // If the XOFF char was sent, or about to be sent...
  316. if ((xon_xoff_state & XON_XOFF_CHAR_MASK) == XOFF_CHAR) {
  317. // Get count of bytes in the RX buffer
  318. const ring_buffer_pos_t rx_count = (ring_buffer_pos_t)(h - t) & (ring_buffer_pos_t)(RX_BUFFER_SIZE - 1);
  319. if (rx_count < (RX_BUFFER_SIZE) / 10) {
  320. #if TX_BUFFER_SIZE > 0
  321. // Signal we want an XON character to be sent.
  322. xon_xoff_state = XON_CHAR;
  323. // Enable TX isr. Non atomic, but it will eventually enable them
  324. SBI(M_UCSRxB, M_UDRIEx);
  325. #else
  326. // If not using TX interrupts, we must send the XON char now
  327. xon_xoff_state = XON_CHAR | XON_XOFF_CHAR_SENT;
  328. while (!TEST(M_UCSRxA, M_UDREx)) sw_barrier();
  329. M_UDRx = XON_CHAR;
  330. #endif
  331. }
  332. }
  333. #endif
  334. return v;
  335. }
  336. ring_buffer_pos_t MarlinSerial::available(void) {
  337. #if RX_BUFFER_SIZE > 256
  338. const bool isr_enabled = TEST(M_UCSRxB, M_RXCIEx);
  339. CBI(M_UCSRxB, M_RXCIEx);
  340. #endif
  341. const ring_buffer_pos_t h = rx_buffer.head, t = rx_buffer.tail;
  342. #if RX_BUFFER_SIZE > 256
  343. if (isr_enabled) SBI(M_UCSRxB, M_RXCIEx);
  344. #endif
  345. return (ring_buffer_pos_t)(RX_BUFFER_SIZE + h - t) & (RX_BUFFER_SIZE - 1);
  346. }
  347. void MarlinSerial::flush(void) {
  348. #if RX_BUFFER_SIZE > 256
  349. const bool isr_enabled = TEST(M_UCSRxB, M_RXCIEx);
  350. CBI(M_UCSRxB, M_RXCIEx);
  351. #endif
  352. rx_buffer.tail = rx_buffer.head;
  353. #if RX_BUFFER_SIZE > 256
  354. if (isr_enabled) SBI(M_UCSRxB, M_RXCIEx);
  355. #endif
  356. #if ENABLED(SERIAL_XON_XOFF)
  357. // If the XOFF char was sent, or about to be sent...
  358. if ((xon_xoff_state & XON_XOFF_CHAR_MASK) == XOFF_CHAR) {
  359. #if TX_BUFFER_SIZE > 0
  360. // Signal we want an XON character to be sent.
  361. xon_xoff_state = XON_CHAR;
  362. // Enable TX isr. Non atomic, but it will eventually enable it.
  363. SBI(M_UCSRxB, M_UDRIEx);
  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 (!TEST(M_UCSRxA, M_UDREx)) sw_barrier();
  368. M_UDRx = XON_CHAR;
  369. #endif
  370. }
  371. #endif
  372. }
  373. #if TX_BUFFER_SIZE > 0
  374. void MarlinSerial::write(const uint8_t c) {
  375. _written = true;
  376. // If the TX interrupts are disabled and the data register
  377. // is empty, just write the byte to the data register and
  378. // be done. This shortcut helps significantly improve the
  379. // effective datarate at high (>500kbit/s) bitrates, where
  380. // interrupt overhead becomes a slowdown.
  381. // Yes, there is a race condition between the sending of the
  382. // XOFF char at the RX isr, but it is properly handled there
  383. if (!TEST(M_UCSRxB, M_UDRIEx) && TEST(M_UCSRxA, M_UDREx)) {
  384. M_UDRx = c;
  385. // clear the TXC bit -- "can be cleared by writing a one to its bit
  386. // location". This makes sure flush() won't return until the bytes
  387. // actually got written
  388. SBI(M_UCSRxA, M_TXCx);
  389. return;
  390. }
  391. const uint8_t i = (tx_buffer.head + 1) & (TX_BUFFER_SIZE - 1);
  392. // If global interrupts are disabled (as the result of being called from an ISR)...
  393. if (!ISRS_ENABLED()) {
  394. // Make room by polling if it is possible to transmit, and do so!
  395. while (i == tx_buffer.tail) {
  396. // If we can transmit another byte, do it.
  397. if (TEST(M_UCSRxA, M_UDREx)) _tx_udr_empty_irq();
  398. // Make sure compiler rereads tx_buffer.tail
  399. sw_barrier();
  400. }
  401. }
  402. else {
  403. // Interrupts are enabled, just wait until there is space
  404. while (i == tx_buffer.tail) { sw_barrier(); }
  405. }
  406. // Store new char. head is always safe to move
  407. tx_buffer.buffer[tx_buffer.head] = c;
  408. tx_buffer.head = i;
  409. // Enable TX isr - Non atomic, but it will eventually enable TX isr
  410. SBI(M_UCSRxB, M_UDRIEx);
  411. }
  412. void MarlinSerial::flushTX(void) {
  413. // No bytes written, no need to flush. This special case is needed since there's
  414. // no way to force the TXC (transmit complete) bit to 1 during initialization.
  415. if (!_written) return;
  416. // If global interrupts are disabled (as the result of being called from an ISR)...
  417. if (!ISRS_ENABLED()) {
  418. // Wait until everything was transmitted - We must do polling, as interrupts are disabled
  419. while (tx_buffer.head != tx_buffer.tail || !TEST(M_UCSRxA, M_TXCx)) {
  420. // If there is more space, send an extra character
  421. if (TEST(M_UCSRxA, M_UDREx))
  422. _tx_udr_empty_irq();
  423. sw_barrier();
  424. }
  425. }
  426. else {
  427. // Wait until everything was transmitted
  428. while (tx_buffer.head != tx_buffer.tail || !TEST(M_UCSRxA, M_TXCx)) sw_barrier();
  429. }
  430. // At this point nothing is queued anymore (DRIE is disabled) and
  431. // the hardware finished transmission (TXC is set).
  432. }
  433. #else // TX_BUFFER_SIZE == 0
  434. void MarlinSerial::write(const uint8_t c) {
  435. _written = true;
  436. while (!TEST(M_UCSRxA, M_UDREx)) sw_barrier();
  437. M_UDRx = c;
  438. }
  439. void MarlinSerial::flushTX(void) {
  440. // No bytes written, no need to flush. This special case is needed since there's
  441. // no way to force the TXC (transmit complete) bit to 1 during initialization.
  442. if (!_written) return;
  443. // Wait until everything was transmitted
  444. while (!TEST(M_UCSRxA, M_TXCx)) sw_barrier();
  445. // At this point nothing is queued anymore (DRIE is disabled) and
  446. // the hardware finished transmission (TXC is set).
  447. }
  448. #endif // TX_BUFFER_SIZE == 0
  449. /**
  450. * Imports from print.h
  451. */
  452. void MarlinSerial::print(char c, int base) {
  453. print((long)c, base);
  454. }
  455. void MarlinSerial::print(unsigned char b, int base) {
  456. print((unsigned long)b, base);
  457. }
  458. void MarlinSerial::print(int n, int base) {
  459. print((long)n, base);
  460. }
  461. void MarlinSerial::print(unsigned int n, int base) {
  462. print((unsigned long)n, base);
  463. }
  464. void MarlinSerial::print(long n, int base) {
  465. if (base == 0) write(n);
  466. else if (base == 10) {
  467. if (n < 0) { print('-'); n = -n; }
  468. printNumber(n, 10);
  469. }
  470. else
  471. printNumber(n, base);
  472. }
  473. void MarlinSerial::print(unsigned long n, int base) {
  474. if (base == 0) write(n);
  475. else printNumber(n, base);
  476. }
  477. void MarlinSerial::print(double n, int digits) {
  478. printFloat(n, digits);
  479. }
  480. void MarlinSerial::println(void) {
  481. print('\r');
  482. print('\n');
  483. }
  484. void MarlinSerial::println(const String& s) {
  485. print(s);
  486. println();
  487. }
  488. void MarlinSerial::println(const char c[]) {
  489. print(c);
  490. println();
  491. }
  492. void MarlinSerial::println(char c, int base) {
  493. print(c, base);
  494. println();
  495. }
  496. void MarlinSerial::println(unsigned char b, int base) {
  497. print(b, base);
  498. println();
  499. }
  500. void MarlinSerial::println(int n, int base) {
  501. print(n, base);
  502. println();
  503. }
  504. void MarlinSerial::println(unsigned int n, int base) {
  505. print(n, base);
  506. println();
  507. }
  508. void MarlinSerial::println(long n, int base) {
  509. print(n, base);
  510. println();
  511. }
  512. void MarlinSerial::println(unsigned long n, int base) {
  513. print(n, base);
  514. println();
  515. }
  516. void MarlinSerial::println(double n, int digits) {
  517. print(n, digits);
  518. println();
  519. }
  520. // Private Methods
  521. void MarlinSerial::printNumber(unsigned long n, uint8_t base) {
  522. if (n) {
  523. unsigned char buf[8 * sizeof(long)]; // Enough space for base 2
  524. int8_t i = 0;
  525. while (n) {
  526. buf[i++] = n % base;
  527. n /= base;
  528. }
  529. while (i--)
  530. print((char)(buf[i] + (buf[i] < 10 ? '0' : 'A' - 10)));
  531. }
  532. else
  533. print('0');
  534. }
  535. void MarlinSerial::printFloat(double number, uint8_t digits) {
  536. // Handle negative numbers
  537. if (number < 0.0) {
  538. print('-');
  539. number = -number;
  540. }
  541. // Round correctly so that print(1.999, 2) prints as "2.00"
  542. double rounding = 0.5;
  543. for (uint8_t i = 0; i < digits; ++i)
  544. rounding *= 0.1;
  545. number += rounding;
  546. // Extract the integer part of the number and print it
  547. unsigned long int_part = (unsigned long)number;
  548. double remainder = number - (double)int_part;
  549. print(int_part);
  550. // Print the decimal point, but only if there are digits beyond
  551. if (digits) {
  552. print('.');
  553. // Extract digits from the remainder one at a time
  554. while (digits--) {
  555. remainder *= 10.0;
  556. int toPrint = int(remainder);
  557. print(toPrint);
  558. remainder -= toPrint;
  559. }
  560. }
  561. }
  562. // Preinstantiate
  563. MarlinSerial customizedSerial;
  564. #endif // !USBCON && (UBRRH || UBRR0H || UBRR1H || UBRR2H || UBRR3H)
  565. // For AT90USB targets use the UART for BT interfacing
  566. #if defined(USBCON) && ENABLED(BLUETOOTH)
  567. HardwareSerial bluetoothSerial;
  568. #endif
  569. #endif // __AVR__