Bundle python files into the compiled firmware

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
User avatar
robin
Posts: 3
Joined: Tue Aug 10, 2021 7:26 am
Contact:

Bundle python files into the compiled firmware

Post by robin » Tue Aug 10, 2021 7:36 am

Hello everyone!

I am trying to bundle my python code files into a MicroPython UF2 build.
Reason for this is that I'd love to be able to flash the UF2 file directly onto my RP2040 and have a fresh MicroPython build as well as all my python files up and running without having to send the files to the device after flashing MP.

My current approach is to put my boot.py, main.py and all the other files files into the ports/rp2/modules directory.
I am a little unclear about the MICROPY_MODULE_FROZEN macro.
If I understand things correctly I this should be defined and then pyexec_file_if_exists() within the main.c file should find my boot.py and main.py files and start executing, right?

So my question is, is that the best approach for this?

Thanks in advance!

Cheers,
Robin

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

Re: Bundle python files into the compiled firmware

Post by Roberthh » Tue Aug 10, 2021 2:57 pm

Just puttign them into the modules directory is not sufficient. You have to re-build the firmware to freeze the code into the image. But then pyexec_file_if_exists() should find it.

User avatar
robin
Posts: 3
Joined: Tue Aug 10, 2021 7:26 am
Contact:

Re: Bundle python files into the compiled firmware

Post by robin » Wed Aug 11, 2021 6:06 am

Oh my text was probably misleading.
I did mean putting the files into the modules dir before actually building MicroPython.
But that's good news that it will autodetect the main.py and boot.py in that folder.
I'll give it a try later today.

Thanks!

User avatar
robin
Posts: 3
Joined: Tue Aug 10, 2021 7:26 am
Contact:

Re: Bundle python files into the compiled firmware

Post by robin » Wed Aug 11, 2021 6:57 am

This works just perfect! 🎉
Here's a quick tutorial for all that see this post.
I'm running those commands inside an Ubuntu 20.04 image in a VirtualBox.

Code: Select all

sudo apt-get install git
sudo apt-get install gcc-arm-none-eabi libnewlib-arm-none-eabi
sudo apt-get install build-essential
sudo apt-get install cmake
git clone git@github.com:micropython/micropython.git
cd micropython/mp-cross
make
cd ..
git submodule update --init lib/pico-sdk lib/tinyusb
Now put all your python files like main.py, boot.py, ... inside the ports/rp2/modules directory.
When finished build this micropython port as shown here:

Code: Select all

cd ports/rp2
make
You should now have the output files within ports/rp2/build-PICO
Especially helpful is the UF2 file which you can drag and drop onto a Pico while it is booted into its built-in bootloader.
This UF2 file now contains MicroPython and all your files frozen into it and your ready 🥳

jedidiah.paterson
Posts: 1
Joined: Thu Jun 09, 2022 11:21 am

Re: Bundle python files into the compiled firmware

Post by jedidiah.paterson » Thu Jun 09, 2022 11:22 am

Hi, This is really cool have you seen anything like this for windows?

willie68
Posts: 3
Joined: Thu Jun 16, 2022 9:08 am

Re: Bundle python files into the compiled firmware

Post by willie68 » Fri Jun 17, 2022 11:24 am

I had the same question yesterday. So i try another aproach with docker dektop for windows. And it worked. See here
viewtopic.php?f=21&t=12559#p68049

Post Reply