New buidl with scheduler breaks code

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

New buidl with scheduler breaks code

Post by Roberthh » Mon Mar 20, 2017 7:20 am

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] )

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: New build with scheduler breaks code

Post by pythoncoder » Mon Mar 20, 2017 11:52 am

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))
Peter Hinch
Index to my micropython libraries.

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

Re: New buidl with scheduler breaks code

Post by Roberthh » Mon Mar 20, 2017 12:19 pm

@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.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: New buidl with scheduler breaks code

Post by pythoncoder » Mon Mar 20, 2017 12:50 pm

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
Peter Hinch
Index to my micropython libraries.

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

Re: New buidl with scheduler breaks code

Post by Roberthh » Mon Mar 20, 2017 5:55 pm

@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)

Damien
Site Admin
Posts: 647
Joined: Mon Dec 09, 2013 5:02 pm

Re: New buidl with scheduler breaks code

Post by Damien » Tue Mar 21, 2017 4:37 am

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.

Post Reply