HardwareSerial.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. Modified 14 August 2012 by Alarus
  17. */
  18. #ifndef HardwareSerial_h
  19. #define HardwareSerial_h
  20. #include <inttypes.h>
  21. #include "Stream.h"
  22. struct ring_buffer;
  23. class HardwareSerial : public Stream
  24. {
  25. private:
  26. ring_buffer *_rx_buffer;
  27. ring_buffer *_tx_buffer;
  28. volatile uint8_t *_ubrrh;
  29. volatile uint8_t *_ubrrl;
  30. volatile uint8_t *_ucsra;
  31. volatile uint8_t *_ucsrb;
  32. volatile uint8_t *_ucsrc;
  33. volatile uint8_t *_udr;
  34. uint8_t _rxen;
  35. uint8_t _txen;
  36. uint8_t _rxcie;
  37. uint8_t _udrie;
  38. uint8_t _u2x;
  39. bool transmitting;
  40. public:
  41. HardwareSerial(ring_buffer *rx_buffer, ring_buffer *tx_buffer,
  42. volatile uint8_t *ubrrh, volatile uint8_t *ubrrl,
  43. volatile uint8_t *ucsra, volatile uint8_t *ucsrb,
  44. volatile uint8_t *ucsrc, volatile uint8_t *udr,
  45. uint8_t rxen, uint8_t txen, uint8_t rxcie, uint8_t udrie, uint8_t u2x);
  46. void begin(unsigned long);
  47. void begin(unsigned long, uint8_t);
  48. void end();
  49. virtual int available(void);
  50. virtual int peek(void);
  51. virtual int read(void);
  52. virtual void flush(void);
  53. virtual size_t write(uint8_t);
  54. inline size_t write(unsigned long n) { return write((uint8_t)n); }
  55. inline size_t write(long n) { return write((uint8_t)n); }
  56. inline size_t write(unsigned int n) { return write((uint8_t)n); }
  57. inline size_t write(int n) { return write((uint8_t)n); }
  58. using Print::write; // pull in write(str) and write(buf, size) from Print
  59. operator bool();
  60. };
  61. // Define config for Serial.begin(baud, config);
  62. #define SERIAL_5N1 0x00
  63. #define SERIAL_6N1 0x02
  64. #define SERIAL_7N1 0x04
  65. #define SERIAL_8N1 0x06
  66. #define SERIAL_5N2 0x08
  67. #define SERIAL_6N2 0x0A
  68. #define SERIAL_7N2 0x0C
  69. #define SERIAL_8N2 0x0E
  70. #define SERIAL_5E1 0x20
  71. #define SERIAL_6E1 0x22
  72. #define SERIAL_7E1 0x24
  73. #define SERIAL_8E1 0x26
  74. #define SERIAL_5E2 0x28
  75. #define SERIAL_6E2 0x2A
  76. #define SERIAL_7E2 0x2C
  77. #define SERIAL_8E2 0x2E
  78. #define SERIAL_5O1 0x30
  79. #define SERIAL_6O1 0x32
  80. #define SERIAL_7O1 0x34
  81. #define SERIAL_8O1 0x36
  82. #define SERIAL_5O2 0x38
  83. #define SERIAL_6O2 0x3A
  84. #define SERIAL_7O2 0x3C
  85. #define SERIAL_8O2 0x3E
  86. #if defined(UBRRH) || defined(UBRR0H)
  87. extern HardwareSerial Serial;
  88. #elif defined(USBCON)
  89. #include "USBAPI.h"
  90. // extern HardwareSerial Serial_;
  91. #endif
  92. #if defined(UBRR1H)
  93. extern HardwareSerial Serial1;
  94. #endif
  95. #if defined(UBRR2H)
  96. extern HardwareSerial Serial2;
  97. #endif
  98. #if defined(UBRR3H)
  99. extern HardwareSerial Serial3;
  100. #endif
  101. extern void serialEventRun(void) __attribute__((weak));
  102. #endif