test_intr_alloc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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 "unity.h"
  12. #include "soc/uart_periph.h"
  13. #include "soc/gpio_periph.h"
  14. #include "esp_intr_alloc.h"
  15. #include "driver/periph_ctrl.h"
  16. #include "driver/timer.h"
  17. #include "sdkconfig.h"
  18. #if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32S3,ESP32C3)
  19. // TODO ESP32-C3 IDF-2585
  20. #define TIMER_DIVIDER 16 /*!< Hardware timer clock divider */
  21. #define TIMER_SCALE (TIMER_BASE_CLK / TIMER_DIVIDER) /*!< used to calculate counter value */
  22. #define TIMER_INTERVAL0_SEC (3.4179) /*!< test interval for timer 0 */
  23. #define TIMER_INTERVAL1_SEC (5.78) /*!< test interval for timer 1 */
  24. static void my_timer_init(int timer_group, int timer_idx, int ival)
  25. {
  26. timer_config_t config;
  27. config.alarm_en = 1;
  28. config.auto_reload = 1;
  29. config.counter_dir = TIMER_COUNT_UP;
  30. config.divider = TIMER_DIVIDER;
  31. config.intr_type = TIMER_INTR_LEVEL;
  32. config.counter_en = TIMER_PAUSE;
  33. /*Configure timer*/
  34. timer_init(timer_group, timer_idx, &config);
  35. /*Stop timer counter*/
  36. timer_pause(timer_group, timer_idx);
  37. /*Load counter value */
  38. timer_set_counter_value(timer_group, timer_idx, 0x00000000ULL);
  39. /*Set alarm value*/
  40. timer_set_alarm_value(timer_group, timer_idx, ival);
  41. /*Enable timer interrupt*/
  42. timer_enable_intr(timer_group, timer_idx);
  43. }
  44. static volatile int count[4]={0,0,0,0};
  45. static void timer_isr(void *arg)
  46. {
  47. int timer_idx = (int)arg;
  48. count[timer_idx]++;
  49. if (timer_idx==0) {
  50. timer_group_clr_intr_status_in_isr(TIMER_GROUP_0, TIMER_0);
  51. timer_group_enable_alarm_in_isr(TIMER_GROUP_0, TIMER_0);
  52. }
  53. if (timer_idx==1) {
  54. timer_group_clr_intr_status_in_isr(TIMER_GROUP_0, TIMER_1);
  55. timer_group_enable_alarm_in_isr(TIMER_GROUP_0, TIMER_1);
  56. }
  57. if (timer_idx==2) {
  58. timer_group_clr_intr_status_in_isr(TIMER_GROUP_1, TIMER_0);
  59. timer_group_enable_alarm_in_isr(TIMER_GROUP_1, TIMER_0);
  60. }
  61. if (timer_idx==3) {
  62. timer_group_clr_intr_status_in_isr(TIMER_GROUP_1, TIMER_1);
  63. timer_group_enable_alarm_in_isr(TIMER_GROUP_1, TIMER_1);
  64. }
  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. esp_rom_printf("handler 1 called.\n");
  216. if ( ctx->flag1 ) {
  217. ctx->flag3 = true;
  218. } else {
  219. ctx->flag1 = true;
  220. }
  221. #ifdef CONFIG_IDF_TARGET_ESP32
  222. SPI2.slave.trans_done = 0;
  223. #else
  224. GPSPI2.slave.trans_done = 0;
  225. #endif
  226. }
  227. void IRAM_ATTR int_handler2(void* arg)
  228. {
  229. intr_alloc_test_ctx_t* ctx = (intr_alloc_test_ctx_t*)arg;
  230. esp_rom_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","[intr_alloc]")
  238. {
  239. intr_alloc_test_ctx_t ctx = {false, false, false, false };
  240. intr_handle_t handle1, handle2;
  241. #ifdef CONFIG_IDF_TARGET_ESP32
  242. //enable HSPI(spi2)
  243. periph_module_enable(PERIPH_HSPI_MODULE);
  244. #else
  245. periph_module_enable(PERIPH_FSPI_MODULE);
  246. #endif
  247. esp_err_t r;
  248. r=esp_intr_alloc(ETS_SPI2_INTR_SOURCE, ESP_INTR_FLAG_SHARED, int_handler1, &ctx, &handle1);
  249. TEST_ESP_OK(r);
  250. //try an invalid assign first
  251. r=esp_intr_alloc(ETS_SPI2_INTR_SOURCE, 0, int_handler2, NULL, &handle2);
  252. TEST_ASSERT_EQUAL_INT(r, ESP_ERR_NOT_FOUND );
  253. //assign shared then
  254. r=esp_intr_alloc(ETS_SPI2_INTR_SOURCE, ESP_INTR_FLAG_SHARED, int_handler2, &ctx, &handle2);
  255. TEST_ESP_OK(r);
  256. #ifdef CONFIG_IDF_TARGET_ESP32
  257. SPI2.slave.trans_inten = 1;
  258. #else
  259. GPSPI2.slave.int_trans_done_en = 1;
  260. #endif
  261. printf("trigger first time.\n");
  262. #ifdef CONFIG_IDF_TARGET_ESP32
  263. SPI2.slave.trans_done = 1;
  264. #else
  265. GPSPI2.slave.trans_done = 1;
  266. #endif
  267. vTaskDelay(100);
  268. TEST_ASSERT( ctx.flag1 && ctx.flag2 );
  269. printf("remove intr 1.\n");
  270. r=esp_intr_free(handle2);
  271. printf("trigger second time.\n");
  272. #ifdef CONFIG_IDF_TARGET_ESP32
  273. SPI2.slave.trans_done = 1;
  274. #else
  275. GPSPI2.slave.trans_done = 1;
  276. #endif
  277. vTaskDelay(500);
  278. TEST_ASSERT( ctx.flag3 && !ctx.flag4 );
  279. printf("test passed.\n");
  280. }
  281. #ifndef CONFIG_FREERTOS_UNICORE
  282. void isr_free_task(void *param)
  283. {
  284. esp_err_t ret = ESP_FAIL;
  285. intr_handle_t *test_handle = (intr_handle_t *)param;
  286. if(*test_handle != NULL) {
  287. ret = esp_intr_free(*test_handle);
  288. if(ret == ESP_OK) {
  289. *test_handle = NULL;
  290. }
  291. }
  292. vTaskDelete(NULL);
  293. }
  294. void isr_alloc_free_test(void)
  295. {
  296. intr_handle_t test_handle = NULL;
  297. esp_err_t ret = esp_intr_alloc(ETS_SPI2_INTR_SOURCE, 0, int_handler1, NULL, &test_handle);
  298. if(ret != ESP_OK) {
  299. printf("alloc isr handle fail\n");
  300. } else {
  301. printf("alloc isr handle on core %d\n",esp_intr_get_cpu(test_handle));
  302. }
  303. TEST_ASSERT(ret == ESP_OK);
  304. xTaskCreatePinnedToCore(isr_free_task, "isr_free_task", 1024*2, (void *)&test_handle, 10, NULL, !xPortGetCoreID());
  305. vTaskDelay(1000/portTICK_RATE_MS);
  306. TEST_ASSERT(test_handle == NULL);
  307. printf("test passed\n");
  308. }
  309. TEST_CASE("alloc and free isr handle on different core", "[intr_alloc]")
  310. {
  311. isr_alloc_free_test();
  312. }
  313. #endif
  314. #endif // #if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32S3, ESP32C3)