test_time.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. // https://github.com/espressif/arduino-esp32/issues/120
  13. TEST_CASE("Reading RTC registers on APP CPU doesn't affect clock", "[newlib]")
  14. {
  15. // This runs on APP CPU:
  16. void time_adc_test_task(void* arg)
  17. {
  18. for (int i = 0; i < 200000; ++i) {
  19. // wait for 20us, reading one of RTC registers
  20. uint32_t ccount = xthal_get_ccount();
  21. while (xthal_get_ccount() - ccount < 20 * CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ) {
  22. volatile uint32_t val = REG_READ(RTC_CNTL_STATE0_REG);
  23. (void) val;
  24. }
  25. }
  26. SemaphoreHandle_t * p_done = (SemaphoreHandle_t *) arg;
  27. xSemaphoreGive(*p_done);
  28. vTaskDelay(1);
  29. vTaskDelete(NULL);
  30. }
  31. SemaphoreHandle_t done = xSemaphoreCreateBinary();
  32. xTaskCreatePinnedToCore(&time_adc_test_task, "time_adc", 4096, &done, 5, NULL, 1);
  33. // This runs on PRO CPU:
  34. for (int i = 0; i < 4; ++i) {
  35. struct timeval tv_start;
  36. gettimeofday(&tv_start, NULL);
  37. vTaskDelay(1000/portTICK_PERIOD_MS);
  38. struct timeval tv_stop;
  39. gettimeofday(&tv_stop, NULL);
  40. float time_sec = tv_stop.tv_sec - tv_start.tv_sec + 1e-6f * (tv_stop.tv_usec - tv_start.tv_usec);
  41. printf("(0) time taken: %f sec\n", time_sec);
  42. TEST_ASSERT_TRUE(fabs(time_sec - 1.0f) < 0.1);
  43. }
  44. TEST_ASSERT_TRUE(xSemaphoreTake(done, 5000 / portTICK_RATE_MS));
  45. }