/* SoftwareSerial.h This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA The latest version of this library can always be found at http://arduiniana.org. */ #ifndef SoftwareSerial_h #define SoftwareSerial_h #include #include /****************************************************************************** * Definitions ******************************************************************************/ #ifndef _SS_MAX_RX_BUFF #define _SS_MAX_RX_BUFF 64 // RX buffer size #endif #ifndef GCC_VERSION #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) #endif class SoftwareSerial : public Stream { private: // per object data uint8_t _receivePin; uint16_t _receiveBitMask; uint32_t _receivePortRegister; uint8_t _transmitPin; uint16_t _transmitBitMask; uint32_t _transmitPortRegister; uint8_t _int_maskLine; // must never be 0! uint32_t _rx_delay_centering; uint32_t _rx_delay_intrabit; uint32_t _rx_delay_stopbit; uint32_t _tx_delay; uint16_t _buffer_overflow:1; uint16_t _inverse_logic:1; // static data static uint8_t _receive_buffer[_SS_MAX_RX_BUFF]; static volatile uint8_t _receive_buffer_tail; static volatile uint8_t _receive_buffer_head; static SoftwareSerial *active_object; // private methods void recv() __attribute__((noinline, section(".ram_text"))); uint8_t rx_pin_read(); void setTX(uint8_t transmitPin); void setRX(uint8_t receivePin); inline void setRxIntMsk(bool enable) __attribute__((__always_inline__)); // Return num - sub, or 1 if the result would be < 1 static uint32_t subtract_cap(uint32_t num, uint16_t sub); // private static method for timing static void tunedDelay(uint32_t delayTicks) __attribute__((noinline, section(".ram_text"))); public: // public methods SoftwareSerial(uint8_t receivePin, uint8_t transmitPin, bool inverse_logic = false); ~SoftwareSerial(); void begin(long speed); bool listen(); void end(); bool isListening() { return this == active_object; } bool stopListening(); bool overflow() { bool ret = _buffer_overflow; if (ret) _buffer_overflow = false; return ret; } int peek(); virtual size_t write(uint8_t byte) __attribute__((noinline, section(".ram_text"))); virtual int read(); virtual int available(); virtual void flush(); operator bool() { return true; } using Print::write; // public only for easy access by interrupt handlers static void handle_interrupt() __attribute__((noinline, section(".ram_text"))); }; #endif