My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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