Skip to content

zephyr: a few quality-of-life improvements, and additional board configurations #17552

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Jul 8, 2025

Conversation

dpgeorge
Copy link
Member

Summary

The main aim here was to get mpremote mip install unittest working on the zephyr port, so it could run more of the test suite. And that lead to a lot of little changes.

Overall this PR aims to bring the zephyr port a bit closer to other ports in terms of functionality and usability:

  • execute boot.py and main.py like other ports
  • add /flash/lib to sys.path on start up if there's a filesystem (eg mpremote mip install now works)
  • enable sys.stdin/out/err
  • improve REPL reliability and performance, it now passes the test from tools/test_serial.py: Add test for serial throughput. #15909, and copying files with mpremote is now reliable
  • fix USB device_next driver compilation error
  • add Pin.OPEN_DRAIN support
  • allow constructing a Pin from an existing Pin (follows other ports)
  • implement C-level pin HAL
  • enable machine.SoftI2C
  • enable importing of .mpy files

It also tweaks/adds a few board configurations:

  • frdm_k64f: get ctrl-C working, enable PWM, increase heap size
  • nucleo_wb55rg: enable BLE, I2C, SPI and add filesystem
  • rpi_pico: use USB for REPL, enable I2C, SPI, add filesystem, increase heap size

In particular, the filesystem settings for rpi_pico now match the rp2 port RPI_PICO board, so you can switch between zephyr and rp2 firmware and retain the filesystem (and even .mpy files on that filesystem work across the two ports!). That allows some nice side-by-side comparisons between the zephyr and rp2 ports for the same hardware. Eg USB CDC throughput is quite a lot higher on the rp2 port using TinyUSB, compared with zephyr's USB device (but maybe this is due to zephyr configuration issues, I didn't investigate further).

Testing

Tested with frdm_k64f, nucleo_wb55rg and rpi_pico zephyr firmware:

Trade-offs and Alternatives

This increases the firmware size of zephyr builds a bit, but (1) these boards have enough flash and RAM; and (2) it's easy enough for custom boards to reconfigure/disable features if needed.

Copy link
Contributor

@bikeNomad bikeNomad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a couple of comments regarding being able to read from output/open-drain Pins.

@dpgeorge
Copy link
Member Author

dpgeorge commented Jul 6, 2025

I've added 3 more commits/changes here:

  • enable sys.maxsize; that costs +48 bytes but allows introspection to the target, needed at least for some tests to work
  • enable emergency exception buffer, again needed for a few tests to run and pass (otherwise the device can lock up running the tests/micropython/heapalloc_exc_compressed_emg_exc.py test)
  • make the id argument to machine.Timer optional, defaulting to -1 (they only allowed value); that matches other ports and gets the tests/extmod/machine_soft_timer.py test passing

Testing was done with rpi_pico firmware.

@danicampora
Copy link
Member

I've added 3 more commits/changes here:

Looks good!

dpgeorge added 18 commits July 8, 2025 10:10
Changes here make the zephyr port act the same as other ports for the
start up and shut down sequence:
- `boot.py` is executed if it exists, and can force a soft reset
- `main.py` is only executed if in friendly REPL and if `boot.py` executed
  successfully; and it can also force a soft reset
- print "MPY: " before "soft reboot" on soft reset

Signed-off-by: Damien George <damien@micropython.org>
If there is a filesystem available, this change makes sure there is a "lib"
in `sys.path`, eg so that "mip install" works correctly.

Signed-off-by: Damien George <damien@micropython.org>
This change enables `sys.stdin`, `sys.stdout` and `sys.stderr` objects.
They are useful for general IO, and also help with testing zephyr boards.

Signed-off-by: Damien George <damien@micropython.org>
There are two changes here:

1. Increase the UART input bufffer to 512 bytes.  That's necessary to get
   basic REPL reliability tests working, and helps improve `mpremote`
   usage, eg copying large files.

