DE1-SOC COMPUTER SYSTEM WITH NIOS II For Quartus II 15.0
DO_KEY3:
movi r12, KEY3
stw r12, 0(r10) /* return KEY3 value */
END_PUSHBUTTON_ISR:
ldw ra, 0(sp) /* Restore all used register to previous values */
ldw r10, 4(sp)
ldw r11, 8(sp)
ldw r12, 12(sp)
ldw r13, 16(sp)
addi sp, sp, 20
ret
.end
Figure 19. Interrupt service routine for the pushbutton keys (Part b).
3.5 Using Interrupts with C Language Code
An example of C language code for the DE1-SoC Computer that uses interrupts is shown in Figure 20. This code
performs exactly the same operations as the code described in Figure 16.
To enable interrupts the code in Figure 20 uses macros that provide access to the Nios II status and control registers.
A collection of such macros, which can be used in any C program, are provided in Figure 21.
The reset and exception handlers for the main program in Figure 20 are given in Figure 22. The function called
the_reset provides a simple reset mechanism by performing a branch to the main program. The function named
the_exception represents a general exception handler that can be used with any C program. It includes assembly
language code to check if the exception is caused by an external interrupt, and, if so, calls a C language routine
named interrupt_handler. This routine can then perform whatever action is needed for the specific application.
In Figure 22, the interrupt_handler code first determines which exception has occurred, by using a macro from
Figure 21 that reads the content of the Nios II interrupt pending register. The interrupt service routine that is
invoked for the interval timer is shown in 23, and the interrupt service routine for the pushbutton switches appears
in Figure 24.
The source code files shown in Figure 16 to Figure 24 are distributed as part of the Altera Monitor Program. The
files can be found under the heading sample programs, and are identified by the name Interrupt Example.
Altera Corporation - University Program
2015
23