Skip to content
This repository
Browse code

Adding new SoftwareSerial (NewSoftSerial by Mikal Hart).

  • Loading branch information...
commit 80bb16db8c2626db8016d8b6eb87be62b23d93bc 1 parent e816ffb
David A. Mellis damellis authored
534 libraries/SoftwareSerial/SoftwareSerial.cpp
... ... @@ -0,0 +1,534 @@
  1 +/*
  2 +SoftwareSerial.cpp (formerly NewSoftSerial.cpp) -
  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 +
  14 +This library is free software; you can redistribute it and/or
  15 +modify it under the terms of the GNU Lesser General Public
  16 +License as published by the Free Software Foundation; either
  17 +version 2.1 of the License, or (at your option) any later version.
  18 +
  19 +This library is distributed in the hope that it will be useful,
  20 +but WITHOUT ANY WARRANTY; without even the implied warranty of
  21 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  22 +Lesser General Public License for more details.
  23 +
  24 +You should have received a copy of the GNU Lesser General Public
  25 +License along with this library; if not, write to the Free Software
  26 +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  27 +
  28 +The latest version of this library can always be found at
  29 +http://arduiniana.org.
  30 +*/
  31 +
  32 +// When set, _DEBUG co-opts pins 11 and 13 for debugging with an
  33 +// oscilloscope or logic analyzer. Beware: it also slightly modifies
  34 +// the bit times, so don't rely on it too much at high baud rates
  35 +#define _DEBUG 0
  36 +#define _DEBUG_PIN1 11
  37 +#define _DEBUG_PIN2 13
  38 +//
  39 +// Includes
  40 +//
  41 +#include <avr/interrupt.h>
  42 +#include <avr/pgmspace.h>
  43 +#include "WConstants.h"
  44 +#include "pins_arduino.h"
  45 +#include "SoftwareSerial.h"
  46 +#include "icrmacros.h"
  47 +//
  48 +// Lookup table
  49 +//
  50 +typedef struct _DELAY_TABLE
  51 +{
  52 + long baud;
  53 + unsigned short rx_delay_centering;
  54 + unsigned short rx_delay_intrabit;
  55 + unsigned short rx_delay_stopbit;
  56 + unsigned short tx_delay;
  57 +} DELAY_TABLE;
  58 +
  59 +#if F_CPU == 16000000
  60 +
  61 +static const DELAY_TABLE PROGMEM table[] =
  62 +{
  63 + // baud rxcenter rxintra rxstop tx
  64 + { 115200, 1, 17, 17, 12, },
  65 + { 57600, 10, 37, 37, 33, },
  66 + { 38400, 25, 57, 57, 54, },
  67 + { 31250, 31, 70, 70, 68, },
  68 + { 28800, 34, 77, 77, 74, },
  69 + { 19200, 54, 117, 117, 114, },
  70 + { 14400, 74, 156, 156, 153, },
  71 + { 9600, 114, 236, 236, 233, },
  72 + { 4800, 233, 474, 474, 471, },
  73 + { 2400, 471, 950, 950, 947, },
  74 + { 1200, 947, 1902, 1902, 1899, },
  75 + { 300, 3804, 7617, 7617, 7614, },
  76 +};
  77 +
  78 +const int XMIT_START_ADJUSTMENT = 5;
  79 +
  80 +#elif F_CPU == 8000000
  81 +
  82 +static const DELAY_TABLE table[] PROGMEM =
  83 +{
  84 + // baud rxcenter rxintra rxstop tx
  85 + { 115200, 1, 5, 5, 3, },
  86 + { 57600, 1, 15, 15, 13, },
  87 + { 38400, 2, 25, 26, 23, },
  88 + { 31250, 7, 32, 33, 29, },
  89 + { 28800, 11, 35, 35, 32, },
  90 + { 19200, 20, 55, 55, 52, },
  91 + { 14400, 30, 75, 75, 72, },
  92 + { 9600, 50, 114, 114, 112, },
  93 + { 4800, 110, 233, 233, 230, },
  94 + { 2400, 229, 472, 472, 469, },
  95 + { 1200, 467, 948, 948, 945, },
  96 + { 300, 1895, 3805, 3805, 3802, },
  97 +};
  98 +
  99 +const int XMIT_START_ADJUSTMENT = 4;
  100 +
  101 +#elif F_CPU == 20000000
  102 +
  103 +// 20MHz support courtesy of the good people at macegr.com.
  104 +// Thanks, Garrett!
  105 +
  106 +static const DELAY_TABLE PROGMEM table[] =
  107 +{
  108 + // baud rxcenter rxintra rxstop tx
  109 + { 115200, 3, 21, 21, 18, },
  110 + { 57600, 20, 43, 43, 41, },
  111 + { 38400, 37, 73, 73, 70, },
  112 + { 31250, 45, 89, 89, 88, },
  113 + { 28800, 46, 98, 98, 95, },
  114 + { 19200, 71, 148, 148, 145, },
  115 + { 14400, 96, 197, 197, 194, },
  116 + { 9600, 146, 297, 297, 294, },
  117 + { 4800, 296, 595, 595, 592, },
  118 + { 2400, 592, 1189, 1189, 1186, },
  119 + { 1200, 1187, 2379, 2379, 2376, },
  120 + { 300, 4759, 9523, 9523, 9520, },
  121 +};
  122 +
  123 +const int XMIT_START_ADJUSTMENT = 6;
  124 +
  125 +#else
  126 +
  127 +#error This version of SoftwareSerial supports only 20, 16 and 8MHz processors
  128 +
  129 +#endif
  130 +
  131 +//
  132 +// Statics
  133 +//
  134 +SoftwareSerial *SoftwareSerial::active_object = 0;
  135 +char SoftwareSerial::_receive_buffer[_SS_MAX_RX_BUFF];
  136 +volatile uint8_t SoftwareSerial::_receive_buffer_tail = 0;
  137 +volatile uint8_t SoftwareSerial::_receive_buffer_head = 0;
  138 +
  139 +//
  140 +// Debugging
  141 +//
  142 +// This function generates a brief pulse
  143 +// for debugging or measuring on an oscilloscope.
  144 +inline void DebugPulse(uint8_t pin, uint8_t count)
  145 +{
  146 +#if _DEBUG
  147 + volatile uint8_t *pport = portOutputRegister(digitalPinToPort(pin));
  148 +
  149 + uint8_t val = *pport;
  150 + while (count--)
  151 + {
  152 + *pport = val | digitalPinToBitMask(pin);
  153 + *pport = val;
  154 + }
  155 +#endif
  156 +}
  157 +
  158 +//
  159 +// Private methods
  160 +//
  161 +
  162 +/* static */
  163 +inline void SoftwareSerial::tunedDelay(uint16_t delay) {
  164 + uint8_t tmp=0;
  165 +
  166 + asm volatile("sbiw %0, 0x01 \n\t"
  167 + "ldi %1, 0xFF \n\t"
  168 + "cpi %A0, 0xFF \n\t"
  169 + "cpc %B0, %1 \n\t"
  170 + "brne .-10 \n\t"
  171 + : "+r" (delay), "+a" (tmp)
  172 + : "0" (delay)
  173 + );
  174 +}
  175 +
  176 +// This function sets the current object as the "listening"
  177 +// one and returns true if it replaces another
  178 +bool SoftwareSerial::listen()
  179 +{
  180 + if (active_object != this)
  181 + {
  182 + _buffer_overflow = false;
  183 + uint8_t oldSREG = SREG;
  184 + cli();
  185 + _receive_buffer_head = _receive_buffer_tail = 0;
  186 + active_object = this;
  187 + SREG = oldSREG;
  188 + return true;
  189 + }
  190 +
  191 + return false;
  192 +}
  193 +
  194 +//
  195 +// The receive routine called by the interrupt handler
  196 +//
  197 +void SoftwareSerial::recv()
  198 +{
  199 +
  200 +#if GCC_VERSION < 40302
  201 +// Work-around for avr-gcc 4.3.0 OSX version bug
  202 +// Preserve the registers that the compiler misses
  203 +// (courtesy of Arduino forum user *etracer*)
  204 + asm volatile(
  205 + "push r18 \n\t"
  206 + "push r19 \n\t"
  207 + "push r20 \n\t"
  208 + "push r21 \n\t"
  209 + "push r22 \n\t"
  210 + "push r23 \n\t"
  211 + "push r26 \n\t"
  212 + "push r27 \n\t"
  213 + ::);
  214 +#endif
  215 +
  216 + uint8_t d = 0;
  217 +
  218 + // If RX line is high, then we don't see any start bit
  219 + // so interrupt is probably not for us
  220 + if (_inverse_logic ? rx_pin_read() : !rx_pin_read())
  221 + {
  222 + // Wait approximately 1/2 of a bit width to "center" the sample
  223 + tunedDelay(_rx_delay_centering);
  224 + DebugPulse(_DEBUG_PIN2, 1);
  225 +
  226 + // Read each of the 8 bits
  227 + for (uint8_t i=0x1; i; i <<= 1)
  228 + {
  229 + tunedDelay(_rx_delay_intrabit);
  230 + DebugPulse(_DEBUG_PIN2, 1);
  231 + uint8_t noti = ~i;
  232 + if (rx_pin_read())
  233 + d |= i;
  234 + else // else clause added to ensure function timing is ~balanced
  235 + d &= noti;
  236 + }
  237 +
  238 + // skip the stop bit
  239 + tunedDelay(_rx_delay_stopbit);
  240 + DebugPulse(_DEBUG_PIN2, 1);
  241 +
  242 + if (_inverse_logic)
  243 + d = ~d;
  244 +
  245 + // if buffer full, set the overflow flag and return
  246 + if ((_receive_buffer_tail + 1) % _SS_MAX_RX_BUFF != _receive_buffer_head)
  247 + {
  248 + // save new data in buffer: tail points to where byte goes
  249 + _receive_buffer[_receive_buffer_tail] = d; // save new byte
  250 + _receive_buffer_tail = (_receive_buffer_tail + 1) % _SS_MAX_RX_BUFF;
  251 + }
  252 + else
  253 + {
  254 +#if _DEBUG // for scope: pulse pin as overflow indictator
  255 + DebugPulse(_DEBUG_PIN1, 1);
  256 +#endif
  257 + _buffer_overflow = true;
  258 + }
  259 + }
  260 +
  261 +#if GCC_VERSION < 40302
  262 +// Work-around for avr-gcc 4.3.0 OSX version bug
  263 +// Restore the registers that the compiler misses
  264 + asm volatile(
  265 + "pop r27 \n\t"
  266 + "pop r26 \n\t"
  267 + "pop r23 \n\t"
  268 + "pop r22 \n\t"
  269 + "pop r21 \n\t"
  270 + "pop r20 \n\t"
  271 + "pop r19 \n\t"
  272 + "pop r18 \n\t"
  273 + ::);
  274 +#endif
  275 +}
  276 +
  277 +void SoftwareSerial::tx_pin_write(uint8_t pin_state)
  278 +{
  279 + if (pin_state == LOW)
  280 + *_transmitPortRegister &= ~_transmitBitMask;
  281 + else
  282 + *_transmitPortRegister |= _transmitBitMask;
  283 +}
  284 +
  285 +uint8_t SoftwareSerial::rx_pin_read()
  286 +{
  287 + return *_receivePortRegister & _receiveBitMask;
  288 +}
  289 +
  290 +//
  291 +// Interrupt handling
  292 +//
  293 +
  294 +/* static */
  295 +inline void SoftwareSerial::handle_interrupt()
  296 +{
  297 + if (active_object)
  298 + {
  299 + active_object->recv();
  300 + }
  301 +}
  302 +
  303 +#if defined(PCINT0_vect)
  304 +ISR(PCINT0_vect)
  305 +{
  306 + SoftwareSerial::handle_interrupt();
  307 +}
  308 +#endif
  309 +
  310 +#if defined(PCINT1_vect)
  311 +ISR(PCINT1_vect)
  312 +{
  313 + SoftwareSerial::handle_interrupt();
  314 +}
  315 +#endif
  316 +
  317 +#if defined(PCINT2_vect)
  318 +ISR(PCINT2_vect)
  319 +{
  320 + SoftwareSerial::handle_interrupt();
  321 +}
  322 +#endif
  323 +
  324 +#if defined(PCINT3_vect)
  325 +ISR(PCINT3_vect)
  326 +{
  327 + SoftwareSerial::handle_interrupt();
  328 +}
  329 +#endif
  330 +
  331 +//
  332 +// Constructor
  333 +//
  334 +SoftwareSerial::SoftwareSerial(uint8_t receivePin, uint8_t transmitPin, bool inverse_logic /* = false */) :
  335 + _rx_delay_centering(0),
  336 + _rx_delay_intrabit(0),
  337 + _rx_delay_stopbit(0),
  338 + _tx_delay(0),
  339 + _buffer_overflow(false),
  340 + _inverse_logic(inverse_logic)
  341 +{
  342 + setTX(transmitPin);
  343 + setRX(receivePin);
  344 +}
  345 +
  346 +//
  347 +// Destructor
  348 +//
  349 +SoftwareSerial::~SoftwareSerial()
  350 +{
  351 + end();
  352 +}
  353 +
  354 +void SoftwareSerial::setTX(uint8_t tx)
  355 +{
  356 + pinMode(tx, OUTPUT);
  357 + digitalWrite(tx, HIGH);
  358 + _transmitBitMask = digitalPinToBitMask(tx);
  359 + uint8_t port = digitalPinToPort(tx);
  360 + _transmitPortRegister = portOutputRegister(port);
  361 +}
  362 +
  363 +void SoftwareSerial::setRX(uint8_t rx)
  364 +{
  365 + pinMode(rx, INPUT);
  366 + if (!_inverse_logic)
  367 + digitalWrite(rx, HIGH); // pullup for normal logic!
  368 + _receivePin = rx;
  369 + _receiveBitMask = digitalPinToBitMask(rx);
  370 + uint8_t port = digitalPinToPort(rx);
  371 + _receivePortRegister = portInputRegister(port);
  372 +}
  373 +
  374 +//
  375 +// Public methods
  376 +//
  377 +
  378 +void SoftwareSerial::begin(long speed)
  379 +{
  380 + _rx_delay_centering = _rx_delay_intrabit = _rx_delay_stopbit = _tx_delay = 0;
  381 +
  382 + for (unsigned i=0; i<sizeof(table)/sizeof(table[0]); ++i)
  383 + {
  384 + long baud = pgm_read_dword(&table[i].baud);
  385 + if (baud == speed)
  386 + {
  387 + _rx_delay_centering = pgm_read_word(&table[i].rx_delay_centering);
  388 + _rx_delay_intrabit = pgm_read_word(&table[i].rx_delay_intrabit);
  389 + _rx_delay_stopbit = pgm_read_word(&table[i].rx_delay_stopbit);
  390 + _tx_delay = pgm_read_word(&table[i].tx_delay);
  391 + break;
  392 + }
  393 + }
  394 +
  395 + // Set up RX interrupts, but only if we have a valid RX baud rate
  396 + if (_rx_delay_stopbit)
  397 + {
  398 + if (digitalPinToPCICR(_receivePin))
  399 + {
  400 + *digitalPinToPCICR(_receivePin) |= _BV(digitalPinToPCICRbit(_receivePin));
  401 + *digitalPinToPCMSK(_receivePin) |= _BV(digitalPinToPCMSKbit(_receivePin));
  402 + }
  403 + tunedDelay(_tx_delay); // if we were low this establishes the end
  404 + }
  405 +
  406 +#if _DEBUG
  407 + pinMode(_DEBUG_PIN1, OUTPUT);
  408 + pinMode(_DEBUG_PIN2, OUTPUT);
  409 +#endif
  410 +
  411 + listen();
  412 +}
  413 +
  414 +void SoftwareSerial::end()
  415 +{
  416 + if (digitalPinToPCMSK(_receivePin))
  417 + *digitalPinToPCMSK(_receivePin) &= ~_BV(digitalPinToPCMSKbit(_receivePin));
  418 +}
  419 +
  420 +
  421 +// Read data from buffer
  422 +int SoftwareSerial::read()
  423 +{
  424 + if (!is_listening())
  425 + return -1;
  426 +
  427 + // Empty buffer?
  428 + if (_receive_buffer_head == _receive_buffer_tail)
  429 + return -1;
  430 +
  431 + // Read from "head"
  432 + uint8_t d = _receive_buffer[_receive_buffer_head]; // grab next byte
  433 + _receive_buffer_head = (_receive_buffer_head + 1) % _SS_MAX_RX_BUFF;
  434 + return d;
  435 +}
  436 +
  437 +int SoftwareSerial::available()
  438 +{
  439 + if (!is_listening())
  440 + return 0;
  441 +
  442 + return (_receive_buffer_tail + _SS_MAX_RX_BUFF - _receive_buffer_head) % _SS_MAX_RX_BUFF;
  443 +}
  444 +
  445 +void SoftwareSerial::write(uint8_t b)
  446 +{
  447 + if (_tx_delay == 0)
  448 + return;
  449 +
  450 + uint8_t oldSREG = SREG;
  451 + cli(); // turn off interrupts for a clean txmit
  452 +
  453 + // Write the start bit
  454 + tx_pin_write(_inverse_logic ? HIGH : LOW);
  455 + tunedDelay(_tx_delay + XMIT_START_ADJUSTMENT);
  456 +
  457 + // Write each of the 8 bits
  458 + if (_inverse_logic)
  459 + {
  460 + for (byte mask = 0x01; mask; mask <<= 1)
  461 + {
  462 + if (b & mask) // choose bit
  463 + tx_pin_write(LOW); // send 1
  464 + else
  465 + tx_pin_write(HIGH); // send 0
  466 +
  467 + tunedDelay(_tx_delay);
  468 + }
  469 +
  470 + tx_pin_write(LOW); // restore pin to natural state
  471 + }
  472 + else
  473 + {
  474 + for (byte mask = 0x01; mask; mask <<= 1)
  475 + {
  476 + if (b & mask) // choose bit
  477 + tx_pin_write(HIGH); // send 1
  478 + else
  479 + tx_pin_write(LOW); // send 0
  480 +
  481 + tunedDelay(_tx_delay);
  482 + }
  483 +
  484 + tx_pin_write(HIGH); // restore pin to natural state
  485 + }
  486 +
  487 + SREG = oldSREG; // turn interrupts back on
  488 + tunedDelay(_tx_delay);
  489 +}
  490 +
  491 +#if !defined(cbi)
  492 +#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
  493 +#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
  494 +#endif
  495 +
  496 +void SoftwareSerial::enable_timer0(bool enable)
  497 +{
  498 + if (enable)
  499 +#if defined(__AVR_ATmega8__)
  500 + sbi(TIMSK, TOIE0);
  501 +#else
  502 + sbi(TIMSK0, TOIE0);
  503 +#endif
  504 + else
  505 +#if defined(__AVR_ATmega8__)
  506 + cbi(TIMSK, TOIE0);
  507 +#else
  508 + cbi(TIMSK0, TOIE0);
  509 +#endif
  510 +}
  511 +
  512 +void SoftwareSerial::flush()
  513 +{
  514 + if (!is_listening())
  515 + return;
  516 +
  517 + uint8_t oldSREG = SREG;
  518 + cli();
  519 + _receive_buffer_head = _receive_buffer_tail = 0;
  520 + SREG = oldSREG;
  521 +}
  522 +
  523 +int SoftwareSerial::peek()
  524 +{
  525 + if (!is_listening())
  526 + return -1;
  527 +
  528 + // Empty buffer?
  529 + if (_receive_buffer_head == _receive_buffer_tail)
  530 + return -1;
  531 +
  532 + // Read from "head"
  533 + return _receive_buffer[_receive_buffer_head];
  534 +}
