test_gpio.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  1. /*
  2. * SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * About test environment UT_T1_GPIO:
  8. * Please connect TEST_GPIO_EXT_OUT_IO and TEST_GPIO_EXT_IN_IO
  9. */
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include "esp_system.h"
  13. #include "esp_sleep.h"
  14. #include "unity.h"
  15. #include "driver/gpio.h"
  16. #include "freertos/FreeRTOS.h"
  17. #include "freertos/task.h"
  18. #include "freertos/queue.h"
  19. #include "sdkconfig.h"
  20. #include "esp_rom_uart.h"
  21. #include "esp_rom_sys.h"
  22. #define WAKE_UP_IGNORE 1 // gpio_wakeup function development is not completed yet, set it deprecated.
  23. #if CONFIG_IDF_TARGET_ESP32
  24. #define TEST_GPIO_EXT_OUT_IO 18 // default output GPIO
  25. #define TEST_GPIO_EXT_IN_IO 19 // default input GPIO
  26. #define TEST_GPIO_OUTPUT_PIN 23
  27. #define TEST_GPIO_INPUT_ONLY_PIN 34
  28. #define TEST_GPIO_OUTPUT_MAX GPIO_NUM_34
  29. #define TEST_GPIO_INPUT_LEVEL_HIGH_PIN 2
  30. #define TEST_GPIO_INPUT_LEVEL_LOW_PIN 4
  31. #elif CONFIG_IDF_TARGET_ESP32S2
  32. // ESP32_S2 DEVKIC uses IO19 and IO20 as USB functions, so it is necessary to avoid using IO19, otherwise GPIO io pull up/down function cannot pass
  33. // Also the first version of ESP32-S2-Saola has pullup issue on GPIO18, which is tied to 3V3 on the
  34. // runner. Also avoid using GPIO18.
  35. #define TEST_GPIO_EXT_OUT_IO 17 // default output GPIO
  36. #define TEST_GPIO_EXT_IN_IO 21 // default input GPIO
  37. #define TEST_GPIO_OUTPUT_PIN 12
  38. #define TEST_GPIO_INPUT_ONLY_PIN 46
  39. #define TEST_GPIO_OUTPUT_MAX GPIO_NUM_46
  40. #define TEST_GPIO_INPUT_LEVEL_HIGH_PIN 17
  41. #define TEST_GPIO_INPUT_LEVEL_LOW_PIN 1
  42. #elif CONFIG_IDF_TARGET_ESP32S3
  43. // IO19 and IO20 are connected as USB functions.
  44. #define TEST_GPIO_EXT_OUT_IO 17 // default output GPIO
  45. #define TEST_GPIO_EXT_IN_IO 21 // default input GPIO
  46. #define TEST_GPIO_OUTPUT_PIN 12
  47. #define TEST_GPIO_OUTPUT_MAX GPIO_NUM_MAX
  48. #define TEST_GPIO_USB_DM_IO 19 // USB D- GPIO
  49. #define TEST_GPIO_USB_DP_IO 20 // USB D+ GPIO
  50. #define TEST_GPIO_INPUT_LEVEL_HIGH_PIN 17
  51. #define TEST_GPIO_INPUT_LEVEL_LOW_PIN 1
  52. #elif CONFIG_IDF_TARGET_ESP32C3
  53. #define TEST_GPIO_EXT_OUT_IO 2 // default output GPIO
  54. #define TEST_GPIO_EXT_IN_IO 3 // default input GPIO
  55. #define TEST_GPIO_OUTPUT_PIN 1
  56. #define TEST_GPIO_OUTPUT_MAX GPIO_NUM_21
  57. #define TEST_GPIO_USB_DM_IO 18 // USB D- GPIO
  58. #define TEST_GPIO_USB_DP_IO 19 // USB D+ GPIO
  59. #define TEST_GPIO_INPUT_LEVEL_HIGH_PIN 10
  60. #define TEST_GPIO_INPUT_LEVEL_LOW_PIN 1
  61. #endif
  62. // If there is any input-only pin, enable input-only pin part of some tests.
  63. #define SOC_HAS_INPUT_ONLY_PIN (CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2)
  64. // define public test io on all boards(esp32, esp32s2, esp32s3, esp32c3)
  65. #define TEST_IO_9 GPIO_NUM_9
  66. #define TEST_IO_10 GPIO_NUM_10
  67. static volatile int disable_intr_times = 0; // use this to calculate how many times it go into interrupt
  68. static volatile int level_intr_times = 0; // use this to get how many times the level interrupt happened
  69. static volatile int edge_intr_times = 0; // use this to get how many times the edge interrupt happened
  70. #if !WAKE_UP_IGNORE
  71. static bool wake_up_result = false; // use this to judge the wake up event happen or not
  72. #endif
  73. /**
  74. * do some initialization operation in this function
  75. * @param num: it is the destination GPIO wanted to be initialized
  76. *
  77. */
  78. static gpio_config_t init_io(gpio_num_t num)
  79. {
  80. TEST_ASSERT(num < TEST_GPIO_OUTPUT_MAX);
  81. gpio_config_t io_conf;
  82. io_conf.intr_type = GPIO_INTR_DISABLE;
  83. io_conf.mode = GPIO_MODE_OUTPUT;
  84. io_conf.pin_bit_mask = (1ULL << num);
  85. io_conf.pull_down_en = 0;
  86. io_conf.pull_up_en = 0;
  87. return io_conf;
  88. }
  89. // edge interrupt event
  90. __attribute__((unused)) static void gpio_isr_edge_handler(void *arg)
  91. {
  92. uint32_t gpio_num = (uint32_t) arg;
  93. esp_rom_printf("GPIO[%d] intr, val: %d\n", gpio_num, gpio_get_level(gpio_num));
  94. edge_intr_times++;
  95. }
  96. #if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32S2, ESP32S3, ESP32C3)
  97. //No runners
  98. // level interrupt event with "gpio_intr_disable"
  99. static void gpio_isr_level_handler(void *arg)
  100. {
  101. uint32_t gpio_num = (uint32_t) arg;
  102. disable_intr_times++;
  103. esp_rom_printf("GPIO[%d] intr, val: %d, disable_intr_times = %d\n", gpio_num, gpio_get_level(gpio_num), disable_intr_times);
  104. gpio_intr_disable(gpio_num);
  105. }
  106. // level interrupt event
  107. static void gpio_isr_level_handler2(void *arg)
  108. {
  109. uint32_t gpio_num = (uint32_t) arg;
  110. level_intr_times++;
  111. esp_rom_printf("GPIO[%d] intr, val: %d\n", gpio_num, gpio_get_level(gpio_num));
  112. if (gpio_get_level(gpio_num)) {
  113. gpio_set_level(TEST_GPIO_EXT_OUT_IO, 0);
  114. } else {
  115. gpio_set_level(TEST_GPIO_EXT_OUT_IO, 1);
  116. }
  117. esp_rom_printf("GPIO[%d] intr, val: %d, level_intr_times = %d\n", TEST_GPIO_EXT_OUT_IO, gpio_get_level(TEST_GPIO_EXT_OUT_IO), level_intr_times);
  118. esp_rom_printf("GPIO[%d] intr, val: %d, level_intr_times = %d\n", gpio_num, gpio_get_level(gpio_num), level_intr_times);
  119. }
  120. #endif //!TEMPORARY_DISABLED_FOR_TARGETS(ESP32S2, ESP32S3, ESP32C3)
  121. #if !WAKE_UP_IGNORE
  122. // get result of waking up or not
  123. static void sleep_wake_up(void *arg)
  124. {
  125. gpio_config_t io_config = init_io(TEST_GPIO_EXT_IN_IO);
  126. io_config.mode = GPIO_MODE_INPUT;
  127. gpio_config(&io_config);
  128. TEST_ESP_OK(gpio_wakeup_enable(TEST_GPIO_EXT_IN_IO, GPIO_INTR_HIGH_LEVEL));
  129. esp_light_sleep_start();
  130. wake_up_result = true;
  131. }
  132. // wake up light sleep event
  133. static void trigger_wake_up(void *arg)
  134. {
  135. gpio_config_t io_config = init_io(TEST_GPIO_EXT_OUT_IO);
  136. gpio_config(&io_config);
  137. gpio_set_level(TEST_GPIO_EXT_OUT_IO, 0);
  138. gpio_install_isr_service(0);
  139. gpio_isr_handler_add(TEST_GPIO_EXT_OUT_IO, gpio_isr_level_handler, (void *) TEST_GPIO_EXT_IN_IO);
  140. gpio_set_level(TEST_GPIO_EXT_OUT_IO, 1);
  141. vTaskDelay(100 / portTICK_RATE_MS);
  142. }
  143. #endif //!WAKE_UP_IGNORE
  144. static void prompt_to_continue(const char *str)
  145. {
  146. printf("%s , please press \"Enter\" to go on!\n", str);
  147. char sign[5] = {0};
  148. while (strlen(sign) == 0) {
  149. /* Flush anything already in the RX buffer */
  150. while (esp_rom_uart_rx_one_char((uint8_t *) sign) == ETS_OK) {
  151. }
  152. /* Read line */
  153. esp_rom_uart_rx_string((uint8_t *) sign, sizeof(sign) - 1);
  154. }
  155. }
  156. static void drive_capability_set_get(gpio_num_t num, gpio_drive_cap_t capability)
  157. {
  158. gpio_config_t pad_io = init_io(num);
  159. TEST_ESP_OK(gpio_config(&pad_io));
  160. TEST_ASSERT(gpio_set_drive_capability(num, GPIO_DRIVE_CAP_MAX) == ESP_ERR_INVALID_ARG);
  161. gpio_drive_cap_t cap;
  162. TEST_ESP_OK(gpio_set_drive_capability(num, capability));
  163. TEST_ESP_OK(gpio_get_drive_capability(num, &cap));
  164. TEST_ASSERT_EQUAL_INT(cap, capability);
  165. }
  166. // test the basic configuration function with right parameters and error parameters
  167. TEST_CASE("GPIO config parameters test", "[gpio]")
  168. {
  169. //error param test
  170. //ESP32 test 41 bit, ESP32-S2 test 48 bit, ESP32-S3 test 50 bit
  171. gpio_config_t io_config = { 0 };
  172. io_config.intr_type = GPIO_INTR_DISABLE;
  173. io_config.pin_bit_mask = ((uint64_t)1 << (GPIO_NUM_MAX + 1));
  174. TEST_ASSERT(gpio_config(&io_config) == ESP_ERR_INVALID_ARG);
  175. // test 0
  176. io_config.pin_bit_mask = 0;
  177. TEST_ASSERT(gpio_config(&io_config) == ESP_ERR_INVALID_ARG);
  178. //ESP32 test 40 bit, ESP32-S2 test 47 bit, ESP32-S3 test 49 bit
  179. io_config.pin_bit_mask = ((uint64_t)1 << GPIO_NUM_MAX);
  180. TEST_ASSERT(gpio_config(&io_config) == ESP_ERR_INVALID_ARG);
  181. io_config.pin_bit_mask = ((uint64_t)1 << TEST_GPIO_OUTPUT_PIN);
  182. TEST_ESP_OK(gpio_config(&io_config));
  183. //This IO is just used for input, C3 and S3 doesn't have input only pin.
  184. #if SOC_HAS_INPUT_ONLY_PIN
  185. io_config.pin_bit_mask = ((uint64_t)1 << TEST_GPIO_INPUT_ONLY_PIN);
  186. io_config.mode = GPIO_MODE_INPUT;
  187. TEST_ESP_OK(gpio_config(&io_config));
  188. io_config.mode = GPIO_MODE_OUTPUT;
  189. // The pin is input only, once set as output should log something
  190. TEST_ASSERT(gpio_config(&io_config) == ESP_ERR_INVALID_ARG);
  191. #endif // SOC_HAS_INPUT_ONLY_PIN
  192. }
  193. #if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32S2, ESP32S3, ESP32C3)
  194. //No runners
  195. TEST_CASE("GPIO rising edge interrupt test", "[gpio][test_env=UT_T1_GPIO]")
  196. {
  197. edge_intr_times = 0; // set it as 0 prepare to test
  198. //init input and output gpio
  199. gpio_config_t output_io = init_io(TEST_GPIO_EXT_OUT_IO);
  200. gpio_config_t input_io = init_io(TEST_GPIO_EXT_IN_IO);
  201. input_io.intr_type = GPIO_INTR_POSEDGE;
  202. input_io.mode = GPIO_MODE_INPUT;
  203. input_io.pull_up_en = 1;
  204. TEST_ESP_OK(gpio_config(&output_io));
  205. TEST_ESP_OK(gpio_config(&input_io));
  206. TEST_ESP_OK(gpio_set_level(TEST_GPIO_EXT_OUT_IO, 0));
  207. //rising edge intr
  208. TEST_ESP_OK(gpio_set_intr_type(TEST_GPIO_EXT_IN_IO, GPIO_INTR_POSEDGE));
  209. TEST_ESP_OK(gpio_install_isr_service(0));
  210. gpio_isr_handler_add(TEST_GPIO_EXT_IN_IO, gpio_isr_edge_handler, (void *)TEST_GPIO_EXT_IN_IO);
  211. TEST_ESP_OK(gpio_set_level(TEST_GPIO_EXT_OUT_IO, 1));
  212. TEST_ASSERT_EQUAL_INT(edge_intr_times, 1);
  213. vTaskDelay(100 / portTICK_RATE_MS);
  214. gpio_isr_handler_remove(TEST_GPIO_EXT_IN_IO);
  215. gpio_uninstall_isr_service();
  216. }
  217. TEST_CASE("GPIO falling edge interrupt test", "[gpio][test_env=UT_T1_GPIO]")
  218. {
  219. edge_intr_times = 0;
  220. gpio_config_t output_io = init_io(TEST_GPIO_EXT_OUT_IO);
  221. gpio_config_t input_io = init_io(TEST_GPIO_EXT_IN_IO);
  222. input_io.intr_type = GPIO_INTR_POSEDGE;
  223. input_io.mode = GPIO_MODE_INPUT;
  224. input_io.pull_up_en = 1;
  225. TEST_ESP_OK(gpio_config(&output_io));
  226. TEST_ESP_OK(gpio_config(&input_io));
  227. TEST_ESP_OK(gpio_set_level(TEST_GPIO_EXT_OUT_IO, 1));
  228. gpio_set_intr_type(TEST_GPIO_EXT_IN_IO, GPIO_INTR_NEGEDGE);
  229. gpio_install_isr_service(0);
  230. gpio_isr_handler_add(TEST_GPIO_EXT_IN_IO, gpio_isr_edge_handler, (void *) TEST_GPIO_EXT_IN_IO);
  231. gpio_set_level(TEST_GPIO_EXT_OUT_IO, 0);
  232. vTaskDelay(100 / portTICK_RATE_MS);
  233. TEST_ASSERT_EQUAL_INT(edge_intr_times, 1);
  234. vTaskDelay(100 / portTICK_RATE_MS);
  235. gpio_isr_handler_remove(TEST_GPIO_EXT_IN_IO);
  236. gpio_uninstall_isr_service();
  237. }
  238. TEST_CASE("GPIO both rising and falling edge interrupt test", "[gpio][test_env=UT_T1_GPIO]")
  239. {
  240. edge_intr_times = 0;
  241. gpio_config_t output_io = init_io(TEST_GPIO_EXT_OUT_IO);
  242. gpio_config_t input_io = init_io(TEST_GPIO_EXT_IN_IO);
  243. input_io.intr_type = GPIO_INTR_POSEDGE;
  244. input_io.mode = GPIO_MODE_INPUT;
  245. input_io.pull_up_en = 1;
  246. TEST_ESP_OK(gpio_config(&output_io));
  247. TEST_ESP_OK(gpio_config(&input_io));
  248. TEST_ESP_OK(gpio_set_level(TEST_GPIO_EXT_OUT_IO, 0));
  249. int level = 0;
  250. gpio_set_intr_type(TEST_GPIO_EXT_IN_IO, GPIO_INTR_ANYEDGE);
  251. gpio_install_isr_service(0);
  252. gpio_isr_handler_add(TEST_GPIO_EXT_IN_IO, gpio_isr_edge_handler, (void *) TEST_GPIO_EXT_IN_IO);
  253. // for rising edge in GPIO_INTR_ANYEDGE
  254. while (1) {
  255. level = level + 1;
  256. gpio_set_level(TEST_GPIO_EXT_OUT_IO, level * 0.2);
  257. if (level > 10) {
  258. break;
  259. }
  260. vTaskDelay(100 / portTICK_RATE_MS);
  261. }
  262. vTaskDelay(100 / portTICK_RATE_MS);
  263. // for falling rdge in GPIO_INTR_ANYEDGE
  264. while (1) {
  265. level = level - 1;
  266. gpio_set_level(TEST_GPIO_EXT_OUT_IO, level / 5);
  267. if (level < 0) {
  268. break;
  269. }
  270. vTaskDelay(100 / portTICK_RATE_MS);
  271. }
  272. vTaskDelay(100 / portTICK_RATE_MS);
  273. TEST_ASSERT_EQUAL_INT(edge_intr_times, 2);
  274. vTaskDelay(100 / portTICK_RATE_MS);
  275. gpio_isr_handler_remove(TEST_GPIO_EXT_IN_IO);
  276. gpio_uninstall_isr_service();
  277. }
  278. TEST_CASE("GPIO input high level trigger, cut the interrupt source exit interrupt test", "[gpio][test_env=UT_T1_GPIO]")
  279. {
  280. level_intr_times = 0;
  281. gpio_config_t output_io = init_io(TEST_GPIO_EXT_OUT_IO);
  282. gpio_config_t input_io = init_io(TEST_GPIO_EXT_IN_IO);
  283. input_io.intr_type = GPIO_INTR_POSEDGE;
  284. input_io.mode = GPIO_MODE_INPUT;
  285. input_io.pull_up_en = 1;
  286. TEST_ESP_OK(gpio_config(&output_io));
  287. TEST_ESP_OK(gpio_config(&input_io));
  288. TEST_ESP_OK(gpio_set_level(TEST_GPIO_EXT_OUT_IO, 0));
  289. gpio_set_intr_type(TEST_GPIO_EXT_IN_IO, GPIO_INTR_HIGH_LEVEL);
  290. gpio_install_isr_service(0);
  291. gpio_isr_handler_add(TEST_GPIO_EXT_IN_IO, gpio_isr_level_handler2, (void *) TEST_GPIO_EXT_IN_IO);
  292. gpio_set_level(TEST_GPIO_EXT_OUT_IO, 1);
  293. vTaskDelay(100 / portTICK_RATE_MS);
  294. TEST_ASSERT_EQUAL_INT_MESSAGE(level_intr_times, 1, "go into high-level interrupt more than once with cur interrupt source way");
  295. gpio_isr_handler_remove(TEST_GPIO_EXT_IN_IO);
  296. gpio_uninstall_isr_service();
  297. }
  298. TEST_CASE("GPIO low level interrupt test", "[gpio][test_env=UT_T1_GPIO]")
  299. {
  300. disable_intr_times = 0;
  301. gpio_config_t output_io = init_io(TEST_GPIO_EXT_OUT_IO);
  302. gpio_config_t input_io = init_io(TEST_GPIO_EXT_IN_IO);
  303. input_io.intr_type = GPIO_INTR_POSEDGE;
  304. input_io.mode = GPIO_MODE_INPUT;
  305. input_io.pull_up_en = 1;
  306. TEST_ESP_OK(gpio_config(&output_io));
  307. TEST_ESP_OK(gpio_config(&input_io));
  308. TEST_ESP_OK(gpio_set_level(TEST_GPIO_EXT_OUT_IO, 1));
  309. gpio_set_intr_type(TEST_GPIO_EXT_IN_IO, GPIO_INTR_LOW_LEVEL);
  310. gpio_install_isr_service(0);
  311. gpio_isr_handler_add(TEST_GPIO_EXT_IN_IO, gpio_isr_level_handler, (void *) TEST_GPIO_EXT_IN_IO);
  312. gpio_set_level(TEST_GPIO_EXT_OUT_IO, 0);
  313. printf("get level:%d\n", gpio_get_level(TEST_GPIO_EXT_IN_IO));
  314. vTaskDelay(100 / portTICK_RATE_MS);
  315. TEST_ASSERT_EQUAL_INT_MESSAGE(disable_intr_times, 1, "go into low-level interrupt more than once with disable way");
  316. gpio_isr_handler_remove(TEST_GPIO_EXT_IN_IO);
  317. gpio_uninstall_isr_service();
  318. }
  319. TEST_CASE("GPIO multi-level interrupt test, to cut the interrupt source exit interrupt ", "[gpio][test_env=UT_T1_GPIO]")
  320. {
  321. level_intr_times = 0;
  322. gpio_config_t output_io = init_io(TEST_GPIO_EXT_OUT_IO);
  323. gpio_config_t input_io = init_io(TEST_GPIO_EXT_IN_IO);
  324. input_io.intr_type = GPIO_INTR_POSEDGE;
  325. input_io.mode = GPIO_MODE_INPUT;
  326. input_io.pull_up_en = 1;
  327. TEST_ESP_OK(gpio_config(&output_io));
  328. TEST_ESP_OK(gpio_config(&input_io));
  329. TEST_ESP_OK(gpio_set_level(TEST_GPIO_EXT_OUT_IO, 0));
  330. gpio_set_intr_type(TEST_GPIO_EXT_IN_IO, GPIO_INTR_HIGH_LEVEL);
  331. gpio_install_isr_service(0);
  332. gpio_isr_handler_add(TEST_GPIO_EXT_IN_IO, gpio_isr_level_handler2, (void *) TEST_GPIO_EXT_IN_IO);
  333. gpio_set_level(TEST_GPIO_EXT_OUT_IO, 1);
  334. vTaskDelay(100 / portTICK_RATE_MS);
  335. TEST_ASSERT_EQUAL_INT_MESSAGE(level_intr_times, 1, "go into high-level interrupt more than once with cur interrupt source way");
  336. gpio_set_level(TEST_GPIO_EXT_OUT_IO, 1);
  337. vTaskDelay(200 / portTICK_RATE_MS);
  338. TEST_ASSERT_EQUAL_INT_MESSAGE(level_intr_times, 2, "go into high-level interrupt more than once with cur interrupt source way");
  339. gpio_isr_handler_remove(TEST_GPIO_EXT_IN_IO);
  340. gpio_uninstall_isr_service();
  341. }
  342. TEST_CASE("GPIO enable and disable interrupt test", "[gpio][test_env=UT_T1_GPIO]")
  343. {
  344. disable_intr_times = 0;
  345. gpio_config_t output_io = init_io(TEST_GPIO_EXT_OUT_IO);
  346. gpio_config_t input_io = init_io(TEST_GPIO_EXT_IN_IO);
  347. input_io.intr_type = GPIO_INTR_POSEDGE;
  348. input_io.mode = GPIO_MODE_INPUT;
  349. input_io.pull_up_en = 1;
  350. TEST_ESP_OK(gpio_config(&output_io));
  351. TEST_ESP_OK(gpio_config(&input_io));
  352. TEST_ESP_OK(gpio_set_level(TEST_GPIO_EXT_OUT_IO, 0)); // Because of GPIO_INTR_HIGH_LEVEL interrupt, 0 must be set first
  353. TEST_ESP_OK(gpio_set_intr_type(TEST_GPIO_EXT_IN_IO, GPIO_INTR_HIGH_LEVEL));
  354. TEST_ESP_OK(gpio_install_isr_service(0));
  355. TEST_ESP_OK(gpio_isr_handler_add(TEST_GPIO_EXT_IN_IO, gpio_isr_level_handler, (void *) TEST_GPIO_EXT_IN_IO));
  356. TEST_ESP_OK(gpio_set_level(TEST_GPIO_EXT_OUT_IO, 1));
  357. TEST_ESP_OK(gpio_isr_handler_remove(TEST_GPIO_EXT_IN_IO));
  358. TEST_ESP_OK(gpio_set_level(TEST_GPIO_EXT_OUT_IO, 0));
  359. TEST_ASSERT_EQUAL_INT_MESSAGE(disable_intr_times, 1, "go into high-level interrupt more than once with disable way");
  360. // not install service now
  361. vTaskDelay(100 / portTICK_RATE_MS);
  362. TEST_ESP_OK(gpio_intr_disable(TEST_GPIO_EXT_IN_IO));
  363. TEST_ESP_OK(gpio_set_level(TEST_GPIO_EXT_OUT_IO, 1));
  364. TEST_ASSERT_EQUAL_INT_MESSAGE(disable_intr_times, 1, "disable interrupt does not work, still go into interrupt!");
  365. gpio_uninstall_isr_service(); //uninstall the service
  366. TEST_ASSERT(gpio_isr_handler_add(TEST_GPIO_EXT_IN_IO, gpio_isr_level_handler, (void *) TEST_GPIO_EXT_IN_IO) == ESP_ERR_INVALID_STATE);
  367. TEST_ASSERT(gpio_isr_handler_remove(TEST_GPIO_EXT_IN_IO) == ESP_ERR_INVALID_STATE);
  368. }
  369. #endif //DISABLED_FOR_TARGETS(ESP32S2, ESP32S3, ESP32C3)
  370. // ESP32 Connect GPIO18 with GPIO19, ESP32-S2 Connect GPIO17 with GPIO21,
  371. // ESP32-S3 Connect GPIO17 with GPIO21, ESP32C3 Connect GPIO2 with GPIO3
  372. // use multimeter to test the voltage, so it is ignored in CI
  373. TEST_CASE("GPIO set gpio output level test", "[gpio][ignore][UT_T1_GPIO]")
  374. {
  375. gpio_config_t io_conf;
  376. io_conf.intr_type = GPIO_INTR_DISABLE;
  377. io_conf.mode = GPIO_MODE_OUTPUT;
  378. io_conf.pin_bit_mask = ((uint64_t)1 << TEST_GPIO_EXT_OUT_IO);
  379. io_conf.pull_down_en = 0;
  380. io_conf.pull_up_en = 0;
  381. gpio_config(&io_conf);
  382. io_conf.pin_bit_mask = ((uint64_t)1 << TEST_GPIO_EXT_IN_IO);
  383. io_conf.mode = GPIO_MODE_INPUT;
  384. gpio_config(&io_conf);
  385. gpio_set_level(TEST_GPIO_EXT_OUT_IO, 0);
  386. // tested voltage is around 0v
  387. TEST_ASSERT_EQUAL_INT_MESSAGE(gpio_get_level(TEST_GPIO_EXT_IN_IO), 0, "get level error! the level should be low!");
  388. vTaskDelay(1000 / portTICK_RATE_MS);
  389. gpio_set_level(TEST_GPIO_EXT_OUT_IO, 1);
  390. // tested voltage is around 3.3v
  391. TEST_ASSERT_EQUAL_INT_MESSAGE(gpio_get_level(TEST_GPIO_EXT_IN_IO), 1, "get level error! the level should be high!");
  392. //This IO is just used for input, C3 and S3 doesn't have input only pin.
  393. #if SOC_HAS_INPUT_ONLY_PIN
  394. io_conf.pin_bit_mask = ((uint64_t)1 << TEST_GPIO_INPUT_ONLY_PIN);
  395. io_conf.mode = GPIO_MODE_OUTPUT;
  396. gpio_config(&io_conf);
  397. TEST_ASSERT(gpio_config(&io_conf) == ESP_ERR_INVALID_ARG);
  398. #endif // SOC_HAS_INPUT_ONLY_PIN
  399. }
  400. // TEST_GPIO_INPUT_LEVEL_HIGH_PIN connects to 3.3v pin, TEST_GPIO_INPUT_LEVEL_LOW_PIN connects to the GND pin
  401. // use multimeter to test the voltage, so it is ignored in CI
  402. TEST_CASE("GPIO get input level test", "[gpio][ignore]")
  403. {
  404. gpio_num_t num1 = TEST_GPIO_INPUT_LEVEL_HIGH_PIN;
  405. int level1 = gpio_get_level(num1);
  406. printf("TEST_GPIO_INPUT_LEVEL_HIGH_PIN's level is: %d\n", level1);
  407. TEST_ASSERT_EQUAL_INT_MESSAGE(level1, 1, "get level error! the level should be high!");
  408. gpio_num_t num2 = TEST_GPIO_INPUT_LEVEL_LOW_PIN;
  409. int level2 = gpio_get_level(num2);
  410. printf("TEST_GPIO_INPUT_LEVEL_LOW_PIN's level is: %d\n", level2);
  411. TEST_ASSERT_EQUAL_INT_MESSAGE(level2, 0, "get level error! the level should be low!");
  412. printf("the memory get: %d\n", esp_get_free_heap_size());
  413. //when case finish, get the result from multimeter, the TEST_GPIO_INPUT_LEVEL_HIGH_PIN is 3.3v, the TEST_GPIO_INPUT_LEVEL_LOW_PIN is 0.00v
  414. }
  415. TEST_CASE("GPIO io pull up/down function", "[gpio]")
  416. {
  417. // First, ensure that the output IO will not affect the level
  418. gpio_config_t io_conf = init_io(TEST_GPIO_EXT_OUT_IO);
  419. gpio_config(&io_conf);
  420. gpio_set_direction(TEST_GPIO_EXT_OUT_IO, GPIO_MODE_INPUT);
  421. io_conf = init_io(TEST_GPIO_EXT_IN_IO);
  422. gpio_config(&io_conf);
  423. gpio_set_direction(TEST_GPIO_EXT_IN_IO, GPIO_MODE_INPUT);
  424. TEST_ESP_OK(gpio_pullup_en(TEST_GPIO_EXT_IN_IO)); // pull up first
  425. vTaskDelay(100 / portTICK_RATE_MS);
  426. TEST_ASSERT_EQUAL_INT_MESSAGE(gpio_get_level(TEST_GPIO_EXT_IN_IO), 1, "gpio_pullup_en error, it can't pull up");
  427. TEST_ESP_OK(gpio_pulldown_dis(TEST_GPIO_EXT_IN_IO)); //can't be pull down
  428. vTaskDelay(100 / portTICK_RATE_MS);
  429. TEST_ASSERT_EQUAL_INT_MESSAGE(gpio_get_level(TEST_GPIO_EXT_IN_IO), 1, "gpio_pulldown_dis error, it can pull down");
  430. TEST_ESP_OK(gpio_pulldown_en(TEST_GPIO_EXT_IN_IO)); // can be pull down now
  431. vTaskDelay(100 / portTICK_RATE_MS);
  432. TEST_ASSERT_EQUAL_INT_MESSAGE(gpio_get_level(TEST_GPIO_EXT_IN_IO), 0, "gpio_pulldown_en error, it can't pull down");
  433. TEST_ESP_OK(gpio_pullup_dis(TEST_GPIO_EXT_IN_IO)); // can't be pull up
  434. vTaskDelay(100 / portTICK_RATE_MS);
  435. TEST_ASSERT_EQUAL_INT_MESSAGE(gpio_get_level(TEST_GPIO_EXT_IN_IO), 0, "gpio_pullup_dis error, it can pull up");
  436. }
  437. #if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32S2, ESP32S3, ESP32C3)
  438. //No runners
  439. TEST_CASE("GPIO output and input mode test", "[gpio][test_env=UT_T1_GPIO]")
  440. {
  441. //ESP32 connect io18 and io19, ESP32-S2 connect io17 and io21, ESP32-S3 connect io17 and io21, ESP32C3 Connect GPIO2 with GPIO3
  442. gpio_config_t output_io = init_io(TEST_GPIO_EXT_OUT_IO);
  443. gpio_config_t input_io = init_io(TEST_GPIO_EXT_IN_IO);
  444. gpio_config(&output_io);
  445. gpio_config(&input_io);
  446. int level = gpio_get_level(TEST_GPIO_EXT_IN_IO);
  447. //disable mode
  448. gpio_set_direction(TEST_GPIO_EXT_OUT_IO, GPIO_MODE_DISABLE);
  449. gpio_set_direction(TEST_GPIO_EXT_IN_IO, GPIO_MODE_OUTPUT);
  450. gpio_set_level(TEST_GPIO_EXT_OUT_IO, !level);
  451. TEST_ASSERT_EQUAL_INT_MESSAGE(gpio_get_level(TEST_GPIO_EXT_IN_IO), level, "direction GPIO_MODE_DISABLE set error, it can output");
  452. //input mode and output mode
  453. gpio_set_direction(TEST_GPIO_EXT_OUT_IO, GPIO_MODE_OUTPUT);
  454. gpio_set_direction(TEST_GPIO_EXT_IN_IO, GPIO_MODE_INPUT);
  455. gpio_set_level(TEST_GPIO_EXT_OUT_IO, 1);
  456. TEST_ASSERT_EQUAL_INT_MESSAGE(gpio_get_level(TEST_GPIO_EXT_IN_IO), 1, "direction GPIO_MODE_OUTPUT set error, it can't output");
  457. gpio_set_level(TEST_GPIO_EXT_OUT_IO, 0);
  458. TEST_ASSERT_EQUAL_INT_MESSAGE(gpio_get_level(TEST_GPIO_EXT_IN_IO), 0, "direction GPIO_MODE_OUTPUT set error, it can't output");
  459. // open drain mode(output), can just output low level
  460. gpio_set_direction(TEST_GPIO_EXT_OUT_IO, GPIO_MODE_OUTPUT_OD);
  461. gpio_set_direction(TEST_GPIO_EXT_IN_IO, GPIO_MODE_INPUT);
  462. gpio_set_level(TEST_GPIO_EXT_OUT_IO, 1);
  463. TEST_ASSERT_EQUAL_INT_MESSAGE(gpio_get_level(TEST_GPIO_EXT_IN_IO), 0, "direction GPIO_MODE_OUTPUT set error, it can't output");
  464. gpio_set_level(TEST_GPIO_EXT_OUT_IO, 0);
  465. TEST_ASSERT_EQUAL_INT_MESSAGE(gpio_get_level(TEST_GPIO_EXT_IN_IO), 0, "direction GPIO_MODE_OUTPUT set error, it can't output");
  466. // open drain mode(output and input), can just output low level
  467. // output test
  468. gpio_set_direction(TEST_GPIO_EXT_OUT_IO, GPIO_MODE_INPUT_OUTPUT_OD);
  469. gpio_set_direction(TEST_GPIO_EXT_IN_IO, GPIO_MODE_INPUT);
  470. gpio_set_level(TEST_GPIO_EXT_OUT_IO, 1);
  471. TEST_ASSERT_EQUAL_INT_MESSAGE(gpio_get_level(TEST_GPIO_EXT_IN_IO), 0, "direction GPIO_MODE_OUTPUT set error, it can't output");
  472. gpio_set_level(TEST_GPIO_EXT_OUT_IO, 0);
  473. TEST_ASSERT_EQUAL_INT_MESSAGE(gpio_get_level(TEST_GPIO_EXT_IN_IO), 0, "direction GPIO_MODE_OUTPUT set error, it can't output");
  474. // GPIO_MODE_INPUT_OUTPUT mode
  475. // output test
  476. level = gpio_get_level(TEST_GPIO_EXT_IN_IO);
  477. gpio_set_direction(TEST_GPIO_EXT_OUT_IO, GPIO_MODE_INPUT_OUTPUT);
  478. gpio_set_direction(TEST_GPIO_EXT_IN_IO, GPIO_MODE_INPUT);
  479. gpio_set_level(TEST_GPIO_EXT_OUT_IO, !level);
  480. TEST_ASSERT_EQUAL_INT_MESSAGE(gpio_get_level(TEST_GPIO_EXT_IN_IO), !level, "direction set error, it can't output");
  481. }
  482. TEST_CASE("GPIO repeate call service and isr has no memory leak test", "[gpio][test_env=UT_T1_GPIO][timeout=90]")
  483. {
  484. gpio_config_t output_io = init_io(TEST_GPIO_EXT_OUT_IO);
  485. gpio_config_t input_io = init_io(TEST_GPIO_EXT_IN_IO);
  486. input_io.intr_type = GPIO_INTR_POSEDGE;
  487. input_io.mode = GPIO_MODE_INPUT;
  488. input_io.pull_up_en = 1;
  489. TEST_ESP_OK(gpio_config(&output_io));
  490. TEST_ESP_OK(gpio_config(&input_io));
  491. TEST_ESP_OK(gpio_set_level(TEST_GPIO_EXT_OUT_IO, 0));
  492. //rising edge
  493. uint32_t size = esp_get_free_heap_size();
  494. for (int i = 0; i < 1000; i++) {
  495. TEST_ESP_OK(gpio_set_intr_type(TEST_GPIO_EXT_IN_IO, GPIO_INTR_POSEDGE));
  496. TEST_ESP_OK(gpio_install_isr_service(0));
  497. TEST_ESP_OK(gpio_isr_handler_add(TEST_GPIO_EXT_IN_IO, gpio_isr_edge_handler, (void *)TEST_GPIO_EXT_IN_IO));
  498. gpio_set_level(TEST_GPIO_EXT_OUT_IO, 1);
  499. TEST_ESP_OK(gpio_isr_handler_remove(TEST_GPIO_EXT_IN_IO));
  500. gpio_set_level(TEST_GPIO_EXT_OUT_IO, 0);
  501. gpio_uninstall_isr_service();
  502. }
  503. TEST_ASSERT_INT32_WITHIN(size, esp_get_free_heap_size(), 100);
  504. }
  505. #endif //DISABLED_FOR_TARGETS(ESP32S2, ESP32S3, ESP32C3)
  506. #if !WAKE_UP_IGNORE
  507. //this function development is not completed yet, set it ignored
  508. TEST_CASE("GPIO wake up enable and disenable test", "[gpio][ignore]")
  509. {
  510. xTaskCreate(sleep_wake_up, "sleep_wake_up", 4096, NULL, 5, NULL);
  511. xTaskCreate(trigger_wake_up, "trigger_wake_up", 4096, NULL, 5, NULL);
  512. vTaskDelay(100 / portTICK_RATE_MS);
  513. TEST_ASSERT_TRUE(wake_up_result);
  514. wake_up_result = false;
  515. TEST_ESP_OK(gpio_wakeup_disable(TEST_GPIO_EXT_IN_IO));
  516. gpio_set_level(TEST_GPIO_EXT_OUT_IO, 1);
  517. vTaskDelay(100 / portTICK_RATE_MS);
  518. TEST_ASSERT_FALSE(wake_up_result);
  519. }
  520. #endif // !WAKE_UP_IGNORE
  521. // this case need the resistance to pull up the voltage or pull down the voltage
  522. // ignored because the voltage needs to be tested with multimeter
  523. TEST_CASE("GPIO verify only the gpio with input ability can be set pull/down", "[gpio][ignore]")
  524. {
  525. gpio_config_t output_io = init_io(TEST_GPIO_EXT_OUT_IO);
  526. gpio_config_t input_io = init_io(TEST_GPIO_EXT_IN_IO);
  527. gpio_config(&output_io);
  528. input_io.mode = GPIO_MODE_INPUT;
  529. gpio_config(&input_io);
  530. printf("pull up test!\n");
  531. // pull up test
  532. gpio_set_direction(TEST_GPIO_EXT_OUT_IO, GPIO_MODE_OUTPUT);
  533. TEST_ESP_OK(gpio_set_pull_mode(TEST_GPIO_EXT_OUT_IO, GPIO_PULLUP_ONLY));
  534. prompt_to_continue("mode: GPIO_MODE_OUTPUT");
  535. gpio_set_direction(TEST_GPIO_EXT_OUT_IO, GPIO_MODE_OUTPUT_OD);
  536. TEST_ESP_OK(gpio_set_pull_mode(TEST_GPIO_EXT_OUT_IO, GPIO_PULLUP_ONLY));
  537. // open drain just can output low level
  538. gpio_set_direction(TEST_GPIO_EXT_OUT_IO, GPIO_MODE_INPUT_OUTPUT_OD);
  539. TEST_ESP_OK(gpio_set_pull_mode(TEST_GPIO_EXT_OUT_IO, GPIO_PULLUP_ONLY));
  540. prompt_to_continue("mode: GPIO_MODE_OUTPUT_OD");
  541. gpio_set_direction(TEST_GPIO_EXT_OUT_IO, GPIO_MODE_INPUT_OUTPUT);
  542. TEST_ESP_OK(gpio_set_pull_mode(TEST_GPIO_EXT_OUT_IO, GPIO_PULLUP_ONLY));
  543. prompt_to_continue("mode: GPIO_MODE_INPUT_OUTPUT");
  544. gpio_set_direction(TEST_GPIO_EXT_OUT_IO, GPIO_MODE_INPUT);
  545. TEST_ESP_OK(gpio_set_pull_mode(TEST_GPIO_EXT_OUT_IO, GPIO_PULLUP_ONLY));
  546. prompt_to_continue("mode: GPIO_MODE_INPUT");
  547. // after pull up the level is high now
  548. // pull down test
  549. printf("pull down test!\n");
  550. gpio_set_direction(TEST_GPIO_EXT_OUT_IO, GPIO_MODE_OUTPUT);
  551. TEST_ESP_OK(gpio_set_pull_mode(TEST_GPIO_EXT_OUT_IO, GPIO_PULLDOWN_ONLY));
  552. prompt_to_continue("mode: GPIO_MODE_OUTPUT");
  553. gpio_set_direction(TEST_GPIO_EXT_OUT_IO, GPIO_MODE_OUTPUT_OD);
  554. TEST_ESP_OK(gpio_set_pull_mode(TEST_GPIO_EXT_OUT_IO, GPIO_PULLDOWN_ONLY));
  555. prompt_to_continue("mode: GPIO_MODE_OUTPUT_OD");
  556. gpio_set_direction(TEST_GPIO_EXT_OUT_IO, GPIO_MODE_INPUT_OUTPUT_OD);
  557. TEST_ESP_OK(gpio_set_pull_mode(TEST_GPIO_EXT_OUT_IO, GPIO_PULLDOWN_ONLY));
  558. prompt_to_continue("mode: GPIO_MODE_INPUT_OUTPUT_OD");
  559. gpio_set_direction(TEST_GPIO_EXT_OUT_IO, GPIO_MODE_INPUT_OUTPUT);
  560. TEST_ESP_OK(gpio_set_pull_mode(TEST_GPIO_EXT_OUT_IO, GPIO_PULLDOWN_ONLY));
  561. prompt_to_continue("mode: GPIO_MODE_INPUT_OUTPUT");
  562. gpio_set_direction(TEST_GPIO_EXT_OUT_IO, GPIO_MODE_INPUT);
  563. TEST_ESP_OK(gpio_set_pull_mode(TEST_GPIO_EXT_OUT_IO, GPIO_PULLDOWN_ONLY));
  564. prompt_to_continue("mode: GPIO_MODE_INPUT");
  565. }
  566. /**
  567. * There are 5 situation for the GPIO drive capability:
  568. * 1. GPIO drive weak capability test
  569. * 2. GPIO drive stronger capability test
  570. * 3. GPIO drive default capability test
  571. * 4. GPIO drive default capability test2
  572. * 5. GPIO drive strongest capability test
  573. *
  574. * How to test:
  575. * when testing, use the sliding resistor and a multimeter
  576. * adjust the resistor from low to high, 0-10k
  577. * watch the current change
  578. * the current test result:
  579. * weak capability: (0.32-10.1)mA
  580. * stronger capability: (0.32-20.0)mA
  581. * default capability: (0.33-39.8)mA
  582. * default capability2: (0.33-39.9)mA
  583. * strongest capability: (0.33-64.2)mA
  584. *
  585. * the data shows:
  586. * weak capability<stronger capability<default capability=default capability2<strongest capability
  587. *
  588. * all of these cases should be ignored that it will not run in CI
  589. */
  590. // drive capability test
  591. TEST_CASE("GPIO drive capability test", "[gpio][ignore]")
  592. {
  593. printf("weak capability test! please view the current change!\n");
  594. drive_capability_set_get(TEST_GPIO_EXT_OUT_IO, GPIO_DRIVE_CAP_0);
  595. prompt_to_continue("If this test finishes");
  596. printf("stronger capability test! please view the current change!\n");
  597. drive_capability_set_get(TEST_GPIO_EXT_OUT_IO, GPIO_DRIVE_CAP_1);
  598. prompt_to_continue("If this test finishes");
  599. printf("default capability test! please view the current change!\n");
  600. drive_capability_set_get(TEST_GPIO_EXT_OUT_IO, GPIO_DRIVE_CAP_2);
  601. prompt_to_continue("If this test finishes");
  602. printf("default capability2 test! please view the current change!\n");
  603. drive_capability_set_get(TEST_GPIO_EXT_OUT_IO, GPIO_DRIVE_CAP_DEFAULT);
  604. prompt_to_continue("If this test finishes");
  605. printf("strongest capability test! please view the current change!\n");
  606. drive_capability_set_get(TEST_GPIO_EXT_OUT_IO, GPIO_DRIVE_CAP_3);
  607. prompt_to_continue("If this test finishes");
  608. }
  609. #if !CONFIG_FREERTOS_UNICORE
  610. void gpio_enable_task(void *param)
  611. {
  612. int gpio_num = (int)param;
  613. TEST_ESP_OK(gpio_intr_enable(gpio_num));
  614. vTaskDelete(NULL);
  615. }
  616. /** Test the GPIO Interrupt Enable API with dual core enabled. The GPIO ISR service routine is registered on one core.
  617. * When the GPIO interrupt on another core is enabled, the GPIO interrupt will be lost.
  618. * First on the core 0, Do the following steps:
  619. * 1. Configure the GPIO9 input_output mode, and enable the rising edge interrupt mode.
  620. * 2. Trigger the GPIO9 interrupt and check if the interrupt responds correctly.
  621. * 3. Disable the GPIO9 interrupt
  622. * Then on the core 1, Do the following steps:
  623. * 1. Enable the GPIO9 interrupt again.
  624. * 2. Trigger the GPIO9 interrupt and check if the interrupt responds correctly.
  625. *
  626. */
  627. TEST_CASE("GPIO Enable/Disable interrupt on multiple cores", "[gpio][ignore]")
  628. {
  629. gpio_config_t io_conf;
  630. io_conf.intr_type = GPIO_INTR_NEGEDGE;
  631. io_conf.mode = GPIO_MODE_INPUT_OUTPUT;
  632. io_conf.pin_bit_mask = (1ULL << TEST_IO_9);
  633. io_conf.pull_down_en = 0;
  634. io_conf.pull_up_en = 1;
  635. TEST_ESP_OK(gpio_config(&io_conf));
  636. TEST_ESP_OK(gpio_set_level(TEST_IO_9, 0));
  637. TEST_ESP_OK(gpio_install_isr_service(0));
  638. TEST_ESP_OK(gpio_isr_handler_add(TEST_IO_9, gpio_isr_edge_handler, (void *) TEST_IO_9));
  639. vTaskDelay(1000 / portTICK_RATE_MS);
  640. TEST_ESP_OK(gpio_set_level(TEST_IO_9, 1));
  641. vTaskDelay(100 / portTICK_RATE_MS);
  642. TEST_ESP_OK(gpio_set_level(TEST_IO_9, 0));
  643. vTaskDelay(100 / portTICK_RATE_MS);
  644. TEST_ESP_OK(gpio_intr_disable(TEST_IO_9));
  645. TEST_ASSERT(edge_intr_times == 1);
  646. xTaskCreatePinnedToCore(gpio_enable_task, "gpio_enable_task", 1024 * 4, (void *)TEST_IO_9, 8, NULL, (xPortGetCoreID() == 0));
  647. vTaskDelay(1000 / portTICK_RATE_MS);
  648. TEST_ESP_OK(gpio_set_level(TEST_IO_9, 1));
  649. vTaskDelay(100 / portTICK_RATE_MS);
  650. TEST_ESP_OK(gpio_set_level(TEST_IO_9, 0));
  651. vTaskDelay(100 / portTICK_RATE_MS);
  652. TEST_ESP_OK(gpio_intr_disable(TEST_IO_9));
  653. TEST_ESP_OK(gpio_isr_handler_remove(TEST_IO_9));
  654. gpio_uninstall_isr_service();
  655. TEST_ASSERT(edge_intr_times == 2);
  656. }
  657. #endif //!CONFIG_FREERTOS_UNICORE
  658. typedef struct {
  659. int gpio_num;
  660. int isr_cnt;
  661. } gpio_isr_param_t;
  662. static void gpio_isr_handler(void *arg)
  663. {
  664. gpio_isr_param_t *param = (gpio_isr_param_t *)arg;
  665. esp_rom_printf("GPIO[%d] intr, val: %d\n", param->gpio_num, gpio_get_level(param->gpio_num));
  666. param->isr_cnt++;
  667. }
  668. /** The previous GPIO interrupt service routine polls the interrupt raw status register to find the GPIO that triggered the interrupt.
  669. * But this will incorrectly handle the interrupt disabled GPIOs, because the raw interrupt status register can still be set when
  670. * the trigger signal arrives, even if the interrupt is disabled.
  671. * First on the core 0:
  672. * 1. Configure the GPIO9 and GPIO10(ESP32, ESP32C3)/GPIO21(ESP32-S2) input_output mode.
  673. * 2. Enable GPIO9 dual edge triggered interrupt, enable GPIO10(ESP32, ESP32C3)/GPIO21(ESP32-S2) falling edge triggered interrupt.
  674. * 3. Trigger GPIO9 interrupt, than disable the GPIO18 interrupt, and than trigger GPIO18 again(This time will not respond to the interrupt).
  675. * 4. Trigger GPIO10(ESP32, ESP32C3)/GPIO21(ESP32-S2) interrupt.
  676. * If the bug is not fixed, you will see, in the step 4, the interrupt of GPIO9 will also respond.
  677. */
  678. TEST_CASE("GPIO ISR service test", "[gpio][ignore]")
  679. {
  680. static gpio_isr_param_t io9_param = {
  681. .gpio_num = TEST_IO_9,
  682. .isr_cnt = 0,
  683. };
  684. static gpio_isr_param_t io10_param = {
  685. .gpio_num = TEST_IO_10,
  686. .isr_cnt = 0,
  687. };
  688. gpio_config_t io_conf;
  689. io_conf.intr_type = GPIO_INTR_DISABLE;
  690. io_conf.mode = GPIO_MODE_INPUT_OUTPUT;
  691. io_conf.pin_bit_mask = (1ULL << TEST_IO_9) | (1ULL << TEST_IO_10);
  692. io_conf.pull_down_en = 0;
  693. io_conf.pull_up_en = 1;
  694. TEST_ESP_OK(gpio_config(&io_conf));
  695. TEST_ESP_OK(gpio_set_level(TEST_IO_9, 0));
  696. TEST_ESP_OK(gpio_set_level(TEST_IO_10, 0));
  697. TEST_ESP_OK(gpio_install_isr_service(0));
  698. TEST_ESP_OK(gpio_set_intr_type(TEST_IO_9, GPIO_INTR_ANYEDGE));
  699. TEST_ESP_OK(gpio_set_intr_type(TEST_IO_10, GPIO_INTR_NEGEDGE));
  700. TEST_ESP_OK(gpio_isr_handler_add(TEST_IO_9, gpio_isr_handler, (void *)&io9_param));
  701. TEST_ESP_OK(gpio_isr_handler_add(TEST_IO_10, gpio_isr_handler, (void *)&io10_param));
  702. printf("Triggering the interrupt of GPIO9\n");
  703. vTaskDelay(1000 / portTICK_RATE_MS);
  704. //Rising edge
  705. TEST_ESP_OK(gpio_set_level(TEST_IO_9, 1));
  706. printf("Disable the interrupt of GPIO9\n");
  707. vTaskDelay(100 / portTICK_RATE_MS);
  708. //Disable GPIO9 interrupt, GPIO18 will not respond to the next falling edge interrupt.
  709. TEST_ESP_OK(gpio_intr_disable(TEST_IO_9));
  710. vTaskDelay(100 / portTICK_RATE_MS);
  711. //Falling edge
  712. TEST_ESP_OK(gpio_set_level(TEST_IO_9, 0));
  713. printf("Triggering the interrupt of GPIO10\n");
  714. vTaskDelay(100 / portTICK_RATE_MS);
  715. TEST_ESP_OK(gpio_set_level(TEST_IO_10, 1));
  716. vTaskDelay(100 / portTICK_RATE_MS);
  717. //Falling edge
  718. TEST_ESP_OK(gpio_set_level(TEST_IO_10, 0));
  719. vTaskDelay(100 / portTICK_RATE_MS);
  720. TEST_ESP_OK(gpio_isr_handler_remove(TEST_IO_9));
  721. TEST_ESP_OK(gpio_isr_handler_remove(TEST_IO_10));
  722. gpio_uninstall_isr_service();
  723. TEST_ASSERT((io9_param.isr_cnt == 1) && (io10_param.isr_cnt == 1));
  724. }
  725. #if CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32C3
  726. TEST_CASE("GPIO input and output of USB pins test", "[gpio]")
  727. {
  728. const int test_pins[] = {TEST_GPIO_USB_DP_IO, TEST_GPIO_USB_DM_IO};
  729. gpio_config_t io_conf = {
  730. .intr_type = GPIO_INTR_DISABLE,
  731. .mode = GPIO_MODE_INPUT_OUTPUT,
  732. .pin_bit_mask = (BIT64(test_pins[0]) | BIT64(test_pins[1])),
  733. .pull_down_en = 0,
  734. .pull_up_en = 0,
  735. };
  736. gpio_config(&io_conf);
  737. for (int i = 0; i < sizeof(test_pins) / sizeof(int); i++) {
  738. int pin = test_pins[i];
  739. // test pin
  740. gpio_set_level(pin, 0);
  741. // tested voltage is around 0v
  742. esp_rom_delay_us(10);
  743. TEST_ASSERT_EQUAL_INT_MESSAGE(gpio_get_level(pin), 0, "get level error! the level should be low!");
  744. vTaskDelay(1000 / portTICK_RATE_MS);
  745. gpio_set_level(pin, 1);
  746. esp_rom_delay_us(10);
  747. // tested voltage is around 3.3v
  748. TEST_ASSERT_EQUAL_INT_MESSAGE(gpio_get_level(pin), 1, "get level error! the level should be high!");
  749. vTaskDelay(1000 / portTICK_RATE_MS);
  750. gpio_set_level(pin, 0);
  751. esp_rom_delay_us(10);
  752. // tested voltage is around 0v
  753. TEST_ASSERT_EQUAL_INT_MESSAGE(gpio_get_level(pin), 0, "get level error! the level should be low!");
  754. vTaskDelay(1000 / portTICK_RATE_MS);
  755. gpio_set_level(pin, 1);
  756. esp_rom_delay_us(10);
  757. // tested voltage is around 3.3v
  758. TEST_ASSERT_EQUAL_INT_MESSAGE(gpio_get_level(pin), 1, "get level error! the level should be high!");
  759. }
  760. }
  761. #endif //CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32C3