Skip to content

alif: implement machine.PWM support#18799

Merged
dpgeorge merged 2 commits intomicropython:masterfrom
dpgeorge:alif-add-machine-pwm
Mar 3, 2026
Merged

alif: implement machine.PWM support#18799
dpgeorge merged 2 commits intomicropython:masterfrom
dpgeorge:alif-add-machine-pwm

Conversation

@dpgeorge
Copy link
Copy Markdown
Member

Summary

This PR adds machine.PWM support to the alif port. It uses the existing common machine bindings and implements the standard set of functionality.

It uses the UTIMER peripheral and makes PWM available on all pins that have an alt function connection to a UTIMER, which is 54 pins (see below about UTIMER11).

Testing

Tested on ALIF_ENSEMBLE and OPENMV_AE3, using an oscilloscope, fading LEDs and the existing tests/extmod_hardware/machine_pwm.py test.

Sample test code that works on both alif boards:

import time, machine
p = machine.PWM("LED_RED", invert=True)
p.freq(1000)
for _ in range(10):
    for duty in range(101):
        p.duty_ns(duty * 10000)
        time.sleep_ms(10)

Trade-offs and Alternatives

This uses the UTIMER peripheral, but not UTIMER11 which is already in use by the HE core for its systick timer. So the following pins don't have PWM available because they need UTIMER11: P2_6, P2_7, P7_6, P7_7, P12_6, P12_7. For OPENMV_AE3, none of those pins are exposed to the user so they don't need PWM.

For the implementation I tried to use the Alif SDK driver, but it's quite limited so most of the code here uses direct register reads/writes.

@dpgeorge
Copy link
Copy Markdown
Member Author

@iabdalkader @kwagyeman FYI

@dpgeorge dpgeorge force-pushed the alif-add-machine-pwm branch from b4206eb to 87a1a94 Compare February 10, 2026 03:12
@codecov
Copy link
Copy Markdown

codecov bot commented Feb 10, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.42%. Comparing base (bebb404) to head (95b3e72).
⚠️ Report is 2 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #18799   +/-   ##
=======================================
  Coverage   98.42%   98.42%           
=======================================
  Files         174      174           
  Lines       22331    22331           
=======================================
  Hits        21980    21980           
  Misses        351      351           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@kwagyeman
Copy link
Copy Markdown
Contributor

@dpgeorge - I have time to test this PR and #18776 tomorrow. Sorry about not getting on it immediately.

@dpgeorge
Copy link
Copy Markdown
Member Author

No hurry!

@kwagyeman
Copy link
Copy Markdown
Contributor

@dpgeorge - Works great! Tested on all pins.

Also, ever think about adding this to the standard lib? https://github.com/redoxcode/micropython-servo. It would make folks lives easier to be able to use this for servo control.

@dpgeorge
Copy link
Copy Markdown
Member Author

Works great! Tested on all pins.

Great, thanks for testing.

Also, ever think about adding this to the standard lib? ...

No, I didn't see that servo driver before. It looks simple but useful.

We do have pyb.Servo() which allows driving a servo to a set position over a period of time (in the background updating the PWM gradually). I would like to generalise that driver to all the ports, but it's not really a priority at the moment.

In the meantime, people could easily install that servo Python driver.

@kwagyeman
Copy link
Copy Markdown
Contributor

@dpgeorge - Okay, feel free to merge.

dpgeorge added 2 commits March 4, 2026 01:27
This commit adds `machine.PWM` support to the alif port.  It uses the
existing common machine bindings and implements the standard set of
functionality: `freq()`, `duty_u16()`, `duty_ns()` and `invert`.

It uses the UTIMER peripheral and makes PWM available on all pins that have
an alt function connection to a UTIMER, which is 54 pins.  It does not use
UTIMER11 which is already in use by the HE core for its systick timer.  So
the following pins don't have PWM available because they need UTIMER11:
P2_6, P2_7, P7_6, P7_7, P12_6, P12_7.

Signed-off-by: Damien George <damien@micropython.org>
Tested on ALIF_ENSEMBLE and OPENMV_AE3 boards.

Signed-off-by: Damien George <damien@micropython.org>
@dpgeorge dpgeorge force-pushed the alif-add-machine-pwm branch from 87a1a94 to 95b3e72 Compare March 3, 2026 14:30
@dpgeorge dpgeorge merged commit 95b3e72 into micropython:master Mar 3, 2026
36 checks passed
@dpgeorge dpgeorge deleted the alif-add-machine-pwm branch March 3, 2026 14:45
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.

2 participants