gpio_example_main.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* GPIO Example
  2. This example code is in the Public Domain (or CC0 licensed, at your option.)
  3. Unless required by applicable law or agreed to in writing, this
  4. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied.
  6. */
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <stdlib.h>
  10. #include <inttypes.h>
  11. #include "freertos/FreeRTOS.h"
  12. #include "freertos/task.h"
  13. #include "freertos/queue.h"
  14. #include "driver/gpio.h"
  15. /**
  16. * Brief:
  17. * This test code shows how to configure gpio and how to use gpio interrupt.
  18. *
  19. * GPIO status:
  20. * GPIO18: output (ESP32C2/ESP32H2 uses GPIO8 as the second output pin)
  21. * GPIO19: output (ESP32C2/ESP32H2 uses GPIO9 as the second output pin)
  22. * GPIO4: input, pulled up, interrupt from rising edge and falling edge
  23. * GPIO5: input, pulled up, interrupt from rising edge.
  24. *
  25. * Note. These are the default GPIO pins to be used in the example. You can
  26. * change IO pins in menuconfig.
  27. *
  28. * Test:
  29. * Connect GPIO18(8) with GPIO4
  30. * Connect GPIO19(9) with GPIO5
  31. * Generate pulses on GPIO18(8)/19(9), that triggers interrupt on GPIO4/5
  32. *
  33. */
  34. #define GPIO_OUTPUT_IO_0 CONFIG_GPIO_OUTPUT_0
  35. #define GPIO_OUTPUT_IO_1 CONFIG_GPIO_OUTPUT_1
  36. #define GPIO_OUTPUT_PIN_SEL ((1ULL<<GPIO_OUTPUT_IO_0) | (1ULL<<GPIO_OUTPUT_IO_1))
  37. #define GPIO_INPUT_IO_0 CONFIG_GPIO_INPUT_0
  38. #define GPIO_INPUT_IO_1 CONFIG_GPIO_INPUT_1
  39. #define GPIO_INPUT_PIN_SEL ((1ULL<<GPIO_INPUT_IO_0) | (1ULL<<GPIO_INPUT_IO_1))
  40. #define ESP_INTR_FLAG_DEFAULT 0
  41. static QueueHandle_t gpio_evt_queue = NULL;
  42. static void IRAM_ATTR gpio_isr_handler(void* arg)
  43. {
  44. uint32_t gpio_num = (uint32_t) arg;
  45. xQueueSendFromISR(gpio_evt_queue, &gpio_num, NULL);
  46. }
  47. static void gpio_task_example(void* arg)
  48. {
  49. uint32_t io_num;
  50. for(;;) {
  51. if(xQueueReceive(gpio_evt_queue, &io_num, portMAX_DELAY)) {
  52. printf("GPIO[%"PRIu32"] intr, val: %d\n", io_num, gpio_get_level(io_num));
  53. }
  54. }
  55. }
  56. void app_main(void)
  57. {
  58. //zero-initialize the config structure.
  59. gpio_config_t io_conf = {};
  60. //disable interrupt
  61. io_conf.intr_type = GPIO_INTR_DISABLE;
  62. //set as output mode
  63. io_conf.mode = GPIO_MODE_OUTPUT;
  64. //bit mask of the pins that you want to set,e.g.GPIO18/19
  65. io_conf.pin_bit_mask = GPIO_OUTPUT_PIN_SEL;
  66. //disable pull-down mode
  67. io_conf.pull_down_en = 0;
  68. //disable pull-up mode
  69. io_conf.pull_up_en = 0;
  70. //configure GPIO with the given settings
  71. gpio_config(&io_conf);
  72. //interrupt of rising edge
  73. io_conf.intr_type = GPIO_INTR_POSEDGE;
  74. //bit mask of the pins, use GPIO4/5 here
  75. io_conf.pin_bit_mask = GPIO_INPUT_PIN_SEL;
  76. //set as input mode
  77. io_conf.mode = GPIO_MODE_INPUT;
  78. //enable pull-up mode
  79. io_conf.pull_up_en = 1;
  80. gpio_config(&io_conf);
  81. //change gpio interrupt type for one pin
  82. gpio_set_intr_type(GPIO_INPUT_IO_0, GPIO_INTR_ANYEDGE);
  83. //create a queue to handle gpio event from isr
  84. gpio_evt_queue = xQueueCreate(10, sizeof(uint32_t));
  85. //start gpio task
  86. xTaskCreate(gpio_task_example, "gpio_task_example", 2048, NULL, 10, NULL);
  87. //install gpio isr service
  88. gpio_install_isr_service(ESP_INTR_FLAG_DEFAULT);
  89. //hook isr handler for specific gpio pin
  90. gpio_isr_handler_add(GPIO_INPUT_IO_0, gpio_isr_handler, (void*) GPIO_INPUT_IO_0);
  91. //hook isr handler for specific gpio pin
  92. gpio_isr_handler_add(GPIO_INPUT_IO_1, gpio_isr_handler, (void*) GPIO_INPUT_IO_1);
  93. //remove isr handler for gpio number.
  94. gpio_isr_handler_remove(GPIO_INPUT_IO_0);
  95. //hook isr handler for specific gpio pin again
  96. gpio_isr_handler_add(GPIO_INPUT_IO_0, gpio_isr_handler, (void*) GPIO_INPUT_IO_0);
  97. printf("Minimum free heap size: %"PRIu32" bytes\n", esp_get_minimum_free_heap_size());
  98. int cnt = 0;
  99. while(1) {
  100. printf("cnt: %d\n", cnt++);
  101. vTaskDelay(1000 / portTICK_PERIOD_MS);
  102. gpio_set_level(GPIO_OUTPUT_IO_0, cnt % 2);
  103. gpio_set_level(GPIO_OUTPUT_IO_1, cnt % 2);
  104. }
  105. }