Is anybody familiar with using interrupts with Pi Pico (with freeRTOS and multiple buttons)? I am trying to make a small project using the Pi Pico and freeRTOS but I am encountering a problem when I am trying to get more than one button to work with interrupts. Basically from time to time my program will register another button as being pushed instead of the one that I am actually pushing. I am using this code to create the interrupts for the 5 buttons on pins from 9 to 13:
gpio_set_dir(BUTTON_UP_PIN, GPIO_IN); gpio_pull_up(BUTTON_UP_PIN); gpio_set_irq_enabled_with_callback(BUTTON_UP_PIN, GPIO_IRQ_EDGE_RISE, 1, button_ISR); //up
gpio_set_dir(BUTTON_LEFT_PIN, GPIO_IN); gpio_pull_up(BUTTON_LEFT_PIN); gpio_set_irq_enabled(BUTTON_LEFT_PIN, GPIO_IRQ_EDGE_RISE, 1); //left
gpio_set_dir(BUTTON_RIGHT_PIN, GPIO_IN); gpio_pull_up(BUTTON_RIGHT_PIN); gpio_set_irq_enabled(BUTTON_RIGHT_PIN, GPIO_IRQ_EDGE_RISE, 1); //right
gpio_set_dir(BUTTON_MID_PIN, GPIO_IN); gpio_pull_up(BUTTON_MID_PIN); gpio_set_irq_enabled(BUTTON_MID_PIN, GPIO_IRQ_EDGE_RISE, 1); //middle
gpio_set_dir(BUTTON_DOWN_PIN, GPIO_IN); gpio_pull_up(BUTTON_DOWN_PIN); gpio_set_irq_enabled(BUTTON_DOWN_PIN, GPIO_IRQ_EDGE_RISE, 1); //down
After that I created a call back function
void button_ISR(uint gpio, uint32_t events) {
xQueueSendToBackFromISR(irq_queue, &gpio, 0);
}
and because I am using freeRTOS i am sending the push of the button to the task I need it in, by using a queue. Here is the furthest back where I tracked the problem. I used a printf() to print the values of gpio and they where wrong here, in the callback, too. (The problem might be from somewhere else but I think it is an interrupt issue and not a freeRTOS issue because the problem appears before I read the queue in the task. But it is just my opinion and a more expert opinion is welcome). As I said, for the most part this works fine. It is just that sometimes, randomly, another button will be registered as getting pushed out of the 5, instead of the one I am actually pushing. Usually what happens is that if I press the button with gpio port 13, once in a while the gpio number 12 will register. This is was happening very often, as in, if I presses the the button with gpio number 13, the only wrong value I would get would be 12 and none other. It is like it was taking a smaller value decreased by one??
I am wondering why might this problem appear, am how to fix it. Is because of the way I am initializing the interrupts? Is there a more safe, proper way of initializing interrupts for multiple buttons? I have seen examples online where people are using the gpio_set_irq_enabled_with_callback() function for ONE button, but I dont know if it is ok to use it with more than one. I used the gpio_set_irq_enabled because i found it in the SDK datasheet and it seemed to be good for what I need judging by its description. But I am not sure if it is the best used here.
Could my problem be because of something else? Maybe the way I set up the CMake files? Maybe something else? It is not a hardware problem as I tried using the polling method to test the buttons and they worked fine with that.
If you have any idea what might cause this issue, please feel free to leave an answer. This has been bugging me for days! Thank you!