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.
--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.
basestring
abstract type is obsolete (b''! =''
is now true)
→ https://docs.python.org/3.1/whatsnew/3.0.html#text-vs-data-instead-of-unicode-vs-8-bitdict.has_key ()
is obsolete
→ https://docs.python.org/3.1/whatsnew/3.0.html#builtinsWhen 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'
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.
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
Please install the following three tools.
Add Python 3.9 to PATH
and install with Install Now
.
py -3 -m pip install --upgrade virtualenv
to install virtualenv.
mkdir c: \ workubit
command, move to the created folder, and create avenv
virtual environment (folder).
Command: py -3 -m virtualenv venv
C: \ workubit \ venv \ Scripts \ activate.bat
.
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
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
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
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.
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.
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
command prompt.cmd
C:\workubit\venv\Scripts\activate.bat
cd c:\workubit\microbit_blinky
mbed compile -m NRF51_MICROBIT -t GCC_ARM
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.
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
The error continues.
This is probably due to confusion with the existing mbed
library.
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
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:\
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.
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
When you try to compile, you will be prompted to run the mbed deploy
command to install the dependent modules and tools.
When I run the mbed deploy
command as follows, I get a message that the tool cannot be found.
command prompt.cmd
mbed deploy
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
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
.
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
In addition, I get a different error, which can be resolved by adding the mbed-src
library.
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
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.
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
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
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
Oops, I had to delete the mbed
library (folder).
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:\
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.
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.
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:\
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
Please install the following tools (if you are reading from the beginning, you have already installed them).
Add Python 3.x to PATH
is optionalcommand 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
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"
Use this template
- https://github.com/jp-rad/mbed-microbit-template/generatecommand 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:\
--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.
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.
Recommended Posts