Page 1 of 1

Minimal MicroPython Init/Deinit For Scripts

Posted: Tue Apr 26, 2022 6:22 pm
by Loxy618
Hi!
I'm a little new at this so hoping someone can provide some guidance. I'm trying to run MicroPython as a static library on a small iot device that has no console access. While the device isn't executing a script its running an application doing whatever else it needs to do. However, the iot device can download a string (the script) at anytime based user's configuration and/or behavior.

When the iot device downloads a script, I want to be able to pass that script to MP to run. What is the minimal setup that MP needs to do to be able to run this script and then shut down? I don't have any concern about doing this every time a script needs to run but want to make sure I'm init-ing and deinit-ing everything properly. Thanks

Code: Select all

mp_stack_set_limit(...) 
gc_init(...)
mp_init()
do_str("for i in range(10):\r\n  x=i", MP_PARSE_FILE_INPUT); #this would be my script
mp_deinit()

Re: Minimal MicroPython Init/Deinit For Scripts

Posted: Wed Apr 27, 2022 7:08 am
by stijn
What you show is about right, but it also depends on build options and how garbage collection is implemented exactly, and so on. So it's not really possible to give one correct answer here. E.g. mp_stack_set_limit() is only needed if you have MICROPY_STACK_CHECK. And you don't have mp_stack_ctrl_init() or mp_stack_set_top() but that might be because your GC implementation doesn't need it?

Best thing to do probably is copying the relevant parts from main.c of the MicroPython port from which you're deriving yours.