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_Due.cpp 6.6KB

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