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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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. HardwareSerial.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. */
  28. #include "Marlin.h"
  29. #include "MarlinSerial.h"
  30. #include "stepper.h"
  31. #ifndef USBCON
  32. // this next line disables the entire HardwareSerial.cpp,
  33. // this is so I can support Attiny series and any other chip without a UART
  34. #if defined(UBRRH) || defined(UBRR0H) || defined(UBRR1H) || defined(UBRR2H) || defined(UBRR3H)
  35. #if UART_PRESENT(SERIAL_PORT)
  36. ring_buffer rx_buffer = { { 0 }, 0, 0 };
  37. #endif
  38. FORCE_INLINE void store_char(unsigned char c) {
  39. CRITICAL_SECTION_START;
  40. uint8_t h = rx_buffer.head;
  41. uint8_t i = (uint8_t)(h + 1) & (RX_BUFFER_SIZE - 1);
  42. // if we should be storing the received character into the location
  43. // just before the tail (meaning that the head would advance to the
  44. // current location of the tail), we're about to overflow the buffer
  45. // and so we don't write the character or advance the head.
  46. if (i != rx_buffer.tail) {
  47. rx_buffer.buffer[h] = c;
  48. rx_buffer.head = i;
  49. }
  50. CRITICAL_SECTION_END;
  51. #if ENABLED(EMERGENCY_PARSER)
  52. emergency_parser(c);
  53. #endif
  54. }
  55. //#elif defined(SIG_USART_RECV)
  56. #if defined(M_USARTx_RX_vect)
  57. // fixed by Mark Sproul this is on the 644/644p
  58. //SIGNAL(SIG_USART_RECV)
  59. SIGNAL(M_USARTx_RX_vect) {
  60. unsigned char c = M_UDRx;
  61. store_char(c);
  62. }
  63. #endif
  64. // Constructors ////////////////////////////////////////////////////////////////
  65. MarlinSerial::MarlinSerial() { }
  66. // Public Methods //////////////////////////////////////////////////////////////
  67. void MarlinSerial::begin(long baud) {
  68. uint16_t baud_setting;
  69. bool useU2X = true;
  70. #if F_CPU == 16000000UL && SERIAL_PORT == 0
  71. // hard-coded exception for compatibility with the bootloader shipped
  72. // with the Duemilanove and previous boards and the firmware on the 8U2
  73. // on the Uno and Mega 2560.
  74. if (baud == 57600) {
  75. useU2X = false;
  76. }
  77. #endif
  78. if (useU2X) {
  79. M_UCSRxA = _BV(M_U2Xx);
  80. baud_setting = (F_CPU / 4 / baud - 1) / 2;
  81. }
  82. else {
  83. M_UCSRxA = 0;
  84. baud_setting = (F_CPU / 8 / baud - 1) / 2;
  85. }
  86. // assign the baud_setting, a.k.a. ubbr (USART Baud Rate Register)
  87. M_UBRRxH = baud_setting >> 8;
  88. M_UBRRxL = baud_setting;
  89. SBI(M_UCSRxB, M_RXENx);
  90. SBI(M_UCSRxB, M_TXENx);
  91. SBI(M_UCSRxB, M_RXCIEx);
  92. }
  93. void MarlinSerial::end() {
  94. CBI(M_UCSRxB, M_RXENx);
  95. CBI(M_UCSRxB, M_TXENx);
  96. CBI(M_UCSRxB, M_RXCIEx);
  97. }
  98. int MarlinSerial::peek(void) {
  99. int v;
  100. CRITICAL_SECTION_START;
  101. uint8_t t = rx_buffer.tail;
  102. if (rx_buffer.head == t) {
  103. v = -1;
  104. }
  105. else {
  106. v = rx_buffer.buffer[t];
  107. }
  108. CRITICAL_SECTION_END;
  109. return v;
  110. }
  111. int MarlinSerial::read(void) {
  112. int v;
  113. CRITICAL_SECTION_START;
  114. uint8_t t = rx_buffer.tail;
  115. if (rx_buffer.head == t) {
  116. v = -1;
  117. }
  118. else {
  119. v = rx_buffer.buffer[t];
  120. rx_buffer.tail = (uint8_t)(t + 1) & (RX_BUFFER_SIZE - 1);
  121. }
  122. CRITICAL_SECTION_END;
  123. return v;
  124. }
  125. void MarlinSerial::flush() {
  126. // don't reverse this or there may be problems if the RX interrupt
  127. // occurs after reading the value of rx_buffer_head but before writing
  128. // the value to rx_buffer_tail; the previous value of rx_buffer_head
  129. // may be written to rx_buffer_tail, making it appear as if the buffer
  130. // were full, not empty.
  131. CRITICAL_SECTION_START;
  132. rx_buffer.head = rx_buffer.tail;
  133. CRITICAL_SECTION_END;
  134. }
  135. /// imports from print.h
  136. void MarlinSerial::print(char c, int base) {
  137. print((long) c, base);
  138. }
  139. void MarlinSerial::print(unsigned char b, int base) {
  140. print((unsigned long) b, base);
  141. }
  142. void MarlinSerial::print(int n, int base) {
  143. print((long) n, base);
  144. }
  145. void MarlinSerial::print(unsigned int n, int base) {
  146. print((unsigned long) n, base);
  147. }
  148. void MarlinSerial::print(long n, int base) {
  149. if (base == 0) {
  150. write(n);
  151. }
  152. else if (base == 10) {
  153. if (n < 0) {
  154. print('-');
  155. n = -n;
  156. }
  157. printNumber(n, 10);
  158. }
  159. else {
  160. printNumber(n, base);
  161. }
  162. }
  163. void MarlinSerial::print(unsigned long n, int base) {
  164. if (base == 0) write(n);
  165. else printNumber(n, base);
  166. }
  167. void MarlinSerial::print(double n, int digits) {
  168. printFloat(n, digits);
  169. }
  170. void MarlinSerial::println(void) {
  171. print('\r');
  172. print('\n');
  173. }
  174. void MarlinSerial::println(const String& s) {
  175. print(s);
  176. println();
  177. }
  178. void MarlinSerial::println(const char c[]) {
  179. print(c);
  180. println();
  181. }
  182. void MarlinSerial::println(char c, int base) {
  183. print(c, base);
  184. println();
  185. }
  186. void MarlinSerial::println(unsigned char b, int base) {
  187. print(b, base);
  188. println();
  189. }
  190. void MarlinSerial::println(int n, int base) {
  191. print(n, base);
  192. println();
  193. }
  194. void MarlinSerial::println(unsigned int n, int base) {
  195. print(n, base);
  196. println();
  197. }
  198. void MarlinSerial::println(long n, int base) {
  199. print(n, base);
  200. println();
  201. }
  202. void MarlinSerial::println(unsigned long n, int base) {
  203. print(n, base);
  204. println();
  205. }
  206. void MarlinSerial::println(double n, int digits) {
  207. print(n, digits);
  208. println();
  209. }
  210. // Private Methods /////////////////////////////////////////////////////////////
  211. void MarlinSerial::printNumber(unsigned long n, uint8_t base) {
  212. unsigned char buf[8 * sizeof(long)]; // Assumes 8-bit chars.
  213. unsigned long i = 0;
  214. if (n == 0) {
  215. print('0');
  216. return;
  217. }
  218. while (n > 0) {
  219. buf[i++] = n % base;
  220. n /= base;
  221. }
  222. for (; i > 0; i--)
  223. print((char)(buf[i - 1] < 10 ?
  224. '0' + buf[i - 1] :
  225. 'A' + buf[i - 1] - 10));
  226. }
  227. void MarlinSerial::printFloat(double number, uint8_t digits) {
  228. // Handle negative numbers
  229. if (number < 0.0) {
  230. print('-');
  231. number = -number;
  232. }
  233. // Round correctly so that print(1.999, 2) prints as "2.00"
  234. double rounding = 0.5;
  235. for (uint8_t i = 0; i < digits; ++i)
  236. rounding /= 10.0;
  237. number += rounding;
  238. // Extract the integer part of the number and print it
  239. unsigned long int_part = (unsigned long)number;
  240. double remainder = number - (double)int_part;
  241. print(int_part);
  242. // Print the decimal point, but only if there are digits beyond
  243. if (digits > 0) print('.');
  244. // Extract digits from the remainder one at a time
  245. while (digits-- > 0) {
  246. remainder *= 10.0;
  247. int toPrint = int(remainder);
  248. print(toPrint);
  249. remainder -= toPrint;
  250. }
  251. }
  252. // Preinstantiate Objects //////////////////////////////////////////////////////
  253. MarlinSerial customizedSerial;
  254. #endif // whole file
  255. #endif // !USBCON
  256. // For AT90USB targets use the UART for BT interfacing
  257. #if defined(USBCON) && ENABLED(BLUETOOTH)
  258. HardwareSerial bluetoothSerial;
  259. #endif
  260. #if ENABLED(EMERGENCY_PARSER)
  261. // Currently looking for: M108, M112, M410
  262. // If you alter the parser please don't forget to update the capabilities in Conditionals.h
  263. void emergency_parser(unsigned char c) {
  264. enum e_parser_state {
  265. state_RESET,
  266. state_M,
  267. state_M1,
  268. state_M10,
  269. state_M11,
  270. state_M2,
  271. state_M3,
  272. state_M4,
  273. state_M41,
  274. state_IGNORE // to '\n'
  275. };
  276. static e_parser_state state = state_RESET;
  277. switch (state) {
  278. case state_RESET:
  279. switch (c) {
  280. case 'M':
  281. state = state_M;
  282. break;
  283. case ';':
  284. state = state_IGNORE;
  285. break;
  286. default: state = state_RESET;
  287. }
  288. break;
  289. case state_M:
  290. switch (c) {
  291. case '1':
  292. state = state_M1;
  293. break;
  294. case '2':
  295. state = state_M2;
  296. break;
  297. case '3':
  298. state = state_M3;
  299. break;
  300. case '4':
  301. state = state_M4;
  302. break;
  303. case ';':
  304. state = state_IGNORE;
  305. break;
  306. default: state = state_RESET;
  307. }
  308. break;
  309. case state_M1:
  310. switch (c) {
  311. case '0':
  312. state = state_M10;
  313. break;
  314. case '1':
  315. state = state_M11;
  316. break;
  317. case ';':
  318. state = state_IGNORE;
  319. break;
  320. default: state = state_RESET;
  321. }
  322. break;
  323. case state_M2:
  324. switch (c) {
  325. case '3': // M23
  326. case '8': // M28
  327. case ';':
  328. state = state_IGNORE;
  329. break;
  330. default: state = state_RESET;
  331. }
  332. break;
  333. case state_M3:
  334. switch (c) {
  335. case '0': // M30
  336. case '2': // M32
  337. case '3': // M33
  338. case ';':
  339. state = state_IGNORE;
  340. break;
  341. default: state = state_RESET;
  342. }
  343. break;
  344. case state_M10:
  345. switch (c) {
  346. case '8': // M108
  347. { state = state_RESET; wait_for_heatup = false; }
  348. break;
  349. case ';':
  350. state = state_IGNORE;
  351. break;
  352. default: state = state_RESET;
  353. }
  354. break;
  355. case state_M11:
  356. switch (c) {
  357. case '2': // M112
  358. state = state_RESET; kill(PSTR(MSG_KILLED));
  359. break;
  360. case '7': // M117
  361. case ';':
  362. state = state_IGNORE;
  363. break;
  364. default: state = state_RESET;
  365. }
  366. break;
  367. case state_M4:
  368. switch (c) {
  369. case '1':
  370. state = state_M41;
  371. break;
  372. case ';':
  373. state = state_IGNORE;
  374. break;
  375. default: state = state_RESET;
  376. }
  377. break;
  378. case state_M41:
  379. switch (c) {
  380. case '0':
  381. { state = state_RESET; stepper.quick_stop(); }
  382. break;
  383. case ';':
  384. state = state_IGNORE;
  385. break;
  386. default: state = state_RESET;
  387. }
  388. break;
  389. case state_IGNORE:
  390. if (c == '\n') state = state_RESET;
  391. break;
  392. default:
  393. state = state_RESET;
  394. }
  395. }
  396. #endif