Simple Interrupt Handler
Posted: Wed Dec 21, 2016 6:43 am
Welcome to the brave new world of ESP32 development.
There is very little native (non arduino) code available for the ESP32. I have been having a lot of trouble setting up an interrupt to handle buttons and sensors. In order to interact with buttons and sensors correctly, you need to take action when they are triggered. Don't fall victim to checking pin status on a loop. Checking status at a given time instead of based on events means you might miss something.
First you need to declare a function to handle the interrupt:
Next you have to set up a pin that is interrupt friendly:
The final step is registering the events with the function from step one:
Note1: Both esp_intr_alloc and gpio_isr_register work, but I am unsure which is the right one to use.
Note2: When you see the parameter 0 in my example, it is the default setting. I would rather use ESP_INTR_FLAG_EDGE or ESP_INTR_FLAG_HIGH. I was never able to get the EDGE or HIGH flags working.
The full main.c is below.
Happy coding.
There is very little native (non arduino) code available for the ESP32. I have been having a lot of trouble setting up an interrupt to handle buttons and sensors. In order to interact with buttons and sensors correctly, you need to take action when they are triggered. Don't fall victim to checking pin status on a loop. Checking status at a given time instead of based on events means you might miss something.
First you need to declare a function to handle the interrupt:
Code: Select all
void gpioHandler(void* arg)
Code: Select all
gpio_pad_select_gpio(GPIO_INTR);
gpio_set_direction(GPIO_INTR, GPIO_MODE_INPUT);
gpio_set_pull_mode(GPIO_INTR, GPIO_PULLUP_ONLY);
gpio_set_intr_type(GPIO_INTR, GPIO_INTR_NEGEDGE);
gpio_intr_enable(GPIO_INTR);
Code: Select all
esp_intr_alloc( ETS_GPIO_INTR_SOURCE, 0, gpioHandler, "Test", NULL);
//gpio_isr_register(gpioHandler, "Test", 0, NULL);
Note2: When you see the parameter 0 in my example, it is the default setting. I would rather use ESP_INTR_FLAG_EDGE or ESP_INTR_FLAG_HIGH. I was never able to get the EDGE or HIGH flags working.
The full main.c is below.
Happy coding.
Code: Select all
#include "freertos/FreeRTOS.h"
#include "esp_wifi.h"
#include "esp_system.h"
#include "esp_event.h"
#include "esp_event_loop.h"
#include "nvs_flash.h"
#include "driver/gpio.h"
#define GPIO_LED GPIO_NUM_27
#define GPIO_INTR GPIO_NUM_17
void gpioHandler(void* arg)
{
printf("Interrupt\n");
gpio_set_level(GPIO_LED, 1);
}
void app_main(void)
{
nvs_flash_init();
tcpip_adapter_init();
gpio_pad_select_gpio(GPIO_LED);
gpio_set_direction(GPIO_LED, GPIO_MODE_OUTPUT);
gpio_set_level(GPIO_LED, 0);
gpio_pad_select_gpio(GPIO_INTR);
gpio_set_direction(GPIO_INTR, GPIO_MODE_INPUT);
gpio_set_pull_mode(GPIO_INTR, GPIO_PULLUP_ONLY);
gpio_set_intr_type(GPIO_INTR, GPIO_INTR_NEGEDGE);
gpio_intr_enable(GPIO_INTR);
esp_intr_alloc( ETS_GPIO_INTR_SOURCE, 0, gpioHandler, "Test", NULL);
//gpio_isr_register(gpioHandler, "Test", 0, NULL);
}