Locked out of my code or: How can I reasonably use the watchdog?

RP2040 based microcontroller boards running MicroPython.
Target audience: MicroPython users with an RP2040 boards.
This does not include conventional Linux-based Raspberry Pi boards.
Post Reply
User avatar
jcf
Posts: 60
Joined: Wed Mar 03, 2021 11:14 am

Locked out of my code or: How can I reasonably use the watchdog?

Post by jcf » Tue Jul 19, 2022 10:25 am

I had an unexpected problem when using the watchdog with a short watchdog interval.
The program had a loop that did something every second.
Now when I wanted to edit the program, I didn't succeed because the Pico booted over and over again and gave me no time to press Stop in Thonny and load my file to edit it.
Fortunately I could stop this by interrupting the I2C hardware and thus provoking an error.

I found a similar discussion already in the ESP part of the forum, concerning WEBREPL.
The outcome was that one should use longer watchdog intervals.

For the moment I'm using this:

Code: Select all

watchdog_time = 5000
def main():
    wdt = WDT(timeout = watchdog_time) 
    #...
    
    while True:
        wdt.feed()
        
        # emergency stop:
        if stop_switch.value() == 0:
            break
        
        #...
    
        time.sleep(1)

main()        
This way I am able to stop the program by keyboard interrupt or by stop button.
There is only one caveit: After the program is interrupted correctly, the Pico boots once again, loses the connection to Thonny and shows the Micropython prompt after pushing the Stop/Restart button in Thonny.
For me the reason is that the watchdog is still running until reboot.

It would be fine if the watchdog could be deinitialized (I have read on pro and contra in the other forum topic), or is there another elegant way out of this?

TheSilverBullet
Posts: 50
Joined: Thu Jul 07, 2022 7:40 am

Re: Locked out of my code or: How can I reasonably use the watchdog?

Post by TheSilverBullet » Tue Jul 19, 2022 11:25 am

Maybe the first lines in your startup-code should look like:

Code: Select all

import sys
probe = machine.Pin(16,machine.Pin.IN,machine.Pin.PULL_UP)
if not probe.value():
	sys.exit()
probe = machine.Pin(16,machine.Pin.IN,None)
del probe

# … your code here …

If you temporarily shorten Pin16 to ground (a 1kΩ resistor not only for the faint-hearted) , the code will stop at sys.exit()

User avatar
jcf
Posts: 60
Joined: Wed Mar 03, 2021 11:14 am

Re: Locked out of my code or: How can I reasonably use the watchdog?

Post by jcf » Tue Jul 19, 2022 4:36 pm

Interesting idea.
wouldd this bring something new for me?

Eventually I could add a

Code: Select all

sys.exit()
into my stop button code?
No, I tried this, the watchdog reboots the system anyway.

So you are right, I have to do it at the beginning, before even starting the watchdog.

TheSilverBullet
Posts: 50
Joined: Thu Jul 07, 2022 7:40 am

Re: Locked out of my code or: How can I reasonably use the watchdog?

Post by TheSilverBullet » Tue Jul 19, 2022 4:40 pm

jcf wrote:
Tue Jul 19, 2022 4:36 pm
I have to do it at the beginning, before even starting the watchdog.
That's the idea. Once the watchdog has been started, that's it.
It can't be stopped, because … well …that's the whole point of having one.

User avatar
jcf
Posts: 60
Joined: Wed Mar 03, 2021 11:14 am

Re: Locked out of my code or: How can I reasonably use the watchdog?

Post by jcf » Tue Jul 19, 2022 4:54 pm

Yes.

I have put it like this now:

Code: Select all

import sys
watchdog_time = 5000

def main():
    
    # to avoid problems with the watchdog: start it only if stop switch not set
    # otherwise directly stop everything
    # this allows editing files from the Pico without trouble
    if stop_switch.value() == 0:
        sys.exit()
    wdt = WDT(timeout = watchdog_time) 
    #...
The "problem" of rebooting when a running program is stopped still remains, but that is not a big deal.

TheSilverBullet
Posts: 50
Joined: Thu Jul 07, 2022 7:40 am

Re: Locked out of my code or: How can I reasonably use the watchdog?

Post by TheSilverBullet » Tue Jul 19, 2022 5:06 pm

and where do you tell the system that there IS a stop_switch?
Somewhere before main()?

User avatar
jcf
Posts: 60
Joined: Wed Mar 03, 2021 11:14 am

Re: Locked out of my code or: How can I reasonably use the watchdog?

Post by jcf » Thu Jul 21, 2022 8:53 am

Yes, clearly, I define it at the beginning of the script:

Code: Select all

# Imports
# ...
# Switches:
d_switch = Pin(14, Pin.IN, Pin.PULL_UP)
stop_switch = Pin(15, Pin.IN, Pin.PULL_UP)
# other hardware
# ...
# functions 
#...

def check_stopswitch():
    # to avoid problems with the watchdog: start it only if stop switch not set
    # otherwise directly stop everything
    # this allows editing files from the Pico without trouble
    if stop_switch.value() == 0:
        oled.clear()
        oled.print("SYSTEM STOPPED")
        print("SYSTEM STOPPED")
        sys.exit()

# -----------------------------------------------------------------------------------------

def main():
    
    check_stopswitch()
    wdt = WDT(timeout = watchdog_time) 
    
   
    
    while True:
        wdt.feed()
        
        check_stopswitch()
       # do my stuff ...
        time.sleep(1)

main()        

The stop switch is checked at the beginning ( in STOP position: no watchdog & end program) and in the loop (if STOP: terminate program).

samneggs
Posts: 20
Joined: Tue Jun 15, 2021 12:57 am

Re: Locked out of my code or: How can I reasonably use the watchdog?

Post by samneggs » Fri Jul 22, 2022 3:41 am

You can use core-1 to count down a global variable and if it gets to zero do a processor reset.
Then on your main routine in core-0 set the variable to a preset value.
This doesn’t work if core-1 crashes but if the code is small and simple it’ll be safe.

Post Reply