I've been carrying out some development based on the hello_gpio_irq.c example for the Pico, as the SPI_Slave example rests in a blocking function - which means that the program cannot be adapted to any real task due to the lack of a call back function.
During work on the above, I've been trying to get the pico to flash the user LED when the IRQ callback happens. Every attempt resulted in the program hanging. I finally commented out all of the sleep_ms() commands, and it runs - But you can't see the LED flash.
My conclusion is that one cannot use both the sleep_ms() command and the IRQ in the same program.
During work on the above, I've been trying to get the pico to flash the user LED when the IRQ callback happens. Every attempt resulted in the program hanging. I finally commented out all of the sleep_ms() commands, and it runs - But you can't see the LED flash.
My conclusion is that one cannot use both the sleep_ms() command and the IRQ in the same program.
Code:
/** * Copyright (c) 2020 Raspberry Pi (Trading) Ltd. * * SPDX-License-Identifier: BSD-3-Clause */#include <stdio.h>#include "pico/stdlib.h"#include "hardware/gpio.h"#include <hardware/irq.h>//#include <pico_time/time.c>#define GPIO_WATCH_PIN 13#ifndef PICO_DEFAULT_LED_PIN#define PICO_DEFAULT_LED_PIN 25#endifstatic char event_str[128];void gpio_event_string(char *buf, uint32_t events);void gpio_callback(uint gpio, uint32_t events) { // Put the GPIO event(s) that just happened into event_str // so we can print it gpio_event_string(event_str, events); printf("GPIO %d %s\n", gpio, event_str); printf("flash %d \n",PICO_DEFAULT_LED_PIN); for (uint i=0;i<5;i++) { gpio_put(PICO_DEFAULT_LED_PIN, true); for(uint16_t y=0;y<1000;y++){ //tried this instead of sleep.. it runs but goes by too quick. } gpio_put(PICO_DEFAULT_LED_PIN, false); //sleep_ms(100); // causes code to hang if uncommented. } printf("flash\n"); }int main() { stdio_init_all(); printf("Hello GPIO IRQ\n"); gpio_init(PICO_DEFAULT_LED_PIN); gpio_set_dir(PICO_DEFAULT_LED_PIN, GPIO_OUT); gpio_put(PICO_DEFAULT_LED_PIN,true); gpio_init(GPIO_WATCH_PIN); gpio_pull_up(GPIO_WATCH_PIN); gpio_set_irq_enabled_with_callback(GPIO_WATCH_PIN, GPIO_IRQ_EDGE_RISE | GPIO_IRQ_EDGE_FALL, true, &gpio_callback); // Wait forever while (1); gpio_put(PICO_DEFAULT_LED_PIN, gpio_get(GPIO_WATCH_PIN)); //Tried to get this to work too, but not yet. //sleep_ms(500);}static const char *gpio_irq_str[] = { "LEVEL_LOW", // 0x1 "LEVEL_HIGH", // 0x2 "EDGE_FALL", // 0x4 "EDGE_RISE" // 0x8};void gpio_event_string(char *buf, uint32_t events) { for (uint i = 0; i < 4; i++) { uint mask = (1 << i); if (events & mask) { // Copy this event string into the user string const char *event_str = gpio_irq_str[i]; while (*event_str != '\0') { *buf++ = *event_str++; } events &= ~mask; // If more events add ", " if (events) { *buf++ = ','; *buf++ = ' '; } } } *buf++ = '\0';}Statistics: Posted by MikeD2 — Mon Jan 05, 2026 2:21 am