Nios II Gen2 Hardware Development Tutorial
Nios II Gen2 Hardware Development Tutorial
2014.09.22
This tutorial describes the system development flow for the Altera® Nios® II processor.
Using the Quartus® II software and the Nios II Embedded Design Suite (EDS), you can:
• build a Nios II hardware system design
• create a software program that runs on the Nios II system and interfaces with components on Altera
development boards
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.
© 2014 Altera Corporation. All rights reserved. ALTERA, ARRIA, CYCLONE, ENPIRION, MAX, MEGACORE, NIOS, QUARTUS and STRATIX words and logos are
trademarks of Altera Corporation and registered in the U.S. Patent and Trademark Office and in other countries. All other words and logos identified as
trademarks or service marks are the property of their respective holders as described at www.altera.com/common/legal.html. Altera warrants performance ISO
of its semiconductor products to current specifications in accordance with Altera's standard warranty, but reserves the right to make changes to any 9001:2008
products and services at any time without notice. Altera assumes no responsibility or liability arising out of the application or use of any information, Registered
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.
www.altera.com
101 Innovation Drive, San Jose, CA 95134
AN-717
2 OpenCore Plus Evaluation 2014.09.22
Related Information
• Altera Development Kits Documentation
• Altera Software Installation and Licensing
Related Information
OpenCore Plus Evaluation of Megafunctions
Provides more information about OpenCore Plus.
Send Feedback
AN-717
2014.09.22 Nios II Design Example 3
The block diagram shows the relationship between the host computer, the target board, the FPGA, and
the Nios II system.
Target Board
Altera FPGA VCC
LED0
Nios II System
Debug LED1
control Instr 8
Nios II/s Data PIO
JTAG controller
core
LED6
LED7
Other logic
Clock
oscillator
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. This design example does not include other logic in the FPGA.
Send Feedback
AN-717
4 Nios II System Development Flow 2014.09.22
Analyze system
requirements
Altera hardware
abstraction layer
Custom hardware Develop software with and peripheral
Integrate Qsys system the Nios II Software
modules into Quartus II project drivers
Build Tools for Eclipse
Send Feedback
AN-717
2014.09.22 Analyzing System Requirements 5
Related Information
Volume 1: Design and Synthesis of the Quartus II Handbook
Provides more information about Qsys and developing custom components
Send Feedback
AN-717
6 Integrating the Qsys System into the Quartus II Project 2014.09.22
Developing Software with the Nios II Software Build Tools for Eclipse
You can perform all software development tasks for your Nios II processor system using the Nios II
Software Build Tools (SBT) for Eclipse™.
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 abstrac‐
tion 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.
The Nios II SBT for Eclipse can produce several outputs but not all projects require all of these outputs.
Output Description
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.
Hexadecimal (Intel-Format) File (.hex) • Contains initialization information for on-chip
memories.
• The Nios II SBT for Eclipse generate these initialization
files for on-chip memories that support initialization of
contents.
Send Feedback
AN-717
2014.09.22 Running and Debugging Software on the Target Board 7
Output Description
Flash memory programming data • Boot code and other arbitrary data you might write to
flash memory.
• The flash programmer adds appropriate boot code to
allow your program to boot from flash memory.
• The Nios II SBT for Eclipse includes a flash programmer
to allow you to write your program or arbitrary data to
flash memory.
This tutorial focuses on downloading only the .elf directly to the Nios II system.
Send Feedback
AN-717
8 Creating the Design Example 2014.09.22
Send Feedback
AN-717
2014.09.22 Start the Quartus II Software and Open the Example Project 9
Related Information
Embedded Peripheral IP User Guide
Provides more information about the JTAG UART, timer, system ID peripheral, and PIO.
Send Feedback
AN-717
10 Specify Target FPGA and Clock Settings 2014.09.22
The Qsys design process does not need to be linear. The design steps in this tutorial are presented in the
most straightforward order for a new user to understand. However, you can perform Qsys design steps in
a different order.
Send Feedback
AN-717
2014.09.22 Add the JTAG UART 11
balanced trade-off between performance and resource utilization. To add a Nios II/f core to the system,
perform the following steps:
1. On the IP Catalog tab, expand Processors and Peripherals, and then click Nios II Gen2 Processor.
2. Click Add.
The Nios II Processor parameter editor appears, displaying the Core Nios II tab.
3. In the Main Tab under Select an Implementation, select Nios II/f.
4. Click Finish and return to the Qsys System Contents tab.
The Nios II core instance appears in the system contents table. Ignore the exception and reset vector
error messages. You resolve these errors in future steps.
5. In the Name column, right-click the Nios II processor and click Rename.
6. Type cpu and press Enter.
7. In the Connections column, connect the clk port of the clk_0 clock source to both the clk1 port of
the on-chip memory and the clk port of the Nios II processor by clicking the hollow dots on the
connection line. The dots become solid indicating the ports are connected.
8. Connect the clk_reset port of the clk_0 clock source to both the reset1 port of the on-chip memory
and the reset_n port of the Nios II processor.
9. Connect the s1 port of the on-chip memory to both the data_master port and instruction_master
port of the Nios II processor.
10.Double-click the Nios II processor row of the system contents table to reopen the Nios II Processor
parameter editor.
11.Under Reset Vector in Vectors tab, select onchip_mem.s1 in the Reset vector memory list and type
0x0 in the Reset vector offset box.
12. Under Exception Vector, select onchip_mem.s1 in the Exception vector memory list and type 0x20
in the Exception vector offset box.
13.Click the Caches and Memory Interfaces tab.
14.In the Instruction cache list, select 2 Kbytes.
15.Choose None for Data Cache size and do not change other default settings.
16.In Advanced Features tab, select Static branch prediction type.
17.Click Finish. You will return to the Qsys System Contents tab.
Do not change any settings on the MMU and MPU Settings and JTAG Debug tabs.
Send Feedback
AN-717
12 Add the Interval Timer 2014.09.22
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.
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.
Related Information
Embedded Peripheral IP User Guide
Provides more information about the JTAG UART, timer, system ID peripheral, and PIO.
Related Information
Embedded Peripheral IP User Guide
Provides more information about the JTAG UART, timer, system ID peripheral, and PIO.
Send Feedback
AN-717
2014.09.22 Add the PIO 13
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.
Related Information
Embedded Peripheral IP User Guide
Provides more information about the JTAG UART, timer, system ID peripheral, and PIO.
Related Information
Embedded Peripheral IP User Guide
Provides more information about the JTAG UART, timer, system ID peripheral, and PIO.
Send Feedback
AN-717
14 Generate the Qsys System 2014.09.22
Qsys provides the Assign Base Addresses command which makes assigning component base addresses
easy. For many systems, including this design example, Assign Base Addresses is adequate. However, you
can adjust the base addresses to suit your needs. Below are some guidelines for assigning base addresses:
• Nios II processor cores can address a 31-bit address span. You must assign base address between
0x00000000 and 0x7FFFFFFF.
Note: The Use most-significant address bit in processor to bypass data cache option is enable by
default. If disabled, the Nios II processor cores supports full 32-bit address.
• Nios II programs use symbolic constants to refer to addresses. You do not have to choose address
values that are easy to remember.
• Address values that differentiate components with only a one-bit address difference produce more
efficient hardware. You do not have to compact all base addresses into the smallest possible address
range, because this can create less efficient hardware.
• Qsys does not attempt to align separate memory components in a contiguous memory range. For
example, if you want an on-chip RAM and an off-chip RAM to be addressable as one contiguous
memory range, you must explicitly assign base addresses.
Qsys also provides an Assign Interrupt Numbers command which connects IRQ signals to produce valid
hardware results. However, assigning IRQs effectively requires an understanding of how software
responds to them. Because Qsys does not know the software behavior, Qsys cannot make educated
guesses about the best IRQ assignment.
The Nios II HAL interprets low IRQ values as higher priority. The timer component must have the
highest IRQ priority to maintain the accuracy of the system clock tick.
To assign appropriate base addresses and IRQs, perform the following steps:
1. On the System menu, click Assign Base Addresses to make Qsys assign functional base addresses to
each component in the system. Values in the Base and End columns might change, reflecting the
addresses that Qsys reassigned.
2. In the IRQ column, connect the Nios II processor to the JTAG UART and interval timer.
3. Click the IRQ value for the jtag_uart component to select it.
4. Type 16 and press Enter to assign a new IRQ value.
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.
Send Feedback
AN-717
2014.09.22 Integrate the Qsys System into the Quartus II Project 15
The Generate dialog box appears and system generation process begins. The generation process can
take several minutes. When generation completes, Qsys will prompt: Create HDL design
files for synthesis.
5. Click Close to close the dialog box.
6. On the File menu, click Exit to close Qsys and return to the Quartus II software.
You are ready to integrate the system into the Quartus II hardware project and use the Nios II SBT for
Eclipse to develop software.
Send Feedback
AN-717
16 Assign FPGA Device 2014.09.22
Send Feedback
AN-717
2014.09.22 Set Timing 17
Related Information
Altera Development Kits Documentation
Set Timing
To ensure the design meets timing, perform the following steps:
1. On the File menu, click Open.
2. In the Files of type list, select Script Files (*.tcl, *.sdc, *.qip).
3. Browse to locate <design files directory>/hw_dev_tutorial.sdc and click Open. The file opens in the text
editor.
4. Locate the following create_clock command:create_clock -name sopc_clk -period
20 [get_ports PLD_CLOCKINPUT]
5. Change the period setting from 20 to the clock period (1/frequency) in nanoseconds of the oscillator
driving the clock pin.
6. On the File menu, click Save.
7. On the Assignments menu, click Settings.
The Settings dialog box appears.
8. Under Category, click TimeQuest Timing Analyzer.
9. Next to File name, click the browse (...) button.
10.Browse to locate <design files directory>/hw_dev_tutorial.sdc and click Open to select the file.
11.Click Add to include hw_dev_tutorial.sdc in the project.
12.Turn on Enable multicorner timing analysis during compilation.
13.Click OK.
Send Feedback
AN-717
18 Download the Hardware Design to the Target FPGA 2014.09.22
The Tasks window and percentage and time counters in the lower-right corner display progress. The
compilation process can take several minutes. When compilation completes, a dialog box displays the
message "Full Compilation was successful."
2. Click OK. The Quartus II software displays the Compilation Report tab.
3. Expand the TimeQuest Timing Analyzer category in the compilation report.
4. Click Multicorner Timing Analysis Summary.
5. Verify that the Worst-case Slack values are positive numbers for Setup, Hold, Recovery, and
Removal.
If any of these values are negative, the design might not operate properly in hardware. To meet timing,
adjust Quartus II assignments to optimize fitting, or reduce the oscillator frequency driving the FPGA.
Related Information
Define the System in Qsys on page 9
Send Feedback
AN-717
2014.09.22 Create a New Nios II Application and BSP from Template 19
Send Feedback
AN-717
20 Run the Program on Target Hardware 2014.09.22
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.
Related Information
Integrate the Qsys System into the Quartus II Project on page 15
Send Feedback