Timer Interrupt Priority

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
User avatar
atmuc
Posts: 18
Joined: Fri Nov 13, 2020 11:44 am
Location: Istanbul

Timer Interrupt Priority

Post by atmuc » Sat Nov 14, 2020 10:23 am

I use timer interrupts and an LCD display. In timer interrupt tick I toggle a GPIO pin to create a square wave. After I started the timer, I run an infinite loop to show the counter on the LCD display. The timer period is 100ms, LCD display period is 1 sec.

When I comment LCD display code I get perfect GPIO output as a square wave. When I run LCD print code, There is a few lags on GPIO output for a few pulses. I think I2C communication causes this lag. This lag period is around 5 secs. For the probability of python garbage collection cause this, I will examine the LCD driver code to reduce garbage collection.

How can I make this timer interrupt with higher priority?

User avatar
jimmo
Posts: 2262
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Timer Interrupt Priority

Post by jimmo » Sun Nov 15, 2020 4:04 am

atmuc wrote:
Sat Nov 14, 2020 10:23 am
How can I make this timer interrupt with higher priority?
Unfortunately on ESP32 interrupts can only run as "soft interrupts" (see https://docs.micropython.org/en/latest/ ... rules.html ), so they are scheduled between Python instructions.
atmuc wrote:
Sat Nov 14, 2020 10:23 am
I think I2C communication causes this lag. This lag period is around 5 secs.
I'm surprised it's 5 seconds though. Can you explain what you mean by "LCD display period is 1 sec.".

User avatar
atmuc
Posts: 18
Joined: Fri Nov 13, 2020 11:44 am
Location: Istanbul

Re: Timer Interrupt Priority

Post by atmuc » Sun Nov 15, 2020 6:18 am

I display a counter with 1 sec period.

Code: Select all

 while True:            
            self.displayController.print(self.counter, 0, 0)            
            self.counter += 1
            sleep(1)

User avatar
jimmo
Posts: 2262
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Timer Interrupt Priority

Post by jimmo » Sun Nov 15, 2020 11:15 am

atmuc wrote:
Sun Nov 15, 2020 6:18 am
I display a counter with 1 sec period.
Try using

Code: Select all

for i in range(100):
  time.sleep_ms(10)
instead of

Code: Select all

time.sleep(1)

User avatar
atmuc
Posts: 18
Joined: Fri Nov 13, 2020 11:44 am
Location: Istanbul

Re: Timer Interrupt Priority

Post by atmuc » Sun Nov 15, 2020 12:09 pm

it still has lags

Image

User avatar
jimmo
Posts: 2262
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Timer Interrupt Priority

Post by jimmo » Mon Nov 16, 2020 10:15 am

I don't know how to interpret that picture sorry -- doesn't have any labels.

Did changing that sleep make any difference?

User avatar
atmuc
Posts: 18
Joined: Fri Nov 13, 2020 11:44 am
Location: Istanbul

Re: Timer Interrupt Priority

Post by atmuc » Mon Nov 16, 2020 10:50 am

This is pin output with a timer interrupt. On each timer tick, output toggles. With perfect timing, all pulse width should be the same.

this output is the same before and after sleep change.

User avatar
jimmo
Posts: 2262
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Timer Interrupt Priority

Post by jimmo » Mon Nov 16, 2020 11:40 pm

ESP32 just doesn't have very good support for low-latency operations.

What I suspect is happening here is that the scheduler isn't getting a change to run because of some quirks with the way sleep works on ESP32. Perhaps instead of time.sleep() you could use a loop that runs until a certain amount of time has elapsed (i.e. use time.ticks_ms and time.ticks_diff to measure the time).

User avatar
atmuc
Posts: 18
Joined: Fri Nov 13, 2020 11:44 am
Location: Istanbul

Re: Timer Interrupt Priority

Post by atmuc » Tue Nov 17, 2020 6:48 am

I tried creating a thread and using an infinite loop to handle timing on Raspberry Pi with dotnet core. This system will communicate with a PC and an LCD display via I2C. Because of those operations and the garbage collection loop is not stable. I also tried nanoFramework and native code integration with custom events.

I think the best stable way to use an Arduino with hardware interrupts. I will write C++ on Arduino, Python on ESP32, and C# on PC. Arduino and ESP32 will communicate via I2C, ESP32 and PC will communicate via a serial port.

Post Reply