time.c 6.2 KB

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