@@ -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) {
237240void 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 ;
0 commit comments