Strange problem with vTaskDelay()

stefanoxjx
Posts: 25
Joined: Mon Feb 12, 2018 6:26 pm

Strange problem with vTaskDelay()

Postby stefanoxjx » Fri Mar 02, 2018 11:22 am

Hi, I've a strange problem with vTaskDelay().
This is the code:
main.c

Code: Select all

void app_main()
{
  ledInit();

  esp_err_t err = nvs_flash_init();
  
  printf("Hello!!!\n");
}
led.h

Code: Select all

#ifndef LED_H
#define LED_H

#include <stdint.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"

//Prototype
void ledInit(void);
void blinkLedTask(void *pvParameter);
void setLed(TickType_t, uint8_t);

//led pin
const gpio_num_t RLED = 2;
const gpio_num_t GLED = 4;
const gpio_num_t BLED = 5;

//blink mode (ms)
const TickType_t NOBLINK=2;
const TickType_t INITBLINK=1000;
const TickType_t ERRORBLINK=500;
const TickType_t OPERATIONBLINK=30;

//Constant
const bool LEDOFF = 0;
const bool LEDON = 1;

#endif //LED_H
led.c

Code: Select all

#include "led.h"

TickType_t ledMode = 0;
uint8_t ledColor = 0;

void ledInit(void)
{
    //R
    gpio_pad_select_gpio(RLED);
    gpio_set_direction(RLED, GPIO_MODE_OUTPUT);
    gpio_set_level(RLED, LEDON);
    vTaskDelay(500 / portTICK_PERIOD_MS);
    gpio_set_level(RLED, LEDOFF);

    //G
    gpio_pad_select_gpio(GLED);
    gpio_set_direction(GLED, GPIO_MODE_OUTPUT);
    gpio_set_level(GLED, LEDON);
    vTaskDelay(500 / portTICK_PERIOD_MS);
    gpio_set_level(GLED, LEDOFF);

    //B
    gpio_pad_select_gpio(BLED);
    gpio_set_direction(BLED, GPIO_MODE_OUTPUT);
    gpio_set_level(BLED, LEDON);
    vTaskDelay(500 / portTICK_PERIOD_MS);
    gpio_set_level(BLED, LEDOFF);

    //Run task
    xTaskCreate(&blinkLedTask, "blinkLedTask", 2048, NULL, 5, NULL);
}


void setLed(TickType_t blinkMode, uint8_t color)
{
    ledMode = blinkMode; //Imposta la velocità del lampeggio
    ledColor = color;    //Imposta il colore del led
}

void blinkLedTask(void *pvParameter)
{
    static bool ledStatus = false;

    while (1)
    {
        printf("ledMode: %d  ledColor: %d\n", ledMode, ledColor); //for debug
    
        if (ledMode == NOBLINK)
            ledStatus = true;
        else
            ledStatus = !ledStatus;

        //all leds off
        gpio_set_level(RLED, LEDOFF);
        gpio_set_level(GLED, LEDOFF);
        gpio_set_level(BLED, LEDOFF);

   
        //power on selected led
        gpio_set_level(ledColor, ledStatus);

        //vTaskDelay(500 / portTICK_PERIOD_MS);
        vTaskDelay(ledMode / portTICK_PERIOD_MS);
    }
}
If in task "blinkLedTask" I write

Code: Select all

vTaskDelay(ledMode / portTICK_PERIOD_MS)
when I call esp_err_t err = nvs_flash_init(); the result is this:

Code: Select all

Task watchdog got triggered. The following tasks did not reset the watchdog in time:
 - IDLE (CPU 0)
 - IDLE (CPU 1)
Tasks currently running:
CPU 0: blinkLedTask
CPU 1: ipc1
Task watchdog got triggered. The following tasks did not reset the watchdog in time:
 - IDLE (CPU 0)
 - IDLE (CPU 1)
Tasks currently running:
CPU 0: blinkLedTask
CPU 1: ipc1
Task watchdog got triggered. The following tasks did not reset the watchdog in time:
 - IDLE (CPU 0)
 - IDLE (CPU 1)
