Upload
others
View
4
Download
0
Embed Size (px)
Citation preview
1
June 2016
Version 1.7
The Nios II Embedded “Hello World” Lab:
For the DE0-CV Development Kit
2
Revision History
1.0 5/1/2015 L. Landis Initial Release
1.1 6/2/2015 L. Landis Added BeMicro
1.2 11/30/2015 I. Rush Added CVE DevKit
1.3 12/2/2015 S Meer Consolidated Sections
1.4 12/4/2015 I.Rush Updated Pinout Table
1.5 3/18/2016 K. Kita Separated Lab by Board
1.6 5/10/2016 J. Xia Revised for university
workshops
1.7 6/6/2016 P. Mayer Added scrolling text
3
Contents Overview ....................................................................................................................................................... 4
Lab Notes ...................................................................................................................................................... 4
Quartus Installation ....................................................................................................................................... 4
Design Flow .................................................................................................................................................. 5
Objective of the “Hello World” lab .............................................................................................................. 6
HARDWARE DESIGN ................................................................................................................................ 8
Get started with Quartus ............................................................................................................................... 8
Building your Qsys based processor system ............................................................................................... 13
Building the Top Level Design ................................................................................................................... 27
SOFTWARE DESIGN ............................................................................................................................... 35
Creating the Software for the “Hello World” design .................................................................................. 35
Downloading the Hardware image to the DE0-CV .................................................................................... 41
Using the Seven-Segment display: ............................................................................................................. 47
Lab Summary .............................................................................................................................................. 52
Appendix A: Hardware Pinout DE0-CV .................................................................................................... 53
4
Overview This lab teaches you how to create an embedded system implemented in programmable logic using
Altera’s “soft” Nios II processor. A soft processor such as the Nios II, available in Altera’s FPGA
families, have built in programmable logic fabric and can be easily modified to suit an applications’
requirements. Altera’s “SoC FPGA” families are hard processor built from “hard” standard cells that
cannot be changed without redesigning the chip. The Nios II processor is supported by a rich set of
peripherals and “IP” blocks built that can be configured and connected to the processor using Altera’s
QSys tool within the Quartus II design tool set. Altera also distributes the Nios II Software Build Tools
(SBT) for Eclipse (for software development) within the Quartus development suite.
The lab is organized to run on a number of Altera development kits. The links to the other kits can be
found in the Design Store as a Design Example and type in “hello” in the search bar. This lab will show
you how to install the Development kit pin settings, design the processor-based hardware system,
download it to the Development Kit, and run a simple “Hello World” software program which displays
text on your terminal. The initial section of the lab is split into a Hardware Section and Software Section.
You can skip the hardware section and move directly to the software section should you choose.
Refer to Appendix A in the Design Example for development kit specific pinout names and configuration
of on-chip memory.
Lab Notes The lab will require you to choose files, components, and other objects; they must be spelled exactly as
directed. This is necessary for consistency and to ensure that each step works properly in the lab, when
creating your own systems you can choose your own names as long as you use them consistently in your
project. The directory paths shown in the figures are for Linux (forward slash directory delimiter). If you
are using Windows, the paths will be shown with backslash directory delimiters.
Quartus Installation Quartus is Altera’s design tool suite. It serves a number of functions:
1. Design creation through the use of HDL languages or schematics
2. System creation through the Qsys graphical interface
3. Generation and editing of constraints: timing, pin locations, physical location on die, IO voltage
levels
4. Synthesis of high level language into an FPGA netlist (“mapping” in FPGA terminology)
5. FPGA place and route (“fitting” in FPGA terminology)
a. Generation of design image (used to program FPGA, “assembly” in FPGA terminology)
6. Timing Analysis
7. Programming/download of design image into FPGA hardware
8. Debugging by insertion of debug logic (in-chip logic analyzer)
9. Interfaces to 3rd party tools such as simulators
10. Launching of Software Build Tools (Eclipse) for Nios II
5
To download Quartus, follow these instructions:
Visit this site: http://dl.altera.com/?edition=web to download version 16.0 of Quartus II.
Select version 16.0 and your PC’s operating system.
For the smallest installation, and quickest download time, only select the technology family you are using
based on your development board.
Figure 1: Quartus download page
Follow the download instructions provided from the web page. No license is required to run the Quartus
Lite software.
Design Flow Unlike system development with hard processors, development with soft processors enables you to
optimize the processor system to your application requirements and use the FPGA to add the performance
and interfaces required by your system. This means that you need to know how to modify the processor
system hardware; this may sound challenging but thanks to the Qsys graphical system design tool this is
actually a relatively easy thing to do as we will demonstrate in this lab.
The Qsys design flow diagram below illustrates how an overall system is integrated using the
combination of the Qsys system integration tool, Quartus for mapping (FPGA terminology for synthesis),
6
fitting (FPGA terminology for place and route), and the NIOS Software Build Tool (SBT) for software
development.
Figure 2: Qsys Development Flow
The above diagram depicts the typical flow for Nios II system design. Hardware System definition is
performed using Qsys; the resultant HDL files from the Qsys system are used by the Quartus II FPGA
design software to map, fit and download the hardware image into the FPGA device. Quartus II also
generates information that describes the configuration of the system designed in Qsys so that the Nios II
SBT can be configured to create a software library that matches the hardware system and contains all the
correct peripheral drivers.
Objective of the “Hello World” lab This lab demonstrates how to use Qsys to design the hardware and software to print “Hello World” to
your screen. This requires a working processor to execute the code, on-chip memory to store the software
executable, and a JTAG UART peripheral to send the “Hello World” text to a terminal. To make the lab a
little bit more interesting and hardware-centric, we will utilize the push button switches and LEDs to
allow interaction with the development kit. We will use connections to memory that the processor can
access to map the various switches and buttons on the device to the LEDs and seven-segment displayC16.
Note that in the appendix section, there are a number of more advanced modules including how to
connect to off-chip memory, utilizing interrupts and for some boards displaying hello world on an LCD
character display as well as on your host computer.
The lab hardware is constructed with the components shown below. Altera utilizes the Qsys network on
chip interconnect to connect the master and slave devices together. To get a clear understanding of how
quickly one can build an Embedded System using Qsys and the Quartus Design Software, you will build
the Nios II system entirely from scratch.
7
Figure 3: Nios II based system used in this lab
8
HARDWARE DESIGN
Get started with Quartus Now you are ready to get started designing hardware! Launch Quartus by double clicking the Quartus
icon:
Next you will launch the New Project Wizard from Quartus from the main panel or alternatively File
New Project Wizard.
Figure 4: Quartus Main Panel
9
Fill in the New Project Wizard first panel with your working directory and project which we will call
hello_world_lab.
Figure 5: New Project Wizard first panel
Click next and select Empty project and click next.
10
Figure 6: New Project Wizard second panel
11
Click next again. There are no files we need to add at this point. Then select your device.
Table 1: Board Name
Board Menu Selection
DE0-CV 5CEBA4F23C7
MAX 10 FPGA DevKit 10M50DAF484C7G
MAX 10 BeMicro Kit 10M08DAF484C8GES
MAX 10 DECA Kit 10M50DAF484D6GES
Cyclone V E FPGA 5CEFA7F31I7
Figure 7: New Project Wizard device selection panel
12
Click next and next again until you finish the New Project Wizard.
Figure 8: New Project Wizard Summary panel
13
Building your Qsys based processor system The Qsys system panel diagram illustrates what you are designing in the Qsys environment. This system
has a single master, the Nios II processor, and 9 slave devices. Building the Qsys system is a highly
efficient way of designing systems with or without a processor.
Launch Qsys from Quartus: Tools Qsys. The initial screen looks something like this:
Figure 9: Qsys main panel
Next, we will add the various components of the system and make the connections between them. By
default Qsys inserts a clock module. We will connect to this later on in the lab.
Below the IP catalog tab, you can search for the various components you want to add to your Qsys based
system. Enter Nios in the search tab and select the Nios II processor from the library.
14
A configuration window will appear, in this select the Nios II/e processor. This version of the Nios II
processor is resource optimized and will work well for the Hello World Lab implementation.
Figure 10: Nios II Gen2 Configuration panel
Click finish and you will see the Nios IIe processor in your connection diagram. For now don’t worry
about the system errors reported, we will address them soon.
Figure 11: Qsys System Contents panel
Qsys has a very elegant and efficient way of making connections by clicking on the nodes on ‘wires’ in
the connections panel on the 2nd
column from the left. You can add the connections as you add
15
components, but it’s often easier to make all the connections once you have finished adding the various
blocks. With the Nios II processor added, you still need to add the On Chip Memory, JTAG UART,
PUSHBUTTON, SWITCHES, LED, and HEX DISPLAY (7-segment display) to your system defined in
Error! Reference source not found..
Search for memory in the IP catalog. You will see many options for memory. It might be easiest to
detach the IP Catalog from the main panel by clicking on the detach window icon.
Figure 12: Detach window icon
Figure 13: IP Catalog search for on-chip memory
Locate the On-Chip Memory (RAM or ROM) component and click Add. You can leave all of the
default settings except that you need to change the memory size from 4096 to 16384. This will ensure that
you have a plenty of space for your software program. Uncheck initialize memory content. This feature
includes the software executable in the hardware image. For this lab, you will initialize the software
executable from Eclipse.
16
Figure 14: On-chip memory configuration panel
Click finish and you will now see a total of three components in your Qsys system: clock, Nios II
processor and on-chip memory.
17
Figure 15: System contents with Nios II and on-chip memory
The next component you will add is the JTAG UART. Search for JTAG in the IP catalog, locate the
JTAG UART and double click or add that component. Keep the default settings and click finish.
Figure 16: JTAG UART configuration panel
The next three components, which handle the interfacing of the switches, pushbuttons, and LEDs, are
actually configured instances of general purpose parallel IO components in the IP catalog. Search for
18
parallel IO (PIO) and select this block. By using the PIO block for the switches, buttons, and LEDs, you
will be able to map these values to address space and your C code will read and write these components.
For the pushbutton block, we will set this up as a 4-bit input using the settings shown below (as there are
four pushbuttons we would like to read from):
Figure 17: Parallel IO configuration panel for pushbutton
Next, you will add a second PIO block. This one will be for the switches that the user can press. For the
switch block, you will set this up as a 10 bit input interface using the settings shown below (since there
are 10 switches on the board). Comment [LL1]:
19
Figure 18: Parallel IO configuration panel for switches
Double click on the PIO component as you did for the SWITCH and BUTTON. This time you will
configure this component as the LED which is a 10 bit output (since there are 10 Red LEDs on the
board), shown below.
Figure 19: Parallel IO configuration panel for LED outputs
Comment [LL2]: You will need to make a new screen shot with the Width = 1 instead of 2.
20
Finally, we will add the six 7-segment displays that will allow us to display text on the board. Create
another PIO component, and configure it as a 7-bit output (since we need one bit for each light). This
configuration is shown below.
Figure 20: Parallel IO configuration panel for seven-segment display outputs
Click finish. You have completed adding the 8 components that make up your Qsys system. Next you will
rename the components in the design with names that are easy to remember.
In the system contents tab, right click on clk_0, select rename, and type in clk. Then, select the
nios2_gen_2_0 component, select rename and type in cpu, similarly rename the rest of the components:
onchip_memory, jtag_uart, button, switch, led, and hex0. This will make these components’ names
easy to remember and reference in future steps. When you finish, your system contents panel should look
like Figure 21: System content connections starting panel. It is important that your names match these
exactly, or your code later on may not compile!
21
Figure 21: System content connections starting panel
The next step consists of making the appropriate connections between the components within Qsys.
Highlight the clock output coming out of the clk pin by clicking on the text that says “clk” above
“clk_reset” description. When first selected, it will be a gray color. Make connections by clicking on the
small open circles on the lines that intersecting with the 7 other components.
You should see something similar to Figure 22: System contents after .
22
Figure 22: System contents after connecting the clock
Perform the same operation to connect the clk_reset to the resets on the other components.
Next, connect the nios2e data master to the slaves: Make the connections between the Nios2e data
master and the s1 connection of the onchip memory, avalon_jtag_slave on the uart, the s1 port on the
button, the s1 port on the switch, the s1 port of the led component, and the s1 port on the hex0 component
as shown below. Instruction master is by default connected to debug_mem_slave.
23
Figure 23: System contents after data master/slave connection
The instruction master signal from the nios2e component does not need to be connected to each slave
component as it only needs access to memory that contains the software executable. Make the connection
between the nios2e instruction master and the onchip_memory s1.
Figure 24: System contents after instruction master/slave connections
24
The next connections to make are the processor interrupt request (IRQ) signals. The UART can drive
interrupts, and hence needs to be wired to the nios2e processor interrupt lines. Make this connection as
shown in Figure 25: System contents after interrupt connections. We will use the default setting for the
IRQ number.
Figure 25: System contents after interrupt connections
Now that the connections are made, we need to add the other five seven-segment displays (as there are six
of them on the board). This can easily be done by selecting hex0, right-clicking, and selecting
“duplicate.” Alternatively, you can click on hex0 and press “ctrl-D” to duplicate the module. Once you
have six of them, rename the new ones so they form the following list (pictured below): hex0, hex1, hex2,
hex3, hex4, hex5. In case the connections were not kept when duplicating the new PIOs, be sure to
connect each “clk” to the clock signal (clk clk), “reset” to “clk_reset,” and “data_master” to “s1” for
all six of the hex PIOs. When all is said and done, your system contents panel should look like Figure 26:
System contents after adding all six 7-segment displays.
Figure 26: System contents after adding all six 7-segment displays
25
You have now completed the internal connections for this Nios II processor based system. The next step
is to make the external connections that connect the Qsys based system to the next higher level in the
hierarchy of your FPGA design, or to FPGA device pins that connect to the PCB. Double click on the
button, switch, led, and hex0-hex5 conduit items under the export column circled in Figure 27: System
contents after exporting PIO switch and LED. This will bring these ports out of the Qsys component to
connect to the top level design. Be sure the names of the components and exports match what is in Figure
27: System contents after exporting PIO switch and LED exactly, or the design may not compile at
runtime.
Figure 27: System contents after exporting PIO switch and LED
Next you will need to generate the base Addresses for your Qsys system. This is achieved by using
clicking on System Assign Base Addresses.
Save your Qsys system by using File Save As and pick a name for the Qsys system that you will
remember. Note that the lab figures call it nios_setup_v2 so to avoid confusion you should name your
.qsys file the same. The information is saved in what is called a .qsys file. Although you are not entirely
finished, it is good practice to save edits along the way.
You should see two error messages in the Message Console of Qsys.
Figure 28: Error message prior to assign memory location to execute from
These error messages have to do with the fact that the nios2e processor doesn’t know where the software
code that handles resets and exceptions is located. This is fairly straightforward to fix.
26
Double click on the cpu component and set the reset vector memory and exception vector memory
both to onchip_memory.s1. This will set the system to execute from onchip memory at these respective
locations upon reset or interrupt. The two errors that were shown in Figure 28: Error message prior to
assign memory location to execute from should now be resolved.
Figure 29: Assign vectors in the nios2e panel
Save your design once again. Note that by saving, you still have not generated the files that you need for
Quartus II compilation or with the Eclipse SBT. The step to complete this is to click on the button on the
lower right of Qsys.
Click on the button ‘Generate HDL’. Click Generate on the panel that appears. Then click Finish to exit
the Qsis window.
Congratulations! This completes the Qsys section of the lab.
27
Building the Top Level Design
The next step will involve adding your Verilog code. Should you want to use a schematic capture
graphical editor, jump to the Error! Reference source not found.. If you are familiar with VHDL, you
an make the same connections in VHDL, but you will have to change the design to VHDL on your own.
For ease of following along the lab document, we recommend continuing the lab in Verilog. It is
important to note that each development kit used has its own names for clocks, switches, pins and so you
will need to use the right names according to the development kit naming convention.
Quartus should be open, bring that to the front of your screen. Note that for this design there is a clock,
reset, push button inputs, switch inputs, LED outputs, six HEX outputs (the seven-segment displays), and
a JTAG UART. The JTAG UART pins are hard wired into the FPGA so you don’t need to add them in
your Verilog source file. The 4 pins: TCLK, TDI, TMS and TDO that constitute a 4 wire JTAG interface
are at a fixed location in your FPGA and they don’t need to be added to your Verilog source file. Only
pins that are synthesized from your RTL source code need to be specified.
Figure 30: Generating Qsys system HDL files
28
Figure 31: Block diagram of hello_world_lab design for the DE0_CV Development Kit
First, we need to write the top-level entity, which we will call hello_world_lab. This file contains the
main user logic and instantiates the Qsys nios_setup_v2 system that we generated in the last step. If you
already have this file, place it in your project directory. If not, in Quartus’s main window, create a new
file and paste the following code in that file (you’ll need to go to FileNew and select Verilog HDL File
under Design Files). Keep in mind that the clock, reset, push button, and LED pin names need to reflect
the names for the DE0_CV Development Kit. Make sure to rename these signals if you are using a
different board. Save this file as a Verilog file in your project directory and name it hello_world_lab.v.
`timescale 1 ps / 1 ps
module hello_world_lab (
//Reset and Clocks
input CLOCK_50, //2.5v
input CPU_RESETn, //3.3-v LVCMOS
input [3:0] KEY, //1.5v; the pushbuttons
input [9:0] SW, //For the switch
output [9:0] LEDR, //1.5v
output [6:0] HEX0,HEX1,HEX2,HEX3,HEX4,HEX5 //For the 7seg displays
);
//Instantiate Qsys based Nios system
//Software LED assignment
nios_setup_v2 u0 (
.clk_clk (CLOCK_50), //clk.clk
.led_external_connection_export (LEDR[9:0]), //led_external_connection.export
.reset_reset_n (CPU_RESETn), //reset.reset_n
.button_external_connection_export (KEY[3:0]), //button_external_connection.export
.switch_external_connection_export (SW[9:0]), //switch_external_connection.export
.hex0_external_connection_export (HEX0), //hex0_external_connection.export
.hex1_external_connection_export (HEX1), //hex1_external_connection.export
.hex2_external_connection_export (HEX2), //hex2_external_connection.export
.hex3_external_connection_export (HEX3), //hex3_external_connection.export
.hex4_external_connection_export (HEX4), //hex4_external_connection.export
.hex5_external_connection_export (HEX5) //hex5_external_connection.export
);
endmodule
In this example, we will connect the push button switches to the LEDs in software.
If you are wondering how to figure out how to hook up the nios_setup_v2 module, you can check
nios_setup_v2_inst.v, which was auto-generated from nios_setup_v2.qsys inside the nios_setup_v2
hello_world_lab
CLOCK_50
CPU_RESETn
KEY[3:0]
TCK
TMS
TDI
TDO
LEDR[9:0]
JTAG
SW[9:0]
HEX0[6:0]
HEX1[6:0]
HEX2[6:0]
HEX3[6:0]
HEX4[6:0]
HEX0[6:0]
Seven Segment Displays
29
directory of your project. Open this file and you see how to instantiate the Qsys system. The contents of
this file are shown below:
Figure 32: Contents of nios_setup_v2_inst.v
Next, we need to add the top-level entity to your project. In the Quartus main window, go to Project
Add/Remove Files in Project. With Files highlighted on the left pane, add hello_world_lab.v and the
nios_setup_v2.qip files. The nios_setup_v2.qip file should be found under nios_setup_v2/synthesis
directory in your project, and you will need to change the filter to display “all files” if you cannot see it.
The .qip file contains the information for the Nios II Qsys system that we created in the last step.
Figure 33: Add/Remove Files pane
30
Next you need to identify the top level entity as hello_world_lab. In the left pane of the settings window,
(found under Assignments Settings) select the General tab. Name the top level entity
“hello_world_lab.” Click OK when complete.
Figure 34: Settings pane
31
Now it is a good idea to make sure your Verilog is syntactically correct. Return to the main Quartus
window and select the Tasks pane. Double-click on the Play (right triangle) button to run
Analysis/Synthesis. You will get warnings but you should get no errors. If you do get an error, it’s likely
syntax (like a missing semicolon). Make changes, save, and continue to run Analysis/Synthesis until it
runs error-free (ignore dangling pin warning for now).
Figure 35: Task pane
32
Almost there! Now we need to add pin assignments for the clock, reset, push buttons, and LEDs. Refer to
the Appendices for specific instructions for your board. If you are using the DE0 CV, the following list
of TCL commands will set up the pins for you so you do not have to manually set dozens of pins using
the pin planner. To use the following script, first open the TCL Console by going to View Utility
WindowsTCL Console (or using the shortcut “Alt-2”). A new window should appear labeled “Quartus
Prime Tcl Console” Copy the list of commands below and paste them into this new window and press
enter. These commands handle routing the pins and voltage levels so they can be easily transferred
between projects that use the same board. For large projects with hundreds of I/Os, it can save a lot of
time to have a script like this.
set_location_assignment PIN_P22 -to CPU_RESETn
set_location_assignment PIN_M9 -to CLOCK_50
set_location_assignment PIN_U21 -to HEX0[0]
set_location_assignment PIN_V21 -to HEX0[1]
set_location_assignment PIN_W22 -to HEX0[2]
set_location_assignment PIN_W21 -to HEX0[3]
set_location_assignment PIN_Y22 -to HEX0[4]
set_location_assignment PIN_Y21 -to HEX0[5]
set_location_assignment PIN_AA22 -to HEX0[6]
set_location_assignment PIN_AA20 -to HEX1[0]
set_location_assignment PIN_AB20 -to HEX1[1]
set_location_assignment PIN_AA19 -to HEX1[2]
set_location_assignment PIN_AA18 -to HEX1[3]
set_location_assignment PIN_AB18 -to HEX1[4]
set_location_assignment PIN_AA17 -to HEX1[5]
set_location_assignment PIN_U22 -to HEX1[6]
set_location_assignment PIN_Y19 -to HEX2[0]
set_location_assignment PIN_AB17 -to HEX2[1]
set_location_assignment PIN_AA10 -to HEX2[2]
set_location_assignment PIN_Y14 -to HEX2[3]
set_location_assignment PIN_V14 -to HEX2[4]
set_location_assignment PIN_AB22 -to HEX2[5]
set_location_assignment PIN_AB21 -to HEX2[6]
set_location_assignment PIN_Y16 -to HEX3[0]
set_location_assignment PIN_W16 -to HEX3[1]
set_location_assignment PIN_Y17 -to HEX3[2]
set_location_assignment PIN_V16 -to HEX3[3]
set_location_assignment PIN_U17 -to HEX3[4]
set_location_assignment PIN_V18 -to HEX3[5]
set_location_assignment PIN_V19 -to HEX3[6]
set_location_assignment PIN_U20 -to HEX4[0]
set_location_assignment PIN_Y20 -to HEX4[1]
set_location_assignment PIN_V20 -to HEX4[2]
set_location_assignment PIN_U16 -to HEX4[3]
set_location_assignment PIN_U15 -to HEX4[4]
set_location_assignment PIN_Y15 -to HEX4[5]
set_location_assignment PIN_P9 -to HEX4[6]
set_location_assignment PIN_N9 -to HEX5[0]
set_location_assignment PIN_M8 -to HEX5[1]
set_location_assignment PIN_T14 -to HEX5[2]
set_location_assignment PIN_P14 -to HEX5[3]
set_location_assignment PIN_C1 -to HEX5[4]
set_location_assignment PIN_C2 -to HEX5[5]
set_location_assignment PIN_W19 -to HEX5[6]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX0[0]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX0[1]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX0[2]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX0[3]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX0[4]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX0[5]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX0[6]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX1[0]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX1[1]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX1[2]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX1[3]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX1[4]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX1[5]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX1[6]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX2[0]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX2[1]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX2[2]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX2[3]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX2[4]
33
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX2[5]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX2[6]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX3[0]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX3[1]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX3[2]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX3[3]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX3[4]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX3[5]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX3[6]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX4[0]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX4[1]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX4[2]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX4[3]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX4[4]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX4[5]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX4[6]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX5[0]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX5[1]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX5[2]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX5[3]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX5[4]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX5[5]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX5[6]
set_location_assignment PIN_L1 -to LEDR[9]
set_location_assignment PIN_L2 -to LEDR[8]
set_location_assignment PIN_U1 -to LEDR[7]
set_location_assignment PIN_U2 -to LEDR[6]
set_location_assignment PIN_N1 -to LEDR[5]
set_location_assignment PIN_N2 -to LEDR[4]
set_location_assignment PIN_Y3 -to LEDR[3]
set_location_assignment PIN_W2 -to LEDR[2]
set_location_assignment PIN_AA1 -to LEDR[1]
set_location_assignment PIN_AA2 -to LEDR[0]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to LEDR[0]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to LEDR[1]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to LEDR[2]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to LEDR[3]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to LEDR[4]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to LEDR[5]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to LEDR[6]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to LEDR[7]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to LEDR[8]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to LEDR[9]
set_location_assignment PIN_M6 -to KEY[3]
set_location_assignment PIN_M7 -to KEY[2]
set_location_assignment PIN_W9 -to KEY[1]
set_location_assignment PIN_U7 -to KEY[0]
set_location_assignment PIN_AB12 -to SW[9]
set_location_assignment PIN_AB13 -to SW[8]
set_location_assignment PIN_AA14 -to SW[6]
set_location_assignment PIN_AA13 -to SW[7]
set_location_assignment PIN_AB15 -to SW[5]
set_location_assignment PIN_AA15 -to SW[4]
set_location_assignment PIN_T12 -to SW[3]
set_location_assignment PIN_T13 -to SW[2]
set_location_assignment PIN_V13 -to SW[1]
set_location_assignment PIN_U13 -to SW[0]
After you have finished entering this script, you can sanity check that the assignments are correctly added
by opening Assignments Assignment Editor.
34
Figure 36: Assignment Editor
Now you can compile your design which will run Analysis/Synthesis, Fitter (place and route in FPGA
terminology), Assembler (generate programming image) and TimeQuest (the static timing analyzer). This
can be achieved by clicking on the play button as shown in Figure 37: Compilation button.
Figure 37: Compilation button
Note that some warnings and information messages come up in the bottom window. You can filter by
message level. The errors are filtered with the button, critical warnings with the button,
warnings with the button, and informational messages with the button. You cannot proceed
if you have errors. In this case there are only critical and standard warnings, primarily because we did not
add timing constraints to this project. Due to the simplicity of this design and low frequency, it’s okay to
start without timing constraints. Consult other Altera online training courses for instructions on how to
add timing constraints to your design. If you receive errors in compilation here, make sure the device
selected is 5CEBA4F23C7 and not the 5CEBA4F23C8, which is what Quartus will AutoDetect if you
remove the device or enter it incorrectly when creating the project.
Figure 38: Filter for critical warnings
Congratulations, your FPGA hardware design is now complete. Now we will create software that will
run on the board and take advantage of the Nios2 processor that we just configured.
35
SOFTWARE DESIGN
Creating the Software for the “Hello World” design Should you choose to start directly in the Software Design section and skip the Hardware Design section,
consult with your lab facilitator to get these two files: nios_setup_v2.sopcinfo and hello_world_lab.sof
as if you generated them from the Hardware Design lab. You will be able to complete all subsequent steps
with these two files.
The NIOS Software Build Tools for Eclipse are included as part of Quartus. These tools will help manage
creation of the application software and Board Support Package (BSP). Launch the SBT Tools NIOS II
Software Build Tools for Eclipse (Tools -> Nios II Software build tools for Eclipse). You can use the
default location that Eclipse picks for you.
Figure 39: Initial Workspace setup
36
Click OK in the Workspace launcher.
Next, the Eclipse SBT will launch. Right click in the area called Project Explorer and select NewNios II
Application and BSP from Template. The BSP is the “Board Support Package” that contains the drivers
for things like translating printf C commands to the appropriate instructions to write to the terminal.
Figure 40: Creating the initial project in the Eclipse SBT
37
Next you will see a panel that requests information to setup your design. First, you need to navigate to
your working directory and click on the .sopcinfo file. The .sopcinfo file informs Eclipse on what your
Qsys system contains. Click OK.
Figure 41: Navigating to correct .sopinfo file
38
Fill in the Project name. Call it hello_world_sw. Next you will be asked to pick a template design. The
Hello World Small is a software application template that writes “Hello from Nios II” to the screen. Click
Finish. Note: make sure to pick Hello World Small and not Hello World or you will not have
enough memory in your FPGA design to store the program executable.
Figure 42: Completing the Nios II Software Examples setup screen with project name and project template
We will now make some modifications to the code to display the results of the pushbuttons (KEY3-0) on
the rightmost LEDs (LED3-0). Click the right arrow next to hello_world_sw. It will show the contents of
your project. Double-click hello_world_small.c. Note the command alt_putstr to write text to the
terminal. This is part of the Altera HAL (Hardware Abstraction Layer) set of software functions. Note
that the alt_putstr command is used versus a standard C printf function because the code space is more
compact using the HAL commands. Code using HAL functions without an operating system is referred
to as “bare metal” programming. A complete list of these functions can be found in the Nios II Software
Developer’s Handbook: https://www.altera.com/en_US/pdfs/literature/hb/nios2/n2sw_nii5v2.pdf .
39
Figure 43: Window of hello_world_small.c
Next you need to add a library declaration, define integer switch_datain, and a few HAL functions to
connect the LEDs to the Push Buttons:
#include <sys/alt_stdio.h>
#include <stdio.h>
#include "altera_avalon_pio_regs.h"
#include "system.h"
int main()
{
int switch_datain;
alt_putstr("Hello from Nios II!\n");
/* Event loop never exits. Read the PB, display on the LED */
while (1){
//Gets the data from the pb, recall that a 0 means the button is pressed
switch_datain = ~IORD_ALTERA_AVALON_PIO_DATA(BUTTON_BASE);
//Mask the bits so the leftmost LEDs are off (we only care about LED3-0)
switch_datain &= (0b0000001111);
//Send the data to the LED
IOWR_ALTERA_AVALON_PIO_DATA(LED_BASE,switch_datain);
}
return 0;
}
Comment [LL3]: Better comments in this code to clarify the IORD_ALTERA_AVALON_PIO_DATA and OIWR_ALTERA_AVALON_PIO_DATA functions.
40
Note the use of the variables BUTTON _BASE and LED_BASE. These variables are created by
importing the information from the .sopcinfo file. You can find defined variables in the system.h file
under the hello_world_sw_bsp project. Double click on system.h file and inspect the defined variable
names for BUTTON_BASE and LED_BASE. These must match your hello_world_small.c code. Edit the
hello_world_small.c source code so that it matches the code shown above. Click the save icon.
The code may appear somewhat cryptic, so we will now take the time to explain what the various lines
do. IOWR_ALTERA_AVALON_PIO_DATA(Location)gets the data from the specified Location
(recall that the list of locations is given in the system.h file under the hello_world_sw_bsp folder) and
reads it into a variable. Calling the function with two parameters, as in
IOWR_ALTERA_AVALON_PIO_DATA(Location, Value) writes the numeric Value to the given
Location. Notice how we are using this function to read the data from the switches and then write this
value to LEDs.
Now that we have written our code, right click on the hello_world_sw project and click on Build Project.
This compiles the software application and the BSP (drivers).
Figure 44: Launching the build
41
Once the build completes, you should observe an “.elf” file (executable load file) under the
hello_world_sw project. If the .elf file does not exist, the project did not build properly. Inspect the
problems tab on the bottom of the Eclipse SBT and determine if there are syntax problems, correct, and
rerun Build Project. Typical problems can be missing semicolons, mismatched brackets and such.
Figure 45: The presence of hello_world_sw.elf indicates the software build ran successfully
Downloading the Hardware image to the DE0-CV To work with the DE0-CV in the context of this lab, you will need to connect a USB cable connecting the
kit to a host PC. The USB blaster utilizes circuitry that formats the image into a data stream that
downloads from the PC to FPGA.
Make sure your development kit is powered up and LEDs are on. SW10 positioned on the lower left
corner of the board must be set to Run mode.
42
Figure 46: DE0-CV Board
If you are only performing the Software design lab, you must first launch Quartus. If you have just
performed the Hardware Design lab, then Quartus should already be open.
43
Launch the Programmer: Tools Programmer. Click Auto Detect and you should see something similar
to below in Figure 47: Programmer panel.
Figure 47: Programmer panel
44
Next, you need to download what is called a “.sof” file or SRAM object file. This is the programming
image file that gets downloaded in the FPGA. The default location is <working_directory>/output_files.
Right click on the first row <none> under File and click on Change File. Navigate to the output_files
directory and select hello_world_lab.sof. Click Open. In the first row under Program/Configure click
in the check box as shown in Figure 48: Programmer panel with Program/Configure checkbox.
Figure 48: Programmer panel with Program/Configure checkbox
45
Click Start. When programming is complete, the Progress meter should read 100% (Successful).
Figure 49: Programmer progress successful
46
Now it is time to download the .elf (software executable) into the Nios IIe processor. Return to the
Eclipse SBT tools. Right click on hello_world_sw and select Run as Run Nios II Hardware. A
window should appear as below. Then click on the Target Connection tab. The connection should
indicate that Eclipse has connected to the USB-blaster. If the connection is not identified, you can Click
Refresh Connections. Note that you might need to stretch the window wider to see the Refresh
Connections button. Once the connection is made to the USB-Blaster, you should observe something
similar to Figure 50: Eclipse SBT tools after connection is made to the USB-Blaster. Click Run. If the
run button is greyed out but your device shows up under the connections window, you may need to select
“Ignore mismatched system ID” and “Ignore mismatched system timestamp.”
Figure 50: Eclipse SBT tools after connection is made to the USB-Blaster
47
Now you have hardware and software downloaded into your board. You should observe “Hello from Nios
II!” printed on the Nios II Console tab.
Figure 51: "Hello from Nios II!" on Nios II Console tab
You can also test the connections between push button and LEDs. Push buttons 0-3 should now turn
LEDs 0-3 on when pressed.
Using the Seven-Segment display: One of the nice things about the Nios2 processor is that since we have already designed the hardware, we
can now change the software without having to reprogram the board. We will now program the Nios2
processor to display text on the seven-segment displays and make pushbuttons speed up and slow down
the text. Replace the code in hello_world_small.c with the following: #include <sys/alt_stdio.h>
#include <stdio.h>
#include "altera_avalon_pio_regs.h"
#include "system.h"
#define CHARLIM 256 //Maximum character length of what the user places in memory. Increase to allow longer sequences
#define CLOCKINIT 50005 //Initial speed of the display. This is a good starting point
#define UPDATECLOCKAMT 10000 //Incremental amount the display updates by when speeding up or slowing down
void initializeDisplay();
void updateText();
char getTxt(char curr);
int getActualText();
void clearActualText();
int updateTimer(int tmr, int speedup_tog, int slowdown_tog);
int updateLocation(int loc, int len);
void updatePBState(int button_datain);
int getBinLowLow(char letter);
int getBinLow(char letter);
int getBin(char letter);
int getBinHigh(char letter);
int getBinHighHigh(char letter);
int getBinaryLetter(char letter);
void print(int let5, int let4, int let3, int let2, int let1, int let0);
void print_letters(char let5, char let4, char let3, char let2, char let1, char let0, int state);
char prevLetter; //The last letter the user entered, used for determining whether or not the display has been updated
char enteredText[CHARLIM]; //The text that the user enters
char text[2*CHARLIM];//The text that has been adjusted for the allowed letters
int pause, reverse, reverse_toggle, speedup_toggle, slowdown_toggle, dance_state, dancing, dance_dir, upsideDown, length,
re_enter, disp_off;
48
int main(){
int button_datain, lights; //Values for the pushbuttons, LEDs, and switches, respectively
int switch_datain = IORD_ALTERA_AVALON_PIO_DATA(SWITCH_BASE); //Read the data from the switches
int location = 0; //Keeps track of where we are in the display, used for scrolling the text
int timer = CLOCKINIT; //Standard speed for movement
initializeDisplay();
/* Event loop never exits.*/
while (1){
//Update the display; delay so the letters stay on the screen (hence the loop)
for(int i = 0; i< timer; i++){
button_datain = IORD_ALTERA_AVALON_PIO_DATA(BUTTON_BASE); // Read the PB
switch_datain = IORD_ALTERA_AVALON_PIO_DATA(SWITCH_BASE); //Read in the switch values
//The rightmost lights reflect the pushbuttons and whether pause, reverse, or dancing have been toggled
lights = (~button_datain & 0b000001111) | ((dancing & 0b000000001) << 2) | ((reverse & 0b0000000001) << 2);
lights = ((lights & 0b0000001111) | (switch_datain & 0b1111110000));
IOWR_ALTERA_AVALON_PIO_DATA(LED_BASE, lights); //Update the LEDs with the values from the pushbuttons and
the switches
updatePBState(button_datain); //Apply the user's button input to change the logic of the display
}
pause = (switch_datain & 0b1000000000) >> 9; //Chooses whether or not we are paused
dance_dir = (switch_datain & 0b0100000000) >> 8; //Chooses direction of the dancing
upsideDown = (switch_datain & 0b0010000000) >> 7; //Chooses direction of the letters
disp_off = (switch_datain & 0b0001000000) >> 6; //Chooses direction of the letters
updateText(); //Decides whether the user has put in a request to update the text on the display, and
handle accordingly
timer = updateTimer(timer, speedup_toggle, slowdown_toggle); //Deal with speeding up or slowing down the
display
location = updateLocation(location, length); //The amount we should shift each letter by based on the user input
//Print the characters out to the screen in the correct order, shifted by the amount. We modulo with the length of the text
so the display keeps wrapping around
print_letters(text[(location % length)], text[(location + 1) % length], text[(location + 2) % length], text[(location + 3) %
length], text[(location + 4) % length], text[(location + 5) % length], dance_state);
}
return 0;
}
//Does initial setup of display
void initializeDisplay(){
//These controls determine what functions the display is executing:
pause = 0;
reverse = 0;
upsideDown = 0;
//These controls handle the toggling of the switches; initially all of the toggled functions are off:
reverse_toggle = 0;
speedup_toggle = 0;
slowdown_toggle = 0;
dancing = 0;
re_enter = 0;
dance_state = 2; //Start with the letters in the middle
//First Turn all six of the seven segment displays off
print(getBin('!'), getBin('!'), getBin('!'), getBin('!'), getBin('!'), getBin('!'));
prevLetter = '!';
//Give the user instructions using the HAL commands
alt_putstr("Hello from Nios II!\n");
alt_putstr("Type what you would like to display and press ENTER\n");
prevLetter = getTxt(prevLetter); //Snag what the user put in
length = getActualText(); //Adjust for special characters such as 'W' or 'M'
alt_putstr("Got it! If you want to change the text, press KEY3\n");
}
//Updates the text from the console once the program is running
void updateText(){
if (re_enter){
alt_putstr("Put your new text into the console and press ENTER\n");
prevLetter = '!';
prevLetter = getTxt(prevLetter);
length = getActualText(); //Adjust for special characters such as 'W' or 'M'
if(length > 0)
text[length-1] = '\0'; //Get rid of any extra stuff at the end
alt_putstr("Got it! If you want to change the text, press KEY3\n");
re_enter = 0; //We don't want to retrigger the entering process until the user does
}
return;
}
//Gets the text the user placed on the console
char getTxt(char curr){
if(curr == '\n')
return curr;
int idx = 0; //Keep track of what we are adding
char newCurr = curr;
//Keep adding characters until we get to the end of the line
while (newCurr != '\n'){
enteredText[idx] = newCurr; //Add the next letter to the entered text register
idx ++;
newCurr = alt_getchar(); //Get the next character
}
length = idx;
return newCurr;
49
}
//Takes the user's input and only uses the allowed letters. Returns the length of the string entered
int getActualText(){
int idx = 0; //We need two indicies because the entered and actual text sequences need not be aligned
char currentLetter; //Keeps track of the character we are wanting to add
//Go through each letter in the entered text
for (int i = 0; i <= length; i++){
currentLetter = enteredText[i];
if (currentLetter > 96){
//Gets only the uppercase letter
currentLetter -= 32;
}
switch(currentLetter){
case 'M':
//We build the letter "M" from two "n's," so we need to change the index twice in the actual text
text[idx] = 'N';
text[idx + 1] = 'N';
idx += 2;
break;
case 'W':
//We build the letter "W" from two "v's," so we need to change the index twice in the actual text
text[idx] = 'V';
text[idx + 1] = 'V';
idx += 2;
break;
default:
//Copy the new letter into the actual text
text[idx] = currentLetter;
idx++;
}
}
return idx;
}
//This function clears the text on the display:
void clearActualText(){
for(int i = 0; i <= length; i++){
text[i] = '\0';
}
return;
}
//This function updates the timer based on whether the user has toggled a speedup or slowdown
int updateTimer(int tmr, int speedup_tog, int slowdown_tog){
if (speedup_tog){
//If the user wishes to speed the display up by pressing KEY0
if ((tmr - UPDATECLOCKAMT) > 0)//We don't want the timer to go negative
return tmr - UPDATECLOCKAMT;
}
else if (slowdown_tog)
//If the user wishes to slow the display down by pressing KEY1
return tmr + UPDATECLOCKAMT;
//Return the modified timer amount so we can update what is stored in the timer variable
return tmr;
}
//This function returns a new Location based on the previous one.
int updateLocation(int loc, int len){
//Move the display if we are unpaused
if (pause == 0){
dancing = 0;
dance_state = 2;
if (reverse == 0)
loc++; //Move the display forwards if the backwards button is NOT toggled (KEY2)
else
loc--; //Otherwise we go backwards if the user toggled the display to go backwards
}
//If we are paused, check to see if dancing has been pressed
else{
reverse = 0;
if (dancing) {
if (dance_dir){
//If the Dancing Direction switch is down (SW8), cycle through the states so
the letters move down
dance_state = (dance_state + 1) % 5;}
else{
//If the Dancing Direction switch is up (SW8), cycle through the states so
the letters move up
dance_state = (dance_state - 1);
if (dance_state < 0)
dance_state = dance_state + 5;
}
}
}
if (loc >= len){ //If we have reached the end of the string, reset the locator back to the beginning
return loc % len;
}
else if (loc < 0){ //If we have reached the end of the string backwards, we need to jump back the other way
return loc + len;
}
return loc;
}
//This function updates the logic based on the buttons pressed
void updatePBState(int button_datain){
//If the user wishes to speed up, press KEY0
if ((button_datain & 1) == 0 && speedup_toggle == 0){
50
speedup_toggle = 1;
}
else if ((button_datain & 1) != 0){
//So the user does not continuously toggle KEY0 when held down
speedup_toggle = 0;
}
//If the user wishes to slow down, KEY1
if ((button_datain & 2) == 0 && slowdown_toggle == 0){
slowdown_toggle = 1;
}
else if ((button_datain & 2) != 0){
//So the user does not continuously toggle KEY1 when held down
slowdown_toggle = 0;
}
//Reverse Button/Dancing Button (if paused), KEY2 (toggle)
if ((button_datain & 4) == 0 && reverse_toggle == 0){
dancing = ~dancing;
reverse_toggle = 1;
reverse = ~reverse;
}
else if ((button_datain & 4) != 0){
//So the user does not continuously toggle KEY2 when held down
reverse_toggle = 0;
}
//If the user wishes to re-enter text, KEY3
if ((button_datain & 8) == 0){
re_enter = 1;
}
}
//Gets the lowest number representation of the given letter
int getBinLowLow(char letter){
int letter_inv = ~getBinaryLetter(letter); //Get the integer representation of the letter; the inverting logic is
easier to deal with
int bit0 = (letter_inv & 0b0000001) << 3; //This logic deals with bit shifting so the letters appear higher on the
display
int res = (0b0000000) | bit0;
return ~res; //Reinvert the binary representation of the modified letter so the display reads the correct
representation
}
//Gets a lower integer representation of the given letter
int getBinLow(char letter){
int letter_inv = ~getBinaryLetter(letter); //Get the integer representation of the letter; inverting the binary makes
the logic easier
int bit0 = (letter_inv & 0b0000001) << 6; //This logic deals with bit shifting so the letters appear higher on the
display
int bit1 = (letter_inv & 0b0000010) << 1;
int bit5 = (letter_inv & 0b0100000) >> 1;
int bit7 = (letter_inv & 0b1000000) >> 3;
int res = (0b0000000) | bit0 | bit1 | bit5 | bit7;
return ~res; //Uninvert the binary representation so the display knows which lights to turn on
}
//Gets the binary representation of the character
int getBin(char letter){
/*Based on the character entered, we convert to binary so the 7-segment knows which lights to turn on.
The 7-segment has inverted logic so a 0 means the light is on and a 1 means the light is off.
The rightmost bit starts the index at HEX#[0], and the leftmost bit is HEX#[6], the pattern
for the 7-segment is shown in the DE0_C5 User Manual*/
switch(letter){
case '0':
return 0b1000000;
case '1':
return 0b1111001;
case '2':
return 0b0100100;
case '3':
return 0b0110000;
case '4':
return 0b0011001;
case '5':
return 0b0010010;
case '6':
return 0b0000010;
case '7':
return 0b1111000;
case '8':
return 0b0000000;
case '9':
return 0b0010000;
case 'A':
return 0b0001000;
case 'B'://Lowercase
return 0b0000011;
case 'C':
return 0b1000110;
case 'D'://Lowercase
return 0b0100001;
case 'E':
return 0b0000110;
case 'F':
return 0b0001110;
case 'G':
return 0b0010000;
case 'H':
return 0b0001001;
51
case 'I':
return 0b1111001;
case 'J':
return 0b1110001;
case 'K':
return 0b0001010;
case 'L':
return 0b1000111;
case 'N':
return 0b0101011;
case 'O':
return 0b1000000;
case 'P':
return 0b0001100;
case 'Q':
return 0b0011000;
case 'R'://Lowercase
return 0b0101111;
case 'S':
return 0b0010010;
case 'T':
return 0b0000111;
case 'U':
return 0b1000001;
case 'V':
return 0b1100011;
case 'X':
return 0b0011011;
case 'Y':
return 0b0010001;
case 'Z':
return 0b0100100;
default:
return 0b1111111;
}
}
//Gets the upper binary representation of the character
int getBinHigh(char letter){
int letter_inv = ~getBinaryLetter(letter); //Get the integer representation of the letter, the logic is easier to work
with inverted
int bit2 = (letter_inv & 0b0000100) >> 1 ; //This logic deals with bit shifting so the letters appear higher on the
display
int bit3 = (letter_inv & 0b0001000) << 3;
int bit4 = (letter_inv & 0b0010000) << 1 ;
int bit6 = (letter_inv & 0b1000000) >> 6;
int res = (0b0000000) | bit2 | bit3 | bit4 | bit6;
return ~res;//Uninvert the logic to get the correct representation
}
//Gets the highest number representation of the given letter
int getBinHighHigh(char letter){
int letter_inv = ~getBinaryLetter(letter); //Get the integer representation of the letter, it is easier to deal with
the inverted logic
int bit3 = (letter_inv & 0b0001000) >> 3; //This logic deals with bit shifting so the letters appear higher on the
display
int res = (0b0000000) | bit3;
return ~res; //Uninvert the logic to get the correct representation
}
//Returns the letter or the upsideDown version of the letter
int getBinaryLetter(char letter){
int let = getBin(letter);
if (upsideDown){
//If the user has toggled the upsideDown switch, (SW8), we can use bit shifting to invert the display
int letter_inv = ~let; //Inverting the binary makes the logic easier to deal with.
int bit0 = (letter_inv & 0b0000001) << 3;
int bit1 = (letter_inv & 0b0000010) << 1;
int bit2 = (letter_inv & 0b0000100) >> 1;
int bit3 = (letter_inv & 0b0001000) >> 3;
int bit4 = (letter_inv & 0b0010000) << 1;
int bit5 = (letter_inv & 0b0100000) >> 1;
int bit6 = (letter_inv & 0b1000000);
int full_inv = (0b0000000) | bit0 | bit1 | bit2 | bit3 | bit4 | bit5 | bit6;
return ~full_inv; //Invert the logic back again
}
//Otherwise, if the SW8 is in the rightside-up position, we can just display the letter
return let;
}
//Prints each of the letters out to the screen
void print(int let5, int let4, int let3, int let2, int let1, int let0){
//Takes the binary value for each letter and places it on each of the six 7-segment displays
IOWR_ALTERA_AVALON_PIO_DATA(HEX5_BASE, let5);
IOWR_ALTERA_AVALON_PIO_DATA(HEX4_BASE, let4);
IOWR_ALTERA_AVALON_PIO_DATA(HEX3_BASE, let3);
IOWR_ALTERA_AVALON_PIO_DATA(HEX2_BASE, let2);
IOWR_ALTERA_AVALON_PIO_DATA(HEX1_BASE, let1);
IOWR_ALTERA_AVALON_PIO_DATA(HEX0_BASE, let0);
return;
}
//Prints each of the letters out to the screen; takes into account the dancing letters
void print_letters(char let5, char let4, char let3, char let2, char let1, char let0, int state){
//If the display is "muted," don't show anything
if (disp_off){
print(getBin('!'), getBin('!'), getBin('!'), getBin('!'), getBin('!'), getBin('!'));
return;
}
//State refers to which dancing state the characters are in
52
switch (state){
//This is the case where the letters are shifted down by the maximum amount on the display
case 0:
IOWR_ALTERA_AVALON_PIO_DATA(HEX5_BASE, getBinLowLow(let5));
IOWR_ALTERA_AVALON_PIO_DATA(HEX4_BASE, getBinLowLow(let4));
IOWR_ALTERA_AVALON_PIO_DATA(HEX3_BASE, getBinLowLow(let3));
IOWR_ALTERA_AVALON_PIO_DATA(HEX2_BASE, getBinLowLow(let2));
IOWR_ALTERA_AVALON_PIO_DATA(HEX1_BASE, getBinLowLow(let1));
IOWR_ALTERA_AVALON_PIO_DATA(HEX0_BASE, getBinLowLow(let0));
return;
//This is the case where the letters are shifted down a bit on the display
case 1:
IOWR_ALTERA_AVALON_PIO_DATA(HEX5_BASE, getBinLow(let5));
IOWR_ALTERA_AVALON_PIO_DATA(HEX4_BASE, getBinLow(let4));
IOWR_ALTERA_AVALON_PIO_DATA(HEX3_BASE, getBinLow(let3));
IOWR_ALTERA_AVALON_PIO_DATA(HEX2_BASE, getBinLow(let2));
IOWR_ALTERA_AVALON_PIO_DATA(HEX1_BASE, getBinLow(let1));
IOWR_ALTERA_AVALON_PIO_DATA(HEX0_BASE, getBinLow(let0));
return;
//This is the "main" case, where the full letters are displayed on the display
case 2:
IOWR_ALTERA_AVALON_PIO_DATA(HEX5_BASE, getBinaryLetter(let5));
IOWR_ALTERA_AVALON_PIO_DATA(HEX4_BASE, getBinaryLetter(let4));
IOWR_ALTERA_AVALON_PIO_DATA(HEX3_BASE, getBinaryLetter(let3));
IOWR_ALTERA_AVALON_PIO_DATA(HEX2_BASE, getBinaryLetter(let2));
IOWR_ALTERA_AVALON_PIO_DATA(HEX1_BASE, getBinaryLetter(let1));
IOWR_ALTERA_AVALON_PIO_DATA(HEX0_BASE, getBinaryLetter(let0));
return;
//This is the case where the letters are shifted up a bit on the display
case 3:
IOWR_ALTERA_AVALON_PIO_DATA(HEX5_BASE, getBinHigh(let5));
IOWR_ALTERA_AVALON_PIO_DATA(HEX4_BASE, getBinHigh(let4));
IOWR_ALTERA_AVALON_PIO_DATA(HEX3_BASE, getBinHigh(let3));
IOWR_ALTERA_AVALON_PIO_DATA(HEX2_BASE, getBinHigh(let2));
IOWR_ALTERA_AVALON_PIO_DATA(HEX1_BASE, getBinHigh(let1));
IOWR_ALTERA_AVALON_PIO_DATA(HEX0_BASE, getBinHigh(let0));
return;
//This is the case where the letters are shifted by the maximum amount on the display
default:
IOWR_ALTERA_AVALON_PIO_DATA(HEX5_BASE, getBinHighHigh(let5));
IOWR_ALTERA_AVALON_PIO_DATA(HEX4_BASE, getBinHighHigh(let4));
IOWR_ALTERA_AVALON_PIO_DATA(HEX3_BASE, getBinHighHigh(let3));
IOWR_ALTERA_AVALON_PIO_DATA(HEX2_BASE, getBinHighHigh(let2));
IOWR_ALTERA_AVALON_PIO_DATA(HEX1_BASE, getBinHighHigh(let1));
IOWR_ALTERA_AVALON_PIO_DATA(HEX0_BASE, getBinHighHigh(let0));
return;
}
}
Right-click on the hello_world_sw folder in the project explorer on the right and select Run as Run
Nios II Hardware. This will run the C program on the Nios2 processor. Now a prompt should appear in
the console telling you to enter text. Type something like “Hello World” into the console and press
ENTER. The text should appear on the seven-segment display. Press KEY0 to speed up the display, and
press KEY1 to slow down the display (holding the button down also works). Press KEY2 to reverse the
direction of the text. While paused, KEY2 toggles cascading letters. If you ever want to change the text,
press KEY3 and enter your new text. SW9 pauses the display. SW8 controls the direction that the letters
cascade (so if SW9 is down, the letters will cascade down whereas if SW9 is up, the letters will cascade
up). SW7 controls the direction the letters face. If SW7 is down, the letters will be right-side up. If SW7
is up, the letters will be upside-down. SW6 mutes the display if pressed.
If you are fluent in C, try modifying the program to add functions for some of the other switches (SW5-
0). When modifying or writing your own program, the variable switch_datain is assigned the value of
the switches.
Lab Summary You now have completed the hardware and software sections of this lab. This includes:
1. Loading the Device Kit pin settings into Quartus
2. Using Qsys to build a Nios II based system
3. Instantiating the Qsys component into your top level design
4. Add some connections between push buttons and LEDs
5. Compiling your hardware
53
6. Importing the Nios II based system into the Eclipse Software Build Tools
7. Building a software project
8. Modifying a software template to perform some simple IO functions
9. Compiling your software
10. Downloading the hardware image into the development kit.
11. Downloading the software executable into the development kits.
12. Testing the hardware
There is a wealth of resources from Altera and partners to take classes on Embedded Hardware,
Embedded Software and reference design starting points to advance your skills using Altera’s powerful
Nios II based hardware and software tools.
Appendix A: Hardware Pinout DE0-CV In the Quartus main window, create a new file called “de0cv.tcl” and add in the following assignments:
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to LEDR[0]
set_location_assignment PIN_P22 -to CPU_RESETn
set_location_assignment PIN_M9 -to CLOCK_50
set_location_assignment PIN_U21 -to HEX0[0]
set_location_assignment PIN_V21 -to HEX0[1]
set_location_assignment PIN_W22 -to HEX0[2]
set_location_assignment PIN_W21 -to HEX0[3]
set_location_assignment PIN_Y22 -to HEX0[4]
set_location_assignment PIN_Y21 -to HEX0[5]
set_location_assignment PIN_AA22 -to HEX0[6]
set_location_assignment PIN_AA20 -to HEX1[0]
set_location_assignment PIN_AB20 -to HEX1[1]
set_location_assignment PIN_AA19 -to HEX1[2]
set_location_assignment PIN_AA18 -to HEX1[3]
set_location_assignment PIN_AB18 -to HEX1[4]
set_location_assignment PIN_AA17 -to HEX1[5]
set_location_assignment PIN_U22 -to HEX1[6]
set_location_assignment PIN_Y19 -to HEX2[0]
set_location_assignment PIN_AB17 -to HEX2[1]
set_location_assignment PIN_AA10 -to HEX2[2]
set_location_assignment PIN_Y14 -to HEX2[3]
set_location_assignment PIN_V14 -to HEX2[4]
set_location_assignment PIN_AB22 -to HEX2[5]
set_location_assignment PIN_AB21 -to HEX2[6]
set_location_assignment PIN_Y16 -to HEX3[0]
set_location_assignment PIN_W16 -to HEX3[1]
set_location_assignment PIN_Y17 -to HEX3[2]
set_location_assignment PIN_V16 -to HEX3[3]
set_location_assignment PIN_U17 -to HEX3[4]
set_location_assignment PIN_V18 -to HEX3[5]
set_location_assignment PIN_V19 -to HEX3[6]
set_location_assignment PIN_U20 -to HEX4[0]
set_location_assignment PIN_Y20 -to HEX4[1]
set_location_assignment PIN_V20 -to HEX4[2]
set_location_assignment PIN_U16 -to HEX4[3]
set_location_assignment PIN_U15 -to HEX4[4]
set_location_assignment PIN_Y15 -to HEX4[5]
set_location_assignment PIN_P9 -to HEX4[6]
set_location_assignment PIN_N9 -to HEX5[0]
set_location_assignment PIN_M8 -to HEX5[1]
set_location_assignment PIN_T14 -to HEX5[2]
set_location_assignment PIN_P14 -to HEX5[3]
set_location_assignment PIN_C1 -to HEX5[4]
set_location_assignment PIN_C2 -to HEX5[5]
set_location_assignment PIN_W19 -to HEX5[6]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX0[0]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX0[1]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX0[2]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX0[3]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX0[4]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX0[5]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX0[6]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX1[0]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX1[1]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX1[2]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX1[3]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX1[4]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX1[5]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX1[6]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX2[0]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX2[1]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX2[2]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX2[3]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX2[4]
54
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX2[5]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX2[6]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX3[0]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX3[1]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX3[2]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX3[3]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX3[4]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX3[5]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX3[6]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX4[0]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX4[1]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX4[2]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX4[3]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX4[4]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX4[5]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX4[6]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX5[0]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX5[1]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX5[2]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX5[3]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX5[4]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX5[5]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to HEX5[6]
set_location_assignment PIN_L1 -to LEDR[9]
set_location_assignment PIN_L2 -to LEDR[8]
set_location_assignment PIN_U1 -to LEDR[7]
set_location_assignment PIN_U2 -to LEDR[6]
set_location_assignment PIN_N1 -to LEDR[5]
set_location_assignment PIN_N2 -to LEDR[4]
set_location_assignment PIN_Y3 -to LEDR[3]
set_location_assignment PIN_W2 -to LEDR[2]
set_location_assignment PIN_AA1 -to LEDR[1]
set_location_assignment PIN_AA2 -to LEDR[0]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to LEDR[0]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to LEDR[1]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to LEDR[2]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to LEDR[3]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to LEDR[4]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to LEDR[5]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to LEDR[6]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to LEDR[7]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to LEDR[8]
set_instance_assignment -name IO_STANDARD "3.3-V LVTTL" -to LEDR[9]
set_location_assignment PIN_M6 -to KEY[3]
set_location_assignment PIN_M7 -to KEY[2]
set_location_assignment PIN_W9 -to KEY[1]set_location_assignment PIN_U7 -to KEY[0]
set_location_assignment PIN_AB12 -to SW[9]
set_location_assignment PIN_AB13 -to SW[8]
set_location_assignment PIN_AA14 -to SW[6]
set_location_assignment PIN_AA13 -to SW[7]
set_location_assignment PIN_AB15 -to SW[5]
set_location_assignment PIN_AA15 -to SW[4]
set_location_assignment PIN_T12 -to SW[3]
set_location_assignment PIN_T13 -to SW[2]
set_location_assignment PIN_V13 -to SW[1]
set_location_assignment PIN_U13 -to SW[0]
Save de0cv.tcl in your project directory. Then go to View Utility Windows Tcl Console. In the Tcl
Console, type in “source de0cv.tcl” and your pin assignments should be successfully added.