BBC micro: bit (V1.5) C/C ++ programming-Mbed CLI offline compilation in Python3 virtual environment on Windows 10

The BBC micro:bit development using the Mbed CLI in a Python3 environment on Widows 10 - this target micro:bit v1.5

BBC micro: bit is a learning microcomputer board that uses nRF51822 for the MCU. With the Mbed development environment, you can fully enjoy the features of nRF51822. Not only micro: bit alone, but also electronic work, wireless communication, etc., the range of utilization methods will expand. In this article, we will explain the problems and solutions in building the development environment by trial and error using the Mbed CLI offline tool that can be compiled even on Windows 10.

Main contents

--Problems with Windows 10 and Python 3 --microbit_blinky sample --microbit-hello-world sample --mbed new (create-only) command --Packaging and template

The BBC micro: bit V2 was released in Japan on November 25, 2020, but we have confirmed the operation with the conventional V1.5 that we have.

[Problem] --Micro: bit programming development with Mbed CLI (offline)

  1. Mbed CLI tools depend on Python2 (2 spec changes)
  2. On Windows 10, a compile error occurs on the microbit-dal
  3. There is a trick to use the mbed library for micro: bit

[Cause 1] Python 3 specification change

When I try to create an mbed project with the Mbed CLI tool in the Ptyhon3 environment, I get stuck in an endless loop while downloading the library as follows.

mbed_new.cmd


[mbed] Adding library "mbed" from "https://mbed.org/users/mbed_official/code/mbed/builds" at branch/tag "tip"
[mbed] Downloading library build "65be27845400" (might take a while)

This is because the download function in mbed-cli/mbed/mbed.py is reading 1MB of data at a time, but I can't get out of that while loop (data = inurl.read (1024 * 1024)". ) In the end, data becomes b'', but b''! ='' is always true).

mbed-cli/mbed/mbed.py


 action("Downloading library build \"%s\" (might take a while)" % rev) 
 inurl = urlopen(url) 
 with open(rev_file, 'wb') as outfd: 
     data = None 
     while data != '': 
         # Download and write the data in 1 MB chunks 
         data = inurl.read(1024 * 1024) 
         outfd.write(data) 

Also, because dict.has_key () has been deprecated, when I try to compile the mbed project, I get the following run-time error for the mbed CLI tool:

mbed_compile.cmd


AttributeError: 'dict' object has no attribute 'has_key'

[Cause 2] Confusion between header files BLE.h and ble.h

The Windows OS is usually case-insensitive, so the toolchain (compiler) cannot correctly search the header files BLE.h (microbit-dal) or ble.h (nrf5 SDK). I get a compile error.

[Cause 3] Mechanism of mbed CLI (mbed OS)

I get an unfamiliar compilation error. This is because I don't understand how mbedlib is embedded and how it works. Therefore, I have only come up with a workaround (knack). m (__) m

Mbed CLI micro: bit tutorial (countermeasures / workarounds)

Advance preparation

Please install the following three tools.

  1. Git --version 1.9.5 or higher
  2. Mercurial --version 2.2.2 or higher
  3. GNU Arm Embedded Toolchain-6-2017-q2-update

