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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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 "stepper.h"
  32. #include "Marlin.h"
  33. #ifndef USBCON
  34. // this next line disables the entire HardwareSerial.cpp,
  35. // this is so I can support Attiny series and any other chip without a UART
  36. #if defined(UBRRH) || defined(UBRR0H) || defined(UBRR1H) || defined(UBRR2H) || defined(UBRR3H)
  37. #if UART_PRESENT(SERIAL_PORT)
  38. ring_buffer_r rx_buffer = { { 0 }, 0, 0 };
  39. #if TX_BUFFER_SIZE > 0
  40. ring_buffer_t tx_buffer = { { 0 }, 0, 0 };
  41. static bool _written;
  42. #endif
  43. #endif
  44. FORCE_INLINE void store_char(unsigned char c) {
  45. CRITICAL_SECTION_START;
  46. uint8_t h = rx_buffer.head;
  47. uint8_t i = (uint8_t)(h + 1) & (RX_BUFFER_SIZE - 1);
  48. // if we should be storing the received character into the location
  49. // just before the tail (meaning that the head would advance to the
  50. // current location of the tail), we're about to overflow the buffer
  51. // and so we don't write the character or advance the head.
  52. if (i != rx_buffer.tail) {
  53. rx_buffer.buffer[h] = c;
  54. rx_buffer.head = i;
  55. }
  56. CRITICAL_SECTION_END;
  57. #if ENABLED(EMERGENCY_PARSER)
  58. emergency_parser(c);
  59. #endif
  60. }
  61. #if TX_BUFFER_SIZE > 0
  62. FORCE_INLINE void _tx_udr_empty_irq(void) {
  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 // TX_BUFFER_SIZE
  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. CRITICAL_SECTION_START;
  137. int v = rx_buffer.head == rx_buffer.tail ? -1 : rx_buffer.buffer[rx_buffer.tail];
  138. CRITICAL_SECTION_END;
  139. return v;
  140. }
  141. int MarlinSerial::read(void) {
  142. int v;
  143. CRITICAL_SECTION_START;
  144. uint8_t t = rx_buffer.tail;
  145. if (rx_buffer.head == t) {
  146. v = -1;
  147. }
  148. else {
  149. v = rx_buffer.buffer[t];
  150. rx_buffer.tail = (uint8_t)(t + 1) & (RX_BUFFER_SIZE - 1);
  151. }
  152. CRITICAL_SECTION_END;
  153. return v;
  154. }
  155. uint8_t MarlinSerial::available(void) {
  156. CRITICAL_SECTION_START;
  157. uint8_t h = rx_buffer.head,
  158. t = rx_buffer.tail;
  159. CRITICAL_SECTION_END;
  160. return (uint8_t)(RX_BUFFER_SIZE + h - t) & (RX_BUFFER_SIZE - 1);
  161. }
  162. void MarlinSerial::flush(void) {
  163. // RX
  164. // don't reverse this or there may be problems if the RX interrupt
  165. // occurs after reading the value of rx_buffer_head but before writing
  166. // the value to rx_buffer_tail; the previous value of rx_buffer_head
  167. // may be written to rx_buffer_tail, making it appear as if the buffer
  168. // were full, not empty.
  169. CRITICAL_SECTION_START;
  170. rx_buffer.head = rx_buffer.tail;
  171. CRITICAL_SECTION_END;
  172. }
  173. #if TX_BUFFER_SIZE > 0
  174. uint8_t MarlinSerial::availableForWrite(void) {
  175. CRITICAL_SECTION_START;
  176. uint8_t h = tx_buffer.head;
  177. uint8_t t = tx_buffer.tail;
  178. CRITICAL_SECTION_END;
  179. return (uint8_t)(TX_BUFFER_SIZE + h - t) & (TX_BUFFER_SIZE - 1);
  180. }
  181. void MarlinSerial::write(uint8_t c) {
  182. _written = true;
  183. CRITICAL_SECTION_START;
  184. bool emty = (tx_buffer.head == tx_buffer.tail);
  185. CRITICAL_SECTION_END;
  186. // If the buffer and the data register is empty, just write the byte
  187. // to the data register and be done. This shortcut helps
  188. // significantly improve the effective datarate at high (>
  189. // 500kbit/s) bitrates, where interrupt overhead becomes a slowdown.
  190. if (emty && TEST(M_UCSRxA, M_UDREx)) {
  191. CRITICAL_SECTION_START;
  192. M_UDRx = c;
  193. SBI(M_UCSRxA, M_TXCx);
  194. CRITICAL_SECTION_END;
  195. return;
  196. }
  197. uint8_t i = (tx_buffer.head + 1) & (TX_BUFFER_SIZE - 1);
  198. // If the output buffer is full, there's nothing for it other than to
  199. // wait for the interrupt handler to empty it a bit
  200. while (i == tx_buffer.tail) {
  201. if (!TEST(SREG, SREG_I)) {
  202. // Interrupts are disabled, so we'll have to poll the data
  203. // register empty flag ourselves. If it is set, pretend an
  204. // interrupt has happened and call the handler to free up
  205. // space for us.
  206. if (TEST(M_UCSRxA, M_UDREx))
  207. _tx_udr_empty_irq();
  208. } else {
  209. // nop, the interrupt handler will free up space for us
  210. }
  211. }
  212. tx_buffer.buffer[tx_buffer.head] = c;
  213. { CRITICAL_SECTION_START;
  214. tx_buffer.head = i;
  215. SBI(M_UCSRxB, M_UDRIEx);
  216. CRITICAL_SECTION_END;
  217. }
  218. return;
  219. }
  220. void MarlinSerial::flushTX(void) {
  221. // TX
  222. // If we have never written a byte, no need to flush. This special
  223. // case is needed since there is no way to force the TXC (transmit
  224. // complete) bit to 1 during initialization
  225. if (!_written)
  226. return;
  227. while (TEST(M_UCSRxB, M_UDRIEx) || !TEST(M_UCSRxA, M_TXCx)) {
  228. if (!TEST(SREG, SREG_I) && TEST(M_UCSRxB, M_UDRIEx))
  229. // Interrupts are globally disabled, but the DR empty
  230. // interrupt should be enabled, so poll the DR empty flag to
  231. // prevent deadlock
  232. if (TEST(M_UCSRxA, M_UDREx))
  233. _tx_udr_empty_irq();
  234. }
  235. // If we get here, nothing is queued anymore (DRIE is disabled) and
  236. // the hardware finished tranmission (TXC is set).
  237. }
  238. #else
  239. void MarlinSerial::write(uint8_t c) {
  240. while (!TEST(M_UCSRxA, M_UDREx))
  241. ;
  242. M_UDRx = c;
  243. }
  244. #endif
  245. // end NEW
  246. /// imports from print.h
  247. void MarlinSerial::print(char c, int base) {
  248. print((long) c, base);
  249. }
  250. void MarlinSerial::print(unsigned char b, int base) {
  251. print((unsigned long) b, base);
  252. }
  253. void MarlinSerial::print(int n, int base) {
  254. print((long) n, base);
  255. }
  256. void MarlinSerial::print(unsigned int n, int base) {
  257. print((unsigned long) n, base);
  258. }
  259. void MarlinSerial::print(long n, int base) {
  260. if (base == 0) {
  261. write(n);
  262. }
  263. else if (base == 10) {
  264. if (n < 0) {
  265. print('-');
  266. n = -n;
  267. }
  268. printNumber(n, 10);
  269. }
  270. else {
  271. printNumber(n, base);
  272. }
  273. }
  274. void MarlinSerial::print(unsigned long n, int base) {
  275. if (base == 0) write(n);
  276. else printNumber(n, base);
  277. }
  278. void MarlinSerial::print(double n, int digits) {
  279. printFloat(n, digits);
  280. }
  281. void MarlinSerial::println(void) {
  282. print('\r');
  283. print('\n');
  284. }
  285. void MarlinSerial::println(const String& s) {
  286. print(s);
  287. println();
  288. }
  289. void MarlinSerial::println(const char c[]) {
  290. print(c);
  291. println();
  292. }
  293. void MarlinSerial::println(char c, int base) {
  294. print(c, base);
  295. println();
  296. }
  297. void MarlinSerial::println(unsigned char b, int base) {
  298. print(b, base);
  299. println();
  300. }
  301. void MarlinSerial::println(int n, int base) {
  302. print(n, base);
  303. println();
  304. }
  305. void MarlinSerial::println(unsigned int n, int base) {
  306. print(n, base);
  307. println();
  308. }
  309. void MarlinSerial::println(long n, int base) {
  310. print(n, base);
  311. println();
  312. }
  313. void MarlinSerial::println(unsigned long n, int base) {
  314. print(n, base);
  315. println();
  316. }
  317. void MarlinSerial::println(double n, int digits) {
  318. print(n, digits);
  319. println();
  320. }
  321. // Private Methods /////////////////////////////////////////////////////////////
  322. void MarlinSerial::printNumber(unsigned long n, uint8_t base) {
  323. if (n) {
  324. unsigned char buf[8 * sizeof(long)]; // Enough space for base 2
  325. int8_t i = 0;
  326. while (n) {
  327. buf[i++] = n % base;
  328. n /= base;
  329. }
  330. while (i--)
  331. print((char)(buf[i] + (buf[i] < 10 ? '0' : 'A' - 10)));
  332. }
  333. else
  334. print('0');
  335. }
  336. void MarlinSerial::printFloat(double number, uint8_t digits) {
  337. // Handle negative numbers
  338. if (number < 0.0) {
  339. print('-');
  340. number = -number;
  341. }
  342. // Round correctly so that print(1.999, 2) prints as "2.00"
  343. double rounding = 0.5;
  344. for (uint8_t i = 0; i < digits; ++i)
  345. rounding *= 0.1;
  346. number += rounding;
  347. // Extract the integer part of the number and print it
  348. unsigned long int_part = (unsigned long)number;
  349. double remainder = number - (double)int_part;
  350. print(int_part);
  351. // Print the decimal point, but only if there are digits beyond
  352. if (digits) {
  353. print('.');
  354. // Extract digits from the remainder one at a time
  355. while (digits--) {
  356. remainder *= 10.0;
  357. int toPrint = int(remainder);
  358. print(toPrint);
  359. remainder -= toPrint;
  360. }
  361. }
  362. }
  363. // Preinstantiate Objects //////////////////////////////////////////////////////
  364. MarlinSerial customizedSerial;
  365. #endif // whole file
  366. #endif // !USBCON
  367. // For AT90USB targets use the UART for BT interfacing
  368. #if defined(USBCON) && ENABLED(BLUETOOTH)
  369. HardwareSerial bluetoothSerial;
  370. #endif
  371. #if ENABLED(EMERGENCY_PARSER)
  372. // Currently looking for: M108, M112, M410
  373. // If you alter the parser please don't forget to update the capabilities in Conditionals_post.h
  374. FORCE_INLINE void emergency_parser(unsigned char c) {
  375. static e_parser_state state = state_RESET;
  376. switch (state) {
  377. case state_RESET:
  378. switch (c) {
  379. case ' ': break;
  380. case 'N': state = state_N; break;
  381. case 'M': state = state_M; break;
  382. default: state = state_IGNORE;
  383. }
  384. break;
  385. case state_N:
  386. switch (c) {
  387. case '0': case '1': case '2':
  388. case '3': case '4': case '5':
  389. case '6': case '7': case '8':
  390. case '9': case '-': case ' ': break;
  391. case 'M': state = state_M; break;
  392. default: state = state_IGNORE;
  393. }
  394. break;
  395. case state_M:
  396. switch (c) {
  397. case ' ': break;
  398. case '1': state = state_M1; break;
  399. case '4': state = state_M4; break;
  400. default: state = state_IGNORE;
  401. }
  402. break;
  403. case state_M1:
  404. switch (c) {
  405. case '0': state = state_M10; break;
  406. case '1': state = state_M11; break;
  407. default: state = state_IGNORE;
  408. }
  409. break;
  410. case state_M10:
  411. state = (c == '8') ? state_M108 : state_IGNORE;
  412. break;
  413. case state_M11:
  414. state = (c == '2') ? state_M112 : state_IGNORE;
  415. break;
  416. case state_M4:
  417. state = (c == '1') ? state_M41 : state_IGNORE;
  418. break;
  419. case state_M41:
  420. state = (c == '0') ? state_M410 : state_IGNORE;
  421. break;
  422. case state_IGNORE:
  423. if (c == '\n') state = state_RESET;
  424. break;
  425. default:
  426. if (c == '\n') {
  427. switch (state) {
  428. case state_M108:
  429. wait_for_heatup = false;
  430. #if DISABLED(ULTIPANEL)
  431. wait_for_user = false;
  432. #endif
  433. break;
  434. case state_M112:
  435. kill(PSTR(MSG_KILLED));
  436. break;
  437. case state_M410:
  438. quickstop_stepper();
  439. break;
  440. default:
  441. break;
  442. }
  443. state = state_RESET;
  444. }
  445. }
  446. }
  447. #endif