test_time.c 22 KB

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