esp_timer_is_active() status after deleting a timer

rocotocloc
Posts: 10
Joined: Tue May 07, 2024 5:47 am

esp_timer_is_active() status after deleting a timer

Postby rocotocloc » Thu May 23, 2024 7:43 am

Hello,

I am using the function esp_timer_is_active() to detect whether a particular timer is active or not but the behaviour is misleading according to the documentation.

Everything works as expected except this function returns TRUE (active) after the timer has been deleted. Is this the expected behaviour? If so, how can I detect whether the timer is still active after being deleted? Do I have to manually set the timer to NULL again by myself?

Please check this example:

Code: Select all

#include <stdio.h>
#include <inttypes.h>
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_chip_info.h"
#include "esp_flash.h"
#include "esp_system.h"
#include <esp_timer.h>
#include <esp_err.h>
#include <esp_log.h>

static const char *TAG = "log";

static void my_timer_callback(void *arg)
{
    ESP_LOGI(TAG, "my_timer_callback");
}

void app_main(void)
{
    esp_timer_handle_t my_timer = NULL;
    esp_timer_create_args_t timer_args = {
        .callback = &my_timer_callback,
    };

    // check the active is not active at the beggining
    if (esp_timer_is_active(my_timer))
    {
        ESP_LOGE(TAG, "TIMER IS ACTIVE");
    }
    else
    {
        ESP_LOGI(TAG, "timer is not active");
    }

    // just create the timer and ensure it's not still active
    ESP_LOGI(TAG, "creating timer");
    ESP_ERROR_CHECK(esp_timer_create(&timer_args, &my_timer));
    if (esp_timer_is_active(my_timer))
    {
        ESP_LOGE(TAG, "TIMER IS ACTIVE");
    }
    else
    {
        ESP_LOGI(TAG, "timer is not active");
    }

    // now we start the timer
    ESP_ERROR_CHECK(esp_timer_start_periodic(my_timer, 3000000));

    if (esp_timer_is_active(my_timer))
    {
        // we ensure is active at this point
        ESP_LOGI(TAG, "timer is active, let's try to stop it");

        // now we stop it and check if it's active after that
        if (esp_timer_stop(my_timer) == ESP_OK)
        {
            if (esp_timer_is_active(my_timer))
            {
                ESP_LOGE(TAG, "TIMER IS ACTIVE AFTER STOPPED");
            }
            else
            {
                ESP_LOGI(TAG, "timer is not active after being stopped");
            }
            ESP_LOGI(TAG, "timer correctly stopped, let's try to delete it");

            // now let's delete it and check again
            if (esp_timer_delete(my_timer) == ESP_OK)
            {
                ESP_LOGI(TAG, "timer correctly deleted");
                if (esp_timer_is_active(my_timer))
                {
                    ESP_LOGE(TAG, "TIMER IS ACTIVE AGAIN AFTER BEING DELETED --> ???");
                }
                else
                {
                    ESP_LOGI(TAG, "timer is not active");
                }
            }
            else
            {
                ESP_LOGE(TAG, "TIMER NOT DELETED");
            }
        }
        else
        {
            ESP_LOGE(TAG, "TIMER NOT STOPPED");
        }
    }
    else
    {
        ESP_LOGE(TAG, "TIMER IS NOT ACTIVE");
    }


}

And this is the output:

I (313) main_task: Calling app_main()
I (313) log: timer is not active
I (313) log: creating timer
I (313) log: timer is not active
I (313) log: timer is active, let's try to stop it
I (323) log: timer is not active after being stopped
I (333) log: timer correctly stopped, let's try to delete it
I (333) log: timer correctly deleted

E (343) log: TIMER IS ACTIVE AGAIN AFTER BEING DELETED --> ???
I (343) main_task: Returned from app_main()

ESP_Sprite
Posts: 9673
Joined: Thu Nov 26, 2015 4:08 am

Re: esp_timer_is_active() status after deleting a timer

Postby ESP_Sprite » Fri May 24, 2024 7:03 am

There is no timer anymore after you delete it; you must not use the handle anymore as it points at non-referenced memory and results if you do so anyway are undefined.

rocotocloc
Posts: 10
Joined: Tue May 07, 2024 5:47 am

Re: esp_timer_is_active() status after deleting a timer

Postby rocotocloc » Fri May 24, 2024 8:38 am

Ok thanks, understood.

Who is online

Users browsing this forum: No registered users and 60 guests