Reboot in timer

azrirhmn
Posts: 1
Joined: Tue Aug 11, 2020 7:30 am

Reboot in timer

Postby azrirhmn » Tue Aug 11, 2020 7:35 am

Hello everyone,
I need some help regarding the timer in ESP32.
When I activated the timer3 and read a value from a digital sensor, the ESP32 reboot by itself everytime.
Below is the part of the code.

Code: Select all

#include <setjmp.h>
#include <actuatorsESP32.h>
#include <PISO.h>

#define divPin 17
PISO p;
jmp_buf env;
actuatorsESP32 a(env);

hw_timer_t * timer3 = NULL;
portMUX_TYPE timerMux3 = portMUX_INITIALIZER_UNLOCKED;

volatile int count1_Sec;                               // Seconds counter of timer 1
volatile int count1_Min;                               // minutes counter of timer 1
volatile int count3_Sec;                               // Seconds counter of timer
volatile boolean stateDiverterValve;
int counter_start;
int timeDiverter;

/*timer 3 did not worked. ESP32 reboots by itself everytime after try to read value of "a.diverterDirPin"*/
void IRAM_ATTR onTimer3()
{
  portENTER_CRITICAL_ISR(&timerMux3);
  volatile bool actualDir, pastDir;
  bool door, cancel;
  if (count3_Sec != timeDiverter) {                           // verifies if the amout of time set was achieved
    count3_Sec++;
  }
  else {
    count3_Sec = 0;
    pastDir = LOW;//digitalRead(a.diverterDirPin);
    Serial.print("First direction -> ");
    Serial.println(pastDir);
    a.diverterMotor_on();
    do {
      actualDir = LOW;//digitalRead(a.diverterDirPin);
      Serial.print("Actual direction -> ");
      Serial.println(actualDir);
    } while (pastDir == actualDir);
    a.diverterMotor_off();
  }
  portEXIT_CRITICAL_ISR(&timerMux3);
}

void diverterValve_on(void) {
  Serial.println("The diverter interruption was set");
  timerAlarmEnable(timer3);
  stateDiverterValve = HIGH;
}

void diverterValve_off() {
  timerAlarmDisable(timer3);
  Serial.println("The diverter interruption was disabled");
  stateDiverterValve = LOW;
}

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.println("SETUP --> START");
  pinMode(divPin, INPUT);
  
  timer3 = timerBegin(2, 80, true);  // timer 2, MWDT clock period = 12.5 ns * TIMGn_Tx_WDT_CLK_PRESCALE -> 12.5 ns * 80 -> 1000 ns = 1 us, countUp
  timerAttachInterrupt(timer3, &onTimer3, true); // edge (not level) triggered
  timerAlarmWrite(timer3, 1000000, true); // 1000000 * 1 us = 1s, autoreload true

void loop() {
    a.relayOn_on();
    delay(100);
    a.cyclage_on();
    delay(500);
    timeDiverter = 10;
    diverterValve_on();
    delay(20000);
    diverterValve_off();
  vTaskDelay(portMAX_DELAY);
}
Thank you !

ESP_Sprite
Posts: 8922
Joined: Thu Nov 26, 2015 4:08 am

Re: Reboot in timer

Postby ESP_Sprite » Tue Aug 11, 2020 11:13 am

I don't think you can put a Serial.print function in an ISR...

lbernstone
Posts: 637
Joined: Mon Jul 22, 2019 3:20 pm

Re: Reboot in timer

Postby lbernstone » Tue Aug 11, 2020 3:19 pm

Nor should you interact with hardware in an interrupt. ISRs need to run quickly, so the system can deal with the next interrupt. Set a flag variable, and do your data processing in the main loop.

tonbor
Posts: 2
Joined: Tue Sep 01, 2020 9:26 am

Re: Reboot in timer

Postby tonbor » Tue Sep 01, 2020 10:32 am

Got the same problem, how did you solve it?

Who is online

Users browsing this forum: nnangeroni and 36 guests