Trouble clearing events from PRU

Hi,

I’m trying to send events from a PRU C program to the host. At the moment my PRU code just delays for 1s, then activates PRU_EVTOUT_1 and toggles an LED so I know it’s working ok. My host program sits there waiting for the PRU_EVTOUT_1 event, then is supposed to increment a counter and clear the event. Well, it sits there for one second, then seems to detect repeated events rapidly incrementing the counter. I assume the event isn’t being cleared, but it’s not clear to me what I’m doing wrong. Quite a bit of this is based on code I’ve grabbed from Derek Molloy’s book.

Any suggestions are gratefully received!

Here’s my code:

PRU code:

#include <stdint.h>

// Interrupts out to host
#define PRU_EVTOUT_0 3 // Program completion
#define PRU_EVTOUT_1 4
#define PRU0_R31_VEC_VALID 32 //

// R30 is the GPIO register
// R31 is the interrupt register
volatile register unsigned int __R31, __R30;

int main(void) {
``
while(!(__R31 & 0x8)) { // Have to hold button down since we're only really polling it here every 1.1s
__delay_cycles(200000000);
__R31 = PRU0_R31_VEC_VALID | PRU_EVTOUT_1;

// Turn LED on
__R30 = __R30 | 0x20;
__delay_cycles(20000000);
// Turn LED off
__R30 = __R30 & ~0x20;
}

// Send interrupt to host and halt
__R31 = 32 | 3;
__halt();

// Should never return
return 0;
}

And the host code:

#include <stdio.h>
#include <stdlib.h>
#include <prussdrv.h>
#include <pruss_intc_mapping.h>
#include <pthread.h>

void *threadFunction(void *value) {
do {
int noTimes = prussdrv_pru_wait_event(PRU_EVTOUT_1);
printf("\nEvent occured (%d)", noTimes);
prussdrv_pru_clear_event(PRU_EVTOUT_1, PRU0_ARM_INTERRUPT);
} while(1);
}

int main(int argc, char **argv) {
pthread_t eventThread;

prussdrv_init();
prussdrv_open(PRU_EVTOUT_0);
prussdrv_open(PRU_EVTOUT_1);

tpruss_intc_initdata pruss_intc_initdata = PRUSS_INTC_INITDATA;
prussdrv_pruintc_init(&pruss_intc_initdata);

printf("Executing program and waiting for termination\n");
prussdrv_exec_program(0, "./text.bin");
if(pthread_create(&eventThread, NULL, &threadFunction, NULL)) {
printf("Failed to create thread!");
}
// prussdrv_start_irqthread(PRU_EVTOUT_1, sched_get_priority_max(SCHED_FIFO)- 2, threadFunction);

// Wait for the PRU to let us know it's done
prussdrv_pru_wait_event(PRU_EVTOUT_0);
printf("All done\n");

prussdrv_pru_disable(0 /* PRU0 */);
prussdrv_exit();

return 0;
}