Unstable resume task
Posted: Thu Jan 11, 2024 7:27 am
Hello,
I need to delay a task for variable times between 1500 to 4500 microseconds. To do so I used the High Resolution Timer (ESP Timer).
To check that implementation I came up with the following piece of code:
The problem is whenever I decrease the timer to less than 3000 microseconds the program will hang after several iterations. I believe it hangs due to the fact that the task is not in suspend state when the timer is off. I can't understand why the task can't enter the suspend state as there is ample time to do that (in order of few milliseconds).
Here is a sample of the last part of the code output:
I need to delay a task for variable times between 1500 to 4500 microseconds. To do so I used the High Resolution Timer (ESP Timer).
To check that implementation I came up with the following piece of code:
- #include <stdio.h>
- #include "freertos/FreeRTOS.h"
- #include "freertos/task.h"
- #include "esp_log.h"
- #include <esp_err.h>
- #include <sys/stat.h>
- #include "freertos/timers.h"
- #include "esp_timer.h"
- #include "sdkconfig.h"
- esp_timer_handle_t theTimer_handler;
- TaskHandle_t task_handler = NULL;
- static void timer_callback(void *param){
- vTaskResume(task_handler);
- printf("task state: %d\n",(int)eTaskGetState( task_handler ));
- printf("Timer: %lld μs\n",esp_timer_get_time());
- }
- void initTimer(){
- // define timmer
- const esp_timer_create_args_t timer_args = {
- .callback = &timer_callback,
- .name = "Timer"};
- // create timmer
- ESP_ERROR_CHECK(esp_timer_create(&timer_args, &theTimer_handler));
- }
- static void the_task(void *arg)
- {
- ESP_ERROR_CHECK(esp_timer_start_once(theTimer_handler, 2800));
- vTaskSuspend(task_handler); // wait for next time
- while (1) {
- ESP_ERROR_CHECK(esp_timer_start_once(theTimer_handler, 2800));
- long long int Timer = esp_timer_get_time();
- printf("SYS Timer: %lld μs\n",esp_timer_get_time());
- vTaskSuspend(task_handler); // wait for next time
- printf("back\n");
- }
- }
- void app_main(void)
- {
- initTimer();
- xTaskCreatePinnedToCore(the_task, "the_task",8196 , NULL, 5, &task_handler,1);
- while (1){
- vTaskDelay(1000 / portTICK_PERIOD_MS);
- }
- }
- [/code]
Here is a sample of the last part of the code output:
Any idea about what am I doing wrong?SYS Timer: 2465120 μs
task state: 1
Timer: 2468504 μs
back
SYS Timer: 2470762 μs
task state: 1
Timer: 2474146 μs
back
SYS Timer: 2476404 μs
task state: 1
Timer: 2479789 μs
back
SYS Timer: 2482047 μs
task state: 1
Timer: 2485431 μs
back
SYS Timer: 2487689 μs
task state: 1
Timer: 2491073 μs
back
SYS Timer: 2493331 μs
task state: 1
back
Timer: 2496727 μs
SYS Timer: 2497243 μs
task state: 0
Timer: 2502354 μs