test_time.c 15 KB

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