Skip to content

MCPWM Driver Implementation - #5344

Open
Alex3404 wants to merge 4 commits into
esp-rs:mainfrom
Alex3404:main
Open

MCPWM Driver Implementation#5344
Alex3404 wants to merge 4 commits into
esp-rs:mainfrom
Alex3404:main

Conversation

@Alex3404

@Alex3404 Alex3404 commented Apr 10, 2026

Copy link
Copy Markdown

Thank you for your contribution!

We appreciate the time and effort you've put into this pull request.
To help us review it efficiently, please ensure you've gone through the following checklist:

Submission Checklist 📝

  • I have updated existing examples or added new ones (if applicable).
  • I have used cargo xtask fmt-packages command to ensure that all changed code is formatted correctly.
  • My changes were added to the CHANGELOG.md in the proper section.
  • I have added necessary changes to user code to the latest Migration Guide.
  • My changes are in accordance to the esp-rs developer guidelines

Extra:

Pull Request Details 📖

Description

I have added support for the capture channels, capture timer, sync events, and some interrupt events for the MCPWM driver. My motivation behind this was that I needed an MCPWM driver for my project, and I wasn't willing to convert my project to esp-idf.

Testing

I have tested the drivers on an ESP32-S3

  • I have used pulse generators for generating sync events, and verified accurate timing through an oscilloscope.
  • Using a pulse generator for verifying capture channel handles events properly
  • Used it in my own project for a triac dimming for measuring us pulse widths of zero crosses, and generating phase shifted gate pulses.

Changelog

esp-hal

  • Added: Better support for the MCPWM peripheral.

esp-hal/MCPWM Driver

  • Added: External sync line and timer sync out support.
  • Added: Add capture channel and capture timer support.
  • Added: esp_hal::mcpwm::timer::Timer::apply_config to apply a config to a timer.
  • Changed: esp_hal::mcpwm::timer::Timer::start is now parameterless. Instead, use esp_hal::mcpwm::timer::Timer::apply_config to apply a config to a timer.
  • Changed: timers default to the config given by PeripheralClockConfig::timer_clock_default().
  • Changed: Timers now have a default configuration when you create a MCPWM instance via McPwm::new.
  • Changed: Timer::start starts with the configured StopCondition rather than being restricted to running till Timer::stop is called.

Migration guide

esp-hal/MCPWM driver

Timer::start signature changed

The Timer::start method is now parameter less and doesn't take in a TimerClockConfig. Instead, use Timer::apply_config method to set the config for the mcpwm::Timer.

TimerClockConfig created by timer_clock_with_frequency and timer_clock_with_prescaler
will still default to StopCondition::RunContinuously. So, any existing code will only need
a simple change.

-mcpwm.timer0.start(timer_clock_cfg);
+mcpwm.timer0.apply_config(timer_clock_cfg)?;
+mcpwm.timer0.start();

Mcpwm removed generics

-McPwm<'_, mcpwm::mcpwm0>
+McPwm<'_>

Timer removed generics

-Timer<TIMER_NUM, mcpwm::mcpwm0>
+Timer<'_>

Operator removed generics

-Operator<'_, OP_NUM, mcpwm::mcpwm0>
+Operator<'_>

LinkedPins removed generics

-LinkedPins<'_, mcpwm::mcpwm0, OP_NUM>
+LinkedPins<'_>

PwmPin generics reduced

-LinkedPins<'_, mcpwm::mcpwm0, OP_NUM, IS_A>
+LinkedPins<'_, IS_A>

Mcpwm::new signature changed

The Mcpwm::new now takes a AnyMcpwm type please use AnyMcpwm::from(mcpwm_peripheral) to construct the MCPWM driver.

-McPwm::new(peripherals.MCPWM0, clock_cfg);
+McPwm::new(AnyMcPwm::from(peripherals.MCPWM0), clock_cfg);

Copilot AI review requested due to automatic review settings April 10, 2026 16:00
@github-actions github-actions Bot added the merge-conflict Merge conflict detected. Automatically added/removed by CI. label Apr 10, 2026
@github-actions

Copy link
Copy Markdown

New commits in main has made this PR unmergable. Please resolve the conflicts.

Comment thread esp-metadata/devices/esp32s3.toml Outdated
Comment thread build_all.ps1 Outdated
Comment thread esp-hal/src/mcpwm/mod.rs Outdated

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR expands the esp-hal MCPWM driver to support additional MCPWM functionality (capture channels + capture timer, sync sources/lines and timer sync-out chaining, plus more interrupt/event handling), and updates device metadata with new MCPWM capability cfgs.

Changes:

  • Add MCPWM capture support (capture timer + capture channels) and new sync abstractions (sync lines + timer sync-out as a sync source).
  • Introduce MCPWM interrupt/event plumbing (timer events, capture events) with a shared per-instance State.
  • Update device metadata / generated cfg lists with new soc_has_mcpwm_* capability symbols.

