Frozen Main.by Boot.py

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
SureshVakati
Posts: 30
Joined: Fri Feb 24, 2017 3:52 pm

Frozen Main.by Boot.py

Postby SureshVakati » Fri Feb 24, 2017 11:22 pm

Hello,
I am working with STM32F405 board, I tried putting main.py Boot.py and some modules in a directory 'stmhal/scripts' and built micropython for my board using make Board=<boardname> FROZEN_DIR=<my dir> , I can import modules but my boot.py and main.py are not running on start up, Do I have to change anything in stmhal/main.c??

I see these lines from stmhal/main.c

/ reset config variables; they should be set by boot.py
MP_STATE_PORT(pyb_config_main) = MP_OBJ_NULL;

// run boot.py, if it exists
// TODO perhaps have pyb.reboot([bootpy]) function to soft-reboot and execute custom boot.py
if (reset_mode == 1 || reset_mode == 3) {
const char *boot_py = "boot.py";
FRESULT res = f_stat(boot_py, NULL);
if (res == FR_OK) {
int ret = pyexec_file(boot_py);
if (ret & PYEXEC_FORCED_EXIT) {
goto soft_reset_exit;
}
if (!ret) {
flash_error(4);
}
}
}

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

Re: Frozen Main.by Boot.py

Postby dhylands » Sat Feb 25, 2017 12:20 am

I think that main.py and boot.py currently need to come from the filesystem.

To execute boot.py as a frozen file, you'd need to do:

Code: Select all

pyexec_frozen_module("boot.py")
and similar for main.py.

That's what I do on the teensy:
https://github.com/dhylands/micropython ... #L206-L212

I then make boot.py look like:
https://github.com/dhylands/micropython ... ts/boot.py
so it checks to see if boot.py exists on the sdcard and if it does, then it executes that.

SureshVakati
Posts: 30
Joined: Fri Feb 24, 2017 3:52 pm

Re: Frozen Main.by Boot.py

Postby SureshVakati » Sat Feb 25, 2017 1:03 am

Code from teensy looks different than stmhal/main.c

This is the code from stmhal/main.c to run boot.py

Code: Select all

// reset config variables; they should be set by boot.py
    MP_STATE_PORT(pyb_config_main) = MP_OBJ_NULL;

    // run boot.py, if it exists
    // TODO perhaps have pyb.reboot([bootpy]) function to soft-reboot and execute custom boot.py
    if (reset_mode == 1 || reset_mode == 3) {
        const char *boot_py = "boot.py";
        FRESULT res = f_stat(boot_py, NULL);
        if (res == FR_OK) {
            int ret = pyexec_file(boot_py);
            if (ret & PYEXEC_FORCED_EXIT) {
                goto soft_reset_exit;
            }
            if (!ret) {
                flash_error(4);
            }
        }
    }



This is from teensy.py

Code: Select all

#if MICROPY_MODULE_FROZEN
    pyexec_frozen_module("boot.py");
#else
    if (!pyexec_file("/boot.py")) {
        flash_error(4);
    }
#endif


So, shall I just replace that above lines with these ?

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

Re: Frozen Main.by Boot.py

Postby dhylands » Sat Feb 25, 2017 1:23 am

