sábado, 19 de agosto de 2017

An introduction to MicroPython

eferemail
shared this article with you from Inoreader

The MicroPython project is an open source implementation of Python 3 that includes a small subset of Python standard libraries, and is optimised to run on microcontrollers with constrained environments like limited ROM, RAM and processing power. It came about after a successful Kickstarter campaign by Damien George.

MicroPython Kickstarter campaign
Fig. 1: MicroPython Kickstarter campaign

What MicroPython is

MicroPython includes the standard Python interpreter that runs on small embedded development boards. With MicroPython you can write Python scripts to control hardware. For example, you can make LEDs blink, communicate with a temperature sensor, control motors and publish a sensor reading on the Internet. It is worth noting that the firmware for such embedded devices is generally coded in Assembly, C or C++, but by using MicroPython you can achieve almost the same results as with high-level Python.

What MicroPython is not

Unlike the desktop version of Python, MicroPython is a stripped-down version for microcontrollers; therefore it does not support all Python libraries and functionalities. In the world of microcontrollers, the firmware is directly burned to the ROM (also known as program memory), and there is no file system. MicroPython implements a minimal type of file system directly on the flash memory of the microcontrollers. If the device has 1MB or more of storage, then it will be set up (upon first boot) to contain a file system. This file system uses FAT format and is stored in the flash memory by MicroPython firmware. This gives you the benefit of being able to access, read and write files within the main Python program for flexible storage operations.

There are two files that are treated specially by ESP8266 when it starts up: boot.py and main.py. The former script is executed first (if it exists), and once it completes, the latter script is executed. You can create these files yourself and populate these with the code that you want to run when the device starts up. boot.py generally contains boot parameters like whether to have serial debugging on or off, which Wi-Fi AP to connect, main code to run and so on. Looping stays in main.py, for example, the logic for blinking LEDs.

It is also worth noting that microcontrollers are generally programmed in C, wherein direct registers are accessed and manipulated to use the peripherals of microcontrollers.Firmware code is cross-compiled and built for the target microcontroller architecture, and is flashed using a suitable programmer. MicroPython abstracts all these steps, but the constraint here is the real-time feature of microcontrollers, in general. Hence, MicroPython is not suitable for strict real-time applications.

Boards supported by MicroPython

There are numerous boards/chipsets supported by MicroPython, and more are expected to do so in the near future as the project matures. The current board support list is given below.

pyboard.

This is the official MicroPython board, which comes with full MicroPython support out-of-the-box. It features STM32F405RGT6 ARM Cortex M4 based processor. MicroPython supports APIs for almost all hardware peripherals of the chip like GPIO, I2C, SPI and UART.

The pyboard
Fig. 2: The pyboard

ESP8266.

MicroPython support for the low-cost Wi-Fi development ESP8266 microcontroller is excellent. Wi-Fi and Internet accessibility are made available within MicroPython using a special network library. There is also a Web based read-evaluate-print-loop (REPL) that allows you to run MicroPython code on ESP8266 using a Web browser within a local network.

ESP8266 microcontroller chip
Fig. 3: ESP8266 microcontroller chip

BBC micro:bit.

This is a new board aimed at teaching kids computer programming. Using MicroPython on micro:bit, you can access the board's peripherals like LEDs and accelerometers.

BBC micro:bit
Fig. 4: BBC micro:bit

Note.

The full list of supported boards can be found at MicroPython website.

Library stacks supported by MicroPython

Python supports a variety of libraries on desktop computers, but porting these directly to a microcontroller is not an easy task as these are not optimised for running on machines with less RAM. Instead of making existing useful Python libraries available in MicroPython, these are stripped in order to be better optimised to run on constrained hardware. Current libraries supported by MicroPython are named with the initial u, which stands for Micro

Library implementation:
• ucollections – collection and container types
• uhashlib – hashing algorithm
• uheapq – heap queue algorithm
• uio – input/output streams
• ujson – JSON encoding and decoding
• uos – basic operating system services
• ure – regular expressions
• usocket – socket module
• ustruct – pack and unpack primitive data types
• utime – time-related functions
• uzlib – zlib decompression

Functionalities specific to MicroPython implementation are available in the following libraries:
• machine – for functions related to the board
• micropython – to access and control MicroPython internals
• network – for network configuration
• uctypes – to access binary data in a structured way

Following libraries are specific to pyboard:
• pyb – for functions related to the board
• Time-related functions
• Reset-related functions
• Interrupt-related functions

Note.

The full list of supported libraries can be found at MicroPython website.

The post An introduction to MicroPython appeared first on Electronics For You.

View on the web
Inoreader is a light and fast RSS Reader. Follow us on Twitter and Facebook.

No hay comentarios:

Publicar un comentario