Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
12 years ago

Interrupt code problem

Hi,

Actually I am a beginner in using niosII. Currently I am having some trouble in trying the interrupt code.

This is a simple interrupt code. I am using SW0 and SW1 as the buttons.

But its not working.

The output showing in nios2 console is just "none"

no changes happening when I slide the switch.

# include "alt_types.h"# include "altera_avalon_pio_regs.h"# include "sys/alt_irq.h"# include "system.h"# include <stdio.h># include <unistd.h>

/* A variable to hold the value of the button pio edge capture register. */

volatile int edge_capture;

int count = 0;

int handle_interrupt();

# ifdef PIO_1_BASE

static void handle_button_interrupts(void* context, alt_u32 id)

{

volatile int* edge_capture_ptr = (volatile int*) context;

*edge_capture_ptr = IORD_ALTERA_AVALON_PIO_EDGE_CAP(PIO_1_BASE);

count += handle_interrupt(*edge_capture_ptr);

printf ("Total count = %d\n", count);

IOWR_ALTERA_AVALON_PIO_EDGE_CAP(PIO_1_BASE, 0);

}

/* Initialize the button_pio. */

static void init_button_pio()

{

volatile void* edge_capture_ptr = (void *) &edge_capture;

IOWR_ALTERA_AVALON_PIO_IRQ_MASK(PIO_1_BASE, 0xf);

IOWR_ALTERA_AVALON_PIO_EDGE_CAP(PIO_1_BASE, 0x0);

alt_irq_register( PIO_1_IRQ, edge_capture_ptr,

handle_button_interrupts );

}

# endif

static void initial_message()

{

printf("\n\n**************************\n");

printf("* Hello from Nios II! *\n");

printf("**************************\n");

}

int main(void)

{

/* Initialize the button pio. */

# ifdef PIO_1_BASE

init_button_pio();# endif

alt_irq_enable_all(PIO_1_IRQ);

/* Initial message to output. */

initial_message();

/* Continue counting loop. */

while( 1 )

{

usleep(100000);

if(edge_capture!=0)

{

handle_interrupt(edge_capture);

}

else

{

printf("none \n");

}

}

return 0;

}

int handle_interrupt(int button)

{

switch (button)

{

case 0x1:

printf("Button 1");

return 1;

break;

case 0x2:

printf( "Button 2 \n");

return 2;

break;

case 0x3:

printf( "Both 3\n");

return 3;

break;

default:

printf( "Button press 0x%x UNKNOWN!!\n", button);

return 0;

break;

}

}

Can anyone help me out??

Thanks in advance.

2 Replies