113 libraries/SoftwareSerial/SoftwareSerial.h
... ... @@ -0,0 +1,113 @@
  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 +
  14 +This library is free software; you can redistribute it and/or
  15 +modify it under the terms of the GNU Lesser General Public
  16 +License as published by the Free Software Foundation; either
  17 +version 2.1 of the License, or (at your option) any later version.
  18 +
  19 +This library is distributed in the hope that it will be useful,
  20 +but WITHOUT ANY WARRANTY; without even the implied warranty of
  21 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  22 +Lesser General Public License for more details.
  23 +
  24 +You should have received a copy of the GNU Lesser General Public
  25 +License along with this library; if not, write to the Free Software
  26 +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  27 +
  28 +The latest version of this library can always be found at
  29 +http://arduiniana.org.
  30 +*/
  31 +
  32 +#ifndef SoftwareSerial_h
  33 +#define SoftwareSerial_h
  34 +
  35 +#include <inttypes.h>
  36 +#include <Stream.h>
  37 +
  38 +/******************************************************************************
  39 +* Definitions
  40 +******************************************************************************/
  41 +
  42 +#define _SS_MAX_RX_BUFF 64 // RX buffer size
  43 +#define _SS_VERSION 11 // software version of this library
  44 +#ifndef GCC_VERSION
  45 +#define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
  46 +#endif
  47 +
  48 +class SoftwareSerial : public Stream
  49 +{
  50 +private:
  51 + // per object data
  52 + uint8_t _receivePin;
  53 + uint8_t _receiveBitMask;
  54 + volatile uint8_t *_receivePortRegister;
  55 + uint8_t _transmitBitMask;
  56 + volatile uint8_t *_transmitPortRegister;
  57 +
  58 + uint16_t _rx_delay_centering;
  59 + uint16_t _rx_delay_intrabit;
  60 + uint16_t _rx_delay_stopbit;
  61 + uint16_t _tx_delay;
  62 +
  63 + uint16_t _buffer_overflow:1;
  64 + uint16_t _inverse_logic:1;
  65 +
  66 + // static data
  67 + static char _receive_buffer[_SS_MAX_RX_BUFF];
  68 + static volatile uint8_t _receive_buffer_tail;
  69 + static volatile uint8_t _receive_buffer_head;
  70 + static SoftwareSerial *active_object;
  71 +
  72 + // private methods
  73 + void recv();
  74 + uint8_t rx_pin_read();
  75 + void tx_pin_write(uint8_t pin_state);
  76 + void setTX(uint8_t transmitPin);
  77 + void setRX(uint8_t receivePin);
  78 +
  79 + // private static method for timing
  80 + static inline void tunedDelay(uint16_t delay);
  81 +
  82 +public:
  83 + // public methods
  84 + SoftwareSerial(uint8_t receivePin, uint8_t transmitPin, bool inverse_logic = false);
  85 + ~SoftwareSerial();
  86 + void begin(long speed);
  87 + bool listen();
  88 + void end();
  89 + bool is_listening() { return this == active_object; }
  90 + bool overflow() { bool ret = _buffer_overflow; _buffer_overflow = false; return ret; }
  91 + static int library_version() { return _SS_VERSION; }
  92 + static void enable_timer0(bool enable);
  93 + int peek();
  94 +
  95 + virtual void write(uint8_t byte);
  96 + virtual int read();
  97 + virtual int available();
  98 + virtual void flush();
  99 +
  100 + // public only for easy access by interrupt handlers
  101 + static inline void handle_interrupt();
  102 +};
  103 +
  104 +// Arduino 0012 workaround
  105 +#undef int
  106 +#undef char
  107 +#undef long
  108 +#undef byte
  109 +#undef float
  110 +#undef abs
  111 +#undef round
  112 +
  113 +#endif
