time.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // Copyright 2015-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. #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 <rom/rtc.h>
  24. #include "esp_attr.h"
  25. #include "esp_intr_alloc.h"
  26. #include "esp_clk.h"
  27. #include "soc/soc.h"
  28. #include "soc/rtc.h"
  29. #include "soc/rtc_cntl_reg.h"
  30. #include "soc/frc_timer_reg.h"
  31. #include "rom/ets_sys.h"
  32. #include "freertos/FreeRTOS.h"
  33. #include "freertos/xtensa_api.h"
  34. #include "freertos/task.h"
  35. #include "sdkconfig.h"
  36. #if defined( CONFIG_ESP32_TIME_SYSCALL_USE_RTC ) || defined( CONFIG_ESP32_TIME_SYSCALL_USE_RTC_FRC1 )
  37. #define WITH_RTC 1
  38. #endif
  39. #if defined( CONFIG_ESP32_TIME_SYSCALL_USE_FRC1 ) || defined( CONFIG_ESP32_TIME_SYSCALL_USE_RTC_FRC1 )
  40. #define WITH_FRC1 1
  41. #endif
  42. #ifdef WITH_RTC
  43. static uint64_t get_rtc_time_us()
  44. {
  45. uint64_t ticks = rtc_time_get();
  46. return (uint32_t) ((ticks * esp_clk_slowclk_cal_get()) >> RTC_CLK_CAL_FRACT);
  47. }
  48. #endif // WITH_RTC
  49. // s_boot_time: time from Epoch to the first boot time
  50. #ifdef WITH_RTC
  51. // when RTC is used to persist time, two RTC_STORE registers are used to store boot time
  52. #elif defined(WITH_FRC1)
  53. static uint64_t s_boot_time;
  54. #endif
  55. #if defined(WITH_RTC) || defined(WITH_FRC1)
  56. static _lock_t s_boot_time_lock;
  57. #endif
  58. #ifdef WITH_FRC1
  59. #define FRC1_PRESCALER 16
  60. #define FRC1_PRESCALER_CTL 2
  61. #define FRC1_TICK_FREQ (APB_CLK_FREQ / FRC1_PRESCALER)
  62. #define FRC1_TICKS_PER_US (FRC1_TICK_FREQ / 1000000)
  63. #define FRC1_ISR_PERIOD_US (FRC_TIMER_LOAD_VALUE(0) / FRC1_TICKS_PER_US)
  64. // Counter frequency will be APB_CLK_FREQ / 16 = 5 MHz
  65. // 1 tick = 0.2 us
  66. // Timer has 23 bit counter, so interrupt will fire each 1677721.6 microseconds.
  67. // This is not a whole number, so timer will drift by 0.3 ppm due to rounding error.
  68. static volatile uint64_t s_microseconds = 0;
  69. static void IRAM_ATTR frc_timer_isr()
  70. {
  71. // Write to FRC_TIMER_INT_REG may not take effect in some cases (root cause TBD)
  72. // This extra write works around this issue.
  73. // FRC_TIMER_LOAD_REG(0) is used here, but any other DPORT register address can also be used.
  74. WRITE_PERI_REG(FRC_TIMER_LOAD_REG(0), FRC_TIMER_LOAD_VALUE(0));
  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. #if defined(WITH_RTC) || defined(WITH_FRC1)
  80. static void set_boot_time(uint64_t time_us)
  81. {
  82. _lock_acquire(&s_boot_time_lock);
  83. #ifdef WITH_RTC
  84. REG_WRITE(RTC_BOOT_TIME_LOW_REG, (uint32_t) (time_us & 0xffffffff));
  85. REG_WRITE(RTC_BOOT_TIME_HIGH_REG, (uint32_t) (time_us >> 32));
  86. #else
  87. s_boot_time = time_us;
  88. #endif
  89. _lock_release(&s_boot_time_lock);
  90. }
  91. static uint64_t get_boot_time()
  92. {
  93. uint64_t result;
  94. _lock_acquire(&s_boot_time_lock);
  95. #ifdef WITH_RTC
  96. result = ((uint64_t) REG_READ(RTC_BOOT_TIME_LOW_REG)) + (((uint64_t) REG_READ(RTC_BOOT_TIME_HIGH_REG)) << 32);
  97. #else
  98. result = s_boot_time;
  99. #endif
  100. _lock_release(&s_boot_time_lock);
  101. return result;
  102. }
  103. #endif //defined(WITH_RTC) || defined(WITH_FRC1)
  104. void esp_setup_time_syscalls()
  105. {
  106. #if defined( WITH_FRC1 )
  107. #if defined( WITH_RTC )
  108. // initialize time from RTC clock
  109. s_microseconds = get_rtc_time_us();
  110. #endif //WITH_RTC
  111. // set up timer
  112. WRITE_PERI_REG(FRC_TIMER_CTRL_REG(0), \
  113. FRC_TIMER_AUTOLOAD | \
  114. (FRC1_PRESCALER_CTL << FRC_TIMER_PRESCALER_S) | \
  115. FRC_TIMER_EDGE_INT);
  116. WRITE_PERI_REG(FRC_TIMER_LOAD_REG(0), FRC_TIMER_LOAD_VALUE(0));
  117. SET_PERI_REG_MASK(FRC_TIMER_CTRL_REG(0),
  118. FRC_TIMER_ENABLE | \
  119. FRC_TIMER_INT_ENABLE);
  120. esp_intr_alloc(ETS_TIMER1_INTR_SOURCE, 0, &frc_timer_isr, NULL, NULL);
  121. #endif // WITH_FRC1
  122. }
  123. clock_t IRAM_ATTR _times_r(struct _reent *r, struct tms *ptms)
  124. {
  125. clock_t t = xTaskGetTickCount() * (portTICK_PERIOD_MS * CLK_TCK / 1000);
  126. ptms->tms_cstime = 0;
  127. ptms->tms_cutime = 0;
  128. ptms->tms_stime = t;
  129. ptms->tms_utime = 0;
  130. struct timeval tv = {0, 0};
  131. _gettimeofday_r(r, &tv, NULL);
  132. return (clock_t) tv.tv_sec;
  133. }
  134. #if defined( WITH_FRC1 ) || defined( WITH_RTC )
  135. static uint64_t get_time_since_boot()
  136. {
  137. uint64_t microseconds = 0;
  138. #ifdef WITH_FRC1
  139. uint32_t timer_ticks_before = READ_PERI_REG(FRC_TIMER_COUNT_REG(0));
  140. microseconds = s_microseconds;
  141. uint32_t timer_ticks_after = READ_PERI_REG(FRC_TIMER_COUNT_REG(0));
  142. if (timer_ticks_after > timer_ticks_before) {
  143. // overflow happened at some point between getting
  144. // timer_ticks_before and timer_ticks_after
  145. // microseconds value is ambiguous, get a new one
  146. microseconds = s_microseconds;
  147. }
  148. microseconds += (FRC_TIMER_LOAD_VALUE(0) - timer_ticks_after) / FRC1_TICKS_PER_US;
  149. #elif defined(WITH_RTC)
  150. microseconds = get_rtc_time_us();
  151. #endif
  152. return microseconds;
  153. }
  154. #endif // defined( WITH_FRC1 ) || defined( WITH_RTC )
  155. int IRAM_ATTR _gettimeofday_r(struct _reent *r, struct timeval *tv, void *tz)
  156. {
  157. (void) tz;
  158. #if defined( WITH_FRC1 ) || defined( WITH_RTC )
  159. if (tv) {
  160. uint64_t microseconds = get_boot_time() + get_time_since_boot();
  161. tv->tv_sec = microseconds / 1000000;
  162. tv->tv_usec = microseconds % 1000000;
  163. }
  164. return 0;
  165. #else
  166. __errno_r(r) = ENOSYS;
  167. return -1;
  168. #endif // defined( WITH_FRC1 ) || defined( WITH_RTC )
  169. }
  170. int settimeofday(const struct timeval *tv, const struct timezone *tz)
  171. {
  172. (void) tz;
  173. #if defined( WITH_FRC1 ) || defined( WITH_RTC )
  174. if (tv) {
  175. uint64_t now = ((uint64_t) tv->tv_sec) * 1000000LL + tv->tv_usec;
  176. uint64_t since_boot = get_time_since_boot();
  177. set_boot_time(now - since_boot);
  178. }
  179. return 0;
  180. #else
  181. errno = ENOSYS;
  182. return -1;
  183. #endif
  184. }
  185. uint32_t system_get_time(void)
  186. {
  187. #if defined( WITH_FRC1 ) || defined( WITH_RTC )
  188. return get_time_since_boot();
  189. #else
  190. return 0;
  191. #endif
  192. }
  193. uint32_t system_get_current_time(void) __attribute__((alias("system_get_time")));
  194. uint32_t system_relative_time(uint32_t current_time)
  195. {
  196. return system_get_time() - current_time;
  197. }
  198. uint64_t system_get_rtc_time(void)
  199. {
  200. #ifdef WITH_RTC
  201. return get_rtc_time_us();
  202. #else
  203. return 0;
  204. #endif
  205. }