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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. MarlinSerial.h - Hardware serial library for Wiring
  24. Copyright (c) 2006 Nicholas Zambetti. All right reserved.
  25. Modified 28 September 2010 by Mark Sproul
  26. Modified 14 February 2016 by Andreas Hardtung (added tx buffer)
  27. */
  28. #ifndef MARLINSERIAL_H
  29. #define MARLINSERIAL_H
  30. #include "MarlinConfig.h"
  31. #ifndef SERIAL_PORT
  32. #define SERIAL_PORT 0
  33. #endif
  34. // The presence of the UBRRH register is used to detect a UART.
  35. #define UART_PRESENT(port) ((port == 0 && (defined(UBRRH) || defined(UBRR0H))) || \
  36. (port == 1 && defined(UBRR1H)) || (port == 2 && defined(UBRR2H)) || \
  37. (port == 3 && defined(UBRR3H)))
  38. // These are macros to build serial port register names for the selected SERIAL_PORT (C preprocessor
  39. // requires two levels of indirection to expand macro values properly)
  40. #define SERIAL_REGNAME(registerbase,number,suffix) SERIAL_REGNAME_INTERNAL(registerbase,number,suffix)
  41. #if SERIAL_PORT == 0 && (!defined(UBRR0H) || !defined(UDR0)) // use un-numbered registers if necessary
  42. #define SERIAL_REGNAME_INTERNAL(registerbase,number,suffix) registerbase##suffix
  43. #else
  44. #define SERIAL_REGNAME_INTERNAL(registerbase,number,suffix) registerbase##number##suffix
  45. #endif
  46. // Registers used by MarlinSerial class (expanded depending on selected serial port)
  47. #define M_UCSRxA SERIAL_REGNAME(UCSR,SERIAL_PORT,A) // defines M_UCSRxA to be UCSRnA where n is the serial port number
  48. #define M_UCSRxB SERIAL_REGNAME(UCSR,SERIAL_PORT,B)
  49. #define M_RXENx SERIAL_REGNAME(RXEN,SERIAL_PORT,)
  50. #define M_TXENx SERIAL_REGNAME(TXEN,SERIAL_PORT,)
  51. #define M_TXCx SERIAL_REGNAME(TXC,SERIAL_PORT,)
  52. #define M_RXCIEx SERIAL_REGNAME(RXCIE,SERIAL_PORT,)
  53. #define M_UDREx SERIAL_REGNAME(UDRE,SERIAL_PORT,)
  54. #define M_UDRIEx SERIAL_REGNAME(UDRIE,SERIAL_PORT,)
  55. #define M_UDRx SERIAL_REGNAME(UDR,SERIAL_PORT,)
  56. #define M_UBRRxH SERIAL_REGNAME(UBRR,SERIAL_PORT,H)
  57. #define M_UBRRxL SERIAL_REGNAME(UBRR,SERIAL_PORT,L)
  58. #define M_RXCx SERIAL_REGNAME(RXC,SERIAL_PORT,)
  59. #define M_USARTx_RX_vect SERIAL_REGNAME(USART,SERIAL_PORT,_RX_vect)
  60. #define M_U2Xx SERIAL_REGNAME(U2X,SERIAL_PORT,)
  61. #define M_USARTx_UDRE_vect SERIAL_REGNAME(USART,SERIAL_PORT,_UDRE_vect)
  62. #define DEC 10
  63. #define HEX 16
  64. #define OCT 8
  65. #define BIN 2
  66. #define BYTE 0
  67. #ifndef USBCON
  68. // Define constants and variables for buffering incoming serial data. We're
  69. // using a ring buffer (I think), in which rx_buffer_head is the index of the
  70. // location to which to write the next incoming character and rx_buffer_tail
  71. // is the index of the location from which to read.
  72. // 256 is the max limit due to uint8_t head and tail. Use only powers of 2. (...,16,32,64,128,256)
  73. #ifndef RX_BUFFER_SIZE
  74. #define RX_BUFFER_SIZE 128
  75. #endif
  76. #ifndef TX_BUFFER_SIZE
  77. #define TX_BUFFER_SIZE 32
  78. #endif
  79. #if ENABLED(SERIAL_XON_XOFF) && RX_BUFFER_SIZE < 1024
  80. #error "XON/XOFF requires RX_BUFFER_SIZE >= 1024 for reliable transfers without drops."
  81. #endif
  82. #if !IS_POWER_OF_2(RX_BUFFER_SIZE) || RX_BUFFER_SIZE < 2
  83. #error "RX_BUFFER_SIZE must be a power of 2 greater than 1."
  84. #endif
  85. #if TX_BUFFER_SIZE && (TX_BUFFER_SIZE < 2 || TX_BUFFER_SIZE > 256 || !IS_POWER_OF_2(TX_BUFFER_SIZE))
  86. #error "TX_BUFFER_SIZE must be 0 or a power of 2 greater than 1."
  87. #endif
  88. #if RX_BUFFER_SIZE > 256
  89. typedef uint16_t ring_buffer_pos_t;
  90. #else
  91. typedef uint8_t ring_buffer_pos_t;
  92. #endif
  93. #if ENABLED(SERIAL_STATS_DROPPED_RX)
  94. extern uint8_t rx_dropped_bytes;
  95. #endif
  96. #if ENABLED(SERIAL_STATS_MAX_RX_QUEUED)
  97. extern ring_buffer_pos_t rx_max_enqueued;
  98. #endif
  99. class MarlinSerial { //: public Stream
  100. public:
  101. MarlinSerial() {};
  102. static void begin(const long);
  103. static void end();
  104. static int peek(void);
  105. static int read(void);
  106. static void flush(void);
  107. static ring_buffer_pos_t available(void);
  108. static void checkRx(void);
  109. static void write(const uint8_t c);
  110. #if TX_BUFFER_SIZE > 0
  111. static uint8_t availableForWrite(void);
  112. static void flushTX(void);
  113. #endif
  114. static void writeNoHandshake(const uint8_t c);
  115. #if ENABLED(SERIAL_STATS_DROPPED_RX)
  116. FORCE_INLINE static uint32_t dropped() { return rx_dropped_bytes; }
  117. #endif
  118. #if ENABLED(SERIAL_STATS_MAX_RX_QUEUED)
  119. FORCE_INLINE static ring_buffer_pos_t rxMaxEnqueued() { return rx_max_enqueued; }
  120. #endif
  121. private:
  122. static void printNumber(unsigned long, const uint8_t);
  123. static void printFloat(double, uint8_t);
  124. public:
  125. static FORCE_INLINE void write(const char* str) { while (*str) write(*str++); }
  126. static FORCE_INLINE void write(const uint8_t* buffer, size_t size) { while (size--) write(*buffer++); }
  127. static FORCE_INLINE void print(const String& s) { for (int i = 0; i < (int)s.length(); i++) write(s[i]); }
  128. static FORCE_INLINE void print(const char* str) { write(str); }
  129. static void print(char, int = BYTE);
  130. static void print(unsigned char, int = BYTE);
  131. static void print(int, int = DEC);
  132. static void print(unsigned int, int = DEC);
  133. static void print(long, int = DEC);
  134. static void print(unsigned long, int = DEC);
  135. static void print(double, int = 2);
  136. static void println(const String& s);
  137. static void println(const char[]);
  138. static void println(char, int = BYTE);
  139. static void println(unsigned char, int = BYTE);
  140. static void println(int, int = DEC);
  141. static void println(unsigned int, int = DEC);
  142. static void println(long, int = DEC);
  143. static void println(unsigned long, int = DEC);
  144. static void println(double, int = 2);
  145. static void println(void);
  146. };
  147. extern MarlinSerial customizedSerial;
  148. #endif // !USBCON
  149. // Use the UART for Bluetooth in AT90USB configurations
  150. #if defined(USBCON) && ENABLED(BLUETOOTH)
  151. extern HardwareSerial bluetoothSerial;
  152. #endif
  153. #endif // MARLINSERIAL_H