Tasks currently running:
CPU 0: blinkLedTask
CPU 1: ipc1
then if in task "blinkLedTask" I write a fixed value:

Code: Select all

vTaskDelay(500 / portTICK_PERIOD_MS)
all works fine.

I tried to verify ledMode and ledColor value in "blinkLedTask" and I've seen that If I write "vTaskDelay(500 / portTICK_PERIOD_MS)" the value of these variables is ok.
If I write "vTaskDelay(ledMode / portTICK_PERIOD_MS)" ledMode and ledColor are equal to zero.


What can generate this problem?

Thanks.
Best regards.

Stefano

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: Strange problem with vTaskDelay()

Postby kolban » Fri Mar 02, 2018 2:28 pm

The vTaskDelay will delay for a tick count ... not milliseconds. What is the setting of your ticks per second in your make menuconfig?

If (for example) a tick were every 10 milliseconds then attempting to delay for 2 / portTICK_PERIOD_MS would be a delay of 0 and that may not produce any delay at all. No delay means no opportunity for a context switch (perhaps even a no-operation) ... resulting in a tight loop and watchdog worries.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

Ritesh
Posts: 1383
Joined: Tue Sep 06, 2016 9:37 am
Location: India
Contact:

Re: Strange problem with vTaskDelay()

Postby Ritesh » Sat Mar 03, 2018 2:31 am

stefanoxjx wrote:Hi, I've a strange problem with vTaskDelay().
This is the code:
main.c

Code: Select all

void app_main()
{
  ledInit();

  esp_err_t err = nvs_flash_init();
  
  printf("Hello!!!\n");
}
led.h

Code: Select all

#ifndef LED_H
#define LED_H

#include <stdint.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"

//Prototype
void ledInit(void);
void blinkLedTask(void *pvParameter);
void setLed(TickType_t, uint8_t);

//led pin
const gpio_num_t RLED = 2;
const gpio_num_t GLED = 4;
const gpio_num_t BLED = 5;

//blink mode (ms)
const TickType_t NOBLINK=2;
const TickType_t INITBLINK=1000;
const TickType_t ERRORBLINK=500;
const TickType_t OPERATIONBLINK=30;

//Constant
const bool LEDOFF = 0;
const bool LEDON = 1;

#endif //LED_H
led.c

Code: Select all

#include "led.h"

TickType_t ledMode = 0;
uint8_t ledColor = 0;

void ledInit(void)
{
    //R
    gpio_pad_select_gpio(RLED);
    gpio_set_direction(RLED, GPIO_MODE_OUTPUT);
    gpio_set_level(RLED, LEDON);
    vTaskDelay(500 / portTICK_PERIOD_MS);
    gpio_set_level(RLED, LEDOFF);

    //G
    gpio_pad_select_gpio(GLED);
    gpio_set_direction(GLED, GPIO_MODE_OUTPUT);
    gpio_set_level(GLED, LEDON);
    vTaskDelay(500 / portTICK_PERIOD_MS);
    gpio_set_level(GLED, LEDOFF);

    //B
    gpio_pad_select_gpio(BLED);
    gpio_set_direction(BLED, GPIO_MODE_OUTPUT);
    gpio_set_level(BLED, LEDON);
    vTaskDelay(500 / portTICK_PERIOD_MS);
    gpio_set_level(BLED, LEDOFF);

    //Run task
    xTaskCreate(&blinkLedTask, "blinkLedTask", 2048, NULL, 5, NULL);
}


void setLed(TickType_t blinkMode, uint8_t color)
{
    ledMode = blinkMode; //Imposta la velocità del lampeggio
    ledColor = color;    //Imposta il colore del led
}

