gpio_example_main.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. /*
  38. * Let's say, GPIO_OUTPUT_IO_0=18, GPIO_OUTPUT_IO_1=19
  39. * In binary representation,
  40. * 1ULL<<GPIO_OUTPUT_IO_0 is equal to 0000000000000000000001000000000000000000 and
  41. * 1ULL<<GPIO_OUTPUT_IO_1 is equal to 0000000000000000000010000000000000000000
  42. * GPIO_OUTPUT_PIN_SEL 0000000000000000000011000000000000000000
  43. * */
  44. #define GPIO_INPUT_IO_0 CONFIG_GPIO_INPUT_0
  45. #define GPIO_INPUT_IO_1 CONFIG_GPIO_INPUT_1
  46. #define GPIO_INPUT_PIN_SEL ((1ULL<<GPIO_INPUT_IO_0) | (1ULL<<GPIO_INPUT_IO_1))
  47. /*
  48. * Let's say, GPIO_INPUT_IO_0=4, GPIO_INPUT_IO_1=5
  49. * In binary representation,
  50. * 1ULL<<GPIO_INPUT_IO_0 is equal to 0000000000000000000000000000000000010000 and
  51. * 1ULL<<GPIO_INPUT_IO_1 is equal to 0000000000000000000000000000000000100000
  52. * GPIO_INPUT_PIN_SEL 0000000000000000000000000000000000110000
  53. * */
  54. #define ESP_INTR_FLAG_DEFAULT 0
  55. static QueueHandle_t gpio_evt_queue = NULL;
  56. static void IRAM_ATTR gpio_isr_handler(void* arg)
  57. {
  58. uint32_t gpio_num = (uint32_t) arg;
  59. xQueueSendFromISR(gpio_evt_queue, &gpio_num, NULL);
  60. }
  61. static void gpio_task_example(void* arg)
  62. {
  63. uint32_t io_num;
  64. for(;;) {
  65. if(xQueueReceive(gpio_evt_queue, &io_num, portMAX_DELAY)) {
  66. printf("GPIO[%"PRIu32"] intr, val: %d\n", io_num, gpio_get_level(io_num));
  67. }
  68. }
  69. }
  70. void app_main(void)
  71. {
  72. //zero-initialize the config structure.
  73. gpio_config_t io_conf = {};
  74. //disable interrupt
  75. io_conf.intr_type = GPIO_INTR_DISABLE;
  76. //set as output mode
  77. io_conf.mode = GPIO_MODE_OUTPUT;
  78. //bit mask of the pins that you want to set,e.g.GPIO18/19
  79. io_conf.pin_bit_mask = GPIO_OUTPUT_PIN_SEL;
  80. //disable pull-down mode
  81. io_conf.pull_down_en = 0;
  82. //disable pull-up mode
  83. io_conf.pull_up_en = 0;
  84. //configure GPIO with the given settings
  85. gpio_config(&io_conf);
  86. //interrupt of rising edge
  87. io_conf.intr_type = GPIO_INTR_POSEDGE;
  88. //bit mask of the pins, use GPIO4/5 here
  89. io_conf.pin_bit_mask = GPIO_INPUT_PIN_SEL;
  90. //set as input mode
  91. io_conf.mode = GPIO_MODE_INPUT;
  92. //enable pull-up mode
  93. io_conf.pull_up_en = 1;
  94. gpio_config(&io_conf);
  95. //change gpio interrupt type for one pin
  96. gpio_set_intr_type(GPIO_INPUT_IO_0, GPIO_INTR_ANYEDGE);
  97. //create a queue to handle gpio event from isr
  98. gpio_evt_queue = xQueueCreate(10, sizeof(uint32_t));
  99. //start gpio task
  100. xTaskCreate(gpio_task_example, "gpio_task_example", 2048, NULL, 10, NULL);
  101. //install gpio isr service
  102. gpio_install_isr_service(ESP_INTR_FLAG_DEFAULT);
  103. //hook isr handler for specific gpio pin
  104. gpio_isr_handler_add(GPIO_INPUT_IO_0, gpio_isr_handler, (void*) GPIO_INPUT_IO_0);
  105. //hook isr handler for specific gpio pin
  106. gpio_isr_handler_add(GPIO_INPUT_IO_1, gpio_isr_handler, (void*) GPIO_INPUT_IO_1);
  107. //remove isr handler for gpio number.
  108. gpio_isr_handler_remove(GPIO_INPUT_IO_0);
  109. //hook isr handler for specific gpio pin again
  110. gpio_isr_handler_add(GPIO_INPUT_IO_0, gpio_isr_handler, (void*) GPIO_INPUT_IO_0);
  111. printf("Minimum free heap size: %"PRIu32" bytes\n", esp_get_minimum_free_heap_size());
  112. int cnt = 0;
  113. while(1) {
  114. printf("cnt: %d\n", cnt++);
  115. vTaskDelay(1000 / portTICK_PERIOD_MS);
  116. gpio_set_level(GPIO_OUTPUT_IO_0, cnt % 2);
  117. gpio_set_level(GPIO_OUTPUT_IO_1, cnt % 2);
  118. }
  119. }