test_time.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 "soc/rtc_cntl_reg.h"
  8. #include "freertos/FreeRTOS.h"
  9. #include "freertos/task.h"
  10. #include "freertos/semphr.h"
  11. #include "sdkconfig.h"
  12. #if portNUM_PROCESSORS == 2
  13. // https://github.com/espressif/arduino-esp32/issues/120
  14. TEST_CASE("Reading RTC registers on APP CPU doesn't affect clock", "[newlib]")
  15. {
  16. // This runs on APP CPU:
  17. void time_adc_test_task(void* arg)
  18. {
  19. for (int i = 0; i < 200000; ++i) {
  20. // wait for 20us, reading one of RTC registers
  21. uint32_t ccount = xthal_get_ccount();
  22. while (xthal_get_ccount() - ccount < 20 * CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ) {
  23. volatile uint32_t val = REG_READ(RTC_CNTL_STATE0_REG);
  24. (void) val;
  25. }
  26. }
  27. SemaphoreHandle_t * p_done = (SemaphoreHandle_t *) arg;
  28. xSemaphoreGive(*p_done);
  29. vTaskDelay(1);
  30. vTaskDelete(NULL);
  31. }
  32. SemaphoreHandle_t done = xSemaphoreCreateBinary();
  33. xTaskCreatePinnedToCore(&time_adc_test_task, "time_adc", 4096, &done, 5, NULL, 1);
  34. // This runs on PRO CPU:
  35. for (int i = 0; i < 4; ++i) {
  36. struct timeval tv_start;
  37. gettimeofday(&tv_start, NULL);
  38. vTaskDelay(1000/portTICK_PERIOD_MS);
  39. struct timeval tv_stop;
  40. gettimeofday(&tv_stop, NULL);
  41. float time_sec = tv_stop.tv_sec - tv_start.tv_sec + 1e-6f * (tv_stop.tv_usec - tv_start.tv_usec);
  42. printf("(0) time taken: %f sec\n", time_sec);
  43. TEST_ASSERT_TRUE(fabs(time_sec - 1.0f) < 0.1);
  44. }
  45. TEST_ASSERT_TRUE(xSemaphoreTake(done, 5000 / portTICK_RATE_MS));
  46. }
  47. #endif // portNUM_PROCESSORS == 2
  48. TEST_CASE("test adjtime function", "[newlib]")
  49. {
  50. struct timeval tv_time;
  51. struct timeval tv_delta;
  52. struct timeval tv_outdelta;
  53. TEST_ASSERT_EQUAL(adjtime(NULL, NULL), 0);
  54. tv_time.tv_sec = 5000;
  55. tv_time.tv_usec = 5000;
  56. TEST_ASSERT_EQUAL(settimeofday(&tv_time, NULL), 0);
  57. tv_outdelta.tv_sec = 5;
  58. tv_outdelta.tv_usec = 5;
  59. TEST_ASSERT_EQUAL(adjtime(NULL, &tv_outdelta), 0);
  60. TEST_ASSERT_EQUAL(tv_outdelta.tv_sec, 0);
  61. TEST_ASSERT_EQUAL(tv_outdelta.tv_usec, 0);
  62. tv_delta.tv_sec = INT_MAX / 1000000L;
  63. TEST_ASSERT_EQUAL(adjtime(&tv_delta, &tv_outdelta), -1);
  64. tv_delta.tv_sec = INT_MIN / 1000000L;
  65. TEST_ASSERT_EQUAL(adjtime(&tv_delta, &tv_outdelta), -1);
  66. tv_delta.tv_sec = 0;
  67. tv_delta.tv_usec = -900000;
  68. TEST_ASSERT_EQUAL(adjtime(&tv_delta, &tv_outdelta), 0);
  69. TEST_ASSERT_TRUE(tv_outdelta.tv_usec <= 0);
  70. tv_delta.tv_sec = 0;
  71. tv_delta.tv_usec = 900000;
  72. TEST_ASSERT_EQUAL(adjtime(&tv_delta, &tv_outdelta), 0);
  73. TEST_ASSERT_TRUE(tv_outdelta.tv_usec >= 0);
  74. tv_delta.tv_sec = -4;
  75. tv_delta.tv_usec = -900000;
  76. TEST_ASSERT_EQUAL(adjtime(&tv_delta, &tv_outdelta), 0);
  77. TEST_ASSERT_EQUAL(tv_outdelta.tv_sec, -4);
  78. TEST_ASSERT_TRUE(tv_outdelta.tv_usec <= 0);
  79. // after settimeofday() adjtime() is stopped
  80. tv_delta.tv_sec = 15;
  81. tv_delta.tv_usec = 900000;
  82. TEST_ASSERT_EQUAL(adjtime(&tv_delta, &tv_outdelta), 0);
  83. TEST_ASSERT_EQUAL(tv_outdelta.tv_sec, 15);
  84. TEST_ASSERT_TRUE(tv_outdelta.tv_usec >= 0);
  85. TEST_ASSERT_EQUAL(gettimeofday(&tv_time, NULL), 0);
  86. TEST_ASSERT_EQUAL(settimeofday(&tv_time, NULL), 0);
  87. TEST_ASSERT_EQUAL(adjtime(NULL, &tv_outdelta), 0);
  88. TEST_ASSERT_EQUAL(tv_outdelta.tv_sec, 0);
  89. TEST_ASSERT_EQUAL(tv_outdelta.tv_usec, 0);
  90. // after gettimeofday() adjtime() is not stopped
  91. tv_delta.tv_sec = 15;
  92. tv_delta.tv_usec = 900000;
  93. TEST_ASSERT_EQUAL(adjtime(&tv_delta, &tv_outdelta), 0);
  94. TEST_ASSERT_EQUAL(tv_outdelta.tv_sec, 15);
  95. TEST_ASSERT_TRUE(tv_outdelta.tv_usec >= 0);
  96. TEST_ASSERT_EQUAL(gettimeofday(&tv_time, NULL), 0);
  97. TEST_ASSERT_EQUAL(adjtime(NULL, &tv_outdelta), 0);
  98. TEST_ASSERT_EQUAL(tv_outdelta.tv_sec, 15);
  99. TEST_ASSERT_TRUE(tv_outdelta.tv_usec >= 0);
  100. tv_delta.tv_sec = 1;
  101. tv_delta.tv_usec = 0;
  102. TEST_ASSERT_EQUAL(adjtime(&tv_delta, NULL), 0);
  103. vTaskDelay(1000 / portTICK_PERIOD_MS);
  104. TEST_ASSERT_EQUAL(adjtime(NULL, &tv_outdelta), 0);
  105. TEST_ASSERT_TRUE(tv_outdelta.tv_sec == 0);
  106. // the correction will be equal to (1_000_000us >> 6) = 15_625 us.
  107. TEST_ASSERT_TRUE(1000000L - tv_outdelta.tv_usec >= 15600);
  108. TEST_ASSERT_TRUE(1000000L - tv_outdelta.tv_usec <= 15650);
  109. }
  110. static volatile bool exit_flag;
  111. static bool adjtime_test_result;
  112. static bool gettimeofday_test_result;
  113. static uint64_t count_adjtime;
  114. static uint64_t count_settimeofday;
  115. static uint64_t count_gettimeofday;
  116. static void adjtimeTask2(void *pvParameters)
  117. {
  118. struct timeval delta = {.tv_sec = 0, .tv_usec = 0};
  119. struct timeval outdelta;
  120. // although exit flag is set in another task, checking (exit_flag == false) is safe
  121. while (exit_flag == false) {
  122. delta.tv_sec += 1;
  123. delta.tv_usec = 900000;
  124. if (delta.tv_sec >= 2146) delta.tv_sec = 1;
  125. adjtime(&delta, &outdelta);
  126. count_adjtime++;
  127. }
  128. vTaskDelete(NULL);
  129. }
  130. static void settimeofdayTask2(void *pvParameters)
  131. {
  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. count_settimeofday++;
  138. vTaskDelay(1);
  139. }
  140. vTaskDelete(NULL);
  141. }
  142. static void gettimeofdayTask2(void *pvParameters)
  143. {
  144. struct timeval tv_time;
  145. // although exit flag is set in another task, checking (exit_flag == false) is safe
  146. while (exit_flag == false) {
  147. gettimeofday(&tv_time, NULL);
  148. count_gettimeofday++;
  149. vTaskDelay(1);
  150. }
  151. vTaskDelete(NULL);
  152. }
  153. TEST_CASE("test for no interlocking adjtime, gettimeofday and settimeofday functions", "[newlib]")
  154. {
  155. TaskHandle_t th[4];
  156. exit_flag = false;
  157. count_adjtime = 0;
  158. count_settimeofday = 0;
  159. count_gettimeofday = 0;
  160. struct timeval tv_time = { .tv_sec = 1520000000, .tv_usec = 900000 };
  161. TEST_ASSERT_EQUAL(settimeofday(&tv_time, NULL), 0);
  162. #ifndef CONFIG_FREERTOS_UNICORE
  163. printf("CPU0 and CPU1. Tasks run: 1 - adjtimeTask, 2 - gettimeofdayTask, 3 - settimeofdayTask \n");
  164. xTaskCreatePinnedToCore(adjtimeTask2, "adjtimeTask1", 2048, NULL, UNITY_FREERTOS_PRIORITY - 1, &th[0], 0);
  165. xTaskCreatePinnedToCore(gettimeofdayTask2, "gettimeofdayTask1", 2048, NULL, UNITY_FREERTOS_PRIORITY - 1, &th[1], 1);
  166. xTaskCreatePinnedToCore(settimeofdayTask2, "settimeofdayTask1", 2048, NULL, UNITY_FREERTOS_PRIORITY - 1, &th[2], 0);
  167. #else
  168. printf("Only one CPU. Tasks run: 1 - adjtimeTask, 2 - gettimeofdayTask, 3 - settimeofdayTask\n");
  169. xTaskCreate(adjtimeTask2, "adjtimeTask1", 2048, NULL, UNITY_FREERTOS_PRIORITY - 1, &th[0]);
  170. xTaskCreate(gettimeofdayTask2, "gettimeofdayTask1", 2048, NULL, UNITY_FREERTOS_PRIORITY - 1, &th[1]);
  171. xTaskCreate(settimeofdayTask2, "settimeofdayTask1", 2048, NULL, UNITY_FREERTOS_PRIORITY - 1, &th[2]);
  172. #endif
  173. printf("start wait for 10 seconds\n");
  174. vTaskDelay(10000 / portTICK_PERIOD_MS);
  175. // set exit flag to let thread exit
  176. exit_flag = true;
  177. vTaskDelay(20 / portTICK_PERIOD_MS);
  178. printf("count_adjtime %lld, count_settimeofday %lld, count_gettimeofday %lld\n", count_adjtime, count_settimeofday, count_gettimeofday);
  179. TEST_ASSERT(count_adjtime > 1000LL && count_settimeofday > 1000LL && count_gettimeofday > 1000LL);
  180. }
  181. static void adjtimeTask(void *pvParameters)
  182. {
  183. struct timeval delta = {.tv_sec = 0, .tv_usec = 0};
  184. struct timeval outdelta = {.tv_sec = 0, .tv_usec = 0};
  185. // although exit flag is set in another task, checking (exit_flag == false) is safe
  186. while (exit_flag == false) {
  187. delta.tv_sec = 1000;
  188. delta.tv_usec = 0;
  189. if(adjtime(&delta, &outdelta) != 0) {
  190. adjtime_test_result = true;
  191. exit_flag = true;
  192. }
  193. delta.tv_sec = 0;
  194. delta.tv_usec = 1000;
  195. if(adjtime(&delta, &outdelta) != 0) {
  196. adjtime_test_result = true;
  197. exit_flag = true;
  198. }
  199. }
  200. vTaskDelete(NULL);
  201. }
  202. static void gettimeofdayTask(void *pvParameters)
  203. {
  204. struct timeval tv_time;
  205. gettimeofday(&tv_time, NULL);
  206. uint64_t time_old = (uint64_t)tv_time.tv_sec * 1000000L + tv_time.tv_usec;
  207. // although exit flag is set in another task, checking (exit_flag == false) is safe
  208. while (exit_flag == false) {
  209. gettimeofday(&tv_time, NULL);
  210. uint64_t time = (uint64_t)tv_time.tv_sec * 1000000L + tv_time.tv_usec;
  211. if(((time - time_old) > 1000000LL) || (time_old > time)) {
  212. printf("ERROR: time jumped for %lld/1000 seconds. No locks. Need to use locks.\n", (time - time_old)/1000000LL);
  213. gettimeofday_test_result = true;
  214. exit_flag = true;
  215. }
  216. time_old = time;
  217. }
  218. vTaskDelete(NULL);
  219. }
  220. TEST_CASE("test for thread safety adjtime and gettimeofday functions", "[newlib]")
  221. {
  222. TaskHandle_t th[4];
  223. exit_flag = false;
  224. adjtime_test_result = false;
  225. gettimeofday_test_result = false;
  226. struct timeval tv_time = { .tv_sec = 1520000000, .tv_usec = 900000 };
  227. TEST_ASSERT_EQUAL(settimeofday(&tv_time, NULL), 0);
  228. #ifndef CONFIG_FREERTOS_UNICORE
  229. printf("CPU0 and CPU1. Tasks run: 1 - adjtimeTask, 2 - gettimeofdayTask\n");
  230. xTaskCreatePinnedToCore(adjtimeTask, "adjtimeTask1", 2048, NULL, UNITY_FREERTOS_PRIORITY - 1, &th[0], 0);
  231. xTaskCreatePinnedToCore(gettimeofdayTask, "gettimeofdayTask1", 2048, NULL, UNITY_FREERTOS_PRIORITY - 1, &th[1], 1);
  232. xTaskCreatePinnedToCore(adjtimeTask, "adjtimeTask2", 2048, NULL, UNITY_FREERTOS_PRIORITY - 1, &th[2], 0);
  233. xTaskCreatePinnedToCore(gettimeofdayTask, "gettimeofdayTask2", 2048, NULL, UNITY_FREERTOS_PRIORITY - 1, &th[3], 1);
  234. #else
  235. printf("Only one CPU. Tasks run: 1 - adjtimeTask, 2 - gettimeofdayTask\n");
  236. xTaskCreate(adjtimeTask, "adjtimeTask1", 2048, NULL, UNITY_FREERTOS_PRIORITY - 1, &th[0]);
  237. xTaskCreate(gettimeofdayTask, "gettimeofdayTask1", 2048, NULL, UNITY_FREERTOS_PRIORITY - 1, &th[1]);
  238. xTaskCreate(adjtimeTask, "adjtimeTask2", 2048, NULL, UNITY_FREERTOS_PRIORITY - 1, &th[2]);
  239. xTaskCreate(gettimeofdayTask, "gettimeofdayTask2", 2048, NULL, UNITY_FREERTOS_PRIORITY - 1, &th[3]);
  240. #endif
  241. printf("start wait for 10 seconds\n");
  242. vTaskDelay(10000 / portTICK_PERIOD_MS);
  243. // set exit flag to let thread exit
  244. exit_flag = true;
  245. vTaskDelay(20 / portTICK_PERIOD_MS);
  246. TEST_ASSERT(adjtime_test_result == false && gettimeofday_test_result == false);
  247. }