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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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. // Disable HardwareSerial.cpp to support chips without a UART (Attiny, etc.)
  34. #if !defined(USBCON) && (defined(UBRRH) || defined(UBRR0H) || defined(UBRR1H) || defined(UBRR2H) || defined(UBRR3H))
  35. #if UART_PRESENT(SERIAL_PORT)
  36. ring_buffer_r rx_buffer = { { 0 }, 0, 0 };
  37. #if TX_BUFFER_SIZE > 0
  38. ring_buffer_t tx_buffer = { { 0 }, 0, 0 };
  39. static bool _written;
  40. #endif
  41. #endif
  42. #if ENABLED(EMERGENCY_PARSER)
  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
  117. FORCE_INLINE void store_char(unsigned char c) {
  118. CRITICAL_SECTION_START;
  119. uint8_t h = rx_buffer.head;
  120. uint8_t 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. uint8_t t = tx_buffer.tail;
  139. uint8_t 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. unsigned char c = M_UDRx;
  160. store_char(c);
  161. }
  162. #endif
  163. // Public Methods
  164. void MarlinSerial::begin(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) {
  172. useU2X = false;
  173. }
  174. #endif
  175. if (useU2X) {
  176. M_UCSRxA = _BV(M_U2Xx);
  177. baud_setting = (F_CPU / 4 / baud - 1) / 2;
  178. }
  179. else {
  180. M_UCSRxA = 0;
  181. baud_setting = (F_CPU / 8 / baud - 1) / 2;
  182. }
  183. // assign the baud_setting, a.k.a. ubbr (USART Baud Rate Register)
  184. M_UBRRxH = baud_setting >> 8;
  185. M_UBRRxL = baud_setting;
  186. SBI(M_UCSRxB, M_RXENx);
  187. SBI(M_UCSRxB, M_TXENx);
  188. SBI(M_UCSRxB, M_RXCIEx);
  189. #if TX_BUFFER_SIZE > 0
  190. CBI(M_UCSRxB, M_UDRIEx);
  191. _written = false;
  192. #endif
  193. }
  194. void MarlinSerial::end() {
  195. CBI(M_UCSRxB, M_RXENx);
  196. CBI(M_UCSRxB, M_TXENx);
  197. CBI(M_UCSRxB, M_RXCIEx);
  198. CBI(M_UCSRxB, M_UDRIEx);
  199. }
  200. void MarlinSerial::checkRx(void) {
  201. if (TEST(M_UCSRxA, M_RXCx)) {
  202. uint8_t c = M_UDRx;
  203. store_char(c);
  204. }
  205. }
  206. int MarlinSerial::peek(void) {
  207. CRITICAL_SECTION_START;
  208. int v = rx_buffer.head == rx_buffer.tail ? -1 : rx_buffer.buffer[rx_buffer.tail];
  209. CRITICAL_SECTION_END;
  210. return v;
  211. }
  212. int MarlinSerial::read(void) {
  213. int v;
  214. CRITICAL_SECTION_START;
  215. uint8_t t = rx_buffer.tail;
  216. if (rx_buffer.head == t) {
  217. v = -1;
  218. }
  219. else {
  220. v = rx_buffer.buffer[t];
  221. rx_buffer.tail = (uint8_t)(t + 1) & (RX_BUFFER_SIZE - 1);
  222. }
  223. CRITICAL_SECTION_END;
  224. return v;
  225. }
  226. uint8_t MarlinSerial::available(void) {
  227. CRITICAL_SECTION_START;
  228. uint8_t h = rx_buffer.head,
  229. t = rx_buffer.tail;
  230. CRITICAL_SECTION_END;
  231. return (uint8_t)(RX_BUFFER_SIZE + h - t) & (RX_BUFFER_SIZE - 1);
  232. }
  233. void MarlinSerial::flush(void) {
  234. // RX
  235. // don't reverse this or there may be problems if the RX interrupt
  236. // occurs after reading the value of rx_buffer_head but before writing
  237. // the value to rx_buffer_tail; the previous value of rx_buffer_head
  238. // may be written to rx_buffer_tail, making it appear as if the buffer
  239. // were full, not empty.
  240. CRITICAL_SECTION_START;
  241. rx_buffer.head = rx_buffer.tail;
  242. CRITICAL_SECTION_END;
  243. }
  244. #if TX_BUFFER_SIZE > 0
  245. uint8_t MarlinSerial::availableForWrite(void) {
  246. CRITICAL_SECTION_START;
  247. uint8_t h = tx_buffer.head;
  248. uint8_t t = tx_buffer.tail;
  249. CRITICAL_SECTION_END;
  250. return (uint8_t)(TX_BUFFER_SIZE + h - t) & (TX_BUFFER_SIZE - 1);
  251. }
  252. void MarlinSerial::write(uint8_t c) {
  253. _written = true;
  254. CRITICAL_SECTION_START;
  255. bool emty = (tx_buffer.head == tx_buffer.tail);
  256. CRITICAL_SECTION_END;
  257. // If the buffer and the data register is empty, just write the byte
  258. // to the data register and be done. This shortcut helps
  259. // significantly improve the effective datarate at high (>
  260. // 500kbit/s) bitrates, where interrupt overhead becomes a slowdown.
  261. if (emty && TEST(M_UCSRxA, M_UDREx)) {
  262. CRITICAL_SECTION_START;
  263. M_UDRx = c;
  264. SBI(M_UCSRxA, M_TXCx);
  265. CRITICAL_SECTION_END;
  266. return;
  267. }
  268. uint8_t i = (tx_buffer.head + 1) & (TX_BUFFER_SIZE - 1);
  269. // If the output buffer is full, there's nothing for it other than to
  270. // wait for the interrupt handler to empty it a bit
  271. while (i == tx_buffer.tail) {
  272. if (!TEST(SREG, SREG_I)) {
  273. // Interrupts are disabled, so we'll have to poll the data
  274. // register empty flag ourselves. If it is set, pretend an
  275. // interrupt has happened and call the handler to free up
  276. // space for us.
  277. if (TEST(M_UCSRxA, M_UDREx))
  278. _tx_udr_empty_irq();
  279. } else {
  280. // nop, the interrupt handler will free up space for us
  281. }
  282. }
  283. tx_buffer.buffer[tx_buffer.head] = c;
  284. { CRITICAL_SECTION_START;
  285. tx_buffer.head = i;
  286. SBI(M_UCSRxB, M_UDRIEx);
  287. CRITICAL_SECTION_END;
  288. }
  289. return;
  290. }
  291. void MarlinSerial::flushTX(void) {
  292. // TX
  293. // If we have never written a byte, no need to flush. This special
  294. // case is needed since there is no way to force the TXC (transmit
  295. // complete) bit to 1 during initialization
  296. if (!_written)
  297. return;
  298. while (TEST(M_UCSRxB, M_UDRIEx) || !TEST(M_UCSRxA, M_TXCx)) {
  299. if (!TEST(SREG, SREG_I) && TEST(M_UCSRxB, M_UDRIEx))
  300. // Interrupts are globally disabled, but the DR empty
  301. // interrupt should be enabled, so poll the DR empty flag to
  302. // prevent deadlock
  303. if (TEST(M_UCSRxA, M_UDREx))
  304. _tx_udr_empty_irq();
  305. }
  306. // If we get here, nothing is queued anymore (DRIE is disabled) and
  307. // the hardware finished tranmission (TXC is set).
  308. }
  309. #else
  310. void MarlinSerial::write(uint8_t c) {
  311. while (!TEST(M_UCSRxA, M_UDREx))
  312. ;
  313. M_UDRx = c;
  314. }
  315. #endif
  316. // end NEW
  317. /// imports from print.h
  318. void MarlinSerial::print(char c, int base) {
  319. print((long) c, base);
  320. }
  321. void MarlinSerial::print(unsigned char b, int base) {
  322. print((unsigned long) b, base);
  323. }
  324. void MarlinSerial::print(int n, int base) {
  325. print((long) n, base);
  326. }
  327. void MarlinSerial::print(unsigned int n, int base) {
  328. print((unsigned long) n, base);
  329. }
  330. void MarlinSerial::print(long n, int base) {
  331. if (base == 0) {
  332. write(n);
  333. }
  334. else if (base == 10) {
  335. if (n < 0) {
  336. print('-');
  337. n = -n;
  338. }
  339. printNumber(n, 10);
  340. }
  341. else {
  342. printNumber(n, base);
  343. }
  344. }
  345. void MarlinSerial::print(unsigned long n, int base) {
  346. if (base == 0) write(n);
  347. else printNumber(n, base);
  348. }
  349. void MarlinSerial::print(double n, int digits) {
  350. printFloat(n, digits);
  351. }
  352. void MarlinSerial::println(void) {
  353. print('\r');
  354. print('\n');
  355. }
  356. void MarlinSerial::println(const String& s) {
  357. print(s);
  358. println();
  359. }
  360. void MarlinSerial::println(const char c[]) {
  361. print(c);
  362. println();
  363. }
  364. void MarlinSerial::println(char c, int base) {
  365. print(c, base);
  366. println();
  367. }
  368. void MarlinSerial::println(unsigned char b, int base) {
  369. print(b, base);
  370. println();
  371. }
  372. void MarlinSerial::println(int n, int base) {
  373. print(n, base);
  374. println();
  375. }
  376. void MarlinSerial::println(unsigned int n, int base) {
  377. print(n, base);
  378. println();
  379. }
  380. void MarlinSerial::println(long n, int base) {
  381. print(n, base);
  382. println();
  383. }
  384. void MarlinSerial::println(unsigned long n, int base) {
  385. print(n, base);
  386. println();
  387. }
  388. void MarlinSerial::println(double n, int digits) {
  389. print(n, digits);
  390. println();
  391. }
  392. // Private Methods
  393. void MarlinSerial::printNumber(unsigned long n, uint8_t base) {
  394. if (n) {
  395. unsigned char buf[8 * sizeof(long)]; // Enough space for base 2
  396. int8_t i = 0;
  397. while (n) {
  398. buf[i++] = n % base;
  399. n /= base;
  400. }
  401. while (i--)
  402. print((char)(buf[i] + (buf[i] < 10 ? '0' : 'A' - 10)));
  403. }
  404. else
  405. print('0');
  406. }
  407. void MarlinSerial::printFloat(double number, uint8_t digits) {
  408. // Handle negative numbers
  409. if (number < 0.0) {
  410. print('-');
  411. number = -number;
  412. }
  413. // Round correctly so that print(1.999, 2) prints as "2.00"
  414. double rounding = 0.5;
  415. for (uint8_t i = 0; i < digits; ++i)
  416. rounding *= 0.1;
  417. number += rounding;
  418. // Extract the integer part of the number and print it
  419. unsigned long int_part = (unsigned long)number;
  420. double remainder = number - (double)int_part;
  421. print(int_part);
  422. // Print the decimal point, but only if there are digits beyond
  423. if (digits) {
  424. print('.');
  425. // Extract digits from the remainder one at a time
  426. while (digits--) {
  427. remainder *= 10.0;
  428. int toPrint = int(remainder);
  429. print(toPrint);
  430. remainder -= toPrint;
  431. }
  432. }
  433. }
  434. // Preinstantiate
  435. MarlinSerial customizedSerial;
  436. #endif // !USBCON && (UBRRH || UBRR0H || UBRR1H || UBRR2H || UBRR3H)
  437. // For AT90USB targets use the UART for BT interfacing
  438. #if defined(USBCON) && ENABLED(BLUETOOTH)
  439. HardwareSerial bluetoothSerial;
  440. #endif