21 libraries/SoftwareSerial/examples/SoftwareSerialExample/SoftwareSerialExample.pde
... ... @@ -0,0 +1,21 @@
  1 +#include <SoftwareSerial.h>
  2 +
  3 +SoftwareSerial mySerial(2, 3);
  4 +
  5 +void setup()
  6 +{
  7 + Serial.begin(57600);
  8 + Serial.println("Goodnight moon!");
  9 +
  10 + // set the data rate for the SoftwareSerial port
  11 + mySerial.begin(4800);
  12 + mySerial.println("Hello, world?");
  13 +}
  14 +
  15 +void loop() // run over and over
  16 +{
  17 + if (mySerial.available())
  18 + Serial.print((char)mySerial.read());
  19 + if (Serial.available())
  20 + mySerial.print((char)Serial.read());
  21 +}
50 libraries/SoftwareSerial/examples/TwoPortRXExample/TwoPortRXExample.pde
... ... @@ -0,0 +1,50 @@
  1 +#include <SoftwareSerial.h>
  2 +
  3 +SoftwareSerial ss(2, 3);
  4 +SoftwareSerial ss2(4, 5);
  5 +
  6 +/* This sample shows how to correctly process received data
  7 + on two different "soft" serial ports. Here we listen on
  8 + the first port (ss) until we receive a '?' character. Then
  9 + we begin listening on the other soft port.
  10 +*/
  11 +
  12 +void setup()
  13 +{
  14 + // Start the HW serial port
  15 + Serial.begin(57600);
  16 +
  17 + // Start each soft serial port
  18 + ss.begin(4800);
  19 + ss2.begin(4800);
  20 +
  21 + // By default, the most recently "begun" port is listening.
  22 + // We want to listen on ss, so let's explicitly select it.
  23 + ss.listen();
  24 +
  25 + // Simply wait for a ? character to come down the pipe
  26 + Serial.println("Data from the first port: ");
  27 + char c = 0;
  28 + do
  29 + if (ss.available())
  30 + {
  31 + c = (char)ss.read();
  32 + Serial.print(c);
  33 + }
  34 + while (c != '?');
  35 +
  36 + // Now listen on the second port
  37 + ss2.listen();
  38 +
  39 + Serial.println("Data from the second port: ");
  40 +}
  41 +
  42 +void loop()
  43 +{
  44 + if (ss2.available())
  45 + {
  46 + char c = (char)ss2.read();
  47 + Serial.print(c);
  48 + }
  49 +}
  50 +
