How to execute a frozen_module when my Nucleo32L432KC boots up?

Discussion and questions about boards that can run MicroPython but don't have a dedicated forum.
Target audience: Everyone interested in running MicroPython on other hardware.
Post Reply
gbergero
Posts: 6
Joined: Fri Apr 12, 2019 11:36 am

How to execute a frozen_module when my Nucleo32L432KC boots up?

Post by gbergero » Thu Apr 18, 2019 8:25 pm

Hi,
im using a Nucleo32l432kc flashed in Micropython and I would like to execute a python script whenever the device boots up.
Problem is, there's no file system so I can't save any code in "main.py" or "boot.py" like you would do on other Nucleo boards. The only way i found to put python code permanently on my Nucleo is to rebuild my firmware with a frozen module ("testModule.py") which worked, but its not very convenient since I have to type

Code: Select all

import testModule
testModule.test()
everytime I reboot the device.
What I want to do is have my Nucleo execute a specific frozen module everytime it boots up. The frozen module would essentially act like "boot.py" but it wouldn't be editable without rebuilding the firmware. I already tryed modifying the main.c file
like in this thread viewtopic.php?f=2&t=3067 but it didn't work.
If anyone can help me achieve this I would really appreciate it.
Thank you
Gbergero

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

Re: How to execute a frozen_module when my Nucleo32L432KC boots up?

Post by dhylands » Thu Apr 18, 2019 11:15 pm

What was your code? and what exactly didn't work?

I modified my main.c so that it would execute a frozen main.py.

Here's what I just did:
1 - created a modules-l432 directory and put main.py and testModule.py in it.

main.py looks like:

Code: Select all

print('Executing modules-432/main.py')

import testModule
testModule.test()
and testModule.py contains:

Code: Select all

def test():
    print('Executing frozen testModule.test()')
I modified main.c:

Code: Select all

531 >git diff main.c
diff --git a/ports/stm32/main.c b/ports/stm32/main.c
index 8c2d7d529..e7eb0c8e8 100644
--- a/ports/stm32/main.c
+++ b/ports/stm32/main.c
@@ -733,6 +733,9 @@ soft_reset:
 
     // Run the main script from the current directory.
     if ((reset_mode == 1 || reset_mode == 3) && pyexec_mode_kind == PYEXEC_MODE_FRIENDLY_REPL) {
+#if 1
+        pyexec_frozen_module("main.py");
+#else
         const char *main_py;
         if (MP_STATE_PORT(pyb_config_main) == MP_OBJ_NULL) {
             main_py = "main.py";
@@ -749,6 +752,7 @@ soft_reset:
                 flash_error(3);
             }
         }
+#endif
     }
 
     // Main script is finished, so now go into REPL mode.
and executed:

Code: Select all

make BOARD=NUCLEO_L432KC clean
make BOARD=NUCLEO_L432KC FROZEN_MPY_DIR=modules-l432
make BOARD=NUCLEO_L432KC FROZEN_MPY_DIR=modules-l432 deploy-stlink
and from the REPL if I do Control-D I see this:

Code: Select all

MPY: soft reboot
Executing modules-l432/main.py
Executing frozen testModule.test()
MicroPython v1.10-290-g8402c26cf-dirty on 2019-04-18; NUCLEO-L432KC with STM32L432KC
Type "help()" for more information.
>>> 

gbergero
Posts: 6
Joined: Fri Apr 12, 2019 11:36 am

Re: How to execute a frozen_module when my Nucleo32L432KC boots up?

Post by gbergero » Fri Apr 19, 2019 12:49 pm

Hi Dave,
Thank alot for you response, i tryed it and it worked!

Code: Select all

Type "help()" for more information.
>>> Executing modules-432/main.py
Executing frozen testModule.test()
MicroPython v1.10-284-ga6e5846ba-dirty on 2019-04-19; NUCLEO-L432KC with STM32L432KC
Type "help()" for more information.
>>>
Here's what my main.c file looks like :

Code: Select all

 // Run the main script from the current directory.
    if ((reset_mode == 1 || reset_mode == 3) && pyexec_mode_kind == PYEXEC_MODE_FRIENDLY_REPL) {
        const char *main_py;
        if(1) {
            pyexec_frozen_module("main.py");
	    main_py = "main.py";
        } else {
            main_py = mp_obj_str_get_str(MP_STATE_PORT(pyb_config_main));
        }
        mp_import_stat_t stat = mp_import_stat(main_py);
        if (stat == MP_IMPORT_STAT_FILE) {
            int ret = pyexec_file(main_py);
            if (ret & PYEXEC_FORCED_EXIT) {
                goto soft_reset_exit;
            }
            if (!ret) {
                flash_error(3);
            }
        }
    }
It looks alot like yours but i had to modify a few lines to get it working.

Post Reply