test_intr_alloc.c 11 KB

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