test_dedicated_gpio.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #include "freertos/FreeRTOS.h"
  2. #include "freertos/task.h"
  3. #include "freertos/semphr.h"
  4. #include "unity.h"
  5. #include "test_utils.h"
  6. #include "esp_rom_sys.h"
  7. #include "soc/soc_caps.h"
  8. #include "hal/cpu_ll.h"
  9. #include "driver/gpio.h"
  10. #if SOC_DEDICATED_GPIO_SUPPORTED
  11. #include "driver/dedic_gpio.h"
  12. TEST_CASE("Dedicated GPIO bundle install/uninstall", "[dedic_gpio]")
  13. {
  14. const int test_gpios[SOC_DEDIC_GPIO_OUT_CHANNELS_NUM / 2] = {0};
  15. const int test2_gpios[SOC_DEDIC_GPIO_OUT_CHANNELS_NUM / 2 + 1] = {0};
  16. const int test3_gpios[SOC_DEDIC_GPIO_OUT_CHANNELS_NUM + 1] = {0};
  17. dedic_gpio_bundle_handle_t test_bundle, test_bundle2, test_bundle3 = NULL;
  18. dedic_gpio_bundle_config_t bundle_config = {
  19. .gpio_array = test_gpios,
  20. .array_size = sizeof(test_gpios) / sizeof(test_gpios[0]),
  21. };
  22. dedic_gpio_bundle_config_t bundle_config2 = {
  23. .gpio_array = test2_gpios,
  24. .array_size = sizeof(test2_gpios) / sizeof(test2_gpios[0]),
  25. .flags = {
  26. .out_en = 1,
  27. },
  28. };
  29. dedic_gpio_bundle_config_t bundle_config3 = {
  30. .gpio_array = test3_gpios,
  31. .array_size = sizeof(test3_gpios) / sizeof(test3_gpios[0]),
  32. .flags = {
  33. .out_en = 1,
  34. },
  35. };
  36. TEST_ASSERT_EQUAL_MESSAGE(ESP_ERR_INVALID_ARG, dedic_gpio_new_bundle(&bundle_config, &test_bundle), "shouldn't create bundle if no mode is specified");
  37. bundle_config.flags.out_en = 1;
  38. TEST_ASSERT_EQUAL_MESSAGE(ESP_OK, dedic_gpio_new_bundle(&bundle_config, &test_bundle), "create bundle with half channels failed");
  39. uint32_t mask = 0;
  40. TEST_ESP_OK(dedic_gpio_get_out_mask(test_bundle, &mask));
  41. TEST_ASSERT_EQUAL_MESSAGE((1 << (SOC_DEDIC_GPIO_OUT_CHANNELS_NUM / 2)) - 1, mask, "wrong out mask");
  42. TEST_ESP_OK(dedic_gpio_get_in_mask(test_bundle, &mask));
  43. TEST_ASSERT_EQUAL_MESSAGE(0, mask, "wrong in mask");
  44. TEST_ASSERT_EQUAL_MESSAGE(ESP_ERR_NOT_FOUND, dedic_gpio_new_bundle(&bundle_config2, &test_bundle2), "shouldn't create bundle if there's no enough channels");
  45. TEST_ASSERT_EQUAL_MESSAGE(ESP_OK, dedic_gpio_del_bundle(test_bundle), "delete bundle failed");
  46. TEST_ASSERT_EQUAL_MESSAGE(ESP_ERR_INVALID_ARG, dedic_gpio_new_bundle(&bundle_config3, &test_bundle3), "shouldn't create bundle if the array size exceeds maximum");
  47. }
  48. #define TEST_GPIO_GROUP_SIZE (4)
  49. typedef struct {
  50. SemaphoreHandle_t sem;
  51. const int gpios[TEST_GPIO_GROUP_SIZE];
  52. } test_dedic_task_context_t;
  53. static void test_dedic_gpio_on_specific_core(void *args)
  54. {
  55. test_dedic_task_context_t *ctx = (test_dedic_task_context_t *)args;
  56. uint32_t value = 0;
  57. cpu_ll_write_dedic_gpio_all(0x0); // clear all out channels
  58. // configure a group of GPIOs, output only
  59. const int bundleA_gpios[] = {ctx->gpios[0], ctx->gpios[1]};
  60. gpio_config_t io_conf = {
  61. .mode = GPIO_MODE_OUTPUT,
  62. };
  63. for (int i = 0; i < sizeof(bundleA_gpios) / sizeof(bundleA_gpios[0]); i++) {
  64. io_conf.pin_bit_mask = 1ULL << bundleA_gpios[i];
  65. gpio_config(&io_conf);
  66. }
  67. // Create bundleA, output only
  68. dedic_gpio_bundle_handle_t bundleA = NULL;
  69. dedic_gpio_bundle_config_t bundleA_config = {
  70. .gpio_array = bundleA_gpios,
  71. .array_size = sizeof(bundleA_gpios) / sizeof(bundleA_gpios[0]),
  72. .flags = {
  73. .out_en = 1,
  74. },
  75. };
  76. TEST_ESP_OK(dedic_gpio_new_bundle(&bundleA_config, &bundleA));
  77. // configure another group of GPIOs, input and output
  78. const int bundleB_gpios[] = {ctx->gpios[2], ctx->gpios[3]};
  79. io_conf.mode = GPIO_MODE_INPUT_OUTPUT;
  80. for (int i = 0; i < sizeof(bundleB_gpios) / sizeof(bundleB_gpios[0]); i++) {
  81. io_conf.pin_bit_mask = 1ULL << bundleB_gpios[i];
  82. gpio_config(&io_conf);
  83. }
  84. // GPIO bundleB, input and output
  85. dedic_gpio_bundle_handle_t bundleB = NULL;
  86. dedic_gpio_bundle_config_t bundleB_config = {
  87. .gpio_array = bundleB_gpios,
  88. .array_size = sizeof(bundleB_gpios) / sizeof(bundleB_gpios[0]),
  89. .flags = {
  90. .in_en = 1,
  91. .out_en = 1,
  92. },
  93. };
  94. TEST_ESP_OK(dedic_gpio_new_bundle(&bundleB_config, &bundleB));
  95. dedic_gpio_bundle_write(bundleA, 0x01, 0x01);
  96. dedic_gpio_bundle_write(bundleB, 0x03, 0x03);
  97. value = cpu_ll_read_dedic_gpio_out();
  98. TEST_ASSERT_EQUAL(0x0D, value); // 1101
  99. value = cpu_ll_read_dedic_gpio_in();
  100. TEST_ASSERT_EQUAL(0x03, value); // 11
  101. dedic_gpio_bundle_write(bundleB, 0x02, 0x0);
  102. value = cpu_ll_read_dedic_gpio_out();
  103. TEST_ASSERT_EQUAL(0x05, value); // 0101
  104. value = cpu_ll_read_dedic_gpio_in();
  105. TEST_ASSERT_EQUAL(0x01, value); // 01
  106. cpu_ll_write_dedic_gpio_all(0x0F); // Set all out channels
  107. value = cpu_ll_read_dedic_gpio_out();
  108. TEST_ASSERT_EQUAL(0x0F, value);
  109. value = cpu_ll_read_dedic_gpio_in();
  110. TEST_ASSERT_EQUAL(0x03, value); // 11
  111. TEST_ASSERT_EQUAL(0x03, dedic_gpio_bundle_read_out(bundleA)); // 11
  112. TEST_ASSERT_EQUAL(0x00, dedic_gpio_bundle_read_in(bundleA)); // input is not enabled for bundleA
  113. TEST_ASSERT_EQUAL(0x03, dedic_gpio_bundle_read_out(bundleB)); // 11
  114. TEST_ASSERT_EQUAL(0x03, dedic_gpio_bundle_read_in(bundleB)); // 11
  115. TEST_ESP_OK(dedic_gpio_del_bundle(bundleA));
  116. TEST_ESP_OK(dedic_gpio_del_bundle(bundleB));
  117. xSemaphoreGive(ctx->sem);
  118. vTaskDelete(NULL);
  119. }
  120. TEST_CASE("Dedicated GPIO run on multiple CPU core", "[dedic_gpio]")
  121. {
  122. SemaphoreHandle_t sem = xSemaphoreCreateCounting(SOC_CPU_CORES_NUM, 0);
  123. for (int i = 0; i < SOC_CPU_CORES_NUM; i++) {
  124. int start_gpio = i * TEST_GPIO_GROUP_SIZE;
  125. test_dedic_task_context_t isr_ctx = {
  126. .sem = sem,
  127. .gpios = {start_gpio, start_gpio + 1, start_gpio + 2, start_gpio + 3}
  128. };
  129. xTaskCreatePinnedToCore(test_dedic_gpio_on_specific_core, "dedic_gpio_test_tsk", 4096, &isr_ctx, 1, NULL, i);
  130. }
  131. for (int i = 0; i < SOC_CPU_CORES_NUM; i++) {
  132. xSemaphoreTake(sem, pdMS_TO_TICKS(1000));
  133. }
  134. vSemaphoreDelete(sem);
  135. }
  136. IRAM_ATTR static void test_dedic_gpio_isr_callback(void *args)
  137. {
  138. SemaphoreHandle_t sem = (SemaphoreHandle_t)args;
  139. BaseType_t high_task_wakeup = pdFALSE;
  140. esp_rom_printf("GPIO event\r\n");
  141. xSemaphoreGiveFromISR(sem, &high_task_wakeup);
  142. if (high_task_wakeup) {
  143. esp_rom_printf("high priority task wake up\r\n");
  144. }
  145. }
  146. TEST_CASE("Dedicated GPIO interrupt and callback", "[dedic_gpio]")
  147. {
  148. SemaphoreHandle_t sem = xSemaphoreCreateBinary();
  149. // configure GPIO
  150. const int bundle_gpios[] = {0, 1};
  151. gpio_config_t io_conf = {
  152. .mode = GPIO_MODE_INPUT_OUTPUT,
  153. };
  154. for (int i = 0; i < sizeof(bundle_gpios) / sizeof(bundle_gpios[0]); i++) {
  155. io_conf.pin_bit_mask = 1ULL << bundle_gpios[i];
  156. gpio_config(&io_conf);
  157. }
  158. dedic_gpio_bundle_handle_t bundle = NULL;
  159. dedic_gpio_bundle_config_t bundle_config = {
  160. .gpio_array = bundle_gpios,
  161. .array_size = sizeof(bundle_gpios) / sizeof(bundle_gpios[0]),
  162. .flags = {
  163. .in_en = 1,
  164. .out_en = 1,
  165. },
  166. };
  167. TEST_ESP_OK(dedic_gpio_new_bundle(&bundle_config, &bundle));
  168. // enable interrupt on GPIO1
  169. TEST_ESP_OK(gpio_set_intr_type(1, GPIO_INTR_POSEDGE));
  170. // install gpio isr service
  171. TEST_ESP_OK(gpio_install_isr_service(0));
  172. // hook isr handler for specific gpio pin
  173. TEST_ESP_OK(gpio_isr_handler_add(1, test_dedic_gpio_isr_callback, sem));
  174. // trigger a posedge on GPIO1
  175. dedic_gpio_bundle_write(bundle, BIT(1), 0x00);
  176. dedic_gpio_bundle_write(bundle, BIT(1), 0xFF);
  177. // wait for done semaphore
  178. TEST_ASSERT_EQUAL(pdTRUE, xSemaphoreTake(sem, pdMS_TO_TICKS(1000)));
  179. // remove isr handler for gpio number
  180. TEST_ESP_OK(gpio_isr_handler_remove(1));
  181. // uninstall GPIO interrupt service
  182. gpio_uninstall_isr_service();
  183. TEST_ESP_OK(dedic_gpio_del_bundle(bundle));
  184. vSemaphoreDelete(sem);
  185. }
  186. #endif // #if SOC_DEDICATED_GPIO_SUPPORTED