Page 1 of 1

How to use LEDC with inverted pin?

Posted: Tue Aug 31, 2021 4:16 pm
by vonnieda
Hi all,

I have a LED I need to control with LEDC for PWM. The pin is inverted in the hardware, i.e. 1 is off, 0 is on. The LEDC API expects the opposite. I've sort of fixed this by inverting my duty cycle, and that works fine, but when I first enable the LEDC channel there is a very brief flicker where the LED turns on before turning off.

Here is how I am configuring LEDC:

Code: Select all

	// Configure LEDC for backlight PWM
	ledc_timer_config_t ledc_timer = {
		.duty_resolution = LEDC_DUTY_RESOLUTION,
		.freq_hz = LEDC_FREQ,
		.speed_mode = LEDC_SPEED_MODE,
		.timer_num = LEDC_TIMER_1
	};
	ledc_timer_config(&ledc_timer);

	ledc_channel_config_t ledc_channel = {
		.channel    = LEDC_CHANNEL_LCD_BL_BASE,
		.duty       = (1 << LEDC_DUTY_RESOLUTION) - 1,
		.gpio_num   = LCD_IO_BACKLIGHT_N,
		.speed_mode = LEDC_SPEED_MODE,
		.hpoint     = 0,
		.timer_sel  = LEDC_TIMER_1
	};
	ledc_channel_config(&ledc_channel);
	
The flicker happens in the call to ledc_channel_config. Before that call the LED is off, during the call it turns on very briefly, and when the call returns it is off again.

So, question: Is there a specific way I should use LEDC with an inverted pin?

And follow up question: Is there a way to avoid the brief "on" before it turns "off"?


Thanks,
Jason

Re: How to use LEDC with inverted pin?

Posted: Tue Aug 31, 2021 5:27 pm
by Victoria Nope
Set the output_invert member of the ledc_channel_config_t structure's flags to 1 or 0 respectively.

Re: How to use LEDC with inverted pin?

Posted: Tue Aug 31, 2021 5:44 pm
by vonnieda
Victoria Nope wrote:
Tue Aug 31, 2021 5:27 pm
Set the output_invert member of the ledc_channel_config_t structure's flags to 1 or 0 respectively.

Thanks for that! Looks like that flag is not in my version of IDF, but easy enough to backport.

Thanks,
Jason

Re: How to use LEDC with inverted pin?

Posted: Wed Sep 01, 2021 2:56 am
by Victoria Nope
If you did not want to upgrade IDF, you could just modify relevant components to pass the inverting value to the already existing out_inv parameter of the esp_rom_gpio_connect_out_signal function call like has been done recently.

But overall upgrade is what I would highly recommend.

Re: How to use LEDC with inverted pin?

Posted: Wed Sep 01, 2021 3:21 am
by ESP_Sprite
Either that, or call esp_rom_gpio_connect_out_signal() in your own code with the proper values after initializing LEDC. +1 on the 'better to upgrade' thing though.