Microcontroller Quitting Mid-Script

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
Loxy618
Posts: 21
Joined: Wed Apr 24, 2019 2:01 am

Microcontroller Quitting Mid-Script

Post by Loxy618 » Wed Apr 24, 2019 2:07 am

I have scripts calling into c-functions and then going back to python specific code. I am trying to handle the case where the script jumps into a c function and incorrectly sets/gets a out of bounds value. At that point, I need to kill the script completely and return up the stack to where the script was initially executed from. This is running on a standalone micro controller with no keyboard or console attached. Suggestions. Thanks!

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

Re: Microcontroller Quitting Mid-Script

Post by dhylands » Wed Apr 24, 2019 4:43 am

rasie an exception which is only caught by the code at the start point.

cduran
Posts: 80
Joined: Thu Mar 17, 2016 4:52 pm

Re: Microcontroller Quitting Mid-Script

Post by cduran » Wed Apr 24, 2019 2:26 pm

I have this same question. Are you suggesting a long jump? Will re-initializing python from within the C function work?

Loxy618
Posts: 21
Joined: Wed Apr 24, 2019 2:01 am

Re: Microcontroller Quitting Mid-Script

Post by Loxy618 » Wed Apr 24, 2019 8:20 pm

I'm looking for something like this in MP

https://pythonextensionpatterns.readthe ... tions.html

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

Re: Microcontroller Quitting Mid-Script

Post by dhylands » Wed Apr 24, 2019 9:55 pm

You can raise an exception from C by using nlr_raise. This will get caught by the python code. nlr_raise is essentially a longjmp.

You can also raise a SystemExit exception which will essentially do a soft reboot.

Post Reply