How to get two HS LEDC channels fading in two "parallel" Tasks?

kb3523
Posts: 10
Joined: Thu Mar 08, 2018 11:32 pm

How to get two HS LEDC channels fading in two "parallel" Tasks?

Postby kb3523 » Thu Mar 08, 2018 11:54 pm

Dear ESP Communitiy,

I can't get this working - what am I missing? Also tried using two different Timers for this. Both tasks are running as far as I can tell, but the second output stays shut. I tried at first fading led1 and as it finishes fading led2 without using tasks with the same configuration and that worked.

Code: Select all

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

#define LEDC_FAST_FADE_TIME (1000)
#define LEDC_SLOW_FADE_TIME (1000)
#define LEDC_DUTY         (4000)
#define LED_GREEN_GPIO (GPIO_NUM_22)
#define LED_RED_GPIO (GPIO_NUM_21)

ledc_channel_config_t green_led_conf;
ledc_channel_config_t red_led_conf;

static const char *TAG = "#LOG:";

static void blink_red (void)
{
	ledc_set_fade_with_time(red_led_conf.speed_mode,red_led_conf.channel, LEDC_DUTY, LEDC_SLOW_FADE_TIME);
	ledc_fade_start(red_led_conf.speed_mode, red_led_conf.channel, LEDC_FADE_NO_WAIT);
	vTaskDelay(LEDC_SLOW_FADE_TIME / portTICK_PERIOD_MS);

	ledc_set_fade_with_time(red_led_conf.speed_mode, red_led_conf.channel, 0, LEDC_SLOW_FADE_TIME);
	ledc_fade_start(red_led_conf.speed_mode, red_led_conf.channel, LEDC_FADE_NO_WAIT);
	vTaskDelay(LEDC_SLOW_FADE_TIME / portTICK_PERIOD_MS);
}

static void blink_green (void)
{
	ledc_set_fade_with_time(green_led_conf.speed_mode, green_led_conf.channel, LEDC_DUTY, LEDC_FAST_FADE_TIME);
	ledc_fade_start(green_led_conf.speed_mode, green_led_conf.channel, LEDC_FADE_NO_WAIT);
	vTaskDelay(LEDC_FAST_FADE_TIME / portTICK_PERIOD_MS);

	ledc_set_fade_with_time(green_led_conf.speed_mode, green_led_conf.channel, 0, LEDC_FAST_FADE_TIME);
	ledc_fade_start(green_led_conf.speed_mode, green_led_conf.channel, LEDC_FADE_NO_WAIT);
	vTaskDelay(LEDC_FAST_FADE_TIME / portTICK_PERIOD_MS);
}

static void blink_red_task (void *pvParameter)
{
	while (1)
	{
		ESP_LOGI(TAG, "RED LED IS BLINKING.. TASK RUNNING");
		blink_red();
	}
}

static void blink_green_fast_task (void *pvParameter)
{
	while (1)
	{
		ESP_LOGI(TAG, "GREEN LED IS BLINKING FAST.. TASK RUNNING");
		blink_green();
	}
}

static void init_ledc (void)
{
    ledc_timer_config_t ledc_timer = {
        .bit_num = LEDC_TIMER_13_BIT, 		  // resolution
        .freq_hz = 5000,                      // frequency of PWM signal
        .speed_mode = LEDC_HIGH_SPEED_MODE,           // timer mode
        .timer_num = LEDC_TIMER_0            // timer index
    };

    // Set configuration of timer0 for high speed channels
    ledc_timer_config(&ledc_timer);

    // Prepare channel configs
    ledc_channel_config_t green_led_conf =
		{
			.channel    = LEDC_CHANNEL_0,
			.duty       = 0,
			.gpio_num   = (GPIO_NUM_22),//LED_GREEN_GPIO,
			.speed_mode = LEDC_HIGH_SPEED_MODE,
			.timer_sel  = LEDC_TIMER_0,
			.intr_type = LEDC_INTR_DISABLE
		};

    ledc_channel_config_t red_led_conf =
		{
			.channel    = LEDC_CHANNEL_1,
			.duty       = 0,
			.gpio_num   = (GPIO_NUM_21), //LED_RED_GPIO,
			.speed_mode = LEDC_HIGH_SPEED_MODE,
			.timer_sel  = LEDC_TIMER_0,
			.intr_type = LEDC_INTR_DISABLE
		};

    // Set LED Controller with previously prepared configuration
	ledc_channel_config(&red_led_conf);
	ledc_channel_config(&green_led_conf);

    // Initialize fade service.
	ledc_fade_func_install(0);

}

void app_main()
{
	init_ledc();

	blink_red();
	blink_green();

	xTaskCreate(&blink_red_task, "blink_red_task", 2048, NULL, 5, NULL);
	vTaskDelay(100);
	xTaskCreate(&blink_green_fast_task, "blink_green_fast_task", 2048, NULL, 5, NULL);

}
If

Any suggestions are welcome ;) Thank you!

kb3523
Posts: 10
Joined: Thu Mar 08, 2018 11:32 pm

Re: How to get two HS LEDC channels fading in two "parallel" Tasks?

Postby kb3523 » Fri Mar 09, 2018 10:38 am

I forget to mention, that with this code was successfully fading both outputs:

