Commit graph

17545 commits

Author SHA1 Message Date
Damien George
ac5b1bce99 esp32/machine_i2c: Factor default pin macros to header file.
So the implementation of I2CTarget can use them.

Signed-off-by: Damien George <damien@micropython.org>
2025-08-01 23:03:17 +10:00
robert-hh
79d182deb2 samd/machine_i2c_target: Support I2C target mode.
Supporting readfrom_mem*(). writeto_mem() and a set of IRQs.  Enabled by
default for SAMD51 devices and SAMD21 devices with external flash.

Tested with ItsyBitsy M4 and ItsyBitsy M0 with both on-board SoftI2C and a
RP2 Pico as controller.

Signed-off-by: Damien George <damien@micropython.org>
Signed-off-by: robert-hh <robert@hammelrath.com>
2025-08-01 23:03:17 +10:00
robert-hh
5c78762c16 mimxrt/machine_i2c_target: Support I2C target mode.
The functionality is similar to the RP2 implementation.  The supported
address size is 7 bit.  In order to achieve a sufficient response, the
target I2C IRQ handler has to run from RAM, causing much more code moved to
RAM than required.

Tested with Teensy 4.1, MIMXRT1021EVK, MIMXRT1011EVK and MIMXRT1170, using
both a On-Board SoftI2C as controller and a RP2 Pico as external
controller.

Signed-off-by: Damien George <damien@micropython.org>
Signed-off-by: robert-hh <robert@hammelrath.com>
2025-08-01 23:03:17 +10:00
Damien George
67a442d8fa alif/machine_i2c: Allow changing I2C SCL/SDA pins.
Signed-off-by: Damien George <damien@micropython.org>
2025-08-01 23:03:17 +10:00
Damien George
6e72cae619 alif/machine_i2c_target: Implement I2CTarget class.
Signed-off-by: Damien George <damien@micropython.org>
2025-08-01 23:03:17 +10:00
Damien George
0c50343145 zephyr/machine_i2c_target: Implement I2CTarget class.
Tested and working on rpi_pico and nucleo_wb55rg.

Signed-off-by: Damien George <damien@micropython.org>
2025-08-01 23:03:17 +10:00
Damien George
1839340dda rp2/machine_i2c_target: Implement I2CTarget class.
Signed-off-by: Damien George <damien@micropython.org>
2025-08-01 23:03:17 +10:00
Damien George
56d2b47370 rp2/machine_i2c: Factor default pin macros to header file.
So they can be reused by the I2CTarget implementation.

Signed-off-by: Damien George <damien@micropython.org>
2025-08-01 23:03:17 +10:00
Damien George
01e570a347 stm32/machine_i2c_target: Implement I2CTarget class.
Works, tested on PYBV10, PYBD_SF2 and PYBD_SF6:

    buf = bytearray(16)
    machine.I2CTargetMemory("X", addr=67, mem=buf)

Signed-off-by: Damien George <damien@micropython.org>
2025-08-01 23:03:17 +10:00
Damien George
78d16672e1 stm32/i2cslave: Account for slow addr_match callback.
Signed-off-by: Damien George <damien@micropython.org>
2025-08-01 23:03:17 +10:00
Damien George
2443878bd9 stm32/i2cslave: Support i2c_slave_process_tx_end callback on F4.
The rounds out the F4 implementation to match the other supported MCUs.

Signed-off-by: Damien George <damien@micropython.org>
2025-08-01 23:03:17 +10:00
Damien George
17d0449ac8 stm32/i2cslave: Add functions to read/write I2C data.
Instead of requiring the callback to consume/provide the data.  This allows
the data to be consumed/provided later on, which will stretch the I2C clock
until that occurs.

Signed-off-by: Damien George <damien@micropython.org>
2025-08-01 23:03:17 +10:00
Damien George
a4ca42f094 stm32/i2cslave: Change irq handler name to i2c_slave_irq_handler.
Remove the "ev" part, so this handler can be generalised to also handle
error IRQs.

Signed-off-by: Damien George <damien@micropython.org>
2025-08-01 23:03:17 +10:00
Damien George
9b1778fc77 stm32/i2c: Move I2C IRQ handlers from stm32_it.c to i2c.c.
And add MP_STATIC_ASSERT to statically check that the IRQ names are correct
on the MCU that it's compiled for.

