test_time.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <math.h>
  9. #include "unity.h"
  10. #include <time.h>
  11. #include <sys/time.h>
  12. #include "freertos/FreeRTOS.h"
  13. #include "freertos/task.h"
  14. #include "freertos/semphr.h"
  15. #include "sdkconfig.h"
  16. #include "soc/rtc.h"
  17. #include "esp_system.h"
  18. #include "test_utils.h"
  19. #include "esp_log.h"
  20. #include "esp_rom_sys.h"
  21. #include "esp_system.h"
  22. #include "esp_timer.h"
  23. #include "esp_private/system_internal.h"
  24. #include "esp_private/esp_timer_private.h"
  25. #include "../priv_include/esp_time_impl.h"
  26. #include "esp_private/system_internal.h"
  27. #include "esp_private/esp_clk.h"
  28. #if CONFIG_IDF_TARGET_ESP32
  29. #include "esp32/rtc.h"
  30. #elif CONFIG_IDF_TARGET_ESP32S2
  31. #include "esp32s2/rtc.h"
  32. #elif CONFIG_IDF_TARGET_ESP32S3
  33. #include "esp32s3/rtc.h"
  34. #elif CONFIG_IDF_TARGET_ESP32C3
  35. #include "esp32c3/rtc.h"
  36. #elif CONFIG_IDF_TARGET_ESP32H4
  37. #include "esp32h4/rtc.h"
  38. #elif CONFIG_IDF_TARGET_ESP32C2
  39. #include "esp32c2/rtc.h"
  40. #elif CONFIG_IDF_TARGET_ESP32C6
  41. #include "esp32c6/rtc.h"
  42. #elif CONFIG_IDF_TARGET_ESP32H2
  43. #include "esp32h2/rtc.h"
  44. #endif
  45. #if portNUM_PROCESSORS == 2
  46. #include "soc/rtc_cntl_reg.h"
  47. // This runs on APP CPU:
  48. static void time_adc_test_task(void* arg)
  49. {
  50. for (int i = 0; i < 200000; ++i) {
  51. // wait for 20us, reading one of RTC registers
  52. uint32_t ccount = xthal_get_ccount();
  53. while (xthal_get_ccount() - ccount < 20 * CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ) {
  54. volatile uint32_t val = REG_READ(RTC_CNTL_STATE0_REG);
  55. (void) val;
  56. }
  57. }
  58. SemaphoreHandle_t * p_done = (SemaphoreHandle_t *) arg;
  59. xSemaphoreGive(*p_done);
  60. vTaskDelay(1);
  61. vTaskDelete(NULL);
  62. }
  63. // https://github.com/espressif/arduino-esp32/issues/120
  64. TEST_CASE("Reading RTC registers on APP CPU doesn't affect clock", "[newlib]")
  65. {
  66. SemaphoreHandle_t done = xSemaphoreCreateBinary();
  67. xTaskCreatePinnedToCore(&time_adc_test_task, "time_adc", 4096, &done, 5, NULL, 1);
  68. // This runs on PRO CPU:
  69. for (int i = 0; i < 4; ++i) {
  70. struct timeval tv_start;
  71. gettimeofday(&tv_start, NULL);
  72. vTaskDelay(1000/portTICK_PERIOD_MS);
  73. struct timeval tv_stop;
  74. gettimeofday(&tv_stop, NULL);
  75. float time_sec = tv_stop.tv_sec - tv_start.tv_sec + 1e-6f * (tv_stop.tv_usec - tv_start.tv_usec);
  76. printf("(0) time taken: %f sec\n", time_sec);
  77. TEST_ASSERT_TRUE(fabs(time_sec - 1.0f) < 0.1);
  78. }
  79. TEST_ASSERT_TRUE(xSemaphoreTake(done, 5000 / portTICK_PERIOD_MS));
  80. }
  81. #endif // portNUM_PROCESSORS == 2
  82. TEST_CASE("test adjtime function", "[newlib]")
  83. {
  84. struct timeval tv_time;
  85. struct timeval tv_delta;
  86. struct timeval tv_outdelta;
  87. TEST_ASSERT_EQUAL(adjtime(NULL, NULL), 0);
  88. tv_time.tv_sec = 5000;
  89. tv_time.tv_usec = 5000;
  90. TEST_ASSERT_EQUAL(settimeofday(&tv_time, NULL), 0);
  91. tv_outdelta.tv_sec = 5;
  92. tv_outdelta.tv_usec = 5;
  93. TEST_ASSERT_EQUAL(adjtime(NULL, &tv_outdelta), 0);
  94. TEST_ASSERT_EQUAL(tv_outdelta.tv_sec, 0);
  95. TEST_ASSERT_EQUAL(tv_outdelta.tv_usec, 0);
  96. tv_delta.tv_sec = INT_MAX / 1000000L;
  97. TEST_ASSERT_EQUAL(adjtime(&tv_delta, &tv_outdelta), -1);
  98. tv_delta.tv_sec = INT_MIN / 1000000L;
  99. TEST_ASSERT_EQUAL(adjtime(&tv_delta, &tv_outdelta), -1);
  100. tv_delta.tv_sec = 0;
  101. tv_delta.tv_usec = -900000;
  102. TEST_ASSERT_EQUAL(adjtime(&tv_delta, &tv_outdelta), 0);
  103. TEST_ASSERT_EQUAL(tv_outdelta.tv_sec, 0);
  104. TEST_ASSERT_EQUAL(tv_outdelta.tv_usec, 0);
  105. TEST_ASSERT_EQUAL(adjtime(NULL, &tv_outdelta), 0);
  106. TEST_ASSERT_LESS_THAN(-800000, tv_outdelta.tv_usec);
  107. tv_delta.tv_sec = -4;
  108. tv_delta.tv_usec = -900000;
  109. TEST_ASSERT_EQUAL(adjtime(&tv_delta, NULL), 0);
  110. TEST_ASSERT_EQUAL(adjtime(NULL, &tv_outdelta), 0);
  111. TEST_ASSERT_EQUAL(tv_outdelta.tv_sec, -4);
  112. TEST_ASSERT_LESS_THAN(-800000, tv_outdelta.tv_usec);
  113. // after settimeofday() adjtime() is stopped
  114. tv_delta.tv_sec = 15;
  115. tv_delta.tv_usec = 900000;
  116. TEST_ASSERT_EQUAL(adjtime(&tv_delta, &tv_outdelta), 0);
  117. TEST_ASSERT_EQUAL(tv_outdelta.tv_sec, -4);
  118. TEST_ASSERT_LESS_THAN(-800000, tv_outdelta.tv_usec);
  119. TEST_ASSERT_EQUAL(adjtime(NULL, &tv_outdelta), 0);
  120. TEST_ASSERT_EQUAL(tv_outdelta.tv_sec, 15);
  121. TEST_ASSERT_GREATER_OR_EQUAL(800000, tv_outdelta.tv_usec);
  122. TEST_ASSERT_EQUAL(gettimeofday(&tv_time, NULL), 0);
  123. TEST_ASSERT_EQUAL(settimeofday(&tv_time, NULL), 0);
  124. TEST_ASSERT_EQUAL(adjtime(NULL, &tv_outdelta), 0);
  125. TEST_ASSERT_EQUAL(tv_outdelta.tv_sec, 0);
  126. TEST_ASSERT_EQUAL(tv_outdelta.tv_usec, 0);
  127. // after gettimeofday() adjtime() is not stopped
  128. tv_delta.tv_sec = 15;
  129. tv_delta.tv_usec = 900000;
  130. TEST_ASSERT_EQUAL(adjtime(&tv_delta, &tv_outdelta), 0);
  131. TEST_ASSERT_EQUAL(tv_outdelta.tv_sec, 0);
  132. TEST_ASSERT_EQUAL(tv_outdelta.tv_usec, 0);
  133. TEST_ASSERT_EQUAL(adjtime(NULL, &tv_outdelta), 0);
  134. TEST_ASSERT_EQUAL(tv_outdelta.tv_sec, 15);
  135. TEST_ASSERT_GREATER_OR_EQUAL(800000, tv_outdelta.tv_usec);
  136. TEST_ASSERT_EQUAL(gettimeofday(&tv_time, NULL), 0);
  137. TEST_ASSERT_EQUAL(adjtime(NULL, &tv_outdelta), 0);
  138. TEST_ASSERT_EQUAL(tv_outdelta.tv_sec, 15);
  139. TEST_ASSERT_GREATER_OR_EQUAL(800000, tv_outdelta.tv_usec);
  140. tv_delta.tv_sec = 1;
  141. tv_delta.tv_usec = 0;
  142. TEST_ASSERT_EQUAL(adjtime(&tv_delta, NULL), 0);
  143. vTaskDelay(1000 / portTICK_PERIOD_MS);
  144. TEST_ASSERT_EQUAL(adjtime(NULL, &tv_outdelta), 0);
  145. TEST_ASSERT_EQUAL(tv_outdelta.tv_sec, 0);
  146. // the correction will be equal to (1_000_000us >> 6) = 15_625 us.
  147. TEST_ASSERT_TRUE(1000000L - tv_outdelta.tv_usec >= 15600);
  148. TEST_ASSERT_TRUE(1000000L - tv_outdelta.tv_usec <= 15650);
  149. }
  150. static volatile bool exit_flag;
  151. static void adjtimeTask2(void *pvParameters)
  152. {
  153. SemaphoreHandle_t *sema = (SemaphoreHandle_t *) pvParameters;
  154. struct timeval delta = {.tv_sec = 0, .tv_usec = 0};
  155. struct timeval outdelta;
  156. // although exit flag is set in another task, checking (exit_flag == false) is safe
  157. while (exit_flag == false) {
  158. delta.tv_sec += 1;
  159. delta.tv_usec = 900000;
  160. if (delta.tv_sec >= 2146) delta.tv_sec = 1;
  161. adjtime(&delta, &outdelta);
  162. }
  163. xSemaphoreGive(*sema);
  164. vTaskDelete(NULL);
  165. }
  166. static void timeTask(void *pvParameters)
  167. {
  168. SemaphoreHandle_t *sema = (SemaphoreHandle_t *) pvParameters;
  169. struct timeval tv_time = { .tv_sec = 1520000000, .tv_usec = 900000 };
  170. // although exit flag is set in another task, checking (exit_flag == false) is safe
  171. while (exit_flag == false) {
  172. tv_time.tv_sec += 1;
  173. settimeofday(&tv_time, NULL);
  174. gettimeofday(&tv_time, NULL);
  175. }
  176. xSemaphoreGive(*sema);
  177. vTaskDelete(NULL);
  178. }
  179. TEST_CASE("test for no interlocking adjtime, gettimeofday and settimeofday functions", "[newlib]")
  180. {
  181. TaskHandle_t th[4];
  182. exit_flag = false;
  183. struct timeval tv_time = { .tv_sec = 1520000000, .tv_usec = 900000 };
  184. TEST_ASSERT_EQUAL(settimeofday(&tv_time, NULL), 0);
  185. const int max_tasks = 2;
  186. SemaphoreHandle_t exit_sema[max_tasks];
  187. for (int i = 0; i < max_tasks; ++i) {
  188. exit_sema[i] = xSemaphoreCreateBinary();
  189. }
  190. #ifndef CONFIG_FREERTOS_UNICORE
  191. printf("CPU0 and CPU1. Tasks run: 1 - adjtimeTask, 2 - gettimeofdayTask, 3 - settimeofdayTask \n");
  192. xTaskCreatePinnedToCore(adjtimeTask2, "adjtimeTask2", 2048, &exit_sema[0], UNITY_FREERTOS_PRIORITY - 1, &th[0], 0);
  193. xTaskCreatePinnedToCore(timeTask, "timeTask", 2048, &exit_sema[1], UNITY_FREERTOS_PRIORITY - 1, &th[1], 1);
  194. #else
  195. printf("Only one CPU. Tasks run: 1 - adjtimeTask, 2 - gettimeofdayTask, 3 - settimeofdayTask\n");
  196. xTaskCreate(adjtimeTask2, "adjtimeTask2", 2048, &exit_sema[0], UNITY_FREERTOS_PRIORITY - 1, &th[0]);
  197. xTaskCreate(timeTask, "timeTask", 2048, &exit_sema[1], UNITY_FREERTOS_PRIORITY - 1, &th[1]);
  198. #endif
  199. printf("start wait for 5 seconds\n");
  200. vTaskDelay(5000 / portTICK_PERIOD_MS);
  201. // set exit flag to let thread exit
  202. exit_flag = true;
  203. for (int i = 0; i < max_tasks; ++i) {
  204. if (!xSemaphoreTake(exit_sema[i], 2000/portTICK_PERIOD_MS)) {
  205. TEST_FAIL_MESSAGE("exit_sema not released by test task");
  206. }
  207. vSemaphoreDelete(exit_sema[i]);
  208. }
  209. }
  210. #ifndef CONFIG_FREERTOS_UNICORE
  211. #define ADJTIME_CORRECTION_FACTOR 6
  212. static int64_t result_adjtime_correction_us[2];
  213. static void get_time_task(void *pvParameters)
  214. {
  215. SemaphoreHandle_t *sema = (SemaphoreHandle_t *) pvParameters;
  216. struct timeval tv_time;
  217. // although exit flag is set in another task, checking (exit_flag == false) is safe
  218. while (exit_flag == false) {
  219. gettimeofday(&tv_time, NULL);
  220. vTaskDelay(1500 / portTICK_PERIOD_MS);
  221. }
  222. xSemaphoreGive(*sema);
  223. vTaskDelete(NULL);
  224. }
  225. static void start_measure(int64_t* sys_time, int64_t* real_time)
  226. {
  227. struct timeval tv_time;
  228. // there shouldn't be much time between gettimeofday and esp_timer_get_time
  229. gettimeofday(&tv_time, NULL);
  230. *real_time = esp_timer_get_time();
  231. *sys_time = (int64_t)tv_time.tv_sec * 1000000L + tv_time.tv_usec;
  232. }
  233. static int64_t calc_correction(const char* tag, int64_t* sys_time, int64_t* real_time)
  234. {
  235. int64_t dt_real_time_us = real_time[1] - real_time[0];
  236. int64_t dt_sys_time_us = sys_time[1] - sys_time[0];
  237. int64_t calc_correction_us = dt_real_time_us >> ADJTIME_CORRECTION_FACTOR;
  238. int64_t real_correction_us = dt_sys_time_us - dt_real_time_us;
  239. int64_t error_us = calc_correction_us - real_correction_us;
  240. printf("%s: dt_real_time = %lli us, dt_sys_time = %lli us, calc_correction = %lli us, error = %lli us\n",
  241. tag, dt_real_time_us, dt_sys_time_us, calc_correction_us, error_us);
  242. TEST_ASSERT_TRUE(dt_sys_time_us > 0 && dt_real_time_us > 0);
  243. TEST_ASSERT_INT_WITHIN(100, 0, error_us);
  244. return real_correction_us;
  245. }
  246. static void measure_time_task(void *pvParameters)
  247. {
  248. SemaphoreHandle_t *sema = (SemaphoreHandle_t *) pvParameters;
  249. int64_t main_real_time_us[2];
  250. int64_t main_sys_time_us[2];
  251. struct timeval tv_time = {.tv_sec = 1550000000, .tv_usec = 0};
  252. TEST_ASSERT_EQUAL(0, settimeofday(&tv_time, NULL));
  253. struct timeval delta = {.tv_sec = 2000, .tv_usec = 900000};
  254. adjtime(&delta, NULL);
  255. gettimeofday(&tv_time, NULL);
  256. start_measure(&main_sys_time_us[0], &main_real_time_us[0]);
  257. {
  258. int64_t real_time_us[2] = { main_real_time_us[0], 0};
  259. int64_t sys_time_us[2] = { main_sys_time_us[0], 0};
  260. // although exit flag is set in another task, checking (exit_flag == false) is safe
  261. while (exit_flag == false) {
  262. vTaskDelay(2000 / portTICK_PERIOD_MS);
  263. start_measure(&sys_time_us[1], &real_time_us[1]);
  264. result_adjtime_correction_us[1] += calc_correction("measure", sys_time_us, real_time_us);
  265. sys_time_us[0] = sys_time_us[1];
  266. real_time_us[0] = real_time_us[1];
  267. }
  268. main_sys_time_us[1] = sys_time_us[1];
  269. main_real_time_us[1] = real_time_us[1];
  270. }
  271. result_adjtime_correction_us[0] = calc_correction("main", main_sys_time_us, main_real_time_us);
  272. int64_t delta_us = result_adjtime_correction_us[0] - result_adjtime_correction_us[1];
  273. printf("\nresult of adjtime correction: %lli us, %lli us. delta = %lli us\n", result_adjtime_correction_us[0], result_adjtime_correction_us[1], delta_us);
  274. TEST_ASSERT_INT_WITHIN(100, 0, delta_us);
  275. xSemaphoreGive(*sema);
  276. vTaskDelete(NULL);
  277. }
  278. TEST_CASE("test time adjustment happens linearly", "[newlib][timeout=15]")
  279. {
  280. exit_flag = false;
  281. SemaphoreHandle_t exit_sema[2];
  282. for (int i = 0; i < 2; ++i) {
  283. exit_sema[i] = xSemaphoreCreateBinary();
  284. result_adjtime_correction_us[i] = 0;
  285. }
  286. xTaskCreatePinnedToCore(get_time_task, "get_time_task", 4096, &exit_sema[0], UNITY_FREERTOS_PRIORITY - 1, NULL, 0);
  287. xTaskCreatePinnedToCore(measure_time_task, "measure_time_task", 4096, &exit_sema[1], UNITY_FREERTOS_PRIORITY - 1, NULL, 1);
  288. printf("start waiting for 10 seconds\n");
  289. vTaskDelay(10000 / portTICK_PERIOD_MS);
  290. // set exit flag to let thread exit
  291. exit_flag = true;
  292. for (int i = 0; i < 2; ++i) {
  293. if (!xSemaphoreTake(exit_sema[i], 2100/portTICK_PERIOD_MS)) {
  294. TEST_FAIL_MESSAGE("exit_sema not released by test task");
  295. }
  296. }
  297. for (int i = 0; i < 2; ++i) {
  298. vSemaphoreDelete(exit_sema[i]);
  299. }
  300. }
  301. #endif
  302. void test_posix_timers_clock (void)
  303. {
  304. #ifndef _POSIX_TIMERS
  305. TEST_ASSERT_MESSAGE(false, "_POSIX_TIMERS - is not defined");
  306. #endif
  307. #if defined( CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER )
  308. printf("CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER ");
  309. #endif
  310. #if defined( CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER )
  311. printf("CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER ");
  312. #endif
  313. #ifdef CONFIG_RTC_CLK_SRC_EXT_CRYS
  314. printf("External (crystal) Frequency = %d Hz\n", rtc_clk_slow_freq_get_hz());
  315. #else
  316. printf("Internal Frequency = %d Hz\n", rtc_clk_slow_freq_get_hz());
  317. #endif
  318. TEST_ASSERT(clock_settime(CLOCK_REALTIME, NULL) == -1);
  319. TEST_ASSERT(clock_gettime(CLOCK_REALTIME, NULL) == -1);
  320. TEST_ASSERT(clock_getres(CLOCK_REALTIME, NULL) == -1);
  321. TEST_ASSERT(clock_settime(CLOCK_MONOTONIC, NULL) == -1);
  322. TEST_ASSERT(clock_gettime(CLOCK_MONOTONIC, NULL) == -1);
  323. TEST_ASSERT(clock_getres(CLOCK_MONOTONIC, NULL) == -1);
  324. #if defined( CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER ) || defined( CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER )
  325. struct timeval now = {0};
  326. now.tv_sec = 10L;
  327. now.tv_usec = 100000L;
  328. TEST_ASSERT(settimeofday(&now, NULL) == 0);
  329. TEST_ASSERT(gettimeofday(&now, NULL) == 0);
  330. struct timespec ts = {0};
  331. TEST_ASSERT(clock_settime(0xFFFFFFFF, &ts) == -1);
  332. TEST_ASSERT(clock_gettime(0xFFFFFFFF, &ts) == -1);
  333. TEST_ASSERT(clock_getres(0xFFFFFFFF, &ts) == 0);
  334. TEST_ASSERT(clock_gettime(CLOCK_REALTIME, &ts) == 0);
  335. TEST_ASSERT(now.tv_sec == ts.tv_sec);
  336. TEST_ASSERT_INT_WITHIN(5000000L, ts.tv_nsec, now.tv_usec * 1000L);
  337. ts.tv_sec = 20;
  338. ts.tv_nsec = 100000000L;
  339. TEST_ASSERT(clock_settime(CLOCK_REALTIME, &ts) == 0);
  340. TEST_ASSERT(gettimeofday(&now, NULL) == 0);
  341. TEST_ASSERT_EQUAL(ts.tv_sec, now.tv_sec);
  342. TEST_ASSERT_INT_WITHIN(5000L, ts.tv_nsec / 1000L, now.tv_usec);
  343. TEST_ASSERT(clock_settime(CLOCK_MONOTONIC, &ts) == -1);
  344. uint64_t delta_monotonic_us = 0;
  345. #if defined( CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER )
  346. TEST_ASSERT(clock_getres(CLOCK_REALTIME, &ts) == 0);
  347. TEST_ASSERT_EQUAL_INT(1000, ts.tv_nsec);
  348. TEST_ASSERT(clock_getres(CLOCK_MONOTONIC, &ts) == 0);
  349. TEST_ASSERT_EQUAL_INT(1000, ts.tv_nsec);
  350. TEST_ASSERT(clock_gettime(CLOCK_MONOTONIC, &ts) == 0);
  351. delta_monotonic_us = esp_system_get_time() - (ts.tv_sec * 1000000L + ts.tv_nsec / 1000L);
  352. TEST_ASSERT(delta_monotonic_us > 0 || delta_monotonic_us == 0);
  353. TEST_ASSERT_INT_WITHIN(5000L, 0, delta_monotonic_us);
  354. #elif defined( CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER )
  355. TEST_ASSERT(clock_getres(CLOCK_REALTIME, &ts) == 0);
  356. TEST_ASSERT_EQUAL_INT(1000000000L / rtc_clk_slow_freq_get_hz(), ts.tv_nsec);
  357. TEST_ASSERT(clock_getres(CLOCK_MONOTONIC, &ts) == 0);
  358. TEST_ASSERT_EQUAL_INT(1000000000L / rtc_clk_slow_freq_get_hz(), ts.tv_nsec);
  359. TEST_ASSERT(clock_gettime(CLOCK_MONOTONIC, &ts) == 0);
  360. delta_monotonic_us = esp_clk_rtc_time() - (ts.tv_sec * 1000000L + ts.tv_nsec / 1000L);
  361. TEST_ASSERT(delta_monotonic_us > 0 || delta_monotonic_us == 0);
  362. TEST_ASSERT_INT_WITHIN(5000L, 0, delta_monotonic_us);
  363. #endif // CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER
  364. #else
  365. struct timespec ts = {0};
  366. TEST_ASSERT(clock_settime(CLOCK_REALTIME, &ts) == -1);
  367. TEST_ASSERT(clock_gettime(CLOCK_REALTIME, &ts) == -1);
  368. TEST_ASSERT(clock_getres(CLOCK_REALTIME, &ts) == -1);
  369. TEST_ASSERT(clock_settime(CLOCK_MONOTONIC, &ts) == -1);
  370. TEST_ASSERT(clock_gettime(CLOCK_MONOTONIC, &ts) == -1);
  371. TEST_ASSERT(clock_getres(CLOCK_MONOTONIC, &ts) == -1);
  372. #endif // defined( CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER ) || defined( CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER )
  373. }
  374. TEST_CASE("test posix_timers clock_... functions", "[newlib]")
  375. {
  376. test_posix_timers_clock();
  377. }
  378. #ifndef _USE_LONG_TIME_T
  379. static struct timeval get_time(const char *desc, char *buffer)
  380. {
  381. struct timeval timestamp;
  382. gettimeofday(&timestamp, NULL);
  383. struct tm* tm_info = localtime(&timestamp.tv_sec);
  384. strftime(buffer, 32, "%c", tm_info);
  385. #if !CONFIG_NEWLIB_NANO_FORMAT
  386. ESP_LOGI("TAG", "%s: %016llX (%s)", desc, timestamp.tv_sec, buffer);
  387. #endif
  388. return timestamp;
  389. }
  390. TEST_CASE("test time_t wide 64 bits", "[newlib]")
  391. {
  392. static char buffer[32];
  393. ESP_LOGI("TAG", "sizeof(time_t): %d (%d-bit)", sizeof(time_t), sizeof(time_t)*8);
  394. TEST_ASSERT_EQUAL(8, sizeof(time_t));
  395. struct tm tm = {4, 14, 3, 19, 0, 138, 0, 0, 0};
  396. struct timeval timestamp = { mktime(&tm), 0 };
  397. #if !CONFIG_NEWLIB_NANO_FORMAT
  398. ESP_LOGI("TAG", "timestamp: %016llX", timestamp.tv_sec);
  399. #endif
  400. settimeofday(&timestamp, NULL);
  401. get_time("Set time", buffer);
  402. while (timestamp.tv_sec < 0x80000003LL) {
  403. vTaskDelay(1000 / portTICK_PERIOD_MS);
  404. timestamp = get_time("Time now", buffer);
  405. }
  406. TEST_ASSERT_EQUAL_MEMORY("Tue Jan 19 03:14:11 2038", buffer, strlen(buffer));
  407. }
  408. TEST_CASE("test time functions wide 64 bits", "[newlib]")
  409. {
  410. static char origin_buffer[32];
  411. char strftime_buf[64];
  412. int year = 2018;
  413. struct tm tm = {0, 14, 3, 19, 0, year - 1900, 0, 0, 0};
  414. time_t t = mktime(&tm);
  415. while (year < 2119) {
  416. struct timeval timestamp = { t, 0 };
  417. ESP_LOGI("TAG", "year: %d", year);
  418. settimeofday(&timestamp, NULL);
  419. get_time("Time now", origin_buffer);
  420. vTaskDelay(10 / portTICK_PERIOD_MS);
  421. t += 86400 * 366;
  422. struct tm timeinfo = { 0 };
  423. time_t now;
  424. time(&now);
  425. localtime_r(&now, &timeinfo);
  426. time_t t = mktime(&timeinfo);
  427. #if !CONFIG_NEWLIB_NANO_FORMAT
  428. ESP_LOGI("TAG", "Test mktime(). Time: %016llX", t);
  429. #endif
  430. TEST_ASSERT_EQUAL(timestamp.tv_sec, t);
  431. // mktime() has error in newlib-3.0.0. It fixed in newlib-3.0.0.20180720
  432. TEST_ASSERT_EQUAL((timestamp.tv_sec >> 32), (t >> 32));
  433. strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo);
  434. ESP_LOGI("TAG", "Test time() and localtime_r(). Time: %s", strftime_buf);
  435. TEST_ASSERT_EQUAL(timeinfo.tm_year, year - 1900);
  436. TEST_ASSERT_EQUAL_MEMORY(origin_buffer, strftime_buf, strlen(origin_buffer));
  437. struct tm *tm2 = localtime(&now);
  438. strftime(strftime_buf, sizeof(strftime_buf), "%c", tm2);
  439. ESP_LOGI("TAG", "Test localtime(). Time: %s", strftime_buf);
  440. TEST_ASSERT_EQUAL(tm2->tm_year, year - 1900);
  441. TEST_ASSERT_EQUAL_MEMORY(origin_buffer, strftime_buf, strlen(origin_buffer));
  442. struct tm *gm = gmtime(&now);
  443. strftime(strftime_buf, sizeof(strftime_buf), "%c", gm);
  444. ESP_LOGI("TAG", "Test gmtime(). Time: %s", strftime_buf);
  445. TEST_ASSERT_EQUAL_MEMORY(origin_buffer, strftime_buf, strlen(origin_buffer));
  446. const char* time_str1 = ctime(&now);
  447. ESP_LOGI("TAG", "Test ctime(). Time: %s", time_str1);
  448. TEST_ASSERT_EQUAL_MEMORY(origin_buffer, time_str1, strlen(origin_buffer));
  449. const char* time_str2 = asctime(&timeinfo);
  450. ESP_LOGI("TAG", "Test asctime(). Time: %s", time_str2);
  451. TEST_ASSERT_EQUAL_MEMORY(origin_buffer, time_str2, strlen(origin_buffer));
  452. printf("\n");
  453. ++year;
  454. }
  455. }
  456. #endif // !_USE_LONG_TIME_T
  457. #if defined( CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER ) && defined( CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER )
  458. extern int64_t s_microseconds_offset;
  459. static const uint64_t s_start_timestamp = 1606838354;
  460. static RTC_NOINIT_ATTR uint64_t s_saved_time;
  461. static RTC_NOINIT_ATTR uint64_t s_time_in_reboot;
  462. typedef enum {
  463. TYPE_REBOOT_ABORT = 0,
  464. TYPE_REBOOT_RESTART,
  465. } type_reboot_t;
  466. static void print_counters(void)
  467. {
  468. int64_t high_res_time = esp_system_get_time();
  469. int64_t rtc = esp_rtc_get_time_us();
  470. uint64_t boot_time = esp_time_impl_get_boot_time();
  471. printf("\tHigh-res time %lld (us)\n", high_res_time);
  472. printf("\tRTC %lld (us)\n", rtc);
  473. printf("\tBOOT %lld (us)\n", boot_time);
  474. printf("\ts_microseconds_offset %lld (us)\n", s_microseconds_offset);
  475. printf("delta RTC - high-res time counters %lld (us)\n", rtc - high_res_time);
  476. }
  477. static void set_initial_condition(type_reboot_t type_reboot, int error_time)
  478. {
  479. print_counters();
  480. struct timeval tv = { .tv_sec = s_start_timestamp, .tv_usec = 0, };
  481. settimeofday(&tv, NULL);
  482. printf("set timestamp %lld (s)\n", s_start_timestamp);
  483. print_counters();
  484. int delay_s = abs(error_time) * 2;
  485. printf("Waiting for %d (s) ...\n", delay_s);
  486. vTaskDelay(delay_s * 1000 / portTICK_PERIOD_MS);
  487. print_counters();
  488. printf("High res counter increased to %d (s)\n", error_time);
  489. esp_timer_private_advance(error_time * 1000000ULL);
  490. print_counters();
  491. gettimeofday(&tv, NULL);
  492. s_saved_time = tv.tv_sec;
  493. printf("s_saved_time %lld (s)\n", s_saved_time);
  494. int dt = s_saved_time - s_start_timestamp;
  495. printf("delta timestamp = %d (s)\n", dt);
  496. TEST_ASSERT_GREATER_OR_EQUAL(error_time, dt);
  497. s_time_in_reboot = esp_rtc_get_time_us();
  498. if (type_reboot == TYPE_REBOOT_ABORT) {
  499. printf("Update boot time based on diff\n");
  500. esp_sync_timekeeping_timers();
  501. print_counters();
  502. printf("reboot as abort\n");
  503. abort();
  504. } else if (type_reboot == TYPE_REBOOT_RESTART) {
  505. printf("reboot as restart\n");
  506. esp_restart();
  507. }
  508. }
  509. static void set_timestamp1(void)
  510. {
  511. set_initial_condition(TYPE_REBOOT_ABORT, 5);
  512. }
  513. static void set_timestamp2(void)
  514. {
  515. set_initial_condition(TYPE_REBOOT_RESTART, 5);
  516. }
  517. static void set_timestamp3(void)
  518. {
  519. set_initial_condition(TYPE_REBOOT_RESTART, -5);
  520. }
  521. static void check_time(void)
  522. {
  523. print_counters();
  524. int latency_before_run_ut = 1 + (esp_rtc_get_time_us() - s_time_in_reboot) / 1000000;
  525. struct timeval tv;
  526. gettimeofday(&tv, NULL);
  527. printf("timestamp %jd (s)\n", (intmax_t)tv.tv_sec);
  528. int dt = tv.tv_sec - s_saved_time;
  529. printf("delta timestamp = %d (s)\n", dt);
  530. TEST_ASSERT_GREATER_OR_EQUAL(0, dt);
  531. TEST_ASSERT_LESS_OR_EQUAL(latency_before_run_ut, dt);
  532. }
  533. TEST_CASE_MULTIPLE_STAGES("Timestamp after abort is correct in case RTC & High-res timer have + big error", "[newlib][reset=abort,SW_CPU_RESET]", set_timestamp1, check_time);
  534. TEST_CASE_MULTIPLE_STAGES("Timestamp after restart is correct in case RTC & High-res timer have + big error", "[newlib][reset=SW_CPU_RESET]", set_timestamp2, check_time);
  535. TEST_CASE_MULTIPLE_STAGES("Timestamp after restart is correct in case RTC & High-res timer have - big error", "[newlib][reset=SW_CPU_RESET]", set_timestamp3, check_time);
  536. #endif // CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER && CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER