Skip to content

Commit dbeae94

Browse files
authored
AnalogWrite - frequency and resolution log errors + returns (espressif#7471)
* Added log errors + returns * Update LEDC docs
1 parent 394f721 commit dbeae94

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

cores/esp32/esp32-hal-ledc.c

+13-4
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,12 @@ void analogWrite(uint8_t pin, int value) {
222222
log_e("No more analogWrite channels available! You can have maximum %u", LEDC_CHANNELS);
223223
return;
224224
}
225+
if(ledcSetup(cnt_channel - 1, analog_frequency, analog_resolution) == 0){
226+
log_e("analogWrite setup failed (freq = %u, resolution = %u). Try setting different resolution or frequency");
227+
return;
228+
}
229+
ledcAttachPin(pin, cnt_channel - 1);
225230
pin_to_channel[pin] = cnt_channel--;
226-
ledcSetup(cnt_channel, analog_frequency, analog_resolution);
227-
ledcAttachPin(pin, cnt_channel);
228231
}
229232
ledcWrite(pin_to_channel[pin] - 1, value);
230233
}
@@ -237,7 +240,10 @@ int8_t analogGetChannel(uint8_t pin) {
237240
void analogWriteFrequency(uint32_t freq) {
238241
if (cnt_channel != LEDC_CHANNELS) {
239242
for (int channel = LEDC_CHANNELS - 1; channel >= cnt_channel; channel--) {
240-
ledcChangeFrequency(channel, freq, analog_resolution);
243+
if (ledcChangeFrequency(channel, freq, analog_resolution) == 0){
244+
log_e("analogWrite frequency cant be set due to selected resolution! Try to adjust resolution first");
245+
return;
246+
}
241247
}
242248
}
243249
analog_frequency = freq;
@@ -250,7 +256,10 @@ void analogWriteResolution(uint8_t bits) {
250256
}
251257
if (cnt_channel != LEDC_CHANNELS) {
252258
for (int channel = LEDC_CHANNELS - 1; channel >= cnt_channel; channel--) {
253-
ledcChangeFrequency(channel, analog_frequency, bits);
259+
if (ledcChangeFrequency(channel, analog_frequency, bits) == 0){
260+
log_e("analogWrite resolution cant be set due to selected frequency! Try to adjust frequency first");
261+
return;
262+
}
254263
}
255264
}
256265
analog_resolution = bits;

docs/source/api/ledc.rst

+22
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,28 @@ It is compatible with Arduinos analogWrite function.
169169
* ``value`` select the duty cycle of pwm.
170170
* range is from 0 (always off) to 255 (always on).
171171

172+
analogWriteResolution
173+
*********************
174+
175+
This function is used to set resolution for all analogWrite channels.
176+
177+
.. code-block:: arduino
178+
179+
void analogWriteResolution(uint8_t bits);
180+
181+
* ``bits`` select resolution for analog channels.
182+
183+
analogWriteFrequency
184+
********************
185+
186+
This function is used to set frequency for all analogWrite channels.
187+
188+
.. code-block:: arduino
189+
190+
void analogWriteFrequency(uint32_t freq);
191+
192+
* ``freq`` select frequency of pwm.
193+
172194
Example Applications
173195
********************
174196

0 commit comments

Comments
 (0)