Page 1 of 1

Timer Interrupt Priority

Posted: Sat Nov 14, 2020 10:23 am
by atmuc
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?

Re: Timer Interrupt Priority

Posted: Sun Nov 15, 2020 4:04 am
by jimmo
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.".

Re: Timer Interrupt Priority

Posted: Sun Nov 15, 2020 6:18 am
by atmuc
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)

Re: Timer Interrupt Priority

Posted: Sun Nov 15, 2020 11:15 am
by jimmo
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)

Re: Timer Interrupt Priority

Posted: Sun Nov 15, 2020 12:09 pm
by atmuc
it still has lags

Image

Re: Timer Interrupt Priority

Posted: Mon Nov 16, 2020 10:15 am
by jimmo
I don't know how to interpret that picture sorry -- doesn't have any labels.

Did changing that sleep make any difference?

Re: Timer Interrupt Priority

Posted: Mon Nov 16, 2020 10:50 am
by atmuc
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.

Re: Timer Interrupt Priority

Posted: Mon Nov 16, 2020 11:40 pm
by jimmo
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).

Re: Timer Interrupt Priority

Posted: Tue Nov 17, 2020 6:48 am
by atmuc
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.