Sei sulla pagina 1di 3

Compsci 104: Interrupt Handling

The goal of this lesson is to become familiar with writing interrupt handling
routines. We will be writing our code in the high level language, C, based on a
provided template.

Discussion of Interrupts and the NiosII Template


The hardware…
Open http://www.cs.duke.edu/courses/spring07/cps104/Altera/Local/PIO.pdf which
contains a description of the generic parallel I/O devices used with the NiosII processor.
Specifically, this document contains information on the Programming model that
describes the structure of the memory mapped device. For this lesson we are using the
buttons (labeled keys on the DE2 board).

The software…
Go to http://www.cs.duke.edu/courses/spring07/cps104/Altera/Local/interrupts.c and
open the file. (Later we’ll run this code on the DE2 board).
This file utilizes infrastructure already provided by Altera for interrupt/exception
handling on the NiosII. Specifically, the macros IORD... and IOWR… provide support
for reading and writing to specific devices. These take care of mapping to specific offsets
required to access the different registers of the PIO devices. In the end these translated to
ldwio and stwio instructions with the appropriate address (pointer) arithmetic required to
access the correct register for the PIO device. (For the curios these macros are defined in
the file C:\altera\61\ip\nios2_ip\altera_nios2\HAL\inc\io.h

The source file, interrupts.c has five functions defined: main, initial_message,
init_button_pio, handle_button_interrupts, and handle_interrupt. We’ll talk about each of
these briefly. On item to note is that a constant BUTTON_PIO_BASE is used in several
places. This value is defined in the include file system.h a copy of this is at
http://www.cs.duke.edu/courses/spring07/cps104/Altera/Local/system.h

Download the NiosII system onto the FPGA.

For review, these are the steps required to download


1. Within Quartus click on Tools->programmer (alternatively you can click on
the symbol with the waves coming out of the middle of the diamond, third
from the left on the toolbar). A window will open.
2. If you do not see USB-Blaster to the right of the Hardware Setup, then we
need to configure the hardware.
a. Click on the hardware setup.
b. From the drop down menu select USB-Blaster
3. Mode should be JTAG
4. Either scroll down in the window, or make larger, until you can click on Add
File.
5. Then browse to the directory
C:\CPS104\DE2_Docs\DE2_demonstrations\SOPC_Builder\Reference_Desig
n\DE2_NIOS and select the file DE2_NIOS.sof
6. Click on the box in the column labeled Program/Configure
7. Click Start
8. You should see something like the following in the bottom Quartus window

Info: Started Programmer operation at Thu Feb 01 09:41:39 2007


Info: Configuring device index 1
Info: Device 1 contains JTAG ID code 0x020B40DD
Info: Configuration succeeded -- 1 device(s) configured
Info: Successfully performed operation(s)
Info: Ended Programmer operation at Thu Feb 01 09:41:41 2007

Running a program on the DE2


For this we will use an existing template application that you must download from
the course web page at
http://www.cs.duke.edu/courses/spring07/cps104/Altera/Local/interrupts.c Save
this file to the local disk or leave it open in the web browser to copy into a new
project.
1. Start the NiosII IDE
2. Create a new project (NiosII C/C++ Application)
3. Select Blank Project from the templates
4. Change the name of the project to interrupts
5. Specify the workspace as C:\CPS104\workspace
6. Specify a SOPC_Builder system (Note this is different than the one we used
for simulation):
C:\CPS104\DE2_Docs\DE2_demonstrations\SOPC_Builder\Reference_Desig
n\DE2_NIOS\system_0.ptf
7. Click next
8. Use an existing library or create a new system library (Note: if you use an
existing library make sure is for the DE2 board and for the simulation (it
should have a system_0 in the name somewhere).
9. Click Finish
10. Either import the saved interrupts.c file or create a new source file and
cut/paste from the web page into the new file.
11. Right Click on the interrupts project and select Run As -> NiosII Hardware
a. This is will compile the program and then download the program to
the DE2 Board and begin execution. You should see Hello from Nios
II! printed in the console of the NiosII IDE.
b. The program is now executing the while (1) loop.
12. Press key 2, 3 or 4 (do not press key 1 it likely causes the system to hang).
a. You should see information about the button that was pressed and a
total count increasing by an amount equal to the button pressed.
Your coding…
For the remainder of this lesson your task is to modify interrupts.c to control the red
LEDs such that one LED lights up at a time scrolling from right to left or left to right.
The direction changes whenever button 4 is pressed. We will use only LEDs 16 to 0
Here’s some useful information…

You can use the following macro to write to the Red LEDs.
IOWR_ALTERA_AVALON_PIO_DATA(LED_RED_BASE, value);
In this case value is a 32-bit integer where each bit maps to the LED to light up. E.g., if
value contains 0x10 LED 4 would be enabled. If the direction is right to left, then after
LED 0 is enabled, the next LED to light up should be LED 16

while( 1 )
{
usleep(100000);
/* some of your code goes here */
}

A final question: Is your code always correct? If yes, prove it. If not,
why not?
See me with your answer.

Potrebbero piacerti anche