3

I am running Ubuntu 22.04 with xorg. I need to find a way to compile microbit python code locally to a firmware hex file. Firstly, I followed the guide here https://microbit-micropython.readthedocs.io/en/latest/devguide/flashfirmware.html.

After a lot of debugging, I got to this point: https://pastebin.com/MGShD31N

However, the file platform.h does exist.

sawntoe@uwubuntu:~/Documents/Assignments/2022/TVP/micropython$ ls /home/sawntoe/Documents/Assignments/2022/TVP/micropython/yotta_modules/mbed-classic/api/platform.h
/home/sawntoe/Documents/Assignments/2022/TVP/micropython/yotta_modules/mbed-classic/api/platform.h
sawntoe@uwubuntu:~/Documents/Assignments/2022/TVP/micropython$ 

At this point, I gave up on this and tried using Mu editor with the AppImage. However, Mu requires wayland, and I am on xorg.

Does anyone have any idea if this is possible? Thanks.

sean teo
  • 57
  • 6

4 Answers4

2

Mu and the uflash command are able to retrieve your Python code from .hex files. Using uflash you can do the following for example:

uflash my_script.py

I think that you want is somehow possible to do, but its harder than just using their web python editor: https://python.microbit.org/v/2

Peter Till
  • 91
  • 6
2

Peter Till answers the original question. The additional below adds to this answer by showing how to automate the build and load process. I use Debian. The original question states that Ubuntu is used, which is built on Debian.

A script to find and mount the micro:bit

When code is loaded to the micro:bit, the board is dismounted from the system. So each time you have new code to load, you have to remount the board.

I modified a script to find and mount the micro:bit.

#!/bin/bash
 
BASEPATH="/media/$(whoami)/"
MICRO="MICROBIT"
 
if [ $# -eq 0 ]
then
    echo "no argument supplied, use 'mount' or 'unmount'"
    exit 1
fi
 
if [ $1 == "--help" ]
then
    echo "mounts or unmounts a BBC micro:bit"
    echo "args: mount - mount the microbit, unmout - unmount the microbit"
fi
 
# how many MICRO found in udiksctl dump
RESULTS=$(udisksctl dump | grep IdLabel | grep -c -i $MICRO)
 
case "$RESULTS" in
 
0 )     echo "no $MICRO found in 'udkisksctl dump'"
        exit 0
        ;;
 
1 )     DEVICELABEL=$(udisksctl dump | grep IdLabel | grep -i $MICRO | cut -d ":" -f 2 | sed 's/^[ \t]*//')
        DEVICE=$(udisksctl dump | grep -i "IdLabel: \+$DEVICELABEL" -B 12 | grep " Device:" | cut -d ":" -f 2 | sed 's/^[ \t]*//')
        DEVICEPATH="$BASEPATH""$DEVICELABEL"
        echo "found one $MICRO, device: $DEVICE"
 
        if [[ -z $(mount | grep "$DEVICE") ]]
        then
            echo "$DEVICELABEL was unmounted"
            if [ $1 == "mount" ]
            then
                udisksctl mount -b "$DEVICE"
                exit 0
            fi
        else
                echo "$DEVICELABEL was mounted"
                if [ $1 == "unmount" ]
                then
                    udisksctl unmount -b "$DEVICE"
                    exit 0
                fi
        fi
        ;;
 
* )     echo "more than one $MICRO found"
        ;;
 
    esac
 
echo "exiting without doing anything"

I alias this script to mm in my .bashrc file.

Automate mounting the micro:bit and flashing the python file

I use the inotifywait command to run mm and to then run uflash to load the .py file I am working on. Each time that the python file is saved, the aliased command mm is run followed by the uflash command.

while inotifywait -e modify <your_file>.py ; do mm && uflash <your_file>.py ; done
Oppy
  • 2,662
  • 16
  • 22
1

Okay, so elaborating on Peter Till's answer.

Firstly, you can use uflash:

uflash path/to/your/code .

Or, you can use microfs:

ufs put path/to/main.py
sean teo
  • 57
  • 6
1

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

# Build one example.
docker run -v $(pwd):/home --rm ghcr.io/carlosperate/microbit-toolchain:latest \
  tools/makecombinedhex.py build/firmware.hex examples/counter.py -o build/counter.hex

# Build all examples.
docker run -v $(pwd):/home --rm ghcr.io/carlosperate/microbit-toolchain:latest \
  bash -c '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/"

Some further comments at: Generating micropython + python code `.hex` file from the command line for the BBC micro:bit

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