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 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 <inttypes.h>
  20. #include <Stream.h>
  21. // Define constants and variables for buffering incoming serial data. We're
  22. // using a ring buffer (I think), in which rx_buffer_head is the index of the
  23. // location to which to write the next incoming character and rx_buffer_tail
  24. // is the index of the location from which to read.
  25. #define RX_BUFFER_SIZE 128
  26. struct ring_buffer
  27. {
  28. unsigned char buffer[RX_BUFFER_SIZE];
  29. int head;
  30. int tail;
  31. };
  32. #if defined(UBRRH) || defined(UBRR0H)
  33. extern ring_buffer rx_buffer;
  34. #endif
  35. class MarlinSerial //: public Stream
  36. {
  37. private:
  38. volatile uint8_t *_ubrrh;
  39. volatile uint8_t *_ubrrl;
  40. volatile uint8_t *_ucsra;
  41. volatile uint8_t *_ucsrb;
  42. volatile uint8_t *_udr;
  43. uint8_t _rxen;
  44. uint8_t _txen;
  45. uint8_t _rxcie;
  46. uint8_t _udre;
  47. uint8_t _u2x;
  48. public:
  49. MarlinSerial(
  50. volatile uint8_t *ubrrh, volatile uint8_t *ubrrl,
  51. volatile uint8_t *ucsra, volatile uint8_t *ucsrb,
  52. volatile uint8_t *udr,
  53. uint8_t rxen, uint8_t txen, uint8_t rxcie, uint8_t udre, uint8_t u2x);
  54. void begin(long);
  55. void end();
  56. inline int available(void)
  57. {
  58. return (unsigned int)(RX_BUFFER_SIZE + rx_buffer.head - rx_buffer.tail) % RX_BUFFER_SIZE;
  59. }
  60. int peek(void);
  61. int read(void);
  62. void flush(void);
  63. inline void write(uint8_t c)
  64. {
  65. while (!((*_ucsra) & (1 << _udre)))
  66. ;
  67. *_udr = c;
  68. }
  69. inline void checkRx(void)
  70. {
  71. if((UCSR0A & (1<<RXC0)) != 0) {
  72. unsigned char c = UDR0;
  73. int i = (unsigned int)(rx_buffer.head + 1) % RX_BUFFER_SIZE;
  74. // if we should be storing the received character into the location
  75. // just before the tail (meaning that the head would advance to the
  76. // current location of the tail), we're about to overflow the buffer
  77. // and so we don't write the character or advance the head.
  78. if (i != rx_buffer.tail) {
  79. rx_buffer.buffer[rx_buffer.head] = c;
  80. rx_buffer.head = i;
  81. }
  82. }
  83. }
  84. private:
  85. void printNumber(unsigned long, uint8_t);
  86. void printFloat(double, uint8_t);
  87. public:
  88. void write(const char *str);
  89. void write( const uint8_t *buffer, size_t size);
  90. void print(const String &);
  91. void print(const char[]);
  92. void print(char, int = BYTE);
  93. void print(unsigned char, int = BYTE);
  94. void print(int, int = DEC);
  95. void print(unsigned int, int = DEC);
  96. void print(long, int = DEC);
  97. void print(unsigned long, int = DEC);
  98. void print(double, int = 2);
  99. void println(const String &s);
  100. void println(const char[]);
  101. void println(char, int = BYTE);
  102. void println(unsigned char, int = BYTE);
  103. void println(int, int = DEC);
  104. void println(unsigned int, int = DEC);
  105. void println(long, int = DEC);
  106. void println(unsigned long, int = DEC);
  107. void println(double, int = 2);
  108. void println(void);
  109. };
  110. #if defined(UBRRH) || defined(UBRR0H)
  111. extern MarlinSerial MSerial;
  112. #endif
  113. #endif