external interrupts consideration

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
LukeVideo
Posts: 22
Joined: Thu May 09, 2019 3:59 pm

external interrupts consideration

Post by LukeVideo » Tue Jun 11, 2019 8:59 pm

Hy,
I was trying to add a second external interrupt to my code. I thought it might help me get out of the while loop.
So tried to duplicate the way the first external interupt worked
So i added


And i had to comment the machine.disable_irq() and enable machine.enable_irq(state) or i had this terrible message when i triggered the interupt.

Code: Select all


interrupt_counter = 0
total_interrupts_counter = 0
reset_counter_val = 0


def inter_counter(pin):
    global interrupt_counter
    interrupt_counter = interrupt_counter+1


def reset_counter(pin):
    global reset_counter_val
    reset_counter_val = reset_counter_val + 1

def update_led(i):
    zeros = 16 - i
    print(zeros)
    for e in range(zeros):
        clock.value(0)
        data.value(0)
        clock.value(1)
        clock.value(0)
    for e in range(i):
        clock.value(0)
        data.value(1)
        clock.value(1)
        clock.value(0)
    latch.value(0)
    latch.value(1)
    latch.value(0)

    print('did the leds')

    msg = json.dumps({"client_id": CLIENT_ID, "led": i, "event": event})
    sleep(0.1)
    print(msg)
    client.publish(TOPIC, msg)



reed.irq(trigger=Pin.IRQ_RISING, handler=inter_counter)
counter_reset.irq(trigger=Pin.IRQ_FALLING, handler=reset_counter)


while True:
    if interrupt_counter > 0:
        sleep(0.1)
        state = machine.disable_irq()
        interrupt_counter = 0
        machine.enable_irq(state)

        total_interrupts_counter = total_interrupts_counter+1
        print("Interrupt has occurred: " + str(total_interrupts_counter))
        update_led(total_interrupts_counter % 17)

    if reset_counter_val > 1:
        print("reset has occurred: " + str(reset_counter_val))
        sleep(0.1)
        state = machine.disable_irq()
        interrupt_counter = 0
        total_interrupts_counter = 0
        reset_counter_val = 0
        update_led(0)
        machine.enable_irq(state)

Code: Select all

Guru Meditation Error: Core  1 panic'ed (Interrupt wdt timeout on CPU1)
Core 1 register dump:
PC      : 0x40092960  PS      : 0x00060734  A0      : 0x80091ba2  A1      : 0x3ffc12f0  
A2      : 0x3ffe86ac  A3      : 0x3ffba7c0  A4      : 0x00000cbd  A5      : 0x3f420737  
A6      : 0x0000abab  A7      : 0x00000000  A8      : 0x3ffba7c0  A9      : 0x00000018  
A10     : 0x3ffba7c0  A11     : 0x00000018  A12     : 0x00000000  A13     : 0x00000001  
A14     : 0x0000abab  A15     : 0x000001ae  SAR     : 0x00000001  EXCCAUSE: 0x00000006  
EXCVADDR: 0x00000000  LBEG    : 0x4000c2e0  LEND    : 0x4000c2f6  LCOUNT  : 0xffffffff  

Backtrace: 0x40092960:0x3ffc12f0 0x40091b9f:0x3ffc1310 0x40090a6f:0x3ffc1330 0x4011d0b9:0x3ffc1370 0x4010b5e9:0x3ffc1390 0x4011d721:0x3ffc13c0 0x4011dc4d:0x3ffc13e0 0x4010cf48:0x3ffc1420 0x4010d56e:0x3ffc1450 0x4010d676:0x3ffc1470 0x400f2729:0x3ffc1490 0x40189a41:0x3ffc14b0 0x400e6064:0x3ffc14e0 0x400e60ce:0x3ffc1510 0x400dfd6e:0x3ffc1540 0x400dbf71:0x3ffc1570 0x400dbfd9:0x3ffc1590 0x400e91ff:0x3ffc15b0 0x400dfdf0:0x3ffc1650 0x400dbf71:0x3ffc1680 0x400dbfd9:0x3ffc16a0 0x400e91ff:0x3ffc16c0 0x400dfdf0:0x3ffc1760 0x400dbf71:0x3ffc17e0 0x400e9175:0x3ffc1800 0x400dfdf0:0x3ffc18a0 0x400dbf71:0x3ffc1920 0x400dbf9e:0x3ffc1940 0x400f756a:0x3ffc1960 0x400f77cd:0x3ffc1a00 0x400f782b:0x3ffc1a20 0x400effc5:0x3ffc1a40 0x40090179:0x3ffc1a70

Core 0 register dump:
PC      : 0x400911a8  PS      : 0x00060034  A0      : 0x800918aa  A1      : 0x3ffb0920  
A2      : 0x3ffb0f9c  A3      : 0x00060a23  A4      : 0x8009129c  A5      : 0x3ffb0920  
A6      : 0x00000003  A7      : 0x00060023  A8      : 0x0000abab  A9      : 0xb33fffff  
A10     : 0x0000abab  A11     : 0x00060021  A12     : 0x00000001  A13     : 0x00000001  
A14     : 0x0000cdcd  A15     : 0x00060023  SAR     : 0x0000001d  EXCCAUSE: 0x00000006  
EXCVADDR: 0x00000000  LBEG    : 0x00000000  LEND    : 0x00000000  LCOUNT  : 0x00000000  

