Interrupt Service Routine on esp32

MohamedTrabelsi
Posts: 4
Joined: Fri Nov 03, 2023 4:07 pm

Interrupt Service Routine on esp32

Postby MohamedTrabelsi » Sat Jan 06, 2024 8:54 am

Hello guys I'm working on a project that activate wifi interface when a button is pressed so I created a project that print a message when the event is triggered the code works fine but when I integrated the same code in the main project the interruption doesn't work
  1. #include <stdio.h>
  2. #include "freertos/FreeRTOS.h"
  3. #include "freertos/task.h"
  4. #include "freertos/semphr.h"
  5. #include "driver/gpio.h"
  6. #define BUTTON_PIN GPIO_NUM_2
  7.  
  8. // Semaphore to synchronize tasks
  9. SemaphoreHandle_t xSemaphore = NULL;
  10.  
  11. // Interrupt service routine for the button press
  12. static void IRAM_ATTR button_isr_handler(void* arg) {
  13.     // Notify the waiting task that the button was pressed
  14.     xSemaphoreGiveFromISR(xSemaphore, NULL);
  15. }
  16.  
  17. // Task that will be notified when the button is pressed
  18. void button_task(void *arg) {
  19.     while (1) {
  20.         // Wait for the semaphore to be given by the ISR
  21.         if (xSemaphoreTake(xSemaphore, portMAX_DELAY)) {
  22.             esp_wifi_start();
  23.             printf("Button Pressed!\n");
  24.         }
  25.     }
  26. }
  27. void vTaskISR(void *pvParameters)
  28. {  
  29.     while(1)
  30.     {
  31.      xSemaphore = xSemaphoreCreateBinary();
  32.  
  33.     // Configure the GPIO pin for the button
  34.     gpio_config_t io_conf = {
  35.         .pin_bit_mask = (1ULL << BUTTON_PIN),
  36.         .mode = GPIO_MODE_INPUT,
  37.         .intr_type = GPIO_INTR_POSEDGE,  
  38.         .pull_up_en = GPIO_PULLUP_ENABLE,
  39.     };
  40.     gpio_config(&io_conf);
  41.  
  42.     // Install ISR service with default configuration
  43.     gpio_install_isr_service(0);
  44.  
  45.     // Hook ISR handler to the button pin
  46.     gpio_isr_handler_add(BUTTON_PIN, button_isr_handler, (void*) BUTTON_PIN);
  47.     vTaskDelete(NULL);
  48.     }
  49. }
  50.  
  51. void app_main() {
  52.     xTaskCreate(&vTaskISR, "ISR", 4096, NULL, 5, NULL);
  53. }

MicroController
Posts: 1705
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Interrupt Service Routine on esp32

Postby MicroController » Sat Jan 06, 2024 11:43 am

Code: Select all

// Task that will be notified when the button is pressed
void button_task(void *arg) {
...
You should actually xTaskCreate a task if you want it to do something ;-)

Who is online

Users browsing this forum: Michaelboeding and 106 guests