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

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