test_isr_latency.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include <esp_types.h>
  2. #include <stdio.h>
  3. #include "freertos/FreeRTOS.h"
  4. #include "freertos/task.h"
  5. #include "freertos/semphr.h"
  6. #include "freertos/queue.h"
  7. #include "freertos/xtensa_api.h"
  8. #include "esp_intr_alloc.h"
  9. #include "xtensa/hal.h"
  10. #include "unity.h"
  11. #include "soc/cpu.h"
  12. #include "test_utils.h"
  13. #define SW_ISR_LEVEL_1 7
  14. static SemaphoreHandle_t sync;
  15. static SemaphoreHandle_t end_sema;
  16. static uint32_t cycle_before_trigger;
  17. static uint32_t cycle_before_exit;
  18. static uint32_t delta_enter_cycles = 0;
  19. static uint32_t delta_exit_cycles = 0;
  20. static void software_isr(void *arg) {
  21. (void)arg;
  22. BaseType_t yield;
  23. delta_enter_cycles += portGET_RUN_TIME_COUNTER_VALUE() - cycle_before_trigger;
  24. xt_set_intclear(1 << SW_ISR_LEVEL_1);
  25. xSemaphoreGiveFromISR(sync, &yield);
  26. if(yield) {
  27. portYIELD_FROM_ISR();
  28. }
  29. cycle_before_exit = portGET_RUN_TIME_COUNTER_VALUE();
  30. }
  31. static void test_task(void *arg) {
  32. (void)arg;
  33. intr_handle_t handle;
  34. esp_err_t err = esp_intr_alloc(ETS_INTERNAL_SW0_INTR_SOURCE, ESP_INTR_FLAG_LEVEL1, &software_isr, NULL, &handle);
  35. TEST_ASSERT_EQUAL_HEX32(ESP_OK, err);
  36. for(int i = 0;i < 10000; i++) {
  37. cycle_before_trigger = portGET_RUN_TIME_COUNTER_VALUE();
  38. xt_set_intset(1 << SW_ISR_LEVEL_1);
  39. xSemaphoreTake(sync, portMAX_DELAY);
  40. delta_exit_cycles += portGET_RUN_TIME_COUNTER_VALUE() - cycle_before_exit;
  41. }
  42. delta_enter_cycles /= 10000;
  43. delta_exit_cycles /= 10000;
  44. esp_intr_free(handle);
  45. xSemaphoreGive(end_sema);
  46. vTaskDelete(NULL);
  47. }
  48. TEST_CASE("isr latency test", "[freertos] [ignore]")
  49. {
  50. sync = xSemaphoreCreateBinary();
  51. TEST_ASSERT(sync != NULL);
  52. end_sema = xSemaphoreCreateBinary();
  53. TEST_ASSERT(end_sema != NULL);
  54. xTaskCreatePinnedToCore(test_task, "tst" , 4096, NULL, configMAX_PRIORITIES - 1, NULL, 0);
  55. BaseType_t result = xSemaphoreTake(end_sema, portMAX_DELAY);
  56. TEST_ASSERT_EQUAL_HEX32(pdTRUE, result);
  57. TEST_PERFORMANCE_LESS_THAN(ISR_ENTER_CYCLES, "%d cycles" ,delta_enter_cycles);
  58. TEST_PERFORMANCE_LESS_THAN(ISR_EXIT_CYCLES, "%d cycles" ,delta_exit_cycles);
  59. }