Code Monkey home page Code Monkey logo

uflash's Introduction

uFlash

THIS MODULE ONLY WORKS WITH PYTHON 2.7 or 3.3+.

A utility for flashing the BBC micro:bit with Python scripts and the MicroPython runtime. You pronounce the name of this utility "micro-flash". ;-)

It provides three services:

  1. A library of functions to programatically create a hex file and flash it onto a BBC micro:bit.
  2. A command line utility called uflash that will flash Python scripts onto a BBC micro:bit.
  3. A command line utility called py2hex for creating hex files from Python scripts and saving them on the local filesystem

Several essential operations are implemented:

  • Encode Python into the hex format.
  • Embed the resulting hexified Python into the MicroPython runtime hex.
  • Extract an encoded Python script from a MicroPython hex file.
  • Discover the connected micro:bit.
  • Copy the resulting hex onto the micro:bit, thus flashing the device.
  • Specify the MicroPython runtime hex in which to embed your Python code.

Installation

To install simply type:

$ pip install uflash

...and the package will download from PyPI. If you wish to upgrade to the latest version, use the following command:

$ pip install --no-cache --upgrade uflash

NB: You must use a USB data cable to connect the micro:bit to your computer (some cables are power only). You're in good shape if, when plugged in, the micro:bit appears as a USB storage device on your file system.

Linux users: For uflash to work you must ensure the micro:bit is mounted as a USB storage device. Usually this is done automatically. If not you've probably configured automounting to be off. If that's the case, we assume you have the technical knowledge to mount the device yourself or to install the required kernel modules if they're missing. Default installs of popular Linux distros "should just work" (tm) out of the box given a default install.

Command Usage

uflash

To read help simply type:

$ uflash --help

or:

$ uflash -h

To discover the version information type:

$ uflash --version

If you type the command on its own then uflash will attempt to find a connected BBC micro:bit and flash an unmodified default version of the MicroPython runtime onto it:

$ uflash
Flashing Python to: /media/ntoll/MICROBIT/micropython.hex

To flash a version of the MicroPython runtime with a specified script embedded within it (so that script is run when the BBC micro:bit boots up) then pass the path to the Python script in as the first argument to the command:

$ uflash my_script.py
Flashing my_script.py to: /media/ntoll/MICROBIT/micropython.hex

You can let uflash watch for changes of your script. It will be flashed automatically every time you save it:

$ uflash -w my_script.py

or:

$ uflash --watch my_script.py

At this point uflash will try to automatically detect the path to the device. However, if you have several devices plugged in and/or know what the path on the filesystem to the BBC micro:bit already is, you can specify this as a second argument to the command:

$ uflash myscript.py /media/ntoll/MICROBIT
Flashing myscript.py to: /media/ntoll/MICROBIT/micropython.hex

You can even flash multiple devices at once:

$ uflash myscript.py /media/ntoll/MICROBIT /media/ntoll/MICROBIT1
Flashing myscript.py to: /media/ntoll/MICROBIT/micropython.hex
Flashing myscript.py to: /media/ntoll/MICROBIT1/micropython.hex

To extract a Python script from a hex file use the "-e" flag like this:

$ uflash -e something.hex myscript.py

This will save the Python script recovered from "something.hex" into the file "myscript.py". If you don't supply a target the recovered script will emit to stdout.

If you're developing MicroPython and have a custom runtime hex file you can specify that uflash use it instead of the built-in version of MicroPython in the following way:

$ uflash -r firmware.hex

or:

$ uflash --runtime=firmware.hex

py2hex

To create output .hex files in the same directory as the input .py files:

$ py2hex tests/example.py
Hexifying example.py as: tests/example.hex

py2hex includes that same -r/--runtime and -m/--minify options as uflash and adds an additional option -o/--outdir:

To create output .hex files in a different directory:

$ py2hex example.py -o /tmp
Hexifying example.py as: /tmp/example.hex

or:

