Timer tick

zliudr
Posts: 357
Joined: Thu Oct 03, 2019 5:15 am

Timer tick

Postby zliudr » Mon Oct 21, 2019 9:10 pm

How do I read the timer tick since the processor has started, like an arduino millisecond tick with millis()? I'm reading the timer section:

https://docs.espressif.com/projects/esp ... timer.html

This seems an overkill when I'm only reading the time tick to determine whether something has occurred long enough in the past.

Thanks.

BTW, I found the xTaskCreate() function that I was looking for in another post :)

markkuk
Posts: 37
Joined: Wed Mar 27, 2019 11:50 am

Re: Timer tick

Postby markkuk » Tue Oct 22, 2019 5:38 am


zliudr
Posts: 357
Joined: Thu Oct 03, 2019 5:15 am

Re: Timer tick

Postby zliudr » Tue Oct 22, 2019 12:48 pm

Thanks. I found it after reading a lot of Google search results. If I want to do a delay, is there a convenience function like the delay() in arduino? I'm using a while loop with the vTaskDelay. What should be the minimal delay for vTaskDelay call? If I am in the main program, can I still use vTaskDelay?

ESP_Dazz
Posts: 308
Joined: Fri Jun 02, 2017 6:50 am

Re: Timer tick

Postby ESP_Dazz » Tue Oct 22, 2019 2:01 pm

zliudr wrote: If I want to do a delay, is there a convenience function like the delay() in arduino? I'm using a while loop with the vTaskDelay.
vTaskDelay() should work as a simple delay. vTaskDelay() essentially blocks the calling task (thread) for a specified number of FreeRTOS ticks. However, since its resolution is FreeRTOS ticks, you should not expect the delay to be high resolution or high accuracy (i.e., the exact delay interval that occurs may be affected by other factors such as higher priority tasks).
zliudr wrote:If I am in the main program, can I still use vTaskDelay?
Do you mean app_main()? If so, then yes, app_main() is a function that is simply called from the main task (thread) which in turn is created automatically on startup.
zliudr wrote:What should be the minimal delay for vTaskDelay call?
The unit of delay used in vTaskDelay() is in terms of FreeRTOS ticks. The FreeRTOS tick frequency is set by default to 100Hz, meaning a tick will occur every 1ms. Therefore calling vTaskDelay(1) will block the calling task by 1ms. Note that you can change the FreeRTOS tick frequency using menuconfig.

You can also use the pdMS_TO_TICKS() macro which will convert a delay interval in ms to the equivalent of FreeRTOS ticks. For example, calling vTaskDelay(pdMS_TO_TICKS(1500)) to delay for 1500ms will be equivalent to calling vTaskDelay(150).

zliudr
Posts: 357
Joined: Thu Oct 03, 2019 5:15 am

Re: Timer tick

Postby zliudr » Tue Oct 22, 2019 2:12 pm

Thank you! If the FreeRTOS tick 1ms, then it is 1000 Hz, not 100 Hz, right? Or do you mean tick is 10ms each so 100 Hz is correct? I read your example of the macro and think I should just go with the pdMS_TO_TICKS().

BTW, do you know how long does it take to switch between tasks? I want to be able to adjust the tick but don't want it too quick and cpu just keep switching tasks and not doing actual work. If I remember correctly, it takes a 80386 4us to switch task :D

ESP_Dazz
Posts: 308
Joined: Fri Jun 02, 2017 6:50 am

Re: Timer tick

Postby ESP_Dazz » Tue Oct 22, 2019 3:12 pm

zliudr wrote: Thank you! If the FreeRTOS tick 1ms, then it is 1000 Hz, not 100 Hz, right? Or do you mean tick is 10ms each so 100 Hz is correct? I read your example of the macro and think I should just go with the pdMS_TO_TICKS().
Yep, I meant 10ms. My bad :lol:
zliudr wrote: BTW, do you know how long does it take to switch between tasks? I want to be able to adjust the tick but don't want it too quick and cpu just keep switching tasks and not doing actual work. If I remember correctly, it takes a 80386 4us to switch task
Im not too sure about how long it tasks to switch tasks, but from a simple test, a task switching due to vTaskDelay() takes approx 2300 CPU cycles, which at 160MHz CPU will be approx 14us. But in general, you shouldn't expect precise time from using vTaskDelay(). If you require precise time keeping, I suggest you use a hardware timer instead.

zliudr
Posts: 357
Joined: Thu Oct 03, 2019 5:15 am

Re: Timer tick

Postby zliudr » Tue Oct 22, 2019 3:51 pm

No problem Dazz. I'm not concerned with accuracy, just heuristic method to wait for UART response instead of checking every cpu cycle. I read somewhere about 100 ticks per second but the whole esp-idf document is over 1600 pages long. You guys have made an extremely impressive platform, to say the least. It's just massive amount of knowledge I need to absorb. Does espressif provide any engineering consulting service so I can talk with a developer? I am potentially interested in paying for help at a good price. I can speak both English and Mandarin. PM me.

Who is online

Users browsing this forum: Baidu [Spider], ESP_Roland, ESP_rrtandler and 120 guests