test_time.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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. *real_time = esp_timer_get_time();
  191. gettimeofday(&tv_time, NULL);
  192. *sys_time = (int64_t)tv_time.tv_sec * 1000000L + tv_time.tv_usec;
  193. }
  194. static void end_measure(int64_t* sys_time, int64_t* real_time)
  195. {
  196. struct timeval tv_time;
  197. gettimeofday(&tv_time, NULL);
  198. *real_time = esp_timer_get_time();
  199. *sys_time = (int64_t)tv_time.tv_sec * 1000000L + tv_time.tv_usec;
  200. }
  201. static int64_t calc_correction(const char* tag, int64_t* sys_time, int64_t* real_time)
  202. {
  203. int64_t dt_real_time_us = real_time[1] - real_time[0];
  204. int64_t dt_sys_time_us = sys_time[1] - sys_time[0];
  205. int64_t calc_correction_us = dt_real_time_us >> ADJTIME_CORRECTION_FACTOR;
  206. int64_t real_correction_us = dt_sys_time_us - dt_real_time_us;
  207. int64_t error_us = calc_correction_us - real_correction_us;
  208. printf("%s: dt_real_time = %lli us, dt_sys_time = %lli us, calc_correction = %lli us, error = %lli us\n",
  209. tag, dt_real_time_us, dt_sys_time_us, calc_correction_us, error_us);
  210. TEST_ASSERT_TRUE(dt_sys_time_us > 0 && dt_real_time_us > 0);
  211. TEST_ASSERT_INT_WITHIN(100, 0, error_us);
  212. return real_correction_us;
  213. }
  214. static void measure_time_task(void *pvParameters)
  215. {
  216. struct timeval tv_time;
  217. int64_t real_time_us[2];
  218. int64_t sys_time_us[2];
  219. int64_t delay_us = 2 * 1000000; // 2 sec
  220. xSemaphoreHandle *sema = (xSemaphoreHandle *) pvParameters;
  221. gettimeofday(&tv_time, NULL);
  222. start_measure(&sys_time_us[0], &real_time_us[0]);
  223. // although exit flag is set in another task, checking (exit_flag == false) is safe
  224. while (exit_flag == false) {
  225. ets_delay_us(delay_us);
  226. end_measure(&sys_time_us[1], &real_time_us[1]);
  227. result_adjtime_correction_us[1] += calc_correction("measure", sys_time_us, real_time_us);
  228. sys_time_us[0] = sys_time_us[1];
  229. real_time_us[0] = real_time_us[1];
  230. }
  231. xSemaphoreGive(*sema);
  232. vTaskDelete(NULL);
  233. }
  234. TEST_CASE("test time adjustment happens linearly", "[newlib][timeout=35]")
  235. {
  236. int64_t real_time_us[2];
  237. int64_t sys_time_us[2];
  238. exit_flag = false;
  239. struct timeval tv_time = {.tv_sec = 1550000000, .tv_usec = 0};
  240. TEST_ASSERT_EQUAL(0, settimeofday(&tv_time, NULL));
  241. struct timeval delta = {.tv_sec = 2000, .tv_usec = 900000};
  242. adjtime(&delta, NULL);
  243. gettimeofday(&tv_time, NULL);
  244. xSemaphoreHandle exit_sema[2];
  245. for (int i = 0; i < 2; ++i) {
  246. exit_sema[i] = xSemaphoreCreateBinary();
  247. result_adjtime_correction_us[i] = 0;
  248. }
  249. start_measure(&sys_time_us[0], &real_time_us[0]);
  250. xTaskCreatePinnedToCore(get_time_task, "get_time_task", 2048, &exit_sema[0], UNITY_FREERTOS_PRIORITY - 1, NULL, 0);
  251. xTaskCreatePinnedToCore(measure_time_task, "measure_time_task", 2048, &exit_sema[1], UNITY_FREERTOS_PRIORITY - 1, NULL, 1);
  252. printf("start waiting for 30 seconds\n");
  253. vTaskDelay(30000 / portTICK_PERIOD_MS);
  254. // set exit flag to let thread exit
  255. exit_flag = true;
  256. for (int i = 0; i < 2; ++i) {
  257. if (!xSemaphoreTake(exit_sema[i], 2100/portTICK_PERIOD_MS)) {
  258. TEST_FAIL_MESSAGE("exit_sema not released by test task");
  259. }
  260. }
  261. end_measure(&sys_time_us[1], &real_time_us[1]);
  262. result_adjtime_correction_us[0] = calc_correction("main", sys_time_us, real_time_us);
  263. int64_t delta_us = result_adjtime_correction_us[0] - result_adjtime_correction_us[1];
  264. 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);
  265. TEST_ASSERT_INT_WITHIN(100, 0, delta_us);
  266. for (int i = 0; i < 2; ++i) {
  267. vSemaphoreDelete(exit_sema[i]);
  268. }
  269. }
  270. #endif
  271. #if defined( CONFIG_ESP32_TIME_SYSCALL_USE_RTC ) || defined( CONFIG_ESP32_TIME_SYSCALL_USE_RTC_FRC1 )
  272. #define WITH_RTC 1
  273. #endif
  274. #if defined( CONFIG_ESP32_TIME_SYSCALL_USE_FRC1 ) || defined( CONFIG_ESP32_TIME_SYSCALL_USE_RTC_FRC1 )
  275. #define WITH_FRC 1
  276. #endif
  277. void test_posix_timers_clock (void)
  278. {
  279. #ifndef _POSIX_TIMERS
  280. TEST_ASSERT_MESSAGE(false, "_POSIX_TIMERS - is not defined");
  281. #endif
  282. #if defined( WITH_FRC )
  283. printf("WITH_FRC ");
  284. #endif
  285. #if defined( WITH_RTC )
  286. printf("WITH_RTC ");
  287. #endif
  288. #ifdef CONFIG_ESP32_RTC_CLK_SRC_EXT_CRYS
  289. printf("External (crystal) Frequency = %d Hz\n", rtc_clk_slow_freq_get_hz());
  290. #else
  291. printf("Internal Frequency = %d Hz\n", rtc_clk_slow_freq_get_hz());
  292. #endif
  293. TEST_ASSERT(clock_settime(CLOCK_REALTIME, NULL) == -1);
  294. TEST_ASSERT(clock_gettime(CLOCK_REALTIME, NULL) == -1);
  295. TEST_ASSERT(clock_getres(CLOCK_REALTIME, NULL) == -1);
  296. TEST_ASSERT(clock_settime(CLOCK_MONOTONIC, NULL) == -1);
  297. TEST_ASSERT(clock_gettime(CLOCK_MONOTONIC, NULL) == -1);
  298. TEST_ASSERT(clock_getres(CLOCK_MONOTONIC, NULL) == -1);
  299. #if defined( WITH_FRC ) || defined( WITH_RTC )
  300. struct timeval now = {0};
  301. now.tv_sec = 10L;
  302. now.tv_usec = 100000L;
  303. TEST_ASSERT(settimeofday(&now, NULL) == 0);
  304. TEST_ASSERT(gettimeofday(&now, NULL) == 0);
  305. struct timespec ts = {0};
  306. TEST_ASSERT(clock_settime(0xFFFFFFFF, &ts) == -1);
  307. TEST_ASSERT(clock_gettime(0xFFFFFFFF, &ts) == -1);
  308. TEST_ASSERT(clock_getres(0xFFFFFFFF, &ts) == 0);
  309. TEST_ASSERT(clock_gettime(CLOCK_REALTIME, &ts) == 0);
  310. TEST_ASSERT(now.tv_sec == ts.tv_sec);
  311. TEST_ASSERT_INT_WITHIN(5000000L, ts.tv_nsec, now.tv_usec * 1000L);
  312. ts.tv_sec = 20;
  313. ts.tv_nsec = 100000000L;
  314. TEST_ASSERT(clock_settime(CLOCK_REALTIME, &ts) == 0);
  315. TEST_ASSERT(gettimeofday(&now, NULL) == 0);
  316. TEST_ASSERT(now.tv_sec == ts.tv_sec);
  317. TEST_ASSERT_INT_WITHIN(5000L, now.tv_usec, ts.tv_nsec / 1000L);
  318. TEST_ASSERT(clock_settime(CLOCK_MONOTONIC, &ts) == -1);
  319. uint64_t delta_monotonic_us = 0;
  320. #if defined( WITH_FRC )
  321. TEST_ASSERT(clock_getres(CLOCK_REALTIME, &ts) == 0);
  322. TEST_ASSERT_EQUAL_INT(1000, ts.tv_nsec);
  323. TEST_ASSERT(clock_getres(CLOCK_MONOTONIC, &ts) == 0);
  324. TEST_ASSERT_EQUAL_INT(1000, ts.tv_nsec);
  325. TEST_ASSERT(clock_gettime(CLOCK_MONOTONIC, &ts) == 0);
  326. delta_monotonic_us = esp_timer_get_time() - (ts.tv_sec * 1000000L + ts.tv_nsec / 1000L);
  327. TEST_ASSERT(delta_monotonic_us > 0 || delta_monotonic_us == 0);
  328. TEST_ASSERT_INT_WITHIN(5000L, 0, delta_monotonic_us);
  329. #elif defined( WITH_RTC )
  330. TEST_ASSERT(clock_getres(CLOCK_REALTIME, &ts) == 0);
  331. TEST_ASSERT_EQUAL_INT(1000000000L / rtc_clk_slow_freq_get_hz(), ts.tv_nsec);
  332. TEST_ASSERT(clock_getres(CLOCK_MONOTONIC, &ts) == 0);
  333. TEST_ASSERT_EQUAL_INT(1000000000L / rtc_clk_slow_freq_get_hz(), ts.tv_nsec);
  334. TEST_ASSERT(clock_gettime(CLOCK_MONOTONIC, &ts) == 0);
  335. delta_monotonic_us = esp_clk_rtc_time() - (ts.tv_sec * 1000000L + ts.tv_nsec / 1000L);
  336. TEST_ASSERT(delta_monotonic_us > 0 || delta_monotonic_us == 0);
  337. TEST_ASSERT_INT_WITHIN(5000L, 0, delta_monotonic_us);
  338. #endif // WITH_FRC
  339. #else
  340. struct timespec ts = {0};
  341. TEST_ASSERT(clock_settime(CLOCK_REALTIME, &ts) == -1);
  342. TEST_ASSERT(clock_gettime(CLOCK_REALTIME, &ts) == -1);
  343. TEST_ASSERT(clock_getres(CLOCK_REALTIME, &ts) == -1);
  344. TEST_ASSERT(clock_settime(CLOCK_MONOTONIC, &ts) == -1);
  345. TEST_ASSERT(clock_gettime(CLOCK_MONOTONIC, &ts) == -1);
  346. TEST_ASSERT(clock_getres(CLOCK_MONOTONIC, &ts) == -1);
  347. #endif // defined( WITH_FRC ) || defined( WITH_RTC )
  348. }
  349. TEST_CASE("test posix_timers clock_... functions", "[newlib]")
  350. {
  351. test_posix_timers_clock();
  352. }