Page 1 of 1

New buidl with scheduler breaks code

Posted: Mon Mar 20, 2017 7:20 am
by Roberthh
Hello @dpgeorge, the new scheduler code is surely a progress, but unfortunately it breaks old code. Attached is a little test program, which used to run until the new release. I guess that just a little change has to done in the lines, where the IRQ handler is set up (line 27), but w/o documentation that is hard to guess.

Code: Select all

from machine import I2C, Pin, Timer
import ads1x15
from array import array

addr = 72
gain = 1

_BUFFERSIZE = const(512)
#
# Interrupt service routine zum messen
# diese wird vom Timer-interrupt aktiviert
#
def sample_auto(x):
    global index_put, ads, data
    if index_put < _BUFFERSIZE:
        data[index_put] = ads.alert_read()
        index_put += 1

data = array("h", [0] * _BUFFERSIZE)
index_put = 0

i2c = I2C(scl=Pin(5), sda=Pin(4), freq=400000)
irq_pin = Pin(13, Pin.IN, Pin.PULL_UP)
ads = ads1x15.ADS1115(i2c, addr, gain)
ads.conversion_start(0, 5)

irq_pin.irq(trigger=Pin.IRQ_FALLING, handler=sample_auto)

while index_put < _BUFFERSIZE:
    pass

irq_pin.irq(handler=None)
#
# at that point data contains 512 samples acquired at the given rate
#

for _ in range(1, _BUFFERSIZE):
    print(data[_], data[_] - data[_ - 1] )

Re: New build with scheduler breaks code

Posted: Mon Mar 20, 2017 11:52 am
by pythoncoder
It's cattled my code on the ESP8266 too which now fails to respond to pin interrupts. Solutions gratefully received.

I'm assuming some change is required to the line which assigns the handler:

Code: Select all

      pin.irq(handler = self._cb_pin, trigger=(Pin.IRQ_FALLING | Pin.IRQ_RISING))

Re: New buidl with scheduler breaks code

Posted: Mon Mar 20, 2017 12:19 pm
by Roberthh
@pythoncoder: maybe only something 'like hard=True' has to be added in the call:

Code: Select all

 pin.irq(handler = self._cb_pin, trigger=(Pin.IRQ_FALLING | Pin.IRQ_RISING), hard=True)

I'll try this evening.

Re: New buidl with scheduler breaks code

Posted: Mon Mar 20, 2017 12:50 pm
by pythoncoder
No joy. It accepts the arg without complaint but I'm still getting no response :(
I've put a comment and request for guidance here https://github.com/micropython/micropython/pull/2878

Re: New buidl with scheduler breaks code

Posted: Mon Mar 20, 2017 5:55 pm
by Roberthh
@pythoncoder, @damien: If you leave off the keywords and supply the parameters in the order handler, IRQ_mode, hard_value, then it works.
e.g.

Code: Select all

irq_pin.irq(sample_auto, irq_pin.IRQ_FALLING, True)

Re: New buidl with scheduler breaks code

Posted: Tue Mar 21, 2017 4:37 am
by Damien
Thanks guys for testing this new code. I'm really trying to maintain backwards compatibility where possible, while at the same time improving the general state of the port with these features. It's good that you are catching these issues so quickly.

The problem should now be fixed and old code should still run as it did before. The main difference that remains is that IRQs (for Pin and Timer) are now soft by default. If you need hard IRQ (because you want a faster response time) then pass "hard=True" to the Pin.irq function (Timer has no such option, yet).

Hard IRQs are fast but you can't allocate memory.

Soft IRQs (the default) are not as fast as hard IRQs but you can allocate memory.