gpio_example_main.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 "freertos/FreeRTOS.h"
  11. #include "freertos/task.h"
  12. #include "freertos/queue.h"
  13. #include "driver/gpio.h"
  14. /**
  15. * Brief:
  16. * This test code shows how to configure gpio and how to use gpio interrupt.
  17. *
  18. * GPIO status:
  19. * GPIO18: output
  20. * GPIO19: output
  21. * GPIO4: input, pulled up, interrupt from rising edge and falling edge
  22. * GPIO5: input, pulled up, interrupt from rising edge.
  23. *
  24. * Test:
  25. * Connect GPIO18 with GPIO4
  26. * Connect GPIO19 with GPIO5
  27. * Generate pulses on GPIO18/19, that triggers interrupt on GPIO4/5
  28. *
  29. */
  30. #define GPIO_OUTPUT_IO_0 18
  31. #define GPIO_OUTPUT_IO_1 19
  32. #define GPIO_OUTPUT_PIN_SEL ((1ULL<<GPIO_OUTPUT_IO_0) | (1ULL<<GPIO_OUTPUT_IO_1))
  33. #define GPIO_INPUT_IO_0 4
  34. #define GPIO_INPUT_IO_1 5
  35. #define GPIO_INPUT_PIN_SEL ((1ULL<<GPIO_INPUT_IO_0) | (1ULL<<GPIO_INPUT_IO_1))
  36. #define ESP_INTR_FLAG_DEFAULT 0
  37. static xQueueHandle gpio_evt_queue = NULL;
  38. static void IRAM_ATTR gpio_isr_handler(void* arg)
  39. {
  40. uint32_t gpio_num = (uint32_t) arg;
  41. xQueueSendFromISR(gpio_evt_queue, &gpio_num, NULL);
  42. }
  43. static void gpio_task_example(void* arg)
  44. {
  45. uint32_t io_num;
  46. for(;;) {
  47. if(xQueueReceive(gpio_evt_queue, &io_num, portMAX_DELAY)) {
  48. printf("GPIO[%d] intr, val: %d\n", io_num, gpio_get_level(io_num));
  49. }
  50. }
  51. }
  52. void app_main(void)
  53. {
  54. //zero-initialize the config structure.
  55. gpio_config_t io_conf = {};
  56. //disable interrupt
  57. io_conf.intr_type = GPIO_INTR_DISABLE;
  58. //set as output mode
  59. io_conf.mode = GPIO_MODE_OUTPUT;
  60. //bit mask of the pins that you want to set,e.g.GPIO18/19
  61. io_conf.pin_bit_mask = GPIO_OUTPUT_PIN_SEL;
  62. //disable pull-down mode
  63. io_conf.pull_down_en = 0;
  64. //disable pull-up mode
  65. io_conf.pull_up_en = 0;
  66. //configure GPIO with the given settings
  67. gpio_config(&io_conf);
  68. //interrupt of rising edge
  69. io_conf.intr_type = GPIO_INTR_POSEDGE;
  70. //bit mask of the pins, use GPIO4/5 here
  71. io_conf.pin_bit_mask = GPIO_INPUT_PIN_SEL;
  72. //set as input mode
  73. io_conf.mode = GPIO_MODE_INPUT;
  74. //enable pull-up mode
  75. io_conf.pull_up_en = 1;
  76. gpio_config(&io_conf);
  77. //change gpio intrrupt type for one pin
  78. gpio_set_intr_type(GPIO_INPUT_IO_0, GPIO_INTR_ANYEDGE);
  79. //create a queue to handle gpio event from isr
  80. gpio_evt_queue = xQueueCreate(10, sizeof(uint32_t));
  81. //start gpio task
  82. xTaskCreate(gpio_task_example, "gpio_task_example", 2048, NULL, 10, NULL);
  83. //install gpio isr service
  84. gpio_install_isr_service(ESP_INTR_FLAG_DEFAULT);
  85. //hook isr handler for specific gpio pin
  86. gpio_isr_handler_add(GPIO_INPUT_IO_0, gpio_isr_handler, (void*) GPIO_INPUT_IO_0);
  87. //hook isr handler for specific gpio pin
  88. gpio_isr_handler_add(GPIO_INPUT_IO_1, gpio_isr_handler, (void*) GPIO_INPUT_IO_1);
  89. //remove isr handler for gpio number.
  90. gpio_isr_handler_remove(GPIO_INPUT_IO_0);
  91. //hook isr handler for specific gpio pin again
  92. gpio_isr_handler_add(GPIO_INPUT_IO_0, gpio_isr_handler, (void*) GPIO_INPUT_IO_0);
  93. printf("Minimum free heap size: %d bytes\n", esp_get_minimum_free_heap_size());
  94. int cnt = 0;
  95. while(1) {
  96. printf("cnt: %d\n", cnt++);
  97. vTaskDelay(1000 / portTICK_RATE_MS);
  98. gpio_set_level(GPIO_OUTPUT_IO_0, cnt % 2);
  99. gpio_set_level(GPIO_OUTPUT_IO_1, cnt % 2);
  100. }
  101. }