Altera_Forum
Honored Contributor
14 years agopush_button irq handler question
Hi,
I use NEEK CycloneIII (CP3C25). App is quite simple it turns on the led corresponding the push button pressed (see code below). I copied irq handler procedure from standard binary_counter.c example and it doesn't work unless I put some delay at the end of handler. When I comment delay in the handler and debug my app it works fine, but when I run app in hardware it doesn't work. Any ideas why the app doesn't run propelry?
# include <stdio.h># include "system.h"# include "alt_types.h"# include "sys/alt_irq.h"# include "altera_avalon_pio_regs.h"
//#include <unistd.h>
/* A variable to hold the value of the button pio edge capture register. */
volatile int edge_capture;
/* button IRQ function */
static void handle_button_interrupts(void* context, alt_u32 id)
{
/* Cast context to edge_capture's type. It is important that this be
* declared volatile to avoid unwanted compiler optimization.
*/
int delay;
volatile int* edge_capture_ptr = (volatile int*) context;
/* Store the value in the Button's edge capture register in *context. */
*edge_capture_ptr = IORD_ALTERA_AVALON_PIO_EDGE_CAP(BUTTON_PIO_BASE);
/* Reset the Button's edge capture register. */
IOWR_ALTERA_AVALON_PIO_EDGE_CAP(BUTTON_PIO_BASE, 0);
//now optional delay is commented
/*delay=0x0;
while (delay<100000)
{
delay++;
}
*/
}
/* Initialize the button_pio. */
static void init_button_pio()
{
/* Recast the edge_capture pointer to match the alt_irq_register() function
* prototype. */
void* edge_capture_ptr = (void*) &edge_capture;
/* Enable all 4 button interrupts. */
IOWR_ALTERA_AVALON_PIO_IRQ_MASK(BUTTON_PIO_BASE, 0xf);
/* Reset the edge capture register. */
IOWR_ALTERA_AVALON_PIO_EDGE_CAP(BUTTON_PIO_BASE, 0x0);
/* Register the interrupt handler. */
alt_irq_register( BUTTON_PIO_IRQ, edge_capture_ptr,
handle_button_interrupts );
}
static void handle_button_press(alt_u8 type)
{
/* Button press actions while counting. */
if (type == 'r')
{
IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE, ~edge_capture);
}
}
int main()
{
IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE, 0xf);
while (1)
{
if(edge_capture!=0)
{
handle_button_press('r');
}
}
return 0;
}