Frozen Main.by Boot.py
- SureshVakati
- Posts: 42
- Joined: Fri Feb 24, 2017 3:52 pm
Frozen Main.by Boot.py
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);
}
}
}
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);
}
}
}
Re: Frozen Main.by Boot.py
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: 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.
To execute boot.py as a frozen file, you'd need to do:
Code: Select all
pyexec_frozen_module("boot.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: 42
- Joined: Fri Feb 24, 2017 3:52 pm
Re: Frozen Main.by Boot.py
Code from teensy looks different than stmhal/main.c
This is the code from stmhal/main.c to run boot.py
This is from teensy.py
So, shall I just replace that above lines with these ?
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
Re: Frozen Main.by Boot.py
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
They both call pyexec_file to execute boot.py
- SureshVakati
- Posts: 42
- Joined: Fri Feb 24, 2017 3:52 pm
Re: Frozen Main.by Boot.py
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.
Re: Frozen Main.by Boot.py
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
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: 42
- Joined: Fri Feb 24, 2017 3:52 pm
Re: Frozen Main.by Boot.py
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']
Re: Frozen Main.by Boot.py
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.
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: 42
- Joined: Fri Feb 24, 2017 3:52 pm
Re: Frozen Main.by Boot.py
Thank you Dave! Thanks for spending your time and helping me!
- SureshVakati
- Posts: 42
- Joined: Fri Feb 24, 2017 3:52 pm
Re: Frozen Main.by Boot.py
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.
My frozen main.py code
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!
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?
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...")
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!