/NeoICSerial

Arduino AltSoftSerial with attachInterrupt for RX chars

Primary LanguageC

The NeoICSerial class is intended as a drop-in replacement for Paul Stoffregen's class AltSoftSerial. It adds the capability to register a function to be called when a new character is received or when all character have been transmitted.

This class can only use one predefined Input Capture pin. Each MCU and board has a pre-determined pin:

Board Transmit Receive PWM Unusable
Teensy 3.0 & 3.1 21 20 22
Teensy 2.0 9 10 (none)
Teensy++ 2.0 25 4 26, 27
Arduino Uno 9 8 10
Arduino Leonardo 5 13 (none)
Arduino Mega 46 48 44, 45
Wiring-S 5 6 4
Sanguino 13 14 12

If the Input Capture pin is not available, you may want to consider NeoHWSerial or NeoSWSerial.

RX character interrupt

To handle received characters with your procedure, you must register it with the NeoICSerial class or your instance:

#include <NeoICSerial.h>
NeoICSerial serial_port;

volatile uint32_t newlines = 0UL;

static void handleRxChar( uint8_t c )
{
  if (c == '\n')
    newlines++;
}

void setup()
{
  serial_port.attachInterrupt( handleRxChar );
  //  OR   NeoICSerial::attachInterrupt( handleRxChar );
  serial_port.begin( 9600 );
}

The registered procedure will be called from the ISR whenever a character is received. The received character will not be stored in the rx_buffer, and it will not be returned from read(). Any characters that were received and buffered before attachInterrupt was called remain in rx_buffer, and could be retrieved by calling read().

If attachInterrupt is never called, or it is passed a NULL procedure, the normal buffering occurs, and all received characters must be obtained by calling read().

TX complete interrupt

To detect when all characters have been transmitted, you must register it with the NeoICSerial class or your instance:

  NeoICSerial serial_port;
  NeoICSerial::attachTxCompleteInterrupt( handleTxComplete );
     //  OR
  serial_port.attachTxCompleteInterrupt( handleTxComplete );

Remember that these registered procedures are called from an interrupt context, and they should return as quickly as possible. Taking too much time in these procedures will cause many unpredictable behaviors, including loss of received data. See the similar warnings for the built-in attachInterrupt for digital pins.

Differences from current AltSoftSerial

Support for Arduino IDE v1.0 was removed.

New methods were added for the user-defined IRS: attachInterrupt/detachInterrupt and attachTxCompleteInterrupt/detachTxCompleteInterrupt.

Methods for compatibilty with other software serial libraries were removed:

	  // for drop-in compatibility with NewSoftSerial, rxPin & txPin ignored
	  AltSoftSerial(uint8_t rxPin, uint8_t txPin, bool inverse = false) { }