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