test_time.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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 "esp32/clk.h"
  13. #include "esp_system.h"
  14. #include "test_utils.h"
  15. #if portNUM_PROCESSORS == 2
  16. // https://github.com/espressif/arduino-esp32/issues/120
  17. TEST_CASE("Reading RTC registers on APP CPU doesn't affect clock", "[newlib]")
  18. {
  19. // This runs on APP CPU:
  20. void time_adc_test_task(void* arg)
  21. {
  22. for (int i = 0; i < 200000; ++i) {
  23. // wait for 20us, reading one of RTC registers
  24. uint32_t ccount = xthal_get_ccount();
  25. while (xthal_get_ccount() - ccount < 20 * CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ) {
  26. volatile uint32_t val = REG_READ(RTC_CNTL_STATE0_REG);
  27. (void) val;
  28. }
  29. }
  30. SemaphoreHandle_t * p_done = (SemaphoreHandle_t *) arg;
  31. xSemaphoreGive(*p_done);
  32. vTaskDelay(1);
  33. vTaskDelete(NULL);
  34. }
  35. SemaphoreHandle_t done = xSemaphoreCreateBinary();
  36. xTaskCreatePinnedToCore(&time_adc_test_task, "time_adc", 4096, &done, 5, NULL, 1);
  37. // This runs on PRO CPU:
  38. for (int i = 0; i < 4; ++i) {
  39. struct timeval tv_start;
  40. gettimeofday(&tv_start, NULL);
  41. vTaskDelay(1000/portTICK_PERIOD_MS);
  42. struct timeval tv_stop;
  43. gettimeofday(&tv_stop, NULL);
  44. float time_sec = tv_stop.tv_sec - tv_start.tv_sec + 1e-6f * (tv_stop.tv_usec - tv_start.tv_usec);
  45. printf("(0) time taken: %f sec\n", time_sec);
  46. TEST_ASSERT_TRUE(fabs(time_sec - 1.0f) < 0.1);
  47. }
  48. TEST_ASSERT_TRUE(xSemaphoreTake(done, 5000 / portTICK_RATE_MS));
  49. }
  50. #endif // portNUM_PROCESSORS == 2
  51. TEST_CASE("test adjtime function", "[newlib]")
  52. {
  53. struct timeval tv_time;
  54. struct timeval tv_delta;
  55. struct timeval tv_outdelta;
  56. TEST_ASSERT_EQUAL(adjtime(NULL, NULL), 0);
  57. tv_time.tv_sec = 5000;
  58. tv_time.tv_usec = 5000;
  59. TEST_ASSERT_EQUAL(settimeofday(&tv_time, NULL), 0);
  60. tv_outdelta.tv_sec = 5;
  61. tv_outdelta.tv_usec = 5;
  62. TEST_ASSERT_EQUAL(adjtime(NULL, &tv_outdelta), 0);
  63. TEST_ASSERT_EQUAL(tv_outdelta.tv_sec, 0);
  64. TEST_ASSERT_EQUAL(tv_outdelta.tv_usec, 0);
  65. tv_delta.tv_sec = INT_MAX / 1000000L;
  66. TEST_ASSERT_EQUAL(adjtime(&tv_delta, &tv_outdelta), -1);
  67. tv_delta.tv_sec = INT_MIN / 1000000L;
  68. TEST_ASSERT_EQUAL(adjtime(&tv_delta, &tv_outdelta), -1);
  69. tv_delta.tv_sec = 0;
  70. tv_delta.tv_usec = -900000;
  71. TEST_ASSERT_EQUAL(adjtime(&tv_delta, &tv_outdelta), 0);
  72. TEST_ASSERT_TRUE(tv_outdelta.tv_usec <= 0);
  73. tv_delta.tv_sec = 0;
  74. tv_delta.tv_usec = 900000;
  75. TEST_ASSERT_EQUAL(adjtime(&tv_delta, &tv_outdelta), 0);
  76. TEST_ASSERT_TRUE(tv_outdelta.tv_usec >= 0);
  77. tv_delta.tv_sec = -4;
  78. tv_delta.tv_usec = -900000;
  79. TEST_ASSERT_EQUAL(adjtime(&tv_delta, &tv_outdelta), 0);
  80. TEST_ASSERT_EQUAL(tv_outdelta.tv_sec, -4);
  81. TEST_ASSERT_TRUE(tv_outdelta.tv_usec <= 0);
  82. // after settimeofday() adjtime() is stopped
  83. tv_delta.tv_sec = 15;
  84. tv_delta.tv_usec = 900000;
  85. TEST_ASSERT_EQUAL(adjtime(&tv_delta, &tv_outdelta), 0);
  86. TEST_ASSERT_EQUAL(tv_outdelta.tv_sec, 15);
  87. TEST_ASSERT_TRUE(tv_outdelta.tv_usec >= 0);
  88. TEST_ASSERT_EQUAL(gettimeofday(&tv_time, NULL), 0);
  89. TEST_ASSERT_EQUAL(settimeofday(&tv_time, NULL), 0);
  90. TEST_ASSERT_EQUAL(adjtime(NULL, &tv_outdelta), 0);
  91. TEST_ASSERT_EQUAL(tv_outdelta.tv_sec, 0);
  92. TEST_ASSERT_EQUAL(tv_outdelta.tv_usec, 0);
  93. // after gettimeofday() adjtime() is not stopped
  94. tv_delta.tv_sec = 15;
  95. tv_delta.tv_usec = 900000;
  96. TEST_ASSERT_EQUAL(adjtime(&tv_delta, &tv_outdelta), 0);
  97. TEST_ASSERT_EQUAL(tv_outdelta.tv_sec, 15);
  98. TEST_ASSERT_TRUE(tv_outdelta.tv_usec >= 0);
  99. TEST_ASSERT_EQUAL(gettimeofday(&tv_time, NULL), 0);
  100. TEST_ASSERT_EQUAL(adjtime(NULL, &tv_outdelta), 0);
  101. TEST_ASSERT_EQUAL(tv_outdelta.tv_sec, 15);
  102. TEST_ASSERT_TRUE(tv_outdelta.tv_usec >= 0);
  103. tv_delta.tv_sec = 1;
  104. tv_delta.tv_usec = 0;
  105. TEST_ASSERT_EQUAL(adjtime(&tv_delta, NULL), 0);
  106. vTaskDelay(1000 / portTICK_PERIOD_MS);
  107. TEST_ASSERT_EQUAL(adjtime(NULL, &tv_outdelta), 0);
  108. TEST_ASSERT_TRUE(tv_outdelta.tv_sec == 0);
  109. // the correction will be equal to (1_000_000us >> 6) = 15_625 us.
  110. TEST_ASSERT_TRUE(1000000L - tv_outdelta.tv_usec >= 15600);
  111. TEST_ASSERT_TRUE(1000000L - tv_outdelta.tv_usec <= 15650);
  112. }
  113. static volatile bool exit_flag;
  114. static void adjtimeTask2(void *pvParameters)
  115. {
  116. xSemaphoreHandle *sema = (xSemaphoreHandle *) pvParameters;
  117. struct timeval delta = {.tv_sec = 0, .tv_usec = 0};
  118. struct timeval outdelta;
  119. // although exit flag is set in another task, checking (exit_flag == false) is safe
  120. while (exit_flag == false) {
  121. delta.tv_sec += 1;
  122. delta.tv_usec = 900000;
  123. if (delta.tv_sec >= 2146) delta.tv_sec = 1;
  124. adjtime(&delta, &outdelta);
  125. }
  126. xSemaphoreGive(*sema);
  127. vTaskDelete(NULL);
  128. }
  129. static void timeTask(void *pvParameters)
  130. {
  131. xSemaphoreHandle *sema = (xSemaphoreHandle *) pvParameters;
  132. struct timeval tv_time = { .tv_sec = 1520000000, .tv_usec = 900000 };
  133. // although exit flag is set in another task, checking (exit_flag == false) is safe
  134. while (exit_flag == false) {
  135. tv_time.tv_sec += 1;
  136. settimeofday(&tv_time, NULL);
  137. gettimeofday(&tv_time, NULL);
  138. }
  139. xSemaphoreGive(*sema);
  140. vTaskDelete(NULL);
  141. }
  142. TEST_CASE("test for no interlocking adjtime, gettimeofday and settimeofday functions", "[newlib]")
  143. {
  144. TaskHandle_t th[4];
  145. exit_flag = false;
  146. struct timeval tv_time = { .tv_sec = 1520000000, .tv_usec = 900000 };
  147. TEST_ASSERT_EQUAL(settimeofday(&tv_time, NULL), 0);
  148. const int max_tasks = 2;
  149. xSemaphoreHandle exit_sema[max_tasks];
  150. for (int i = 0; i < max_tasks; ++i) {
  151. exit_sema[i] = xSemaphoreCreateBinary();
  152. }
  153. #ifndef CONFIG_FREERTOS_UNICORE
  154. printf("CPU0 and CPU1. Tasks run: 1 - adjtimeTask, 2 - gettimeofdayTask, 3 - settimeofdayTask \n");
  155. xTaskCreatePinnedToCore(adjtimeTask2, "adjtimeTask2", 2048, &exit_sema[0], UNITY_FREERTOS_PRIORITY - 1, &th[0], 0);
  156. xTaskCreatePinnedToCore(timeTask, "timeTask", 2048, &exit_sema[1], UNITY_FREERTOS_PRIORITY - 1, &th[1], 1);
  157. #else
  158. printf("Only one CPU. Tasks run: 1 - adjtimeTask, 2 - gettimeofdayTask, 3 - settimeofdayTask\n");
  159. xTaskCreate(adjtimeTask2, "adjtimeTask2", 2048, &exit_sema[0], UNITY_FREERTOS_PRIORITY - 1, &th[0]);
  160. xTaskCreate(timeTask, "timeTask", 2048, &exit_sema[1], UNITY_FREERTOS_PRIORITY - 1, &th[1]);
  161. #endif
  162. printf("start wait for 5 seconds\n");
  163. vTaskDelay(5000 / portTICK_PERIOD_MS);
  164. // set exit flag to let thread exit
  165. exit_flag = true;
  166. for (int i = 0; i < max_tasks; ++i) {
  167. if (!xSemaphoreTake(exit_sema[i], 2000/portTICK_PERIOD_MS)) {
  168. TEST_FAIL_MESSAGE("exit_sema not released by test task");
  169. }
  170. vSemaphoreDelete(exit_sema[i]);
  171. }
  172. }
  173. #ifndef CONFIG_FREERTOS_UNICORE
  174. #define ADJTIME_CORRECTION_FACTOR 6
  175. static int64_t result_adjtime_correction_us[2];
  176. static void get_time_task(void *pvParameters)
  177. {
  178. xSemaphoreHandle *sema = (xSemaphoreHandle *) pvParameters;
  179. struct timeval tv_time;
  180. // although exit flag is set in another task, checking (exit_flag == false) is safe
  181. while (exit_flag == false) {
  182. gettimeofday(&tv_time, NULL);
  183. }
  184. xSemaphoreGive(*sema);
  185. vTaskDelete(NULL);
  186. }
  187. static void start_measure(int64_t* sys_time, int64_t* real_time)
  188. {
  189. struct timeval tv_time;
  190. int64_t t1, t2;
  191. do {
  192. t1 = esp_timer_get_time();
  193. gettimeofday(&tv_time, NULL);
  194. t2 = esp_timer_get_time();
  195. } while (t2 - t1 > 40);
  196. *real_time = t2;
  197. *sys_time = (int64_t)tv_time.tv_sec * 1000000L + tv_time.tv_usec;
  198. }
  199. static int64_t calc_correction(const char* tag, int64_t* sys_time, int64_t* real_time)
  200. {
  201. int64_t dt_real_time_us = real_time[1] - real_time[0];
  202. int64_t dt_sys_time_us = sys_time[1] - sys_time[0];
  203. int64_t calc_correction_us = dt_real_time_us >> ADJTIME_CORRECTION_FACTOR;
  204. int64_t real_correction_us = dt_sys_time_us - dt_real_time_us;
  205. int64_t error_us = calc_correction_us - real_correction_us;
  206. printf("%s: dt_real_time = %lli us, dt_sys_time = %lli us, calc_correction = %lli us, error = %lli us\n",
  207. tag, dt_real_time_us, dt_sys_time_us, calc_correction_us, error_us);
  208. TEST_ASSERT_TRUE(dt_sys_time_us > 0 && dt_real_time_us > 0);
  209. TEST_ASSERT_INT_WITHIN(100, 0, error_us);
  210. return real_correction_us;
  211. }
  212. static void measure_time_task(void *pvParameters)
  213. {
  214. xSemaphoreHandle *sema = (xSemaphoreHandle *) pvParameters;
  215. int64_t main_real_time_us[2];
  216. int64_t main_sys_time_us[2];
  217. struct timeval tv_time = {.tv_sec = 1550000000, .tv_usec = 0};
  218. TEST_ASSERT_EQUAL(0, settimeofday(&tv_time, NULL));
  219. struct timeval delta = {.tv_sec = 2000, .tv_usec = 900000};
  220. adjtime(&delta, NULL);
  221. gettimeofday(&tv_time, NULL);
  222. start_measure(&main_sys_time_us[0], &main_real_time_us[0]);
  223. {
  224. int64_t real_time_us[2] = { main_real_time_us[0], 0};
  225. int64_t sys_time_us[2] = { main_sys_time_us[0], 0};
  226. // although exit flag is set in another task, checking (exit_flag == false) is safe
  227. while (exit_flag == false) {
  228. ets_delay_us(2 * 1000000); // 2 sec
  229. start_measure(&sys_time_us[1], &real_time_us[1]);
  230. result_adjtime_correction_us[1] += calc_correction("measure", sys_time_us, real_time_us);
  231. sys_time_us[0] = sys_time_us[1];
  232. real_time_us[0] = real_time_us[1];
  233. }
  234. main_sys_time_us[1] = sys_time_us[1];
  235. main_real_time_us[1] = real_time_us[1];
  236. }
  237. result_adjtime_correction_us[0] = calc_correction("main", main_sys_time_us, main_real_time_us);
  238. int64_t delta_us = result_adjtime_correction_us[0] - result_adjtime_correction_us[1];
  239. 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);
  240. TEST_ASSERT_INT_WITHIN(100, 0, delta_us);
  241. xSemaphoreGive(*sema);
  242. vTaskDelete(NULL);
  243. }
  244. TEST_CASE("test time adjustment happens linearly", "[newlib][timeout=35]")
  245. {
  246. exit_flag = false;
  247. xSemaphoreHandle exit_sema[2];
  248. for (int i = 0; i < 2; ++i) {
  249. exit_sema[i] = xSemaphoreCreateBinary();
  250. result_adjtime_correction_us[i] = 0;
  251. }
  252. xTaskCreatePinnedToCore(get_time_task, "get_time_task", 4096, &exit_sema[0], UNITY_FREERTOS_PRIORITY - 1, NULL, 0);
  253. xTaskCreatePinnedToCore(measure_time_task, "measure_time_task", 4096, &exit_sema[1], UNITY_FREERTOS_PRIORITY - 1, NULL, 1);
  254. printf("start waiting for 30 seconds\n");
  255. vTaskDelay(30000 / portTICK_PERIOD_MS);
  256. // set exit flag to let thread exit
  257. exit_flag = true;
  258. for (int i = 0; i < 2; ++i) {
  259. if (!xSemaphoreTake(exit_sema[i], 2100/portTICK_PERIOD_MS)) {
  260. TEST_FAIL_MESSAGE("exit_sema not released by test task");
  261. }
  262. }
  263. for (int i = 0; i < 2; ++i) {
  264. vSemaphoreDelete(exit_sema[i]);
  265. }
  266. }
  267. #endif
  268. #if defined( CONFIG_ESP32_TIME_SYSCALL_USE_RTC ) || defined( CONFIG_ESP32_TIME_SYSCALL_USE_RTC_FRC1 )
  269. #define WITH_RTC 1
  270. #endif
  271. #if defined( CONFIG_ESP32_TIME_SYSCALL_USE_FRC1 ) || defined( CONFIG_ESP32_TIME_SYSCALL_USE_RTC_FRC1 )
  272. #define WITH_FRC 1
  273. #endif
  274. void test_posix_timers_clock (void)
  275. {
  276. #ifndef _POSIX_TIMERS
  277. TEST_ASSERT_MESSAGE(false, "_POSIX_TIMERS - is not defined");
  278. #endif
  279. #if defined( WITH_FRC )
  280. printf("WITH_FRC ");
  281. #endif
  282. #if defined( WITH_RTC )
  283. printf("WITH_RTC ");
  284. #endif
  285. #ifdef CONFIG_ESP32_RTC_CLK_SRC_EXT_CRYS
  286. printf("External (crystal) Frequency = %d Hz\n", rtc_clk_slow_freq_get_hz());
  287. #else
  288. printf("Internal Frequency = %d Hz\n", rtc_clk_slow_freq_get_hz());
  289. #endif
  290. TEST_ASSERT(clock_settime(CLOCK_REALTIME, NULL) == -1);
  291. TEST_ASSERT(clock_gettime(CLOCK_REALTIME, NULL) == -1);
  292. TEST_ASSERT(clock_getres(CLOCK_REALTIME, NULL) == -1);
  293. TEST_ASSERT(clock_settime(CLOCK_MONOTONIC, NULL) == -1);
  294. TEST_ASSERT(clock_gettime(CLOCK_MONOTONIC, NULL) == -1);
  295. TEST_ASSERT(clock_getres(CLOCK_MONOTONIC, NULL) == -1);
  296. #if defined( WITH_FRC ) || defined( WITH_RTC )
  297. struct timeval now = {0};
  298. now.tv_sec = 10L;
  299. now.tv_usec = 100000L;
  300. TEST_ASSERT(settimeofday(&now, NULL) == 0);
  301. TEST_ASSERT(gettimeofday(&now, NULL) == 0);
  302. struct timespec ts = {0};
  303. TEST_ASSERT(clock_settime(0xFFFFFFFF, &ts) == -1);
  304. TEST_ASSERT(clock_gettime(0xFFFFFFFF, &ts) == -1);
  305. TEST_ASSERT(clock_getres(0xFFFFFFFF, &ts) == 0);
  306. TEST_ASSERT(clock_gettime(CLOCK_REALTIME, &ts) == 0);
  307. TEST_ASSERT(now.tv_sec == ts.tv_sec);
  308. TEST_ASSERT_INT_WITHIN(5000000L, ts.tv_nsec, now.tv_usec * 1000L);
  309. ts.tv_sec = 20;
  310. ts.tv_nsec = 100000000L;
  311. TEST_ASSERT(clock_settime(CLOCK_REALTIME, &ts) == 0);
  312. TEST_ASSERT(gettimeofday(&now, NULL) == 0);
  313. TEST_ASSERT(now.tv_sec == ts.tv_sec);
  314. TEST_ASSERT_INT_WITHIN(5000L, now.tv_usec, ts.tv_nsec / 1000L);
  315. TEST_ASSERT(clock_settime(CLOCK_MONOTONIC, &ts) == -1);
  316. uint64_t delta_monotonic_us = 0;
  317. #if defined( WITH_FRC )
  318. TEST_ASSERT(clock_getres(CLOCK_REALTIME, &ts) == 0);
  319. TEST_ASSERT_EQUAL_INT(1000, ts.tv_nsec);
  320. TEST_ASSERT(clock_getres(CLOCK_MONOTONIC, &ts) == 0);
  321. TEST_ASSERT_EQUAL_INT(1000, ts.tv_nsec);
  322. TEST_ASSERT(clock_gettime(CLOCK_MONOTONIC, &ts) == 0);
  323. delta_monotonic_us = esp_timer_get_time() - (ts.tv_sec * 1000000L + ts.tv_nsec / 1000L);
  324. TEST_ASSERT(delta_monotonic_us > 0 || delta_monotonic_us == 0);
  325. TEST_ASSERT_INT_WITHIN(5000L, 0, delta_monotonic_us);
  326. #elif defined( WITH_RTC )
  327. TEST_ASSERT(clock_getres(CLOCK_REALTIME, &ts) == 0);
  328. TEST_ASSERT_EQUAL_INT(1000000000L / rtc_clk_slow_freq_get_hz(), ts.tv_nsec);
  329. TEST_ASSERT(clock_getres(CLOCK_MONOTONIC, &ts) == 0);
  330. TEST_ASSERT_EQUAL_INT(1000000000L / rtc_clk_slow_freq_get_hz(), ts.tv_nsec);
  331. TEST_ASSERT(clock_gettime(CLOCK_MONOTONIC, &ts) == 0);
  332. delta_monotonic_us = esp_clk_rtc_time() - (ts.tv_sec * 1000000L + ts.tv_nsec / 1000L);
  333. TEST_ASSERT(delta_monotonic_us > 0 || delta_monotonic_us == 0);
  334. TEST_ASSERT_INT_WITHIN(5000L, 0, delta_monotonic_us);
  335. #endif // WITH_FRC
  336. #else
  337. struct timespec ts = {0};
  338. TEST_ASSERT(clock_settime(CLOCK_REALTIME, &ts) == -1);
  339. TEST_ASSERT(clock_gettime(CLOCK_REALTIME, &ts) == -1);
  340. TEST_ASSERT(clock_getres(CLOCK_REALTIME, &ts) == -1);
  341. TEST_ASSERT(clock_settime(CLOCK_MONOTONIC, &ts) == -1);
  342. TEST_ASSERT(clock_gettime(CLOCK_MONOTONIC, &ts) == -1);
  343. TEST_ASSERT(clock_getres(CLOCK_MONOTONIC, &ts) == -1);
  344. #endif // defined( WITH_FRC ) || defined( WITH_RTC )
  345. }
  346. TEST_CASE("test posix_timers clock_... functions", "[newlib]")
  347. {
  348. test_posix_timers_clock();
  349. }