$ py2hex example.py --outdir /tmp
Hexifying example.py as: /tmp/example.hex

py2hex can handle multiple input files:

$ py2hex a.py b.py c.py
Hexifying a.py as: a.hex
Hexifying b.py as: b.hex
Hexifying c.py as: c.hex

or:

$ py2hex *.py
Hexifying a.py as: a.hex
Hexifying b.py as: b.hex
Hexifying c.py as: c.hex

Development

The source code is hosted in GitHub. Please feel free to fork the repository. Assuming you have Git installed you can download the code from the canonical repository with the following command:

$ git clone https://github.com/ntoll/uflash.git

Ensure you have the correct dependencies for development installed by creating a virtualenv and running:

$ pip install -r requirements.txt

To locally install your development version of the module into a virtualenv, run the following command:

$ python setup.py develop

There is a Makefile that helps with most of the common workflows associated with development. Typing make on its own will list the options thus:

$ make

There is no default Makefile target right now. Try:

make clean - reset the project and remove auto-generated assets.
make pyflakes - run the PyFlakes code checker.
make pep8 - run the PEP8 style checker.
make test - run the test suite.
make coverage - view a report on test coverage.
make check - run all the checkers and tests.
make package - create a deployable package for the project.
make rpm - create an rpm package for the project.
make publish - publish the project to PyPI.
make docs - run sphinx to create project documentation.

uflash's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

uflash's Issues

Feature-Request: Non zero exit code in case of errors

Hi,

great tool for the mircobit! I just was wondering, if you could make the script exit with a non-zero exit code in case an error occurred.
E.g. at the point where the exeption is printed a following "exit(1)" would help to determin from e.g. a shell script if all worked or not.

Regards,
Stefan

Extract scripts more robustly

Currently uFlash hexelifies by adding an “extended linear address record type" entry right before the python script starts. The unhexlify function relays on this entry to be immediately before the script, which might not be explicitly declared a second time if the hex file was created or extracted using different methods.

For testing purposes I am attaching a hex file where the data between start of address 0003xxxx and 0x0003e000 is present as such, without a double :020000040003F7 entry:
ubit_flash.txt (just rename from txt to hex).

installing uflash gives an error

i've just installed uflash with command sudo -H pip3 install uflash although it appears to have successfully installed i get the error output

