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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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. }
  277. else {
  278. // nop, the interrupt handler will free up space for us
  279. }
  280. }
  281. tx_buffer.buffer[tx_buffer.head] = c;
  282. { CRITICAL_SECTION_START;
  283. tx_buffer.head = i;
  284. SBI(M_UCSRxB, M_UDRIEx);
  285. CRITICAL_SECTION_END;
  286. }
  287. return;
  288. }
  289. void MarlinSerial::flushTX(void) {
  290. // TX
  291. // If we have never written a byte, no need to flush. This special
  292. // case is needed since there is no way to force the TXC (transmit
  293. // complete) bit to 1 during initialization
  294. if (!_written)
  295. return;
  296. while (TEST(M_UCSRxB, M_UDRIEx) || !TEST(M_UCSRxA, M_TXCx)) {
  297. if (!TEST(SREG, SREG_I) && TEST(M_UCSRxB, M_UDRIEx))
  298. // Interrupts are globally disabled, but the DR empty
  299. // interrupt should be enabled, so poll the DR empty flag to
  300. // prevent deadlock
  301. if (TEST(M_UCSRxA, M_UDREx))
  302. _tx_udr_empty_irq();
  303. }
  304. // If we get here, nothing is queued anymore (DRIE is disabled) and
  305. // the hardware finished tranmission (TXC is set).
  306. }
  307. #else
  308. void MarlinSerial::write(uint8_t c) {
  309. while (!TEST(M_UCSRxA, M_UDREx))
  310. ;
  311. M_UDRx = c;
  312. }
  313. #endif
  314. // end NEW
  315. /// imports from print.h
  316. void MarlinSerial::print(char c, int base) {
  317. print((long)c, base);
  318. }
  319. void MarlinSerial::print(unsigned char b, int base) {
  320. print((unsigned long)b, base);
  321. }
  322. void MarlinSerial::print(int n, int base) {
  323. print((long)n, base);
  324. }
  325. void MarlinSerial::print(unsigned int n, int base) {
  326. print((unsigned long)n, base);
  327. }
  328. void MarlinSerial::print(long n, int base) {
  329. if (base == 0)
  330. write(n);
  331. else if (base == 10) {
  332. if (n < 0) {
  333. print('-');
  334. n = -n;
  335. }
  336. printNumber(n, 10);
  337. }
  338. else
  339. printNumber(n, base);
  340. }
  341. void MarlinSerial::print(unsigned long n, int base) {
  342. if (base == 0) write(n);
  343. else printNumber(n, base);
  344. }
  345. void MarlinSerial::print(double n, int digits) {
  346. printFloat(n, digits);
  347. }
  348. void MarlinSerial::println(void) {
  349. print('\r');
  350. print('\n');
  351. }
  352. void MarlinSerial::println(const String& s) {
  353. print(s);
  354. println();
  355. }
  356. void MarlinSerial::println(const char c[]) {
  357. print(c);
  358. println();
  359. }
  360. void MarlinSerial::println(char c, int base) {
  361. print(c, base);
  362. println();
  363. }
  364. void MarlinSerial::println(unsigned char b, int base) {
  365. print(b, base);
  366. println();
  367. }
  368. void MarlinSerial::println(int n, int base) {
  369. print(n, base);
  370. println();
  371. }
  372. void MarlinSerial::println(unsigned int n, int base) {
  373. print(n, base);
  374. println();
  375. }
  376. void MarlinSerial::println(long n, int base) {
  377. print(n, base);
  378. println();
  379. }
  380. void MarlinSerial::println(unsigned long n, int base) {
  381. print(n, base);
  382. println();
  383. }
  384. void MarlinSerial::println(double n, int digits) {
  385. print(n, digits);
  386. println();
  387. }
  388. // Private Methods
  389. void MarlinSerial::printNumber(unsigned long n, uint8_t base) {
  390. if (n) {
  391. unsigned char buf[8 * sizeof(long)]; // Enough space for base 2
  392. int8_t i = 0;
  393. while (n) {
  394. buf[i++] = n % base;
  395. n /= base;
  396. }
  397. while (i--)
  398. print((char)(buf[i] + (buf[i] < 10 ? '0' : 'A' - 10)));
  399. }
  400. else
  401. print('0');
  402. }
  403. void MarlinSerial::printFloat(double number, uint8_t digits) {
  404. // Handle negative numbers
  405. if (number < 0.0) {
  406. print('-');
  407. number = -number;
  408. }
  409. // Round correctly so that print(1.999, 2) prints as "2.00"
  410. double rounding = 0.5;
  411. for (uint8_t i = 0; i < digits; ++i)
  412. rounding *= 0.1;
  413. number += rounding;
  414. // Extract the integer part of the number and print it
  415. unsigned long int_part = (unsigned long)number;
  416. double remainder = number - (double)int_part;
  417. print(int_part);
  418. // Print the decimal point, but only if there are digits beyond
  419. if (digits) {
  420. print('.');
  421. // Extract digits from the remainder one at a time
  422. while (digits--) {
  423. remainder *= 10.0;
  424. int toPrint = int(remainder);
  425. print(toPrint);
  426. remainder -= toPrint;
  427. }
  428. }
  429. }
  430. // Preinstantiate
  431. MarlinSerial customizedSerial;
  432. #endif // !USBCON && (UBRRH || UBRR0H || UBRR1H || UBRR2H || UBRR3H)
  433. // For AT90USB targets use the UART for BT interfacing
  434. #if defined(USBCON) && ENABLED(BLUETOOTH)
  435. HardwareSerial bluetoothSerial;
  436. #endif