We will use PolyMCU as a framework to port new platforms to MicroPython.
We will use Nordic nRF52 DK board for our initial port. This board has plenty of flash memory (512kB) and RAM (64kB). We actually use the Nordic nRF52 Preview DK that has half of the RAM memory but it should still be fine for MicroPython.
The MicroPython port started by integrating the minimal MicroPython firmware into PolyMCU framework as a new PolyMCU application Github commit
Then we created a new PolyMCU library in Lib/MicroPython
(Github commit). Its initial CMake file CMakeLists.txt
clone MicroPython into PolyMCU and build core MicroPython as a static library.
We also created the MicroPython configuration files related to our port Lib/MicroPython/mpconfigport.h
and Lib/MicroPython/mphalport.h
and the adding support for the standard input and output based on CMSIS UART driver Lib/MicroPython/uart_core.c
.
We know we might need some platform specific definitions at some point. So we introduce them now in Github commit.
MicroPython extracts the Python strings at build time – this process is called string interning. In MicroPython, this interned string is known as a qstr
. The QSTR extraction support is added in this Github commit.
One of the most important module of MicroPython is pyb
. This module adds a Python API to communicate with the micro-controller. For instance, there is an interface to turn on and off the board LEDs. There is a Python interface for UART, I2C, SPI and GPIO. Here is the commit that adds the initial pyb
support: Github commit.
But pyb
module cannot discover the board support by itself. We need to connect its interface to the actual board features. Here is the example of adding:
At this stage, we had the Python command line interpreter:
>>> 1+1
2
>>> print(“Hello World!”)
Hello World!
We were able to turn on and off the LEDs:
>>> import pyb
>>> led = pyb.LED(1)
>>> led.on()
>>> led.off()
Testing GPIO with the Nordic nRF52 DK buttons (The buttons 1 to 4 are connected to P0.13 – P0.16 pins):
>>> import pyb
>>> dir(pyb.Pin.cpu)
>>> pin = pyb.Pin(pyb.Pin.cpu.P13, pyb.Pin.IN, pull=pyb.Pin.PULL_UP)
>>> print(pin.value())
1
>>> print(pin.value()) # While pressing Button 1
0
And even communication with SparkFun Digital Temperature Sensor Breakout – TMP102 through I2C
>>> from pyb import I2C
>>> i2c = I2C(1, I2C.MASTER)
>>> i2c.send(0x00, addr=0x48)
>>> data = [0, 0]
>>> data = bytearray(2)
>>> i2c.recv(data, addr=0x48)
>>> print(((data[0] << 4) | (data[1] >> 4)) * 0.0625)
21.5
>>> i2c.deinit()
The last part of the exercise was to get the Python command interpreter through NUS (Nordic UART Service). So you could play with the Python remotely from your computer through a bluetooth. The code is here: Github commit
And it works!
git clone https://github.com/labapart/polymcu.git
mkdir Build && cd Build
export CROSS_COMPILE=~/Toolchains/gcc-arm-none-eabi-5_4-2016q3/bin/arm-none-eabi-
cmake -DAPPLICATION=LabAPart/MicroPython -DBOARD=Nordic/nRF52DK ..
make
And deploy with sudo make install
cmake -DAPPLICATION=LabAPart/MicroPython -DBOARD=Nordic/nRF52DK -DNORDIC_NUS=1 ..
make
No comment yet