Building wheels for collected packages: uflash
  Running setup.py bdist_wheel for uflash ... error
  Complete output from command /usr/bin/python3 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-ied4gn_o/uflash/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" bdist_wheel -d /tmp/tmp2uvtutdcpip-wheel- --python-tag cp36:
  usage: -c [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
     or: -c --help [cmd1 cmd2 ...]
     or: -c --help-commands
     or: -c cmd --help
  
  error: invalid command 'bdist_wheel'
  
  ----------------------------------------
  Failed building wheel for uflash

py2hex?

The py2hex tool that is described seems to be missing. I see the function in uflash.py, but one still has to add something trivial like

py2hex.py:
--->>---
import uflash
import sys
if name == 'main':
uflash.py2hex(sys.argv[1:])
---<<----

or am I missing some trick here?

Feature: Auto-reset the Microbit after flashing is complete

I don't know whether this is possible from the tool, but having the Microbit automatically reset after flashing a new script would be great.

There's a pseudo file on the mounted drive called "HARD_RST.CFG" vs. "AUTO_RST.CFG" which can be used to set this directly on the Microbit, but it only works by going into maintenance mode first (https://www.mbed.com/en/development/hardware/prototyping-production/daplink/daplink-on-kl26z/).

The file mentions sending a "send a serial break command" would have the same effect. Perhaps the tool could do this via the COM port.

Problems when appending scripts to recent runtime

uFlash 1.0.8 appears to be incorrectly appending scripts to a runtime that I'm supplying which is based on the latest commit (f682113) of MicroPython.

  • I have compiled an unmodified runtime of MicroPython using the above referenced commit.
  • Flashing my runtime to the device without a script and carrying out some brief tests suggests that it's working properly - E.G. display, buttons, speech and radio all work.
  • Using uFlash to flash any script and supplying my runtime results in the device seemingly doing nothing. The script is not executed, nothing is printed to serial & I am unable to access the REPL.
  • Using uFlash to flash a script without specifying a runtime works.
  • Using uFlash to flash a script using an older runtime such as https://github.com/ntoll/microrepl/blob/master/firmware.hex works.
  • If I flash my runtime, rename my script to main.py and "put" it on the device using microfs it works - E.G. the script executes on boot, behaves as I would expect it to & I'm able to access the REPL.

mp.zip

Please see attached for my runtime. For what it's worth it was compiled under Windows 10 X64 which I realize might not be a typical build environment.
Apologies if the issue in fact turns out to be related to my runtime as opposed to uFlash.

Is it possible to flash several .py files simultaneously?

This is my shell script to run the files simultaneously. bright.py file is imported in resistance.py. However, I get an import error that there is no such module bright. What could be the problem?

COMMAND1="uflash resistance.py" 
COMMAND2="uflash bright.py"
$COMMAND2 & $COMMAND1 &

PWM weirdness

There's an issue raised in microbit micropython repository and I think I've encountered something similar. A servo driver I wrote is now pointing all over the place.

Problem

Perhaps the best example of the issue is an LED. I used the following script:

Code

from microbit import pin0, sleep, display

display.scroll("1")

for i in range(0, 1023, 10):
    pin0.write_analog(i)
    sleep(100)


display.scroll("2")

pin0.set_analog_period(5)
for i in range(0, 1023, 10):
    pin0.write_analog(i)
    sleep(100)

Video

there's a video of it here.

Both flicker but the first is slightly worse.

Reproduced in

(all examples below return >>> MicroPython v1.7-9-gbe020eb on 2016-04-18; micro:bit with nRF51822 as their version)

Reproduced in:

  • microbit.org.uk editor
  • mu-2016-07-03_19_19_01
  • mu-2016-07-31_23_01_47.exe
  • mu-2016-08-13_20_40_52.exe
  • mu-2016-10-15_13_20_00.exe
  • mu-2016-11-14_08_54_40.exe
  • mu-2016-12-23_14_19_21.exe
  • current version of uflash.

Not Reproduced in:

  • microbit.co.uk editor
  • mu-2016-05-04-07-19-54.exe
  • mu-2016-06-23_18_11_29.exe
  • mu-2016-06-27_05_46_19.exe

Thoughts

It stops working between two sequential windows builds listed on the amazon cloud: 27-06-2016 and 03-07-2016.

Perhaps this commit in mu? and this one in uflash?. The runtime was updated.

.hex files flashed in the current version of uflash still flicker. I can checkout old versions of uflash but I cannot get it to flash a .hex. Not sure why.

There was an issue with PWM weirdness when they introduced to speech module. Perhaps that?

Next uFlash release

@ntoll FYI

Looks like the latest release in PyPI is 2.0.0, which has an old version of MicroPython for micro:bit V2.

v2.1.0 for micro:bit V2 and v1.1.0 for micro:bit V1 have been released recently, so I can update uFlash to include these versions and we can release uFlash 2.1.0.

Currently finding a small issue encoding the filesystem in the V1 hex file, so I'll need to fix that before I can submit a PR. I'll see if I have some time tonight, otherwise I'll be busy until next week.

Performance penalty if firmware hex string is dropped?

Whilst packaging uflash and the MicroPython firmware, I've been wondering whether there would be a (significant) performance penalty during flashing if the firmware was always read from the local hex file (and the firmware string dropped from the uflash.py file)?

In selfish terms of downstream packaging of uflash, having only one copy of the firmware - in a separate file - would make managing firmware updates very straightforward. In Debian we'd use the Debian-built version of the firmware. Keeping the duplicate copy as an embedded string in the source code not only doubles the size of the package but also creates extra work "injecting" the locally-built firmware back into the source code after each update.

Is performance known to be okay reading the firmware hex in from the local file? Have I missed something important as to why the string version of the firmware is necessary?

I can't import log

Hi! :)