Signed-off-by: Damien George <damien@micropython.org>
2025-08-01 23:03:17 +10:00
Damien George
1b578fe2c0 extmod/machine_i2c_target: Add new machine.I2CTarget class.
This commit implements a generic I2C target/peripheral/"slave" device,
called `machine.I2CTarget`.  It can work in two separate modes:

- A general device with interrupts/events/callbacks for low-level I2C
  operations like address match, read request and stop.

- A memory device that allows reading/writing a specific region of memory
  (or "registers") on the target I2C device.

To make a memory device is very simple:

    from machine import I2CTarget

    mem = bytearray(8)
    i2c = I2CTarget(addr=67, mem=mem)

That's all that's needed to start the I2C target.  From then on it will
respond to any I2C controller on the bus, allowing reads and writes to the
mem bytearray.

It's also possible to register to receive events.  For example to be
notified when the memory is read/written:

    from machine import I2CTarget

    def irq_handler(i2c_target):
        flags = i2c_target.irq().flags()
        if flags & I2CTarget.IRQ_END_READ:
            print("controller read target at addr", i2c_target.memaddr)
        if flags & I2CTarget.IRQ_END_WRITE:
            print("controller wrote target at addr", i2c_target.memaddr)

    mem = bytearray(8)
    i2c = I2CTarget(addr=67, mem=mem)
    i2c.irq(irq_handler)

Instead of a memory device, an arbitrary I2C device can be implemented
using all the events (see docs).

This is based on the discussion in #3935.

Signed-off-by: Damien George <damien@micropython.org>
2025-08-01 23:03:17 +10:00
Koudai Aono
ab7c5a1733 docs/library/btree: Fix method links to explicitly specify class.
So they don't clash with other potential references.

Signed-off-by: Koudai Aono <koxudaxi@gmail.com>
2025-08-01 22:58:26 +10:00
root
769453c750 rp2/rp2_pio: Fix use of PIO2 in prog data structure.
The RP2350 PIO2 State Machines (8, 9, 10, 11) did not work.  The data
structure used to pass the PIO arguments was missing an entry for PIO2,
thus causing the PIO2 instances to write wrong data to wrong locations.

Fixes issue #17509.

Signed-off-by: Matt Westveld <github@intergalacticmicro.com>
2025-08-01 22:38:00 +10:00
Dryw Wade
c9b52b2b7f rp2/CMakeLists.txt: Fix flash size check logic.
Follow up to 6bfb83e30a, if the variable
`PICO_FLASH_SIZE_BYTES` is not a numeric constant, eg "(2 * 1024 * 1024)",
then it won't pass the GREATER check.  So change the if logic to just test
if it's defined.

Signed-off-by: Dryw Wade <dryw.wade@sparkfun.com>
2025-08-01 16:20:49 +10:00
Damien George
a9a606bf5d docs/library/rp2.StateMachine: Add a note about PIO in and jmp pins.
Signed-off-by: Damien George <damien@micropython.org>
2025-08-01 15:10:15 +10:00
Damien George
41987c6cf4 rp2/rp2_pio: Configure jmp_pin for PIO use if it's isolation is set.
Signed-off-by: Damien George <damien@micropython.org>
2025-08-01 15:10:15 +10:00
Jos Verlinde
dea949e860 tools/mpremote: Update ESPxxx detection for USB-CDC ports.
Detection of ESP-XX devices was based on just the names of the USB driver
name, and did not account for the switch of the newer ESP-xx devices to
USB-CDC.  On Windows this caused unwanted/unneeded resets as the DTR/RTS
signals are also used for automatic device reset over USB-CDC.  See
https://github.com/micropython/micropython/issues/9659#issuecomment-3124704572

This commit uses the Espressif registered VID 0x303A to detect USB-CDC
ports, to enable the same DTR/RTS settings as used on the UART-USB
connection.

Also improved the robustness of the code using `getattr()`.

