I2CTarget, improved floats and native emitter, STM32N6 & ESP32C2 support
This release of MicroPython sees the introduction of machine.I2CTarget, which allows Python code to implement an I2C target device. It's available on the alif, esp32, mimxrt, rp2, samd, stm32 and zephyr ports. In the simplest case it can create an I2C register/memory device that connects to a bytearray (or similar buffer) and allows an I2C controller to read from and write into that bytearray. For more complex scenarios the I2CTarget class exposes a set of interrupts which can be acted upon by Python code, allowing an arbitrary I2C device to be created. See the documentation for more details and examples.
Floating point support has been improved in a few ways in this release. First, the formatting (printing) of floats has been rewritten to significantly improve the repr reversibility of floating-point numbers. That is, formatting and then re-parsing a float should return the original value. In MicroPython the percentage of floats that correctly repr and parse back was around 28% (single precision) and 38% (double precision), but has now been improved (in the standard build configuration) to 98.5% and 99.8% (single and double respectively). In addition to printing out more accurate floats, that helps a lot when saving floats to .mpy files, because it means they can be loaded back to the same value.
Second, there is now support in the compiler for handling floats as constants. Floats can now be folded (along with integers) in arithmetic operations, and be part of const assignments. This helps to optimize bytecode because constant float expressions can now be evaluated in the compiler instead of at runtime.
The third float improvement involves object representation C, where floats are stored within the immediate object value rather than on the heap. In this object module, the float can only take up 30 bits, so the last two bits of the single precision number are lost. Previously they were just truncated, but now a heuristic is applied when recovering the float value from the object such that the last two bits are copied from the previous two. This alleviates a bias towards zero and in general improves floating point calculations when using this representation.
The native and viper emitter backends have been improved to emit more optimal machine code, for example to use more compact instructions in loads and stores. This is for all supported architectures: ARM, Thumb, Xtensa, RISC-V 32, x86 and x64. Thumb v1 (for example RP2040) now supports long jumps greater than 12 bits, allowing larger Python functions to be compiled to this native architecture. Also, the inline Xtensa assembler now implements most of the LX3 opcodes, with additions including addx2, subx2, ssl and ssr, among others.
Two new MCUs are supported by this release: STM32N6xx and ESP32-C2 (aka ESP8684). The STM32N6xx is a new STMicroelectronics MCU running at 800MHz with a large amount of RAM and machine-learning accelerators. MicroPython now supports this MCU with USB, XSPI memory-mapped external flash, a filesystem, basic peripherals and deepsleep. The ESP32-C2 is by Espressif and is a small and low cost RISC-V MCU with WiFi and BLE. MicroPython running on this MCU supports a REPL, filesystem, GPIO, I2C, ADC, PWM, timers, WiFi and BLE.
The date range supported by the time module has now been standardized to work the same across all platforms. The functions time(), localtime() and mktime() now work properly on a reasonable range of dates, from at least 1970 up to 2099, regardless of the Epoch used by the platform.
The MicroPython virtual machine is now able to avoid heap-allocating slices when subscripting bytearray and memoryview objects, for example expressions like bytearray_obj[a:b] = c. The slice object is now allocated on the C stack, helping to reduce memory churn and allowing such expressions to work when the heap is locked, for example during a hard interrupt handler. This improvement goes in the general direction of MicroPython using the heap as little as possible, making execution more deterministic.
Other improvements to the core runtime include support for using __all__ in star imports, support for PEP487's __set_name__ special method, support for the _b/o/x specifier in str.format, and arrays can now be extended from any iterable, not just from an object with the buffer protocol. A new MicroPython-specific sys.implementation._thread attribute has been added, which exists when threading is enabled and tells which threading model is used (GIL or unsafe/no-GIL).
The framebuf module now supports blit'ing read-only data to a FrameBuffer, which helps when implementing custom fonts that can now be stored in ROM. The DTLS implementation in the tls module now enables DTLS HelloVerify and Anti Replay protection (for the mbedTLS backend), allowing proper DTLS servers to be implemented. The lwIP socket layer now has a queue of incoming UDP and raw packets (previously it only had room for one outstanding packet), allowing for more robust and efficient UDP protocols.
Mpremote has had a few improvements. There is a new fs tree command which mimics the Unix tree command, and includes the -s and -h options to show the file sizes in the output. The df command has been enhanced to use the new no-argument vfs.mount() query, and shows a much better summary of the mounted filesystems. The mip install command now uses hashes to skip files that exist. The location of the user config.py file is now more portable across many different OSes and configuration styles, thanks to the use of the platformdirs helper package. There is also support for targets without the errno module, improved disconnect handling, and better ESP-device detection for USB-CDC ports.
The mpy_ld.py linker can now resolve fixed-address symbols if requested, for example on ESP8266 it can link to ROM-provided functions. There is also now support for ABS32 text relocations on ARMv6M (RP2040). These improvements extend the set of C extensions that can be compiled for such targets.
Libraries that have been updated in this release include: lwIP updated to STABLE-2_2_1_RELEASE, LittleFS updated to v2.11, libhydrogen updated to the latest release, and stm32lib updated to include support for STM32N6.
There have been many improvements to the test suite, to both the tests and the test runners. This is in part to aid the new Octoprobe test framework, which provides hardware-in-the-loop testing. The continuous integration testing now also includes an undefined behaviour sanitizer (UBSan) build, an address sanitizer (ASan) build, a long-long unix variant, and also testing of object representation C. MicroPython has a lot of tests!
The alif port now supports pin interrupts, has improved SPI transfers, and I2C configuration now allows changing the SCL and SDA pins.
The esp32 port is updated to use ESP-IDF v5.4.2, and now supports ESP32-C2. Most ESP32-based boards now auto-detect the size of their flash on boot and automatically create an appropriate vfs partition based on the size of the flash. This allows the same firmware image to work on boards with various flash sizes. This port has also added a new esp32.PCNT class along with machine.Counter and machine.Encoder, which can count input edges, including motor rotation. The PWM class has been improved, and its API now matches other ports. There is support for the LAN8670 PHY, and a new esp32.idf_task_info() function (useful with the new utop package in micropython-lib). The UART.sendbreak() method has been rewritten so that it doesn't reconfigure the UART during its execution.
The nrf port has had fixes and improvements to its UART REPL to make it more robust, and work with mpremote. It also now uses the correct iRAM address for native code execution on nRF52 MCUs. The nrf implementation of machine.enable_irq() and machine.disable_irq() has been reworked so it uses the code common with all other ports, and this is a breaking change (on nrf boards only) due to the signature change of enable_irq(). Previously the signature was enable_irq(), and now the signature matches other ports, and the docs, and is enable_irq(state), where state is the return value from disable_irq().
The rp2 port now enables compressed error messages by default, which reduces firmware size by about 3000 bytes. The pico-sdk alarm pool is now used (again, instead of custom soft-time code) for power-saving delays, and lightsleep has been improved. Open drain mode has been fixed on RP2350 with more than 32 GPIOs, and support for hard IRQ timer callbacks has been added.
The webassembly port has seen improvements to its FFI interface with JavaScript: it improves "has" and "get" proxying, fixes binding of self to JavaScript methods, implements equality for JsProxy objects, and reuses JsProxy references when possible to improve equality relationships of JavaScript objects on the Python side.
The zephyr port has been updated to use Zephyr v4.0.0, and many improvements have been made: PWM support has been added, UARTs are now interrupt driven with ring-buffers and can set the baudrate and other parameters, GPIO supports open-drain mode, SoftI2C and SoftSPI have been enabled, and the zephyr.FlashArea class now contains constants which enumerate the available partitions. REPL reliability has also been improved and it now supports ctrl-C in the default configuration. BLE can now create services at runtime using the standard BLE.register_services() method. Other small things have been added to make the zephyr port match the behaviour of other bare-metal ports, including: boot.py and main.py are now executed at start-up, /lib is added to sys.path as appropriate, stdin/out/err have been enabled in the sys module, along with the ability to import .mpy files, and await/async keywords are enabled. This port now uses the standardized ROM configuration levels, with the default being the
"basic" level.
New boards added in this release are: GARATRONIC_PYBSTICK26_ESP32C3 and SPARKFUN_IOT_REDBOARD_ESP32 (esp32 port), SPARKFUN_REDBOARD_TURBO and SPARKFUN_SAMD21_DEV_BREAKOUT (samd port), NUCLEO_N657X0 and OPENMV_N6 (stm32 port), beagleplay_cc1352p7, nrf5340dk, nrf9151dk and rpi_pico (zephyr port).
The change in code size since the previous release for select builds of various ports is (absolute and percentage change in the text section):
The leading causes of these changes in code size are:
bare-arm, minimal: various code size improvements including optimized integer var-arg handling
unix: remove static PATH variable (bss size reduction), implement DTLS HelloVerify and Anti Replay protection (size increase)
stm32: implement machine.I2CTarget, improve accuracy of float formatting and range of time functions, improve native emitter and inline assembler, update LittleFS to v2.11.
cc3200: enable io.IOBase at core feature level, avoid heap-allocating slices when subscripting built-ins.
esp8266: add more LX3 opcodes to the inline assembler, update LittleFS to v2.11, native emitter improvements, add float constant folding.
esp32: update to IDF 5.4.2 (+10k), implement machine.I2CTarget.
mimxrt: implement machine.I2CTarget, improve accuracy of float formatting, improve native emitter, add float constant folding, update LittleFS to v2.11.
Thanks to everyone who contributed to this release: Alessandro Gatti, Andrea Giammarchi, Andrew Leech, Angus Gratton, Anson Mansfield, Anton Blanchard, Ayush Singh, Chris Webb, Christian Lang, Damien George, Daniel Campora, Daniël van de Giessen, David Schneider, David Yang, Detlev Zundel, Dryw Wade, dubiousjim, Elvis Pfutzenreuter, ennyKey, Garatronic, Herwin Grobben, iabdalkader, IhorNehrutsa, Jeff Epler, Jim Mussared, Jonathan Hogg, Jos Verlinde, Koudai Aono, Malcolm McKellips, Matt Trentini, Maureen Helm, Meir Armon, Patrick Joy, Peter Harper, Phil Howard, purewack, Rick Sorensen, robert-hh, root, SiZiOUS, stijn, TianShuang Ke, Vdragon, Yanfeng Liu, Yoctopuce dev, Yuuki NAGAO.
MicroPython is a global Open Source project, and contributions were made from the following timezones: -0600, -0500, -0400, -0300, +0000, +0100, +0200, +0300, +0530, +0800, +0900, +1000, +1100.
The work done in this release was funded in part through GitHub Sponsors, and in part by George Robotics, Espressif, Arduino, OpenMV, Yoctopuce, Microbric and Planet Innovation.
What follows is a detailed list of changes, generated from the git commit history, and organized into sections.
Main components
all:
rename the "NORETURN" macro to "MP_NORETURN"
bump Ruff version to v0.11.6
go back to using default ruff quote style
py core:
objrange: match CPython range slicing
objstr: fix handling of OP_MODULO with namedtuple
dynruntime.mk: fix use of musl's libm.a when LINK_RUNTIME=1
mkrules.cmake: add CMake support for compressed error messages
make struct-initializing macros compatible with C++
malloc: add mutex for tracked allocations
modthread: initialize thread state nlr_top to NULL
makeversionhdr.py: change utcfromtimestamp() to fromtimestamp()
mpconfig: enable io.IOBase at core feature level
emitinlinethumb: refactor string literal as array initializer
objstr: add support for the :_b/o/x specifier in str.format
emitnative: improve Viper register-indexed code for Arm
emitnative: improve Viper register-indexed code for Thumb