2011-10-27 17:45:13 +02:00
|
|
|
/*
|
|
|
|
SoftwareSerial.h (formerly NewSoftSerial.h) -
|
|
|
|
Multi-instance software serial library for Arduino/Wiring
|
|
|
|
-- Interrupt-driven receive and other improvements by ladyada
|
|
|
|
(http://ladyada.net)
|
|
|
|
-- Tuning, circular buffer, derivation from class Print/Stream,
|
|
|
|
multi-instance support, porting to 8MHz processors,
|
|
|
|
various optimizations, PROGMEM delay tables, inverse logic and
|
|
|
|
direct port writing by Mikal Hart (http://www.arduiniana.org)
|
|
|
|
-- Pin change interrupt macros by Paul Stoffregen (http://www.pjrc.com)
|
|
|
|
-- 20MHz processor support by Garrett Mace (http://www.macetech.com)
|
|
|
|
-- ATmega1280/2560 support by Brett Hagman (http://www.roguerobotics.com/)
|
|
|
|
|
|
|
|
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 <inttypes.h>
|
|
|
|
#include <Stream.h>
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* Definitions
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
#define _SS_MAX_RX_BUFF 64 // RX buffer size
|
|
|
|
#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;
|
|
|
|
uint8_t _receiveBitMask;
|
|
|
|
volatile uint8_t *_receivePortRegister;
|
|
|
|
uint8_t _transmitBitMask;
|
|
|
|
volatile uint8_t *_transmitPortRegister;
|
2014-04-23 19:13:58 +02:00
|
|
|
volatile uint8_t *_pcint_maskreg;
|
|
|
|
uint8_t _pcint_maskvalue;
|
2011-10-27 17:45:13 +02:00
|
|
|
|
Fix SoftwareSerial timings
Instead of using a lookup table with (wrong) timings, this calculates
the timings in SoftwareSerial::begin. This is probably a bit slower, but
since it typically happens once, this shouldn't be a problem.
Additionally, since the lookup tables can be removed, this is also a lot
smaller, as well as supporting arbitrary CPU speeds and baudrates,
instead of the limited set that was defined before.
Furthermore, this switches to use the _delay_loop_2 function from
avr-libc instead of a handcoded delay function. The avr-libc function
only takes two instructions, as opposed to four instructions for the old
one. The compiler also inlines the avr-libc function, which makes the
timings more reliable.
The calculated timings directly rely on the instructions generated by
the compiler, since a significant amount of time is spent processing
(compared to the delays, especially at higher speeds). This means that
if the code is changed, or a different compiler is used, the
calculations might need changing (though a few cycles more or less
shouldn't cause immediate breakage).
The timings in the code have been calculated from the assembly generated
by gcc 4.8.2 and gcc 4.3.2.
The RX baudrates supported by SoftwareSerial are still not unlimited. At
16Mhz, using gcc 4.8.2, everything up to 115200 works. At 8Mhz, it works
up to 57600. Using gcc 4.3.2, it also works up to 57600 at 16Mhz and up
to 38400 at 8Mhz. Note that at these highest speeds, communication
works, but is still quite sensitive to other interrupts (like the
millis() interrupts) when bytes are sent back-to-back, so there still
are corrupted bytes in RX.
TX works up to 115200 for all combinations of compiler and clock rates.
This fixes #2019
2014-04-23 19:23:58 +02:00
|
|
|
// Expressed as 4-cycle delays (must never be 0!)
|
2011-10-27 17:45:13 +02:00
|
|
|
uint16_t _rx_delay_centering;
|
|
|
|
uint16_t _rx_delay_intrabit;
|
|
|
|
uint16_t _rx_delay_stopbit;
|
|
|
|
uint16_t _tx_delay;
|
|
|
|
|
|
|
|
uint16_t _buffer_overflow:1;
|
|
|
|
uint16_t _inverse_logic:1;
|
|
|
|
|
|
|
|
// static data
|
|
|
|
static char _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
|
2014-04-23 15:52:57 +02:00
|
|
|
void recv() __attribute__((__always_inline__));
|
2011-10-27 17:45:13 +02:00
|
|
|
uint8_t rx_pin_read();
|
2014-04-22 17:45:21 +02:00
|
|
|
void tx_pin_write(uint8_t pin_state) __attribute__((__always_inline__));
|
2011-10-27 17:45:13 +02:00
|
|
|
void setTX(uint8_t transmitPin);
|
|
|
|
void setRX(uint8_t receivePin);
|
Disable the RX PCINT inside SoftwareSerial::recv
Before, the interrupt would remain enabled during reception, which would
re-set the PCINT flag because of the level changes inside the received
byte. Because interrupts are globally disabled, this would not
immediately trigger an interrupt, but the flag would be remembered to
trigger another PCINT interrupt immediately after the first one is
processed.
Typically this was not a problem, because the second interrupt would see
the stop bit, or an idle line, and decide that the interrupt triggered
for someone else. However, at high baud rates, this could cause the
next interrupt for the real start bit to be delayed so much that the
byte got corrupted.
By clearing the interrupt mask bit for just the RX pin (as opposed to
the PCINT mask bit for the entire port), any PCINT events on other bits
can still set the PCINT flag and be processed as normal. In this case,
it's likely that there will be corruption, but that's inevitable when
(other) interrupts happen during SoftwareSerial reception.
2014-04-23 19:16:21 +02:00
|
|
|
void setRxIntMsk(bool enable) __attribute__((__always_inline__));
|
2011-10-27 17:45:13 +02:00
|
|
|
|
Fix SoftwareSerial timings
Instead of using a lookup table with (wrong) timings, this calculates
the timings in SoftwareSerial::begin. This is probably a bit slower, but
since it typically happens once, this shouldn't be a problem.
Additionally, since the lookup tables can be removed, this is also a lot
smaller, as well as supporting arbitrary CPU speeds and baudrates,
instead of the limited set that was defined before.
Furthermore, this switches to use the _delay_loop_2 function from
avr-libc instead of a handcoded delay function. The avr-libc function
only takes two instructions, as opposed to four instructions for the old
one. The compiler also inlines the avr-libc function, which makes the
timings more reliable.
The calculated timings directly rely on the instructions generated by
the compiler, since a significant amount of time is spent processing
(compared to the delays, especially at higher speeds). This means that
if the code is changed, or a different compiler is used, the
calculations might need changing (though a few cycles more or less
shouldn't cause immediate breakage).
The timings in the code have been calculated from the assembly generated
by gcc 4.8.2 and gcc 4.3.2.
The RX baudrates supported by SoftwareSerial are still not unlimited. At
16Mhz, using gcc 4.8.2, everything up to 115200 works. At 8Mhz, it works
up to 57600. Using gcc 4.3.2, it also works up to 57600 at 16Mhz and up
to 38400 at 8Mhz. Note that at these highest speeds, communication
works, but is still quite sensitive to other interrupts (like the
millis() interrupts) when bytes are sent back-to-back, so there still
are corrupted bytes in RX.
TX works up to 115200 for all combinations of compiler and clock rates.
This fixes #2019
2014-04-23 19:23:58 +02:00
|
|
|
// Return num - sub, or 1 if the result would be < 1
|
|
|
|
static uint16_t subtract_cap(uint16_t num, uint16_t sub);
|
|
|
|
|
2011-10-27 17:45:13 +02:00
|
|
|
// private static method for timing
|
|
|
|
static inline void tunedDelay(uint16_t delay);
|
|
|
|
|
|
|
|
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; }
|
2013-06-04 11:28:30 +02:00
|
|
|
bool stopListening();
|
2013-06-13 09:56:46 +02:00
|
|
|
bool overflow() { bool ret = _buffer_overflow; if (ret) _buffer_overflow = false; return ret; }
|
2011-10-27 17:45:13 +02:00
|
|
|
int peek();
|
|
|
|
|
|
|
|
virtual size_t write(uint8_t byte);
|
|
|
|
virtual int read();
|
|
|
|
virtual int available();
|
|
|
|
virtual void flush();
|
|
|
|
|
|
|
|
using Print::write;
|
|
|
|
|
|
|
|
// public only for easy access by interrupt handlers
|
2014-04-23 15:52:57 +02:00
|
|
|
static inline void handle_interrupt() __attribute__((__always_inline__));
|
2011-10-27 17:45:13 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Arduino 0012 workaround
|
|
|
|
#undef int
|
|
|
|
#undef char
|
|
|
|
#undef long
|
|
|
|
#undef byte
|
|
|
|
#undef float
|
|
|
|
#undef abs
|
|
|
|
#undef round
|
|
|
|
|
|
|
|
#endif
|