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.h 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. #pragma once
  23. /**
  24. * MarlinSerial_Due.h - Hardware serial library for Arduino DUE
  25. * Copyright (c) 2017 Eduardo José Tagle. All right reserved
  26. * Based on MarlinSerial for AVR, copyright (c) 2006 Nicholas Zambetti. All right reserved.
  27. */
  28. #include <WString.h>
  29. #include "../../inc/MarlinConfigPre.h"
  30. #define DEC 10
  31. #define HEX 16
  32. #define OCT 8
  33. #define BIN 2
  34. // Define constants and variables for buffering incoming serial data. We're
  35. // using a ring buffer (I think), in which rx_buffer_head is the index of the
  36. // location to which to write the next incoming character and rx_buffer_tail
  37. // is the index of the location from which to read.
  38. // 256 is the max limit due to uint8_t head and tail. Use only powers of 2. (...,16,32,64,128,256)
  39. #ifndef RX_BUFFER_SIZE
  40. #define RX_BUFFER_SIZE 128
  41. #endif
  42. #ifndef TX_BUFFER_SIZE
  43. #define TX_BUFFER_SIZE 32
  44. #endif
  45. //#if ENABLED(SERIAL_XON_XOFF) && RX_BUFFER_SIZE < 1024
  46. // #error "SERIAL_XON_XOFF requires RX_BUFFER_SIZE >= 1024 for reliable transfers without drops."
  47. //#elif RX_BUFFER_SIZE && (RX_BUFFER_SIZE < 2 || !IS_POWER_OF_2(RX_BUFFER_SIZE))
  48. // #error "RX_BUFFER_SIZE must be a power of 2 greater than 1."
  49. //#elif TX_BUFFER_SIZE && (TX_BUFFER_SIZE < 2 || TX_BUFFER_SIZE > 256 || !IS_POWER_OF_2(TX_BUFFER_SIZE))
  50. // #error "TX_BUFFER_SIZE must be 0, a power of 2 greater than 1, and no greater than 256."
  51. //#endif
  52. // Templated type selector
  53. template<bool b, typename T, typename F> struct TypeSelector { typedef T type;} ;
  54. template<typename T, typename F> struct TypeSelector<false, T, F> { typedef F type; };
  55. // Templated structure wrapper
  56. template<typename S, unsigned int addr> struct StructWrapper {
  57. constexpr StructWrapper(int) {}
  58. FORCE_INLINE S* operator->() const { return (S*)addr; }
  59. };
  60. template<typename Cfg>
  61. class MarlinSerial {
  62. protected:
  63. // Information for all supported UARTs
  64. static constexpr uint32_t BASES[] = {0x400E0800U, 0x40098000U, 0x4009C000U, 0x400A0000U, 0x400A4000U};
  65. static constexpr IRQn_Type IRQS[] = { UART_IRQn, USART0_IRQn, USART1_IRQn, USART2_IRQn, USART3_IRQn};
  66. static constexpr int IRQ_IDS[] = { ID_UART, ID_USART0, ID_USART1, ID_USART2, ID_USART3};
  67. // Alias for shorter code
  68. static constexpr StructWrapper<Uart,BASES[Cfg::PORT]> HWUART = 0;
  69. static constexpr IRQn_Type HWUART_IRQ = IRQS[Cfg::PORT];
  70. static constexpr int HWUART_IRQ_ID = IRQ_IDS[Cfg::PORT];
  71. // Base size of type on buffer size
  72. typedef typename TypeSelector<(Cfg::RX_SIZE>256), uint16_t, uint8_t>::type ring_buffer_pos_t;
  73. struct ring_buffer_r {
  74. volatile ring_buffer_pos_t head, tail;
  75. unsigned char buffer[Cfg::RX_SIZE];
  76. };
  77. struct ring_buffer_t {
  78. volatile uint8_t head, tail;
  79. unsigned char buffer[Cfg::TX_SIZE];
  80. };
  81. static ring_buffer_r rx_buffer;
  82. static ring_buffer_t tx_buffer;
  83. static bool _written;
  84. static constexpr uint8_t XON_XOFF_CHAR_SENT = 0x80, // XON / XOFF Character was sent
  85. XON_XOFF_CHAR_MASK = 0x1F; // XON / XOFF character to send
  86. // XON / XOFF character definitions
  87. static constexpr uint8_t XON_CHAR = 17, XOFF_CHAR = 19;
  88. static uint8_t xon_xoff_state,
  89. rx_dropped_bytes,
  90. rx_buffer_overruns,
  91. rx_framing_errors;
  92. static ring_buffer_pos_t rx_max_enqueued;
  93. FORCE_INLINE static void store_rxd_char();
  94. FORCE_INLINE static void _tx_thr_empty_irq();
  95. static void UART_ISR();
  96. public:
  97. MarlinSerial() {};
  98. static void begin(const long);
  99. static void end();
  100. static int peek();
  101. static int read();
  102. static void flush();
  103. static ring_buffer_pos_t available();
  104. static void write(const uint8_t c);
  105. static void flushTX();
  106. FORCE_INLINE static uint8_t dropped() { return Cfg::DROPPED_RX ? rx_dropped_bytes : 0; }
  107. FORCE_INLINE static uint8_t buffer_overruns() { return Cfg::RX_OVERRUNS ? rx_buffer_overruns : 0; }
  108. FORCE_INLINE static uint8_t framing_errors() { return Cfg::RX_FRAMING_ERRORS ? rx_framing_errors : 0; }
  109. FORCE_INLINE static ring_buffer_pos_t rxMaxEnqueued() { return Cfg::MAX_RX_QUEUED ? rx_max_enqueued : 0; }
  110. FORCE_INLINE static void write(const char* str) { while (*str) write(*str++); }
  111. FORCE_INLINE static void write(const uint8_t* buffer, size_t size) { while (size--) write(*buffer++); }
  112. FORCE_INLINE static void print(const String& s) { for (int i = 0; i < (int)s.length(); i++) write(s[i]); }
  113. FORCE_INLINE static void print(const char* str) { write(str); }
  114. static void print(char, int = 0);
  115. static void print(unsigned char, int = 0);
  116. static void print(int, int = DEC);
  117. static void print(unsigned int, int = DEC);
  118. static void print(long, int = DEC);
  119. static void print(unsigned long, int = DEC);
  120. static void print(double, int = 2);
  121. static void println(const String& s);
  122. static void println(const char[]);
  123. static void println(char, int = 0);
  124. static void println(unsigned char, int = 0);
  125. static void println(int, int = DEC);
  126. static void println(unsigned int, int = DEC);
  127. static void println(long, int = DEC);
  128. static void println(unsigned long, int = DEC);
  129. static void println(double, int = 2);
  130. static void println();
  131. operator bool() { return true; }
  132. private:
  133. static void printNumber(unsigned long, const uint8_t);
  134. static void printFloat(double, uint8_t);
  135. };
  136. // Serial port configuration
  137. template <uint8_t serial>
  138. struct MarlinSerialCfg {
  139. static constexpr int PORT = serial;
  140. static constexpr unsigned int RX_SIZE = RX_BUFFER_SIZE;
  141. static constexpr unsigned int TX_SIZE = TX_BUFFER_SIZE;
  142. static constexpr bool XONOFF = ENABLED(SERIAL_XON_XOFF);
  143. static constexpr bool EMERGENCYPARSER = ENABLED(EMERGENCY_PARSER);
  144. static constexpr bool DROPPED_RX = ENABLED(SERIAL_STATS_DROPPED_RX);
  145. static constexpr bool RX_OVERRUNS = ENABLED(SERIAL_STATS_RX_BUFFER_OVERRUNS);
  146. static constexpr bool RX_FRAMING_ERRORS = ENABLED(SERIAL_STATS_RX_FRAMING_ERRORS);
  147. static constexpr bool MAX_RX_QUEUED = ENABLED(SERIAL_STATS_MAX_RX_QUEUED);
  148. };
  149. #if SERIAL_PORT >= 0
  150. extern MarlinSerial<MarlinSerialCfg<SERIAL_PORT>> customizedSerial1;
  151. #endif
  152. #if defined(SERIAL_PORT_2) && SERIAL_PORT_2 >= 0
  153. extern MarlinSerial<MarlinSerialCfg<SERIAL_PORT_2>> customizedSerial2;
  154. #endif