I am using an ESP32-WROOM-32D module to drive a 320x240 color LCD via SPI. I also drive the display's backlight with a simple output pin on IO04.
To change the backlight smoothly I'm using the ledc module and everything works as expected until a software reset occurs. For several reasons I need to reset my device with the `esp_restart` function, and after the reset the backlight is dead.
Everything else works as expected and I could confirm that the problem is due to backlight control by switching from the ledc module to simple IO control (in which case there are no issues).
I would say either the ledc module or the IO4 itself remains in an incosistent state after a software reset. Hardware resets behave normally.
This is the initialization code that I'm using:
Code: Select all
static void display_backlight_init() {
ledc_timer_config_t ledc_timer = {
.duty_resolution = LEDC_TIMER_8_BIT, // resolution of PWM duty
.freq_hz = 1000, // frequency of PWM signal
.speed_mode = LEDC_LOW_SPEED_MODE, // timer mode
.timer_num = LEDC_TIMER_2, // timer index
.clk_cfg = LEDC_AUTO_CLK, // Auto select the source clock
};
ledc_timer_config(&ledc_timer);
ledc_channel_config_t ledc_channel = {.channel = LEDC_CHANNEL_1,
.duty = 0xFF,
.gpio_num = GPIO_NUM_4,
.speed_mode = LEDC_LOW_SPEED_MODE,
.hpoint = 0,
.timer_sel = LEDC_TIMER_2};
ledc_channel_config(&ledc_channel);
ledc_stop(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1, 0);
display_set_backlight(40);
}
void display_set_backlight(int percentage) {
int duty = (percentage * 0xFF) / 100;
if (duty != ledc_get_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1)) {
ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1, duty);
ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1);
}
}
Additionally, before resetting the module I tried to stop the ledc module with the `ledc_stop(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1, 0);` function call.
Is there any additional step I should take to properly re-initialize my backlight control?