The teensy doesn't have an internal filesystem, so it doesn't have to do any filesystem initialization (and also doesn't have a reset_mode), which is why they look a bit different.

They both call pyexec_file to execute boot.py

SureshVakati
Posts: 30
Joined: Fri Feb 24, 2017 3:52 pm

Re: Frozen Main.by Boot.py

Postby SureshVakati » Sat Feb 25, 2017 2:33 am

Yeah, My question is if I replace "int ret = pyexec_file(boot_py);" from \stmhal\main.c to int ret =pyexec_frozen_module("boot.py") and rebuild my firmware, then I don't need to have default "boot.py" in SD card right? If SD card is present, Micropython is not creating those default files (main.py, boot.py) (I checked in \flash, \SD) upon hard reset.

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

Re: Frozen Main.by Boot.py

Postby dhylands » Sat Feb 25, 2017 4:26 am

The internal flash filesystem is initialized whether an sdcard is used or not:
https://github.com/micropython/micropyt ... ain.c#L560

If you're going to use frozen main.py then you should probably replace this whole block:
https://github.com/micropython/micropyt ... #L645-L663
not just the call to pyexec_file, but it depends on the exact behaviour you want.

If you just replaced the pyexec_file call that it would only call the frozen main.py if main.py exists in /flash due to the call to mp_import_stat

SureshVakati
Posts: 30
Joined: Fri Feb 24, 2017 3:52 pm

Re: Frozen Main.by Boot.py

Postby SureshVakati » Sat Feb 25, 2017 5:44 am

Code: Select all

MicroPython v1.8.6 on 2016-11-15; Terminus2-405 with STM32F405
Type "help()" for more information.
>>> import os
>>> os.getcwd()
'/sd'
>>> os.listdir()
['System Volume Information']
>>> os.chdir('/flash')
>>> os.listdir()
['main.py', 'pybcdc.inf', 'README.txt', 'boot.py', 'System Volume Information']

When I do hard-reset or watch dog timer resets the board, the default working directory is going back to '/sd' and as there are no boot.py and main.py, system simply stays idle. I remember it was not like this before. Previously, when I insert SD card, it was showing all my scripts at /flash + SD space as a one file system on my windows machine or at least it was copying all files to SD card, Now, I can't see those scripts from /flash when I connect to my PC unless I unmount SD cad. I don't know what did I change in configuration file.

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

Re: Frozen Main.by Boot.py

Postby dhylands » Sat Feb 25, 2017 6:09 am

It's never done that.

If the SD card is inserted, then the sd card is shared with the windows machine, otherwise the internal filesystem is shared with the windows machine.

The internal filesystem is still available when the sdcard is inserted. You can do os.listdir('/flash') and os.listdir('/sd') while the sdcard is inserted.

If an sdcard is inserted, then it will ignore the internal filesystem and boot from the sdcard. A commit was made a couple weeks ago:
https://github.com/micropython/micropyt ... 956de77b41
and if the file /flash/SKIPSD exists then it will boot from /flash even when an sdcard is inserted.

SureshVakati
Posts: 30
Joined: Fri Feb 24, 2017 3:52 pm

Re: Frozen Main.by Boot.py

Postby SureshVakati » Sat Feb 25, 2017 6:22 am

Thank you Dave! Thanks for spending your time and helping me!

SureshVakati
Posts: 30
Joined: Fri Feb 24, 2017 3:52 pm

Re: Frozen Main.by Boot.py

Postby SureshVakati » Wed Mar 08, 2017 3:53 pm

Hello Dave,
Could you correct me if I am wrong on this?
What I am trying to achieve is, My STM32F4 should run frozen boot.py and frozen main.py if 'boot.py' and 'main.py' are not present on SD card, doesn't matter, if 'boot.py' 'main.py' present in '/flash' or not.

Code: Select all

import pyb
try:
    import os
    os.stat('/sd/boot.py')
    execfile('/sd/boot.py')
except:
    print("Executing frozen boot.py")         
    pyb.usb_mode('VCP+HID')
    #pyb.main("Frozen module name.py") here?


My frozen main.py code

Code: Select all


try:   
        import os
        os.stat('/sd/main.py')
        execfile('/sd/main.py')
except:
        import pyb
        print("Executing frozen main.py")       
        import time,pyb,gc       
        if pyb.SD.present():
           while(1):
              #Do Something....
        else:
           print("This project needs an SD card...")          



Now, if I replace "int ret = pyexec_file(boot_py);" from \stmhal\main.c to int ret =pyexec_frozen_module("boot.py")
https://github.com/micropython/micropyt ... ain.c#L592
same as above for main.py as well
https://github.com/micropython/micropyt ... ain.c#L655

Then can I achieve what I am trying to do? If not, could you provide me an example code of stmhal/main.c for boot.py?
Thank you!


Return to “General Discussion and Questions”

Who is online

Users browsing this forum: No registered users and 1 guest