Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ledc driver bug, after fast switching between 0% and 100% duty with fade, the leds stays on (IDFGH-4918) #6710

Closed
mvarchdev opened this issue Mar 13, 2021 · 4 comments
Labels
Resolution: Done Issue is done internally Status: Done Issue is done internally

Comments

@mvarchdev
Copy link

mvarchdev commented Mar 13, 2021

Environment

  • Development Kit: ESP32-DevKitC
  • Kit version: v4
  • Module or chip used: ESP32-WROOM-32U
  • IDF version: v4.3-beta1
  • Build System: idf.py
  • Compiler version: (crosstool-NG esp-2020r3) 8.4.0
  • Operating System: Linux
  • Using an IDE?: Yes - Visual Studio code with installed esp-idf extension, but building and flashing from the server (Ubuntu wth esp-idf, same version)
  • Power Supply: USB

Problem Description

I am working on a project, something like smart home, just school project... I created IFTTT (If this then this) mechanism where conditions can be added while running. There is one loop checking if something was changed (button high, etc...), I added ledc to make led bars easily controllable with it. gpio_set_level works fine, and ledc_set_fade_with_time with low click frequency too, but as soon as I start clicking faster, the ledc_set_fade_with_time bugs, and some leds will stay on. (The main function is just from duty 0% to 100%, then inverted after button click (100% to 0%) and so on). The debug output showing some weird things, like there would be actual duty 10000 and more. You will see in the videos:

First: VIDEO1
Second: VIDEO2

Can you please help me?

Here is debug log with ledc:
NORMAL
D (00:00:30.093) ledc: cur duty: 0; target: 2047, step: 1023, cycle: 1; scale: 1; dir: 1

D (00:00:31.061) ledc: cur duty: 2047; target: 0, step: 1023, cycle: 1; scale: 1; dir: 0

D (00:00:31.062) ledc: cur duty: 0; target: 2047, step: 1023, cycle: 1; scale: 1; dir: 1

D (00:00:31.985) ledc: cur duty: 2047; target: 0, step: 1023, cycle: 1; scale: 1; dir: 0

D (00:00:31.986) ledc: cur duty: 0; target: 2047, step: 1023, cycle: 1; scale: 1; dir: 1

D (00:00:32.975) ledc: cur duty: 2047; target: 0, step: 1023, cycle: 1; scale: 1; dir: 0

D (00:00:32.976) ledc: cur duty: 0; target: 2047, step: 1023, cycle: 1; scale: 1; dir: 1

D (00:00:33.415) ledc: cur duty: 0; target: 2047, step: 1023, cycle: 1; scale: 1; dir: 1

GLITCHED
D (00:00:41.912) ledc: cur duty: 9587; target: 2047, step: 1023, cycle: 1; scale: 1; dir: 0

D (00:00:42.034) ledc: cur duty: 9098; target: 0, step: 1023, cycle: 1; scale: 2; dir: 0

D (00:00:42.035) ledc: cur duty: 291; target: 2047, step: 1023, cycle: 2; scale: 1; dir: 1

D (00:00:42.158) ledc: cur duty: 291; target: 0, step: 291, cycle: 13; scale: 1; dir: 0

D (00:00:42.159) ledc: cur duty: 193; target: 2047, step: 1023, cycle: 2; scale: 1; dir: 1

D (00:00:42.264) ledc: cur duty: 507; target: 2047, step: 1023, cycle: 2; scale: 1; dir: 1

D (00:00:42.265) ledc: cur duty: 193; target: 0, step: 193, cycle: 20; scale: 1; dir: 0

D (00:00:42.403) ledc: cur duty: 783; target: 0, step: 783, cycle: 5; scale: 1; dir: 0

D (00:00:42.404) ledc: cur duty: 10243; target: 2047, step: 1023, cycle: 1; scale: 2; dir: 0

D (00:00:42.509) ledc: cur duty: 9399; target: 0, step: 1023, cycle: 1; scale: 2; dir: 0

D (00:00:42.510) ledc: cur duty: 224; target: 2047, step: 1023, cycle: 2; scale: 1; dir: 1

D (00:00:42.633) ledc: cur duty: 224; target: 0, step: 224, cycle: 17; scale: 1; dir: 0

D (00:00:42.634) ledc: cur duty: 149; target: 2047, step: 1023, cycle: 2; scale: 1; dir: 1

D (00:00:42.754) ledc: cur duty: 502; target: 2047, step: 1023, cycle: 2; scale: 1; dir: 1

D (00:00:42.756) ledc: cur duty: 149; target: 0, step: 149, cycle: 26; scale: 1; dir: 0

V (00:00:42.892) esp_adapter: thread sem get: sem=0x3ffccb50
D (00:00:43.111) ledc: cur duty: 705; target: 0, step: 705, cycle: 5; scale: 1; dir: 0

