system_time.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Copyright 2017 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // Provides strong definition for system time functions relied upon
  15. // by core components.
  16. #include "sdkconfig.h"
  17. #if CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER
  18. #include "esp_timer.h"
  19. #include "esp_timer_impl.h"
  20. #include "esp_private/startup_internal.h"
  21. #if CONFIG_IDF_TARGET_ESP32
  22. #include "esp32/rtc.h"
  23. #elif CONFIG_IDF_TARGET_ESP32S2
  24. #include "esp32s2/rtc.h"
  25. #elif CONFIG_IDF_TARGET_ESP32S3
  26. #include "esp32s3/rtc.h"
  27. #elif CONFIG_IDF_TARGET_ESP32C3
  28. #include "esp32c3/rtc.h"
  29. #elif CONFIG_IDF_TARGET_ESP32H2
  30. #include "esp32h2/rtc.h"
  31. #endif
  32. // Correction for underlying timer to keep definition
  33. // of system time consistent.
  34. static int64_t s_correction_us = 0;
  35. void esp_timer_impl_init_system_time(void)
  36. {
  37. s_correction_us = esp_rtc_get_time_us() - g_startup_time - esp_timer_impl_get_time();
  38. }
  39. int64_t IRAM_ATTR esp_system_get_time(void)
  40. {
  41. return esp_timer_get_time() + s_correction_us;
  42. }
  43. uint32_t IRAM_ATTR esp_system_get_time_resolution(void)
  44. {
  45. return 1000;
  46. }
  47. #endif