REPL does not stop boot.py

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
smhodge
Posts: 65
Joined: Tue Jan 22, 2019 2:16 am

REPL does not stop boot.py

Post by smhodge » Sun Dec 29, 2019 3:00 am

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.

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

Re: REPL does not stop boot.py

Post by dhylands » Mon Dec 30, 2019 4:31 pm

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.

smhodge
Posts: 65
Joined: Tue Jan 22, 2019 2:16 am

Re: REPL does not stop boot.py

Post by smhodge » Mon Dec 30, 2019 5:06 pm

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

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

Re: REPL does not stop boot.py

Post by dhylands » Tue Dec 31, 2019 11:23 pm

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

smhodge
Posts: 65
Joined: Tue Jan 22, 2019 2:16 am

Re: REPL does not stop boot.py

Post by smhodge » Wed Jan 01, 2020 1:05 am

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.

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

Re: REPL does not stop boot.py

Post by pythoncoder » Wed Jan 01, 2020 5:31 am

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.
Peter Hinch

Post Reply