Recent

Author Topic: gpio_set_irq_enabled_with_callback on the PiPico  (Read 595 times)

Germo

  • New member
  • *
  • Posts: 9
gpio_set_irq_enabled_with_callback on the PiPico
« on: June 15, 2025, 11:25:02 am »
I'm enjoying FreePascal for programming my PiPico, but i ran into trouble.

I managed to use the GPIO. But i can't get the IRQ on that working.
When i start my program on the PiPico, the debugger halts on the 'gpio_set_irq_enabled_with_callback' function.

Anyone can point me in the right direction?

Thaddy

  • Hero Member
  • *****
  • Posts: 17418
  • Ceterum censeo Trumpum esse delendum (Tnx Charlie)
Re: gpio_set_irq_enabled_with_callback on the PiPico
« Reply #1 on: June 15, 2025, 04:13:50 pm »
You need to do this something like this:
Code: Pascal  [Select][+][-]
  1. program gpio_irq_test;
  2.  
  3. {$MODE OBJFPC}
  4. {$H+}
  5.  
  6. uses
  7.   pico_gpio_c, pico_irq_c;
  8.  
  9. const
  10.   LED_PIN = 25;
  11.   IRQ_PIN = 15;
  12.  
  13. procedure irq_callback(gpio: byte; events: longword); cdecl;
  14. begin
  15.   gpio_put(LED_PIN, not gpio_get(LED_PIN)); // Toggle LED on IRQ
  16. end;
  17.  
  18. begin
  19.   stdio_init_all;
  20.   gpio_init(LED_PIN);
  21.   gpio_set_dir(LED_PIN, GPIO_OUT);
  22.   gpio_init(IRQ_PIN);
  23.   gpio_set_dir(IRQ_PIN, GPIO_IN);
  24.   gpio_pull_up(IRQ_PIN);
  25.  
  26.   gpio_set_irq_enabled_with_callback(
  27.     IRQ_PIN,
  28.     GPIO_IRQ_EDGE_FALL,
  29.     true,
  30.     @irq_callback
  31.   );
  32.  
  33.   while true do
  34.     // Main loop
  35.   end;
  36. end.

And make sure you do this:
Link with pico_multicore, pico_stdlib, and hardware_gpio.
But you did not give me the hardware you want to control. So your milage may vary.
The above is very simple - for IoT fanatics like you and me, and should work.
Note everything is cdecl, but you know that.
« Last Edit: June 15, 2025, 04:23:37 pm by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

Germo

  • New member
  • *
  • Posts: 9
Re: gpio_set_irq_enabled_with_callback on the PiPico
« Reply #2 on: June 15, 2025, 06:11:43 pm »

But you did not give me the hardware you want to control. So your milage may vary.
The above is very simple - for IoT fanatics like you and me, and should work.
Note everything is cdecl, but you know that.

Thanks for the reply, but that does not work for me.

I'm using a rp2040. (after https://wiki.freepascal.org/ARM_Embedded_Tutorial_-_FPC_and_the_Raspberry_Pi_Pico )

I thought it would be very simple, but adding the irq-handler seems to not work

And no, i did not know that everything is cdecl (but that did not do the trick)

Thaddy

  • Hero Member
  • *****
  • Posts: 17418
  • Ceterum censeo Trumpum esse delendum (Tnx Charlie)
Re: gpio_set_irq_enabled_with_callback on the PiPico
« Reply #3 on: June 15, 2025, 07:35:29 pm »
Come back later, this was just a part of my robot code.
It should have helped you , though.
You are probably over-complicating things.
Can you give some code that is not working?
My example was targeted at the 2040.
The key is the callback.
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

 

TinyPortal © 2005-2018