Signed-off-by: Jos Verlinde <Jos_Verlinde@hotmail.com>
2025-08-01 14:58:20 +10:00
Jos Verlinde
4ba626ab5a tools/mpremote: Fix errno.ENOTBLK attribute error on Windows.
Not all errors defined in stdlib errno are available on Windows.
Specifically, errno.ENOTBLK is not.

Fixes issue #17773.

Signed-off-by: Jos Verlinde <Jos_Verlinde@hotmail.com>
2025-08-01 14:52:59 +10:00
Chris Webb
953da2080e tests/micropython: Test that viper offset stores don't clobber base reg.
When running the viper boundary tests, assert that the offset stores don't
clobber the base register, which is saved and temporarily modified on some
architectures.

Signed-off-by: Chris Webb <chris@arachsys.com>
2025-08-01 14:17:49 +10:00
Chris Webb
f39434e9fb py/asmthumb: Don't corrupt base register in large offset store.
asm_thumb_store_reg_reg_offset() modifies the base register when storing
with a large offset which triggers the generic path. If a variable lives
in that register, this corrupts it. Fix this by saving the base register
on the stack before modifying it.

Signed-off-by: Chris Webb <chris@arachsys.com>
2025-08-01 14:15:39 +10:00
Yoctopuce dev
69ead7d98e py/parse: Add support for math module constants and float folding.
Add a new MICROPY_COMP_CONST_FLOAT feature, enabled by in mpy-cross and
when compiling with MICROPY_CONFIG_ROM_LEVEL_CORE_FEATURES.  The new
feature leverages the code of MICROPY_COMP_CONST_FOLDING to support folding
of floating point constants.

If MICROPY_COMP_MODULE_CONST is defined as well, math module constants are
made available at compile time. For example:

    _DEG_TO_GRADIANT = const(math.pi / 180)
    _INVALID_VALUE = const(math.nan)

A few corner cases had to be handled:
- The float const folding code should not fold expressions resulting into
  complex results, as the mpy parser for complex immediates has
  limitations.
- The constant generation code must distinguish between -0.0 and 0.0, which
  are different even if C consider them as ==.

This change removes previous limitations on the use of `const()`
expressions that would result in floating point number, so the test cases
of micropython/const_error have to be updated.

Additional test cases have been added to cover the new repr() code (from a
previous commit).  A few other simple test cases have been added to handle
the use of floats in `const()` expressions, but the float folding code
itself is also tested when running general float test cases, as float
expressions often get resolved at compile-time (with this change).

Signed-off-by: Yoctopuce dev <dev@yoctopuce.com>
2025-08-01 13:35:44 +10:00
SiZiOUS
f67a370311 embed/port: Fix alloca include for Windows platforms.
When building the embedded port on MinGW-w64, I receive the following
error:

    fatal error: alloca.h: No such file or directory

MinGW-w64 (used on MSYS2) doesn't include `alloca.h`, but `alloca()` is
provided via `malloc.h` instead.  And this fix is also needed for other
Windows build systems.

Signed-off-by: SiZiOUS <sizious@gmail.com>
2025-08-01 12:01:37 +10:00
Damien George
f8f6d71940 nrf/drivers/bluetooth: Change soft-device download URL to self hosted.
The existing URLs have started to return a HTTP 403.  The simplest way
around this is to host the files at micropython.org, and point to them from
the download script.

The soft-device files have been retrieved from:
- https://www.nordicsemi.com/Products/Development-software/s110/download
- https://www.nordicsemi.com/Products/Development-software/s132/download
- https://www.nordicsemi.com/Products/Development-software/s140/download

Signed-off-by: Damien George <damien@micropython.org>
2025-08-01 11:34:42 +10:00
Damien George
947d5448b4 tests/cpydiff: Remove passing types_float_rounding test.
Since commit dbbaa959c8, this test now
produces the same output on MicroPython as CPython does, namely -1e+01.

Signed-off-by: Damien George <damien@micropython.org>
2025-08-01 11:13:12 +10:00
Damien George
97d56527a0 github/workflows: Build unix port for docs and run workflow more often.
The unix port is needed to build the docs, due to the cpydiff tests which
run both CPython and MicroPython (unix port).  That was previously not
failing the CI because the output from MicroPython was:

    /bin/sh: 1: ../ports/unix/build-standard/micropython: not found