69 libraries/SoftwareSerial/icrmacros.h
... ... @@ -0,0 +1,69 @@
  1 +/*
  2 +icrmacros.h
  3 +
  4 +A place to put useful ICR (interrupt change register) macros
  5 +
  6 +If you want to support non-Arduino processors you can extend or replace
  7 +this file.
  8 +
  9 +This library is free software; you can redistribute it and/or
  10 +modify it under the terms of the GNU Lesser General Public
  11 +License as published by the Free Software Foundation; either
  12 +version 2.1 of the License, or (at your option) any later version.
  13 +
  14 +This library is distributed in the hope that it will be useful,
  15 +but WITHOUT ANY WARRANTY; without even the implied warranty of
  16 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17 +Lesser General Public License for more details.
  18 +
  19 +You should have received a copy of the GNU Lesser General Public
  20 +License along with this library; if not, write to the Free Software
  21 +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  22 +
  23 +The latest version of this library can always be found at
  24 +http://arduiniana.org.
  25 +*/
  26 +
  27 +// Abstractions for maximum portability between processors
  28 +// These are macros to associate pins to pin change interrupts
  29 +#if !defined(digitalPinToPCICR) // Courtesy Paul Stoffregen
  30 +
  31 +#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__)
  32 +
  33 +#define digitalPinToPCICR(p) (((p) >= 0 && (p) <= 21) ? (&PCICR) : ((uint8_t *)0))
  34 +#define digitalPinToPCICRbit(p) (((p) <= 7) ? 2 : (((p) <= 13) ? 0 : 1))
  35 +#define digitalPinToPCMSK(p) (((p) <= 7) ? (&PCMSK2) : (((p) <= 13) ? (&PCMSK0) : (((p) <= 21) ? (&PCMSK1) : ((uint8_t *)0))))
  36 +#define digitalPinToPCMSKbit(p) (((p) <= 7) ? (p) : (((p) <= 13) ? ((p) - 8) : ((p) - 14)))
  37 +
  38 +#elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  39 +// Specifically for the Arduino Mega 2560 (or 1280 on the original Arduino Mega)
  40 +// A majority of the pins are NOT PCINTs, SO BE WARNED (i.e. you cannot use them as receive pins)
  41 +// Only pins available for RECEIVE (TRANSMIT can be on any pin):
  42 +// (I've deliberately left out pin mapping to the Hardware USARTs - seems senseless to me)
  43 +// Pins: 10, 11, 12, 13, 50, 51, 52, 53, 62, 63, 64, 65, 66, 67, 68, 69
  44 +
  45 +#define digitalPinToPCICR(p) ( (((p) >= 10) && ((p) <= 13)) || \
  46 + (((p) >= 50) && ((p) <= 53)) || \
  47 + (((p) >= 62) && ((p) <= 69)) ? (&PCICR) : ((uint8_t *)0) )
  48 +
  49 +#define digitalPinToPCICRbit(p) ( (((p) >= 10) && ((p) <= 13)) || (((p) >= 50) && ((p) <= 53)) ? 0 : \
  50 + ( (((p) >= 62) && ((p) <= 69)) ? 2 : \
  51 + 0 ) )
  52 +
  53 +#define digitalPinToPCMSK(p) ( (((p) >= 10) && ((p) <= 13)) || (((p) >= 50) && ((p) <= 53)) ? (&PCMSK0) : \
  54 + ( (((p) >= 62) && ((p) <= 69)) ? (&PCMSK2) : \
  55 + ((uint8_t *)0) ) )
  56 +
  57 +#define digitalPinToPCMSKbit(p) ( (((p) >= 10) && ((p) <= 13)) ? ((p) - 6) : \
  58 + ( ((p) == 50) ? 3 : \
  59 + ( ((p) == 51) ? 2 : \
  60 + ( ((p) == 52) ? 1 : \
  61 + ( ((p) ==53) ? 0 : \
  62 + ( (((p) >= 62) && ((p) <= 69)) ? ((p) - 62) : \
  63 + 0 ) ) ) ) ) )
  64 +
  65 +#else
  66 +#error This processor is not supported by SoftwareSerial
  67 +#endif
  68 +#endif
  69 +
29 libraries/SoftwareSerial/keywords.txt
... ... @@ -0,0 +1,29 @@
  1 +#######################################
  2 +# Syntax Coloring Map for NewSoftSerial
  3 +#######################################
  4 +
  5 +#######################################
  6 +# Datatypes (KEYWORD1)
  7 +#######################################
  8 +
  9 +NewSoftSerial KEYWORD1
  10 +
  11 +#######################################
  12 +# Methods and Functions (KEYWORD2)
  13 +#######################################
  14 +
  15 +begin KEYWORD2
  16 +end KEYWORD2
  17 +read KEYWORD2
  18 +available KEYWORD2
  19 +is_listening KEYWORD2
  20 +overflow KEYWORD2
  21 +library_version KEYWORD2
  22 +enable_timer0 KEYWORD2
  23 +flush KEYWORD2
  24 +listen KEYWORD2
  25 +
  26 +#######################################
  27 +# Constants (LITERAL1)
  28 +#######################################
  29 +

0 comments on commit 80bb16d

Please sign in to comment.
Something went wrong with that request. Please try again.