Author Topic: RFM69, Arduino m0, and Interrupts  (Read 1612 times)

damien8181

  • Newbie
  • *
  • Posts: 1
  • Country: jp
RFM69, Arduino m0, and Interrupts
« on: July 09, 2016, 12:53:58 AM »
Hi all,

I have been trying an RFM69 project using an Arduino Uno and an Arduino m0 pro.  I am using the github library code provided by LowPowerLab at https://github.com/LowPowerLab/RFM69 and the tester code provided by Adafruit at https://learn.adafruit.com/adafruit-feather-m0-radio-with-rfm69-packet-radio/using-the-radio.  My immediate goal is to create a c++ object that has a RFM69 object plus some extra data.  I was able to get the m0 receiving messages from the Uno, where the Uno is using a modified version of the Adafruit code (i.e. it was set to work on the Uno).  The m0 was correctly receiving the messages, printing them out, and blinking when tested.  I used this same code to test with a c++ object, the code is as follows:

Code: [Select]
#ifndef TESTER_h
#define TESTER_h


#include <RFM69.h> 
#include <stdlib.h>
#include <Arduino.h>
#include <unistd.h>


// Constants
#ifdef _WIN32 || _WIN64
   //define something for Windows
   void delay(int theDelay){
     Sleep(theDelayTime);
   }
#endif

#ifdef __linux__
  void delay(int theDelayTime){
    usleep(theDelayTime);
  }
#endif

#define IS_RFM69HCW    false // set to 'true' if you are using an RFM69HCW module
#define RFM69_CS      8
#define RFM69_IRQ     3
#define RFM69_IRQN    3  // Pin 3 is IRQ 3!
#define RFM69_RST     4

#define FREQUENCY    RF69_315MHZ

#define ENCRYPTKEY    "sampleEncryptKey"
#define PROMISCUOUS_MODE false

#define DEFAULT_NET_ID 100
#define DEFAULT_NODE_ID 2

/*
 * Tester Class
 *
 *
 *
 */
class Tester {
  private:
 
    int myNetID;
    int myNodeID;
   
    RFM69 myRadio;

  public:
  //Constructors
    Tester(){
      myNetID = DEFAULT_NET_ID;
      myNodeID = DEFAULT_NODE_ID;
    }
       
    ~Tester(){
      //Nothing to delete     
    }

    void initTester(){     
      myRadio.initialize(FREQUENCY,myNodeID,myNetID);
      myRadio.setPowerLevel(31);
      myRadio.encrypt(ENCRYPTKEY);
      myRadio.promiscuous(PROMISCUOUS_MODE);
    }
       
    char *getData(){     
      if (myRadio.receiveDone()){
return (char*)myRadio.DATA;
      }
      myRadio.receiveDone();
      Serial.flush();
      return (char*) "No Message";
    }
   
    void blinkIt(){
      Serial.println("Blinking");     
      //check if received message contains Hello World
      if (strstr((char *)myRadio.DATA, "Hello World"))
      {
        //check if sender wanted an ACK
        if (myRadio.ACKRequested()){
          myRadio.sendACK();
          Serial.println(" - ACK sent");
        }
      } 
      Blink(13, 40, 3); //blink LED 3 times, 40ms between blinks
      Serial.println("Blinked");
    }
   
    void flushIt(){     
      myRadio.receiveDone(); //put radio in RX mode
      Serial.flush();
    }

    void Blink(byte PIN, byte DELAY_MS, byte loops)
    {
      for (byte i=0; i<loops; i++)
      {
        digitalWrite(PIN,HIGH);
        delay(DELAY_MS);
        digitalWrite(PIN,LOW);
        delay(DELAY_MS);
      }
    }
  };
#endif

The m0 test code I used is as follows: 

Code: [Select]
#include <Tester.h>

#define SERIAL_BAUD   115200
#define LED           13  // onboard blinky

Tester test1;



void Blink(byte PIN, byte DELAY_MS, byte loops){
  for (byte i=0; i<loops; i++)
  {
    Serial.println("Blinky.");
    digitalWrite(PIN,HIGH);
    Serial.println("Blinky1.");
    delay(DELAY_MS);
    Serial.println("Blinky2.");
    digitalWrite(PIN,LOW);
    Serial.println("Blinky3.");   
    delay(DELAY_MS);
    Serial.println("Blinky4.");
  }
  Serial.println("Outside the Blink Loop.");
}



void setup() {
  // put your setup code here, to run once:

  pinMode(LED, OUTPUT);

  /**************\
  *  UART Setup  *
  \**************/ 
  while (!Serial); // wait until serial console is open
  Serial.begin(SERIAL_BAUD);
  delay(100);
  pinMode(LED, OUTPUT);
  test1.initTester();
  Serial.println("Initialized tester."); 
}

void loop() {
  // put your main code here, to run repeatedly:
  char* theData = test1.getData();
  if(strcmp(theData, "No Message")){
    Serial.println(theData);
  }
  //test1.blinkIt();
  //test1.flushIt(); 
  Blink(LED, 50, 3);
  //Serial.println("Blinked?");
  test1.flushIt();
}

This code works if the Blink code is removed; however, any time the Blink code is allowed to run, the m0 freezes.  I am not sure exactly why this is happening, but I am inclined to think that it has something to do with timer interrupts within the m0.  Is it possible that the RFM69 has disabled interrupts while the delay function's timer interrupt fires, and so the interrupt is ignored?  If so, is there a way this can be fixed?
Any help is appreciated.