Skip to content

[WIP] Refactor/i hardware pwm #2956

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

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from

Conversation

pljakobs
Copy link
Contributor

after the discussion in #2955, I started thinking a bit more about how I could implement a richer HardwarePWM interface without breaking compatibility and while maintaining the core API in place, too.

My goal was to:

  • provide a richer set of options when instantiating HardwarePWM (currently used to initialize phase shifting, spread spectrum will come later, but also configuring channel groups to timers - which gets rather close to the Esp32 ledc API)
  • provide extensibility to abstract functions provided by hardware and / or the platform API and allow them to be used as part of the SMING HardwarePWM interface - those will often be hardware specific but might be implemented as hardware independent, too

I took the suggestion of extending the C header at pwm.h to provide the configuration option structures necessary at HardwarePWM instantiation. I'm not perfectly happy with that as it creates quite a bit of opportunity to mess up memory - and no good way to test for correct parameters. But I guess that's what we get for using C.

I have also moved the pre-existing HardwarePWM class to an interface only implementation IHardwarePWM that all hardware specific classes now inherit from. This allows me to extend the interface in the the child classes while maintaining a coherent set of base functions for any older code to work with. Code that wants to use the extended functionality will have to be platform specific.

This PR currently only implements the Hardware specific classes for Esp32 and Esp8266, CI failures for Host and RP2040 are expected.

The main goal is to get feedback.

pljakobs added 2 commits May 9, 2025 08:49
…This uses the esp32 ledc hpoint setting to stagger the starting point of pwm channels evenly across the pwm period to improve EMI of multi-channel high current PWM applications
Copy link

what-the-diff bot commented May 13, 2025

PR Summary

  • Enhancements to ESP32's PWM Configuration: The update introduces enhancements to the ESP32's Pulse Width Modulation (PWM) mechanism, allowing for a more detailed configuration. This provides more ways by which the PWM could be controlled, tweaking its performance to better suit specific needs.

  • Update to the HardwarePWM Class: The constructors of the HardwarePWM class has been updated to accept the new configuration settings, meaning these can now be set up at initialization and won't need to be tweaked later.

  • New Initialization Method: A new method called Init has been added to the HardwarePWM class. This method offers a way to build PWM mechanisms based on the specific configuration provided.

  • Phase Shift Support: The update includes checks for phase shift configurations, which is handy for optimizing the PWM's performance based on specific requirements. It can configure each channel depending on the phase shift settings.

  • Specific File for ESP32 PWM Configuration: A dedicated HardwarePWM.h header file has been added for the ESP32 implementation, which includes details about the initialization and configuration of the PWM process.

  • Ensuring Compatibility: To ensure compatibility with ESP8266, an empty PWM_Options structure has been added to its PWM header.

  • Uniform Refactoring Across Platforms: Changes have been made to the constructors and methods of the HardwarePWM class across different platforms, ensuring consistent implementation of the new interface across all.

  • Implementation of an Interface Class: The HardwarePWM class has been refactored into an interface class- IHardwarePWM, and the implementation details have been moved to platform-specific class implementations. This modular approach allows for fine-tuning and potential extensions in the future while maintaining an easily manageable codebase.

@pljakobs
Copy link
Contributor Author

regarding those ci failures: for the esp32 builds on MacOS, it looks like maybe instead of trying to include arch/esp32/Core/HardwarePWM.h, it tries to include from arch/host? not sure why that would be, but that file currently doesn't exist. Might be an issue with the build environment on Mac?

@mikee47
Copy link
Contributor

mikee47 commented May 13, 2025

My thoughts on the code as it stands:

  • Be careful with over-commenting. For instance, only IHardwarePWM interface needs to be documented, adding comments to the implementations will result in increased maintenance burden, risk of inconsistency, etc.
  • Now I'm looking at it, I think driver/pwm.h is the wrong place to add C++ interface definitions. I'd only put stuff there if it was available in the lower-level driver, but for the Esp32 that's part of the IDF. Just put these definitions directly into Arch/Esp32/Core/HardwarePWM.h.

Given that you are currently focused on the Esp32 capability, I think it would be better to create a separate library first. There are a number of advantages to this:

  • Trying to develop new capabilities and integrated it in one go is a bit much
  • Only need to consider Esp32 capabilities, just mark it as Esp32-only (COMPONENT_SOC=esp32*)
  • Start by copying the existing Esp32 code
  • Get the code running as you require rather than trying to do everything all-at-once

Once the library is stable and works as you like, we then have something concrete to consider migrating into the main Sming codebase. The HardwareSPI library may serve as an example of how it could be structured. The object structure is a bit convoluted (with a Controller class inheriting from the architecture-specific ControllerBase class) but the main goal was to avoid code duplication without virtual classes.

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

Successfully merging this pull request may close these issues.

2 participants