Learning MicroPython - Part 1

Learning MicroPython - Part 1

MicroPython is a variant of Python. There are several functions, class libraries built (optimized to run on microcontrollers) into MicroPython which are to be understood so that they can be used while writing the application code. Each of these functions are not available for all the MicroPython ports to various systems. Studying the documentation and understanding the same is very critical while porting any microcontroller. MicroPython has an official board pyboard based on STM32F405RG, so, the best way to learn MicroPython is to order this board and start with it. There are other boards like WiPy, ESP8266, Nucleo-F411RE board, Espruino Pico. Check the below page from MicroPython which lists the boards.


When you download the MicroPython source code (version 1.11) from the link mentioned in our last article, you see the below file structure from that download.


While the list of folders and files look huge, a kick off to the things gives you a feel that it is not so difficult. Moreover, MicroPython can be treated as a small operating system that shall be ported to microcontroller. Now after reading this, we get a feel that this is similar to the RTOS porting what we do and exactly that is what we do. So, if you have worked on RTOS porting you should find this process very easy. Now it is important that we understand the folders and underlying files to start our porting. When you go through above libraries, a differentiation between the standard Python and MicroPython has been made by prefixing MicroPython modules with 'u'.

To find the documentation to the libraries which are used, check the below path.

micropython-1.11\docs\library

When you check the above path, you see all the standard python and MicoPython libraries details as below.



The documentation to libraries specific to pyboard (Ex: pyb.ADC.rst) and ESP32 (Ex: ESP32.rst) could also be seen in the above list.

You can import actual libraries into your code using the "import" keyword

import machine
import array
....

When you open any code example, you see the above declaration which meant the functions specific to those modules shall be imported.

What is .rst file in Python?
.rst refers to reStructuredText. It is a file format for textual data used primarily in the Python programming language community for technical documentation. 

Post a Comment

0 Comments