Serial Interrupts

ninja2

Well-known member
For many years I've used the NeoHWSerial library by SlashDevin :

"The NeoHWSerial class is a drop-in replacement for the Arduino built-in class HardwareSerial. It adds the capability to handle received characters with a user-defined function during the RX interrupt. This can improve performance significantly by eliminating all processing associated with storing and retrieving characters in the ring buffer and all polling code that constantly checked for new characters: available and read are unnecessary."

This extract compiles and works on a MEGA 2560, but not Teensy of course:

Code:
#include <Streaming.h>
#include <NeoHWSerial.h>            // using NeoHWSerial (instead of HardwareSerial)

// -----------------------------------------------------------------------------------------------
//#define DEBUG_PORT_TYPE NeoHWSerial
#define Serial  NeoSerial           
#define bt      NeoSerial1           // Bluetooth on Serial1
#define bt_baud 38400               // CJ bt modules all set to 38400 (also 38400 is mandatory in AT/config. mode)

void setup() {
  Serial.begin(115200);
  while (!Serial && millis() < 1000);
  Serial << F("------ BTtest.A ------\n");
  initBluetooth();
  Serial << F("----- setup done -----\n");
}

void loop() {
}

void initBluetooth(){
  bt.attachInterrupt(BTisr);
  bt.begin(bt_baud);
  bt << F("*** BT initialised *** \n"); // tell receiver we're awake
}

const byte numChars = 150;
char BTtext[numChars];                                    // array to store BT RX text

volatile uint8_t  idx = 0;
volatile uint32_t lineCount = 0UL;
         uint32_t prevLineCount;

// Interrupt Service Routine - NeoHWSerial on Serial 1
static void BTisr(uint8_t readByte){
  if (readByte != '\n'){
    BTtext[idx] = readByte;
    idx++;
    if (idx >= numChars) idx = numChars - 1;
    }
  else {                                                  // line feed detected
    BTtext[idx] = '\0';                                   // terminate the string
    lineCount++;
    idx = 0;
    }
  }

I'm hoping to create a personal version of this library for Teensy 3.* or 4.*
That's well above my pay grade, so I'm hoping to get some expert guidance :)

Alternately there may be another existing teensy library that does this?

If I ignore / eliminate the copious warnings when I try to compile for teensy, this is the primary error:

Code:
C:\Users\...\Documents\ARDUINO\SKETCHES\BTtest\BTtest.ino: In function 'void setup()':
C:\Users\cj\Documents\ARDUINO\SKETCHES\BTtest\BTtest.ino:6:17: error: 'NeoSerial' was not declared in this scope; did you mean 'NeoHWSerial'?
    6 | #define Serial  NeoSerial
      |                 ^~~~~~~~~
C:\Users\...\Documents\ARDUINO\SKETCHES\BTtest\BTtest.ino:11:3: note: in expansion of macro 'Serial'
   11 |   Serial.begin(115200);
      |   ^~~~~~
C:\Users\...\Documents\ARDUINO\SKETCHES\BTtest\BTtest.ino: In function 'void initBluetooth()':
C:\Users\...\Documents\ARDUINO\SKETCHES\BTtest\BTtest.ino:7:17: error: 'NeoSerial1' was not declared in this scope; did you mean 'Serial1'?
    7 | #define bt      NeoSerial1           // Bluetooth on Serial1
      |                 ^~~~~~~~~~
C:\Users\...\Documents\ARDUINO\SKETCHES\BTtest\BTtest.ino:22:3: note: in expansion of macro 'bt'
   22 |   bt.attachInterrupt(BTisr);
      |   ^~

exit status 1

Compilation error: 'NeoSerial' was not declared in this scope; did you mean 'NeoHWSerial'?

Suggestions welcome ...
 
Looks like that code is for older Arduno AVR boards like UNO and not really applicable to the Teensy code.

There have been a few threads over the years talking about doing similar stuff, like using DMA or the like. You might try searching for
those.

It is a little more complex, as there is one ISR that handles lots of stuff...

Good luck
 
Back
Top