test_intr_alloc.c 11 KB

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