DavidPinball
Posts: 3
Joined: Wed Jul 28, 2021 11:14 pm

PIO interrupt issue

Wed Jul 28, 2021 11:24 pm

Hi

I'm trying to use one of the interrupts in PIO, as follows:

Code: Select all

.program pio_irq
    irq wait 0
 
I've configured the pio interrupt and the irq...

Code: Select all

    irq_set_exclusive_handler(PIO0_IRQ_0, isr);
    irq_set_enabled(PIO0_IRQ_0, true);

    pio_set_irq0_source_enabled(pio, pis_interrupt0, true);

The interrupt is being called as I have a printf in there, but the handler is repeatedly called, nothing I do is resetting the interrupt - I'm expecting the PIO to raise the interrupt once and then stop.

Code: Select all

void isr()
{
    printf("In here");
    pio_interrupt_clear(pio0, 0);
    irq_clear(PIO0_IRQ_0);
}
Any help with resetting the interrupt much appreciated.

kilograham
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 876
Joined: Fri Apr 12, 2019 11:00 am
Location: austin tx

Re: PIO interrupt issue

Thu Jul 29, 2021 2:40 am

a PIO program loops by default... so the irq is raised again. You need to explicitly hang the state machine by JMPing to the same location for example.

Note also you never need to call irq_clear as resetting the underlying IRQ cause always clears it for you.

DavidPinball
Posts: 3
Joined: Wed Jul 28, 2021 11:14 pm

Re: PIO interrupt issue

Thu Jul 29, 2021 7:30 pm

kilograham wrote: a PIO program loops by default... so the irq is raised again. You need to explicitly hang the state machine by JMPing to the same location for example.

Note also you never need to call irq_clear as resetting the underlying IRQ cause always clears it for you.
Thanks for the reply - I realised late last night that the pio will loop as the PC will just keep incrementing and overflowing back to zero.

So I'm just using pio_interrupt_clear to reset the interrupt in my isr, but I'm still not seeing it getting reset. I have a 'irq wait 0' in the pio which I believe will set the interrupt and wait until it is reset before continuing, but I don't see it resetting.

Code: Select all

.program pio_irq
.wrap_target
    ; LED On
    set pins, 1
    ; Wait for high on pin 12
    wait 1 pin 12
    ; LED Off
    set pins, 0

    ; Wait for isr to finish
    irq wait 0

.wrap
In the code above, I see the LED go on, then when I shunt pin 12 to Vcc I see the LED go off, but it never goes back on again after the isr has returned - the irq wait command isn't returning. If I remove the irq wait command I see the LED go on when I remove the shunt to Vcc on pin 12.
Last edited by DavidPinball on Thu Jul 29, 2021 8:13 pm, edited 2 times in total.

kilograham
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 876
Joined: Fri Apr 12, 2019 11:00 am
Location: austin tx

Re: PIO interrupt issue

Thu Jul 29, 2021 7:42 pm

DavidPinball wrote:
Thu Jul 29, 2021 7:30 pm
kilograham wrote: a PIO program loops by default... so the irq is raised again. You need to explicitly hang the state machine by JMPing to the same location for example.

Note also you never need to call irq_clear as resetting the underlying IRQ cause always clears it for you.
Thanks for the reply - I realised late last night that the pio will loop as the PC will just keep incrementing and overflowing back to zero.
To be clear(er by the time i've written it), it doesn't overflow, it reaches an implicit .wrap at the end of the program and immediately jumps to the implicit .wrap_target at the beginning.... these are provided by pioasm as a convenience (looping code is common) if you don't use .wrap etc. yourself. Note that if you were to fall off the end of the program, you'd execute whatever was at the next slot in the PIO instruction memory after the program (modulo the 32 - the instruction memory size)
So I'm just using pio_interrupt_clear to reset the interrupt in my isr, but I'm still not seeing it getting reset. I have a 'wait irq 0' in the pio which I believe will set the interrupt and wait until it is reset before continuing, but I don't see it resetting.

Code: Select all

.program pio_irq
.wrap_target
    ; LED On
    set pins, 1
    ; Wait for high on pin 12
    wait 1 pin 12
    ; LED Off
    set pins, 0

    ; Wait for isr to finish
    irq wait 1

.wrap
In the code above, I see the LED go on, then when I shunt pin 12 to Vcc I see the LED go off, but it never goes back on again after the isr has returned - the irq wait command isn't returning. If I remove the irq wait command I see the LED go on when I remove the shunt to Vcc on pin 12.
This is getting confusing, you say you have a wait irq 0 but i see a irq wait 1. can you please post all the pertinent code fragments

DavidPinball
Posts: 3
Joined: Wed Jul 28, 2021 11:14 pm

Re: PIO interrupt issue

Thu Jul 29, 2021 10:23 pm

Sorry, I made a right mess of that, I've now corrected my post above.

I've found that the isr is being called fine, but only when the 'wait 1 pin 12' either is satisfied first time or it's commented out. If the wait is hit and it has to wait but is then satisfied, the 'irq wait 0' blocks without calling the isr.

I can see this behaviour by turning off the LED at the start of the pio loop and turning on again before and after 'irq wait 0' and seeing the LED behaviour. Also, printf in the isr shows that the isr isn't hit again.

Return to “SDK”