test_time.c 22 KB

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