Pi Pico - Arduino IDE| Timer seems to freeze the board

Hello !

I'm currently working on a sensor monitoring project, using a Raspeberry Pi Pico. A part of his project consists in sending a HTTP request, and then the microcontroller starts a periodic measure on the sensor. The request is handled in the program by a struct type "Command", which contains all the parameters that can be added to the request (especially the measuring interval).

The starting of the measure and the measure are handled in a class named "behavior". When the measure needs to be done, a timer is set to the period previously specified in the HTTP request. The callback parameter of the timer is a static bool function declared in the same file as the 'behavior' class.

The timer library I use is designed for the Pi Pico : https://github.com/khoih-prog/RPI_PICO_TimerInterrupt

The main file checks at every loop for the network state then for a new request that would be then sent to the behavior instance.

The timer's debug shows that it starts correctly, however, the callback fucntion is never called, and after 1sec the board is no longer running the program, i cannot even close the serial monitor.

The code is down below, I cropped a lot of it to only keep the part I'm talking about and to be easier to read.

Main file :

 
#include "ISnetwork.h"
#include "behavior.h"

ISnetwork network;
behavior behavior;

void setup() {
  pinMode(15, OUTPUT);
  network.networkSetup();    //Hosting the server
  behavior.sensorSetup();    
}

void loop() {


  while ( network.networkCheck() < 0) { }   //Network state check 

Serial.println("loop");           

  behavior.behaviorHandler(network.queryAK(), network.userQuery);  //This is checking for a request then sending it

    
  if (network.userQuery.connected()) {   //End of connection if it hasn't been done 
    network.endQuery();
  }
}

behavior.h

#ifndef behavior_H
#define behavior_H

#include <Arduino.h>

#include <Ethernet.h>
#include "Command.h"



#define TIMER_INTERRUPT_DEBUG         1
#define _TIMERINTERRUPT_LOGLEVEL_     4
#include "RPi_Pico_TimerInterrupt.h"

#define LOCAL_DEBUG               1

class behavior  {


  public:
    static bool measure(struct repeating_timer *t);

    struct Command activeBehavior, lastBehavior, activeCommand;
    EthernetClient activeQuery;

    
    void sensorSetup();
    void behaviorHandler(struct Command activeCommand, EthernetClient activeQuery);
   
    void regularMeasure();

};

#endif

behavior.cpp

#include "behavior.h"

RPI_PICO_Timer ITimer0(0);

bool behavior::measure(struct repeating_timer *t) {
  
  Serial.println("Measuring requested");

  return true;
}



void behavior::regularMeasure() {

  RPI_PICO_Timer ITimer0(0);
  Serial.println("REGULAR MEASURE");
  lastBehavior = activeCommand;
  activeBehavior = activeCommand;
  
  if (activeCommand.readingPeriod * 1000 >= 1e6 && activeCommand.readingPeriod * 1000 <= 3600e6) {

    Serial.println("Initialisation timer");
    if (ITimer0.attachInterruptInterval(activeCommand.readingPeriod * 1000, behavior::measure)) {
      Serial.println("Timer Start OK");
      ITimer0.enableTimer();

    } else {
      Serial.println("Failed to start timer");
    }
  }
}



void behavior::behaviorHandler(struct Command command, EthernetClient query) {

  activeCommand = command;
  activeQuery = query;

  switch (activeCommand.mode) { 
    case 1:
      {
        Serial.print(">Selected mode: ");
        regularMeasure();
        break;
      }

  }

}

When i send a request for a measure to be done once a second, the serial monitor shows especially

[TISR] _timerNo = 0, Clock (Hz) = 1000000.00, _fre (Hz) = 1.00
[TISR] _count = 0-1001000
[TISR] add_repeating_timer_us = 1001000
Timer Start OK

Then it shows the "loop" debug line I added in the loop function of the main file for 1 second before freezing.

However it never shows "Measuring Requested", the callback function is never called.

EDIT: Actually, the board is freezing when the interruption is activated. If I set the timer to a 10 secs interval, it will freeze 9-10 secs later.

I thought of replacing my behavior class by a behavior namespace since there is only one instance of it that will be used, and I have read that Timers don't work with multiple instances, do you think it would solve the issue ?

Do you have an idea of what is causing this ? By the way if you need more informations feel free to ask.

Thanks !

NB: The example code "TimerInterruptTest" provided by the library is running fine.

Seems like I have found the solution !
ITimer0 must be declared outside any class .

behavior.h

#ifndef behavior_H
#define behavior_H

#include <Arduino.h>

#include <Ethernet.h>
#include "Command.h"



#define TIMER_INTERRUPT_DEBUG         1
#define _TIMERINTERRUPT_LOGLEVEL_     4
#include "RPi_Pico_TimerInterrupt.h"

#define LOCAL_DEBUG               1

class behavior  {


  public:
    static bool measure(struct repeating_timer *t);

    struct Command activeBehavior, lastBehavior, activeCommand;
    EthernetClient activeQuery;

    
    void sensorSetup();
    void behaviorHandler(struct Command activeCommand, EthernetClient activeQuery);
   
    void regularMeasure();

};

static RPI_PICO_Timer ITimer(0);

#endif

behavior.cpp

#include "behavior.h"


bool behavior::measure(struct repeating_timer *t) {
  
  Serial.println("Measuring requested");

  return true;
}



void behavior::regularMeasure() {


  Serial.println("REGULAR MEASURE");
  lastBehavior = activeCommand;
  activeBehavior = activeCommand;
  
  if (activeCommand.readingPeriod * 1000 >= 1e6 && activeCommand.readingPeriod * 1000 <= 3600e6) {

    Serial.println("Initialisation timer");
    if (ITimer0.attachInterruptInterval(activeCommand.readingPeriod * 1000, behavior::measure)) {
      Serial.println("Timer Start OK");


    } else {
      Serial.println("Failed to start timer");
    }
  }
}



void behavior::behaviorHandler(struct Command command, EthernetClient query) {

  activeCommand = command;
  activeQuery = query;

  switch (activeCommand.mode) { 
    case 1:
      {
        Serial.print(">Selected mode: ");
        regularMeasure();
        break;
      }

  }

}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.