Interrupt Routine, counting more than sys.maxsize

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
ThomasChr
Posts: 113
Joined: Sat Nov 25, 2017 7:50 am

Interrupt Routine, counting more than sys.maxsize

Post by ThomasChr » Wed Jan 02, 2019 7:06 pm

Hello Forum,

I've got an ESP8266 with Micropython which is counting pulses. The pulses are count in an Interrupt Handler. They can be many and eventually they could be more than sys.maxize (2147483647). In the micropython manual I can read that:
MicroPython supports integers of arbitrary precision. Values between 2**30 -1 and -2**30 will be stored in a single machine word. Larger values are stored as Python objects. Consequently changes to long integers cannot be considered atomic. The use of long integers in ISR’s is unsafe because memory allocation may be attempted as the variable’s value changes.
https://docs.micropython.org/en/latest/ ... rules.html

I tried to trigger such event but I'm not able to get a number which is not type int:
>>> a = 4564648646845648648648646
>>> type(a)
<class 'int'>
>>> a = 1
>>> type(a)
<class 'int'>
Even triying to trigger the problem did not work:
import time
import machine

# Test if values above 2147483647 can be set in the interrupt routine
testval = 0

def test(tim):
global testval
print(str(time.ticks_ms()) + ' Enter testval: ' + str(testval))
testval = testval + 1147483647
print(str(time.ticks_ms()) + ' Exit testval: ' + str(testval))

tim = machine.Timer(-1)
tim.init(period = 2 * 1000, mode = machine.Timer.PERIODIC, callback = test)
Can anyone bring some light into this?

Thank you!

Thomas

User avatar
dhylands
Posts: 3195
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Interrupt Routine, counting more than sys.maxsize

Post by dhylands » Wed Jan 02, 2019 7:15 pm

Micropython has the notion of hard ISRs and threaded ISRs. I believe that on the ESP8266 all of the ISRs are threaded. Threaded ISRs don't have the same restrictions that hard ISRs do.

jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

Re: Interrupt Routine, counting more than sys.maxsize

Post by jickster » Wed Jan 02, 2019 7:28 pm

dhylands wrote:Micropython has the notion of hard ISRs and threaded ISRs. I believe that on the ESP8266 all of the ISRs are threaded. Threaded ISRs don't have the same restrictions that hard ISRs do.
Where’s docs for this concept?


Sent from my iPhone using Tapatalk Pro

User avatar
Roberthh
Posts: 1455
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Interrupt Routine, counting more than sys.maxsize

Post by Roberthh » Wed Jan 02, 2019 7:39 pm

It does not seem to be documented, but for ESP8266 there was a discussion, that normally ISR are threaded, but ther is the option rto specify it as hard, with the parameter hard=True in the ISR setting, like:

irq_pin.irq(handler=sample_pin, trigger=Pin.IRQ_FALLING, hard=True)

I do not know whether this applies to all callback handlers that can be specified, but definitely for pin-irq

ThomasChr
Posts: 113
Joined: Sat Nov 25, 2017 7:50 am

Re: Interrupt Routine, counting more than sys.maxsize

Post by ThomasChr » Wed Jan 02, 2019 7:58 pm

In my case I‘m usig a Pin Change Interrupt. Does anyone know what the advantages and disadvantages of a hard ISR are?
Last edited by ThomasChr on Wed Jan 02, 2019 8:06 pm, edited 1 time in total.

ThomasChr
Posts: 113
Joined: Sat Nov 25, 2017 7:50 am

Re: Interrupt Routine, counting more than sys.maxsize

Post by ThomasChr » Wed Jan 02, 2019 8:06 pm

Of course, my code to count the Pulses from a Power Meter is no secret, if it helps: https://github.com/ThomasChr/ESP8266-re ... er/main.py

User avatar
Roberthh
Posts: 1455
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Interrupt Routine, counting more than sys.maxsize

Post by Roberthh » Wed Jan 02, 2019 8:08 pm

The response time is faster. There is a long thread here: https://github.com/micropython/micropython/issues/2972

ThomasChr
Posts: 113
Joined: Sat Nov 25, 2017 7:50 am

Re: Interrupt Routine, counting more than sys.maxsize

Post by ThomasChr » Thu Jan 03, 2019 10:49 am

Thanks a lot. Now I'm seeing much clearer :-)

I think the conclusion for me is that you can't really rely on the Interrupt of the ESP8266 to be very accurate/fast. But for my purpose they are working well enough (A timing of 1 ms would be 360 kW which I will never use in my flat) so I don't need to use some other hardware.

PS: Changing the Frequency to 160 MHz leads do double fired Pin Change Interrupts all 3 Minutes. I'm not sure if thats a hardware problem, but I simply run at 80 MHz and everything works fine.

User avatar
Roberthh
Posts: 1455
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Interrupt Routine, counting more than sys.maxsize

Post by Roberthh » Thu Jan 03, 2019 11:46 am

The double fire pin change interrupts is also something I cam across. This is cause by slow transitions of your input pulse in combination with the noise of the input stage. To avoid that, use a schmitt-trigger gate to condition the input signal.
See here: https://forum.pycom.io/topic/935/unhand ... -handler/6
That was seen on a ESP32, but I repeated that test for a ESP8266 with similar results.

Post Reply