After introducing the SAM-D21 microcontroller board as shown in Figure 1 and using the ADC/DAC of this chip that is a 32-bit ARM Cortex-M0+ architecture microcontroller, this time we try to compile and use Python.

Figure 1 SAM-D21

Steps

In this experiment, Linux Mint was used, and the pre-compilation prerequisite is the ARM compiler, which can be installed with the following command.

sudo apt install gcc-arm-none-eabi

Download

Download the MicroPython source file from github with the following command.

git clone --recurse-submodules https://github.com/micropython/micropython.git

An example of the result of the work is shown in Figure 2.

git clone --recurse-submodules https://github.com/micropython/micropython.git
Figure 2 Example result from git clone command

Once the download is complete, use the command to move into the micropython folder as follows:

cd ~/micropython

Compile mpy-cross

MicroPython python compiler mpy-cross provides bytecode with the extension .mpy which is useful in making the code smaller and easier to implement due to saving ROM/RAM space. It also requires mpy-cross to be compiled in MicroPython itself, so what needs to be done in step 2 is to compile mpy-cross as a tool for compile MicroPython to another layer by going to mpy-xxx and then do the following make:

cd mpy-cross

make clean

make

Once compiled, you will find a file named mpy-cross.

Complie submodules ของ sam-d21

Once mpy-cross is in place, the next step is to create a library that will be invoked with sam-d21 by accessing samd’s ports like this:

cd ~/micropython/ports/samd

After that, compile with the following command

make  clean
make  submodules

Compile MicroPython

The command for compiling MicroPython for use with the SAM-D21 is to set the board to be ADAFRUIT_TRINKET_M0 as follows:

make BOARD=ADAFRUIT_TRINKET_M0

When the compilation is finished without any errors related to arm-none-eabi-gcc or forget mpy-cross you get a folder named build-ADAFRUIT_TRINKET_M0

Upload

Once compiled, the next step is to upload it to the board by dragging the firmware.uf2 file into the board folder by changing the mode to DFU by pressing Reset twice in a row. Once the file has been thrown, the board will upload to the ROM and reset itself. That’s it, making it available to MicroPython.

Testing

Example of a program to read data from the board. can be written as follows

##################################################################################
import gc
import os
import sys
import time
import machine as mc

gc.enable()
gc.collect()

stat = os.statvfs('/flash')
block_size = stat[0]
total_blocks = stat[2]
free_blocks  = stat[3]
rom_total = (total_blocks * block_size)
rom_free = (free_blocks * block_size)
rom_usage = (rom_total-rom_free)

print("Platform .......: {}".format(sys.platform))
print("Frequency ......: {}".format(mc.freq()))
print("ID .............: {}".format(mc.unique_id()))
print("Memoey")
print("    Total ......: {} bytes".format(gc.mem_alloc()+gc.mem_free()))
print("    Used .......: {} bytes".format(gc.mem_alloc()))
print("    Free .......: {} bytes".format(gc.mem_free()))
print("Flash ROM")
print("    Total ......: {} bytes".format(rom_total))
print("    Used .......: {} bytes".format(rom_usage))
print("    Free .......: {} bytes".format(rom_free))

An example of the result of the work is shown in Figure 3.

Figure 3 An example of the program output.

Conclusion

From this article, it is easy to build a MicroPython for use with a SAM-D1 microcontroller and easy to install via using the UF2 boot loader. It uses Python like any other microcontroller, but the pins and functions are different. Finally, have fun with programming.

Reference

  1. MicroPython

(C) 2020-2022, By Jarut Busarathid and Danai Jedsadathitikul
Updated 2022-01-30