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
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
added optional usePhaseShift parameter to HardwarePWM constructor. …
…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
  • Loading branch information
pljakobs committed May 9, 2025
commit da69d825d3f51af4eac9ddf465a8c970285f381e
28 changes: 24 additions & 4 deletions Sming/Arch/Esp32/Core/HardwarePWM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,16 +135,32 @@ uint32_t maxDuty(ledc_timer_bit_t bits)
{
return (1U << bits) - 1;
}
int hpointForPin(uint8_t channelIndex, uint8_t channel_count)
{
/*
debug_i("calculating hpoint for channel: %d\n", channelIndex);
debug_i(" channel_count : %d\n", channel_count);
debug_i(" maxDuty : %d\n", maxDuty(DEFAULT_RESOLUTION));
*/
int _hpoint=maxDuty(DEFAULT_RESOLUTION) * channelIndex / channel_count;
// debug_i("hpoint is %d\n", _hpoint);
return _hpoint;
}

} //namespace

HardwarePWM::HardwarePWM(const uint8_t* pins, uint8_t no_of_pins) : channel_count(no_of_pins)
HardwarePWM::HardwarePWM(const uint8_t* pins, uint8_t no_of_pins, bool usePhaseShift ) : channel_count(no_of_pins)
{
assert(no_of_pins > 0 && no_of_pins <= SOC_LEDC_CHANNEL_NUM);
no_of_pins = std::min(uint8_t(SOC_LEDC_CHANNEL_NUM), no_of_pins);

_usePhaseShift = usePhaseShift;
periph_module_enable(PERIPH_LEDC_MODULE);

if (_usePhaseShift) {
debug_i("Using phase shift for %d channels\n", no_of_pins);
}

for(uint8_t i = 0; i < no_of_pins; i++) {
channels[i] = pins[i];

Expand Down Expand Up @@ -181,7 +197,7 @@ HardwarePWM::HardwarePWM(const uint8_t* pins, uint8_t no_of_pins) : channel_coun
.intr_type = LEDC_INTR_DISABLE,
.timer_sel = pinToTimer(i),
.duty = 0,
.hpoint = 0,
.hpoint = _usePhaseShift?hpointForPin(i,channel_count):0,
};
debug_d("ledc_channel\n"
"\tspeed_mode: %i\r\n"
Expand All @@ -191,7 +207,7 @@ HardwarePWM::HardwarePWM(const uint8_t* pins, uint8_t no_of_pins) : channel_coun
"\tgpio_num: %i\r\n"
"\tduty: %i\r\n"
"\thpoint: %i\r\n\n",
pinToGroup(i), pinToChannel(i), pinToTimer(i), ledc_channel.intr_type, pins[i], 0, 0);
pinToGroup(i), pinToChannel(i), pinToTimer(i), ledc_channel.intr_type, pins[i], 0, _usePhaseShift?hpointForPin(i,channel_count):0);
ESP_ERROR_CHECK(ledc_channel_config(&ledc_channel));
ledc_bind_channel_timer(pinToGroup(i), pinToChannel(i), pinToTimer(i));
}
Expand Down Expand Up @@ -247,7 +263,11 @@ void HardwarePWM::setPeriod(uint32_t period)
// Set the frequency globally, will add per timer functions later.
// Also, this can be done smarter.
for(uint8_t i = 0; i < channel_count; i++) {
ESP_ERROR_CHECK(ledc_set_freq(pinToGroup(i), pinToTimer(i), periodToFrequency(period)));
if(_usePhaseShift){
ESP_ERROR_CHECK(ledc_set_duty_with_hpoint(pinToGroup(i), pinToChannel(i), period, hpointForPin(i,channel_count)));
}else{
ESP_ERROR_CHECK(ledc_set_freq(pinToGroup(i), pinToTimer(i), periodToFrequency(period)));
}
}
// ledc_update_duty();
update();
Expand Down
2 changes: 1 addition & 1 deletion Sming/Arch/Esp8266/Core/HardwarePWM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ static const uint8_t gpioPinFunc[]{
FUNC_GPIO15, //
};

HardwarePWM::HardwarePWM(const uint8_t* pins, uint8_t noOfPins) : channel_count(noOfPins)
HardwarePWM::HardwarePWM(const uint8_t* pins, uint8_t no_of_pins, bool usePhaseShift ) : channel_count(no_of_pins)
{
if(noOfPins == 0) {
return;
Expand Down
2 changes: 1 addition & 1 deletion Sming/Arch/Host/Core/HardwarePWM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

#include <HardwarePWM.h>

HardwarePWM::HardwarePWM(const uint8_t*, uint8_t no_of_pins) : channel_count(no_of_pins)
HardwarePWM::HardwarePWM(const uint8_t* pins, uint8_t no_of_pins, bool usePhaseShift ) : channel_count(no_of_pins)
{
}

Expand Down
2 changes: 1 addition & 1 deletion Sming/Arch/Rp2040/Core/HardwarePWM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

#define PWM_FREQ_DEFAULT 1000

HardwarePWM::HardwarePWM(const uint8_t* pins, uint8_t noOfPins) : channel_count(noOfPins)
HardwarePWM::HardwarePWM(const uint8_t* pins, uint8_t no_of_pins, bool usePhaseShift ) : channel_count(no_of_pins)
{
assert(noOfPins > 0 && noOfPins <= PWM_CHANNEL_NUM_MAX);
noOfPins = std::min(uint8_t(PWM_CHANNEL_NUM_MAX), noOfPins);
Expand Down
13 changes: 11 additions & 2 deletions Sming/Core/HardwarePWM.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,16 @@ class HardwarePWM
/** @brief Instantiate hardware PWM object
* @param pins Pointer to array of pins to control
* @param no_of_pins Quantity of elements in array of pins
* @param usePhaseShift Use ledc's hpoint phase shift feature to stagger the rising edge of the PWM
* singals evenly across the period. This is useful for driving multiple LEDs
* connected to the same power rail to reduce the peak current draw and help with
* electromagnetic interference.
* @param useSpreadSpectrum implement minimal spread spectrum modulation to further reduce EMI. This
* feature uses a hardware timer to modulate the base frequency of the PWM signal.
* Depending on the base frequency, this may cause quite some system load, so
* choosing a good balance between CPU resources and acceptable EMI is important.
*/
HardwarePWM(const uint8_t* pins, uint8_t no_of_pins);

HardwarePWM(const uint8_t* pins, uint8_t no_of_pins, bool usePhaseShift=false);
virtual ~HardwarePWM();

/** @brief Set PWM duty cycle
Expand Down Expand Up @@ -154,6 +161,8 @@ class HardwarePWM

private:
uint8_t channel_count;
bool _usePhaseShift;
bool _useSpreadSpectrum;
uint8_t channels[PWM_CHANNEL_NUM_MAX];
uint32_t maxduty{0};
};
Expand Down
Loading