time.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // Copyright 2015-2016 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 <errno.h>
  15. #include <stdlib.h>
  16. #include <time.h>
  17. #include <reent.h>
  18. #include <sys/types.h>
  19. #include <sys/reent.h>
  20. #include <sys/time.h>
  21. #include <sys/times.h>
  22. #include <sys/lock.h>
  23. #include "esp_attr.h"
  24. #include "esp_intr_alloc.h"
  25. #include "soc/soc.h"
  26. #include "soc/rtc_cntl_reg.h"
  27. #include "soc/frc_timer_reg.h"
  28. #include "rom/ets_sys.h"
  29. #include "freertos/FreeRTOS.h"
  30. #include "freertos/xtensa_api.h"
  31. #include "freertos/task.h"
  32. #include "sdkconfig.h"
  33. #if defined( CONFIG_ESP32_TIME_SYSCALL_USE_RTC ) || defined( CONFIG_ESP32_TIME_SYSCALL_USE_RTC_FRC1 )
  34. #define WITH_RTC 1
  35. #endif
  36. #if defined( CONFIG_ESP32_TIME_SYSCALL_USE_FRC1 ) || defined( CONFIG_ESP32_TIME_SYSCALL_USE_RTC_FRC1 )
  37. #define WITH_FRC1 1
  38. #endif
  39. #ifdef WITH_RTC
  40. static uint64_t get_rtc_time_us()
  41. {
  42. SET_PERI_REG_MASK(RTC_CNTL_TIME_UPDATE_REG, RTC_CNTL_TIME_UPDATE_M);
  43. while (GET_PERI_REG_MASK(RTC_CNTL_TIME_UPDATE_REG, RTC_CNTL_TIME_VALID_M) == 0) {
  44. ;
  45. }
  46. CLEAR_PERI_REG_MASK(RTC_CNTL_TIME_UPDATE_REG, RTC_CNTL_TIME_UPDATE_M);
  47. uint64_t low = READ_PERI_REG(RTC_CNTL_TIME0_REG);
  48. uint64_t high = READ_PERI_REG(RTC_CNTL_TIME1_REG);
  49. uint64_t ticks = (high << 32) | low;
  50. return ticks * 100 / (RTC_CTNL_SLOWCLK_FREQ / 10000); // scale RTC_CTNL_SLOWCLK_FREQ to avoid overflow
  51. }
  52. #endif // WITH_RTC
  53. // s_boot_time: time from Epoch to the first boot time
  54. #ifdef WITH_RTC
  55. static RTC_DATA_ATTR struct timeval s_boot_time;
  56. #elif defined(WITH_FRC1)
  57. static struct timeval s_boot_time;
  58. #endif
  59. #if defined(WITH_RTC) || defined(WITH_FRC1)
  60. static _lock_t s_boot_time_lock;
  61. #endif
  62. #ifdef WITH_FRC1
  63. #define FRC1_PRESCALER 16
  64. #define FRC1_PRESCALER_CTL 2
  65. #define FRC1_TICK_FREQ (APB_CLK_FREQ / FRC1_PRESCALER)
  66. #define FRC1_TICKS_PER_US (FRC1_TICK_FREQ / 1000000)
  67. #define FRC1_ISR_PERIOD_US (FRC_TIMER_LOAD_VALUE(0) / FRC1_TICKS_PER_US)
  68. // Counter frequency will be APB_CLK_FREQ / 16 = 5 MHz
  69. // 1 tick = 0.2 us
  70. // Timer has 23 bit counter, so interrupt will fire each 1677721.6 microseconds.
  71. // This is not a whole number, so timer will drift by 0.3 ppm due to rounding error.
  72. static volatile uint64_t s_microseconds = 0;
  73. static void IRAM_ATTR frc_timer_isr()
  74. {
  75. WRITE_PERI_REG(FRC_TIMER_INT_REG(0), FRC_TIMER_INT_CLR);
  76. s_microseconds += FRC1_ISR_PERIOD_US;
  77. }
  78. #endif // WITH_FRC1
  79. void esp_setup_time_syscalls()
  80. {
  81. #if defined( WITH_FRC1 )
  82. #if defined( WITH_RTC )
  83. // initialize time from RTC clock
  84. s_microseconds = get_rtc_time_us();
  85. #endif //WITH_RTC
  86. // set up timer
  87. WRITE_PERI_REG(FRC_TIMER_CTRL_REG(0), \
  88. FRC_TIMER_AUTOLOAD | \
  89. (FRC1_PRESCALER_CTL << FRC_TIMER_PRESCALER_S) | \
  90. FRC_TIMER_EDGE_INT);
  91. WRITE_PERI_REG(FRC_TIMER_LOAD_REG(0), FRC_TIMER_LOAD_VALUE(0));
  92. SET_PERI_REG_MASK(FRC_TIMER_CTRL_REG(0),
  93. FRC_TIMER_ENABLE | \
  94. FRC_TIMER_INT_ENABLE);
  95. esp_intr_alloc(ETS_TIMER1_INTR_SOURCE, 0, &frc_timer_isr, NULL, NULL);
  96. #endif // WITH_FRC1
  97. }
  98. clock_t IRAM_ATTR _times_r(struct _reent *r, struct tms *ptms)
  99. {
  100. clock_t t = xTaskGetTickCount() * (portTICK_PERIOD_MS * CLK_TCK / 1000);
  101. ptms->tms_cstime = 0;
  102. ptms->tms_cutime = 0;
  103. ptms->tms_stime = t;
  104. ptms->tms_utime = 0;
  105. struct timeval tv = {0, 0};
  106. _gettimeofday_r(r, &tv, NULL);
  107. return (clock_t) tv.tv_sec;
  108. }
  109. #if defined( WITH_FRC1 ) || defined( WITH_RTC )
  110. static uint64_t get_time_since_boot()
  111. {
  112. uint64_t microseconds = 0;
  113. #ifdef WITH_FRC1
  114. uint32_t timer_ticks_before = READ_PERI_REG(FRC_TIMER_COUNT_REG(0));
  115. microseconds = s_microseconds;
  116. uint32_t timer_ticks_after = READ_PERI_REG(FRC_TIMER_COUNT_REG(0));
  117. if (timer_ticks_after > timer_ticks_before) {
  118. // overflow happened at some point between getting
  119. // timer_ticks_before and timer_ticks_after
  120. // microseconds value is ambiguous, get a new one
  121. microseconds = s_microseconds;
  122. }
  123. microseconds += (FRC_TIMER_LOAD_VALUE(0) - timer_ticks_after) / FRC1_TICKS_PER_US;
  124. #elif defined(WITH_RTC)
  125. microseconds = get_rtc_time_us();
  126. #endif
  127. return microseconds;
  128. }
  129. #endif // defined( WITH_FRC1 ) || defined( WITH_RTC )
  130. int IRAM_ATTR _gettimeofday_r(struct _reent *r, struct timeval *tv, void *tz)
  131. {
  132. (void) tz;
  133. #if defined( WITH_FRC1 ) || defined( WITH_RTC )
  134. uint64_t microseconds = get_time_since_boot();
  135. if (tv) {
  136. _lock_acquire(&s_boot_time_lock);
  137. microseconds += s_boot_time.tv_usec;
  138. tv->tv_sec = s_boot_time.tv_sec + microseconds / 1000000;
  139. tv->tv_usec = microseconds % 1000000;
  140. _lock_release(&s_boot_time_lock);
  141. }
  142. return 0;
  143. #else
  144. __errno_r(r) = ENOSYS;
  145. return -1;
  146. #endif // defined( WITH_FRC1 ) || defined( WITH_RTC )
  147. }
  148. int settimeofday(const struct timeval *tv, const struct timezone *tz)
  149. {
  150. (void) tz;
  151. #if defined( WITH_FRC1 ) || defined( WITH_RTC )
  152. if (tv) {
  153. _lock_acquire(&s_boot_time_lock);
  154. uint64_t now = ((uint64_t) tv->tv_sec) * 1000000LL + tv->tv_usec;
  155. uint64_t since_boot = get_time_since_boot();
  156. uint64_t boot_time = now - since_boot;
  157. s_boot_time.tv_sec = boot_time / 1000000;
  158. s_boot_time.tv_usec = boot_time % 1000000;
  159. _lock_release(&s_boot_time_lock);
  160. }
  161. return 0;
  162. #else
  163. errno = ENOSYS;
  164. return -1;
  165. #endif
  166. }
  167. uint32_t system_get_time(void)
  168. {
  169. #if defined( WITH_FRC1 ) || defined( WITH_RTC )
  170. return get_time_since_boot();
  171. #else
  172. return 0;
  173. #endif
  174. }
  175. uint32_t system_get_current_time(void) __attribute__((alias("system_get_time")));
  176. uint32_t system_relative_time(uint32_t current_time)
  177. {
  178. return system_get_time() - current_time;
  179. }
  180. uint64_t system_get_rtc_time(void)
  181. {
  182. #ifdef WITH_RTC
  183. return get_rtc_time_us();
  184. #else
  185. return 0;
  186. #endif
  187. }