External C modules#

The MicroPython interpreter can be extended with custom C modules.

Unix port#

Quick demonstration extending the stock MicroPython Unix port with a custom c module (cexample). Check the MicroPython documentation for details.

First update the local MicroPython clone (assuming you have already cloned the github repo):

%%bash

cd $IOT/mp
if [ ! -d micropython ]
then
    git clone git@github.com:micropython/micropython.git
else
    cd micropython
    git checkout master
    git pull
    git merge master
fi

Now compile the custom MicroPython interpreter:

%%service arm32

cd $IOT/mp/micropython/ports/unix
make clean 
make submodules
make USER_C_MODULES=../../examples/usercmodule

Check it out:

%%bash

cd $IOT/mp/micropython/ports/unix
./micropython -c "import cexample; print(cexample.add_ints(4, -7))"
-3

esp32 port#

%%service esp-idf

cd $IOT/mp/micropython/ports/esp32
make submodules
make clean 
make BOARD=GENERIC_OTA \
     USER_C_MODULES=../../../examples/usercmodule/micropython.cmake \
     deploy