tt_nios2_hardware_tutorial
tt_nios2_hardware_tutorial
TU-N2HWDV-4.0
Subscribe
© 2011 Altera Corporation. All rights reserved. ALTERA, ARRIA, CYCLONE, HARDCOPY, MAX, MEGACORE, NIOS, QUARTUS and STRATIX are Reg. U.S. Pat.
& Tm. Off. and/or trademarks of Altera Corporation in the U.S. and other countries. All other trademarks and service marks are the property of their respective
holders as described at www.altera.com/common/legal.html. Altera warrants performance of its semiconductor products to current specifications in accordance
with Altera’s standard warranty, but reserves the right to make changes to any products and services at any time without notice. Altera assumes no responsibility or
liability arising out of the application or use of any information, product, or service described herein except as expressly agreed to in writing by Altera. Altera
customers are advised to obtain the latest version of device specifications before relying on any published information and before placing orders for products or
services.
Additional Information
Document Revision History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Info–1
How to Contact Altera . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Info–1
Typographic Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Info–2
This tutorial introduces you to the system development flow for the Nios® II
processor. Using the Quartus® II software and the Nios II Embedded Design Suite
(EDS), you build a Nios II hardware system design and create a software program
that runs on the Nios II system and interfaces with components on Altera®
development boards. The tutorial is a good starting point if you are new to the Nios II
processor or the general concept of building embedded systems in FPGAs.
Building embedded systems in FPGAs involves system requirements analysis,
hardware design tasks, and software design tasks. This tutorial guides you through
the basics of each topic, with special focus on the hardware design steps. Where
appropriate, the tutorial refers you to further documentation for greater detail.
f If you are interested only in software development for the Nios II processor, refer to
the tutorial in the Getting Started with the Graphical User Interface chapter of the Nios II
Software Developer’s Handbook.
When you complete this tutorial, you will understand the Nios II system
development flow, and you will be able to create your own custom Nios II system.
Design Example
The design example you build in this tutorial demonstrates a small Nios II system for
control applications, that displays character I/O output and blinks LEDs in a binary
counting pattern. This Nios II system can also communicate with a host computer,
allowing the host computer to control logic inside the FPGA.
The example Nios II system contains the following components:
■ Nios II/s processor core
■ On-chip memory
■ Timer
■ JTAG UART
■ 8-bit parallel I/O (PIO) pins to control the LEDs
■ System identification component
Figure 1–1 is a block diagram showing the relationship among the host computer, the
target board, the FPGA, and the Nios II system.
Target Board
LED0
Nios II System
Debug LED1
control Instr 8
JTAG controller
LED6
LED7
Other logic
Clock
oscillator
As shown in Figure 1–1, other logic can exist within the FPGA alongside the Nios II
system. In fact, most FPGA designs with a Nios II system also include other logic. A
Nios II system can interact with other on-chip logic, depending on the needs of the
overall system. For the sake of simplicity, the design example in this tutorial does not
include other logic in the FPGA.
You can build the design example in this tutorial with any Altera development board
or your own custom board that meets the following requirements:
■ The board must have an Altera Stratix® series, Cyclone® series, or Arria® series
FPGA.
■ The FPGA must contain a minimum of 2500 logic elements (LE) or adaptive look-
up tables (ALUT).
■ The FPGA must contain a minimum of 50 M4K or M9K memory blocks.
■ An oscillator must drive a constant clock frequency to an FPGA pin. The
maximum frequency limit depends on the speed grade of the FPGA. Frequencies
of 50 MHz or less should work for most boards; higher frequencies might work.
■ FPGA I/O pins can optionally connect to eight or fewer LEDs to provide a visual
indicator of processor activity.
■ The board must have a JTAG connection to the FPGA that provides a
programming interface and communication link to the Nios II system. This
connection can be a dedicated 10-pin JTAG header for an Altera USB-Blaster™
download cable (revision B or higher) or a USB connection with USB-Blaster
circuitry embedded on the board.
1 To complete this tutorial, you must refer to the documentation for your board that
describes clock frequencies and pinouts. For Altera development boards, you can find
this information in the associated reference manual.
f For information about Altera development kits and development boards, refer to the
Literature: Development Kits page of the Altera website.
f For more information about OpenCore Plus, refer to OpenCore Plus Evaluation of
Megafunctions.
Analyze system
requirements
Custom
instruction
Nios II and
cores Define and generate custom
and system in Qsys peripheral
standard logic
peripherals
Altera
hardware
abstraction
Custom Integrate Qsys system Develop software with layer
hardware the Nios II Software and
into Quartus II project Build Tools for Eclipse peripheral
modules
drivers
f After completing this tutorial, refer to the Nios II Software Developer’s Handbook,
especially the tutorial in the Getting Started with the Graphical User Interface chapter, for
more information about the software development process. The handbook is a
complete reference for developing software for the Nios II processor.
f For more information about the following topics, refer to the related documentation:
■ For Nios II processor cores, refer to the Nios II Processor Reference Handbook.
■ For Qsys and developing custom components, refer to the System Design with Qsys
section of Volume 1: Design and Synthesis of the Quartus II Handbook.
■ For custom instructions, refer to the Nios II Custom Instruction User Guide.
f For further information about using the Quartus II software, refer to Introduction to the
Quartus II Software, the Quartus II Handbook, and the Quartus II Software Interactive
Tutorial in the Training Courses section of the Altera website.
Developing Software with the Nios II Software Build Tools for Eclipse
Using the Nios II Software Build Tools (SBT) for Eclipse™, you perform all software
development tasks for your Nios II processor system. After you generate the system
with Qsys, you can begin designing your C/C++ application code immediately with
the Nios II SBT for Eclipse. Altera provides component drivers and a hardware
abstraction layer (HAL) which allows you to write Nios II programs quickly and
independently of the low-level hardware details. In addition to your application code,
you can design and reuse custom libraries in your Nios II SBT for Eclipse projects.
To create a new Nios II C/C++ application project, the Nios II SBT for Eclipse uses
information from the .sopcinfo file. You also need the .sof file to configure the FPGA
before running and debugging the application project on target hardware.
The Nios II SBT for Eclipse can produce several outputs, listed below. Not all projects
require all of these outputs.
■ system.h file—Defines symbols for referencing the hardware in the system. The
Nios II SBT for Eclipse automatically create this file when you create a new board
support package (BSP).
■ Executable and Linking Format File (.elf)—Is the result of compiling a C/C++
application project, that you can download directly to the Nios II processor.
f For extensive information about developing software for the Nios II processor, refer to
the Nios II Software Developer's Handbook.
f For information about running and debugging Nios II programs, refer to the tutorial
in the Getting Started with the Graphical User Interface chapter of the Nios II Software
Developer’s Handbook.
f Altera provides several working Nios II reference designs which you can use as a
starting point for your own designs. After installing the Nios II EDS, refer to the
<Nios II EDS install path>/examples/verilog or the <Nios II EDS install path>/
examples/vhdl directory. Demonstration applications are also available in newer
development kit installations.
f For information about performing hardware simulation for Nios II system, refer to
Simulating Nios II Embedded Processor Designs.
f The design files appear next to this document on the Literature: Nios II Processor
page of the Altera website.
f For more information about these and other components, refer to the Embedded
Peripherals IP User Guide.
The .bdf contains an input pin for the clock input and eight output pins to drive LEDs
on the board. Next, you create a new Qsys system, which you ultimately connect to
these pins.
1 If a warning appears stating the selected device family does not match the
Quartus project settings, ignore the warning. You specify the device in the
Quartus project settings later in this tutorial.
2. In the documentation for your board, look up the clock frequency of the oscillator
that drives the FPGA.
3. On the Clock Settings tab, double-click the clock frequency in the MHz column
for clk_0. clk_0 is the default clock input name for the Qsys system. The
frequency you specify for clk_0 must match the oscillator that drives the FPGA.
4. Type the clock frequency and press Enter.
Next, you begin to add hardware components to the Qsys system. As you add each
component, you configure it appropriately to match the design specifications.
Figure 1–5 shows the On-Chip Memory (RAM or ROM) parameter editor.
1 For more information about on-chip memory, you can click Documentation in the
On-Chip Memory (RAM or ROM) parameter editor. This documentation feature is
available in the parameter editor for each component.
7. In the Name column of the system contents table, right-click the on-chip memory
and click Rename.
8. Type onchip_mem and press Enter.
1 You must type these tutorial component names exactly as specified. Otherwise, the
tutorial programs written for this Nios II system fail in later steps. In general, it is a
good habit to give descriptive names to hardware components. Nios II programs use
these symbolic names to access the component hardware. Therefore, your choice of
component names can make Nios II programs easier to read and understand.
Figure 1–6 shows the Core Nios II tab of the Nios II Processor parameter editor.
15. Click the Caches and Memory Interfaces tab. Figure 1–7 shows the GUI.
16. In the Instruction cache list, select 2 Kbytes.
17. In the Burst transfers list, select Disable.
18. In the Number of tightly coupled instruction master port(s) list, select None.
Figure 1–7 shows the Caches and Memory Interfaces tab of the Nios II Processor
parameter editor.
Figure 1–7. Nios II Parameter Editor – Caches and Memory Interfaces Tab
1 Do not change any settings on the Advanced Features, MMU and MPU Settings,
JTAG Debug Module, or Custom Instruction tabs.
19. Click Finish. You return to the Qsys System Contents tab.
f For more information about configuring the Nios II core, refer to the Instantiating the
Nios II Processor in Qsys chapter of the Nios II Processor Reference Handbook.
f For more information about connecting memory to Nios II systems, refer to the System
Design with Qsys section of Volume 1: Design and Synthesis of the Quartus II Handbook.
3. Click Finish. You return to the Qsys System Contents tab, and an instance of the
JTAG UART appears in the system contents table.
4. In the Name column, right-click the JTAG UART and click Rename.
5. Type jtag_uart and press Enter.
6. Connect the clk port of the clk_0 clock source to the clk port of the JTAG UART.
7. Connect the clk_reset port of the clk_0 clock source to the reset port of the JTAG
UART.
8. Connect the data_master port of the Nios II processor to the avalan_jtag_slave
port of the JTAG UART.
1 The instruction_master port of the Nios II processor does not connect to the JTAG
UART because the JTAG UART is not a memory device and cannot send instructions
to the Nios II processor.
f For more information about the JTAG UART, refer to the JTAG UART Core chapter in
the Embedded Peripherals IP User Guide.
4. Click Finish. You return to the Qsys System Contents tab, and an instance of the
interval timer appears in the system contents table.
5. In the Name column, right-click the interval timer and click Rename.
6. Type sys_clk_timer and press Enter.
7. Connect the clk port of the clk_0 clock source to the clk port of the interval timer.
8. Connect the clk_reset port of the clk_0 clock source to the reset port of the
interval timer.
9. Connect the data_master port of the Nios II processor to the s1 port of the interval
timer.
f For more information about the timer, refer to the Timer Core chapter in the Embedded
Peripherals IP User Guide.
3. Click Finish. You return to the Qsys System Contents tab, and an instance of the
system ID peripheral appears in the system contents table.
4. In the Name column, right-click the system ID peripheral and click Rename.
5. Type sysid and press Enter.
6. Connect the clk port of the clk_0 clock source to the clk port of the system ID
peripheral.
7. Connect the clk_reset port of the clk_0 clock source to the reset port of the
system ID peripheral.
8. Connect the data_master port of the Nios II processor to the control_slave port
of the system ID peripheral.
f For more information about the system ID peripheral, refer to the System ID Core
chapter in the Embedded Peripherals IP User Guide.
1 Do not change the default settings. The parameter editor defaults to an 8-bit output-
only PIO, which exactly matches the needs for the design example.
3. Click Finish. You return to the Qsys System Contents tab, and an instance of the
PIO appears in the system contents table.
4. In the Name column, right-click the PIO and click Rename.
5. Type led_pio and press Enter.
6. Connect the clk port of the clk_0 clock source to the clk port of the PIO.
7. Connect the clk_reset port of the clk_0 clock source to the reset port of the PIO.
8. Connect the data_master port of the Nios II processor to the s1 port of the PIO.
9. In the external_connection row, click Click to export in the Export column to
export the PIO ports.
f For more information about the PIO, refer to the PIO Core chapter in the Embedded
Peripherals IP User Guide.
5. Click the IRQ value for the sys_clk_timer component to select it.
6. Type 1 and press Enter to assign a new IRQ value.
Figure 1–12 shows the Qsys System Contents tab with the complete system.
4. Type first_nios2_system in the File name box and click Save. The Generate
dialog box appears and system generation process begins.
The generation process can take several minutes. Output messages appear as
generation progresses. When generation completes, the final "Info: Finished:
Create HDL design files for synthesis" message appears. Figure 1–13 shows the
successful system generation.
f For more information about generating systems with Qsys, refer to the System Design
with Qsys section of Volume 1: Design and Synthesis of the Quartus II Handbook. For
information about hardware simulation for Nios II systems, refer to Simulating Nios II
Embedded Processor Designs.
f For further information about using the Quartus II software, refer to Introduction to the
Quartus II Software, the Quartus II Handbook, and the Quartus II Software Interactive
Tutorial in the Training section of the Altera website.
1 How you instantiate the system module depends on the design entry method of the
overall Quartus II project. For example, if you were using Verilog HDL for design
entry, you would instantiate the Verilog module first_nios2_system defined in the file
first_nios2_system.v.
To instantiate the system module in the .bdf, perform the following steps:
1. Double-click in the empty space to the right of the input and output wires. The
Symbol dialog box appears.
2. Under Libraries, expand Project.
3. Click first_nios2_system. The Symbol dialog box displays the first_nios2_system
symbol.
4. Click OK. You return to the .bdf schematic. The first_nios2_system symbol tracks
with your mouse pointer.
5. Position the symbol so the pins on the symbol align with the wires on the
schematic.
6. Click to anchor the symbol in place.
7. If your target board does not have LEDs that the Nios II system can drive, you
must delete the LEDG[7..0] pins. To delete the pins, perform the following steps:
a. Click the output symbol LEDG[7..0] to select it.
b. On your keyboard, press Delete.
8. To save the completed .bdf, click Save on the File menu.
Figure 1–14 shows the completed .bdf schematic using the LED pins.
1 You must know the pin layout for the board to complete this section. You also must
know other requirements for using the board, which are beyond the scope of this
document. Refer to the documentation for your board.
2. In the Family list, select the FPGA family that matches your board.
3. Under Target device, select Specific device selected in 'Available devices' list.
4. Under Available devices, select the exact device that matches your board.
6. Select the appropriate FPGA pin that connects to the oscillator on the board.
1 If your design fails to work, recheck your board documentation for this step
first.
Figure 1–17 shows the Unused Pins page of the Device and Pin Options dialog box.
Figure 1–17. The Unused Pins Page of the Device and Pin Options Dialog Box
14. In the Reserve all unused pins list, select As input tri-stated with weak pull-up.
With this setting, all unused I/O pins on the FPGA enter a high-impedance state
after power-up.
c Unused pins are set as input tri-stated with weak pull-up to remove contention which
might damage the board. Depending on the board, you might have to make more
assignments for the project to function correctly. You can damage the board if you fail
to account for the board design. Consult with the maker of the board for specific
contention information.
15. Click OK to close the Device and Pin Options dialog box.
16. Click OK to close the Device dialog box.
f For more information about making assignments in the Quartus II software, refer to
the Volume 2: Design Implementation and Optimization of the Quartus II Handbook.
f For more information about meeting timing requirements in the Quartus II software,
refer to the Volume 1: Design and Synthesis of the Quartus II Handbook.
Congratulations! You have finished integrating the Nios II system into the Quartus II
project. You are ready to download the .sof to the target board.
3. Click Hardware Setup in the upper left corner of the Quartus II Programmer to
verify your download cable settings. The Hardware Setup dialog box appears.
4. Select the appropriate download cable in the Currently selected hardware list. If
the appropriate download cable does not appear in the list, you must first install
drivers for the cable.
f For information about download cables and drivers, refer to the Download
Cables page of the Altera website.
5. Click Close.
6. In the nios2_quartus2_project.sof row, turn on Program/Configure.
7. Click Start. The Progress meter sweeps to 100% as the Quartus II software
configures the FPGA.
At this point, the Nios II system is configured and running in the FPGA, but it does
not yet have a program in memory to execute.
f For a complete tutorial on using the Nios II SBT for Eclipse to develop programs, refer
to the Getting Started with the Graphical User Interface chapter of the Nios II Software
Developer’s Handbook.
4. On the File menu, point to New, and then click Nios II Application and BSP from
Template. The Nios II Application and BSP from Template wizard appears.
Figure 1–20 shows the GUI.
1 Though not needed for this tutorial, note the BSP Editor button in the lower
right corner of the dialog box. You use the Nios II BSP Editor to access
advanced BSP settings.
3. Adjust the following settings to reduce the size of the compiled executable:
a. Turn on Reduced device drivers.
b. Turn off Support C++.
c. Turn off GPROF support.
d. Turn on Small C library.
e. Turn off ModelSim only, no hardware support.
f For more information about BSPs, refer to the Nios II Software Developer's
Handbook.
4. Click OK. The BSP regenerates, the Properties dialog box closes, and you return to
the Nios II SBT for Eclipse.
5. In the Project Explorer view, right-click the count_binary project and click Build
Project.
The Build Project dialog box appears, and the Nios II SBT for Eclipse begins
compiling the project. When compilation completes, a "count_binary build complete"
message appears in the Console view.
1 If the Run Configurations dialog box appears, verify that Project name and
ELF file name contain relevant data, then click Run.
When the target hardware starts running the program, the Nios II Console view
displays character I/O output. Figure 1–22 shows the output. If you connected
LEDs to the Nios II system in “Integrate the Qsys System into the Quartus II
Project” on page 1–24, then the LEDs blink in a binary counting pattern.
2. Click the Terminate icon (the red square) on the toolbar of the Nios II Console
view to terminate the run session. When you click the Terminate icon, the Nios II
SBT for Eclipse disconnects from the target hardware.
You can edit the count_binary.c program in the Nios II SBT for Eclipse text editor and
repeat these two steps to witness your changes executing on the target board. If you
rerun the program, buffered characters from the previous run session might display in
the Console view before the program begins executing.
f For information on running and debugging programs on target hardware, refer to the
tutorial in the Getting Started with the Graphical User Interface chapter of the Nios II
Software Developer’s Handbook.
This chapter provides additional information about the document and Altera.
f Refer to the Nios II Embedded Design Suite Release Notes page of the Altera website
for the latest features, enhancements, and known issues in the current release.
Typographic Conventions
The following table shows the typographic conventions this document uses.