esp_timer_impl.h 5.0 KB

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