Skip to content

Commit 7788752

Browse files
authored
Provide frequency/duty_cycle API for PwmOut::begin(...). (#204)
1 parent 5f501e5 commit 7788752

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

Diff for: cores/arduino/pwm.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,28 @@ bool PwmOut::begin(uint32_t period_width, uint32_t pulse_width, bool raw /*= fal
9090
return _enabled;
9191
}
9292

93+
/* -------------------------------------------------------------------------- */
94+
bool PwmOut::begin(float freq_hz, float duty_perc) {
95+
/* -------------------------------------------------------------------------- */
96+
_enabled = true;
97+
int max_index = PINS_COUNT;
98+
_enabled &= cfg_pin(max_index);
99+
100+
if(_enabled) {
101+
_enabled &= timer.begin(TIMER_MODE_PWM, (_is_agt) ? AGT_TIMER : GPT_TIMER, timer_channel, freq_hz, duty_perc);
102+
}
103+
104+
if(_enabled) {
105+
timer.add_pwm_extended_cfg();
106+
timer.enable_pwm_channel(_pwm_channel);
107+
108+
_enabled &= timer.open();
109+
_enabled &= timer.start();
110+
}
111+
112+
return _enabled;
113+
}
114+
93115
bool PwmOut::period(int ms) {
94116
return timer.set_period_ms((double)ms);
95117
}

Diff for: cores/arduino/pwm.h

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class PwmOut {
2222
TIMER_SOURCE_DIV_256
2323
TIMER_SOURCE_DIV_1024 */
2424
bool begin(uint32_t period_usec, uint32_t pulse_usec, bool raw = false, timer_source_div_t sd = TIMER_SOURCE_DIV_1);
25+
bool begin(float freq_hz, float duty_perc);
2526
void end();
2627
bool period(int ms);
2728
bool pulseWidth(int ms);

0 commit comments

Comments
 (0)