ESP32 timer interrupt problem [SOLVED]

Any bugs you encounter with Flowcode should be discussed here.
Post Reply
Sasi
Posts: 107
http://meble-kuchenne.info.pl
Joined: Wed Dec 02, 2020 12:11 pm
Has thanked: 37 times
Been thanked: 14 times

Flowcode v10 ESP32 timer interrupt problem [SOLVED]

Post by Sasi »

Hello,

I would like to ask for help.
Now I'm stuck on timer interrupts. :(
According to my intention, the attached program would produce a 500Hz square signal on the Pin17 output.
Instead, Pin17 will go low level for 700us every 480ms.
In fact, the MCU restarts every 480ms because the pin12 pin also behaves the same way.
Timer interrupt test.fcfx
(10.38 KiB) Downloaded 694 times
Regards,
Sasi
Last edited by Sasi on Tue May 28, 2024 7:17 pm, edited 1 time in total.

medelec35
Matrix Staff
Posts: 1955
Joined: Wed Dec 02, 2020 11:07 pm
Has thanked: 626 times
Been thanked: 656 times

Re: ESP32 timer interrupt problem

Post by medelec35 »

Hello.
have you tried setting the pin directly from within the interrupt, instead of the variable value?
Also before the main loop, set a pin hight for say 2 seconds then low again, with an LED attached to it.
That way you can tell if the chip is resetting due to the watchdog timer.
Martin

Sasi
Posts: 107
Joined: Wed Dec 02, 2020 12:11 pm
Has thanked: 37 times
Been thanked: 14 times

Re: ESP32 timer interrupt problem

Post by Sasi »

Hi Martin,

Yes, I tried setting port 17 directly in the ISR, the result is the same. In the test program, I used port 12 to demonstrate MCU restart. It goes low level when the program starts, and goes high level in the main loop. It can only go low lewel if the MCU reboots for some reason. However, this pin of the port behaves the same as 17.

Sasi

mnfisher
Valued Contributor
Posts: 1514
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 138 times
Been thanked: 725 times

Re: ESP32 timer interrupt problem

Post by mnfisher »

As the code stands - the wdt will cause a reboot repeatedly. Your main loop needs to have a delay in it to allow the (co-operative) multi-tasking to work (and there are at least 5 tasks running created by the RTOS)

One option might be to create a separate task (with xTaskCreate) -

(In pseudocode)

.led = false
Loop
.led = !.led
set pin .led
delay 2ms
end loop

But I don't think that a 2ms second delay is long enough - so this technique would work for slower pulse rates but not 500Hz...
So - plan B is to do the job in the timer ISR and toggle the pin every time the ISR is called.

ISR:
$PIN17 = !$PIN17

Note that this isn't 'guaranteed' to give exact times (it relies on the multi-tasking and tasks 'yielding') but I've found this to be pretty good!

Plan C is to either use the SPI or RMT hardware to do the job - perfect if the timing needs to be 'just so' - but rather more involved to write :-(


Martin

Sasi
Posts: 107
Joined: Wed Dec 02, 2020 12:11 pm
Has thanked: 37 times
Been thanked: 14 times

Re: ESP32 timer interrupt problem

Post by Sasi »

Hi Martin,

Thank you for your reply and suggestions.
According to them, I have to think differently when using ESP32 than with Microchip processors. (this is my first more complex project with this MCU)
So now I understand why Martin (medelec35) suggested flashing the LED in the main loop.
I'm doing a bit of reading on the subject now. :)

Regards,
Sasi

mnfisher
Valued Contributor
Posts: 1514
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 138 times
Been thanked: 725 times

Re: ESP32 timer interrupt problem [SOLVED]

Post by mnfisher »

It is an amazingly powerful chip - with a lot of features. It does take a slightly different 'mindset' to MCUs that run a single program though..

I did a very simple SQW example using a timer. Pulses are 2.00 or 2.01ms according to my scope. However the chip is doing nothing except looping on a delay. The pin to toggle is in properties (Pin1) - I used A22.
sqw.fcfx
(9.01 KiB) Downloaded 711 times
Martin

Sasi
Posts: 107
Joined: Wed Dec 02, 2020 12:11 pm
Has thanked: 37 times
Been thanked: 14 times

Re: ESP32 timer interrupt problem [SOLVED]

Post by Sasi »

Thank you very much for your help.
I tried the program you provided. Interrupt timing accuracy will be on target. :D
Regards,
Sasi

mnfisher
Valued Contributor
Posts: 1514
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 138 times
Been thanked: 725 times

Re: ESP32 timer interrupt problem [SOLVED]

Post by mnfisher »

Cool - glad it's working for you...

Post Reply