which doesn't match the CPython output for any of the cpydiff tests, and so
it was considered a "pass" in terms of the output differing.

Also, run the docs workflow when py/ or tests/cpydiff/ changes, because the
cpydiff results may change when the core code changes.

Signed-off-by: Damien George <damien@micropython.org>
2025-08-01 11:04:01 +10:00
Yoctopuce dev
dbbaa959c8 py/formatfloat: Improve accuracy of float formatting code.
Following discussions in PR #16666, this commit updates the float
formatting code to improve the `repr` reversibility, i.e. the percentage of
valid floating point numbers that do parse back to the same number when
formatted by `repr` (in CPython it's 100%).

This new code offers a choice of 3 float conversion methods, depending on
the desired tradeoff between code size and conversion precision:

- BASIC method is the smallest code footprint

- APPROX method uses an iterative method to approximate the exact
  representation, which is a bit slower but but does not have a big impact
  on code size.  It provides `repr` reversibility on >99.8% of the cases in
  double precision, and on >98.5% in single precision (except with REPR_C,
  where reversibility is 100% as the last two bits are not taken into
  account).

- EXACT method uses higher-precision floats during conversion, which
  provides perfect results but has a higher impact on code size.  It is
  faster than APPROX method, and faster than the CPython equivalent
  implementation.  It is however not available on all compilers when using
  FLOAT_IMPL_DOUBLE.

Here is the table comparing the impact of the three conversion methods on
code footprint on PYBV10 (using single-precision floats) and reversibility
rate for both single-precision and double-precision floats.  The table
includes current situation as a baseline for the comparison:

              PYBV10  REPR_C   FLOAT  DOUBLE
    current = 364688   12.9%   27.6%   37.9%
    basic   = 364812   85.6%   60.5%   85.7%
    approx  = 365080  100.0%   98.5%   99.8%
    exact   = 366408  100.0%  100.0%  100.0%

Signed-off-by: Yoctopuce dev <dev@yoctopuce.com>
2025-08-01 00:47:33 +10:00
Yoctopuce dev
e4e1c9f413 py/parsenum: Refactor float parsing code.
This commit extracts from the current float parsing code two functions
which could be reused elsewhere in MicroPython.

The code used to multiply a float x by a power of 10 is also simplified by
applying the binary exponent separately from the power of 5.  This avoids
the risk of overflow in the intermediate stage, before multiplying by x.

Signed-off-by: Yoctopuce dev <dev@yoctopuce.com>
2025-08-01 00:47:33 +10:00
Damien George
ffa98cb014 webassembly/proxy_js: Reuse JsProxy ref if object matches.
This reduces memory use by reusing objects, and improves identity/equality
relationships of JavaScript objects on the Python side.

In 77bd8fe5b8 PyProxy's were reused when the
same Python object was proxied across to JavaScript.  This commit does the
same thing but for JsProxy's going from JS to Python.  If an existing
JsProxy reference exists for the JS object about to be proxied across, then
it's reused.

This helps reduce the number of alive objects (memory use), and, more
importantly, improves equality relationships of JavaScript objects on the
Python side.  Eg we now get, on the Python side:

    import js

    print(js.Object == js.Object)

that prints True.  Previously it was False.

Note that this change does not make identity work with `is`, for example
`js.Object is js.Object` is actually False.  With more work that could be
made True but for now we leave that as-is.

The behaviour with this commit matches Pyodide semantics.

Signed-off-by: Damien George <damien@micropython.org>
2025-07-31 11:40:50 +10:00
Damien George
813f0c1cb9 webassembly/objjsproxy: Implement equality for JsProxy objects.
Signed-off-by: Damien George <damien@micropython.org>
2025-07-31 11:40:03 +10:00
Damien George
241ee163c0 py/objboundmeth: Add option to use mp_is_equal instead of == comparison.
This option is needed for ports such as webassembly where objects are
proxied and can be identical without being the same C pointer.

Signed-off-by: Damien George <damien@micropython.org>
2025-07-31 11:38:35 +10:00
Angus Gratton
fdbd23268d tests/run-multitests.py: Escape encoding errors instead of crashing.
It's possible for a test to output non-ASCII characters (for example, due
to a hard fault or serial noise or memory corruption). Rather than crashing
the test runner, backslash escape those characters and treat them as
program output.

