esp_timer_impl.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * SPDX-FileCopyrightText: 2017-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. /**
  8. * @file private_include/esp_timer_impl.h
  9. *
  10. * @brief Interface between common and platform-specific parts of esp_timer.
  11. *
  12. * The functions in this header file are implemented for each supported SoC.
  13. * High level functions defined in esp_timer.c call the functions here to
  14. * interact with the hardware.
  15. */
  16. #include <stdint.h>
  17. #include "esp_err.h"
  18. #include "esp_intr_alloc.h"
  19. /**
  20. * @brief Minimal initialization of platform specific layer of esp_timer
  21. * This function can be called very early in startup process, after this call
  22. * only esp_timer_get_time function can be used.
  23. * esp_timer_impl_init has to be called after this function to initialize the
  24. * rest of esp_timer implementation.
  25. * @return ESP_OK
  26. */
  27. esp_err_t esp_timer_impl_early_init(void);
  28. /**
  29. * @brief Initialize platform specific layer of esp_timer
  30. * @param alarm_handler function to call on timer interrupt
  31. * Before calling this function, esp_timer_impl_early_init must be called.
  32. * @return ESP_OK, ESP_ERR_NO_MEM, or one of the errors from interrupt allocator
  33. */
  34. esp_err_t esp_timer_impl_init(intr_handler_t alarm_handler);
  35. /**
  36. * @brief Deinitialize platform specific layer of esp_timer
  37. */
  38. void esp_timer_impl_deinit(void);
  39. /**
  40. * @brief Set up the timer interrupt to fire at a particular time
  41. *
  42. * If the alarm time is too close in the future, implementation should set the
  43. * alarm to the earliest time possible.
  44. *
  45. * @param timestamp time in microseconds when interrupt should fire (relative to
  46. * boot time, i.e. as returned by esp_timer_impl_get_time)
  47. */
  48. void esp_timer_impl_set_alarm(uint64_t timestamp);
  49. /**
  50. * @brief Notify esp_timer implementation that APB frequency has changed
  51. *
  52. * Called by the frequency switching code.
  53. *
  54. * @param apb_ticks_per_us new number of APB clock ticks per microsecond
  55. */
  56. void esp_timer_impl_update_apb_freq(uint32_t apb_ticks_per_us);
  57. /**
  58. * @brief Adjust current esp_timer time by a certain value
  59. *
  60. * Called from light sleep code to synchronize esp_timer time with RTC time.
  61. *
  62. * @param time_us adjustment to apply to esp_timer time, in microseconds
  63. */
  64. void esp_timer_impl_advance(int64_t time_us);
  65. /**
  66. * @brief Get time, in microseconds, since esp_timer_impl_init was called
  67. * @return timestamp in microseconds
  68. */
  69. int64_t esp_timer_impl_get_time(void);
  70. /**
  71. * @brief Get minimal timer period, in microseconds
  72. * Periods shorter than the one returned may not be possible to achieve due to
  73. * interrupt latency and context switch time. Short period of periodic timer may
  74. * cause the system to spend all the time servicing the interrupt and timer
  75. * callback, preventing other tasks from running.
  76. * @return minimal period of periodic timer, in microseconds
  77. */
  78. uint64_t esp_timer_impl_get_min_period_us(void);
  79. /**
  80. * @brief obtain internal critical section used esp_timer implementation
  81. * This can be used when a sequence of calls to esp_timer has to be made,
  82. * and it is necessary that the state of the timer is consistent between
  83. * the calls. Should be treated in the same way as a spinlock.
  84. * Call esp_timer_impl_unlock to release the lock
  85. */
  86. void esp_timer_impl_lock(void);
  87. /**
  88. * @brief counterpart of esp_timer_impl_lock
  89. */
  90. void esp_timer_impl_unlock(void);
  91. /**
  92. * @brief Get counting register
  93. *
  94. * Bit depth dependents on implementation and can be 32-bit or 64-bit.
  95. *
  96. * @return the value of the counting register
  97. */
  98. uint64_t esp_timer_impl_get_counter_reg(void);
  99. /**
  100. * @brief Get alarm register
  101. *
  102. * Bit depth dependents on implementation and can be 32-bit or 64-bit.
  103. *
  104. * @return the value of the alarm register
  105. */
  106. uint64_t esp_timer_impl_get_alarm_reg(void);
  107. #if CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER
  108. /**
  109. * @brief Initialize esp_timer as system time provider.
  110. */
  111. void esp_timer_impl_init_system_time(void);
  112. #endif