system_time.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // Copyright 2020 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. #include "esp_system.h"
  15. #include "esp_attr.h"
  16. #include "soc/spinlock.h"
  17. #include "soc/rtc.h"
  18. #include "sdkconfig.h"
  19. #if CONFIG_IDF_TARGET_ESP32
  20. #include "esp32/rtc.h"
  21. #elif CONFIG_IDF_TARGET_ESP32S2
  22. #include "esp32s2/rtc.h"
  23. #endif
  24. #include "esp_private/startup_internal.h"
  25. // A component in the build should provide strong implementations that make use of
  26. // and actual hardware timer to provide timekeeping functions.
  27. int64_t IRAM_ATTR __attribute__((weak)) esp_system_get_time(void)
  28. {
  29. int64_t t = 0;
  30. static spinlock_t s_time_lock = SPINLOCK_INITIALIZER;
  31. spinlock_acquire(&s_time_lock, SPINLOCK_WAIT_FOREVER);
  32. t = (esp_rtc_get_time_us() - g_startup_time);
  33. spinlock_release(&s_time_lock);
  34. return t;
  35. }
  36. uint32_t IRAM_ATTR __attribute__((weak)) esp_system_get_time_resolution(void)
  37. {
  38. return 1000000L / rtc_clk_slow_freq_get_hz();
  39. }