Port of MicroPython

Available standart modules

  • _thread – Low-level threading API

  • array – Efficient arrays of numeric values

  • asyncio – Asynchronous I/O

  • binascii – Convert between binary and ASCII

  • builtins – Built-in objects

  • cmath – Mathematical functions for complex numbers

  • collections – Container datatypes

  • deflate – deflate compression & decompression

  • errno – Standart errno system symbols

  • gc – Garbage Collector interface

  • hashlib – Secure hashes and message digests

  • heapq – Heap queue algorithm

  • io – Core tools for working with streams

  • json – JSON encoder and decoder

  • machine – functions related to the hardware

  • math – Mathematical functions

  • micropython – Access and control MicroPython internals

  • os – Miscellaneous operation system interfaces

  • pyb – Functions related to the board

  • random – Generate pseudo-random numbers

  • re – Regular expression operations

  • select – Waiting for I/O completion

  • struct – Interpret bytes as packed binary data

  • sys – System-specific parameters and functions

  • time – Time access and conversions

Summary of changes

MicroPython runs on microcomputers such as Raspberry Pi, which have gigabytes of RAM, and on microcontrollers, which have less than 100KB of RAM. As a result, the implementation of standard libraries and the number of available modules and methods are different on different platforms. Python Gateway is based on the STM32 (ST Microelectronics chip) and provides 80KB of RAM for the user program.

When working with microcontroller peripherals and interrupts, the most difficult problem is sharing access to objects in memory and hardware. In the Python language, the creation of new objects is implicit, and this can cause serious difficulties for a novice programmer when writing interrupt handlers.

We have added intermediate message queues for FIFO0 and FIFO1 (buffers for receiving CAN messages) and queue for sending CAN messages (pyb.CAN class), and buffers for receiving and sending UART data (pyb.UART class). To handle the queues, including calling the callbacks, we have added a dedicated Python thread. It allows to safely allocate memory and modify objects in callbacks.

Device properties in PGN 126996 and 126998 may contain Unicode strings, so we added UTF-16 support to str.encode() and bytes.decode() methods of the standard library.

Changes to pyb.CAN

New constants

Name

Value

Description

CAN.TX_EMPTY

0

TX mailbox is empty

CAN.TX_PENDING

1

TX mailbox contains frame to send

CAN.TX_DONE

2

Frame sent successfully

CAN.TX_CANCEL

3

Attempt to cancel frame transmission

CAN.TX_ABORT

4

Frame transmission canceled

CAN.TX_ERROR

5

Frame transmission error

Changed methods

pyb.CAN.any(fifo)

Returns True if there is a message waiting in the receive queue or FIFO, False otherwise. When the NMEA2000 object is used, it uses FIFO0 to receive messages with 29-bit message identifiers.

pyb.CAN.recv(fifo, list=None, *, timeout=5000)

First, it reads from the queue, and if the queue is empty, it reads from the FIFO.

pyb.CAN.send(data, id, *, timeout=0, rtr=False, extframe=False, fdf=False, brs=False)

Write frame to transmit queue instead of sending frame to CAN bus.

pyb.CAN.rxcallback(fifo, fun)

When using the NMEA2000 object, the FIFO0 callback is used by the object to receive CAN frames with 29-bit identifiers. The NMEA2000.rxcallback() should be used to process incoming NMEA 2000 messages. The pyb.CAN.rxcallback() can be used in the user program with FIFO1 to receive frames with 11-bit identifiers. It is possible to allocate memory in the callback fun.

New methods

pyb.CAN.test(count=10, timeout=3000)

Receive a specified number of frames to test the CAN bus connection. Print received frames to console in RAW format.

The RAW format is used in Yacht Devices software and hardware products, and is described in Appendix E of the Yacht Devices Wi-Fi Gateway manual. See also: nmea2000.toraw().

Parameters:
  • count (int) – Frames to receive.

  • timeout (int) – Timeout for single frame.

pyb.CAN.txcallback(callback)

Register callback on TX event.

This callback is triggered when the Gateway or the user program has sent a message. The NMEA2000 object does not provide it is own txcallback(). The callback function must accept 5 parameters: CAN object, data, message id, TX state and unique number. The unique number is returned by the NMEA2000.send() method and can be used to track message delivery.

Parameters:

callback (function(pyb.CAN, bytes, int, int, int) or None) – Function to call on TX event.

Changes to pyb.UART

Changed methods

UART.init(baudrate, bits=8, parity=None, stop=1, *, tx_invert=False, rx_invert=False, timeout=0, flow=0, timeout_char=0, read_buf_len=329, write_buf_len=329)

Default value of read_buf_len has been increased to 329, write_buf_len has been added.

The tx_invert and rx_inver parameters have been added. Inverting the transmit or receive lines can be used to connect non-NMEA devices, e.g. devices with TTL logic. The Gateway has two UART ports, one for transmit data and one for receive data. Receive settings have no effect on the transmit port and vice versa.

Parameters:
  • tx_invert (bool) – Invert TX line.

  • rx_invert (bool) – Invert RX line.

  • write_buf_len (int) – Transmit buffer length in characters (0 to disable). For 9-bit characters, two bytes are used per character.

pyb.UART.write(buf)

Write to transmit queue instead of sending directly to UART.

pyb.UART.writechar(char)

Write to transmit queue instead of sending directly to UART.

New methods

pyb.UART.test(count=10, timeout=3000)

Receive specified number of lines and print to console to test UART connection.

Parameters:
  • count (int) – Lines to receive.

  • timeout (int) – Timeout for single line.

pyb.UART.baudrate([baudrate])

Sets or returns the UART baud rate. Note that not all baud rates (that are not a multiple of 300) can be set. Baud rates up to 1Mbps are supported.

Parameters:

baudrate (int) – Baudrate to set.

Returns:

Actual baudrate.

Return type:

int

pyb.UART.rxcallback(callback, maxlen=100, end='\n', strip=False, timeout=0, force=False)

The receive queue is used by the NMEA0183 object, it will stop receiving messages if the callback is redefined in the user program.

UART reads incoming messages until end is received or timeout has elapsed. If both conditions are set and timeout occurs before the end character is read, the force parameter can be used to invoke callback with received data.

Parameters:
  • callback (function(pyb.UART, bytes) or None) – Function to call on data receive. None – disable callback.

  • maxlen (int) – Maximum line length in characters.

  • end (None or bytes or str) – Callback is invoked when this character is received.

  • strip (bool) – Strip whitespace characters from the end of the received message.

  • timeout (int) – Maximum delay between messages or time until received message is discarded. 1-3 millisecond timeouts may not work reliably due to implementation specifics.

  • force (bool) – Force callback call when maxlen is reached and/or terminator is not received. Otherwise the received data is discarded.