D (00:00:43.112) ledc: cur duty: 8680; target: 2047, step: 1023, cycle: 1; scale: 1; dir: 0

D (00:00:43.234) ledc: cur duty: 8191; target: 0, step: 1023, cycle: 1; scale: 2; dir: 0

D (00:00:43.235) ledc: cur duty: 114; target: 2047, step: 1023, cycle: 2; scale: 1; dir: 1

I tried to create similiar code because i do not want to post here my work, so here it is - same problem, try. Thanks for help.

Expected Behavior

It should create something like snake following leds (fade on, fade off when clicked button, in this sequence: gpio 17,16,4,0)

Actual Behavior

It is doing its work, but after switching and clicking button faster, it glitches and leds stays on (video)

Steps to reproduce

  1. copy code from bellow
  2. build and flash to ESP32 with idf.py flash

Code to reproduce this issue

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/ledc.h"
#include "esp_err.h"
#include "driver/gpio.h"

#define LEDC_TEST_CH_NUM (4)
#define LEDC_TEST_DUTY (4095)
#define LEDC_TEST_FADE_TIME (1000)

ledc_channel_config_t ledc_channel[LEDC_TEST_CH_NUM] = {
    {.channel = LEDC_CHANNEL_0,
     .duty = 0,
     .gpio_num = 0,
     .speed_mode = LEDC_HIGH_SPEED_MODE,
     .hpoint = 0,
     .timer_sel = LEDC_TIMER_0},

    {.channel = LEDC_CHANNEL_1,
     .duty = 0,
     .gpio_num = 4,
     .speed_mode = LEDC_HIGH_SPEED_MODE,
     .hpoint = 0,
     .timer_sel = LEDC_TIMER_0},

    {.channel = LEDC_CHANNEL_2,
     .duty = 0,
     .gpio_num = 16,
     .speed_mode = LEDC_HIGH_SPEED_MODE,
     .hpoint = 0,
     .timer_sel = LEDC_TIMER_0},

    {.channel = LEDC_CHANNEL_3,
     .duty = 0,
     .gpio_num = 17,
     .speed_mode = LEDC_HIGH_SPEED_MODE,
     .hpoint = 0,
     .timer_sel = LEDC_TIMER_0},
};

void buttcheck()
{
    bool new, old = false, out = false;
    int ch, z = 0;
    while (1)
    {
        new = gpio_get_level(35);
        if ((new != old) && new)
        {
            switch (z)
            {
            case 0:
                ledc_set_fade_with_time(ledc_channel[0].speed_mode,
                                        ledc_channel[0].channel, 0, LEDC_TEST_FADE_TIME);
                ledc_fade_start(ledc_channel[0].speed_mode,
                                ledc_channel[0].channel, LEDC_FADE_NO_WAIT);

                ledc_set_fade_with_time(ledc_channel[3].speed_mode,
                                        ledc_channel[3].channel, LEDC_TEST_DUTY, LEDC_TEST_FADE_TIME);
                ledc_fade_start(ledc_channel[3].speed_mode,
                                ledc_channel[3].channel, LEDC_FADE_NO_WAIT);
                z++;
                break;
            case 1:
                ledc_set_fade_with_time(ledc_channel[3].speed_mode,
                                        ledc_channel[3].channel, 0, LEDC_TEST_FADE_TIME);
                ledc_fade_start(ledc_channel[3].speed_mode,
                                ledc_channel[3].channel, LEDC_FADE_NO_WAIT);

                ledc_set_fade_with_time(ledc_channel[2].speed_mode,
                                        ledc_channel[2].channel, LEDC_TEST_DUTY, LEDC_TEST_FADE_TIME);
                ledc_fade_start(ledc_channel[2].speed_mode,
                                ledc_channel[2].channel, LEDC_FADE_NO_WAIT);
                z++;
                break;
            case 2:
                ledc_set_fade_with_time(ledc_channel[2].speed_mode,
                                        ledc_channel[2].channel, 0, LEDC_TEST_FADE_TIME);
                ledc_fade_start(ledc_channel[2].speed_mode,
                                ledc_channel[2].channel, LEDC_FADE_NO_WAIT);

                ledc_set_fade_with_time(ledc_channel[1].speed_mode,
                                        ledc_channel[1].channel, LEDC_TEST_DUTY, LEDC_TEST_FADE_TIME);
                ledc_fade_start(ledc_channel[1].speed_mode,
                                ledc_channel[1].channel, LEDC_FADE_NO_WAIT);
                z++;
                break;
            case 3:
                ledc_set_fade_with_time(ledc_channel[1].speed_mode,
                                        ledc_channel[1].channel, 0, LEDC_TEST_FADE_TIME);
                ledc_fade_start(ledc_channel[1].speed_mode,
                                ledc_channel[1].channel, LEDC_FADE_NO_WAIT);

                ledc_set_fade_with_time(ledc_channel[0].speed_mode,
                                        ledc_channel[0].channel, LEDC_TEST_DUTY, LEDC_TEST_FADE_TIME);
                ledc_fade_start(ledc_channel[0].speed_mode,
                                ledc_channel[0].channel, LEDC_FADE_NO_WAIT);
                z = 0;
                break;
            default:
                break;
            }
        }
        old = new;
        vTaskDelay(10 / portTICK_PERIOD_MS);
    }
}

