test_gpio.c 37 KB

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