Nios® V/II Embedded Design Suite (EDS)
Support for Embedded Development Tools, Processors (SoCs and Nios® V/II processor), Embedded Development Suites (EDSs), Boot and Configuration, Operating Systems, C and C++

alt_irq_register question

Altera_Forum
Honored Contributor II
1,155 Views

Hello, 

 

I have a specific question about using global variables instead of passing context argument. 

 

What's the advantage to pass variable through the context argument if we can directly access to the global variable? 

 

I have an example: 

 

//Global Variable 

volatile int edge_capture; 

 

 

 

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. 

*/ 

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); 

 

/* 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 );  

 

 

Instead, we could only use the edge_capture variable 

 

 

static void handle_button_interrupts(void* context, alt_u32 id) 

/* Store the value in the Button's edge capture register in *context. */ 

edge_capture = 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); 

 

/* Initialize the button_pio. */ 

 

static void init_button_pio() 

/* 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, NULL, handle_button_interrupts );  

 

 

Best regards 

 

Christian
0 Kudos
0 Replies
Reply