Interrupts code is not working correctly.

greenleaf
Posts: 4
Joined: Tue Jun 11, 2024 4:52 pm

Interrupts code is not working correctly.

Postby greenleaf » Tue Jun 11, 2024 7:21 pm

I want to be able to read the signal from an lm393 sensor using the Espressif framework on an esp32.

I have the following code:

Code: Select all

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <inttypes.h>

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"

#include "esp_log.h"
#include "driver/pulse_cnt.h"
#include "driver/gpio.h"
#include "esp_sleep.h"

#include "config.h"
#include "speed_calculations.h"

#define ESP_INTR_FLAG_DEFAULT 0

static QueueHandle_t gpio_evt_queue = NULL;

static void IRAM_ATTR gpio_isr_handler(void* arg)
{
    uint32_t gpio_num = (uint32_t) arg;
    xQueueSendFromISR(gpio_evt_queue, &gpio_num, NULL);
}

static void gpio_task_handler(void* arg)
{
    uint32_t io_num;
    for (;;) {
        if (xQueueReceive(gpio_evt_queue, &io_num, portMAX_DELAY)) {
            printf("GPIO[%"PRIu32"] intr, val: %d\n", io_num, gpio_get_level(io_num));
        }
    }
}

void app_main()
{
    gpio_config_t io_conf = {};
    io_conf.intr_type = GPIO_INTR_POSEDGE;
    io_conf.mode = GPIO_MODE_INPUT;
    io_conf.pin_bit_mask = CONFIG_GPIO_FRONT_WHEEL | CONFIG_GPIO_REAR_WHEEL;
    io_conf.pull_up_en = 1;
    gpio_config(&io_conf);

    gpio_set_intr_type(CONFIG_GPIO_FRONT_WHEEL | CONFIG_GPIO_REAR_WHEEL, GPIO_INTR_ANYEDGE);

    gpio_evt_queue = xQueueCreate(10, sizeof(uint32_t));
    xTaskCreate(gpio_task_handler, "gpio_task_handler", 2048, NULL, 10, NULL);

    gpio_install_isr_service(ESP_INTR_FLAG_DEFAULT);
    gpio_isr_handler_add(CONFIG_GPIO_FRONT_WHEEL, gpio_isr_handler, (void*) CONFIG_GPIO_FRONT_WHEEL);
    gpio_isr_handler_add(CONFIG_GPIO_REAR_WHEEL, gpio_isr_handler, (void*) CONFIG_GPIO_FRONT_WHEEL);

    printf("Minimum free heap size: %"PRIu32" bytes\n", esp_get_minimum_free_heap_size());
    
    while (1) {
        printf("cnt: %d\n", 0);
        vTaskDelay(1000 / portTICK_PERIOD_MS);
    }
}
When uploading and setting pins 18 and 19 that are set up as the input pins nothing happens though. Additionally, the cnt message in the while loop is not printing.

How can I fix it?

ESP_Sprite
Posts: 9577
Joined: Thu Nov 26, 2015 4:08 am

Re: Interrupts code is not working correctly.

Postby ESP_Sprite » Wed Jun 12, 2024 12:56 am

Can you try to move the gpio_set_intr_type() call to after you set the interrupt handlers?

greenleaf
Posts: 4
Joined: Tue Jun 11, 2024 4:52 pm

Re: Interrupts code is not working correctly.

Postby greenleaf » Sun Jun 16, 2024 1:12 am

That seems to work better. Not sure I understand why!

ESP_Sprite
Posts: 9577
Joined: Thu Nov 26, 2015 4:08 am

Re: Interrupts code is not working correctly.

Postby ESP_Sprite » Tue Jun 18, 2024 8:58 am

greenleaf wrote:
Sun Jun 16, 2024 1:12 am
That seems to work better. Not sure I understand why!
I don't entirely remember, but I think not having an interrupt handler installed for the GPIO interrupt means that if at that time an interrupt is generated, it never gets acknowledged and the ESP simply keeps running the interrupt routine over and over again.

User avatar
ok-home
Posts: 68
Joined: Sun May 02, 2021 7:23 pm
Location: Russia Novosibirsk
Contact:

Re: Interrupts code is not working correctly.

Postby ok-home » Tue Jun 18, 2024 12:10 pm

hi
I see some errors in the code here

Code: Select all

io_conf.pin_bit_mask = CONFIG_GPIO_FRONT_WHEEL | CONFIG_GPIO_REAR_WHEEL;
- bitmask ?

Code: Select all

gpio_set_intr_type(CONFIG_GPIO_FRONT_WHEEL | CONFIG_GPIO_REAR_WHEEL, GPIO_INTR_ANYEDGE);
- gpionum ???

make a separate initialization for each gpio and define where bitmask and gpionum are.

Who is online

Users browsing this forum: Bing [Bot] and 328 guests