Backtrace: 0x400911a8:0x3ffb0920 0x400918a7:0x3ffb0940 0x40090247:0x3ffb0960 0x40090069:0x3ffb0980 0x400846e6:0x3ffb0990 0x4018a4ff:0x00000000

Rebooting.
Did I do something obviously wrong ?

After some research i noticed the guilty line was the MQTT call at the end of my update_led function... so i swapped the two last lines.

Code: Select all

machine.enable_irq(state)
update_led(0)
It's better but both interrupts still seem to interfear with each other ans i cant light up all my leds with the first interupt because in triggers the second one that resets i all...

Probably best not to mess with these.

LukeVideo
Posts: 22
Joined: Thu May 09, 2019 3:59 pm

Re: external interrupts consideration

Post by LukeVideo » Wed Jun 12, 2019 9:04 am

So i have modified the code a little to keep only one external

Code: Select all

import machine
import json
from machine import Pin
from time import sleep
from umqtt.simple import MQTTClient
from utils.ConnectWifi import connect


try:
    connect()
except:
    print("no connectWifi")


# shift register
clock = Pin(17, Pin.OUT)
clock.value(0)
data = Pin(12, Pin.OUT)
data.value(0)
latch = Pin(13, Pin.OUT)
latch.value(0)
shift_reset = Pin(23, Pin.OUT)
shift_reset.value(1)

# smoothie bike
reed = Pin(39, Pin.IN, Pin.PULL_DOWN)
counter_reset = Pin(38, Pin.IN, Pin.PULL_DOWN)
level_1 = Pin(36, Pin.IN, Pin.PULL_UP)
level_2 = Pin(37, Pin.IN, Pin.PULL_DOWN)



SERVER = '192.168.1.x'  # MQTT Server Address (Change to IP  of your Pi)

CLIENT_ID = 'Spootnik'
TOPIC = b'diodbox'
event = 'unknown_event'
client = MQTTClient(CLIENT_ID, SERVER)
client.connect()   # Connect to MQTT broker


def on_mess(topic, msg):
    print(topic, msg)
    global event
    event = msg

print(event)
client.set_callback(on_mess)
client.subscribe(topic=b"event")
client.wait_msg()

led = [0]

interrupt_counter = 0
total_interrupts_counter = 0
reset_counter_val = 0


def inter_counter(pin):
    print("lol")
    global interrupt_counter
    interrupt_counter = interrupt_counter+1


def reset_counter(pin):
    
    global reset_counter_val
    reset_counter_val = reset_counter_val + 1

def update_led(i):
    zeros = 16 - i
    print(zeros)
    for e in range(zeros):
        clock.value(0)
        data.value(0)
        clock.value(1)
        clock.value(0)
    for e in range(i):
        clock.value(0)
        data.value(1)
        clock.value(1)
        clock.value(0)
    latch.value(0)
    latch.value(1)
    latch.value(0)

    print('did the leds')

    msg = json.dumps({"client_id": CLIENT_ID, "led": i, "event": event})
    sleep(0.1)
    print(msg)
    client.publish(TOPIC, msg)



reed.irq(trigger=Pin.IRQ_RISING, handler=inter_counter)


while True:

    print(counter_reset.value(), interrupt_counter)
    sleep(0.1)
    # if reset_counter_val > 1:
    if counter_reset.value() == 1:
        sleep(0.1)
        # state2 = machine.disable_irq()
        # interrupt_counter = 0
        # machine.enable_irq(state2)
        # total_interrupts_counter = 0
        reset_counter_val = 0
        print("reset has occurred: " + str(reset_counter_val))
        update_led(0)
    if interrupt_counter > 0:
        sleep(0.1)
        state = machine.disable_irq()
        interrupt_counter = 0
        machine.enable_irq(state)

        total_interrupts_counter = total_interrupts_counter+1
        print("Interrupt has occurred: " + str(total_interrupts_counter))
        update_led(total_interrupts_counter % 17)

But still the reset triggered an interrupt. So i changed the Pin of the reset to the pin 22. And all was good.

SO What is wrong with Pin 39 and 38. According to the pin diagram they are both input only and ADC1_* So are all ADC1 triggered together ???

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

Re: external interrupts consideration

Post by pythoncoder » Wed Jun 12, 2019 4:45 pm

According to my docs for the reference board (ESP32-devkit-C) the two input-only pins associated with ADC1 are GPIO34 and GPIO35. There is no 38 or 39. What board are you using?

I assume you've ensured that the logic 1 level is <=3.3V.
Peter Hinch

LukeVideo
Posts: 22
Joined: Thu May 09, 2019 3:59 pm

Re: external interrupts consideration

Post by LukeVideo » Wed Jun 12, 2019 8:54 pm

Hy Peter,
i have used the 3.3v outputs and added a 330ohm resistor to avoid any damage.
I'm using the heltec lorawan V2 board
https://heltec.org/project/wifi-lora-32/
the pin out diagram
https://github.com/Heltec-Aaron-Lee/WiF ... _32_V2.pdf

So using two ADC1_* pins (39 38) with an interrupt is problematic as both pins trigger the external interrup.

Using the external interupt on pin 22 works like a charm.

Post Reply