Refactors the string encoding step to a single helper to avoid copy-paste.

This work was funded through GitHub Sponsors.

Signed-off-by: Angus Gratton <angus@redyak.com.au>
2025-07-31 11:17:13 +10:00
Angus Gratton
4bdf2a2dc0 tests/multi_bluetooth: Extend the deep sleep test timeout.
As per comment, if a boot.py is present that connects to Wi-Fi then waking
can take a little longer.

This work was funded through GitHub Sponsors.

Signed-off-by: Angus Gratton <angus@redyak.com.au>
2025-07-31 11:15:03 +10:00
Damien George
ff6491ded0 tests/run-natmodtests.py: Automatically skip tests that are too large.
This follows a similar change made for `run-tests.py` in commit
229104558f.  The change here uses the same
logic to detect if a natmod test is too big for the target (eg overflows
(I)RAM loading the native .mpy), by printing "START TEST" at the start of
the test.

Typical output is now something like this:

    ...
    pass  extmod/random_basic.py
    pass  extmod/random_extra_float.py
    pass  extmod/random_extra.py
    SKIP  extmod/random_seed_default.py
    LRGE  extmod/re1.py
    SKIP  extmod/re_debug.py
    pass  extmod/re_error.py
    pass  extmod/re_group.py
    pass  extmod/re_groups.py
    ...

and the tests that are too large are reported at the end, and written to
the `_result.json` file.

Signed-off-by: Damien George <damien@micropython.org>
2025-07-31 11:07:50 +10:00
Damien George
a9b038a57e examples/bluetooth/ble_advertising.py: Fix decoding UUIDs.
The UUID32 case was incorrect: first, the "<d" should have been "<I", and
second, the UUID constructor treats integer arguments as UUID16.  So this
UUID32 case needs to pass in the actual data bytes.

And then it's simpler to just make all cases pass in the data bytes.

Signed-off-by: Damien George <damien@micropython.org>
2025-07-31 11:02:41 +10:00
Damien George
6a8c45b6c4 docs/library/bluetooth: Document all allowed args to UUID constructor.
Signed-off-by: Damien George <damien@micropython.org>
2025-07-31 11:02:41 +10:00
Damien George
4360da1684 zephyr/mpconfigport: Use MICROPY_CONFIG_ROM_LEVEL_BASIC_FEATURES.
This commit adjusts the configuration of the standard zephyr build to use
MICROPY_CONFIG_ROM_LEVEL_BASIC_FEATURES.  That's a lot cleaner than
explicitly enabling/disabling options, and allows boards to more easily
fine-tune the settings, eg select a different feature level.

Features that are now enabled are:
- async/await keyword support
- `filter`, `property` and `reversed` builtins
- `range` attributes
- `str.count()` method
- `array` module with `array.array` object
- `collections` module with `collections.namedtuple` object
- `struct` module with everything
- `id = const()` and constant folding in the compiler

Bulding qemu_cortex_m3, the code size was originally:

    Memory region         Used Size  Region Size  %age Used
               FLASH:      193864 B       256 KB     73.95%
                 RAM:       61992 B        64 KB     94.59%

and with this commit it is now:

    Memory region         Used Size  Region Size  %age Used
               FLASH:      200698 B       256 KB     76.56%
                 RAM:       61992 B        64 KB     94.59%

That's a mild increase of +6834 bytes flash usage for a good selection of
new features.

Signed-off-by: Damien George <damien@micropython.org>
2025-07-31 10:48:56 +10:00
Damien George
68434b4be7 zephyr/mpconfigport_minimal: Use MICROPY_CONFIG_ROM_LEVEL_MINIMUM.
This commit adjusts the configuration of the minimal zephyr build to use
MICROPY_CONFIG_ROM_LEVEL_MINIMUM.  That's a lot cleaner than explicitly
enabling/disabling options.

Prior to this change the minimal build for qemu_cortex_m3 had size:

    Memory region         Used Size  Region Size  %age Used
               FLASH:      114436 B       256 KB     43.65%
                 RAM:       26320 B        64 KB     40.16%

