Timer causing reset?

SoCalPinPlayer
Posts: 3
Joined: Sun Aug 20, 2017 1:35 am

Timer causing reset?

Postby SoCalPinPlayer » Sun Sep 10, 2017 10:08 pm

Hi All

I am trying to get a hardware based timer on the ESP32 Sparkfun Thing working. It seems as soon as I incorporate much in the way of code once the timer has triggered an interrupt the CPU resets. For example in the code below if I comment out the last to println's ("line 3" and "line 4") the timer function works perfectly. But as soon as I un-comment those lines it crashes the CPU after executing the onTimer routine about 9 times.

I have tried timerAlarmDisable and timerAlarmEnable before/after the println's but that has no impact. I know this example is pretty useless but it is the simplest way I have been able to reproduce the problem. In the actual code the onTimer routine calls other functions that in turn have println's as well as code to operate some servos.

My assumption here is that I am having a stack overflow issue but have no idea how to solve this.

Your help is greatly appreciated.
David

------- CODE --------
/*
Repeat timer example
This example shows how to use hardware timer in ESP32. The timer calls onTimer
function.
This example code is in the public domain.
*/

hw_timer_t * timer = NULL;

//portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED;

/* LED pin */
int led = 5;
/* LED state */
volatile byte state = LOW;


void IRAM_ATTR onTimer(){

// It is safe to use digitalRead/Write here if you want to toggle an output
state = !state;
digitalWrite(led, state);
Serial.println("In IRAM");
Serial.println("Line 2");
Serial.println("Line 3");
Serial.println("Line 4");
// turn on the timer


}

void setup() {
Serial.begin(9600);

Serial.println("In the setup routine.....");
pinMode(led, OUTPUT);

// Use 1st timer of 4 (counted from zero).
// Set 80 divider for prescaler (see ESP32 Technical Reference Manual for more
// info).
timer = timerBegin(0, 80, true);

// Attach onTimer function to our timer.
timerAttachInterrupt(timer, &onTimer, true);

// Set alarm to call onTimer function every second (value in microseconds).
// Repeat the alarm (third parameter)
// timerAlarmWrite(timer, 500000, true);
timerAlarmWrite(timer, 20000, true);
// Start an alarm
timerAlarmEnable(timer);
}

void loop() {

}
------ END CODE -------

------ OUTPUT EXAMPLE -----
In IRAM
Line 2
Line 3
Line 4
I⸮R⸮⸮fgRIn the setup routine.....
In IRAM
Line 2
Line 3
Line 4
In IRAM
Line 2
Line 3
Line 4
In IRAM
Line 2
Line 3
Line 4
In IRAM
Line 2
Line 3
Line 4
In IRAM
Line 2
Line 3
Line 4
In IRAM
Line 2
Line 3
Line 4
In IRAM
Line 2
Line 3
Line 4
In IRAM
Line 2
Line 3
Line 4
In IRAM
Line 2
Line 3
Line 4
In IRAM
Line 2
Line 3
Line 4
In IRAM
Line 2
Line 3
Line 4
In IRAM
Line 2
Line 3
Line 4
In IRAM
Line 2
Line 3
Line 4
In IRAM
Line 2
Line 3
Line 4
In IRAM
Line 2
Line 3
Line 4
In IRAM
Line 2
Line 3
Line 4
In IRAM
Line 2
Line 3
Line 4
I⸮R⸮⸮fgRIn the setup routine.....
In IRAM
Line 2
Line 3
Line 4
In IRAM
Line 2
Line 3
Line 4
----- END OUTPUT EXAMPLE ---

User avatar
martinayotte
Posts: 137
Joined: Fri Nov 13, 2015 4:27 pm

Re: Timer causing reset?

Postby martinayotte » Mon Sep 11, 2017 3:17 pm

You cannot use Serial.println() inside an interrupt handler !
Instead, use a global variable which interrupt handler will turn it on, and check this variable in loop() where you can do the prints.

Who is online

Users browsing this forum: Google [Bot], Majestic-12 [Bot], ThomasK and 15 guests