Bringing MicroPython on NuttX

MicroPython is a lean and fast implementation of the Python3 programming language that is optimized to run on a microcontroller. In this article will show, the steps to compile MicroPython as a built-in app for NuttShell on NuttX over ZKit-ARM-1769.

Prerequisites

The article Building NuttX for ZKit-ARM-1769 shows how to set up the environment for building NuttX. The same environment is required for bringing up MicroPython on NuttShell.

Download the following files from the NuttX SourceForge Download page and follow the steps from the above article.

  • nuttx-7.12.tar.gz - contains NuttX RTOS

  • apps-7.12.tar.gz - contains sample applications

Toolchain

The toolchain needs to be setup in the host system that builds the NuttX along with MicroPython. The toolchain used is available here.

  • arm-2014.05-28-arm-none-eabi-i686-pc-linux-gnu.tar.bz2 - toolchain.

The above toolchain requires the 32-bit libraries to be installed.

# dpkg --add-architecture i386
# apt-get update
# apt-get install libstdc++6:i386 libgcc1:i386 zlib1g:i386 libncurses5:i386
$ tar -xf arm-2014.05-28-arm-none-eabi-i686-pc-linux-gnu.tar.bz2

The above command will create a directory arm-2014.05. This directory has to be added to the PATH variable.

$ export PATH=<path/to/>arm-2014.05/bin:$PATH

Configuring NuttShell on NuttX

The folder configs/zkit-arm-1769 corresponds to our target board. Pre-canned NuttX configurations to run applications are available under the sub-folders here. The configuration for NuttShell is available under nsh directory.

NuttX can be built for a specific test application by selecting one of the above pre-canned configurations. A script called configure.sh is available for this purpose, under nuttx/tools. The script has to be invoked with the board name and test application name as argument, as shown below.

# Make sure you are in nuttx-dev/nuttx
$ pushd tools
$ ./configure.sh zkit-arm-1769/nsh
$ popd

MicroPython as Built-in application

MicroPython is available as an interpreter in NuttX. The location is apps/interpreters.

Before invoking make, we will have to configure MicroPython as built-in application. The following configuration variables are to be verified or added in the .config,

CONFIG_BUILTIN=y
CONFIG_NSH_BUILTIN_APPS=y
CONFIG_LIBM=y
CONFIG_INTERPRETERS_MICROPYTHON=y
CONFIG_INTERPRETERS_MICROPYTHON_URL="https://github.com/micropython/micropython/archive"
CONFIG_INTERPRETERS_MICROPYTHON_VERSION="1.3.8"
CONFIG_INTERPRETERS_MICROPYTHON_APPNAME="python"
CONFIG_INTERPRETERS_MICROPYTHON_STACKSIZE=2048
CONFIG_INTERPRETERS_MICROPYTHON_PRIORITY=100

The steps to build are the same. Invoke make and use CodeSourcery toolchain, by setting CONFIG_ARMV7M_TOOLCHAIN_CODESOURCERYL to y, and disable the Buildroot toolchain, by setting CONFIG_ARMV7M_TOOLCHAIN_BUILDROOT to n.

$ make CONFIG_ARMV7M_TOOLCHAIN_CODESOURCERYL=y CONFIG_ARMV7M_TOOLCHAIN_BUILDROOT=n

Finally, we get nuttx.hex, that can be flashed on to the board. The board prompts into nsh>. Issuing help on this prompt will show python as built-in app as shown below.

NuttShell (NSH) NuttX-7.12
nsh> ?
help usage: help [-v] [<cmd>]

  [         echo        ifdown      mkrd        pwd     umount
  ?         exec        ifup        mh          rm      unset
  break     exit        kill        mount       rmdir   usleep
  cat       false       losetup     mv          set     wget
  cd        free        ls          mw          sh      xd
  cp        get         mb          nslookup    sleep
  cmp       help        mkdir       ping        test
  dd        hexdump     mkfatfs     ps          true
  df        ifconfig    mkfifo      put         uname

Builtin Apps:
  python
nsh>
nsh> python
micropython
Micro Python  on 2015-12-25
Type "help()" for more information.
>>>