test_intr_alloc.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. Tests for the interrupt allocator.
  3. */
  4. #include <esp_types.h>
  5. #include <stdio.h>
  6. #include "rom/ets_sys.h"
  7. #include "freertos/FreeRTOS.h"
  8. #include "freertos/task.h"
  9. #include "freertos/semphr.h"
  10. #include "freertos/queue.h"
  11. #include "freertos/xtensa_api.h"
  12. #include "unity.h"
  13. #include "soc/uart_reg.h"
  14. #include "soc/dport_reg.h"
  15. #include "soc/io_mux_reg.h"
  16. #include "esp_intr_alloc.h"
  17. #include "driver/timer.h"
  18. #define TIMER_DIVIDER 16 /*!< Hardware timer clock divider */
  19. #define TIMER_SCALE (TIMER_BASE_CLK / TIMER_DIVIDER) /*!< used to calculate counter value */
  20. #define TIMER_INTERVAL0_SEC (3.4179) /*!< test interval for timer 0 */
  21. #define TIMER_INTERVAL1_SEC (5.78) /*!< test interval for timer 1 */
  22. static void my_timer_init(int timer_group, int timer_idx, int ival)
  23. {
  24. timer_config_t config;
  25. config.alarm_en = 1;
  26. config.auto_reload = 1;
  27. config.counter_dir = TIMER_COUNT_UP;
  28. config.divider = TIMER_DIVIDER;
  29. config.intr_type = TIMER_INTR_LEVEL;
  30. config.counter_en = TIMER_PAUSE;
  31. /*Configure timer*/
  32. timer_init(timer_group, timer_idx, &config);
  33. /*Stop timer counter*/
  34. timer_pause(timer_group, timer_idx);
  35. /*Load counter value */
  36. timer_set_counter_value(timer_group, timer_idx, 0x00000000ULL);
  37. /*Set alarm value*/
  38. timer_set_alarm_value(timer_group, timer_idx, ival);
  39. /*Enable timer interrupt*/
  40. timer_enable_intr(timer_group, timer_idx);
  41. }
  42. static volatile int count[4]={0,0,0,0};
  43. static void timer_isr(void *arg)
  44. {
  45. int timer_idx = (int)arg;
  46. count[timer_idx]++;
  47. if (timer_idx==0) {
  48. TIMERG0.int_clr_timers.t0 = 1;
  49. TIMERG0.hw_timer[0].update=1;
  50. TIMERG0.hw_timer[0].config.alarm_en = 1;
  51. }
  52. if (timer_idx==1) {
  53. TIMERG0.int_clr_timers.t1 = 1;
  54. TIMERG0.hw_timer[1].update=1;
  55. TIMERG0.hw_timer[1].config.alarm_en = 1;
  56. }
  57. if (timer_idx==2) {
  58. TIMERG1.int_clr_timers.t0 = 1;
  59. TIMERG1.hw_timer[0].update=1;
  60. TIMERG1.hw_timer[0].config.alarm_en = 1;
  61. }
  62. if (timer_idx==3) {
  63. TIMERG1.int_clr_timers.t1 = 1;
  64. TIMERG1.hw_timer[1].update=1;
  65. TIMERG1.hw_timer[1].config.alarm_en = 1;
  66. }
  67. // ets_printf("int %d\n", timer_idx);
  68. }
  69. static void timer_test(int flags) {
  70. int x;
  71. timer_isr_handle_t inth[4];
  72. my_timer_init(TIMER_GROUP_0, TIMER_0, 110000);
  73. my_timer_init(TIMER_GROUP_0, TIMER_1, 120000);
  74. my_timer_init(TIMER_GROUP_1, TIMER_0, 130000);
  75. my_timer_init(TIMER_GROUP_1, TIMER_1, 140000);
  76. timer_isr_register(TIMER_GROUP_0, TIMER_0, timer_isr, (void*)0, flags|ESP_INTR_FLAG_INTRDISABLED, &inth[0]);
  77. timer_isr_register(TIMER_GROUP_0, TIMER_1, timer_isr, (void*)1, flags, &inth[1]);
  78. timer_isr_register(TIMER_GROUP_1, TIMER_0, timer_isr, (void*)2, flags, &inth[2]);
  79. timer_isr_register(TIMER_GROUP_1, TIMER_1, timer_isr, (void*)3, flags, &inth[3]);
  80. timer_start(TIMER_GROUP_0, TIMER_0);
  81. timer_start(TIMER_GROUP_0, TIMER_1);
  82. timer_start(TIMER_GROUP_1, TIMER_0);
  83. timer_start(TIMER_GROUP_1, TIMER_1);
  84. for (x=0; x<4; x++) count[x]=0;
  85. printf("Interrupts allocated: %d (dis) %d %d %d\n",
  86. esp_intr_get_intno(inth[0]), esp_intr_get_intno(inth[1]),
  87. esp_intr_get_intno(inth[2]), esp_intr_get_intno(inth[3]));
  88. printf("Timer values on start: %d %d %d %d\n", count[0], count[1], count[2], count[3]);
  89. vTaskDelay(1000 / portTICK_PERIOD_MS);
  90. printf("Timer values after 1 sec: %d %d %d %d\n", count[0], count[1], count[2], count[3]);
  91. TEST_ASSERT(count[0]==0);
  92. TEST_ASSERT(count[1]!=0);
  93. TEST_ASSERT(count[2]!=0);
  94. TEST_ASSERT(count[3]!=0);
  95. printf("Disabling timers 1 and 2...\n");
  96. esp_intr_enable(inth[0]);
  97. esp_intr_disable(inth[1]);
  98. esp_intr_disable(inth[2]);
  99. for (x=0; x<4; x++) count[x]=0;
  100. vTaskDelay(1000 / portTICK_PERIOD_MS);
  101. printf("Timer values after 1 sec: %d %d %d %d\n", count[0], count[1], count[2], count[3]);
  102. TEST_ASSERT(count[0]!=0);
  103. TEST_ASSERT(count[1]==0);
  104. TEST_ASSERT(count[2]==0);
  105. TEST_ASSERT(count[3]!=0);
  106. printf("Disabling other half...\n");
  107. esp_intr_enable(inth[1]);
  108. esp_intr_enable(inth[2]);
  109. esp_intr_disable(inth[0]);
  110. esp_intr_disable(inth[3]);
  111. for (x=0; x<4; x++) count[x]=0;
  112. vTaskDelay(1000 / portTICK_PERIOD_MS);
  113. printf("Timer values after 1 sec: %d %d %d %d\n", count[0], count[1], count[2], count[3]);
  114. TEST_ASSERT(count[0]==0);
  115. TEST_ASSERT(count[1]!=0);
  116. TEST_ASSERT(count[2]!=0);
  117. TEST_ASSERT(count[3]==0);
  118. printf("Done.\n");
  119. esp_intr_free(inth[0]);
  120. esp_intr_free(inth[1]);
  121. esp_intr_free(inth[2]);
  122. esp_intr_free(inth[3]);
  123. }
  124. static volatile int int_timer_ctr;
  125. void int_timer_handler(void *arg) {
  126. xthal_set_ccompare(1, xthal_get_ccount()+8000000);
  127. int_timer_ctr++;
  128. }
  129. void local_timer_test()
  130. {
  131. intr_handle_t ih;
  132. esp_err_t r;
  133. r=esp_intr_alloc(ETS_INTERNAL_TIMER1_INTR_SOURCE, 0, int_timer_handler, NULL, &ih);
  134. TEST_ASSERT(r==ESP_OK);
  135. printf("Int timer 1 intno %d\n", esp_intr_get_intno(ih));
  136. xthal_set_ccompare(1, xthal_get_ccount()+8000000);
  137. int_timer_ctr=0;
  138. vTaskDelay(1000 / portTICK_PERIOD_MS);
  139. printf("Timer val after 1 sec: %d\n", int_timer_ctr);
  140. TEST_ASSERT(int_timer_ctr!=0);
  141. printf("Disabling int\n");
  142. esp_intr_disable(ih);
  143. int_timer_ctr=0;
  144. vTaskDelay(1000 / portTICK_PERIOD_MS);
  145. printf("Timer val after 1 sec: %d\n", int_timer_ctr);
  146. TEST_ASSERT(int_timer_ctr==0);
  147. printf("Re-enabling\n");
  148. esp_intr_enable(ih);
  149. vTaskDelay(1000 / portTICK_PERIOD_MS);
  150. printf("Timer val after 1 sec: %d\n", int_timer_ctr);
  151. TEST_ASSERT(int_timer_ctr!=0);
  152. printf("Free int, re-alloc disabled\n");
  153. r=esp_intr_free(ih);
  154. TEST_ASSERT(r==ESP_OK);
  155. r=esp_intr_alloc(ETS_INTERNAL_TIMER1_INTR_SOURCE, ESP_INTR_FLAG_INTRDISABLED, int_timer_handler, NULL, &ih);
  156. TEST_ASSERT(r==ESP_OK);
  157. int_timer_ctr=0;
  158. vTaskDelay(1000 / portTICK_PERIOD_MS);
  159. printf("Timer val after 1 sec: %d\n", int_timer_ctr);
  160. TEST_ASSERT(int_timer_ctr==0);
  161. printf("Re-enabling\n");
  162. esp_intr_enable(ih);
  163. vTaskDelay(1000 / portTICK_PERIOD_MS);
  164. printf("Timer val after 1 sec: %d\n", int_timer_ctr);
  165. TEST_ASSERT(int_timer_ctr!=0);
  166. r=esp_intr_free(ih);
  167. TEST_ASSERT(r==ESP_OK);
  168. printf("Done.\n");
  169. }
  170. TEST_CASE("Intr_alloc test, CPU-local int source", "[esp32]")
  171. {
  172. local_timer_test();
  173. }
  174. TEST_CASE("Intr_alloc test, private ints", "[esp32]")
  175. {
  176. timer_test(0);
  177. }
  178. TEST_CASE("Intr_alloc test, shared ints", "[esp32]")
  179. {
  180. timer_test(ESP_INTR_FLAG_SHARED);
  181. }
  182. TEST_CASE("Can allocate IRAM int only with an IRAM handler", "[esp32]")
  183. {
  184. void dummy(void* arg)
  185. {
  186. }
  187. IRAM_ATTR void dummy_iram(void* arg)
  188. {
  189. }
  190. RTC_IRAM_ATTR void dummy_rtc(void* arg)
  191. {
  192. }
  193. intr_handle_t ih;
  194. esp_err_t err = esp_intr_alloc(ETS_INTERNAL_PROFILING_INTR_SOURCE,
  195. ESP_INTR_FLAG_IRAM, &dummy, NULL, &ih);
  196. TEST_ASSERT_EQUAL_INT(ESP_ERR_INVALID_ARG, err);
  197. err = esp_intr_alloc(ETS_INTERNAL_PROFILING_INTR_SOURCE,
  198. ESP_INTR_FLAG_IRAM, &dummy_iram, NULL, &ih);
  199. TEST_ESP_OK(err);
  200. err = esp_intr_free(ih);
  201. TEST_ESP_OK(err);
  202. err = esp_intr_alloc(ETS_INTERNAL_PROFILING_INTR_SOURCE,
  203. ESP_INTR_FLAG_IRAM, &dummy_rtc, NULL, &ih);
  204. TEST_ESP_OK(err);
  205. err = esp_intr_free(ih);
  206. TEST_ESP_OK(err);
  207. }