Reviewed changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 9 comments.

Show a summary per file
File Description
esp-metadata/devices/esp32s3.toml Adds MCPWM capability symbol(s) and reformats metadata.
esp-metadata/devices/esp32h2.toml Adds MCPWM capability symbols and reformats metadata.
esp-metadata/devices/esp32c6.toml Adds MCPWM capability symbols and reformats metadata.
esp-metadata/devices/esp32c5.toml Adds MCPWM capability symbols and reformats metadata.
esp-metadata-generated/src/_build_script_utils.rs Emits/validates new soc_has_mcpwm_* cfgs for build scripts and check-cfg.
esp-hal/src/mcpwm/mod.rs Refactors MCPWM instance plumbing; adds capture/sync fields and interrupt/event infrastructure; updates docs.
esp-hal/src/mcpwm/timer.rs Adds sync-in/out configuration, timer events/interrupt helpers, and new timer config fields.
esp-hal/src/mcpwm/operator.rs Adapts operators/pins to new MCPWM Instance abstraction and register access patterns.
esp-hal/src/mcpwm/sync.rs New sync module: SyncLine, SyncOut, and a sealed SyncSource abstraction.
esp-hal/src/mcpwm/capture.rs New capture module: capture timer + capture channels + configuration + interrupt helpers.
esp-hal/Cargo.toml Re-formats and alters dependencies (notably PAC dependency sources/paths).
build_all.ps1 Adds a helper script to build esp-hal for multiple chips.

Comment thread esp-hal/Cargo.toml Outdated
Comment thread esp-hal/src/mcpwm/mod.rs Outdated
Comment thread esp-hal/src/mcpwm/mod.rs Outdated
Comment thread esp-hal/src/mcpwm/timer.rs
Comment thread esp-hal/src/mcpwm/capture.rs Outdated
Comment thread esp-hal/src/mcpwm/capture.rs Outdated
Comment thread esp-hal/src/mcpwm/sync.rs
Comment thread esp-hal/src/mcpwm/mod.rs
Comment thread esp-hal/src/mcpwm/mod.rs
Copilot AI review requested due to automatic review settings April 10, 2026 17:33

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 11 out of 11 changed files in this pull request and generated 16 comments.

Comments suppressed due to low confidence (1)

esp-hal/Cargo.toml:313

  • This PR removes the psram feature definition, but the codebase still uses #[cfg(feature = "psram")] (e.g., in esp-hal/src/lib.rs and esp-hal/src/psram/*) and the build script checks cfg!(feature = "psram"). Please restore the psram feature (and its docs/CI feature matrices) or update the codebase to the new intended feature flag, otherwise PSRAM support becomes impossible to enable.
#! ### Unstable APIs
#! Unstable APIs are drivers and features that are not yet ready for general use.
#! They may be incomplete, have bugs, or be subject to change without notice.
#! Unstable APIs are not covered by semver guarantees.

