Reading/writing timer in interrupt
Posted: Thu Aug 22, 2019 9:17 am
Is there a trick to accessing timers from within an interrupt? I have configured 2 x timer interrupts, and they are running great. Next I want to read the value of a timer... but if I un-comment the line containing timerRead() it crashes with "Guru Meditation Error: Core 1 panic'ed (LoadProhibited)". Code about as simple as I can make it so I'll post the whole thing here. Just getting started with ESP32 but experienced with various other micros, no idea what I'm doing wrong here. Thanks~!
- #include <Arduino.h>
- #define LED 4
- #define DELAYPERIOD 100
- hw_timer_t * timer0 = NULL;
- hw_timer_t * timer1 = NULL;
- portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED;
- void IRAM_ATTR onTimer0(){ // 90us timer interrupt
- static uint64_t foo;
- // foo = timerRead(timer1);
- }
- void IRAM_ATTR onTimer1(){ // 2400us timer interrupt
- }
- void setup() {
- pinMode(LED, OUTPUT);
- timer0 = timerBegin(0, 80, true); // Timer0 config: (timer0, prescaler = 12.5ns*80 = 1us, count up)
- timerAttachInterrupt(timer0, &onTimer0, true); // Timer0 interrupt: (pointer, IRQ function, edge trigger)
- timerAlarmWrite(timer0, 90, true); // Timer0 alarm: (pointer, 90 x 1us = 90us, auto reload)
- timerAlarmEnable(timer0); // Timer0 start
- timer1 = timerBegin(1, 1280, true); // Timer1 config: (timer1, prescaler = 12.5ns*1280 = 16us, count up)
- timerAttachInterrupt(timer1, &onTimer1, true); // Timer1 interrupt: (pointer, IRQ function, edge trigger)
- timerAlarmWrite(timer1, 150, true); // Timer1 alarm: (pointer, 150 x 16us = 2.4ms, auto reload)
- timerAlarmEnable(timer1); // Timer1 start
- }
- void loop() {
- digitalWrite(LED, 1);
- delay(DELAYPERIOD);
- digitalWrite(LED, 0);
- delay(DELAYPERIOD);
- }