[175] | 1 | /*
|
---|
| 2 | SoftwareSerial.cpp - library for Arduino M0/M0 pro
|
---|
| 3 | Copyright (c) 2016 Arduino Srl. All rights reserved.
|
---|
| 4 | Written by Chiara Ruggeri (chiara@arduino.org)
|
---|
| 5 |
|
---|
| 6 | This library is free software; you can redistribute it and/or
|
---|
| 7 | modify it under the terms of the GNU Lesser General Public
|
---|
| 8 | License as published by the Free Software Foundation; either
|
---|
| 9 | version 2.1 of the License, or (at your option) any later version.
|
---|
| 10 |
|
---|
| 11 | This library is distributed in the hope that it will be useful,
|
---|
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 14 | Lesser General Public License for more details.
|
---|
| 15 |
|
---|
| 16 | You should have received a copy of the GNU Lesser General Public
|
---|
| 17 | License along with this library; if not, write to the Free Software
|
---|
| 18 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
---|
| 19 |
|
---|
| 20 | Enjoy!
|
---|
| 21 | */
|
---|
| 22 |
|
---|
| 23 | #ifndef SoftwareSerial_h
|
---|
| 24 | #define SoftwareSerial_h
|
---|
| 25 |
|
---|
| 26 | #include <inttypes.h>
|
---|
| 27 | #include <Stream.h>
|
---|
| 28 | #include <variant.h>
|
---|
| 29 |
|
---|
| 30 | /******************************************************************************
|
---|
| 31 | * Definitions
|
---|
| 32 | ******************************************************************************/
|
---|
| 33 |
|
---|
| 34 | #define _SS_MAX_RX_BUFF 64 // RX buffer size
|
---|
| 35 | #ifndef GCC_VERSION
|
---|
| 36 | #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
|
---|
| 37 | #endif
|
---|
| 38 |
|
---|
| 39 | class SoftwareSerial : public Stream
|
---|
| 40 | {
|
---|
| 41 | private:
|
---|
| 42 | // per object data
|
---|
| 43 | uint8_t _transmitPin;
|
---|
| 44 | uint8_t _receivePin;
|
---|
| 45 | uint32_t _receiveBitMask;
|
---|
| 46 | volatile PORT_IN_Type *_receivePortRegister;
|
---|
| 47 | uint32_t _transmitBitMask;
|
---|
| 48 | volatile PORT_OUT_Type *_transmitPortRegister;
|
---|
| 49 |
|
---|
| 50 | // Expressed as 4-cycle delays (must never be 0!)
|
---|
| 51 | uint16_t _rx_delay_centering;
|
---|
| 52 | uint16_t _rx_delay_intrabit;
|
---|
| 53 | uint16_t _rx_delay_stopbit;
|
---|
| 54 | uint16_t _tx_delay;
|
---|
| 55 |
|
---|
| 56 | uint16_t _buffer_overflow:1;
|
---|
| 57 | uint16_t _inverse_logic:1;
|
---|
| 58 |
|
---|
| 59 | // static data
|
---|
| 60 | static char _receive_buffer[_SS_MAX_RX_BUFF];
|
---|
| 61 | static volatile uint8_t _receive_buffer_tail;
|
---|
| 62 | static volatile uint8_t _receive_buffer_head;
|
---|
| 63 | static SoftwareSerial *active_object;
|
---|
| 64 |
|
---|
| 65 | // private methods
|
---|
| 66 | void recv() __attribute__((__always_inline__));
|
---|
| 67 | uint32_t rx_pin_read();
|
---|
| 68 | void tx_pin_write(uint8_t pin_state) __attribute__((__always_inline__));
|
---|
| 69 | void setTX(uint8_t transmitPin);
|
---|
| 70 | void setRX(uint8_t receivePin);
|
---|
| 71 | void setRxIntMsk(bool enable) __attribute__((__always_inline__));
|
---|
| 72 |
|
---|
| 73 |
|
---|
| 74 | public:
|
---|
| 75 | // public methods
|
---|
| 76 | SoftwareSerial(uint8_t receivePin, uint8_t transmitPin, bool inverse_logic = false);
|
---|
| 77 | ~SoftwareSerial();
|
---|
| 78 | void begin(long speed);
|
---|
| 79 | bool listen();
|
---|
| 80 | void end();
|
---|
| 81 | bool isListening() { return this == active_object; }
|
---|
| 82 | bool stopListening();
|
---|
| 83 | bool overflow() { bool ret = _buffer_overflow; if (ret) _buffer_overflow = false; return ret; }
|
---|
| 84 | int peek();
|
---|
| 85 |
|
---|
| 86 | virtual size_t write(uint8_t byte);
|
---|
| 87 | virtual int read();
|
---|
| 88 | virtual int available();
|
---|
| 89 | virtual void flush();
|
---|
| 90 | operator bool() { return true; }
|
---|
| 91 |
|
---|
| 92 | using Print::write;
|
---|
| 93 |
|
---|
| 94 | // public only for easy access by interrupt handlers
|
---|
| 95 | static inline void handle_interrupt() __attribute__((__always_inline__));
|
---|
| 96 | };
|
---|
| 97 |
|
---|
| 98 | // Arduino 0012 workaround
|
---|
| 99 | #undef int
|
---|
| 100 | #undef char
|
---|
| 101 | #undef long
|
---|
| 102 | #undef byte
|
---|
| 103 | #undef float
|
---|
| 104 | #undef abs
|
---|
| 105 | #undef round
|
---|
| 106 |
|
---|
| 107 | #endif
|
---|
| 108 |
|
---|