PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
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.

151 line
4.4KB

  1. /*
  2. SoftwareSerial.h (formerly NewSoftSerial.h) -
  3. Multi-instance software serial library for Arduino/Wiring
  4. -- Interrupt-driven receive and other improvements by ladyada
  5. (http://ladyada.net)
  6. -- Tuning, circular buffer, derivation from class Print/Stream,
  7. multi-instance support, porting to 8MHz processors,
  8. various optimizations, PROGMEM delay tables, inverse logic and
  9. direct port writing by Mikal Hart (http://www.arduiniana.org)
  10. -- Pin change interrupt macros by Paul Stoffregen (http://www.pjrc.com)
  11. -- 20MHz processor support by Garrett Mace (http://www.macetech.com)
  12. -- ATmega1280/2560 support by Brett Hagman (http://www.roguerobotics.com/)
  13. This library is free software; you can redistribute it and/or
  14. modify it under the terms of the GNU Lesser General Public
  15. License as published by the Free Software Foundation; either
  16. version 2.1 of the License, or (at your option) any later version.
  17. This library is distributed in the hope that it will be useful,
  18. but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. Lesser General Public License for more details.
  21. You should have received a copy of the GNU Lesser General Public
  22. License along with this library; if not, write to the Free Software
  23. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  24. The latest version of this library can always be found at
  25. http://arduiniana.org.
  26. */
  27. #ifndef SoftwareSerial_h
  28. #define SoftwareSerial_h
  29. #include <inttypes.h>
  30. #include <Stream.h>
  31. #include <HardwareSerial.h>
  32. /******************************************************************************
  33. * Definitions
  34. ******************************************************************************/
  35. #define _SS_MAX_RX_BUFF 64 // RX buffer size
  36. #ifndef GCC_VERSION
  37. #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
  38. #endif
  39. #if defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MKL26Z64__) || defined(__MK64FX512__) || defined(__MK66FX1M0__) || defined(__IMXRT1052__) || defined(__IMXRT1062__)
  40. class SoftwareSerial : public Stream
  41. {
  42. public:
  43. SoftwareSerial(uint8_t rxPin, uint8_t txPin, bool inverse_logic = false);
  44. ~SoftwareSerial() { end(); }
  45. void begin(unsigned long speed);
  46. void end();
  47. bool listen() { return true; }
  48. bool isListening() { return true; }
  49. bool overflow() { bool ret = buffer_overflow; buffer_overflow = false; return ret; }
  50. virtual int available();
  51. virtual int read();
  52. int peek();
  53. virtual void flush();
  54. virtual size_t write(uint8_t byte);
  55. using Print::write;
  56. private:
  57. HardwareSerial *port;
  58. uint32_t cycles_per_bit;
  59. #if defined(__IMXRT1052__) || defined(__IMXRT1062__)
  60. volatile uint32_t *txreg;
  61. volatile uint32_t *rxreg;
  62. #else
  63. volatile uint8_t *txreg;
  64. volatile uint8_t *rxreg;
  65. #endif
  66. bool buffer_overflow;
  67. uint8_t txpin;
  68. uint8_t rxpin;
  69. };
  70. #else
  71. class SoftwareSerial : public Stream
  72. {
  73. private:
  74. // per object data
  75. uint8_t _receivePin;
  76. uint8_t _receiveBitMask;
  77. volatile uint8_t *_receivePortRegister;
  78. uint8_t _transmitBitMask;
  79. volatile uint8_t *_transmitPortRegister;
  80. uint16_t _rx_delay_centering;
  81. uint16_t _rx_delay_intrabit;
  82. uint16_t _rx_delay_stopbit;
  83. uint16_t _tx_delay;
  84. uint16_t _buffer_overflow:1;
  85. uint16_t _inverse_logic:1;
  86. // static data
  87. static char _receive_buffer[_SS_MAX_RX_BUFF];
  88. static volatile uint8_t _receive_buffer_tail;
  89. static volatile uint8_t _receive_buffer_head;
  90. static SoftwareSerial *active_object;
  91. // private methods
  92. void recv();
  93. uint8_t rx_pin_read();
  94. void tx_pin_write(uint8_t pin_state);
  95. void setTX(uint8_t transmitPin);
  96. void setRX(uint8_t receivePin);
  97. // private static method for timing
  98. static inline void tunedDelay(uint16_t delay);
  99. public:
  100. // public methods
  101. SoftwareSerial(uint8_t receivePin, uint8_t transmitPin, bool inverse_logic = false);
  102. ~SoftwareSerial();
  103. void begin(long speed);
  104. bool listen();
  105. void end();
  106. bool isListening() { return this == active_object; }
  107. bool overflow() { bool ret = _buffer_overflow; _buffer_overflow = false; return ret; }
  108. int peek();
  109. virtual size_t write(uint8_t byte);
  110. virtual int read();
  111. virtual int available();
  112. virtual void flush();
  113. using Print::write;
  114. // public only for easy access by interrupt handlers
  115. static inline void handle_interrupt();
  116. };
  117. // Arduino 0012 workaround
  118. #undef int
  119. #undef char
  120. #undef long
  121. #undef byte
  122. #undef float
  123. #undef abs
  124. #undef round
  125. #endif
  126. #endif