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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. // Imports from Atmel USB Stack/CDC implementation
  32. extern "C" {
  33. bool usb_task_cdc_isenabled();
  34. bool usb_task_cdc_dtr_active();
  35. bool udi_cdc_is_rx_ready();
  36. int udi_cdc_getc();
  37. bool udi_cdc_is_tx_ready();
  38. int udi_cdc_putc(int value);
  39. };
  40. // Pending character
  41. static int pending_char = -1;
  42. // Public Methods
  43. void MarlinSerialUSB::begin(const long) {}
  44. void MarlinSerialUSB::end() {}
  45. int MarlinSerialUSB::peek() {
  46. if (pending_char >= 0)
  47. return pending_char;
  48. // If USB CDC not enumerated or not configured on the PC side
  49. if (!usb_task_cdc_isenabled())
  50. return -1;
  51. // If no bytes sent from the PC
  52. if (!udi_cdc_is_rx_ready())
  53. return -1;
  54. pending_char = udi_cdc_getc();
  55. TERN_(EMERGENCY_PARSER, emergency_parser.update(static_cast<MSerialT*>(this)->emergency_state, (char)pending_char));
  56. return pending_char;
  57. }
  58. int MarlinSerialUSB::read() {
  59. if (pending_char >= 0) {
  60. int ret = pending_char;
  61. pending_char = -1;
  62. return ret;
  63. }
  64. // If USB CDC not enumerated or not configured on the PC side
  65. if (!usb_task_cdc_isenabled())
  66. return -1;
  67. // If no bytes sent from the PC
  68. if (!udi_cdc_is_rx_ready())
  69. return -1;
  70. int c = udi_cdc_getc();
  71. TERN_(EMERGENCY_PARSER, emergency_parser.update(static_cast<MSerialT*>(this)->emergency_state, (char)c));
  72. return c;
  73. }
  74. bool MarlinSerialUSB::available() {
  75. /* If Pending chars */
  76. return pending_char >= 0 ||
  77. /* or USB CDC enumerated and configured on the PC side and some
  78. bytes where sent to us */
  79. (usb_task_cdc_isenabled() && udi_cdc_is_rx_ready());
  80. }
  81. void MarlinSerialUSB::flush() { }
  82. size_t MarlinSerialUSB::write(const uint8_t c) {
  83. /* Do not even bother sending anything if USB CDC is not enumerated
  84. or not configured on the PC side or there is no program on the PC
  85. listening to our messages */
  86. if (!usb_task_cdc_isenabled() || !usb_task_cdc_dtr_active())
  87. return 0;
  88. /* Wait until the PC has read the pending to be sent data */
  89. while (usb_task_cdc_isenabled() &&
  90. usb_task_cdc_dtr_active() &&
  91. !udi_cdc_is_tx_ready()) {
  92. };
  93. /* Do not even bother sending anything if USB CDC is not enumerated
  94. or not configured on the PC side or there is no program on the PC
  95. listening to our messages at this point */
  96. if (!usb_task_cdc_isenabled() || !usb_task_cdc_dtr_active())
  97. return 0;
  98. // Fifo full
  99. // udi_cdc_signal_overrun();
  100. udi_cdc_putc(c);
  101. return 1;
  102. }
  103. // Preinstantiate
  104. #if SERIAL_PORT == -1
  105. MSerialT customizedSerial1(TERN0(EMERGENCY_PARSER, true));
  106. #endif
  107. #if SERIAL_PORT_2 == -1
  108. MSerialT customizedSerial2(TERN0(EMERGENCY_PARSER, true));
  109. #endif
  110. #endif // HAS_USB_SERIAL
  111. #endif // ARDUINO_ARCH_SAM