Page 1 of 1

Interrupt Routine, counting more than sys.maxsize

Posted: Wed Jan 02, 2019 7:06 pm
by ThomasChr
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

Re: Interrupt Routine, counting more than sys.maxsize

Posted: Wed Jan 02, 2019 7:15 pm
by dhylands
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.

Re: Interrupt Routine, counting more than sys.maxsize

Posted: Wed Jan 02, 2019 7:28 pm
by jickster
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

Re: Interrupt Routine, counting more than sys.maxsize

Posted: Wed Jan 02, 2019 7:39 pm
by Roberthh
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

Re: Interrupt Routine, counting more than sys.maxsize

Posted: Wed Jan 02, 2019 7:58 pm
by ThomasChr
In my case I‘m usig a Pin Change Interrupt. Does anyone know what the advantages and disadvantages of a hard ISR are?

Re: Interrupt Routine, counting more than sys.maxsize

Posted: Wed Jan 02, 2019 8:06 pm
by ThomasChr
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

Re: Interrupt Routine, counting more than sys.maxsize

Posted: Wed Jan 02, 2019 8:08 pm
by Roberthh
The response time is faster. There is a long thread here: https://github.com/micropython/micropython/issues/2972

Re: Interrupt Routine, counting more than sys.maxsize

Posted: Thu Jan 03, 2019 10:49 am
by ThomasChr
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.

Re: Interrupt Routine, counting more than sys.maxsize

Posted: Thu Jan 03, 2019 11:46 am
by Roberthh
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.