Nios® II Embedded Design Suite (EDS)
Support for Embedded Development Tools, Processors (SoCs and Nios® II processor), Embedded Development Suites (EDSs), Boot and Configuration, Operating Systems, C and C++
Announcements
Intel Support hours are Monday-Fridays, 8am-5pm PST, except Holidays. Thanks to our community members who provide support during our down time or before we get to your questions. We appreciate you!

Need Forum Guidance? Click here
Search our FPGA Knowledge Articles here.

Interrupt code problem

Altera_Forum
Honored Contributor II
864 Views

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.
0 Kudos
2 Replies
Altera_Forum
Honored Contributor II
62 Views

Hi, maybe in you interrupt function, you should write  

IOWR_ALTERA_AVALON_PIO_EDGE_CAP(PIO_1_BASE, 1); 

 

Avoid printf() in interrupts 

 

Here, you could post your code within [ code] balises 

 

(Instead of printf(), you could light up some LEDs)
Reply