1

Is it possible to generate a .hex file with MicroPython and my own python program code at a Linux command line, rather than in one of the editors?

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
Ben Clifford
  • 1,398
  • 1
  • 12
  • 23

3 Answers3

4

Looking at the tag in your question, it looks like you want to use MicroPython on the BBC micro:bit, correct? If that's the case then youu can use this Python command line tool: https://github.com/ntoll/uflash/

Instructions on how to install it and use it can be found in the README at that link.

This works with Python 2 and 3, and your Linux distribution is very likely to have at least one Python version available out-of-the-box.

If you have pip installed you can easily install it with: pip install uflash

But you can also download the source code, using git or downloading a zip file from GitHub (https://github.com/ntoll/uflash/archive/master.zip), and run it without installing anything. In this case you can execute the uFlash script with Python:

python uflash.py path_to_your_code.py

And the current version of uFlash includes the latest version of MicroPython for the micro:bit.

carlosperate
  • 594
  • 4
  • 11
0

You can write the micropython code for the microbit in any text editor, such as vscode or vim. Save it as a .py file.

To create the .hex file, use the py2exe tool that is installed along with uflash when you install uflash using the command:

pip install uflash

To create a .hex file for a microbit micropython file called hello.py:

py2hex hello.py

This creates a file called hello.hex. This can be dragged and dropped onto your connected microbit through the file explorer. I use Nautilus and the microbit appears as 'MICROBIT'.

You can automate the creation and loading of the .hex file to the microbit using uflash, e.g.

uflash hello.py

This will create the .hex file and then load it onto an attached microbit. The .hex file will not be left on your file system though. The microbit has a habit of no longer being attached to the file system after loading a .hex file and needs to be re-attached in between builds.

Oppy
  • 2,662
  • 16
  • 22
0

Working Ubuntu 22.04 host CLI setup with Carlos Atencio's Docker to build your own firmware

After trying to setup the toolchain for a while, I finally decided to Google for a Docker image with the toolchain, and found https://github.com/carlosperate/docker-microbit-toolchain at this commit from Carlos Atencio, a Micro:Bit foundation employee, and that just absolutely worked:

# Get examples.
git clone https://github.com/bbcmicrobit/micropython
cd micropython
git checkout 7fc33d13b31a915cbe90dc5d515c6337b5fa1660

# Get Docker image.
docker pull ghcr.io/carlosperate/microbit-toolchain:latest

# Build setup to be run once.
docker run -v $(pwd):/home --rm ghcr.io/carlosperate/microbit-toolchain:latest yt target bbc-microbit-classic-gcc-nosd@https://github.com/lancaster-university/yotta-target-bbc-microbit-classic-gcc-nosd
docker run -v $(pwd):/home --rm ghcr.io/carlosperate/microbit-toolchain:latest make all
sudo chmod -R +666 .

# Build one example.
tools/makecombinedhex.py build/firmware.hex examples/counter.py -o build/counter.hex

# Build all examples.
for f in examples/*; do b="$(basename "$f")"; echo $b; tools/makecombinedhex.py build/firmware.hex "$f" -o "build/${b%.py}.hex"; done

And you can then flash the example you want to run with:

cp build/counter.hex "/media/$USER/MICROBIT/"

What uflash does it to ship its own precompiled firmware.hex which is the part that requires the toolchain, and it then just uses that to build the combined hex in Python.

The cool thing is that now that we have the toolchain, we can also create examples directy in C/C++/assembly: How to compile C/C++ code into a .hex file for the BBC micro:bit? which can likely run much faster.

Previous failed attempts at setting it up myself

The Yotta package manager used by BBC Microbit bit rot almost immediately after it got was discontinued, making pip install yota approaches like: https://flames-of-code.netlify.app/blog/microbit-cpp-1/ very difficult.

The GCC gcc-arm-embedded toolchain PPA ppa:team-gcc-arm-embedded/ppa has also been discontinued: https://askubuntu.com/questions/1243252/how-to-install-arm-none-eabi-gdb-on-ubuntu-20-04-lts-focal-fossa and now you would have to download from an arm.com website.

Atencios' Docker setup explains how to do it though: https://github.com/carlosperate/docker-microbit-toolchain/blob/master/Dockerfile , the key is likely using his magically crafted requirements.txt, likely kept back from the day when things really worked, to avoid the infinitely many dependency issues of yotta. He's on Ubuntu 20.04.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985