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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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. */
  30. #include "MarlinSerial.h"
  31. #include "Marlin.h"
  32. // Disable HardwareSerial.cpp to support chips without a UART (Attiny, etc.)
  33. #if !defined(USBCON) && (defined(UBRRH) || defined(UBRR0H) || defined(UBRR1H) || defined(UBRR2H) || defined(UBRR3H))
  34. #if UART_PRESENT(SERIAL_PORT)
  35. ring_buffer_r rx_buffer = { { 0 }, 0, 0 };
  36. #if TX_BUFFER_SIZE > 0
  37. ring_buffer_t tx_buffer = { { 0 }, 0, 0 };
  38. static bool _written;
  39. #endif
  40. #endif
  41. #if ENABLED(EMERGENCY_PARSER)
  42. #include "stepper.h"
  43. #include "language.h"
  44. // Currently looking for: M108, M112, M410
  45. // If you alter the parser please don't forget to update the capabilities in Conditionals_post.h
  46. FORCE_INLINE void emergency_parser(const unsigned char c) {
  47. static e_parser_state state = state_RESET;
  48. switch (state) {
  49. case state_RESET:
  50. switch (c) {
  51. case ' ': break;
  52. case 'N': state = state_N; break;
  53. case 'M': state = state_M; break;
  54. default: state = state_IGNORE;
  55. }
  56. break;
  57. case state_N:
  58. switch (c) {
  59. case '0': case '1': case '2':
  60. case '3': case '4': case '5':
  61. case '6': case '7': case '8':
  62. case '9': case '-': case ' ': break;
  63. case 'M': state = state_M; break;
  64. default: state = state_IGNORE;
  65. }
  66. break;
  67. case state_M:
  68. switch (c) {
  69. case ' ': break;
  70. case '1': state = state_M1; break;
  71. case '4': state = state_M4; break;
  72. default: state = state_IGNORE;
  73. }
  74. break;
  75. case state_M1:
  76. switch (c) {
  77. case '0': state = state_M10; break;
  78. case '1': state = state_M11; break;
  79. default: state = state_IGNORE;
  80. }
  81. break;
  82. case state_M10:
  83. state = (c == '8') ? state_M108 : state_IGNORE;
  84. break;
  85. case state_M11:
  86. state = (c == '2') ? state_M112 : state_IGNORE;
  87. break;
  88. case state_M4:
  89. state = (c == '1') ? state_M41 : state_IGNORE;
  90. break;
  91. case state_M41:
  92. state = (c == '0') ? state_M410 : state_IGNORE;
  93. break;
  94. case state_IGNORE:
  95. if (c == '\n') state = state_RESET;
  96. break;
  97. default:
  98. if (c == '\n') {
  99. switch (state) {
  100. case state_M108:
  101. wait_for_user = wait_for_heatup = false;
  102. break;
  103. case state_M112:
  104. kill(PSTR(MSG_KILLED));
  105. break;
  106. case state_M410:
  107. quickstop_stepper();
  108. break;
  109. default:
  110. break;
  111. }
  112. state = state_RESET;
  113. }
  114. }
  115. }
  116. #endif // EMERGENCY_PARSER
  117. FORCE_INLINE void store_char(unsigned char c) {
  118. CRITICAL_SECTION_START;
  119. const uint8_t h = rx_buffer.head,
  120. i = (uint8_t)(h + 1) & (RX_BUFFER_SIZE - 1);
  121. // if we should be storing the received character into the location
  122. // just before the tail (meaning that the head would advance to the
  123. // current location of the tail), we're about to overflow the buffer
  124. // and so we don't write the character or advance the head.
  125. if (i != rx_buffer.tail) {
  126. rx_buffer.buffer[h] = c;
  127. rx_buffer.head = i;
  128. }
  129. CRITICAL_SECTION_END;
  130. #if ENABLED(EMERGENCY_PARSER)
  131. emergency_parser(c);
  132. #endif
  133. }
  134. #if TX_BUFFER_SIZE > 0
  135. FORCE_INLINE void _tx_udr_empty_irq(void) {
  136. // If interrupts are enabled, there must be more data in the output
  137. // buffer. Send the next byte
  138. const uint8_t t = tx_buffer.tail,
  139. c = tx_buffer.buffer[t];
  140. tx_buffer.tail = (t + 1) & (TX_BUFFER_SIZE - 1);
  141. M_UDRx = c;
  142. // clear the TXC bit -- "can be cleared by writing a one to its bit
  143. // location". This makes sure flush() won't return until the bytes
  144. // actually got written
  145. SBI(M_UCSRxA, M_TXCx);
  146. if (tx_buffer.head == tx_buffer.tail) {
  147. // Buffer empty, so disable interrupts
  148. CBI(M_UCSRxB, M_UDRIEx);
  149. }
  150. }
  151. #ifdef M_USARTx_UDRE_vect
  152. ISR(M_USARTx_UDRE_vect) {
  153. _tx_udr_empty_irq();
  154. }
  155. #endif
  156. #endif // TX_BUFFER_SIZE
  157. #ifdef M_USARTx_RX_vect
  158. ISR(M_USARTx_RX_vect) {
  159. const unsigned char c = M_UDRx;
  160. store_char(c);
  161. }
  162. #endif
  163. // Public Methods
  164. void MarlinSerial::begin(const long baud) {
  165. uint16_t baud_setting;
  166. bool useU2X = true;
  167. #if F_CPU == 16000000UL && SERIAL_PORT == 0
  168. // hard-coded exception for compatibility with the bootloader shipped
  169. // with the Duemilanove and previous boards and the firmware on the 8U2
  170. // on the Uno and Mega 2560.
  171. if (baud == 57600) useU2X = false;
  172. #endif
  173. if (useU2X) {
  174. M_UCSRxA = _BV(M_U2Xx);
  175. baud_setting = (F_CPU / 4 / baud - 1) / 2;
  176. }
  177. else {
  178. M_UCSRxA = 0;
  179. baud_setting = (F_CPU / 8 / baud - 1) / 2;
  180. }
  181. // assign the baud_setting, a.k.a. ubbr (USART Baud Rate Register)
  182. M_UBRRxH = baud_setting >> 8;
  183. M_UBRRxL = baud_setting;
  184. SBI(M_UCSRxB, M_RXENx);
  185. SBI(M_UCSRxB, M_TXENx);
  186. SBI(M_UCSRxB, M_RXCIEx);
  187. #if TX_BUFFER_SIZE > 0
  188. CBI(M_UCSRxB, M_UDRIEx);
  189. _written = false;
  190. #endif
  191. }
  192. void MarlinSerial::end() {
  193. CBI(M_UCSRxB, M_RXENx);
  194. CBI(M_UCSRxB, M_TXENx);
  195. CBI(M_UCSRxB, M_RXCIEx);
  196. CBI(M_UCSRxB, M_UDRIEx);
  197. }
  198. void MarlinSerial::checkRx(void) {
  199. if (TEST(M_UCSRxA, M_RXCx)) {
  200. const uint8_t c = M_UDRx;
  201. store_char(c);
  202. }
  203. }
  204. int MarlinSerial::peek(void) {
  205. CRITICAL_SECTION_START;
  206. const int v = rx_buffer.head == rx_buffer.tail ? -1 : rx_buffer.buffer[rx_buffer.tail];
  207. CRITICAL_SECTION_END;
  208. return v;
  209. }
  210. int MarlinSerial::read(void) {
  211. int v;
  212. CRITICAL_SECTION_START;
  213. const uint8_t t = rx_buffer.tail;
  214. if (rx_buffer.head == t)
  215. v = -1;
  216. else {
  217. v = rx_buffer.buffer[t];
  218. rx_buffer.tail = (uint8_t)(t + 1) & (RX_BUFFER_SIZE - 1);
  219. }
  220. CRITICAL_SECTION_END;
  221. return v;
  222. }
  223. uint8_t MarlinSerial::available(void) {
  224. CRITICAL_SECTION_START;
  225. const uint8_t h = rx_buffer.head,
  226. t = rx_buffer.tail;
  227. CRITICAL_SECTION_END;
  228. return (uint8_t)(RX_BUFFER_SIZE + h - t) & (RX_BUFFER_SIZE - 1);
  229. }
  230. void MarlinSerial::flush(void) {
  231. // RX
  232. // don't reverse this or there may be problems if the RX interrupt
  233. // occurs after reading the value of rx_buffer_head but before writing
  234. // the value to rx_buffer_tail; the previous value of rx_buffer_head
  235. // may be written to rx_buffer_tail, making it appear as if the buffer
  236. // were full, not empty.
  237. CRITICAL_SECTION_START;
  238. rx_buffer.head = rx_buffer.tail;
  239. CRITICAL_SECTION_END;
  240. }
  241. #if TX_BUFFER_SIZE > 0
  242. uint8_t MarlinSerial::availableForWrite(void) {
  243. CRITICAL_SECTION_START;
  244. const uint8_t h = tx_buffer.head,
  245. t = tx_buffer.tail;
  246. CRITICAL_SECTION_END;
  247. return (uint8_t)(TX_BUFFER_SIZE + h - t) & (TX_BUFFER_SIZE - 1);
  248. }
  249. void MarlinSerial::write(const uint8_t c) {
  250. _written = true;
  251. CRITICAL_SECTION_START;
  252. bool emty = (tx_buffer.head == tx_buffer.tail);
  253. CRITICAL_SECTION_END;
  254. // If the buffer and the data register is empty, just write the byte
  255. // to the data register and be done. This shortcut helps
  256. // significantly improve the effective datarate at high (>
  257. // 500kbit/s) bitrates, where interrupt overhead becomes a slowdown.
  258. if (emty && TEST(M_UCSRxA, M_UDREx)) {
  259. CRITICAL_SECTION_START;
  260. M_UDRx = c;
  261. SBI(M_UCSRxA, M_TXCx);
  262. CRITICAL_SECTION_END;
  263. return;
  264. }
  265. const uint8_t i = (tx_buffer.head + 1) & (TX_BUFFER_SIZE - 1);
  266. // If the output buffer is full, there's nothing for it other than to
  267. // wait for the interrupt handler to empty it a bit
  268. while (i == tx_buffer.tail) {
  269. if (!TEST(SREG, SREG_I)) {
  270. // Interrupts are disabled, so we'll have to poll the data
  271. // register empty flag ourselves. If it is set, pretend an
  272. // interrupt has happened and call the handler to free up
  273. // space for us.
  274. if (TEST(M_UCSRxA, M_UDREx))
  275. _tx_udr_empty_irq();
  276. } else {
  277. // nop, the interrupt handler will free up space for us
  278. }
  279. }
  280. tx_buffer.buffer[tx_buffer.head] = c;
  281. { CRITICAL_SECTION_START;
  282. tx_buffer.head = i;
  283. SBI(M_UCSRxB, M_UDRIEx);
  284. CRITICAL_SECTION_END;
  285. }
  286. return;
  287. }
  288. void MarlinSerial::flushTX(void) {
  289. // TX
  290. // If we have never written a byte, no need to flush. This special
  291. // case is needed since there is no way to force the TXC (transmit
  292. // complete) bit to 1 during initialization
  293. if (!_written)
  294. return;
  295. while (TEST(M_UCSRxB, M_UDRIEx) || !TEST(M_UCSRxA, M_TXCx)) {
  296. if (!TEST(SREG, SREG_I) && TEST(M_UCSRxB, M_UDRIEx))
  297. // Interrupts are globally disabled, but the DR empty
  298. // interrupt should be enabled, so poll the DR empty flag to
  299. // prevent deadlock
  300. if (TEST(M_UCSRxA, M_UDREx))
  301. _tx_udr_empty_irq();
  302. }
  303. // If we get here, nothing is queued anymore (DRIE is disabled) and
  304. // the hardware finished tranmission (TXC is set).
  305. }
  306. #else
  307. void MarlinSerial::write(uint8_t c) {
  308. while (!TEST(M_UCSRxA, M_UDREx))
  309. ;
  310. M_UDRx = c;
  311. }
  312. #endif
  313. // end NEW
  314. /// imports from print.h
  315. void MarlinSerial::print(char c, int base) {
  316. print((long)c, base);
  317. }
  318. void MarlinSerial::print(unsigned char b, int base) {
  319. print((unsigned long)b, base);
  320. }
  321. void MarlinSerial::print(int n, int base) {
  322. print((long)n, base);
  323. }
  324. void MarlinSerial::print(unsigned int n, int base) {
  325. print((unsigned long)n, base);
  326. }
  327. void MarlinSerial::print(long n, int base) {
  328. if (base == 0)
  329. write(n);
  330. else if (base == 10) {
  331. if (n < 0) {
  332. print('-');
  333. n = -n;
  334. }
  335. printNumber(n, 10);
  336. }
  337. else
  338. printNumber(n, base);
  339. }
  340. void MarlinSerial::print(unsigned long n, int base) {
  341. if (base == 0) write(n);
  342. else printNumber(n, base);
  343. }
  344. void MarlinSerial::print(double n, int digits) {
  345. printFloat(n, digits);
  346. }
  347. void MarlinSerial::println(void) {
  348. print('\r');
  349. print('\n');
  350. }
  351. void MarlinSerial::println(const String& s) {
  352. print(s);
  353. println();
  354. }
  355. void MarlinSerial::println(const char c[]) {
  356. print(c);
  357. println();
  358. }
  359. void MarlinSerial::println(char c, int base) {
  360. print(c, base);
  361. println();
  362. }
  363. void MarlinSerial::println(unsigned char b, int base) {
  364. print(b, base);
  365. println();
  366. }
  367. void MarlinSerial::println(int n, int base) {
  368. print(n, base);
  369. println();
  370. }
  371. void MarlinSerial::println(unsigned int n, int base) {
  372. print(n, base);
  373. println();
  374. }
  375. void MarlinSerial::println(long n, int base) {
  376. print(n, base);
  377. println();
  378. }
  379. void MarlinSerial::println(unsigned long n, int base) {
  380. print(n, base);
  381. println();
  382. }
  383. void MarlinSerial::println(double n, int digits) {
  384. print(n, digits);
  385. println();
  386. }
  387. // Private Methods
  388. void MarlinSerial::printNumber(unsigned long n, uint8_t base) {
  389. if (n) {
  390. unsigned char buf[8 * sizeof(long)]; // Enough space for base 2
  391. int8_t i = 0;
  392. while (n) {
  393. buf[i++] = n % base;
  394. n /= base;
  395. }
  396. while (i--)
  397. print((char)(buf[i] + (buf[i] < 10 ? '0' : 'A' - 10)));
  398. }
  399. else
  400. print('0');
  401. }
  402. void MarlinSerial::printFloat(double number, uint8_t digits) {
  403. // Handle negative numbers
  404. if (number < 0.0) {
  405. print('-');
  406. number = -number;
  407. }
  408. // Round correctly so that print(1.999, 2) prints as "2.00"
  409. double rounding = 0.5;
  410. for (uint8_t i = 0; i < digits; ++i)
  411. rounding *= 0.1;
  412. number += rounding;
  413. // Extract the integer part of the number and print it
  414. unsigned long int_part = (unsigned long)number;
  415. double remainder = number - (double)int_part;
  416. print(int_part);
  417. // Print the decimal point, but only if there are digits beyond
  418. if (digits) {
  419. print('.');
  420. // Extract digits from the remainder one at a time
  421. while (digits--) {
  422. remainder *= 10.0;
  423. int toPrint = int(remainder);
  424. print(toPrint);
  425. remainder -= toPrint;
  426. }
  427. }
  428. }
  429. // Preinstantiate
  430. MarlinSerial customizedSerial;
  431. #endif // !USBCON && (UBRRH || UBRR0H || UBRR1H || UBRR2H || UBRR3H)
  432. // For AT90USB targets use the UART for BT interfacing
  433. #if defined(USBCON) && ENABLED(BLUETOOTH)
  434. HardwareSerial bluetoothSerial;
  435. #endif