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.

MarlinSerialUSB.cpp 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 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 <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * MarlinSerial_Due.cpp - Hardware serial library for Arduino DUE
  24. * Copyright (c) 2017 Eduardo José Tagle. All right reserved
  25. * Based on MarlinSerial for AVR, copyright (c) 2006 Nicholas Zambetti. All right reserved.
  26. */
  27. #ifdef ARDUINO_ARCH_SAM
  28. #include "../../inc/MarlinConfig.h"
  29. #if HAS_USB_SERIAL
  30. #include "MarlinSerialUSB.h"
  31. #if ENABLED(EMERGENCY_PARSER)
  32. #include "../../feature/e_parser.h"
  33. #endif
  34. // Imports from Atmel USB Stack/CDC implementation
  35. extern "C" {
  36. bool usb_task_cdc_isenabled();
  37. bool usb_task_cdc_dtr_active();
  38. bool udi_cdc_is_rx_ready();
  39. int udi_cdc_getc();
  40. bool udi_cdc_is_tx_ready();
  41. int udi_cdc_putc(int value);
  42. };
  43. // Pending character
  44. static int pending_char = -1;
  45. #if ENABLED(EMERGENCY_PARSER)
  46. static EmergencyParser::State emergency_state; // = EP_RESET
  47. #endif
  48. // Public Methods
  49. void MarlinSerialUSB::begin(const long) {}
  50. void MarlinSerialUSB::end() {}
  51. int MarlinSerialUSB::peek() {
  52. if (pending_char >= 0)
  53. return pending_char;
  54. // If USB CDC not enumerated or not configured on the PC side
  55. if (!usb_task_cdc_isenabled())
  56. return -1;
  57. // If no bytes sent from the PC
  58. if (!udi_cdc_is_rx_ready())
  59. return -1;
  60. pending_char = udi_cdc_getc();
  61. TERN_(EMERGENCY_PARSER, emergency_parser.update(emergency_state, (char)pending_char));
  62. return pending_char;
  63. }
  64. int MarlinSerialUSB::read() {
  65. if (pending_char >= 0) {
  66. int ret = pending_char;
  67. pending_char = -1;
  68. return ret;
  69. }
  70. // If USB CDC not enumerated or not configured on the PC side
  71. if (!usb_task_cdc_isenabled())
  72. return -1;
  73. // If no bytes sent from the PC
  74. if (!udi_cdc_is_rx_ready())
  75. return -1;
  76. int c = udi_cdc_getc();
  77. TERN_(EMERGENCY_PARSER, emergency_parser.update(emergency_state, (char)c));
  78. return c;
  79. }
  80. bool MarlinSerialUSB::available() {
  81. /* If Pending chars */
  82. return pending_char >= 0 ||
  83. /* or USB CDC enumerated and configured on the PC side and some
  84. bytes where sent to us */
  85. (usb_task_cdc_isenabled() && udi_cdc_is_rx_ready());
  86. }
  87. void MarlinSerialUSB::flush() { }
  88. void MarlinSerialUSB::flushTX() { }
  89. void MarlinSerialUSB::write(const uint8_t c) {
  90. /* Do not even bother sending anything if USB CDC is not enumerated
  91. or not configured on the PC side or there is no program on the PC
  92. listening to our messages */
  93. if (!usb_task_cdc_isenabled() || !usb_task_cdc_dtr_active())
  94. return;
  95. /* Wait until the PC has read the pending to be sent data */
  96. while (usb_task_cdc_isenabled() &&
  97. usb_task_cdc_dtr_active() &&
  98. !udi_cdc_is_tx_ready()) {
  99. };
  100. /* Do not even bother sending anything if USB CDC is not enumerated
  101. or not configured on the PC side or there is no program on the PC
  102. listening to our messages at this point */
  103. if (!usb_task_cdc_isenabled() || !usb_task_cdc_dtr_active())
  104. return;
  105. // Fifo full
  106. // udi_cdc_signal_overrun();
  107. udi_cdc_putc(c);
  108. }
  109. /**
  110. * Imports from print.h
  111. */
  112. void MarlinSerialUSB::print(char c, int base) {
  113. print((long)c, base);
  114. }
  115. void MarlinSerialUSB::print(unsigned char b, int base) {
  116. print((unsigned long)b, base);
  117. }
  118. void MarlinSerialUSB::print(int n, int base) {
  119. print((long)n, base);
  120. }
  121. void MarlinSerialUSB::print(unsigned int n, int base) {
  122. print((unsigned long)n, base);
  123. }
  124. void MarlinSerialUSB::print(long n, int base) {
  125. if (base == 0)
  126. write(n);
  127. else if (base == 10) {
  128. if (n < 0) {
  129. print('-');
  130. n = -n;
  131. }
  132. printNumber(n, 10);
  133. }
  134. else
  135. printNumber(n, base);
  136. }
  137. void MarlinSerialUSB::print(unsigned long n, int base) {
  138. if (base == 0) write(n);
  139. else printNumber(n, base);
  140. }
  141. void MarlinSerialUSB::print(double n, int digits) {
  142. printFloat(n, digits);
  143. }
  144. void MarlinSerialUSB::println() {
  145. print('\r');
  146. print('\n');
  147. }
  148. void MarlinSerialUSB::println(const String& s) {
  149. print(s);
  150. println();
  151. }
  152. void MarlinSerialUSB::println(const char c[]) {
  153. print(c);
  154. println();
  155. }
  156. void MarlinSerialUSB::println(char c, int base) {
  157. print(c, base);
  158. println();
  159. }
  160. void MarlinSerialUSB::println(unsigned char b, int base) {
  161. print(b, base);
  162. println();
  163. }
  164. void MarlinSerialUSB::println(int n, int base) {
  165. print(n, base);
  166. println();
  167. }
  168. void MarlinSerialUSB::println(unsigned int n, int base) {
  169. print(n, base);
  170. println();
  171. }
  172. void MarlinSerialUSB::println(long n, int base) {
  173. print(n, base);
  174. println();
  175. }
  176. void MarlinSerialUSB::println(unsigned long n, int base) {
  177. print(n, base);
  178. println();
  179. }
  180. void MarlinSerialUSB::println(double n, int digits) {
  181. print(n, digits);
  182. println();
  183. }
  184. // Private Methods
  185. void MarlinSerialUSB::printNumber(unsigned long n, uint8_t base) {
  186. if (n) {
  187. unsigned char buf[8 * sizeof(long)]; // Enough space for base 2
  188. int8_t i = 0;
  189. while (n) {
  190. buf[i++] = n % base;
  191. n /= base;
  192. }
  193. while (i--)
  194. print((char)(buf[i] + (buf[i] < 10 ? '0' : 'A' - 10)));
  195. }
  196. else
  197. print('0');
  198. }
  199. void MarlinSerialUSB::printFloat(double number, uint8_t digits) {
  200. // Handle negative numbers
  201. if (number < 0.0) {
  202. print('-');
  203. number = -number;
  204. }
  205. // Round correctly so that print(1.999, 2) prints as "2.00"
  206. double rounding = 0.5;
  207. LOOP_L_N(i, digits)
  208. rounding *= 0.1;
  209. number += rounding;
  210. // Extract the integer part of the number and print it
  211. unsigned long int_part = (unsigned long)number;
  212. double remainder = number - (double)int_part;
  213. print(int_part);
  214. // Print the decimal point, but only if there are digits beyond
  215. if (digits) {
  216. print('.');
  217. // Extract digits from the remainder one at a time
  218. while (digits--) {
  219. remainder *= 10.0;
  220. int toPrint = int(remainder);
  221. print(toPrint);
  222. remainder -= toPrint;
  223. }
  224. }
  225. }
  226. // Preinstantiate
  227. #if SERIAL_PORT == -1
  228. MarlinSerialUSB customizedSerial1;
  229. #endif
  230. #if SERIAL_PORT_2 == -1
  231. MarlinSerialUSB customizedSerial2;
  232. #endif
  233. #endif // HAS_USB_SERIAL
  234. #endif // ARDUINO_ARCH_SAM