Nios II Classic Software Developer’s Handbook

ID 683282
Date 5/14/2015
Public
Document Table of Contents

8.2.7.2. Registering the Button PIO ISR with the HAL

Based on the code in the example, the following execution flow is possible:
  • Button is pressed, generating an IRQ.
  • The ISR gains control.
    • With the IIC, the HAL general exception funnel gains control of the processor, and dispatches the handle_button_interrupts() ISR.
    • With an EIC, the processor branches to the address in the vector table, which transfers control to the handle_button_interrupts() ISR.
  • handle_button_interrupts() services the hardware interrupt and returns.
  • Normal program operation continues with an updated value of edge_capture.

Example 8–2. Registering the Button PIO ISR with the HAL

#include "sys/alt_irq.h"
#include "system.h"
...
/* Declare a global variable to hold the edge capture value. */
volatile int edge_capture;
...
/* 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 ISR. */
#ifdef ALT_ENHANCED_INTERRUPT_API_PRESENT
alt_ic_isr_register(BUTTON_PIO_IRQ_INTERRUPT_CONTROLLER_ID,
BUTTON_PIO_IRQ,
handle_button_interrupts,
edge_capture_ptr, 0x0);
#else
alt_irq_register( BUTTON_PIO_IRQ,
edge_capture_ptr,
handle_button_interrupts );
#endif
}
Note: Additional software examples that demonstrate implementing ISRs, such as the count_binary example project template, are installed with the Nios II Embedded Design Suite (EDS).