system_time.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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_system.h"
  21. #include "esp_newlib.h"
  22. #include "esp_log.h"
  23. #include "esp_private/startup_internal.h"
  24. #if CONFIG_IDF_TARGET_ESP32
  25. #include "esp32/rtc.h"
  26. #elif CONFIG_IDF_TARGET_ESP32S2
  27. #include "esp32s2/rtc.h"
  28. #elif CONFIG_IDF_TARGET_ESP32S3
  29. #include "esp32s3/rtc.h"
  30. #elif CONFIG_IDF_TARGET_ESP32C3
  31. #include "esp32c3/rtc.h"
  32. #elif CONFIG_IDF_TARGET_ESP32H2
  33. #include "esp32h2/rtc.h"
  34. #endif
  35. __attribute__((unused)) static const char* TAG = "system_time";
  36. // Correction for underlying timer to keep definition
  37. // of system time consistent.
  38. static int64_t s_correction_us = 0;
  39. void esp_timer_impl_init_system_time(void)
  40. {
  41. s_correction_us = esp_rtc_get_time_us() - g_startup_time - esp_timer_impl_get_time();
  42. #if defined(CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER) && defined(CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER)
  43. esp_err_t err = esp_register_shutdown_handler(esp_sync_counters_rtc_and_frc);
  44. if (err != ESP_OK) {
  45. ESP_LOGW(TAG, "Register shutdown handler failed, err = 0x%x", err);
  46. }
  47. #endif
  48. }
  49. int64_t IRAM_ATTR esp_system_get_time(void)
  50. {
  51. return esp_timer_get_time() + s_correction_us;
  52. }
  53. uint32_t IRAM_ATTR esp_system_get_time_resolution(void)
  54. {
  55. return 1000;
  56. }
  57. #endif