2011-10-27 17:45:13 +02:00
|
|
|
/*
|
|
|
|
SoftwareSerial.cpp (formerly NewSoftSerial.cpp) -
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// When set, _DEBUG co-opts pins 11 and 13 for debugging with an
|
|
|
|
// oscilloscope or logic analyzer. Beware: it also slightly modifies
|
|
|
|
// the bit times, so don't rely on it too much at high baud rates
|
|
|
|
#define _DEBUG 0
|
|
|
|
#define _DEBUG_PIN1 11
|
|
|
|
#define _DEBUG_PIN2 13
|
|
|
|
//
|
|
|
|
// Includes
|
|
|
|
//
|
|
|
|
#include <avr/interrupt.h>
|
|
|
|
#include <avr/pgmspace.h>
|
2012-12-01 00:20:55 +01:00
|
|
|
#include <Arduino.h>
|
|
|
|
#include <SoftwareSerial.h>
|
2011-10-27 17:45:13 +02:00
|
|
|
//
|
|
|
|
// Lookup table
|
|
|
|
//
|
|
|
|
typedef struct _DELAY_TABLE
|
|
|
|
{
|
|
|
|
long baud;
|
|
|
|
unsigned short rx_delay_centering;
|
|
|
|
unsigned short rx_delay_intrabit;
|
|
|
|
unsigned short rx_delay_stopbit;
|
|
|
|
unsigned short tx_delay;
|
|
|
|
} DELAY_TABLE;
|
|
|
|
|
|
|
|
#if F_CPU == 16000000
|
|
|
|
|
|
|
|
static const DELAY_TABLE PROGMEM table[] =
|
|
|
|
{
|
|
|
|
// baud rxcenter rxintra rxstop tx
|
|
|
|
{ 115200, 1, 17, 17, 12, },
|
|
|
|
{ 57600, 10, 37, 37, 33, },
|
|
|
|
{ 38400, 25, 57, 57, 54, },
|
|
|
|
{ 31250, 31, 70, 70, 68, },
|
|
|
|
{ 28800, 34, 77, 77, 74, },
|
|
|
|
{ 19200, 54, 117, 117, 114, },
|
|
|
|
{ 14400, 74, 156, 156, 153, },
|
|
|
|
{ 9600, 114, 236, 236, 233, },
|
|
|
|
{ 4800, 233, 474, 474, 471, },
|
|
|
|
{ 2400, 471, 950, 950, 947, },
|
|
|
|
{ 1200, 947, 1902, 1902, 1899, },
|
2012-12-01 00:20:55 +01:00
|
|
|
{ 600, 1902, 3804, 3804, 3800, },
|
2011-10-27 17:45:13 +02:00
|
|
|
{ 300, 3804, 7617, 7617, 7614, },
|
|
|
|
};
|
|
|
|
|
|
|
|
const int XMIT_START_ADJUSTMENT = 5;
|
|
|
|
|
|
|
|
#elif F_CPU == 8000000
|
|
|
|
|
|
|
|
static const DELAY_TABLE table[] PROGMEM =
|
|
|
|
{
|
|
|
|
// baud rxcenter rxintra rxstop tx
|
|
|
|
{ 115200, 1, 5, 5, 3, },
|
|
|
|
{ 57600, 1, 15, 15, 13, },
|
|
|
|
{ 38400, 2, 25, 26, 23, },
|
|
|
|
{ 31250, 7, 32, 33, 29, },
|
|
|
|
{ 28800, 11, 35, 35, 32, },
|
|
|
|
{ 19200, 20, 55, 55, 52, },
|
|
|
|
{ 14400, 30, 75, 75, 72, },
|
|
|
|
{ 9600, 50, 114, 114, 112, },
|
|
|
|
{ 4800, 110, 233, 233, 230, },
|
|
|
|
{ 2400, 229, 472, 472, 469, },
|
|
|
|
{ 1200, 467, 948, 948, 945, },
|
2012-12-01 00:20:55 +01:00
|
|
|
{ 600, 948, 1895, 1895, 1890, },
|
2011-10-27 17:45:13 +02:00
|
|
|
{ 300, 1895, 3805, 3805, 3802, },
|
|
|
|
};
|
|
|
|
|
|
|
|
const int XMIT_START_ADJUSTMENT = 4;
|
|
|
|
|
|
|
|
#elif F_CPU == 20000000
|
|
|
|
|
|
|
|
// 20MHz support courtesy of the good people at macegr.com.
|
|
|
|
// Thanks, Garrett!
|
|
|
|
|
|
|
|
static const DELAY_TABLE PROGMEM table[] =
|
|
|
|
{
|
|
|
|
// baud rxcenter rxintra rxstop tx
|
|
|
|
{ 115200, 3, 21, 21, 18, },
|
|
|
|
{ 57600, 20, 43, 43, 41, },
|
|
|
|
{ 38400, 37, 73, 73, 70, },
|
|
|
|
{ 31250, 45, 89, 89, 88, },
|
|
|
|
{ 28800, 46, 98, 98, 95, },
|
|
|
|
{ 19200, 71, 148, 148, 145, },
|
|
|
|
{ 14400, 96, 197, 197, 194, },
|
|
|
|
{ 9600, 146, 297, 297, 294, },
|
|
|
|
{ 4800, 296, 595, 595, 592, },
|
|
|
|
{ 2400, 592, 1189, 1189, 1186, },
|
|
|
|
{ 1200, 1187, 2379, 2379, 2376, },
|
2012-12-01 00:20:55 +01:00
|
|
|
{ 600, 2379, 4759, 4759, 4755, },
|
2011-10-27 17:45:13 +02:00
|
|
|
{ 300, 4759, 9523, 9523, 9520, },
|
|
|
|
};
|
|
|
|
|
|
|
|
const int XMIT_START_ADJUSTMENT = 6;
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#error This version of SoftwareSerial supports only 20, 16 and 8MHz processors
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//
|
|
|
|
// Statics
|
|
|
|
//
|
|
|
|
SoftwareSerial *SoftwareSerial::active_object = 0;
|
|
|
|
char SoftwareSerial::_receive_buffer[_SS_MAX_RX_BUFF];
|
|
|
|
volatile uint8_t SoftwareSerial::_receive_buffer_tail = 0;
|
|
|
|
volatile uint8_t SoftwareSerial::_receive_buffer_head = 0;
|
|
|
|
|
|
|
|
//
|
|
|
|
// Debugging
|
|
|
|
//
|
|
|
|
// This function generates a brief pulse
|
|
|
|
// for debugging or measuring on an oscilloscope.
|
|
|
|
inline void DebugPulse(uint8_t pin, uint8_t count)
|
|
|
|
{
|
|
|
|
#if _DEBUG
|
|
|
|
volatile uint8_t *pport = portOutputRegister(digitalPinToPort(pin));
|
|
|
|
|
|
|
|
uint8_t val = *pport;
|
|
|
|
while (count--)
|
|
|
|
{
|
|
|
|
*pport = val | digitalPinToBitMask(pin);
|
|
|
|
*pport = val;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Private methods
|
|
|
|
//
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
inline void SoftwareSerial::tunedDelay(uint16_t delay) {
|
|
|
|
uint8_t tmp=0;
|
|
|
|
|
|
|
|
asm volatile("sbiw %0, 0x01 \n\t"
|
|
|
|
"ldi %1, 0xFF \n\t"
|
|
|
|
"cpi %A0, 0xFF \n\t"
|
|
|
|
"cpc %B0, %1 \n\t"
|
|
|
|
"brne .-10 \n\t"
|
|
|
|
: "+r" (delay), "+a" (tmp)
|
|
|
|
: "0" (delay)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// This function sets the current object as the "listening"
|
|
|
|
// one and returns true if it replaces another
|
|
|
|
bool SoftwareSerial::listen()
|
|
|
|
{
|
2013-06-04 10:58:56 +02:00
|
|
|
if (!_rx_delay_stopbit)
|
|
|
|
return false;
|
|
|
|
|
2011-10-27 17:45:13 +02:00
|
|
|
if (active_object != this)
|
|
|
|
{
|
2013-06-04 11:30:26 +02:00
|
|
|
if (active_object)
|
|
|
|
active_object->stopListening();
|
|
|
|
|
2011-10-27 17:45:13 +02:00
|
|
|
_buffer_overflow = false;
|
|
|
|
_receive_buffer_head = _receive_buffer_tail = 0;
|
|
|
|
active_object = this;
|
2013-06-04 11:30:26 +02:00
|
|
|
|
|
|
|
setRxIntMsk(true);
|
2011-10-27 17:45:13 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-06-04 11:28:30 +02:00
|
|
|
// Stop listening. Returns true if we were actually listening.
|
|
|
|
bool SoftwareSerial::stopListening()
|
|
|
|
{
|
|
|
|
if (active_object == this)
|
|
|
|
{
|
2013-06-04 11:30:26 +02:00
|
|
|
setRxIntMsk(false);
|
2013-06-04 11:28:30 +02:00
|
|
|
active_object = NULL;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-10-27 17:45:13 +02:00
|
|
|
//
|
|
|
|
// The receive routine called by the interrupt handler
|
|
|
|
//
|
|
|
|
void SoftwareSerial::recv()
|
|
|
|
{
|
|
|
|
|
|
|
|
#if GCC_VERSION < 40302
|
|
|
|
// Work-around for avr-gcc 4.3.0 OSX version bug
|
|
|
|
// Preserve the registers that the compiler misses
|
|
|
|
// (courtesy of Arduino forum user *etracer*)
|
|
|
|
asm volatile(
|
|
|
|
"push r18 \n\t"
|
|
|
|
"push r19 \n\t"
|
|
|
|
"push r20 \n\t"
|
|
|
|
"push r21 \n\t"
|
|
|
|
"push r22 \n\t"
|
|
|
|
"push r23 \n\t"
|
|
|
|
"push r26 \n\t"
|
|
|
|
"push r27 \n\t"
|
|
|
|
::);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
uint8_t d = 0;
|
|
|
|
|
|
|
|
// If RX line is high, then we don't see any start bit
|
|
|
|
// so interrupt is probably not for us
|
|
|
|
if (_inverse_logic ? rx_pin_read() : !rx_pin_read())
|
|
|
|
{
|
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
|
|
|
// Disable further interrupts during reception, this prevents
|
|
|
|
// triggering another interrupt directly after we return, which can
|
|
|
|
// cause problems at higher baudrates.
|
|
|
|
setRxIntMsk(false);
|
|
|
|
|
2011-10-27 17:45:13 +02:00
|
|
|
// Wait approximately 1/2 of a bit width to "center" the sample
|
|
|
|
tunedDelay(_rx_delay_centering);
|
|
|
|
DebugPulse(_DEBUG_PIN2, 1);
|
|
|
|
|
|
|
|
// Read each of the 8 bits
|
2014-04-22 22:24:43 +02:00
|
|
|
for (uint8_t i=8; i > 0; --i)
|
2011-10-27 17:45:13 +02:00
|
|
|
{
|
|
|
|
tunedDelay(_rx_delay_intrabit);
|
2014-04-22 22:24:43 +02:00
|
|
|
d >>= 1;
|
2011-10-27 17:45:13 +02:00
|
|
|
DebugPulse(_DEBUG_PIN2, 1);
|
|
|
|
if (rx_pin_read())
|
2014-04-22 22:24:43 +02:00
|
|
|
d |= 0x80;
|
2011-10-27 17:45:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// skip the stop bit
|
|
|
|
tunedDelay(_rx_delay_stopbit);
|
|
|
|
DebugPulse(_DEBUG_PIN2, 1);
|
|
|
|
|
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
|
|
|
// Re-enable interrupts when we're sure to be inside the stop bit
|
|
|
|
setRxIntMsk(true);
|
2011-10-27 17:45:13 +02:00
|
|
|
if (_inverse_logic)
|
|
|
|
d = ~d;
|
|
|
|
|
|
|
|
// if buffer full, set the overflow flag and return
|
2014-04-23 17:45:10 +02:00
|
|
|
uint8_t next = (_receive_buffer_tail + 1) % _SS_MAX_RX_BUFF;
|
|
|
|
if (next != _receive_buffer_head)
|
2011-10-27 17:45:13 +02:00
|
|
|
{
|
|
|
|
// save new data in buffer: tail points to where byte goes
|
|
|
|
_receive_buffer[_receive_buffer_tail] = d; // save new byte
|
2014-04-23 17:45:10 +02:00
|
|
|
_receive_buffer_tail = next;
|
2011-10-27 17:45:13 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#if _DEBUG // for scope: pulse pin as overflow indictator
|
|
|
|
DebugPulse(_DEBUG_PIN1, 1);
|
|
|
|
#endif
|
|
|
|
_buffer_overflow = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if GCC_VERSION < 40302
|
|
|
|
// Work-around for avr-gcc 4.3.0 OSX version bug
|
|
|
|
// Restore the registers that the compiler misses
|
|
|
|
asm volatile(
|
|
|
|
"pop r27 \n\t"
|
|
|
|
"pop r26 \n\t"
|
|
|
|
"pop r23 \n\t"
|
|
|
|
"pop r22 \n\t"
|
|
|
|
"pop r21 \n\t"
|
|
|
|
"pop r20 \n\t"
|
|
|
|
"pop r19 \n\t"
|
|
|
|
"pop r18 \n\t"
|
|
|
|
::);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t SoftwareSerial::rx_pin_read()
|
|
|
|
{
|
|
|
|
return *_receivePortRegister & _receiveBitMask;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Interrupt handling
|
|
|
|
//
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
inline void SoftwareSerial::handle_interrupt()
|
|
|
|
{
|
|
|
|
if (active_object)
|
|
|
|
{
|
|
|
|
active_object->recv();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(PCINT0_vect)
|
|
|
|
ISR(PCINT0_vect)
|
|
|
|
{
|
|
|
|
SoftwareSerial::handle_interrupt();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(PCINT1_vect)
|
In SoftwareSerial, use ISR_ALIASOF to prevent duplication
Previously, up to four separate but identical ISR routines were defined,
for PCINT0, PCINT1, PCINT2 and PCINT3. Each of these would generate
their own function, with a lot of push-popping because another function
was called.
Now, the ISR_ALIASOF macro from avr-libc is used to declare just the
PCINT0 version and make all other ISRs point to that one, saving a lot
of program space, as well as some speed because of improved inlining.
On an Arduino Uno with gcc 4.3, this saves 168 bytes. With gcc 4.8, this
saves 150 bytes.
2014-04-23 14:41:12 +02:00
|
|
|
ISR(PCINT1_vect, ISR_ALIASOF(PCINT0_vect));
|
2011-10-27 17:45:13 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(PCINT2_vect)
|
In SoftwareSerial, use ISR_ALIASOF to prevent duplication
Previously, up to four separate but identical ISR routines were defined,
for PCINT0, PCINT1, PCINT2 and PCINT3. Each of these would generate
their own function, with a lot of push-popping because another function
was called.
Now, the ISR_ALIASOF macro from avr-libc is used to declare just the
PCINT0 version and make all other ISRs point to that one, saving a lot
of program space, as well as some speed because of improved inlining.
On an Arduino Uno with gcc 4.3, this saves 168 bytes. With gcc 4.8, this
saves 150 bytes.
2014-04-23 14:41:12 +02:00
|
|
|
ISR(PCINT2_vect, ISR_ALIASOF(PCINT0_vect));
|
2011-10-27 17:45:13 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(PCINT3_vect)
|
In SoftwareSerial, use ISR_ALIASOF to prevent duplication
Previously, up to four separate but identical ISR routines were defined,
for PCINT0, PCINT1, PCINT2 and PCINT3. Each of these would generate
their own function, with a lot of push-popping because another function
was called.
Now, the ISR_ALIASOF macro from avr-libc is used to declare just the
PCINT0 version and make all other ISRs point to that one, saving a lot
of program space, as well as some speed because of improved inlining.
On an Arduino Uno with gcc 4.3, this saves 168 bytes. With gcc 4.8, this
saves 150 bytes.
2014-04-23 14:41:12 +02:00
|
|
|
ISR(PCINT3_vect, ISR_ALIASOF(PCINT0_vect));
|
2011-10-27 17:45:13 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
//
|
|
|
|
// Constructor
|
|
|
|
//
|
|
|
|
SoftwareSerial::SoftwareSerial(uint8_t receivePin, uint8_t transmitPin, bool inverse_logic /* = false */) :
|
|
|
|
_rx_delay_centering(0),
|
|
|
|
_rx_delay_intrabit(0),
|
|
|
|
_rx_delay_stopbit(0),
|
|
|
|
_tx_delay(0),
|
|
|
|
_buffer_overflow(false),
|
|
|
|
_inverse_logic(inverse_logic)
|
|
|
|
{
|
|
|
|
setTX(transmitPin);
|
|
|
|
setRX(receivePin);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Destructor
|
|
|
|
//
|
|
|
|
SoftwareSerial::~SoftwareSerial()
|
|
|
|
{
|
|
|
|
end();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SoftwareSerial::setTX(uint8_t tx)
|
|
|
|
{
|
|
|
|
pinMode(tx, OUTPUT);
|
2014-05-23 11:29:30 +02:00
|
|
|
digitalWrite(tx, _inverse_logic ? LOW : HIGH);
|
2011-10-27 17:45:13 +02:00
|
|
|
_transmitBitMask = digitalPinToBitMask(tx);
|
|
|
|
uint8_t port = digitalPinToPort(tx);
|
|
|
|
_transmitPortRegister = portOutputRegister(port);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SoftwareSerial::setRX(uint8_t rx)
|
|
|
|
{
|
|
|
|
pinMode(rx, INPUT);
|
|
|
|
if (!_inverse_logic)
|
|
|
|
digitalWrite(rx, HIGH); // pullup for normal logic!
|
|
|
|
_receivePin = rx;
|
|
|
|
_receiveBitMask = digitalPinToBitMask(rx);
|
|
|
|
uint8_t port = digitalPinToPort(rx);
|
|
|
|
_receivePortRegister = portInputRegister(port);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Public methods
|
|
|
|
//
|
|
|
|
|
2012-12-02 17:22:59 +01:00
|
|
|
void SoftwareSerial::begin(long speed)
|
2011-10-27 17:45:13 +02:00
|
|
|
{
|
|
|
|
_rx_delay_centering = _rx_delay_intrabit = _rx_delay_stopbit = _tx_delay = 0;
|
|
|
|
|
|
|
|
for (unsigned i=0; i<sizeof(table)/sizeof(table[0]); ++i)
|
|
|
|
{
|
2012-12-02 17:22:59 +01:00
|
|
|
long baud = pgm_read_dword(&table[i].baud);
|
2011-10-27 17:45:13 +02:00
|
|
|
if (baud == speed)
|
|
|
|
{
|
2013-06-04 11:04:30 +02:00
|
|
|
if (digitalPinToPCICR(_receivePin))
|
|
|
|
{
|
|
|
|
// Only setup rx when we have a valid PCINT for this pin
|
|
|
|
_rx_delay_centering = pgm_read_word(&table[i].rx_delay_centering);
|
|
|
|
_rx_delay_intrabit = pgm_read_word(&table[i].rx_delay_intrabit);
|
|
|
|
_rx_delay_stopbit = pgm_read_word(&table[i].rx_delay_stopbit);
|
|
|
|
}
|
2011-10-27 17:45:13 +02:00
|
|
|
_tx_delay = pgm_read_word(&table[i].tx_delay);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set up RX interrupts, but only if we have a valid RX baud rate
|
|
|
|
if (_rx_delay_stopbit)
|
|
|
|
{
|
2013-06-04 11:22:19 +02:00
|
|
|
// Enable the PCINT for the entire port here, but never disable it
|
|
|
|
// (others might also need it, so we disable the interrupt by using
|
|
|
|
// the per-pin PCMSK register).
|
2013-06-04 11:04:30 +02:00
|
|
|
*digitalPinToPCICR(_receivePin) |= _BV(digitalPinToPCICRbit(_receivePin));
|
2014-04-23 19:13:58 +02:00
|
|
|
// Precalculate the pcint mask register and value, so setRxIntMask
|
|
|
|
// can be used inside the ISR without costing too much time.
|
|
|
|
_pcint_maskreg = digitalPinToPCMSK(_receivePin);
|
|
|
|
_pcint_maskvalue = _BV(digitalPinToPCMSKbit(_receivePin));
|
|
|
|
|
2011-10-27 17:45:13 +02:00
|
|
|
tunedDelay(_tx_delay); // if we were low this establishes the end
|
|
|
|
}
|
|
|
|
|
|
|
|
#if _DEBUG
|
|
|
|
pinMode(_DEBUG_PIN1, OUTPUT);
|
|
|
|
pinMode(_DEBUG_PIN2, OUTPUT);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
listen();
|
|
|
|
}
|
|
|
|
|
2013-06-04 11:22:19 +02:00
|
|
|
void SoftwareSerial::setRxIntMsk(bool enable)
|
|
|
|
{
|
|
|
|
if (enable)
|
2014-04-23 19:13:58 +02:00
|
|
|
*_pcint_maskreg |= _pcint_maskvalue;
|
2013-06-04 11:22:19 +02:00
|
|
|
else
|
2014-04-23 19:13:58 +02:00
|
|
|
*_pcint_maskreg &= ~_pcint_maskvalue;
|
2013-06-04 11:22:19 +02:00
|
|
|
}
|
|
|
|
|
2011-10-27 17:45:13 +02:00
|
|
|
void SoftwareSerial::end()
|
|
|
|
{
|
2013-06-13 10:26:15 +02:00
|
|
|
stopListening();
|
2011-10-27 17:45:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Read data from buffer
|
|
|
|
int SoftwareSerial::read()
|
|
|
|
{
|
|
|
|
if (!isListening())
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
// Empty buffer?
|
|
|
|
if (_receive_buffer_head == _receive_buffer_tail)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
// Read from "head"
|
|
|
|
uint8_t d = _receive_buffer[_receive_buffer_head]; // grab next byte
|
|
|
|
_receive_buffer_head = (_receive_buffer_head + 1) % _SS_MAX_RX_BUFF;
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
|
|
|
int SoftwareSerial::available()
|
|
|
|
{
|
|
|
|
if (!isListening())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return (_receive_buffer_tail + _SS_MAX_RX_BUFF - _receive_buffer_head) % _SS_MAX_RX_BUFF;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t SoftwareSerial::write(uint8_t b)
|
|
|
|
{
|
|
|
|
if (_tx_delay == 0) {
|
|
|
|
setWriteError();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-04-22 20:52:09 +02:00
|
|
|
// By declaring these as local variables, the compiler will put them
|
|
|
|
// in registers _before_ disabling interrupts and entering the
|
|
|
|
// critical timing sections below, which makes it a lot easier to
|
|
|
|
// verify the cycle timings
|
|
|
|
volatile uint8_t *reg = _transmitPortRegister;
|
|
|
|
uint8_t reg_mask = _transmitBitMask;
|
|
|
|
uint8_t inv_mask = ~_transmitBitMask;
|
2011-10-27 17:45:13 +02:00
|
|
|
uint8_t oldSREG = SREG;
|
2014-04-22 20:52:09 +02:00
|
|
|
bool inv = _inverse_logic;
|
|
|
|
uint16_t delay = _tx_delay;
|
|
|
|
|
|
|
|
if (inv)
|
|
|
|
b = ~b;
|
|
|
|
|
2011-10-27 17:45:13 +02:00
|
|
|
cli(); // turn off interrupts for a clean txmit
|
|
|
|
|
|
|
|
// Write the start bit
|
2014-04-22 20:52:09 +02:00
|
|
|
if (inv)
|
|
|
|
*reg |= reg_mask;
|
|
|
|
else
|
|
|
|
*reg &= inv_mask;
|
2011-10-27 17:45:13 +02:00
|
|
|
|
2014-04-22 20:52:09 +02:00
|
|
|
tunedDelay(delay);
|
2011-10-27 17:45:13 +02:00
|
|
|
|
2014-04-22 20:52:09 +02:00
|
|
|
// Write each of the 8 bits
|
|
|
|
for (uint8_t i = 8; i > 0; --i)
|
2011-10-27 17:45:13 +02:00
|
|
|
{
|
2014-04-22 20:52:09 +02:00
|
|
|
if (b & 1) // choose bit
|
|
|
|
*reg |= reg_mask; // send 1
|
2014-04-22 16:30:13 +02:00
|
|
|
else
|
2014-04-22 20:52:09 +02:00
|
|
|
*reg &= inv_mask; // send 0
|
2011-10-27 17:45:13 +02:00
|
|
|
|
2014-04-22 20:52:09 +02:00
|
|
|
tunedDelay(delay);
|
|
|
|
b >>= 1;
|
2011-10-27 17:45:13 +02:00
|
|
|
}
|
|
|
|
|
2014-04-22 16:30:13 +02:00
|
|
|
// restore pin to natural state
|
2014-04-22 20:52:09 +02:00
|
|
|
if (inv)
|
|
|
|
*reg &= inv_mask;
|
|
|
|
else
|
|
|
|
*reg |= reg_mask;
|
2014-04-22 16:30:13 +02:00
|
|
|
|
2011-10-27 17:45:13 +02:00
|
|
|
SREG = oldSREG; // turn interrupts back on
|
|
|
|
tunedDelay(_tx_delay);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SoftwareSerial::flush()
|
|
|
|
{
|
|
|
|
if (!isListening())
|
|
|
|
return;
|
|
|
|
|
|
|
|
uint8_t oldSREG = SREG;
|
|
|
|
cli();
|
|
|
|
_receive_buffer_head = _receive_buffer_tail = 0;
|
|
|
|
SREG = oldSREG;
|
|
|
|
}
|
|
|
|
|
|
|
|
int SoftwareSerial::peek()
|
|
|
|
{
|
|
|
|
if (!isListening())
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
// Empty buffer?
|
|
|
|
if (_receive_buffer_head == _receive_buffer_tail)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
// Read from "head"
|
|
|
|
return _receive_buffer[_receive_buffer_head];
|
|
|
|
}
|