My problem is that when I upload the firmware with uflash, and try to import the log module, I get this error:

>>> import log
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: no module named 'log'

However, some modules load with no problem, like import radio or import neopixel.

Am I doing something wrong? Thank you very much in advance!

Script size check should return something useful

At the moment it is basically doing an assert len(data) <= 0x2000.
There should be a useful error message coming back instead, so that other tools could rely on uFlash to deal with this case gracefully and be able to respond accordingly.
Perhaps even add a check_script_size() type of function ?

flashing a script is different from putting a main.py

I was trying a test program. I plan to have a large file so thought i'd give it a dummy run.
if i flash my script like this uflash test_fs.py . When the script executes it crashes with OSError: 28.
When i flash a blank micropython.hex uflash then put my script as main.py ufs put test_fs.py main.py it executes ok.
Here's a copy of the program which crashes:
test_fs.py.txt

Please tag releases

Please tag releases to keep this repo in sync with PyPI's release versions of uflash

uflash needs a --port option

uflash needs a --port option to direct the process to the correct microbit when multiple microbits are plugged in. It also probably needs to mimic how ufs (microfs) selects a port. I have 2 microbits plugged in uflash flashes the first, where as ufs ls lists the contents of the second

Error when flashing a Python script

Hi,

I am trying to flash a Python script but get the following error:

ushort format requires 0 <= number <= (0x7fff * 2 + 1)

What does this mean, please, and how do I get around it?
Many thanks,
Andrew.

Allow for script minification

There should be a flag (--minify and -m) that causes uflash to "minify" the Python script using various heuristics so it'll fit into the limited amount of memory available.

See: mu-editor/mu#385 for an example of what I mean.

Package for Debian?

Hi. I consider packaging uflash and uploading it into Debian. This might be easier with your help. Are you interested in helping out to make this happen?

with --runtime disabled, how to upload a new hex file?

I just started with a micro:bit and had issues to install mu, so I was happy to find uflash.
However, trying to test it with an expansion board results in module issues with "superbit". I found a hex file which probably includes this, but as far as I can see, the current uflash (2.0.0) doesn't support uploading a hex file.
Is there another tool to upload a hex file, or is there an alternative to install the module along with my python file?
Or should I just try to use an older version of uflash (which leaves the question open why -r has been deprecated).

Getting weird error popup when running on Windows

Every time I run the tool, a popup dialog comes up saying something like "There's no disk in the drive. Please insert a disk into driver \Device\Hardware1\DR1" (translated from German). The dialog asks for [Cancel], [Repeat], [Continue]. Clicking on [Continue] let's flashing continue and the tool does write to the correct mounted drive.

Flash multiple microbits at once

Now that we have the radio module, there's an obvious need to flash multiple devices at once, when updating a microbit squadron with your new game.

Currently you can address non-default targets like uflash race.py /media/tom/MICROBIT1 but it would be nice to have:
uflash race.py /media/tom/MICROBIT /media/tom/MICROBIT1

This would allow shell expansions like:

$ uflash race.py /media/tom/MICROBIT*
or
$ uflash race.py /media/tom/MICROBIT{,1,2}

Happy to write the patch if there's agreement on this feature.

uflash: document required kernel modules

Plugged it in, it's blinking with the demo.
Buttons and sensors seems to work normally (as used in the demo).

Ran:

tw@tux:~/w$ virtualenv --python=python3 mipy-env
tw@tux:~/w$ . mipy-env/bin/activate
(mipy-env)tw@tux:~/w$ pip install uflash
Downloading/unpacking uflash
  http://localhost:3141/root/pypi/+simple/uflash/ uses an insecure transport scheme (http). Consider using https if localhost:3141 has it available
  Downloading uflash-1.0.3.tar.gz (273kB): 273kB downloaded
  Running setup.py (path:/home/tw/w/mipy-env/build/uflash/setup.py) egg_info for package uflash

