Can I enable/disable IRQs?

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
maxlock
Posts: 4
Joined: Sat Sep 17, 2016 7:07 pm

Can I enable/disable IRQs?

Post by maxlock » Sat Sep 17, 2016 7:18 pm

Hi All,
I've got an ESP12 running esp8266-20160917-v1.8.4-16-gb9672bc.bin (latest nightly) and I'm struggling to disable an IRQ to allow me to debounce a connected switch. Here what I'm seeing....

=== from machine import Pin
=== import esp
=== esp.osdebug(None)
===
=== def handle_tip(p):
=== print('tip')
===
=== bucket = Pin(0, Pin.IN, Pin.PULL_UP)
=== bucket_irq = bucket.irq(trigger=Pin.IRQ_RISING, handler=handle_tip)
===
>>>
>>> bucket_irq
<IRQ>
>>> bucket_irq.disable()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'IRQ' object has no attribute 'disable'
>>> bucket_irq.disable_irq()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'IRQ' object has no attribute 'disable_irq'

Firstly, is the sane, and additionally is this possible right now? I've seen https://github.com/micropython/micropython/pull/2090 but this seems to control all irqs not an individual. If so is something like this do-able?

def handle_tip(p):
self.state = machine.disable_irq()
print('tip')
tim = Timer(-1)
tim.init(period=DEBOUNCE_TIME, mode=Timer.ONE_SHOT, callback=lambda t:machine.enable_irq(self.state))

-Thanks Max.

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

Re: Can I enable/disable IRQs?

Post by pythoncoder » Mon Sep 19, 2016 7:09 am

Disabling interrupts is a little drastic and best done for very brief periods of time. Consider the following approach.

Have a timer running continuously (a virtual timer will be fine) with a period of, say, 50ms. The callback function compares the state of the pin with its previous state before saving its current state. If the states differ, it calls the pin change function. Note that the pin change function is called in an interrupt context with its rules about not using the heap. To avoid this have the function set a flag to be tested in the main program loop.

This approach works for most non-pathological switches. If you're interested in the topic of switch debouncing the following link, supplied by @dhylands, is worth a read http://www.ganssle.com/debouncing.htm.
Peter Hinch
Index to my micropython libraries.

maxlock
Posts: 4
Joined: Sat Sep 17, 2016 7:07 pm

Re: Can I enable/disable IRQs?

Post by maxlock » Mon Sep 19, 2016 7:46 am

Thanks, thats very helpful, I was trying to think of another way of doing it, If I get something going I'll post a follow up here for future reference.

-Cheers Max.

maxlock
Posts: 4
Joined: Sat Sep 17, 2016 7:07 pm

Re: Can I enable/disable IRQs?

Post by maxlock » Mon Sep 19, 2016 7:56 pm

ok, so this seems to do the trick just using a timer.

Code: Select all

def check_switch(p):
  global switch_state
  global switched
  global last_switch_state
  switch_state = switch.value()
  if switch_state != last_switch_state:
    switched = True
  last_switch_state = switch_state

switch = Pin(0, Pin.IN, Pin.PULL_UP)
switch_state = switch.value()
last_switch_state = switch_state
switched = False

tim = Timer(-1)
tim.init(period=50, mode=Timer.PERIODIC, callback=check_switch)

while True:
  if switched:
    if switch_state == 1:
      print('tipped')
    switched = False
  time.sleep_ms(20)
Thanks! :)

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

Re: Can I enable/disable IRQs?

Post by pythoncoder » Tue Sep 20, 2016 6:01 am

That looks good to me ;)
Peter Hinch
Index to my micropython libraries.

Post Reply