test_intr_alloc.c 10 KB

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