Losing the REPL when an IRQ is triggered

Questions and discussion about The WiPy 1.0 board and CC3200 boards.
Target audience: Users with a WiPy 1.0 or CC3200 board.
jgmdavies
Posts: 57
Joined: Tue Aug 09, 2016 2:39 pm

Re: Losing the REPL when an IRQ is triggered

Post by jgmdavies » Tue Aug 16, 2016 10:52 am

@holdenweb I get these same issues with timer irqs and the REPL (usually via station-mode WiFi in my case).

I've tried several variations, including different timers, 16 bit and 32 bit, and irq handlers (always very simple, and fine when called directly).

Does anyone know if there's a fundamental issue with timer irqs and the REPL please?

I'm running MicroPython v1.8.3-17-g2196799 on 2016-08-15; WiPy with CC3200, and I got the same with v1.8.2.

Thanks,
Jim

holdenweb
Posts: 9
Joined: Wed Mar 09, 2016 12:40 pm

Re: Losing the REPL when an IRQ is triggered

Post by holdenweb » Sat Aug 20, 2016 3:07 pm

Thanks for the input. I haven't made any progress on this, so I am hoping someone with the ability to debug the implementation will be able to explain what blocks the REPL.

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

Re: Losing the REPL when an IRQ is triggered

Post by pythoncoder » Sun Aug 21, 2016 4:12 pm

Is it reasonable to expect code to run 'in the background' concurrently with the REPL? Perhaps Dave (@dhylands) can comment. Every application I've written with interrupts has a main loop which runs while the interrupts are handled and exchanges data with the interrupt handlers. When the program terminates (whether by design, by ctrl-C or by throwing an exception) the REPL returns.

At this point the object whose method is the interrupt handler will be out of scope: the handler must stop running at this point. After all, an object whose method is an interrupt handler could go out of scope in the course of program execution whereupon its presence in RAM could be garbage collected. MicroPython must handle this auto-magically.

So the 'normal' state of affairs is (presumably) that when the REPL is present interrupt handlers cease being called.
Peter Hinch
Index to my micropython libraries.

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

Re: Losing the REPL when an IRQ is triggered

Post by dhylands » Sun Aug 21, 2016 6:05 pm

Interrupt will keep running when you return to the REPL, up until the time you hit Control-D.

The objects associated with the interrupt handlers will still be "in scope" and will be consuming memory from the heap, since they can't be GC'd (the timer still has a pointer to the callback routine).

On the pyboard, for example, you can do something like this:

Code: Select all

pyb.Timer(2, freq=4, callback=lambda t: print('.', end=''))
and you'll see dots printed while you're at the REPL. You can still use the REPL:

Code: Select all

>>> pyb.Timer(2, freq=4, callback=lambda t: print('.', end=''))
Timer(2, freq=4, prescaler=0, period=10499999, mode=UP, div=1)
>>> .........p.r.int....(..'..t.his. i.s .a ..t.es.t..'..).
this is a test
>>> ..........................................
I'm not familiar enough with the WiPy to know whether having the interrupts still running when you get to the REPL will be problematic or not.

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

Re: Losing the REPL when an IRQ is triggered

Post by pythoncoder » Mon Aug 22, 2016 10:53 am

Interesting! Thank you. I suspect that, depending on the application, that feature could bite back - I'd naively assumed that ctrl-C stopped everything in its tracks. Do you think I should update the "Writing interrupt handlers" doc?
Peter Hinch
Index to my micropython libraries.

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

Re: Losing the REPL when an IRQ is triggered

Post by dhylands » Mon Aug 22, 2016 4:16 pm

It's probably worth mentioning.

For interrupts which are self-contained (like a LED flasher or my dot-printer example) the interrupt will just happily continue.

For interrupts which generate data and need somebody to consume that data (or the inverse), you could have interesting issues depending on how the interrupt handler is coded.

tallyboy91
Posts: 7
Joined: Sun Aug 28, 2016 9:21 pm

Re: Losing the REPL when an IRQ is triggered

Post by tallyboy91 » Wed Aug 31, 2016 2:58 am

Has anyone definitively gotten this to work on the WiPy?

I'm using the expansion board with this code:

Code: Select all

from machine import Pin

def pincb(pin):
    print("callback")
    return

pin_int = Pin('GP17', Pin.IN, Pin.PULL_UP)
pin_int.irq(trigger=Pin.IRQ_FALLING, handler=pincb)
What's weird is that about 1 out of every 4 times, the above doesn't lock up the REPL/Telnet/FTP on the first button push. The second button push does lock REPL/Telnet/FTP up.

I can still see "callback" whenever I push the button but no serial REPL, Telnet and FTP access.

I'm still fiddling with various permutations but so far no luck.

Thanks,

aw

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

Re: Losing the REPL when an IRQ is triggered

Post by dhylands » Wed Aug 31, 2016 4:16 am

Have you tried toggling an LED instead of printing something. It's possible that printing from inside the IRQ is what's causing the problem.

tallyboy91
Posts: 7
Joined: Sun Aug 28, 2016 9:21 pm

Re: Losing the REPL when an IRQ is triggered

Post by tallyboy91 » Wed Aug 31, 2016 2:39 pm

This is exhibiting the same behavior:

Code: Select all

from machine import Pin

led=Pin('GP16', Pin.OUT)
pin_int = Pin('GP17', Pin.IN, Pin.PULL_UP)

def pincb(pin):
    global led
    led.toggle()
    

pin_int.irq(trigger=Pin.IRQ_FALLING, handler=pincb)
I also tried a return in pincb but that did not work either.

Thanks,

aw

danielm
Posts: 167
Joined: Mon Oct 05, 2015 12:24 pm

Re: Losing the REPL when an IRQ is triggered

Post by danielm » Thu Sep 08, 2016 8:33 pm

Are there any news regarding this topic?

Post Reply