test_time.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include "unity.h"
  4. #include "driver/adc.h"
  5. #include <time.h>
  6. #include <sys/time.h>
  7. #include "freertos/FreeRTOS.h"
  8. #include "freertos/task.h"
  9. #include "freertos/semphr.h"
  10. #include "sdkconfig.h"
  11. #include "soc/rtc.h"
  12. #include "soc/rtc_cntl_reg.h"
  13. #include "esp_system.h"
  14. #include "test_utils.h"
  15. #include "esp_log.h"
  16. #include "esp_rom_sys.h"
  17. #include "esp_system.h"
  18. #include "esp_timer.h"
  19. #if CONFIG_IDF_TARGET_ESP32
  20. #include "esp32/clk.h"
  21. #define TARGET_DEFAULT_CPU_FREQ_MHZ CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ
  22. #elif CONFIG_IDF_TARGET_ESP32S2
  23. #include "esp32s2/clk.h"
  24. #define TARGET_DEFAULT_CPU_FREQ_MHZ CONFIG_ESP32S2_DEFAULT_CPU_FREQ_MHZ
  25. #elif CONFIG_IDF_TARGET_ESP32S3
  26. #include "esp32s3/clk.h"
  27. #define TARGET_DEFAULT_CPU_FREQ_MHZ CONFIG_ESP32S3_DEFAULT_CPU_FREQ_MHZ
  28. #elif CONFIG_IDF_TARGET_ESP32C3
  29. #include "esp32c3/clk.h"
  30. #define TARGET_DEFAULT_CPU_FREQ_MHZ CONFIG_ESP32C3_DEFAULT_CPU_FREQ_MHZ
  31. #endif
  32. #if portNUM_PROCESSORS == 2
  33. // https://github.com/espressif/arduino-esp32/issues/120
  34. TEST_CASE("Reading RTC registers on APP CPU doesn't affect clock", "[newlib]")
  35. {
  36. // This runs on APP CPU:
  37. void time_adc_test_task(void* arg)
  38. {
  39. for (int i = 0; i < 200000; ++i) {
  40. // wait for 20us, reading one of RTC registers
  41. uint32_t ccount = xthal_get_ccount();
  42. while (xthal_get_ccount() - ccount < 20 * TARGET_DEFAULT_CPU_FREQ_MHZ) {
  43. volatile uint32_t val = REG_READ(RTC_CNTL_STATE0_REG);
  44. (void) val;
  45. }
  46. }
  47. SemaphoreHandle_t * p_done = (SemaphoreHandle_t *) arg;
  48. xSemaphoreGive(*p_done);
  49. vTaskDelay(1);
  50. vTaskDelete(NULL);
  51. }
  52. SemaphoreHandle_t done = xSemaphoreCreateBinary();
  53. xTaskCreatePinnedToCore(&time_adc_test_task, "time_adc", 4096, &done, 5, NULL, 1);
  54. // This runs on PRO CPU:
  55. for (int i = 0; i < 4; ++i) {
  56. struct timeval tv_start;
  57. gettimeofday(&tv_start, NULL);
  58. vTaskDelay(1000/portTICK_PERIOD_MS);
  59. struct timeval tv_stop;
  60. gettimeofday(&tv_stop, NULL);
  61. float time_sec = tv_stop.tv_sec - tv_start.tv_sec + 1e-6f * (tv_stop.tv_usec - tv_start.tv_usec);
  62. printf("(0) time taken: %f sec\n", time_sec);
  63. TEST_ASSERT_TRUE(fabs(time_sec - 1.0f) < 0.1);
  64. }
  65. TEST_ASSERT_TRUE(xSemaphoreTake(done, 5000 / portTICK_RATE_MS));
  66. }
  67. #endif // portNUM_PROCESSORS == 2
  68. TEST_CASE("test adjtime function", "[newlib]")
  69. {
  70. struct timeval tv_time;
  71. struct timeval tv_delta;
  72. struct timeval tv_outdelta;
  73. TEST_ASSERT_EQUAL(adjtime(NULL, NULL), 0);
  74. tv_time.tv_sec = 5000;
  75. tv_time.tv_usec = 5000;
  76. TEST_ASSERT_EQUAL(settimeofday(&tv_time, NULL), 0);
  77. tv_outdelta.tv_sec = 5;
  78. tv_outdelta.tv_usec = 5;
  79. TEST_ASSERT_EQUAL(adjtime(NULL, &tv_outdelta), 0);
  80. TEST_ASSERT_EQUAL(tv_outdelta.tv_sec, 0);
  81. TEST_ASSERT_EQUAL(tv_outdelta.tv_usec, 0);
  82. tv_delta.tv_sec = INT_MAX / 1000000L;
  83. TEST_ASSERT_EQUAL(adjtime(&tv_delta, &tv_outdelta), -1);
  84. tv_delta.tv_sec = INT_MIN / 1000000L;
  85. TEST_ASSERT_EQUAL(adjtime(&tv_delta, &tv_outdelta), -1);
  86. tv_delta.tv_sec = 0;
  87. tv_delta.tv_usec = -900000;
  88. TEST_ASSERT_EQUAL(adjtime(&tv_delta, &tv_outdelta), 0);
  89. TEST_ASSERT_EQUAL(tv_outdelta.tv_sec, 0);
  90. TEST_ASSERT_EQUAL(tv_outdelta.tv_usec, 0);
  91. TEST_ASSERT_EQUAL(adjtime(NULL, &tv_outdelta), 0);
  92. TEST_ASSERT_LESS_THAN(-800000, tv_outdelta.tv_usec);
  93. tv_delta.tv_sec = -4;
  94. tv_delta.tv_usec = -900000;
  95. TEST_ASSERT_EQUAL(adjtime(&tv_delta, NULL), 0);
  96. TEST_ASSERT_EQUAL(adjtime(NULL, &tv_outdelta), 0);
  97. TEST_ASSERT_EQUAL(tv_outdelta.tv_sec, -4);
  98. TEST_ASSERT_LESS_THAN(-800000, tv_outdelta.tv_usec);
  99. // after settimeofday() adjtime() is stopped
  100. tv_delta.tv_sec = 15;
  101. tv_delta.tv_usec = 900000;
  102. TEST_ASSERT_EQUAL(adjtime(&tv_delta, &tv_outdelta), 0);
  103. TEST_ASSERT_EQUAL(tv_outdelta.tv_sec, -4);
  104. TEST_ASSERT_LESS_THAN(-800000, tv_outdelta.tv_usec);
  105. TEST_ASSERT_EQUAL(adjtime(NULL, &tv_outdelta), 0);
  106. TEST_ASSERT_EQUAL(tv_outdelta.tv_sec, 15);
  107. TEST_ASSERT_GREATER_OR_EQUAL(800000, tv_outdelta.tv_usec);
  108. TEST_ASSERT_EQUAL(gettimeofday(&tv_time, NULL), 0);
  109. TEST_ASSERT_EQUAL(settimeofday(&tv_time, NULL), 0);
  110. TEST_ASSERT_EQUAL(adjtime(NULL, &tv_outdelta), 0);
  111. TEST_ASSERT_EQUAL(tv_outdelta.tv_sec, 0);
  112. TEST_ASSERT_EQUAL(tv_outdelta.tv_usec, 0);
  113. // after gettimeofday() adjtime() is not 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, 0);
  118. TEST_ASSERT_EQUAL(tv_outdelta.tv_usec, 0);
  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(adjtime(NULL, &tv_outdelta), 0);
  124. TEST_ASSERT_EQUAL(tv_outdelta.tv_sec, 15);
  125. TEST_ASSERT_GREATER_OR_EQUAL(800000, tv_outdelta.tv_usec);
  126. tv_delta.tv_sec = 1;
  127. tv_delta.tv_usec = 0;
  128. TEST_ASSERT_EQUAL(adjtime(&tv_delta, NULL), 0);
  129. vTaskDelay(1000 / portTICK_PERIOD_MS);
  130. TEST_ASSERT_EQUAL(adjtime(NULL, &tv_outdelta), 0);
  131. TEST_ASSERT_EQUAL(tv_outdelta.tv_sec, 0);
  132. // the correction will be equal to (1_000_000us >> 6) = 15_625 us.
  133. TEST_ASSERT_TRUE(1000000L - tv_outdelta.tv_usec >= 15600);
  134. TEST_ASSERT_TRUE(1000000L - tv_outdelta.tv_usec <= 15650);
  135. }
  136. static volatile bool exit_flag;
  137. static void adjtimeTask2(void *pvParameters)
  138. {
  139. xSemaphoreHandle *sema = (xSemaphoreHandle *) pvParameters;
  140. struct timeval delta = {.tv_sec = 0, .tv_usec = 0};
  141. struct timeval outdelta;
  142. // although exit flag is set in another task, checking (exit_flag == false) is safe
  143. while (exit_flag == false) {
  144. delta.tv_sec += 1;
  145. delta.tv_usec = 900000;
  146. if (delta.tv_sec >= 2146) delta.tv_sec = 1;
  147. adjtime(&delta, &outdelta);
  148. }
  149. xSemaphoreGive(*sema);
  150. vTaskDelete(NULL);
  151. }
  152. static void timeTask(void *pvParameters)
  153. {
  154. xSemaphoreHandle *sema = (xSemaphoreHandle *) pvParameters;
  155. struct timeval tv_time = { .tv_sec = 1520000000, .tv_usec = 900000 };
  156. // although exit flag is set in another task, checking (exit_flag == false) is safe
  157. while (exit_flag == false) {
  158. tv_time.tv_sec += 1;
  159. settimeofday(&tv_time, NULL);
  160. gettimeofday(&tv_time, NULL);
  161. }
  162. xSemaphoreGive(*sema);
  163. vTaskDelete(NULL);
  164. }
  165. TEST_CASE("test for no interlocking adjtime, gettimeofday and settimeofday functions", "[newlib]")
  166. {
  167. TaskHandle_t th[4];
  168. exit_flag = false;
  169. struct timeval tv_time = { .tv_sec = 1520000000, .tv_usec = 900000 };
  170. TEST_ASSERT_EQUAL(settimeofday(&tv_time, NULL), 0);
  171. const int max_tasks = 2;
  172. xSemaphoreHandle exit_sema[max_tasks];
  173. for (int i = 0; i < max_tasks; ++i) {
  174. exit_sema[i] = xSemaphoreCreateBinary();
  175. }
  176. #ifndef CONFIG_FREERTOS_UNICORE
  177. printf("CPU0 and CPU1. Tasks run: 1 - adjtimeTask, 2 - gettimeofdayTask, 3 - settimeofdayTask \n");
  178. xTaskCreatePinnedToCore(adjtimeTask2, "adjtimeTask2", 2048, &exit_sema[0], UNITY_FREERTOS_PRIORITY - 1, &th[0], 0);
  179. xTaskCreatePinnedToCore(timeTask, "timeTask", 2048, &exit_sema[1], UNITY_FREERTOS_PRIORITY - 1, &th[1], 1);
  180. #else
  181. printf("Only one CPU. Tasks run: 1 - adjtimeTask, 2 - gettimeofdayTask, 3 - settimeofdayTask\n");
  182. xTaskCreate(adjtimeTask2, "adjtimeTask2", 2048, &exit_sema[0], UNITY_FREERTOS_PRIORITY - 1, &th[0]);
  183. xTaskCreate(timeTask, "timeTask", 2048, &exit_sema[1], UNITY_FREERTOS_PRIORITY - 1, &th[1]);
  184. #endif
  185. printf("start wait for 5 seconds\n");
  186. vTaskDelay(5000 / portTICK_PERIOD_MS);
  187. // set exit flag to let thread exit
  188. exit_flag = true;
  189. for (int i = 0; i < max_tasks; ++i) {
  190. if (!xSemaphoreTake(exit_sema[i], 2000/portTICK_PERIOD_MS)) {
  191. TEST_FAIL_MESSAGE("exit_sema not released by test task");
  192. }
  193. vSemaphoreDelete(exit_sema[i]);
  194. }
  195. }
  196. #ifndef CONFIG_FREERTOS_UNICORE
  197. #define ADJTIME_CORRECTION_FACTOR 6
  198. static int64_t result_adjtime_correction_us[2];
  199. static void get_time_task(void *pvParameters)
  200. {
  201. xSemaphoreHandle *sema = (xSemaphoreHandle *) pvParameters;
  202. struct timeval tv_time;
  203. // although exit flag is set in another task, checking (exit_flag == false) is safe
  204. while (exit_flag == false) {
  205. gettimeofday(&tv_time, NULL);
  206. }
  207. xSemaphoreGive(*sema);
  208. vTaskDelete(NULL);
  209. }
  210. static void start_measure(int64_t* sys_time, int64_t* real_time)
  211. {
  212. struct timeval tv_time;
  213. int64_t t1, t2;
  214. do {
  215. t1 = esp_timer_get_time();
  216. gettimeofday(&tv_time, NULL);
  217. t2 = esp_timer_get_time();
  218. } while (t2 - t1 > 40);
  219. *real_time = t2;
  220. *sys_time = (int64_t)tv_time.tv_sec * 1000000L + tv_time.tv_usec;
  221. }
  222. static int64_t calc_correction(const char* tag, int64_t* sys_time, int64_t* real_time)
  223. {
  224. int64_t dt_real_time_us = real_time[1] - real_time[0];
  225. int64_t dt_sys_time_us = sys_time[1] - sys_time[0];
  226. int64_t calc_correction_us = dt_real_time_us >> ADJTIME_CORRECTION_FACTOR;
  227. int64_t real_correction_us = dt_sys_time_us - dt_real_time_us;
  228. int64_t error_us = calc_correction_us - real_correction_us;
  229. printf("%s: dt_real_time = %lli us, dt_sys_time = %lli us, calc_correction = %lli us, error = %lli us\n",
  230. tag, dt_real_time_us, dt_sys_time_us, calc_correction_us, error_us);
  231. TEST_ASSERT_TRUE(dt_sys_time_us > 0 && dt_real_time_us > 0);
  232. TEST_ASSERT_INT_WITHIN(100, 0, error_us);
  233. return real_correction_us;
  234. }
  235. static void measure_time_task(void *pvParameters)
  236. {
  237. xSemaphoreHandle *sema = (xSemaphoreHandle *) pvParameters;
  238. int64_t main_real_time_us[2];
  239. int64_t main_sys_time_us[2];
  240. struct timeval tv_time = {.tv_sec = 1550000000, .tv_usec = 0};
  241. TEST_ASSERT_EQUAL(0, settimeofday(&tv_time, NULL));
  242. struct timeval delta = {.tv_sec = 2000, .tv_usec = 900000};
  243. adjtime(&delta, NULL);
  244. gettimeofday(&tv_time, NULL);
  245. start_measure(&main_sys_time_us[0], &main_real_time_us[0]);
  246. {
  247. int64_t real_time_us[2] = { main_real_time_us[0], 0};
  248. int64_t sys_time_us[2] = { main_sys_time_us[0], 0};
  249. // although exit flag is set in another task, checking (exit_flag == false) is safe
  250. while (exit_flag == false) {
  251. esp_rom_delay_us(2 * 1000000); // 2 sec
  252. start_measure(&sys_time_us[1], &real_time_us[1]);
  253. result_adjtime_correction_us[1] += calc_correction("measure", sys_time_us, real_time_us);
  254. sys_time_us[0] = sys_time_us[1];
  255. real_time_us[0] = real_time_us[1];
  256. }
  257. main_sys_time_us[1] = sys_time_us[1];
  258. main_real_time_us[1] = real_time_us[1];
  259. }
  260. result_adjtime_correction_us[0] = calc_correction("main", main_sys_time_us, main_real_time_us);
  261. int64_t delta_us = result_adjtime_correction_us[0] - result_adjtime_correction_us[1];
  262. 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);
  263. TEST_ASSERT_INT_WITHIN(100, 0, delta_us);
  264. xSemaphoreGive(*sema);
  265. vTaskDelete(NULL);
  266. }
  267. TEST_CASE("test time adjustment happens linearly", "[newlib][timeout=35]")
  268. {
  269. exit_flag = false;
  270. xSemaphoreHandle exit_sema[2];
  271. for (int i = 0; i < 2; ++i) {
  272. exit_sema[i] = xSemaphoreCreateBinary();
  273. result_adjtime_correction_us[i] = 0;
  274. }
  275. xTaskCreatePinnedToCore(get_time_task, "get_time_task", 4096, &exit_sema[0], UNITY_FREERTOS_PRIORITY - 1, NULL, 0);
  276. xTaskCreatePinnedToCore(measure_time_task, "measure_time_task", 4096, &exit_sema[1], UNITY_FREERTOS_PRIORITY - 1, NULL, 1);
  277. printf("start waiting for 30 seconds\n");
  278. vTaskDelay(30000 / portTICK_PERIOD_MS);
  279. // set exit flag to let thread exit
  280. exit_flag = true;
  281. for (int i = 0; i < 2; ++i) {
  282. if (!xSemaphoreTake(exit_sema[i], 2100/portTICK_PERIOD_MS)) {
  283. TEST_FAIL_MESSAGE("exit_sema not released by test task");
  284. }
  285. }
  286. for (int i = 0; i < 2; ++i) {
  287. vSemaphoreDelete(exit_sema[i]);
  288. }
  289. }
  290. #endif
  291. void test_posix_timers_clock (void)
  292. {
  293. #ifndef _POSIX_TIMERS
  294. TEST_ASSERT_MESSAGE(false, "_POSIX_TIMERS - is not defined");
  295. #endif
  296. #if defined( CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER )
  297. printf("CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER ");
  298. #endif
  299. #if defined( CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER )
  300. printf("CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER ");
  301. #endif
  302. #ifdef CONFIG_ESP32_RTC_CLK_SRC_EXT_CRYS
  303. printf("External (crystal) Frequency = %d Hz\n", rtc_clk_slow_freq_get_hz());
  304. #else
  305. printf("Internal Frequency = %d Hz\n", rtc_clk_slow_freq_get_hz());
  306. #endif
  307. TEST_ASSERT(clock_settime(CLOCK_REALTIME, NULL) == -1);
  308. TEST_ASSERT(clock_gettime(CLOCK_REALTIME, NULL) == -1);
  309. TEST_ASSERT(clock_getres(CLOCK_REALTIME, NULL) == -1);
  310. TEST_ASSERT(clock_settime(CLOCK_MONOTONIC, NULL) == -1);
  311. TEST_ASSERT(clock_gettime(CLOCK_MONOTONIC, NULL) == -1);
  312. TEST_ASSERT(clock_getres(CLOCK_MONOTONIC, NULL) == -1);
  313. #if defined( CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER ) || defined( CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER )
  314. struct timeval now = {0};
  315. now.tv_sec = 10L;
  316. now.tv_usec = 100000L;
  317. TEST_ASSERT(settimeofday(&now, NULL) == 0);
  318. TEST_ASSERT(gettimeofday(&now, NULL) == 0);
  319. struct timespec ts = {0};
  320. TEST_ASSERT(clock_settime(0xFFFFFFFF, &ts) == -1);
  321. TEST_ASSERT(clock_gettime(0xFFFFFFFF, &ts) == -1);
  322. TEST_ASSERT(clock_getres(0xFFFFFFFF, &ts) == 0);
  323. TEST_ASSERT(clock_gettime(CLOCK_REALTIME, &ts) == 0);
  324. TEST_ASSERT(now.tv_sec == ts.tv_sec);
  325. TEST_ASSERT_INT_WITHIN(5000000L, ts.tv_nsec, now.tv_usec * 1000L);
  326. ts.tv_sec = 20;
  327. ts.tv_nsec = 100000000L;
  328. TEST_ASSERT(clock_settime(CLOCK_REALTIME, &ts) == 0);
  329. TEST_ASSERT(gettimeofday(&now, NULL) == 0);
  330. TEST_ASSERT_EQUAL(ts.tv_sec, now.tv_sec);
  331. TEST_ASSERT_INT_WITHIN(5000L, ts.tv_nsec / 1000L, now.tv_usec);
  332. TEST_ASSERT(clock_settime(CLOCK_MONOTONIC, &ts) == -1);
  333. uint64_t delta_monotonic_us = 0;
  334. #if defined( CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER )
  335. TEST_ASSERT(clock_getres(CLOCK_REALTIME, &ts) == 0);
  336. TEST_ASSERT_EQUAL_INT(1000, ts.tv_nsec);
  337. TEST_ASSERT(clock_getres(CLOCK_MONOTONIC, &ts) == 0);
  338. TEST_ASSERT_EQUAL_INT(1000, ts.tv_nsec);
  339. TEST_ASSERT(clock_gettime(CLOCK_MONOTONIC, &ts) == 0);
  340. delta_monotonic_us = esp_timer_get_time() - (ts.tv_sec * 1000000L + ts.tv_nsec / 1000L);
  341. TEST_ASSERT(delta_monotonic_us > 0 || delta_monotonic_us == 0);
  342. TEST_ASSERT_INT_WITHIN(5000L, 0, delta_monotonic_us);
  343. #elif defined( CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER )
  344. TEST_ASSERT(clock_getres(CLOCK_REALTIME, &ts) == 0);
  345. TEST_ASSERT_EQUAL_INT(1000000000L / rtc_clk_slow_freq_get_hz(), ts.tv_nsec);
  346. TEST_ASSERT(clock_getres(CLOCK_MONOTONIC, &ts) == 0);
  347. TEST_ASSERT_EQUAL_INT(1000000000L / rtc_clk_slow_freq_get_hz(), ts.tv_nsec);
  348. TEST_ASSERT(clock_gettime(CLOCK_MONOTONIC, &ts) == 0);
  349. delta_monotonic_us = esp_clk_rtc_time() - (ts.tv_sec * 1000000L + ts.tv_nsec / 1000L);
  350. TEST_ASSERT(delta_monotonic_us > 0 || delta_monotonic_us == 0);
  351. TEST_ASSERT_INT_WITHIN(5000L, 0, delta_monotonic_us);
  352. #endif // CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER
  353. #else
  354. struct timespec ts = {0};
  355. TEST_ASSERT(clock_settime(CLOCK_REALTIME, &ts) == -1);
  356. TEST_ASSERT(clock_gettime(CLOCK_REALTIME, &ts) == -1);
  357. TEST_ASSERT(clock_getres(CLOCK_REALTIME, &ts) == -1);
  358. TEST_ASSERT(clock_settime(CLOCK_MONOTONIC, &ts) == -1);
  359. TEST_ASSERT(clock_gettime(CLOCK_MONOTONIC, &ts) == -1);
  360. TEST_ASSERT(clock_getres(CLOCK_MONOTONIC, &ts) == -1);
  361. #endif // defined( CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER ) || defined( CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER )
  362. }
  363. TEST_CASE("test posix_timers clock_... functions", "[newlib]")
  364. {
  365. test_posix_timers_clock();
  366. }
  367. #ifdef CONFIG_SDK_TOOLCHAIN_SUPPORTS_TIME_WIDE_64_BITS
  368. #include <string.h>
  369. static struct timeval get_time(const char *desc, char *buffer)
  370. {
  371. struct timeval timestamp;
  372. gettimeofday(&timestamp, NULL);
  373. struct tm* tm_info = localtime(&timestamp.tv_sec);
  374. strftime(buffer, 32, "%c", tm_info);
  375. ESP_LOGI("TAG", "%s: %016llX (%s)", desc, timestamp.tv_sec, buffer);
  376. return timestamp;
  377. }
  378. TEST_CASE("test time_t wide 64 bits", "[newlib]")
  379. {
  380. static char buffer[32];
  381. ESP_LOGI("TAG", "sizeof(time_t): %d (%d-bit)", sizeof(time_t), sizeof(time_t)*8);
  382. TEST_ASSERT_EQUAL(8, sizeof(time_t));
  383. struct tm tm = {4, 14, 3, 19, 0, 138, 0, 0, 0};
  384. struct timeval timestamp = { mktime(&tm), 0 };
  385. ESP_LOGI("TAG", "timestamp: %016llX", timestamp.tv_sec);
  386. settimeofday(&timestamp, NULL);
  387. get_time("Set time", buffer);
  388. while (timestamp.tv_sec < 0x80000003LL) {
  389. vTaskDelay(1000 / portTICK_PERIOD_MS);
  390. timestamp = get_time("Time now", buffer);
  391. }
  392. TEST_ASSERT_EQUAL_MEMORY("Tue Jan 19 03:14:11 2038", buffer, strlen(buffer));
  393. }
  394. TEST_CASE("test time functions wide 64 bits", "[newlib]")
  395. {
  396. static char origin_buffer[32];
  397. char strftime_buf[64];
  398. int year = 2018;
  399. struct tm tm = {0, 14, 3, 19, 0, year - 1900, 0, 0, 0};
  400. time_t t = mktime(&tm);
  401. while (year < 2119) {
  402. struct timeval timestamp = { t, 0 };
  403. ESP_LOGI("TAG", "year: %d", year);
  404. settimeofday(&timestamp, NULL);
  405. get_time("Time now", origin_buffer);
  406. vTaskDelay(10 / portTICK_PERIOD_MS);
  407. t += 86400 * 366;
  408. struct tm timeinfo = { 0 };
  409. time_t now;
  410. time(&now);
  411. localtime_r(&now, &timeinfo);
  412. time_t t = mktime(&timeinfo);
  413. ESP_LOGI("TAG", "Test mktime(). Time: %016llX", t);
  414. TEST_ASSERT_EQUAL(timestamp.tv_sec, t);
  415. // mktime() has error in newlib-3.0.0. It fixed in newlib-3.0.0.20180720
  416. TEST_ASSERT_EQUAL((timestamp.tv_sec >> 32), (t >> 32));
  417. strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo);
  418. ESP_LOGI("TAG", "Test time() and localtime_r(). Time: %s", strftime_buf);
  419. TEST_ASSERT_EQUAL(timeinfo.tm_year, year - 1900);
  420. TEST_ASSERT_EQUAL_MEMORY(origin_buffer, strftime_buf, strlen(origin_buffer));
  421. struct tm *tm2 = localtime(&now);
  422. strftime(strftime_buf, sizeof(strftime_buf), "%c", tm2);
  423. ESP_LOGI("TAG", "Test localtime(). Time: %s", strftime_buf);
  424. TEST_ASSERT_EQUAL(tm2->tm_year, year - 1900);
  425. TEST_ASSERT_EQUAL_MEMORY(origin_buffer, strftime_buf, strlen(origin_buffer));
  426. struct tm *gm = gmtime(&now);
  427. strftime(strftime_buf, sizeof(strftime_buf), "%c", gm);
  428. ESP_LOGI("TAG", "Test gmtime(). Time: %s", strftime_buf);
  429. TEST_ASSERT_EQUAL_MEMORY(origin_buffer, strftime_buf, strlen(origin_buffer));
  430. const char* time_str1 = ctime(&now);
  431. ESP_LOGI("TAG", "Test ctime(). Time: %s", time_str1);
  432. TEST_ASSERT_EQUAL_MEMORY(origin_buffer, time_str1, strlen(origin_buffer));
  433. const char* time_str2 = asctime(&timeinfo);
  434. ESP_LOGI("TAG", "Test asctime(). Time: %s", time_str2);
  435. TEST_ASSERT_EQUAL_MEMORY(origin_buffer, time_str2, strlen(origin_buffer));
  436. printf("\n");
  437. ++year;
  438. }
  439. }
  440. #endif // CONFIG_SDK_TOOLCHAIN_SUPPORTS_TIME_WIDE_64_BITS