## Enables APIs that are not stable and thus come with no stability guarantees.
## Never enable this feature in a library crate using esp-hal.
unstable = [
    "dep:digest",

Comment thread esp-hal/src/mcpwm/mod.rs Outdated
Comment thread esp-hal/src/mcpwm/mod.rs
Comment thread esp-hal/src/mcpwm/mod.rs
Comment thread esp-hal/src/mcpwm/mod.rs
Comment thread esp-hal/src/mcpwm/timer.rs Outdated
Comment thread esp-metadata/devices/esp32c6.toml Outdated
Comment thread esp-hal/Cargo.toml Outdated
Comment thread esp-hal/src/mcpwm/timer.rs Outdated
Comment thread esp-hal/src/mcpwm/capture.rs Outdated
Comment thread esp-hal/src/mcpwm/mod.rs Outdated
@github-actions github-actions Bot removed the merge-conflict Merge conflict detected. Automatically added/removed by CI. label Apr 10, 2026
Copilot AI review requested due to automatic review settings April 10, 2026 18:03

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 11 out of 11 changed files in this pull request and generated 6 comments.

Comment thread esp-hal/src/mcpwm/mod.rs Outdated
Comment thread esp-hal/src/mcpwm/mod.rs
Comment thread esp-hal/Cargo.toml Outdated
Comment thread esp-hal/src/mcpwm/sync.rs Outdated
Comment thread esp-metadata/devices/esp32h2.toml Outdated
Comment thread esp-hal/src/mcpwm/timer.rs Outdated
Copilot AI review requested due to automatic review settings April 10, 2026 19:59
@github-actions

github-actions Bot commented Apr 10, 2026

Copy link
Copy Markdown

⚠️ Do not edit CHANGELOG.md manually

This PR modifies the following changelog file(s) directly:

  • esp-hal/CHANGELOG.md

Changelog entries are no longer maintained by hand. Instead, add your entries to the PR description under the # Changelog section:

# Changelog

## esp-hal

- Added: Short description of the change
- Fixed: Short description of the fix

Migration guide entries go in the # Migration guide section (area is required; each breaking change needs a ### Title heading):

# Migration guide

## esp-hal/SPI

### `OldType` has been renamed to `NewType`

Replace all uses of `OldType` with `NewType`.

Please revert the changes to CHANGELOG.md and move your entries to the PR description instead.
Run cargo xtask check-pr-changelog locally to validate the format before pushing.

@github-actions github-actions Bot added the merge-conflict Merge conflict detected. Automatically added/removed by CI. label Apr 10, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 13 out of 13 changed files in this pull request and generated 8 comments.

Comment thread esp-hal/src/mcpwm/mod.rs Outdated
Comment thread esp-hal/src/mcpwm/mod.rs Outdated
Comment thread esp-hal/src/mcpwm/timer.rs
Comment thread esp-hal/src/mcpwm/timer.rs Outdated
Comment thread esp-hal/MIGRATING-1.0.0.md Outdated
Comment thread esp-hal/Cargo.toml Outdated
Comment thread esp-hal/src/mcpwm/capture.rs Outdated
Comment thread esp-hal/src/mcpwm/timer.rs
Copilot AI review requested due to automatic review settings April 10, 2026 20:06

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 13 out of 13 changed files in this pull request and generated 4 comments.

Comment thread esp-hal/src/mcpwm/mod.rs Outdated
Comment thread esp-hal/src/mcpwm/mod.rs Outdated
Comment thread esp-hal/src/mcpwm/mod.rs Outdated
Comment thread esp-hal/Cargo.toml Outdated
Comment thread esp-metadata/devices/esp32c5.toml Outdated
Copilot AI review requested due to automatic review settings April 10, 2026 21:22

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 13 out of 13 changed files in this pull request and generated 4 comments.

Comment thread esp-hal/src/mcpwm/mod.rs Outdated
Comment thread esp-hal/src/mcpwm/timer.rs Outdated
Comment thread esp-hal/src/mcpwm/capture.rs Outdated
Comment thread esp-hal/CHANGELOG.md Outdated
Copilot AI review requested due to automatic review settings April 10, 2026 23:08

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 22 out of 22 changed files in this pull request and generated 2 comments.

Comment thread esp-hal/src/mcpwm/mod.rs Outdated
Comment thread esp-hal/src/mcpwm/mod.rs Outdated
Copilot AI review requested due to automatic review settings April 10, 2026 23:17

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 22 out of 22 changed files in this pull request and generated 1 comment.

Comment thread esp-hal/src/mcpwm/timer.rs
Copilot AI review requested due to automatic review settings April 29, 2026 05:20

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 23 out of 23 changed files in this pull request and generated 6 comments.

Comment thread hil-test/src/bin/misc_drivers.rs Outdated
Comment thread hil-test/src/bin/misc_drivers.rs Outdated
Comment thread hil-test/src/bin/misc_drivers.rs Outdated
Comment thread esp-hal/Cargo.toml Outdated
Comment thread esp-hal/MIGRATING-1.0.0.md Outdated
Comment thread esp-hal/MIGRATING-1.0.0.md Outdated
@github-actions github-actions Bot removed the merge-conflict Merge conflict detected. Automatically added/removed by CI. label Jul 10, 2026
Comment thread esp-metadata/devices/esp32/soc.toml Outdated
Comment thread esp-metadata/devices/esp32/soc.toml
Comment thread esp-metadata/devices/esp32s3/soc.toml Outdated
@Alex3404
Alex3404 marked this pull request as draft July 14, 2026 02:11
@Alex3404
Alex3404 marked this pull request as ready for review July 14, 2026 08:04
@Alex3404
Alex3404 marked this pull request as draft July 14, 2026 08:20
@github-actions github-actions Bot added the merge-conflict Merge conflict detected. Automatically added/removed by CI. label Jul 14, 2026
@github-actions

Copy link
Copy Markdown

New commits in main have made this PR unmergeable. Please resolve the conflicts.

@github-actions github-actions Bot added merge-conflict Merge conflict detected. Automatically added/removed by CI. and removed merge-conflict Merge conflict detected. Automatically added/removed by CI. labels Aug 5, 2026
@github-actions

github-actions Bot commented Aug 5, 2026

Copy link
Copy Markdown

New commits in main have made this PR unmergeable. Please resolve the conflicts.

Fix new line ending
@Alex3404
Alex3404 marked this pull request as ready for review August 6, 2026 01:39
@Alex3404
Alex3404 requested a review from bugadani August 6, 2026 01:39
@bugadani

bugadani commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

I've just looked at the PR description but this needs to be changed:

-McPwm::new(peripherals.MCPWM0, clock_cfg);
+McPwm::new(AnyMcPwm::from(peripherals.MCPWM0), clock_cfg);

Take the peripehral as it is, convert to AnyMcPwm in the constructor, like other drivers do.

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

Labels

merge-conflict Merge conflict detected. Automatically added/removed by CI.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants