test_intr_alloc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*
  2. Tests for the interrupt allocator.
  3. */
  4. #include <esp_types.h>
  5. #include <stdio.h>
  6. #if CONFIG_IDF_TARGET_ESP32
  7. #include "esp32/rom/ets_sys.h"
  8. #elif CONFIG_IDF_TARGET_ESP32S2
  9. #include "esp32s2/rom/ets_sys.h"
  10. #endif
  11. #include "freertos/FreeRTOS.h"
  12. #include "freertos/task.h"
  13. #include "freertos/semphr.h"
  14. #include "freertos/queue.h"
  15. #include "freertos/xtensa_api.h"
  16. #include "unity.h"
  17. #include "soc/uart_periph.h"
  18. #include "soc/dport_reg.h"
  19. #include "soc/gpio_periph.h"
  20. #include "esp_intr_alloc.h"
  21. #include "driver/periph_ctrl.h"
  22. #include "driver/timer.h"
  23. #define TIMER_DIVIDER 16 /*!< Hardware timer clock divider */
  24. #define TIMER_SCALE (TIMER_BASE_CLK / TIMER_DIVIDER) /*!< used to calculate counter value */
  25. #define TIMER_INTERVAL0_SEC (3.4179) /*!< test interval for timer 0 */
  26. #define TIMER_INTERVAL1_SEC (5.78) /*!< test interval for timer 1 */
  27. static void my_timer_init(int timer_group, int timer_idx, int ival)
  28. {
  29. timer_config_t config;
  30. config.alarm_en = 1;
  31. config.auto_reload = 1;
  32. config.counter_dir = TIMER_COUNT_UP;
  33. config.divider = TIMER_DIVIDER;
  34. config.intr_type = TIMER_INTR_LEVEL;
  35. config.counter_en = TIMER_PAUSE;
  36. /*Configure timer*/
  37. timer_init(timer_group, timer_idx, &config);
  38. /*Stop timer counter*/
  39. timer_pause(timer_group, timer_idx);
  40. /*Load counter value */
  41. timer_set_counter_value(timer_group, timer_idx, 0x00000000ULL);
  42. /*Set alarm value*/
  43. timer_set_alarm_value(timer_group, timer_idx, ival);
  44. /*Enable timer interrupt*/
  45. timer_enable_intr(timer_group, timer_idx);
  46. }
  47. static volatile int count[4]={0,0,0,0};
  48. static void timer_isr(void *arg)
  49. {
  50. int timer_idx = (int)arg;
  51. count[timer_idx]++;
  52. if (timer_idx==0) {
  53. timer_group_clr_intr_status_in_isr(TIMER_GROUP_0, TIMER_0);
  54. timer_group_enable_alarm_in_isr(TIMER_GROUP_0, TIMER_0);
  55. }
  56. if (timer_idx==1) {
  57. timer_group_clr_intr_status_in_isr(TIMER_GROUP_0, TIMER_1);
  58. timer_group_enable_alarm_in_isr(TIMER_GROUP_0, TIMER_1);
  59. }
  60. if (timer_idx==2) {
  61. timer_group_clr_intr_status_in_isr(TIMER_GROUP_1, TIMER_0);
  62. timer_group_enable_alarm_in_isr(TIMER_GROUP_1, TIMER_0);
  63. }
  64. if (timer_idx==3) {
  65. timer_group_clr_intr_status_in_isr(TIMER_GROUP_1, TIMER_1);
  66. timer_group_enable_alarm_in_isr(TIMER_GROUP_1, TIMER_1);
  67. }
  68. // ets_printf("int %d\n", timer_idx);
  69. }
  70. static void timer_test(int flags) {
  71. int x;
  72. timer_isr_handle_t inth[4];
  73. my_timer_init(TIMER_GROUP_0, TIMER_0, 110000);
  74. my_timer_init(TIMER_GROUP_0, TIMER_1, 120000);
  75. my_timer_init(TIMER_GROUP_1, TIMER_0, 130000);
  76. my_timer_init(TIMER_GROUP_1, TIMER_1, 140000);
  77. timer_isr_register(TIMER_GROUP_0, TIMER_0, timer_isr, (void*)0, flags|ESP_INTR_FLAG_INTRDISABLED, &inth[0]);
  78. timer_isr_register(TIMER_GROUP_0, TIMER_1, timer_isr, (void*)1, flags, &inth[1]);
  79. timer_isr_register(TIMER_GROUP_1, TIMER_0, timer_isr, (void*)2, flags, &inth[2]);
  80. timer_isr_register(TIMER_GROUP_1, TIMER_1, timer_isr, (void*)3, flags, &inth[3]);
  81. timer_start(TIMER_GROUP_0, TIMER_0);
  82. timer_start(TIMER_GROUP_0, TIMER_1);
  83. timer_start(TIMER_GROUP_1, TIMER_0);
  84. timer_start(TIMER_GROUP_1, TIMER_1);
  85. for (x=0; x<4; x++) count[x]=0;
  86. printf("Interrupts allocated: %d (dis) %d %d %d\n",
  87. esp_intr_get_intno(inth[0]), esp_intr_get_intno(inth[1]),
  88. esp_intr_get_intno(inth[2]), esp_intr_get_intno(inth[3]));
  89. printf("Timer values on start: %d %d %d %d\n", count[0], count[1], count[2], count[3]);
  90. vTaskDelay(1000 / portTICK_PERIOD_MS);
  91. printf("Timer values after 1 sec: %d %d %d %d\n", count[0], count[1], count[2], count[3]);
  92. TEST_ASSERT(count[0]==0);
  93. TEST_ASSERT(count[1]!=0);
  94. TEST_ASSERT(count[2]!=0);
  95. TEST_ASSERT(count[3]!=0);
  96. printf("Disabling timers 1 and 2...\n");
  97. esp_intr_enable(inth[0]);
  98. esp_intr_disable(inth[1]);
  99. esp_intr_disable(inth[2]);
  100. for (x=0; x<4; x++) count[x]=0;
  101. vTaskDelay(1000 / portTICK_PERIOD_MS);
  102. printf("Timer values after 1 sec: %d %d %d %d\n", count[0], count[1], count[2], count[3]);
  103. TEST_ASSERT(count[0]!=0);
  104. TEST_ASSERT(count[1]==0);
  105. TEST_ASSERT(count[2]==0);
  106. TEST_ASSERT(count[3]!=0);
  107. printf("Disabling other half...\n");
  108. esp_intr_enable(inth[1]);
  109. esp_intr_enable(inth[2]);
  110. esp_intr_disable(inth[0]);
  111. esp_intr_disable(inth[3]);
  112. for (x=0; x<4; x++) count[x]=0;
  113. vTaskDelay(1000 / portTICK_PERIOD_MS);
  114. printf("Timer values after 1 sec: %d %d %d %d\n", count[0], count[1], count[2], count[3]);
  115. TEST_ASSERT(count[0]==0);
  116. TEST_ASSERT(count[1]!=0);
  117. TEST_ASSERT(count[2]!=0);
  118. TEST_ASSERT(count[3]==0);
  119. printf("Done.\n");
  120. esp_intr_free(inth[0]);
  121. esp_intr_free(inth[1]);
  122. esp_intr_free(inth[2]);
  123. esp_intr_free(inth[3]);
  124. }
  125. static volatile int int_timer_ctr;
  126. void int_timer_handler(void *arg) {
  127. xthal_set_ccompare(1, xthal_get_ccount()+8000000);
  128. int_timer_ctr++;
  129. }
  130. void local_timer_test(void)
  131. {
  132. intr_handle_t ih;
  133. esp_err_t r;
  134. r=esp_intr_alloc(ETS_INTERNAL_TIMER1_INTR_SOURCE, 0, int_timer_handler, NULL, &ih);
  135. TEST_ASSERT(r==ESP_OK);
  136. printf("Int timer 1 intno %d\n", esp_intr_get_intno(ih));
  137. xthal_set_ccompare(1, xthal_get_ccount()+8000000);
  138. int_timer_ctr=0;
  139. vTaskDelay(1000 / portTICK_PERIOD_MS);
  140. printf("Timer val after 1 sec: %d\n", int_timer_ctr);
  141. TEST_ASSERT(int_timer_ctr!=0);
  142. printf("Disabling int\n");
  143. esp_intr_disable(ih);
  144. int_timer_ctr=0;
  145. vTaskDelay(1000 / portTICK_PERIOD_MS);
  146. printf("Timer val after 1 sec: %d\n", int_timer_ctr);
  147. TEST_ASSERT(int_timer_ctr==0);
  148. printf("Re-enabling\n");
  149. esp_intr_enable(ih);
  150. vTaskDelay(1000 / portTICK_PERIOD_MS);
  151. printf("Timer val after 1 sec: %d\n", int_timer_ctr);
  152. TEST_ASSERT(int_timer_ctr!=0);
  153. printf("Free int, re-alloc disabled\n");
  154. r=esp_intr_free(ih);
  155. TEST_ASSERT(r==ESP_OK);
  156. r=esp_intr_alloc(ETS_INTERNAL_TIMER1_INTR_SOURCE, ESP_INTR_FLAG_INTRDISABLED, int_timer_handler, NULL, &ih);
  157. TEST_ASSERT(r==ESP_OK);
  158. int_timer_ctr=0;
  159. vTaskDelay(1000 / portTICK_PERIOD_MS);
  160. printf("Timer val after 1 sec: %d\n", int_timer_ctr);
  161. TEST_ASSERT(int_timer_ctr==0);
  162. printf("Re-enabling\n");
  163. esp_intr_enable(ih);
  164. vTaskDelay(1000 / portTICK_PERIOD_MS);
  165. printf("Timer val after 1 sec: %d\n", int_timer_ctr);
  166. TEST_ASSERT(int_timer_ctr!=0);
  167. r=esp_intr_free(ih);
  168. TEST_ASSERT(r==ESP_OK);
  169. printf("Done.\n");
  170. }
  171. TEST_CASE("Intr_alloc test, CPU-local int source", "[esp32]")
  172. {
  173. local_timer_test();
  174. }
  175. TEST_CASE("Intr_alloc test, private ints", "[esp32]")
  176. {
  177. timer_test(0);
  178. }
  179. TEST_CASE("Intr_alloc test, shared ints", "[esp32]")
  180. {
  181. timer_test(ESP_INTR_FLAG_SHARED);
  182. }
  183. TEST_CASE("Can allocate IRAM int only with an IRAM handler", "[esp32]")
  184. {
  185. void dummy(void* arg)
  186. {
  187. }
  188. IRAM_ATTR void dummy_iram(void* arg)
  189. {
  190. }
  191. RTC_IRAM_ATTR void dummy_rtc(void* arg)
  192. {
  193. }
  194. intr_handle_t ih;
  195. esp_err_t err = esp_intr_alloc(ETS_INTERNAL_SW0_INTR_SOURCE,
  196. ESP_INTR_FLAG_IRAM, &dummy, NULL, &ih);
  197. TEST_ASSERT_EQUAL_INT(ESP_ERR_INVALID_ARG, err);
  198. err = esp_intr_alloc(ETS_INTERNAL_SW0_INTR_SOURCE,
  199. ESP_INTR_FLAG_IRAM, &dummy_iram, NULL, &ih);
  200. TEST_ESP_OK(err);
  201. err = esp_intr_free(ih);
  202. TEST_ESP_OK(err);
  203. err = esp_intr_alloc(ETS_INTERNAL_SW0_INTR_SOURCE,
  204. ESP_INTR_FLAG_IRAM, &dummy_rtc, NULL, &ih);
  205. TEST_ESP_OK(err);
  206. err = esp_intr_free(ih);
  207. TEST_ESP_OK(err);
  208. }
  209. #include "soc/spi_periph.h"
  210. typedef struct {
  211. bool flag1;
  212. bool flag2;
  213. bool flag3;
  214. bool flag4;
  215. } intr_alloc_test_ctx_t;
  216. void IRAM_ATTR int_handler1(void* arg)
  217. {
  218. intr_alloc_test_ctx_t* ctx=(intr_alloc_test_ctx_t*)arg;
  219. ets_printf("handler 1 called.\n");
  220. if ( ctx->flag1 ) {
  221. ctx->flag3 = true;
  222. } else {
  223. ctx->flag1 = true;
  224. }
  225. SPI2.slave.trans_done = 0;
  226. }
  227. void IRAM_ATTR int_handler2(void* arg)
  228. {
  229. intr_alloc_test_ctx_t* ctx = (intr_alloc_test_ctx_t*)arg;
  230. ets_printf("handler 2 called.\n");
  231. if ( ctx->flag2 ) {
  232. ctx->flag4 = true;
  233. } else {
  234. ctx->flag2 = true;
  235. }
  236. }
  237. TEST_CASE("allocate 2 handlers for a same source and remove the later one","[esp32]")
  238. {
  239. intr_alloc_test_ctx_t ctx = {false, false, false, false };
  240. intr_handle_t handle1, handle2;
  241. //enable HSPI(spi2)
  242. periph_module_enable(PERIPH_HSPI_MODULE);
  243. esp_err_t r;
  244. r=esp_intr_alloc(ETS_SPI2_INTR_SOURCE, ESP_INTR_FLAG_SHARED, int_handler1, &ctx, &handle1);
  245. TEST_ESP_OK(r);
  246. //try an invalid assign first
  247. r=esp_intr_alloc(ETS_SPI2_INTR_SOURCE, 0, int_handler2, NULL, &handle2);
  248. TEST_ASSERT_EQUAL_INT(r, ESP_ERR_NOT_FOUND );
  249. //assign shared then
  250. r=esp_intr_alloc(ETS_SPI2_INTR_SOURCE, ESP_INTR_FLAG_SHARED, int_handler2, &ctx, &handle2);
  251. TEST_ESP_OK(r);
  252. SPI2.slave.trans_inten = 1;
  253. printf("trigger first time.\n");
  254. SPI2.slave.trans_done = 1;
  255. vTaskDelay(100);
  256. TEST_ASSERT( ctx.flag1 && ctx.flag2 );
  257. printf("remove intr 1.\n");
  258. r=esp_intr_free(handle2);
  259. printf("trigger second time.\n");
  260. SPI2.slave.trans_done = 1;
  261. vTaskDelay(500);
  262. TEST_ASSERT( ctx.flag3 && !ctx.flag4 );
  263. printf("test passed.\n");
  264. }
  265. #ifndef CONFIG_FREERTOS_UNICORE
  266. void isr_free_task(void *param)
  267. {
  268. esp_err_t ret = ESP_FAIL;
  269. intr_handle_t *test_handle = (intr_handle_t *)param;
  270. if(*test_handle != NULL) {
  271. ret = esp_intr_free(*test_handle);
  272. if(ret == ESP_OK) {
  273. *test_handle = NULL;
  274. }
  275. }
  276. vTaskDelete(NULL);
  277. }
  278. void isr_alloc_free_test(void)
  279. {
  280. intr_handle_t test_handle = NULL;
  281. esp_err_t ret = esp_intr_alloc(ETS_SPI2_INTR_SOURCE, 0, int_handler1, NULL, &test_handle);
  282. if(ret != ESP_OK) {
  283. printf("alloc isr handle fail\n");
  284. } else {
  285. printf("alloc isr handle on core %d\n",esp_intr_get_cpu(test_handle));
  286. }
  287. TEST_ASSERT(ret == ESP_OK);
  288. xTaskCreatePinnedToCore(isr_free_task, "isr_free_task", 1024*2, (void *)&test_handle, 10, NULL, !xPortGetCoreID());
  289. vTaskDelay(1000/portTICK_RATE_MS);
  290. TEST_ASSERT(test_handle == NULL);
  291. printf("test passed\n");
  292. }
  293. TEST_CASE("alloc and free isr handle on different core", "[esp32]")
  294. {
  295. isr_alloc_free_test();
  296. }
  297. #endif