Installing collected packages: uflash
  Running setup.py install for uflash

    Installing uflash script to /home/tw/w/mipy-env/bin
Successfully installed uflash
(mipy-env)tw@tux:~/w$ uflash
Unable to find micro:bit. Is it plugged in?

Ubuntu 16.04, Kernel 4.6.4, Python 3.5.1, USB3 Port (have no USB2).

uflash fails to find microbit

I was trying some blocks and returned to micropython.
when i tried command uflash my-script.py i get error message
Error flashing pongo.py to microbit: Unable to find micro:bit. Is it plugged in?

solution
uflash my-script.py ~/Downloads
that creates a micropython.hex in ~/Downloads I just copied that using a GUI file manager to my microbit

but with this script now flashed to the micobit uflash my-script.py still fails

Flashing with custom micropython is not working

Using this command:
uflash -r custom_runtime.hex my_script.py (1)
I've tried to flash microbit using a custom runtime hex file you I created instead of the built in hex.

Microbit gets flashed but does not run my script. At first I thought there's something wrong with my custom runtime hex file, but then I tried with bbcmicrobit/micropython.
I built the frimware hex following their documentation, tried to flash microbit uflash -r microbit-micropython.hex my_script.py (2), no results.

Finally my last attempt was to run the same command but this time using the already built in hex file.
uflash -r built-in.hex my_script.py (3), works like a charm.

I'm wondering the reasons behind (3) working and not (1) & (2). Is uflash capable of only embedding scripts with the already built in run time hex?

Possible to pull MicroPython runtime from device, update, and then flash?

Is it possible for uflash to pull a runtime from a micro:bit, validate that it is a MicroPython runtime, and then reflash the device after inserting the desired Python scripts, or is the process write-only?

The reason for the query is that the MicroPython runtime, as built via yotta and distributed with uflash/Mu, is built partly (as far I can see following the yotta build dependency chain) from sources that include non-free code (https://github.com/ARMmbed/ble-nrf51822 for example). Please correct me if this is not the case.

If it were possible to pull the runtime off a device prior to flashing, it would mean that the firmware would not need to be distributed in the downstream packages of operating systems that only distribute Free Software.

The MIT-licensed uflash repository contains firmware.hex, and uflash.py contains a copy of the firmware, and is MIT-licensed per header. Can I check that this is compatible with the more restrictive licensing of some of the upstream source(s)?

(I note the separate issue discussing the provenance of the included firmware.hex file at #46).

Update to latest firmware

It looks like the firmware of uflash is built from 24th Oct 2016 (correct me if I'm wrong!). There have been some important bug fixes since then (notably PWM) and it would be great to update the firmware to the latest (also in mu).

Check .hex file burning completed in microbit

how we check .hex file burning completed in microbit? to run another .hex file
in my case I am running one .hex file and before completing its burring I run another .hex file so I got a error like microbit not connected check microbit .
so if we able to identify first burning is not completed to burn another hex file then i think it solve a problem

Upstream source of firmware.hex ?

Hi,

Please can you tell me where the baked-in default firmware.hex is from? (e.g. which branch or tag in the BBCMicroBit MicroPython repo)

Thanks
Wayne

Failed to create process error

Hi, I'm not sure if I am trying to use this right, but when I try to run uflash I get a Failed To Create Process error.

Any suggestions how I should fix this?

Is there a reason that uflash doesn't preserve filenames?

Is there a reason that 'uflash my_file.py' creates micropython.hex instead of my_file.hex? Would there be an objection to changing it or would it be preferable to add an option like '-k' and/or '--keepname'?
I would like the functionality to be able to pre-hexlify files (uflash my_file,py ./). I'll start looking at the code to see how this could be done.

uflash should run without installing

At present, uflash relies on the setuptools console entry points mechanism via setup.py, which means you can't just do "uflash.py args" even though it's just one module!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.