Code: Select all

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

#define LEDC_HS_CH0_GPIO       (GPIO_NUM_21)
#define LEDC_HS_CH1_GPIO       (GPIO_NUM_22)
#define LEDC_DUTY         (4000)
#define LEDC_TEST_FADE_TIME    (1000)
#define LEDC_FAST_FADE_TIME (250)
#define LEDC_SLOW_FADE_TIME (500)

void app_main()
{

    ledc_timer_config_t ledc_timer = {
        .bit_num = LEDC_TIMER_13_BIT, // resolution of PWM duty Later: .duty_resolution =
        .freq_hz = 5000,                      // frequency of PWM signal
        .speed_mode = LEDC_HIGH_SPEED_MODE,           // timer mode
        .timer_num = LEDC_TIMER_0            // timer index
    };

    // Set configuration of timer0 for high speed channels
    ledc_timer_config(&ledc_timer);

    // Prepare channel configs
    ledc_channel_config_t ledc_channel[2] = {
        {
            .channel    = LEDC_CHANNEL_0,
            .duty       = 0,
            .gpio_num   = LEDC_HS_CH0_GPIO,
            .speed_mode = LEDC_HIGH_SPEED_MODE,
            .timer_sel  = LEDC_TIMER_0
        },
        {
            .channel    = LEDC_CHANNEL_1,
            .duty       = 0,
            .gpio_num   = LEDC_HS_CH1_GPIO,
            .speed_mode = LEDC_HIGH_SPEED_MODE,
            .timer_sel  = LEDC_TIMER_0
        }
    };

    // Set LED Controller with previously prepared configuration
    ledc_channel_config(&ledc_channel[0]);
    ledc_channel_config(&ledc_channel[1]);

    // Initialize fade service.
    ledc_fade_func_install(0);

    while (1) {
		printf("LEDC 1 fade up to duty = %d\n", LEDC_DUTY);
		ledc_set_fade_with_time(ledc_channel[1].speed_mode, ledc_channel[1].channel, LEDC_DUTY, LEDC_TEST_FADE_TIME);
		ledc_fade_start(ledc_channel[1].speed_mode, ledc_channel[1].channel, LEDC_FADE_NO_WAIT);
		vTaskDelay(LEDC_TEST_FADE_TIME / portTICK_PERIOD_MS);

		printf("LEDC 1 fade down to duty = 0\n");
		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);
		vTaskDelay(LEDC_TEST_FADE_TIME / portTICK_PERIOD_MS);

		printf("LEDC 2 fade up to duty = %d\n", LEDC_DUTY);
		ledc_set_fade_with_time(ledc_channel[0].speed_mode, ledc_channel[0].channel, LEDC_DUTY, LEDC_SLOW_FADE_TIME);
		ledc_fade_start(ledc_channel[0].speed_mode,	ledc_channel[0].channel, LEDC_FADE_NO_WAIT);
		vTaskDelay(LEDC_SLOW_FADE_TIME / portTICK_PERIOD_MS);

		printf("LEDC 2 fade up to duty = %d\n", LEDC_DUTY);
		ledc_set_fade_with_time(ledc_channel[0].speed_mode, ledc_channel[0].channel, 0, LEDC_SLOW_FADE_TIME);
		ledc_fade_start(ledc_channel[0].speed_mode,ledc_channel[0].channel, LEDC_FADE_NO_WAIT);
		vTaskDelay(LEDC_SLOW_FADE_TIME / portTICK_PERIOD_MS);

		printf("LEDC 1 set DUTY to %d\n", LEDC_DUTY);
		ledc_set_duty(ledc_channel[0].speed_mode, ledc_channel[0].channel, LEDC_DUTY);
		ledc_update_duty(ledc_channel[0].speed_mode, ledc_channel[0].channel);
		vTaskDelay(1000 / portTICK_PERIOD_MS);

		printf("LEDC 1 set duty = 0\n");
		ledc_set_duty(ledc_channel[0].speed_mode, ledc_channel[0].channel, 0);
		ledc_update_duty(ledc_channel[0].speed_mode, ledc_channel[0].channel);
		vTaskDelay(1000 / portTICK_PERIOD_MS);
    }
}
Maybe for one of you it is obvious why this example works and mine posted above don't, but I cant see any real difference. I'm also quiet new to RTOS and ESP-IDF. Thank you for reading this through! :)

kb3523
Posts: 10
Joined: Thu Mar 08, 2018 11:32 pm

Re: How to get two HS LEDC channels fading in two "parallel" Tasks?

Postby kb3523 » Fri Mar 09, 2018 9:27 pm

Sorry :)
I was too noob to initialize an array of structs correctly..

False:

Code: Select all

ledc_channel_config_t ledc_channel[2];
   .
   .
   .
ledc_channel_config_t ledc_channel[2] = {
        {
            .channel    = LEDC_CHANNEL_0,
            .duty       = 0,
.
.
Right:

Code: Select all

   ledc_channel_config_t ledc_channel[2];
   .
   .
   .
ledc_channel[0].channel   = LEDC_CHANNEL_0;
 ledc_channel[0].duty= 0;
         .
         .

Who is online

Users browsing this forum: Bing [Bot] and 135 guests