My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

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