My First HPS-Fpga
My First HPS-Fpga
com
March 4, 2014
CONTENTS
Overview
This tutorial is meant for any SoC FPGA starters who wants to know more about how to use the
HPS/ARM to communicate with FPGA. The “My First HPS-FPGA” project is used to demonstrate
the implementation details. This project includes one Quartus project and one ARM C Project and it
demonstrates how HPS/ARM program controls the ten LEDs connected to FPGA.
Before reading this tutorial, developers need to get familiar with those skills included in:
DE1-SoC_Getting_Started_Guide.pdf
My_First_Fpga.pdf
My_First_HPS.pdf
For tutorial purpose, this document asks developers to create a HPS enabled Quartus project based
on the project named my_first_hps-fpga_base. However, for the development of a formal HPS
enabled project, developers are expected to create a Quartus project based on the DE1-SOC-GHRD
(Golden Hardware Reference Design) project, which is included in the SYSTEM CD.
This tutorial pre-assumed the developers have the following background knowledge:
Before starting this tutorial, please note that the following items are required to complete the
demonstration project:
A x86 PC
Windows 7 Installed
One USB Port
Quartus II 13.1 or Later Installed
Altera SoC EDS 13.1 or Later Installed
Win32 Disk Imager Installed
In Altera SoC FPGA, the HPS logic and FPGA fabric are connected through the AXI (Advanced
eXtensible Interface) bridge. For HPS logic to communicate with FPGA fabric, Altera system
integration tool Qsys should be used to design the system. The system must include Altera HPS
HPS-to-FPGA Bridge
Lightweight HPS-to-FPGA Bridge
Figure 1-1 shows a block diagram of the AXI bridges in the context of the FPGA fabric and the
L3 interconnect to the HPS. Each master (M) and slave (S) interface is shown with its data width(s).
The clock domain for each interconnect is shown in parentheses.
The HPS-to-FPGA bridge is mastered by the level 3 (L3) main switch and the lightweight
HPS-to-FPGA bridge is mastered by the L3 slave peripheral switch. In the Quartus of this
demonstration, HPS-to-FPGA bridge is used for ARM/HSP to control the LEDs connected to
FPGA.
DE1-SoC My First HPS FPGA 5 www.terasic.com
March 4, 2014
The FPGA-to-HPS bridge masters the L3 main switch, allowing any master implemented in the
FPGA fabric to access most slaves in the HPS. For example, the FPGA-to-HPS bridge can access
the accelerator coherency.
All three bridges contains global programmer view GPV register. The GPV register control the
behavior of the bridge. Access to the GPV registers of all three bridges is provided through the
lightweight HPS-to-FPGA bridge.
The demonstration source codes include a Quartus project and a C project. They are located in the
folder:
CD-ROM\Demonstration\SOC_FPGA\my_first_hps-fpga
The Quartus Project is located in the sub-folder “fpga-rtl” and the C project is located in the
sub-folder “hps-c”. In this tutorial, developer are expected to establish these projects from scratch.
Quartus Project
This chapter introduces how the MY First HSP-FPGA Quartus project is created based on the
my_first_hps-fpga_base Quartus project. Based this Quarturs project, a PIO component for
controlling LED is added, and a connection between the slave port of the PIO component and the
master port of HPS component is established.
This Quartus project includes all required pin declares for both HPS and FPGA. Note, the pin
declare of HPS only needs to specify pin direction and IO standard. Pin location is not required for
the pin declare of HPS. The golden project also includes basic Qsys system which already includes
a HPS component. The HPS component has been well-configured according to hardware design of
DE1-SoC HPS.
Developers can open the Qsys system by opening the Quartus project, and clicking the menu item
“ToolsQsys” in Quartus II. When Qsys tool is launched, it will ask user to select a target Qsys
system file. In this case, please select the Qsys file “soc_system.qsys”. Figure 2-1 shows the
content of soc_system.qsys Qsys system. It contains hps_0 HPS component.
Figure 2-2 shows the lightweight HPS-to-FPGA AXI Master port of the HPS component.
Developers can connect this port to any memory-mapped slave port of components which developer
wish to access from HPS/ARM.
This section will show how to add a PIO component in Qsys and connect the PIO component to the
HPS component. The PIO component is used to control the ten red LEDs connected to FPGA. First,
please copy the my_first_hps-fpga_base Quartus project to local disk. Open the project and open
the Qsys system file “soc_system.qsys”.
In the Library dialog of Qsys tool, enter ‘pio’ search key as shown in Figure 2-3. When “PIO
(Parallel I/O)” appears, select it. Then, click “Add…” to add the PIO component to the system.
When PIO dialog appears, please change Width to 10, make sure “Output” Direction is selected,
and change the Output Port Reset Value to 0x3ff as shown in Figure 2-4.
DE1-SoC My First HPS FPGA 9 www.terasic.com
March 4, 2014
Figure 2-4 Configure PIO Component
When the PIO component is added into the system, please connect the h2f_lw_axi_master AXI
master port to the s1 slave port of the PIO component as shown in Figure 2-5. By the way, please
change PIO component name to pio_led, change the Clock Input to clk_0, export the Conduit
signal as pio_led_external_connection, and connect the Reset Input to system reset. Note, the
Base address of pio_led PIO component is very important. The ARM program will access the
component according to this base address. In this demonstration, the base address is fixed at
0x0000_0000. The ARM program developer should remember this base address or use a given
Linux shell batch file to extract the address information to a header file hps_0.h. The detail
procedure will be described in the next chapter.
In the Qsys tool, click menu item “GenerateHDL Example…” can find the new interface signal
pio_led_external_connection_export for the added pio_led PIO component as shown in Figure
2-6. Developer can click ‘Copy’ to copy the content to a clipboard, then paste the
pio_led_external_connection_export signal to Quartus top and connect it to the LEDR port as
shown in Figure 2-7. Before closing the Qsys tool, please remember to click the menu item
“GenerateGenerate…” to generate source code for the system.
DE1-SoC My First HPS FPGA 10 www.terasic.com
March 4, 2014
Figure 2-6 pio_led Interface of System
Now, developers can start the compile process by clicking the menu item “ProcessingStart
Compilation”. When the compilation process is completed successfully, soc_system.sof is
generated. Developers can use this file to configure FPGA by Quartus Programming through the
DE1-SoC on-board USB-Blaster II.
C Project
This chapter introduces how to design an ARM C program to control the pio_led PIO controller.
Altera SoC EDS is used to compile the C project. For ARM program to control the pio_led PIO
component, pio_led address is required. The Linux built-in driver ‘/dev/mem’ and mmap
system-call are used to map the physical base address of pie_led component to a virtual address
which can be directly accessed by Linux application software.
pio_led component information is required for ARM C program as the program will attempt to control the
component. This section describes how to use a given Linux shell batch file to extract the Qsys HPS
information to a header file which will be included in the C program later.
The batch file mentioned above is called as generate_hps_qsys_header.sh. It is located in the same
folder as my_first_hps-fpga Quartus project. To generate the header file, launch Altera SoC EDS
command shell, go to the Quartus project folder, and execute generate_hps_qsys_header.sh by
typing ‘./generate_hps_qys_header.sh” followed by ENTER key. A header file hps_0.h is generated.
In the header file, the pio_led base address is represented by a constant PIO_LED_BASE as show
in Figure 3-1. The pio_led width is represented by a constant PIO_LED_DATA_WIDTH. These
two constants will be used in the C program demonstration code.
This section will describe how to map the pio_led physical address into a virtual address which is
accessible by an application software. Figure 3-2 shows the C program to derive the virtual address
of pio_led base address. First, open system-call is used to open memory device driver “/dev/mem”,
and then the mmap system-call is used to map HPS physical address into a virtual address
represented by the void pointer variable virtual_base. Then, the virtual address of pio_led can be
calculated by adding the below two offset addresses to virtual_base.
Offset address of Lightweight HPS-to-FPGA AXI bus relative to HPS base address
Offset address of Pio_led relative to Lightweight HPS-to-FPGA AXI bus
The first offset address is 0xff200000 which is defined as a constant ALT_LWFPGASLVS_OFST in
the header hps.h. The hps.h is a header of Altera SoC EDS. It is located in the folder:
Quartus Installed Folder\embedded\ip\altera\hps\altera_hps\hwlib\include\socal
The second offset address is 0x00000000 which is defined as PIO_LED_BASE in the hps_0.h
header file which is generated in above section.
C programmers need to understand the Register Map of the PIO core for pio_led before they can
control it. Figure 3-3 shows the Register Map for the PIO Core. Each register is 32-bit width. For
detail information, please refer to the datasheet of PIO Core. For led control, we just need to write
output value to the offset 0 register. Because the led on DE1-SoC is low active, writing a value
0x00000000 to the offset 0 register will turn on all of the ten red LEDs. Writing a value 0x000003ff
to the offset 0 register will turn off all of ten red LEDs. In C program, writing a value 0x000003ff to
the offset 0 register of pio_led can be implemented as:
*(uint32_t *) h2p_lw_led_addr= 0x000003ff;
The state will cast the void pointer to a uint32_t pointer, so C compiler knows write a 32-bit value
0x000003ff to the virtual address h2p_lw_led_addr.
In the main program, the LED is controlled to perform LED light sifting operation as shown in
Figure 3-4 . When finishing 60 times of shift cycle, the program will be terminated.
Figure 3-5 shows the content of Makefile for this C project. Because the program will include the
hps.h provided by Altera SoC EDS, so the Makefile should include the following path:
“${SOCEDS_DEST_ROOT}/ip/altera/hps/altera_hps/hwlib/include”
In the makefile, ARM cross-compile also be specified.
To compile the project, type “make” as shown in Figure 3-6. Type “ls” to check the generated
ARM execution file “my_first_hps-fpga”.
To execute the demo, please boot the Linux from the SD-card in DE1-SoC. Copy the execution file
“my_first_hps-fpga” to the Linux directory, and type “chmod +x my_first_hps-fpga” to add
execution attribute to the execute file. Use Quartus Programmer to configure FPGA with the
soc_system.sof generated in previous chapter. Then, type “./my_first_hps-fpga” to launch the ARM
program. The LED on DE1-SoC will be expected to perform 60 times of LED light shift operation,
and then the program is terminated.
For details about booting the Linux from SD-card, please refer to the document:
DE1-SoC_Getting_Started_Guide.pdf
For details about copying files to Linux directory, please refer to the document:
My_First_HPS.pdf