void blinkLedTask(void *pvParameter)
{
    static bool ledStatus = false;

    while (1)
    {
        printf("ledMode: %d  ledColor: %d\n", ledMode, ledColor); //for debug
    
        if (ledMode == NOBLINK)
            ledStatus = true;
        else
            ledStatus = !ledStatus;

        //all leds off
        gpio_set_level(RLED, LEDOFF);
        gpio_set_level(GLED, LEDOFF);
        gpio_set_level(BLED, LEDOFF);

   
        //power on selected led
        gpio_set_level(ledColor, ledStatus);

        //vTaskDelay(500 / portTICK_PERIOD_MS);
        vTaskDelay(ledMode / portTICK_PERIOD_MS);
    }
}
If in task "blinkLedTask" I write

Code: Select all

vTaskDelay(ledMode / portTICK_PERIOD_MS)
when I call esp_err_t err = nvs_flash_init(); the result is this:

Code: Select all

Task watchdog got triggered. The following tasks did not reset the watchdog in time:
 - IDLE (CPU 0)
 - IDLE (CPU 1)
Tasks currently running:
CPU 0: blinkLedTask
CPU 1: ipc1
Task watchdog got triggered. The following tasks did not reset the watchdog in time:
 - IDLE (CPU 0)
 - IDLE (CPU 1)
Tasks currently running:
CPU 0: blinkLedTask
CPU 1: ipc1
Task watchdog got triggered. The following tasks did not reset the watchdog in time:
 - IDLE (CPU 0)
 - IDLE (CPU 1)
Tasks currently running:
CPU 0: blinkLedTask
CPU 1: ipc1
then if in task "blinkLedTask" I write a fixed value:

Code: Select all

vTaskDelay(500 / portTICK_PERIOD_MS)
all works fine.

I tried to verify ledMode and ledColor value in "blinkLedTask" and I've seen that If I write "vTaskDelay(500 / portTICK_PERIOD_MS)" the value of these variables is ok.
If I write "vTaskDelay(ledMode / portTICK_PERIOD_MS)" ledMode and ledColor are equal to zero.


What can generate this problem?

Thanks.
Best regards.

Stefano
As per log it says that your task is not feeding watchdog means not able to do context switching to give chance to other tasks like IDLE or other tasks.

You need to check make menuconfig as well to verify ticks per seconds as well as task watchdog time out which s default 5 second I believe.

So, you need to provide proper delay into vTaskDelete API to provide context switching for other tasks otherwise it will create Watchdog issue.

Or you need to increase Task Watchdog value using make menuconfig whose range is till 60 seconds but that is not proper way if you task is in blocking state forever.

Hope you understand it and let me know if you need any help.
Regards,
Ritesh Prajapati

stefanoxjx
Posts: 25
Joined: Mon Feb 12, 2018 6:26 pm

Re: Strange problem with vTaskDelay()

Postby stefanoxjx » Sat Mar 03, 2018 5:45 pm

Hi, I don't understand :(
In "make menuconfig" I see:

Code: Select all

(100) Tick rate (Hz)
But why if I write the line

Code: Select all

vTaskDelay(500 / portTICK_PERIOD_MS);
the task work fine and with:

Code: Select all

vTaskDelay(ledMode / portTICK_PERIOD_MS);
the task doesn't work though ledMode is equal to 500?

Can you explain me?
Thanks.
Regards.

Stefano

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: Strange problem with vTaskDelay()

Postby WiFive » Sat Mar 03, 2018 8:48 pm

You never call setled and ledmode is initialized to 0

Ritesh
Posts: 1383
Joined: Tue Sep 06, 2016 9:37 am
Location: India
Contact:

Re: Strange problem with vTaskDelay()

Postby Ritesh » Sat Mar 03, 2018 10:53 pm

stefanoxjx wrote:Hi, I don't understand :(
In "make menuconfig" I see:

Code: Select all

(100) Tick rate (Hz)
But why if I write the line

Code: Select all

vTaskDelay(500 / portTICK_PERIOD_MS);
the task work fine and with:

Code: Select all

vTaskDelay(ledMode / portTICK_PERIOD_MS);
the task doesn't work though ledMode is equal to 500?

Can you explain me?
Thanks.
Regards.

Stefano
If Value of ledmode is 500 then both will be idle in that condition and it should work.

So please make sure value for that using debug print
Regards,
Ritesh Prajapati

Who is online

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