Commit graph

4938 commits

Author SHA1 Message Date
Daniël van de Giessen
8c47ff7153 esp32/network_ppp: Correctly clean up PPP PCB after close.
If PPP is still connected, freeing the PCB will fail and thus instead we
should trigger a disconnect and wait for the lwIP callback to actually
free the PCB.

When PPP is not connected we should check if the freeing failed, warn
the user if so, and only mark the connection as inactive if not.

When all this happens during garbage collection the best case is that
the PPP connection is already dead, which means the callback will be
called immediately and cleanup will happen correctly. The worst case is
that the connection is still alive, thus we are unable to free the PCB
(lwIP won't let us) and it remains referenced in the netif_list, meaning
a use-after-free happens later when lwIP traverses that linked list.

This change does not fully prevent that, but it *does* improve how the
PPP.active(False) method on the ESP32 port behaves: It no longer
immediately tries to free (and fails), but instead triggers a disconnect
and lets the cleanup happen correctly through the status callback.

Signed-off-by: Daniël van de Giessen <daniel@dvdgiessen.nl>
2025-08-11 12:38:37 +10:00
Daniël van de Giessen
adcfdf625b esp32/network_ppp: Use non-thread-safe API inside status callback.
The status callback runs on the lwIP tcpip_thread, and thus must use the
non-thread-safe API because the thread-safe API would cause a deadlock.

Signed-off-by: Daniël van de Giessen <daniel@dvdgiessen.nl>
2025-08-11 12:38:37 +10:00
Daniël van de Giessen
361c615f3e esp32/network_ppp: Use thread-safe API for PPPoS input.
A small follow-up to 3b1e22c669, in which
the entire PPP implementation was reworked to more closely resemble the
extmod version. One of the differences between extmod and the ESP32 port
is that the ESP32 port uses the thread-safe API, but in that changeset
the PPP input function was accidentally changed to use the non-safe API.

Signed-off-by: Daniël van de Giessen <daniel@dvdgiessen.nl>
2025-08-11 12:38:37 +10:00
Daniël van de Giessen
1273751a3b esp32: Support building against IDFv5.5.
Signed-off-by: Daniël van de Giessen <daniel@dvdgiessen.nl>
2025-08-11 12:23:02 +10:00
Angus Gratton
593ae04eeb esp32/machine_timer: Fix machine.Timer() tick frequency on ESP32C2,C6.
Also future-proofs this code for other chips. Apart form C6 and C2, all
currently supported chips use APB clock for GPTIMER_CLK_SRC_DEFAULT.

ESP32-C2 uses 40MHz PLL but APB_CLK_FREQ was 26MHz.
ESP32-C6 uses 80MHz PLL but APB_CLK_FREQ was 40MHz.

Implementation now gets the correct frequency from ESP-IDF.

This work was funded through GitHub Sponsors.

Signed-off-by: Angus Gratton <angus@redyak.com.au>
2025-08-07 16:55:46 +10:00
Angus Gratton
ce109af712 esp32/machine_timer: Enable timer clock source for ESP32C6.
Otherwise the PLL is not enabled.  These changes are adapted from
`timer_legacy.h` in ESP-IDF.

This work was funded through GitHub Sponsors.

Signed-off-by: Angus Gratton <angus@redyak.com.au>
2025-08-07 16:55:23 +10:00
Damien George
d5ecda05eb ports: Allow MICROPY_PY_MACHINE_I2C_TARGET to be disabled by board cfg.
Signed-off-by: Damien George <damien@micropython.org>
2025-08-07 10:33:26 +10:00
Damien George
255d74b5a8 renesas-ra/mpconfigport: Enable MICROPY_TIME_SUPPORT_Y1969_AND_BEFORE.
This setting was missed in df05caea6c.  It's
needed for this port to pass its `tests/ports/renesas-ra/modtime.py` test.

Signed-off-by: Damien George <damien@micropython.org>
2025-08-04 10:41:06 +10:00
Damien George
3c9546ea09 esp32/mpconfigport: Disable I2CTarget on ESP32-C6 to reduce code size.
I2CTarget costs about 8k of flash size on ESP32-S2, and about 11k on
ESP32-C6.  The ESP32-C6 only has about 8k remaining, so disable I2CTarget
on that SoC until more flash can be made available.

Signed-off-by: Damien George <damien@micropython.org>
2025-08-02 08:53:36 +10:00
Phil Howard
e6739fc87e rp2/rp2_flash: Add binary info for ROMFS.
This describes the ROMFS location and size in Pico SDK's binary declaration
format, so it can be read from a .uf2 file for use with various tools.

Signed-off-by: Phil Howard <github@gadgetoid.com>
2025-08-02 00:36:50 +10:00
Jonathan Hogg
327655905e esp32/modules/machine.py: Add Counter and Encoder classes.
Adds a Python override of the `machine` module, which delegates to the
built-in module and adds an implementation of `Counter` and `Encoder`,
based on the `esp32.PCNT` class.

Original implementation by: Jonathan Hogg <me@jonathanhogg.com>

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2025-08-01 23:45:18 +10:00
Jonathan Hogg
c3f3339c87 esp32/modesp32: Add esp32.PCNT class.
Add a new `esp32.PCNT` class that provides complete, low-level support to
the ESP32 PCNT pulse counting hardware units.

This can be used as a building block to implement the higher-level
`machine.Counter` and `machine.Encoder` classes.

This is enabled by default on all OG, S2, S3, C6 boards, but not on C3 (as
the PCNT peripheral is not supported).

Original implementation by: Jonathan Hogg <me@jonathanhogg.com>

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
Signed-off-by: Angus Gratton <angus@redyak.com.au>
2025-08-01 23:45:18 +10:00
Damien George
7bc83afee2 esp32/machine_i2c_target: Implement I2CTarget class.
Only soft IRQs are supported.

Signed-off-by: Damien George <damien@micropython.org>
2025-08-01 23:03:17 +10:00
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
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
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
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
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
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
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
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
Christian Lang
ebc9525c95 rp2/modmachine: Do not use deprecated XOSC_MHZ and XOSC_KHZ.
XOSC_MHZ and XOSC_KHZ may not be defined if we use a custom XIN clock
by defining PLL_SYS_REFDIV etc. calculated by vcocalc.py.

Signed-off-by: Christian Lang <lang.chr86@gmail.com>
2025-07-25 11:25:24 +10:00
87b7a9d734 stm32: Add casts when printing small integers.
All these arguments are of type `mp_{u,}int_t`, but the actual value is
always a small integer.  Cast it so that it can format with the `%d/%u`
formatter.

Before, the compiler plugin produced an error in the PYBD_SF6 build, which
is a nanboxing build with 64-bit ints.

Signed-off-by: Jeff Epler <jepler@gmail.com>
2025-07-25 11:01:16 +10:00
338ca3b68f unix/coverage: Remove unused printf arguments.
Signed-off-by: Jeff Epler <jepler@gmail.com>
2025-07-25 10:59:17 +10:00
aa9152ae0c unix/coverage: Provide argmuents of expected integer types.
Signed-off-by: Jeff Epler <jepler@gmail.com>
2025-07-25 10:59:04 +10:00
db7e935917 unix/coverage: Cast values to int for format printing.
During the coverage test, all the values encountered are within the range
of `%d`.

These locations were found using an experimental gcc plugin for `mp_printf`
error checking.

Signed-off-by: Jeff Epler <jepler@gmail.com>
2025-07-25 10:58:24 +10:00
61006d8016 unix/coverage: Cast values to fit %x formatting code.
This fixes the following diagnostic produced by the plugin:

    error: argument 3: Format ‘%x’ requires a ‘int’ or
        ‘unsigned int’ (32 bits), not ‘long unsigned int’ [size 64]
        [-Werror=format=]

Signed-off-by: Jeff Epler <jepler@gmail.com>
2025-07-25 10:57:59 +10:00
a06857a11a unix/coverage: Cast type names to qstr explicitly.
Signed-off-by: Jeff Epler <jepler@gmail.com>
2025-07-25 10:57:54 +10:00
a1a8eacdce unix/coverage: Avoid type checking an invalid string.
We still want this not to crash a runtime but the new static checker
wouldn't like it.

Signed-off-by: Jeff Epler <jepler@gmail.com>
2025-07-25 10:57:39 +10:00
7493275918 py/mpconfig,ports: Define new HEX_FMT formatting macro.
Signed-off-by: Jeff Epler <jepler@gmail.com>
2025-07-25 10:55:28 +10:00
0c8d35b322 ports: Eliminate define of {U,}INT_FMT where redundant.
The default definition in `py/mpconfig.h` for 32-bit architectures is
`%u/%d`, so these can be removed.

Signed-off-by: Jeff Epler <jepler@gmail.com>
2025-07-25 10:54:27 +10:00