pimoroni-pico icon indicating copy to clipboard operation
pimoroni-pico copied to clipboard

How to compile micropython

Open VitRuzicka opened this issue 1 year ago • 7 comments

Hi, I need to make some changes in the source code of one of the libraries included in this project. How could I then compile your micropython from source? Or is it possible to change pinout defined in the drivers->hub75.hpp in compiled version ? Best regards, Vit

VitRuzicka avatar May 08 '23 09:05 VitRuzicka

Just fork the repository, make changes and GitHub Actions will build new versions of all the MicroPython firmwares. Trying to do it locally is... not fun.

If you're determined, the build steps are more or less documented by the workflow file here: https://github.com/pimoroni/pimoroni-pico/blob/main/.github/workflows/micropython.yml

Gadgetoid avatar May 09 '23 12:05 Gadgetoid

@Gadgetoid I would much rather prefer the fun way, so I'l just do a fork. Do I have to nudge somehow Github Actions to compile the code? Or does it work automatically?

Thanks, Vit

VitRuzicka avatar May 09 '23 14:05 VitRuzicka

I just had to figure this out myself. Assuming you've checked out Micropython and pimoroni-pico repositories, you can do this:

MPY= # wherever you have the micropython repo
PMP= # wherever you have the pimoroni-pico repo
cd $MPY/ports/rp2
patch -p3 < $PMP/micropython/micropython_nano_specs.patch # only needs to be done once
make USER_C_MODULES=$PMP/micropython/modules/micropython-interstate75_ulab.cmake PICO_BUILD_DOCS=0 MICROPY_BOARD=PICO # add DEBUG=1 if debugging symbols are desired
# result will be in build-PICO/firmware.uf2

Additionally, if you export CMAKE_EXPORT_COMPILE_COMMANDS=1 prior to running that, cmake will generate a compile_commands.json file in the $MPY/ports/rp2/build-PICO build directory. This can be used by your editor to help with finding include paths.

ned-pcs avatar May 10 '23 18:05 ned-pcs

Thank you @ned-pcs I'll look into it. VR

VitRuzicka avatar May 13 '23 14:05 VitRuzicka

This is more or less the build process from our CI for PICO:

git clone https://github.com/pimoroni/pimoroni-pico
git clone https://github.com/micropython/micropython

git -C pimoroni-pico submodule update --init

cd micropython/ports/rp2
make BOARD=PICO submodules

git apply ../../../pimoroni-pico/micropython/micropython_nano_specs.patch

cmake -S . -B build-PICO -DPICO_BUILD_DOCS=0 -DUSER_C_MODULES=../../../pimoroni-pico/micropython/modules/micropython-pico.cmake -DMICROPY_BOARD_DIR=`pwd`/../../../pimoroni-pico/micropython/board/PICO/ -DMICROPY_BOARD=pico
cmake --build build-PICO -j

And PICO_W:

git clone https://github.com/pimoroni/pimoroni-pico
git clone https://github.com/micropython/micropython

git -C pimoroni-pico submodule update --init

cd micropython/ports/rp2
make BOARD=PICO_W submodules

git apply ../../../pimoroni-pico/micropython/micropython_nano_specs.patch

cmake -S . -B build-PICO_W -DPICO_BUILD_DOCS=0 -DUSER_C_MODULES=../../../pimoroni-pico/micropython/modules/micropython-picow.cmake -DMICROPY_BOARD_DIR=`pwd`/../../../pimoroni-pico/micropython/board/PICO_W/ -DMICROPY_BOARD=pico_w
cmake --build build-PICO_W -j

This uses -DMICROPY_BOARD_DIR (a CMake arg) to specify our version of the PICO_W board dir, which ensures various libs like gfx_pack.py, pimoroni.py and our boot.py are included. Without boot.py - which switches our C++ memory shim over to using MicroPython's heap - you may run into C++ OOM issues.

Some boards - like Inky - also have a pico_sdk.patch which add some startup clock config, RTC config and GPIO latching. If you see a build error like undefined reference to runtime_wakeup_gpio_state'` then you're missing that patch.

Details of which BOARD, CMake file and whether a board-specific patch is needed can be gleaned from the CI config: https://github.com/pimoroni/pimoroni-pico/blob/00d161794736d545d933181566409c5b2cc3a8dc/.github/workflows/micropython.yml#L69-L90

Gadgetoid avatar May 15 '23 10:05 Gadgetoid

If anyone has problems with that then remember that PICO got renamed to RPI_PICO and PICO_W to RPI_PICO_W

Obsttube avatar Nov 27 '23 18:11 Obsttube

I suppose it's worth noting here. I'm in the process of writing some tooling to make the MicroPython build steps possible to run locally, in the same way they are run on CI, see: https://github.com/pimoroni/pimoroni-pico/pull/878

Gadgetoid avatar Nov 27 '23 20:11 Gadgetoid