My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

MarlinSerial.h 6.2KB

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