Is there any way of uploading a .mpy file and execute it?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
ferran.campos
Posts: 5
Joined: Tue Jul 23, 2019 4:45 pm

Is there any way of uploading a .mpy file and execute it?

Post by ferran.campos » Fri Sep 06, 2019 1:34 pm

Hi,

I'm looking for an existing way of uploading a .mpy file dinamically, like uploading the file through the HAL port while micropython is running, get the file and execute it. By this, I'm aiming to upload and execute files without having to reprogram the chip, instead of freezing them into the firmware. Does this code or any similar exist?

I'm currently using a STM32 NUCLEO F446ZE with micropython's minimal port, but if there's any other way of doing this or any other port capable of doing it I would be very grateful to know about them.

Thank you.

kevinkk525
Posts: 969
Joined: Sat Feb 03, 2018 7:02 pm

Re: Is there any way of uploading a .mpy file and execute it?

Post by kevinkk525 » Fri Sep 06, 2019 1:52 pm

On my esp32 I use an ftp server to upload new .mpy files. Then I restart it manually but you could implement something so that the device reboots itself to load the updated files.
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Is there any way of uploading a .mpy file and execute it?

Post by Roberthh » Fri Sep 06, 2019 3:42 pm

Using mpy-cross, you can compile the .py file. If the target has a file system, then you can upload it with any suitable tool, like pyboard.py, ampy, rshell. But there is a difference to frozen bytecode. Frozen bytecode is executed from flash. The bytecode and static data remains in flash. Only dynamic data is in RAM. Uploaded mpy files are loaded to RAM for execution. Therefore they need more RAM. Still, the RAM demand is smaller than loading .py files on the target, since no RAM is required for compiling.

ferran.campos
Posts: 5
Joined: Tue Jul 23, 2019 4:45 pm

Re: Is there any way of uploading a .mpy file and execute it?

Post by ferran.campos » Mon Sep 09, 2019 2:22 pm

Thank you for your responses, I've searching for some clues in the code and I found some save-load file inside persistentcode.*

For what I've understand, these functions work with the board file system, they open and read a .mpy file and then build a reader object with a data attribute which is a reader_posix who has a 'buf' attribute where stores the content of the .mpy file for it's posterior process, am I wrong?

In my case, my board doesn't have one, so I was thinking of modifying these functions or create new one based on them, who could take the content of a .mpy through a HAL_Recieve, and store it inside a reader_posix object which would be inside a reader object which will be passed to 'do_execute_raw_code' for the code execution.

Probably, I am missing some things or not considering others but is the idea I came up with. Do you have any tips or functions I should take a look at before trying my idea?

Thank you very much.

Post Reply