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

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