Skip to content

Provide frequency/duty_cycle API for PwmOut::begin(...). #204

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 1 commit into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
22 changes: 22 additions & 0 deletions cores/arduino/pwm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,28 @@ bool PwmOut::begin(uint32_t period_width, uint32_t pulse_width, bool raw /*= fal
return _enabled;
}

/* -------------------------------------------------------------------------- */
bool PwmOut::begin(float freq_hz, float duty_perc) {
/* -------------------------------------------------------------------------- */
_enabled = true;
int max_index = PINS_COUNT;
_enabled &= cfg_pin(max_index);

if(_enabled) {
_enabled &= timer.begin(TIMER_MODE_PWM, (_is_agt) ? AGT_TIMER : GPT_TIMER, timer_channel, freq_hz, duty_perc);
}

if(_enabled) {
timer.add_pwm_extended_cfg();
timer.enable_pwm_channel(_pwm_channel);

_enabled &= timer.open();
_enabled &= timer.start();
}

return _enabled;
}

bool PwmOut::period(int ms) {
return timer.set_period_ms((double)ms);
}
Expand Down
1 change: 1 addition & 0 deletions cores/arduino/pwm.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class PwmOut {
TIMER_SOURCE_DIV_256
TIMER_SOURCE_DIV_1024 */
bool begin(uint32_t period_usec, uint32_t pulse_usec, bool raw = false, timer_source_div_t sd = TIMER_SOURCE_DIV_1);
bool begin(float freq_hz, float duty_perc);
void end();
bool period(int ms);
bool pulseWidth(int ms);
Expand Down