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

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