Building a Python3 environment and its virtual environment

  1. Download the Ptyhon3 installer for Windows 10 from the Download Page (https://www.python.org/downloads/). → https://www.python.org/ftp/python/3.9.1/python-3.9.1-amd64.exe
  2. Run the downloaded installer to install Python3 (3.9.1). Check Add Python 3.9 to PATH and install with Install Now. image.png

Building a virtual environment (virtualenv)

  1. At the command prompt, run py -3 -m pip install --upgrade virtualenv to install virtualenv. image.png
  2. Create a folder with the mkdir c: \ workubit command, move to the created folder, and create avenv virtual environment (folder). Command: py -3 -m virtualenv venv
  3. To activate the virtual environment, run C: \ workubit \ venv \ Scripts \ activate.bat. image.png

Mbed CLI installation and configuration

In the Python3 virtual environment (venv), install the Mbed CLI and related tools (pip install).

command prompt.cmd


C:\workubit\venv\Scripts\activate.bat
pip install mbed-cli jsonschema pyelftools
mbed --version

image.png

Set the mbed command to the folder where the GNU Arm Embedded Toolchain executable file is saved.

C:\workubit\venv\Scripts\activate.bat
mbed config --global GCC_ARM_PATH "C:\Program Files (x86)\GNU Tools Arm Embedded\6 2017-q2-update\bin"
mbed config --list

Sample --microbit_blinky

Import and compile a sample LED blinking called microbit_blinky. Open a command prompt, enable the Python3 virtual environment, and run the mbed CLI tool.

command prompt.cmd


cd c:\workubit
C:\workubit\venv\Scripts\activate.bat

mbed import http://os.mbed.com/teams/BBC/code/microbit_blinky/

cd microbit_blinky
mbed compile -m NRF51_MICROBIT -t GCC_ARM

image.png

Run-time error in mbed CLI tool

However, when I execute the compile command with mbed compile ..., I get a has_key attribute (method) error. This is because dict.has_key () has been deprecated in Python3.

image.png

A search with the grep tool shows that has_key () is used in three modules as follows:

grep.txt


□ Search conditions"has_key"
Search target*.py
Folder C:\workubit\microbit_blinky\.temp\tools
    (Search subfolders)
    (Uppercase and lowercase letters)
    (Automatic identification of character code set)
    (Output matching lines)


C:\workubit\microbit_blinky\.temp\tools\build_api.py(530,31)  [SJIS]:                 if self.flags.has_key(k):
C:\workubit\microbit_blinky\.temp\tools\build_profiles.py(37,29)  [SJIS]:         if MBED_SDK_REV_MAP.has_key(build):
C:\workubit\microbit_blinky\.temp\tools\project.py(157,31)  [SJIS]:                 if self.flags.has_key(k):
3 were searched.

How to fix has_key (patch)

You can manually fix these one by one, but I have prepared a patch file, so please use it.

** Procedure 1. Save the patch file in the project folder **

Save the following patch information in a text editor in the c: \ workubit \ microbit_blinky folder with the file name has_key.patch.

has_key.patch


--- ./.temp/tools/build_api.py
+++ ./.temp/tools/build_api.py
@@ -529,3 +529,3 @@
             for k,v in profile_data.items():
-                if self.flags.has_key(k):
+                if k in self.flags:
                     self.flags[k] = v
--- ./.temp/tools/build_profiles.py
+++ ./.temp/tools/build_profiles.py
@@ -36,3 +36,3 @@
     for build in builds:
-        if MBED_SDK_REV_MAP.has_key(build):
+        if build in MBED_SDK_REV_MAP:
             idx = MBED_SDK_REV_MAP[build]
--- ./.temp/tools/project.py
+++ ./.temp/tools/project.py
@@ -156,3 +156,3 @@
             for k,v in profile_data.items():
-                if self.flags.has_key(k):
+                if k in self.flags:
                     self.flags[k] = v

** Procedure 2. Run patch in Git Bash **

After installing Git, you can use the patch command. Select Git Git Bash from the Start menu to start MINGW64 (Git Bash) and execute the following commands in order.

git.bash


cd /c/workubit/microbit_blinky
patch -p1 < has_key.patch

image.png

Re-execute compilation

command prompt.cmd


C:\workubit\venv\Scripts\activate.bat
cd c:\workubit\microbit_blinky
mbed compile -m NRF51_MICROBIT -t GCC_ARM

image.png

Header file error

The next error is that the header file mbed.h cannot be found. This is because the standard mbed library doesn't support micro: bit.

image.png

Therefore, add the mbed-src library published by Lancaster University. I get an error with mbed add ..., but ignore it.

command prompt.cmd


cd c:\workubit\microbit_blinky
C:\workubit\venv\Scripts\activate.bat
mbed add http://os.mbed.com/teams/Lancaster-University/code/mbed-src/
mbed compile -m NRF51_MICROBIT -t GCC_ARM

image.png

Still more errors

The error continues. This is probably due to confusion with the existing mbed library.

image.png

Therefore, delete the mbed library (folder) and compile.

command prompt.cmd


cd c:\workubit\microbit_blinky
C:\workubit\venv\Scripts\activate.bat
rmdir /s /q  mbed
mbed compile -m NRF51_MICROBIT -t GCC_ARM

image.png

If the compilation is successful, a HEX format file will be generated in . \ BUILD \ NRF51_MICROBIT \ GCC_ARM \ microbit_blinky.hex, so copy it to the micro: bit drive (for example, D: ) with the copy command. ..

command prompt.cmd


copy .\BUILD\NRF51_MICROBIT\GCC_ARM\microbit_blinky.hex d:\

image.png

Finally, I was able to compile micro_blinky. If the transfer (copy) of the HEX file to the micro: bit body is successful, only one dot on the upper right of the LED display will blink.

Difficult road-microbit-hello-world

Next, let's compile a sample program called microbit-hello-world.

command prompt.cmd


C:\workubit\venv\Scripts\activate.bat
cd c:\workubit

mbed import http://os.mbed.com/teams/microbit/code/microbit-hello-world

cd microbit-hello-world
mbed compile -m NRF51_MICROBIT -t GCC_ARM

image.png

When you try to compile, you will be prompted to run the mbed deploy command to install the dependent modules and tools.

image.png

When I run the mbed deploy command as follows, I get a message that the tool cannot be found.

command prompt.cmd


mbed deploy

image.png

Comparing the difference with microbit_blinky, the mbed.bld file does not exist. So add the mbed.bld file as follows and run mbed deploy and mbed compile ....

command prompt.cmd


@echo http://mbed.org/users/mbed_official/code/mbed/builds/87f2f5183dfb > mbed.bld
mbed deploy
mbed compile -m NRF51_MICROBIT -t GCC_ARM

image.png

As with microbit_blinky, a has_key attribute (method) error will occur, so start MINGW64 (Git Bash), copy the has_key.patch file, and execute patch.

image.png

git.bash


cp /c/workubit/microbit_blinky/has_key.patch /c/workubit/microbit-hello-world/
cd /c/workubit/microbit-hello-world
patch -p1 < has_key.patch

image.png

In addition, I get a different error, which can be resolved by adding the mbed-src library.

image.png

command prompt.cmd


C:\workubit\venv\Scripts\activate.bat

cd c:\workubit\microbit-hello-world
mbed add http://os.mbed.com/teams/Lancaster-University/code/mbed-src/
mbed compile -m NRF51_MICROBIT -t GCC_ARM

image.png

algorithm not found

When I compile it again, I get a new error saying that'algorithm' cannot be found. This is because BLE.h and ble.h cannot be distinguished on Windows OS and the compiler cannot search the header file correctly.

image.png

Therefore, the line that refers to the lowercase ble.h header file is explicitly added as headers/ble.h and the parent folder is added so that it can be distinguished from BLE.h.

Again, create a sdk_ble_h.patch file and run patch on MINGW64 (Git Bash).

sdk_ble_h.patch


--- microbit\microbit-dal\nRF51822/nrf51-sdk/source/nordic_sdk/components/ble/ble_services/ble_dfu/ble_dfu.h
+++ microbit\microbit-dal\nRF51822/nrf51-sdk/source/nordic_sdk/components/ble/ble_services/ble_dfu/ble_dfu.h
@@ -53 +53 @@
-#include "ble.h"
+#include "headers/ble.h"
--- microbit\microbit-dal\nRF51822/nrf51-sdk/source/nordic_sdk/components/ble/common/ble_advdata.h
+++ microbit\microbit-dal\nRF51822/nrf51-sdk/source/nordic_sdk/components/ble/common/ble_advdata.h
@@ -48 +48 @@
-#include "ble.h"
+#include "headers/ble.h"
--- microbit\microbit-dal\nRF51822/nrf51-sdk/source/nordic_sdk/components/ble/common/ble_conn_params.h
+++ microbit\microbit-dal\nRF51822/nrf51-sdk/source/nordic_sdk/components/ble/common/ble_conn_params.h
@@ -45 +45 @@
-#include "ble.h"
+#include "headers/ble.h"
--- microbit\microbit-dal\nRF51822/nrf51-sdk/source/nordic_sdk/components/ble/common/ble_conn_state.c
+++ microbit\microbit-dal\nRF51822/nrf51-sdk/source/nordic_sdk/components/ble/common/ble_conn_state.c
@@ -37 +37 @@
-#include "ble.h"
+#include "headers/ble.h"
--- microbit\microbit-dal\nRF51822/nrf51-sdk/source/nordic_sdk/components/ble/common/ble_conn_state.h
+++ microbit\microbit-dal\nRF51822/nrf51-sdk/source/nordic_sdk/components/ble/common/ble_conn_state.h
@@ -65 +65 @@
-#include "ble.h"
+#include "headers/ble.h"
--- microbit\microbit-dal\nRF51822/nrf51-sdk/source/nordic_sdk/components/ble/common/ble_gatt_db.h
+++ microbit\microbit-dal\nRF51822/nrf51-sdk/source/nordic_sdk/components/ble/common/ble_gatt_db.h
@@ -38 +38 @@
-#include "ble.h"
+#include "headers/ble.h"
--- microbit\microbit-dal\nRF51822/nrf51-sdk/source/nordic_sdk/components/ble/common/ble_srv_common.c
+++ microbit\microbit-dal\nRF51822/nrf51-sdk/source/nordic_sdk/components/ble/common/ble_srv_common.c
@@ -42 +42 @@
-#include "ble.h"
+#include "headers/ble.h"
--- microbit\microbit-dal\nRF51822/nrf51-sdk/source/nordic_sdk/components/ble/common/ble_srv_common.h
+++ microbit\microbit-dal\nRF51822/nrf51-sdk/source/nordic_sdk/components/ble/common/ble_srv_common.h
@@ -48 +48 @@
-#include "ble.h"
+#include "headers/ble.h"
--- microbit\microbit-dal\nRF51822/nrf51-sdk/source/nordic_sdk/components/ble/device_manager/device_manager.h
+++ microbit\microbit-dal\nRF51822/nrf51-sdk/source/nordic_sdk/components/ble/device_manager/device_manager.h
@@ -91 +91 @@
-#include "ble.h"
+#include "headers/ble.h"
--- microbit\microbit-dal\nRF51822/nrf51-sdk/source/nordic_sdk/components/ble/peer_manager/id_manager.h
+++ microbit\microbit-dal\nRF51822/nrf51-sdk/source/nordic_sdk/components/ble/peer_manager/id_manager.h
@@ -38 +38 @@
-#include "ble.h"
+#include "headers/ble.h"
--- microbit\microbit-dal\nRF51822/nrf51-sdk/source/nordic_sdk/components/softdevice/common/softdevice_handler/ble_stack_handler_types.h
+++ microbit\microbit-dal\nRF51822/nrf51-sdk/source/nordic_sdk/components/softdevice/common/softdevice_handler/ble_stack_handler_types.h
@@ -49 +49 @@
-#include "ble.h"
+#include "headers/ble.h"
--- microbit\microbit-dal\nRF51822/nrf51-sdk/source/nordic_sdk/components/softdevice/common/softdevice_handler/softdevice_handler.c
+++ microbit\microbit-dal\nRF51822/nrf51-sdk/source/nordic_sdk/components/softdevice/common/softdevice_handler/softdevice_handler.c
@@ -47 +47 @@
-    #include "ble.h"
+    #include "headers/ble.h"
--- microbit\microbit-dal\nRF51822/source/btle/custom/custom_helper.h
+++ microbit\microbit-dal\nRF51822/source/btle/custom/custom_helper.h
@@ -21 +21 @@
-#include "ble.h"
+#include "headers/ble.h"
--- microbit\microbit-dal\nRF51822/source/btle/btle.h
+++ microbit\microbit-dal\nRF51822/source/btle/btle.h
@@ -27 +27 @@
-#include "ble.h"
+#include "headers/ble.h"
--- microbit\microbit-dal\nRF51822/source/nRF5xGap.h
+++ microbit\microbit-dal\nRF51822/source/nRF5xGap.h
@@ -38 +38 @@
-#include "ble.h"
+#include "headers/ble.h"
--- microbit\microbit-dal\nRF51822/source/nRF5xGattServer.h
+++ microbit\microbit-dal\nRF51822/source/nRF5xGattServer.h
@@ -23 +23 @@
-#include "ble.h" /* nordic ble */
+#include "headers/ble.h" /* nordic ble */
--- microbit\microbit-dal\nRF51822/source/nRF5xServiceDiscovery.h
+++ microbit\microbit-dal\nRF51822/source/nRF5xServiceDiscovery.h
@@ -24 +24 @@
-#include "ble.h"
+#include "headers/ble.h"
--- microbit\microbit-dal\source/bluetooth/MicroBitBLEManager.cpp
+++ microbit\microbit-dal\source/bluetooth/MicroBitBLEManager.cpp
@@ -43 +43 @@
-#include "ble.h"
+#include "headers/ble.h"

git.bash


cd /c/workubit/microbit-hello-world
patch -p1 < sdk_ble_h.patch

image.png

CortexContextSwitch.s error

If you continue to compile, you will get an error in CortexContextSwitch.s.

command prompt.cmd


C:\workubit\venv\Scripts\activate.bat
cd c:\workubit\microbit-hello-world
mbed compile -m NRF51_MICROBIT -t GCC_ARM

image.png

A CortexContextSwitch.s file for microbit-dal is available, so copy it.

copy /Y .\microbit\microbit-dal\source\asm\CortexContextSwitch.s.gcc .\microbit\microbit-dal\source\asm\CortexContextSwitch.s
mbed compile -m NRF51_MICROBIT -t GCC_ARM

image.png

Oops, I had to delete the mbed library (folder).

image.png

Finally, if the compilation is successful, a HEX format file will be generated, so transfer (copy) this to the micro: bit.

command prompt.cmd


C:\workubit\venv\Scripts\activate.bat
cd c:\workubit\microbit-hello-world
rmdir /s /q  mbed
mbed compile -m NRF51_MICROBIT -t GCC_ARM

copy .\BUILD\NRF51_MICROBIT\GCC_ARM\microbit-hello-world.hex d:\

image.png

If the transfer (copy) of the HEX file to the micro: bit main unit is successful, "HELLO WORLD! :)" will be scrolled once on the LED display.

Build by yourself-microbit_mbed template

Based on the know-how so far, I will create a new project and compile it again.

command prompt.cmd


C:\workubit\venv\Scripts\activate.bat

mbed config --list
REM mbed config --global GCC_ARM_PATH "C:\Program Files (x86)\GNU Tools Arm Embedded\6 2017-q2-update\bin"
REM mbed config --list

cd c:\workubit
mbed new microbit_mbed --create-only --scm=none
cd microbit_mbed

@echo http://mbed.org/users/mbed_official/code/mbed/builds/87f2f5183dfb > mbed.bld
mbed deploy

However, after 20 minutes, the download is not complete and the disk space does not seem to increase. image.png

image.png

This is the very first problem with while data! ='':. Create a patch file and run patch as before.

venv/Lib/site-packages/mbed/mbed_chunk.path


--- ./mbed.py
+++ ./mbed.py
@@ -397 +397 @@
-                    while data != '':
+                    while data != b'':

git.bash


cd /c/workubit/venv/Lib/site-packages/mbed
patch -p1 < mbed_chunk.patch

I think that this has solved (avoided) all the problems, so I will create a new one.

command prompt.cmd


rmdir /s /q c:\workubit\microbit_mbed

C:\workubit\venv\Scripts\activate.bat

mbed config --list
REM mbed config --global GCC_ARM_PATH "C:\Program Files (x86)\GNU Tools Arm Embedded\6 2017-q2-update\bin"
REM mbed config --list

cd c:\workubit
mbed new microbit_mbed --create-only --scm=none
cd microbit_mbed

@echo http://mbed.org/users/mbed_official/code/mbed/builds/87f2f5183dfb > mbed.bld
mbed deploy

rem mbed config root .
mbed target NRF51_MICROBIT
mbed toolchain GCC_ARM

mbed add https://developer.mbed.org/teams/Lancaster-University/code/microbit/
mbed add http://os.mbed.com/teams/Lancaster-University/code/mbed-src/

copy /Y .\microbit\microbit-dal\source\asm\CortexContextSwitch.s.gcc .\microbit\microbit-dal\source\asm\CortexContextSwitch.s

rmdir /s /q mbed

copy /Y C:\workubit\microbit-hello-world\main.cpp .\
copy /Y C:\workubit\microbit-hello-world\sdk_ble_h.patch .\
copy /Y C:\workubit\microbit-hello-world\has_key.patch .\

>>>bash
cd /c/workubit/microbit_mbed
patch -p1 < has_key.patch
patch -p1 < sdk_ble_h.patch
<<<bash

mbed compile

copy .\BUILD\NRF51_MICROBIT\GCC_ARM\microbit_mbed.hex d:\

image.png

This deliverable-completed version

There is a Python package and a GitHub template that uses the know-how gained in this article, so let's use it.

--Python package mbedubitwin10 - https://github.com/jp-rad/mbed-microbit-win10-setup.git --GitHub template mbed-microbit-template - https://github.com/jp-rad/mbed-microbit-template.git

Advance preparation

Please install the following tools (if you are reading from the beginning, you have already installed them).

  1. Git --version 1.9.5 or higher
  2. Mercurial --version 2.2.2 or higher
  3. GNU Arm Embedded Toolchain-6-2017-q2-update
  1. Python3-Add Python 3.x to PATH is optional

Building a virtual environment (virtualenv)

command prompt.cmd


py -3 -m pip install --upgrade virtualenv
mkdir c:\workubit
cd c:\workubit
py -3 -m virtualenv venv
C:\workubit\venv\Scripts\activate.bat

Installation and configuration of Mbed CLI and mbedubitwin10

command prompt.cmd


C:\workubit\venv\Scripts\activate.bat
pip install mbed-cli git+https://github.com/jp-rad/mbed-microbit-win10-setup.git

mbed config --global GCC_ARM_PATH "C:\Program Files (x86)\GNU Tools Arm Embedded\6 2017-q2-update\bin"

Creating a GitHub repository

git clone and compile

command prompt.cmd


C:\workubit\venv\Scripts\activate.bat
cd c:\workubit
git clone <<your repository>>
cd <<your directory>>
mbedubitwin10
mbed compile
copy .\BUILD\NRF51_MICROBIT\GCC_ARM\microbit-mbed.hex d:\

in conclusion

--Examined the problems, solutions and workarounds when using the Mbed CLI in the Python3 environment of Windows 10. --The microbit_blinky sample and the microbit-hello-world sample were compiled, and the HEX format file was transferred (copied) to the micro: bit main unit. --The mbed new command created and compiled a project from scratch. --We have confirmed that Mbed CLI programming can be done more easily using Python packages and GitHub templates.

PS

As of December 31, 2020 (Japan time), when this article was written, the mbed.com service was down overall. It seems better to keep a complete set of offline environments at hand.

image.png

Recommended Posts

BBC micro: bit (V1.5) C/C ++ programming-Mbed CLI offline compilation in Python3 virtual environment on Windows 10
Install python2.7 on windows 32bit environment
Creating a python virtual environment on Windows
Create a Python virtual development environment on Windows
virtual environment in python
Build Python environment on Windows
Build python environment on windows
Create a Python environment for professionals in VS Code on Windows
Build a 64-bit Python 2.7 environment with TDM-GCC and MinGW-w64 on Windows 7
Python environment construction memo on Windows 10
Installing Kivy on Windows10 64bit Python3.5
Put MeCab in "Windows 10; Python3.5 (64bit)"
Installing Kivy-Designer on Windows10 64bit Python3.5
Install Python development environment on Windows 10
Unable to import packages installed in virtual environment with Anaconda on Windows 10
Steps to create a Python virtual environment with VS Code on Windows
Python 2.7, 3.4, 3.5 extension module build environment on Windows
Using venv in Windows + Docker environment [Python]
Python virtual environment and packages on Ubuntu
Python garbled in Windows + Git Bash environment
I want to use Python in the environment of pyenv + pipenv on Windows 10
Ubuntu18.04.05 Creating a python virtual environment in LTS
Simply build a Python 3 execution environment on Windows
Install python package in personal environment on Ubuntu
Python (Windows 10) Virtual Environment / Package with VS Code
Create an environment of 64bit Windows + python 2.7 + MeCab 0.996
Create a virtual environment with conda in Python
Install the python package in an offline environment
Install Python 3.5.1 + numpy + scipy + α in Windows environment
[Venv] Create a python virtual environment on Ubuntu
[Node-RED] Execute Python on Anaconda virtual environment from Node-RED [Anaconda] [Python]
Work in a virtual environment with Python virtualenv.
Use jupyter-lab installed in python virtual environment (venv)