and had the following test results (running using the CI settings, ie
`-d basics float --exclude inf_nan_arith`):

    352 tests performed (7092 individual testcases)
    352 tests passed
    254 tests skipped: ...

With the changes here the qemu_cortex_m3 size is now:

    Memory region         Used Size  Region Size  %age Used
               FLASH:       99428 B       256 KB     37.93%
                 RAM:       26312 B        64 KB     40.15%

That's a good decrease of about 15k firmware size.  And the test suite
still passes with:

    342 tests performed (6776 individual testcases)
    341 tests passed
    265 tests skipped: ...

Signed-off-by: Damien George <damien@micropython.org>
2025-07-31 10:48:56 +10:00
Damien George
135c1cc7cd extmod/modtls_mbedtls: Do gc_collect and retry ssl_init on any error.
Contrary to the docs, mbedtls can return more than just
MBEDTLS_ERR_SSL_ALLOC_FAILED when `mbedtls_ssl_setup()` fails.  At least
MBEDTLS_ERR_MD_ALLOC_FAILED was also seen on ESP32_GENERIC, but there
could possibly be other error codes.

To cover all these codes, just check if `ret` is non-0, and in that case
do a `gc_collect()` and retry the init.

Signed-off-by: Damien George <damien@micropython.org>
2025-07-31 10:46:47 +10:00
Yanfeng Liu
ab4af2c1a6 py/mphal: Add stddef.h header for size_t.
This includes "stddef.h" for `size_t` to resolve NuttX integration build
issues.

Signed-off-by: Yanfeng Liu <yfliu2008@qq.com>
2025-07-30 11:47:43 +10:00
Angus Gratton
d4399b3230 esp32: Fix first line ESP32-C2 serial output after reset or deepsleep.
ESP32-C2 ROM prints at 74880bps (same as ESP8266), so need a newline
before first MicroPython output to avoid it being appended on end of
a line of noise.

This work was funded through GitHub Sponsors.

Signed-off-by: Angus Gratton <angus@redyak.com.au>
2025-07-30 10:51:29 +10:00
Angus Gratton
77c9eb7795 esp32: Add "Free RAM" optimisation config flags.
This is necessary for ESP32-C2 Wi-Fi & BT to work reliably (and for TLS to
work at all). On IDF 5.4.2 the free static RAM goes from 60KB to 100KB, and
there will also be a reduction in lwIP & Wi-Fi memory use at runtime.

The performance trade-off seems low for most use cases, although it will
probably be significant for certain combinations of load (i.e. heavy
TCP/IP, heavy BT throughput, and some peripheral driver functions).

Added as a set of config flags because this is potentially useful on other
SoCs where the goal is to maximise RAM available for MicroPython.

This work was funded through GitHub Sponsors.

Signed-off-by: Angus Gratton <angus@redyak.com.au>
2025-07-30 10:51:29 +10:00
TianShuang Ke
ca9916968c esp32: Add support for ESP32-C2 (aka ESP8684).
Includes:
esp32/esp32c2: Adapt to target chip ESP32C2.
esp32/esp32c2: Fix heap size is too small to enable Bluetooth.

Signed-off-by: TianShuangKe <qinyun575@gmail.com>
Signed-off-by: Angus Gratton <angus@redyak.com.au>
2025-07-30 10:51:29 +10:00
Anson Mansfield
88cb6bc818 tests/run-internalbench.py: Allow running internalbench on hardware.
Signed-off-by: Anson Mansfield <amansfield@mantaro.com>
2025-07-29 11:49:10 +10:00
Anson Mansfield
b9d6d6af4b tests/internal_bench/var: Benchmark descriptor access.
Signed-off-by: Anson Mansfield <amansfield@mantaro.com>
2025-07-29 10:08:53 +10:00
Anson Mansfield
c3e77ad6db tests/internal_bench/class_create: Benchmark class creation.
Signed-off-by: Anson Mansfield <amansfield@mantaro.com>
2025-07-29 10:08:53 +10:00
Anson Mansfield
d5dc554742 docs: Document PEP487 __set_name__ implementation.
Signed-off-by: Anson Mansfield <amansfield@mantaro.com>
2025-07-29 09:44:26 +10:00