Port of MicroPython
Available standart modules
_thread
– Low-level threading APIarray
– Efficient arrays of numeric valuesasyncio
– Asynchronous I/Obinascii
– Convert between binary and ASCIIbuiltins
– Built-in objectscmath
– Mathematical functions for complex numberscollections
– Container datatypesdeflate
– deflate compression & decompressionerrno
– Standart errno system symbolsgc
– Garbage Collector interfacehashlib
– Secure hashes and message digestsheapq
– Heap queue algorithmio
– Core tools for working with streamsjson
– JSON encoder and decodermachine
– functions related to the hardwaremath
– Mathematical functionsmicropython
– Access and control MicroPython internalsos
– Miscellaneous operation system interfacespyb
– Functions related to the boardrandom
– Generate pseudo-random numbersre
– Regular expression operationsselect
– Waiting for I/O completionstruct
– Interpret bytes as packed binary datasys
– System-specific parameters and functionstime
– 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. Thepyb.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 callbackfun
.
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()
.
- 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()
. Thecallback
function must accept 5 parameters:CAN object
,data
,message id
, TX state andunique number
. The unique number is returned by theNMEA2000.send()
method and can be used to track message delivery.
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
andrx_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.
- 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.
- 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.
- 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 ortimeout
has elapsed. If both conditions are set andtimeout
occurs before theend
character is read, theforce
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 whenmaxlen
is reached and/or terminator is not received. Otherwise the received data is discarded.