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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. #ifndef USBCON
  31. // this next line disables the entire HardwareSerial.cpp,
  32. // this is so I can support Attiny series and any other chip without a UART
  33. #if defined(UBRRH) || defined(UBRR0H) || defined(UBRR1H) || defined(UBRR2H) || defined(UBRR3H)
  34. #if UART_PRESENT(SERIAL_PORT)
  35. ring_buffer rx_buffer = { { 0 }, 0, 0 };
  36. #endif
  37. FORCE_INLINE void store_char(unsigned char c) {
  38. CRITICAL_SECTION_START;
  39. uint8_t h = rx_buffer.head;
  40. uint8_t i = (uint8_t)(h + 1) & (RX_BUFFER_SIZE - 1);
  41. // if we should be storing the received character into the location
  42. // just before the tail (meaning that the head would advance to the
  43. // current location of the tail), we're about to overflow the buffer
  44. // and so we don't write the character or advance the head.
  45. if (i != rx_buffer.tail) {
  46. rx_buffer.buffer[h] = c;
  47. rx_buffer.head = i;
  48. }
  49. CRITICAL_SECTION_END;
  50. }
  51. //#elif defined(SIG_USART_RECV)
  52. #if defined(M_USARTx_RX_vect)
  53. // fixed by Mark Sproul this is on the 644/644p
  54. //SIGNAL(SIG_USART_RECV)
  55. SIGNAL(M_USARTx_RX_vect) {
  56. unsigned char c = M_UDRx;
  57. store_char(c);
  58. }
  59. #endif
  60. // Constructors ////////////////////////////////////////////////////////////////
  61. MarlinSerial::MarlinSerial() { }
  62. // Public Methods //////////////////////////////////////////////////////////////
  63. void MarlinSerial::begin(long baud) {
  64. uint16_t baud_setting;
  65. bool useU2X = true;
  66. #if F_CPU == 16000000UL && SERIAL_PORT == 0
  67. // hard-coded exception for compatibility with the bootloader shipped
  68. // with the Duemilanove and previous boards and the firmware on the 8U2
  69. // on the Uno and Mega 2560.
  70. if (baud == 57600) {
  71. useU2X = false;
  72. }
  73. #endif
  74. if (useU2X) {
  75. M_UCSRxA = _BV(M_U2Xx);
  76. baud_setting = (F_CPU / 4 / baud - 1) / 2;
  77. }
  78. else {
  79. M_UCSRxA = 0;
  80. baud_setting = (F_CPU / 8 / baud - 1) / 2;
  81. }
  82. // assign the baud_setting, a.k.a. ubbr (USART Baud Rate Register)
  83. M_UBRRxH = baud_setting >> 8;
  84. M_UBRRxL = baud_setting;
  85. SBI(M_UCSRxB, M_RXENx);
  86. SBI(M_UCSRxB, M_TXENx);
  87. SBI(M_UCSRxB, M_RXCIEx);
  88. }
  89. void MarlinSerial::end() {
  90. CBI(M_UCSRxB, M_RXENx);
  91. CBI(M_UCSRxB, M_TXENx);
  92. CBI(M_UCSRxB, M_RXCIEx);
  93. }
  94. int MarlinSerial::peek(void) {
  95. int v;
  96. CRITICAL_SECTION_START;
  97. uint8_t t = rx_buffer.tail;
  98. if (rx_buffer.head == t) {
  99. v = -1;
  100. }
  101. else {
  102. v = rx_buffer.buffer[t];
  103. }
  104. CRITICAL_SECTION_END;
  105. return v;
  106. }
  107. int MarlinSerial::read(void) {
  108. int v;
  109. CRITICAL_SECTION_START;
  110. uint8_t t = rx_buffer.tail;
  111. if (rx_buffer.head == t) {
  112. v = -1;
  113. }
  114. else {
  115. v = rx_buffer.buffer[t];
  116. rx_buffer.tail = (uint8_t)(t + 1) & (RX_BUFFER_SIZE - 1);
  117. }
  118. CRITICAL_SECTION_END;
  119. return v;
  120. }
  121. void MarlinSerial::flush() {
  122. // don't reverse this or there may be problems if the RX interrupt
  123. // occurs after reading the value of rx_buffer_head but before writing
  124. // the value to rx_buffer_tail; the previous value of rx_buffer_head
  125. // may be written to rx_buffer_tail, making it appear as if the buffer
  126. // were full, not empty.
  127. CRITICAL_SECTION_START;
  128. rx_buffer.head = rx_buffer.tail;
  129. CRITICAL_SECTION_END;
  130. }
  131. /// imports from print.h
  132. void MarlinSerial::print(char c, int base) {
  133. print((long) c, base);
  134. }
  135. void MarlinSerial::print(unsigned char b, int base) {
  136. print((unsigned long) b, base);
  137. }
  138. void MarlinSerial::print(int n, int base) {
  139. print((long) n, base);
  140. }
  141. void MarlinSerial::print(unsigned int n, int base) {
  142. print((unsigned long) n, base);
  143. }
  144. void MarlinSerial::print(long n, int base) {
  145. if (base == 0) {
  146. write(n);
  147. }
  148. else if (base == 10) {
  149. if (n < 0) {
  150. print('-');
  151. n = -n;
  152. }
  153. printNumber(n, 10);
  154. }
  155. else {
  156. printNumber(n, base);
  157. }
  158. }
  159. void MarlinSerial::print(unsigned long n, int base) {
  160. if (base == 0) write(n);
  161. else printNumber(n, base);
  162. }
  163. void MarlinSerial::print(double n, int digits) {
  164. printFloat(n, digits);
  165. }
  166. void MarlinSerial::println(void) {
  167. print('\r');
  168. print('\n');
  169. }
  170. void MarlinSerial::println(const String& s) {
  171. print(s);
  172. println();
  173. }
  174. void MarlinSerial::println(const char c[]) {
  175. print(c);
  176. println();
  177. }
  178. void MarlinSerial::println(char c, int base) {
  179. print(c, base);
  180. println();
  181. }
  182. void MarlinSerial::println(unsigned char b, int base) {
  183. print(b, base);
  184. println();
  185. }
  186. void MarlinSerial::println(int n, int base) {
  187. print(n, base);
  188. println();
  189. }
  190. void MarlinSerial::println(unsigned int n, int base) {
  191. print(n, base);
  192. println();
  193. }
  194. void MarlinSerial::println(long n, int base) {
  195. print(n, base);
  196. println();
  197. }
  198. void MarlinSerial::println(unsigned long n, int base) {
  199. print(n, base);
  200. println();
  201. }
  202. void MarlinSerial::println(double n, int digits) {
  203. print(n, digits);
  204. println();
  205. }
  206. // Private Methods /////////////////////////////////////////////////////////////
  207. void MarlinSerial::printNumber(unsigned long n, uint8_t base) {
  208. unsigned char buf[8 * sizeof(long)]; // Assumes 8-bit chars.
  209. unsigned long i = 0;
  210. if (n == 0) {
  211. print('0');
  212. return;
  213. }
  214. while (n > 0) {
  215. buf[i++] = n % base;
  216. n /= base;
  217. }
  218. for (; i > 0; i--)
  219. print((char)(buf[i - 1] < 10 ?
  220. '0' + buf[i - 1] :
  221. 'A' + buf[i - 1] - 10));
  222. }
  223. void MarlinSerial::printFloat(double number, uint8_t digits) {
  224. // Handle negative numbers
  225. if (number < 0.0) {
  226. print('-');
  227. number = -number;
  228. }
  229. // Round correctly so that print(1.999, 2) prints as "2.00"
  230. double rounding = 0.5;
  231. for (uint8_t i = 0; i < digits; ++i)
  232. rounding /= 10.0;
  233. number += rounding;
  234. // Extract the integer part of the number and print it
  235. unsigned long int_part = (unsigned long)number;
  236. double remainder = number - (double)int_part;
  237. print(int_part);
  238. // Print the decimal point, but only if there are digits beyond
  239. if (digits > 0) print('.');
  240. // Extract digits from the remainder one at a time
  241. while (digits-- > 0) {
  242. remainder *= 10.0;
  243. int toPrint = int(remainder);
  244. print(toPrint);
  245. remainder -= toPrint;
  246. }
  247. }
  248. // Preinstantiate Objects //////////////////////////////////////////////////////
  249. MarlinSerial customizedSerial;
  250. #endif // whole file
  251. #endif // !USBCON
  252. // For AT90USB targets use the UART for BT interfacing
  253. #if defined(USBCON) && ENABLED(BLUETOOTH)
  254. HardwareSerial bluetoothSerial;
  255. #endif