MarlinSerial.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. #if !defined(SERIAL_PORT)
  21. #define SERIAL_PORT 0
  22. #endif
  23. // The presence of the UBRRH register is used to detect a UART.
  24. #if ((SERIAL_PORT == 0 && (defined(UBRRH) || defined(UBRR0H))) || \
  25. (SERIAL_PORT == 1 && defined(UBRR1H)) || \
  26. (SERIAL_PORT == 2 && defined(UBRR2H)) || \
  27. (SERIAL_PORT == 3 && defined(UBRR3H)))
  28. #define HAS_UART
  29. #endif
  30. // These are macros to build serial port register names for the selected SERIAL_PORT (C preprocessor
  31. // requires two levels of indirection to expand macro values properly)
  32. #define SERIAL_REGNAME(registerbase,number,suffix) SERIAL_REGNAME_INTERNAL(registerbase,number,suffix)
  33. #if SERIAL_PORT == 0 && (!defined(UBRR0H) || !defined(UDR0)) // use un-numbered registers if necessary
  34. #define SERIAL_REGNAME_INTERNAL(registerbase,number,suffix) registerbase##suffix
  35. #else
  36. #define SERIAL_REGNAME_INTERNAL(registerbase,number,suffix) registerbase##number##suffix
  37. #endif
  38. // Registers used by MarlinSerial class (these are expanded
  39. // depending on selected serial port
  40. #define M_UCSRxA SERIAL_REGNAME(UCSR,SERIAL_PORT,A) // defines M_UCSRxA to be UCSRnA where n is the serial port number
  41. #define M_UCSRxB SERIAL_REGNAME(UCSR,SERIAL_PORT,B)
  42. #define M_RXENx SERIAL_REGNAME(RXEN,SERIAL_PORT,)
  43. #define M_TXENx SERIAL_REGNAME(TXEN,SERIAL_PORT,)
  44. #define M_RXCIEx SERIAL_REGNAME(RXCIE,SERIAL_PORT,)
  45. #define M_UDREx SERIAL_REGNAME(UDRE,SERIAL_PORT,)
  46. #define M_UDRx SERIAL_REGNAME(UDR,SERIAL_PORT,)
  47. #define M_UBRRxH SERIAL_REGNAME(UBRR,SERIAL_PORT,H)
  48. #define M_UBRRxL SERIAL_REGNAME(UBRR,SERIAL_PORT,L)
  49. #define M_RXCx SERIAL_REGNAME(RXC,SERIAL_PORT,)
  50. #define M_FEx SERIAL_REGNAME(FE,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 AT90USB
  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. #define RX_BUFFER_SIZE 128
  64. extern uint8_t selectedSerialPort;
  65. struct ring_buffer
  66. {
  67. unsigned char buffer[RX_BUFFER_SIZE];
  68. int head;
  69. int tail;
  70. };
  71. #ifdef HAS_UART
  72. extern ring_buffer rx_buffer;
  73. #endif
  74. class MarlinSerial //: public Stream
  75. {
  76. public:
  77. static void begin(long);
  78. static void end();
  79. static int peek(void);
  80. static int read(void);
  81. static void flush(void);
  82. static /*FORCE_INLINE*/ int available(void)
  83. {
  84. return (unsigned int)(RX_BUFFER_SIZE + rx_buffer.head - rx_buffer.tail) % RX_BUFFER_SIZE;
  85. }
  86. /*
  87. FORCE_INLINE void write(uint8_t c)
  88. {
  89. while (!((M_UCSRxA) & (1 << M_UDREx)))
  90. ;
  91. M_UDRx = c;
  92. }
  93. */
  94. static void write(uint8_t c)
  95. {
  96. if (selectedSerialPort == 0)
  97. {
  98. while (!((M_UCSRxA) & (1 << M_UDREx)));
  99. M_UDRx = c;
  100. }
  101. else if (selectedSerialPort == 1)
  102. {
  103. while (!((UCSR1A) & (1 << UDRE1)));
  104. UDR1 = c;
  105. }
  106. }
  107. static void checkRx(void)
  108. {
  109. if (selectedSerialPort == 0) {
  110. if((M_UCSRxA & (1<<M_RXCx)) != 0) {
  111. // Test for a framing error.
  112. if (M_UCSRxA & (1<<M_FEx)) {
  113. // Characters received with the framing errors will be ignored.
  114. // The temporary variable "c" was made volatile, so the compiler does not optimize this out.
  115. (void)(*(char *)M_UDRx);
  116. } else {
  117. unsigned char c = M_UDRx;
  118. int i = (unsigned int)(rx_buffer.head + 1) % RX_BUFFER_SIZE;
  119. // if we should be storing the received character into the location
  120. // just before the tail (meaning that the head would advance to the
  121. // current location of the tail), we're about to overflow the buffer
  122. // and so we don't write the character or advance the head.
  123. if (i != rx_buffer.tail) {
  124. rx_buffer.buffer[rx_buffer.head] = c;
  125. rx_buffer.head = i;
  126. }
  127. //selectedSerialPort = 0;
  128. #ifdef DEBUG_DUMP_TO_2ND_SERIAL
  129. UDR1 = c;
  130. #endif //DEBUG_DUMP_TO_2ND_SERIAL
  131. }
  132. }
  133. } else { // if(selectedSerialPort == 1) {
  134. if((UCSR1A & (1<<RXC1)) != 0) {
  135. // Test for a framing error.
  136. if (UCSR1A & (1<<FE1)) {
  137. // Characters received with the framing errors will be ignored.
  138. // The temporary variable "c" was made volatile, so the compiler does not optimize this out.
  139. (void)(*(char *)UDR1);
  140. } else {
  141. unsigned char c = UDR1;
  142. int i = (unsigned int)(rx_buffer.head + 1) % RX_BUFFER_SIZE;
  143. // if we should be storing the received character into the location
  144. // just before the tail (meaning that the head would advance to the
  145. // current location of the tail), we're about to overflow the buffer
  146. // and so we don't write the character or advance the head.
  147. if (i != rx_buffer.tail) {
  148. rx_buffer.buffer[rx_buffer.head] = c;
  149. rx_buffer.head = i;
  150. }
  151. //selectedSerialPort = 1;
  152. #ifdef DEBUG_DUMP_TO_2ND_SERIAL
  153. M_UDRx = c;
  154. #endif //DEBUG_DUMP_TO_2ND_SERIAL
  155. }
  156. }
  157. }
  158. }
  159. private:
  160. static void printNumber(unsigned long, uint8_t);
  161. static void printFloat(double, uint8_t);
  162. public:
  163. static /*FORCE_INLINE*/ void write(const char *str)
  164. {
  165. while (*str)
  166. write(*str++);
  167. }
  168. static /*FORCE_INLINE*/ void write(const uint8_t *buffer, size_t size)
  169. {
  170. while (size--)
  171. write(*buffer++);
  172. }
  173. /* static FORCE_INLINE void print(const String &s)
  174. {
  175. for (int i = 0; i < (int)s.length(); i++) {
  176. write(s[i]);
  177. }
  178. }*/
  179. static FORCE_INLINE void print(const char *str)
  180. {
  181. write(str);
  182. }
  183. static void print(char, int = BYTE);
  184. static void print(unsigned char, int = BYTE);
  185. static void print(int, int = DEC);
  186. static void print(unsigned int, int = DEC);
  187. static void print(long, int = DEC);
  188. static void print(unsigned long, int = DEC);
  189. static void print(double, int = 2);
  190. // static void println(const String &s);
  191. static void println(const char[]);
  192. static void println(char, int = BYTE);
  193. static void println(unsigned char, int = BYTE);
  194. static void println(int, int = DEC);
  195. static void println(unsigned int, int = DEC);
  196. static void println(long, int = DEC);
  197. static void println(unsigned long, int = DEC);
  198. static void println(double, int = 2);
  199. static void println(void);
  200. };
  201. extern MarlinSerial MSerial;
  202. #endif // !AT90USB
  203. // Use the UART for BT in AT90USB configurations
  204. #if defined(AT90USB) && defined (BTENABLED)
  205. extern HardwareSerial bt;
  206. #endif
  207. #endif