Page 1 of 1

REPL does not stop boot.py

Posted: Sun Dec 29, 2019 3:00 am
by smhodge
I have code running on a board (a pyboard but I don't think that's relevant) that simply uses a timer with a callback to flash a led. Works fine, starts running (because boot.py invokes it) every time I reset the board. However, if I start a REPL session by using Ctrl-C while that code is running, the code keeps on running and the led keeps flashing. I would have thought the REPL would have stopped any code running on the board and then cleared the code out of the board's RAM, ready to have new code entered, but it appears that is not happening, or that boot.py is being invoked again. How can the two sets of code run concurrently? What do I need to do to prevent this? Thanks.

Re: REPL does not stop boot.py

Posted: Mon Dec 30, 2019 4:31 pm
by dhylands
No. Entering the REPL doesn't clear out memory etc. Which is a good thing because it allows you to investigate things when your program crashes.

If you do a Soft Reset (i.e. Control-D) once you get into the REPL, then your program will be cleared out of memory.

You could also have your code use a try/catch to grab the KeyboardInterrupt and do a clean shutdown. Pressing Control-C will generate a KeyboardInterrupt exception.

Re: REPL does not stop boot.py

Posted: Mon Dec 30, 2019 5:06 pm
by smhodge
OK, I see that's a good thing about the REPL, but if I use Ctrl-D when in the REPL it reboots the program. Here is the REPL session, with comments added:

KeyboardInterrupt:
MicroPython v1.11 on 2019-05-29; PYBv1.1 with STM32F405RG
Type "help()" for more information.
# test to see if in REPL
>>> 1+11
12
>>>
# Ctrl-D here
MPY: sync filesystems
MPY: soft reboot
Enter main loop <-- issued by my program
...leds flashing

Re: REPL does not stop boot.py

Posted: Tue Dec 31, 2019 11:23 pm
by dhylands
It turns out that if you do a Control-D from the raw REPL then main.py won't get executed.

So if you move your code to main.py instead of boot.py then you can do:

Control-A Control-D Control-B

and get into the REPL without main.py being executed. During development, I often use a jumper or pushbutton to indicate "run" mode and have main.py check the jumper and just exit to the REPL based on the jumper being present/absent

Re: REPL does not stop boot.py

Posted: Wed Jan 01, 2020 1:05 am
by smhodge
Thanks. I just have to remember "cadbee": Ctrl-C, Ctrl-A, Ctrl-D, Ctrl-B does it. I like your jumper technique; I'll give it a try.

Re: REPL does not stop boot.py

Posted: Wed Jan 01, 2020 5:31 am
by pythoncoder
My approach is not to touch main.py until my application is debugged. Say the application is in my_app.py - it can be tested by issuing

Code: Select all

import my_app
at the REPL, interrupted with ctrl-c and cleared out of RAM with ctrl-d. Once it's tested and ready to deploy, main.py simply requires the above one-liner for it to auto-run.