NIOS2 API函数

C Example

Example 8–1 illustrates an ISR that services a hardware interrupt from a button
parallel I/O (PIO) component. This example is based on a Nios II system with a 4-bit PIO peripheral connected to push buttons. An IRQ is generated any time a button is pushed. The ISR code reads the PIO peripheral’s edge capture register and stores the value to a global variable. The address of the global variable is passed to the ISR in the context pointer.

ALT_ENHANCED_INTERRUPT_API_PRESENT被定义时,启用此版本的中断注册API函数

// Example 8–1. An ISR to Service a Button PIO Interrupt
#include "system.h"
#include "altera_avalon_pio_regs.h"
#include "alt_types.h"
#ifdef ALT_ENHANCED_INTERRUPT_API_PRESENT
static void handle_button_interrupts(void* context)
#else
static void handle_button_interrupts(void* context, alt_u32 id)
#endif
{
/* 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;
/*
* Read the edge capture register on the button PIO.
* Store value.
*/
*edge_capture_ptr =
IORD_ALTERA_AVALON_PIO_EDGE_CAP(BUTTON_PIO_BASE);
/* Write to the edge capture register to reset it. */
IOWR_ALTERA_AVALON_PIO_EDGE_CAP(BUTTON_PIO_BASE, 0);
/* Read the PIO to delay ISR exit. This is done to prevent a
spurious interrupt in systems with high processor -> pio
latency and fast interrupts. */
IORD_ALTERA_AVALON_PIO_EDGE_CAP(BUTTON_PIO_BASE);
}

// ------------------------------------------

// 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
}
void (*alt_isr_func) (void* isr_context)
{
}

int alt_ic_isr_register(
  alt_u32 ic_id,
  alt_u32 irq,
  alt_isr_func isr,
  void *isr_context,
  void* flags);

The function has the following parameters:
ic_id is the interrupt controller identifier (ID) as defined in system.h. With daisy-chained EICs, ic_id identifies the EIC in the daisy chain. With the IIC, ic_id is not significant.
irq is the hardware interrupt number for the device, as defined in system.h.

For the IIC, irq is the IRQ number. Interrupt priority corresponds inversely to the IRQ number. Therefore, IRQ0 represents the highest priority interrupt and IRQ31 is the lowest.

For an EIC, irq is the interrupt port ID.

isr_context points to a data structure associated with the device driver instance.
isr_context is passed as the input argument to the isr function. It is used to pass context-specific information to the ISR, and can point to any ISR-specific information. The context value is opaque to the HAL; it is provided entirely for the benefit of the user-defined ISR.
isr is a pointer to the ISR function that is called in response to IRQ number irq.
The ISR function prototype is:
void (*handler_func)(void * isr_context);
The input argument provided to this function is the isr_context.
Registering a null pointer for isr results in the interrupt being disabled.
flags is reserved.
The HAL registers the ISR by one of the foll

你可能感兴趣的:(NIOS2 API函数)