Time Acquisition and Calibration
Introduction
This section has been systematically narrated in the System Time of the ESP-IDF Programming Guide, and this document is only a supplementary summary.
Common Time Acquisition Interfaces
- esp_timer_get_time
Get the time since esp_timer initialization, in μs
Use RTC for timing when the ESP chip is in sleep mode, and compensate for time through RTC after exiting sleep mode
- esp_rtc_get_time_us
Get the time of the RTC counter (RTC_SLOW_CLK), in μs
- esp_cpu_get_cycle_count
First get the CPU cycle count, then divide by the result corresponding to esp_rom_get_cpu_ticks_per_us() to get the corresponding time, in μs
- gettimeofday
Use esp_timer as the clock source, in μs
Use RTC for timing when the ESP chip is in sleep mode, and compensate for time through RTC after exiting sleep mode
- xTaskGetTickCount
Get the FreeRTOS Tick count, in Ticks
Here are some experience summaries:
To get the current time, it is recommended to use the POSIX function gettimeofday, but this function has a slightly higher overhead
Using esp_timer_get_time can generate timestamps with microsecond precision, but each call to the timing function will generate a certain amount of overhead
When measuring small code segments that run for less than 1-2 ms, there may be large differences in timing measurements due to the function being in flash. This problem can be solved by moving the code to IRAM
By using GPIO to quickly flip, very small code execution times can be observed through a logic analyzer
RC Clock Calibration
There are many sources of RTC clock, but only the external 32K clock can provide a higher precision clock, while other RC clocks have lower precision and will accumulate more errors after running for a long time. There is no significant difference in the precision of different clock sources inside the RTC.