Interrupt handling on Teensy 4

ninja2

Well-known member
The basic sketch below is for one of my Adafruit RFM95W Lora modules that I am trying to control with Teensy 4.1
It relies on this Lora library by Sandeep Mistry that is available via the IDE library manager.

The sketch compiles with warnings but doesn't work yet.

The warnings are about invalid conversion from 'int' to 'IRQ_NUMBER_t' but I couldn't fnd the data type for IRQ_NUMBER_t ??

I've not had to think about interrupts on Teensy before. Are they handled under same principles as general Arduino boards ?

Here are the compile warnings:
Code:
In file included from /home/cj/.arduino15/packages/teensy/hardware/avr/1.59.0/cores/teensy4/wiring.h:39,
                 from /home/cj/.arduino15/packages/teensy/hardware/avr/1.59.0/cores/teensy4/WProgram.h:46,
                 from /home/cj/.cache/arduino/sketches/1995C1DB9FDB144AA3AD17A324F4D31A/pch/Arduino.h:6,
                 from /home/cj/ARDUINO/SKETCHES/libraries/LoRa/src/LoRa.h:9,
                 from /home/cj/ARDUINO/SKETCHES/libraries/LoRa/src/LoRa.cpp:6:
/home/cj/ARDUINO/SKETCHES/libraries/LoRa/src/LoRa.cpp: In member function 'void LoRaClass::onReceive(void (*)(int))':
/home/cj/.arduino15/packages/teensy/hardware/avr/1.59.0/cores/teensy4/core_pins.h:1784:59: warning: invalid conversion from 'int' to 'IRQ_NUMBER_t' [-fpermissive]
 1784 | #define digitalPinToInterrupt(p)  ((p) < NUM_DIGITAL_PINS ? (p) : -1)
      |                                   ~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~
      |                                                           |
      |                                                           int
/home/cj/ARDUINO/SKETCHES/libraries/LoRa/src/LoRa.cpp:377:27: note: in expansion of macro 'digitalPinToInterrupt'
  377 |     SPI.notUsingInterrupt(digitalPinToInterrupt(_dio0));
      |                           ^~~~~~~~~~~~~~~~~~~~~
In file included from /home/cj/ARDUINO/SKETCHES/libraries/LoRa/src/LoRa.h:10,
                 from /home/cj/ARDUINO/SKETCHES/libraries/LoRa/src/LoRa.cpp:6:
/home/cj/.arduino15/packages/teensy/hardware/avr/1.59.0/libraries/SPI/SPI.h:1171:45: note:   initializing argument 1 of 'void SPIClass::notUsingInterrupt(IRQ_NUMBER_t)'
 1171 |         void notUsingInterrupt(IRQ_NUMBER_t interruptName);
      |                                ~~~~~~~~~~~~~^~~~~~~~~~~~~
In file included from /home/cj/.arduino15/packages/teensy/hardware/avr/1.59.0/cores/teensy4/wiring.h:39,
                 from /home/cj/.arduino15/packages/teensy/hardware/avr/1.59.0/cores/teensy4/WProgram.h:46,
                 from /home/cj/.cache/arduino/sketches/1995C1DB9FDB144AA3AD17A324F4D31A/pch/Arduino.h:6,
                 from /home/cj/ARDUINO/SKETCHES/libraries/LoRa/src/LoRa.h:9,
                 from /home/cj/ARDUINO/SKETCHES/libraries/LoRa/src/LoRa.cpp:6:
/home/cj/ARDUINO/SKETCHES/libraries/LoRa/src/LoRa.cpp: In member function 'void LoRaClass::onTxDone(void (*)())':
/home/cj/.arduino15/packages/teensy/hardware/avr/1.59.0/cores/teensy4/core_pins.h:1784:59: warning: invalid conversion from 'int' to 'IRQ_NUMBER_t' [-fpermissive]
 1784 | #define digitalPinToInterrupt(p)  ((p) < NUM_DIGITAL_PINS ? (p) : -1)
      |                                   ~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~
      |                                                           |
      |                                                           int
/home/cj/ARDUINO/SKETCHES/libraries/LoRa/src/LoRa.cpp:395:27: note: in expansion of macro 'digitalPinToInterrupt'
  395 |     SPI.notUsingInterrupt(digitalPinToInterrupt(_dio0));
      |                           ^~~~~~~~~~~~~~~~~~~~~
In file included from /home/cj/ARDUINO/SKETCHES/libraries/LoRa/src/LoRa.h:10,
                 from /home/cj/ARDUINO/SKETCHES/libraries/LoRa/src/LoRa.cpp:6:
/home/cj/.arduino15/packages/teensy/hardware/avr/1.59.0/libraries/SPI/SPI.h:1171:45: note:   initializing argument 1 of 'void SPIClass::notUsingInterrupt(IRQ_NUMBER_t)'
 1171 |         void notUsingInterrupt(IRQ_NUMBER_t interruptName);
      |                                ~~~~~~~~~~~~~^~~~~~~~~~~~~
Memory Usage on Teensy 4.1:
  FLASH: code:14288, data:4040, headers:8292   free for files:8099844
   RAM1: variables:4896, code:11592, padding:21176   free for local variables:486624
   RAM2: variables:12416  free for malloc/new:511872

here is the code:
Code:
#include <Streaming.h>
#include <SPI.h>
#include <LoRa.h>

void setup() {
  Serial.begin(115200);
  while (!Serial && millis() < 1000);
  Serial << "\n\n==== LoraSender.b ====\n";

  Serial << "* Start LoRa radio: ";
  if (!LoRa.begin(915E6)) {
    Serial << "FAILED program *HALT");
    while (1);
  }
  Serial << "OK\n";
}

int counter = 0;

void loop() {
  Serial << "Sending packet: " << counter << '\n';
  LoRa.beginPacket();
  LoRa.print("hello ");
  LoRa.print(counter);
  LoRa.endPacket();
  counter++;
  delay(5000);
}

I have also modified the Lora.h in my libraries to recognise Teensy SPI1 pins as shown below. DIO0_PIN is the interrupt pin:
Code:
#ifdef __IMXRT1062__                                      // setup for SPI1 on T4.*
#define LORA_DEFAULT_SPI           SPI1
#define LORA_DEFAULT_SPI_FREQUENCY 8E6
#define LORA_DEFAULT_SS_PIN        38
#define LORA_DEFAULT_RESET_PIN     2
#define LORA_DEFAULT_DIO0_PIN      3
#elif defined(ARDUINO_SAMD_MKRWAN1300)
#define LORA_DEFAULT_SPI           SPI1
#define LORA_DEFAULT_SPI_FREQUENCY 200000
#define LORA_DEFAULT_SS_PIN        LORA_IRQ_DUMB
#define LORA_DEFAULT_RESET_PIN     -1
#define LORA_DEFAULT_DIO0_PIN      -1
#elif defined(ARDUINO_SAMD_MKRWAN1310)
#define LORA_DEFAULT_SPI           SPI1
#define LORA_DEFAULT_SPI_FREQUENCY 200000
#define LORA_DEFAULT_SS_PIN        LORA_IRQ_DUMB
#define LORA_DEFAULT_RESET_PIN     -1
#define LORA_DEFAULT_DIO0_PIN      LORA_IRQ
#else
#define LORA_DEFAULT_SPI           SPI
#define LORA_DEFAULT_SPI_FREQUENCY 8E6
#define LORA_DEFAULT_SS_PIN        10
#define LORA_DEFAULT_RESET_PIN     9
#define LORA_DEFAULT_DIO0_PIN      2
#endif
 
Last edited:
Back
Top