test_dedicated_gpio.c 7.9 KB

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