test_time.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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