2. Remove `uart_sem` semaphore.  This is no longer needed because
   `zephyr_getchar()` should be fully non-blocking and have as low a
   latency as possible.  `mp_hal_stdin_rx_chr()` (which calls
   `zephyr_getchar`) already uses `MICROPY_EVENT_POLL_HOOK` to get
   an efficient wait, and doing an extra wait and check for the
   semaphore in `zephyr_getchar()` just introduces unnecessary latency and
   can lead to slower input, and potentially overflowing the UART input
   buffer.

Signed-off-by: Damien George <damien@micropython.org>
The blocklist argument is not available in zephyr 4.0.0.

Signed-off-by: Damien George <damien@micropython.org>
Adding this constant is all that's needed to support open-drain pins.

Signed-off-by: Damien George <damien@micropython.org>
Following other ports.

Signed-off-by: Damien George <damien@micropython.org>
Signed-off-by: Damien George <damien@micropython.org>
These work now that the C-level pin HAL is implemented.

Signed-off-by: Damien George <damien@micropython.org>
Support for importing .mpy files is quite fundamental to MicroPython these
days, eg it allows installing more efficient .mpy code via "mip install"
(and installing `unittest` only works with the .mpy version because the .py
version uses f-strings, which are not enabled on the zephyr port).  So
enable it generally for use by all boards.

As part of this, also enable:
- min/max: needed by `micropython/import_mpy_invalid.py`, and widely used
- sys.modules: needed by `run-tests.py` to run .mpy tests with --via-mpy
- io module: needed to run .mpy tests, and useful for `io.IOBase`
- array slice assign: needed to run .mpy tests, and generally useful as a
  way to do a memory copy.

Signed-off-by: Damien George <damien@micropython.org>
Needed for some ARMv6M boards, eg rpi_pico.

Signed-off-by: Damien George <damien@micropython.org>
Changes:
- Enable CONFIG_PWM so that `machine.PWM()` works.
- Increase MicroPython GC heap size.

Signed-off-by: Damien George <damien@micropython.org>
Bluetooth works well now on this board, so enable all supported features.

Also increase the MicroPython GC heap size to make use of the available
RAM.

Unfortunately the filesystem does not match the stm32 port's NUCLEO_WB55
configuration.  That's not possible to do because stm32 uses a 512 byte
flash erase size, while zephyr uses 4096 bytes.  But at least here in
zephyr there's now a sizable and usable filesystem.

Signed-off-by: Damien George <damien@micropython.org>
Although the rpi_pico can already build and run with the zephyr port, this
configuration improves it in a number of ways:
- Use the USB CDC ACM as the REPL, rather than just a UART.
- Enable I2C and SPI, and add I2C1.
- Enable a filesystem, which matches exactly the rp2 port's RPI_PICO
  configuration.  So switching between zephyr and rp2 is possible and will
  retain the filesystem.
- Make the MicroPython GC heap make the most use of the available RAM.

Signed-off-by: Damien George <damien@micropython.org>
Costs +48 bytes.  Useful to introspect the target.

Signed-off-by: Damien George <damien@micropython.org>
Needed to pass exception tests.

Signed-off-by: Damien George <damien@micropython.org>
With a default of -1, for soft timer.  This matches other ports, and the
`extmod/machine_timer.c` implementation.

This change allows the `tests/extmod/machine_soft_timer.py` test to pass.

Signed-off-by: Damien George <damien@micropython.org>
Some targets like frdm_k64f don't support GPIO_OUTPUT|GPIO_INPUT, so just
use GPIO_OUTPUT in those cases (it seems they still support reading the
current output state even when configured only as GPIO_OUTPUT, unlike other
targets which require both settings).

Signed-off-by: Damien George <damien@micropython.org>
@dpgeorge dpgeorge force-pushed the zephyr-improve-config branch from 3e7fdb0 to 9a9e552 Compare July 8, 2025 01:22
@dpgeorge dpgeorge merged commit 9a9e552 into micropython:master Jul 8, 2025
7 checks passed
@dpgeorge dpgeorge deleted the zephyr-improve-config branch July 8, 2025 01:33
@jonnor
Copy link
Contributor

jonnor commented Jul 11, 2025

Very nice! Zephyr support seems to be soaring ahead lately :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy