gpio_example_main.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. gpio_config_t io_conf;
  55. //disable interrupt
  56. io_conf.intr_type = GPIO_PIN_INTR_DISABLE;
  57. //set as output mode
  58. io_conf.mode = GPIO_MODE_OUTPUT;
  59. //bit mask of the pins that you want to set,e.g.GPIO18/19
  60. io_conf.pin_bit_mask = GPIO_OUTPUT_PIN_SEL;
  61. //disable pull-down mode
  62. io_conf.pull_down_en = 0;
  63. //disable pull-up mode
  64. io_conf.pull_up_en = 0;
  65. //configure GPIO with the given settings
  66. gpio_config(&io_conf);
  67. //interrupt of rising edge
  68. io_conf.intr_type = GPIO_PIN_INTR_POSEDGE;
  69. //bit mask of the pins, use GPIO4/5 here
  70. io_conf.pin_bit_mask = GPIO_INPUT_PIN_SEL;
  71. //set as input mode
  72. io_conf.mode = GPIO_MODE_INPUT;
  73. //enable pull-up mode
  74. io_conf.pull_up_en = 1;
  75. gpio_config(&io_conf);
  76. //change gpio intrrupt type for one pin
  77. gpio_set_intr_type(GPIO_INPUT_IO_0, GPIO_INTR_ANYEDGE);
  78. //create a queue to handle gpio event from isr
  79. gpio_evt_queue = xQueueCreate(10, sizeof(uint32_t));
  80. //start gpio task
  81. xTaskCreate(gpio_task_example, "gpio_task_example", 2048, NULL, 10, NULL);
  82. //install gpio isr service
  83. gpio_install_isr_service(ESP_INTR_FLAG_DEFAULT);
  84. //hook isr handler for specific gpio pin
  85. gpio_isr_handler_add(GPIO_INPUT_IO_0, gpio_isr_handler, (void*) GPIO_INPUT_IO_0);
  86. //hook isr handler for specific gpio pin
  87. gpio_isr_handler_add(GPIO_INPUT_IO_1, gpio_isr_handler, (void*) GPIO_INPUT_IO_1);
  88. //remove isr handler for gpio number.
  89. gpio_isr_handler_remove(GPIO_INPUT_IO_0);
  90. //hook isr handler for specific gpio pin again
  91. gpio_isr_handler_add(GPIO_INPUT_IO_0, gpio_isr_handler, (void*) GPIO_INPUT_IO_0);
  92. int cnt = 0;
  93. while(1) {
  94. printf("cnt: %d\n", cnt++);
  95. vTaskDelay(1000 / portTICK_RATE_MS);
  96. gpio_set_level(GPIO_OUTPUT_IO_0, cnt % 2);
  97. gpio_set_level(GPIO_OUTPUT_IO_1, cnt % 2);
  98. }
  99. }