void app_main(void)
{
    int ch;

    gpio_config_t button = {
        .pin_bit_mask = BIT64(35),
        .mode = GPIO_MODE_INPUT,
        .pull_up_en = GPIO_PULLUP_DISABLE,
        .pull_down_en = GPIO_PULLDOWN_ENABLE,
        .intr_type = GPIO_INTR_DISABLE,
    };

    gpio_config(&button);

    ledc_timer_config_t ledc_timer = {
        .duty_resolution = LEDC_TIMER_13_BIT,
        .freq_hz = 5000,
        .speed_mode = LEDC_HIGH_SPEED_MODE,
        .timer_num = LEDC_TIMER_0,
        .clk_cfg = LEDC_AUTO_CLK,
    };

    ledc_timer_config(&ledc_timer);
  
    ledc_fade_func_install(0);

    for (ch = 0; ch < LEDC_TEST_CH_NUM; ch++)
    {
        ledc_channel_config(&ledc_channel[ch]);
    }

    xTaskCreate(buttcheck, NULL, 2048, NULL, 4, NULL);
}
@espressif-bot espressif-bot added the Status: Opened Issue is new label Mar 13, 2021
@github-actions github-actions bot changed the title Ledc driver bug, after fast switching from the 0 duty and 100% duty with fade, the leds stays on Ledc driver bug, after fast switching from the 0 duty and 100% duty with fade, the leds stays on (IDFGH-4918) Mar 13, 2021
@mvarchdev mvarchdev changed the title Ledc driver bug, after fast switching from the 0 duty and 100% duty with fade, the leds stays on (IDFGH-4918) Ledc driver bug, after fast switching between 0% and 100% duty with fade, the leds stays on (IDFGH-4918) Mar 13, 2021
@Alvin1Zhang
Copy link
Collaborator

Thanks for the very detailed report and sorry for the inconvenience, we will look into.

@mvarchdev
Copy link
Author

Thanks for the very detailed report and sorry for the inconvenience, we will look into.

Hey, what is your progress? @Alvin1Zhang Thanks in advance.

@Alvin1Zhang
Copy link
Collaborator

@mvarchdev Thanks for reporting and sorry for the slow turnaround. Thanks for being patient, we had a fix under internal reviewing. The issue will be closed automatically once the fix is available on GitHub. Thanks.

@mvarchdev
Copy link
Author

Hello. How is the fix going on? Thanks in advance.

@espressif-bot espressif-bot added Status: In Progress Work is in progress and removed Status: Opened Issue is new labels Dec 13, 2021
@espressif-bot espressif-bot added Resolution: NA Issue resolution is unavailable Status: Done Issue is done internally Resolution: Done Issue is done internally and removed Status: In Progress Work is in progress Resolution: NA Issue resolution is unavailable labels Jan 26, 2022
dskulina pushed a commit to playable-tech/esp-idf that referenced this issue Feb 4, 2022
Add test cases for fade concurrency issue and fade timing check.

Closes espressif#6710
dskulina pushed a commit to playable-tech/esp-idf that referenced this issue Feb 5, 2022
Add test cases for fade concurrency issue and fade timing check.

Closes espressif#6710
vortigont pushed a commit to vortigont/esp-idf that referenced this issue Feb 19, 2022
Add test cases for fade concurrency issue and fade timing check.

Closes espressif#6710

cherry-pick be2ab09

 Conflicts:
	components/driver/ledc.c
espressif-bot pushed a commit that referenced this issue Feb 23, 2022
Add test cases for fade concurrency issue and fade timing check.

Closes #6710

(cherry picked from commit be2ab09)
espressif-bot pushed a commit that referenced this issue Feb 24, 2022
Add test cases for fade concurrency issue and fade timing check.

Closes #6710

(cherry picked from commit be2ab09)
espressif-bot pushed a commit that referenced this issue Mar 7, 2022
Add test cases for fade concurrency issue and fade timing check.

Closes #6710

(cherry picked from commit be2ab09)
espressif-bot pushed a commit that referenced this issue May 9, 2022
Add test cases for fade concurrency issue and fade timing check.

Closes #6710

(cherry picked from commit be2ab09)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Resolution: Done Issue is done internally Status: Done Issue is done internally
Projects
None yet
Development

No branches or pull requests

3 participants