Nios II Processor: Hardware Abstraction Layer Exercise Manual
Nios II Processor: Hardware Abstraction Layer Exercise Manual
Abstraction Layer
Exercise Manual
Software Requirements:
Hardware Requirements:
Use the link below to download the design files for the exercises:
https://www.altera.com/customertraining/OLT/NiosII_HAL/lab.zip
Exercise Manual Nios II Processor: Hardware Abstraction Layer
Exercise 1
Loading NIOS_HAL_training
project and creating a NIOS II
project in Eclipse
2
Developing Software for the Nios II ProcessorExercise Manual
In this section, you will use the Nios II Software Build Tools for Eclipse to create a
software project. You will then program the FPGA development board with the .sof file
provided, download and run the design on the board.
Part A - Create software project based on the “Hello World” software template
____ 3. When the tool opens, create a new Nios II Application and BSP from
Template by clicking on the File menu ... New.
3
Exercise Manual Nios II Processor: Hardware Abstraction Layer
____ 4. When the new project wizard opens, browse for the nios_setup.sopcinfo file in
the NIOS_II HAL_Training directory of this training
4
Developing Software for the Nios II ProcessorExercise Manual
____ 5. Name the project nios_hal_lab. Then select the Hello World Small template
and click Finish
Note:
• There is another project named Hello World, make sure you select the Hello World
Small project since it is the one that requires less memory and it will be able to fit in
our design.
____ 6. Your new NIOS II project will look like this and include 2 projects,
nios_hal_lab_bsp and nios_hal_lab
5
Exercise Manual Nios II Processor: Hardware Abstraction Layer
____ 7. Now build the nios_hal_lab and nios_hal_lab_bsp by clicking on the Project
menu ... Build All.
____ 8. Once the build finished, launch the Quartus Prime Programmer from the Nios
II menu.
____ 12.
Note: You may need to check the Hardware Setup to ensure you are selecting the
right hardware.
6
Developing Software for the Nios II ProcessorExercise Manual
____ 13. Close the Quartus Prime Programmer. Do not save changes to the .cdf file.
____ 14. Run the Hello World application on the development board. Right-click on the
nios_hal_lab project, and select Run As Nios II Hardware.
____ 15. If the Run Configurations page appears next, click Run.
7
Exercise Manual Nios II Processor: Hardware Abstraction Layer
Note: If the Run button is greyed out, go to Target Connection tab, click
Refresh Connections, verify the appropriate download cable for your board,
click Apply and Run.
____ 16. The Nios II Console tab shows the text “Hello from Nios II!”.
Summary:
Every software application project created in the Nios II Software Build Tools requires
an associated BSP (Board Support Package) project. You can create a new BSP every
time you create an application project if you like, or you can re-use existing BSP projects
to avoid having to establish new BSP settings for your application. The choice is yours.
You can even change what BSP project a given application project is associated with to
fit your changing project requirements. To help illustrate these options you performed
the following steps in the previous lab exercise:
• Created an application and BSP project pair using a built-in software template.
• Ran this project on the development board.
• Created a “blank” application project, associated a pre-existing BSP with it, and
added software code to it. We then ran this on the development board.
• Created a brand new BSP project with new project settings, re-associated an
existing software application project to it, and ran the application on the
development board.
8
Developing Software for the Nios II ProcessorExercise Manual
---------------------------------------------------------------------------------------------
Intel Corporation. All rights reserved.
Intel, the Intel logo, Altera, Arria, Cyclone, Enpirion, MAX, Nios, Quartus and
Stratix words and logos are trademarks of Intel Corporation or its subsidiaries in
the U.S. and/or other countries.
9
Exercise Manual Nios II Processor: Hardware Abstraction Layer
Exercise 2
Controlling LEDs
10
Developing Software for the Nios II ProcessorExercise Manual
You will use the file from the project from the previous lab and only modify the
“hello_world_small.c” file.
Note: This code contains the includes that are needed to use the hal
functions
2. Inside the main function replace the code with the while loop below that counts
from 0 to 7 like below:
int main()
{
11
Exercise Manual Nios II Processor: Hardware Abstraction Layer
4. Now add a delay function usleep after each iteration to be able to notice how LEDs
change. We will use the 1 second delay define as the value.
for(int i=0;i<7;i++){
IOWR_ALTERA_AVALON_PIO_DATA(LED_BASE, i);
usleep(delay);
}
5. Click the Save button or CTRL+ S to save the project.
6. Compile the project by highlighting the nios_hal_training folder in the Project
Explorer window, then right-clicking and selecting Build Project.
7. Once the code is complete and successfully built, highlight the application
project, nios_hal_training, and select Run As → Nios II Hardware.
8. In the Nios II Debug Perspective click the Resume button or F8. You should see
how the LEDs in the board change their state each second and display a binary
count from 0-7
12
Developing Software for the Nios II ProcessorExercise Manual
---------------------------------------------------------------------------------------------
Intel Corporation. All rights reserved.
Intel, the Intel logo, Altera, Arria, Cyclone, Enpirion, MAX, Nios, Quartus and
Stratix words and logos are trademarks of Intel Corporation or its subsidiaries in
the U.S. and/or other countries.
13
Exercise Manual Nios II Processor: Hardware Abstraction Layer
Exercise 3
Accessing Buttons
14
Developing Software for the Nios II ProcessorExercise Manual
You will use the contents of the file, “lab3_base.c” that is provided in the software
directory as a starting point for this exercise and fill in the missing sections using
commands that you learned in the previous section of the presentation.
buttons = IORD_ALTERA_AVALON_PIO_DATA(PB_BASE);
}
print_cnt = 1;
2. Now, inside the switch loop replace INSERT CODE 2 with the code below to turn
on LED 0 when the PUSH_BUTTON_0 is pressed. This will turn on the LED and
print a messsage to the NIOS terminal.
case PUSH_BUTTON_0:
IOWR_ALTERA_AVALON_PIO_DATA(LED_BASE, LED0_ON);
if (print_cnt == 1)
printf("Push Button 0 pressed\n");
break;
3. Repeat Step 2 for PUSH BUTTON_1 and PUSH BUTTON_2 for INSERT CODE
3
case PUSH_BUTTON_1:
IOWR_ALTERA_AVALON_PIO_DATA(LED_BASE, LED1_ON);
if (print_cnt == 1)
printf("Push Button 1 pressed\n");
break;
case PUSH_BUTTON_2:
IOWR_ALTERA_AVALON_PIO_DATA(LED_BASE, LED2_ON);
if (print_cnt == 1)
printf("Push Button 2 pressed\n");
break;
4. Click the Save button or CTRL+ S to save the project.
5. Compile the project by highlighting the nios_hal_training folder in the Project
Explorer window, then right-clicking and selecting Build Project.
6. Once the code is complete and successfully built, highlight the application
project, nios_hal_training, and select Run As → Nios II Hardware.
15
Exercise Manual Nios II Processor: Hardware Abstraction Layer
7. In the Nios II Debug Perspective click the Resume button or F8. You should see
how the LEDs in the board turn on and a serial message each time a push button
is pressed.
16
Developing Software for the Nios II ProcessorExercise Manual
---------------------------------------------------------------------------------------------
Intel Corporation. All rights reserved.
Intel, the Intel logo, Altera, Arria, Cyclone, Enpirion, MAX, Nios, Quartus and
Stratix words and logos are trademarks of Intel Corporation or its subsidiaries in
the U.S. and/or other countries.
17
Exercise Manual Nios II Processor: Hardware Abstraction Layer
Exercise 4
18
Developing Software for the Nios II ProcessorExercise Manual
19
Exercise Manual Nios II Processor: Hardware Abstraction Layer
____ 2. In order to use a timer you need to create a timestamp, paste at INSERT CODE 1 the
following code:
____ 3. Now you need to determine the overhead of the timer stamp functions, you will need
to do a simple subtraction. Paste at INSERT CODE 2 the following code:
// INSERT CODE 2-Determine the timer overhead involved to record time stamp
time1 = alt_timestamp();
time2 = alt_timestamp();
____ 4. Now add a for cycle and count how much it takes to finish. Paste at INSERT
CODE 3 the following code:
//INSERT CODE 3-Start a timer measurement and define a cycle of 10*500ms
time1 = alt_timestamp();
for(int i =0;i<10;i++)
{
printf("Cycle : %d\n", i);
usleep(500000);
____ 5. At the end measure the time it took and display the ticks and the numbers of
ticks per second. Paste at INSERT CODE 4 the following code:
//INSERT CODE 4-Stop timer and print measurements
time2 = alt_timestamp();
while(1);
____ 6. Click the Save button or CTRL+ S to save the project.
____ 7. Compile the project by highlighting the nios_hal_training folder in the
Project Explorer window, then right-clicking and selecting Build Project.
____ 8. Once the code is complete and successfully built, highlight the application
project, nios_hal_training, and select Run As → Nios II Hardware.
____ 9. In the Nios II Debug Perspective click the Resume button or F8. You should
see how the LEDs in the board turn on and a serial message each time a push
button is pressed.
20
Developing Software for the Nios II ProcessorExercise Manual
____ 10. For the complete code please refer to the file “lab4a_final.c”
21
Exercise Manual Nios II Processor: Hardware Abstraction Layer
22
Developing Software for the Nios II ProcessorExercise Manual
____ 2. In order to use aan alarm you need a callback function, paste at STEP 1 the following
code:
//STEP 1 Define callback function for alarm
unsigned long alarm_callback (void* context)
{
//Set alarm flag
printf ("ALARM!!!\n");
return 0;
____ 3. Now you need to define the alarm with its parameters. Paste at STEP 2 the following
code:
if (alt_alarm_start(&my_alarm, 3000, alarm_callback, NULL) < 0)
{
printf ("No System Clock Available\n");
____ 8. For the complete code please refer to the file “lab4b_final.c”
23
Exercise Manual Nios II Processor: Hardware Abstraction Layer
---------------------------------------------------------------------------------------------
Intel Corporation. All rights reserved.
Intel, the Intel logo, Altera, Arria, Cyclone, Enpirion, MAX, Nios, Quartus and
Stratix words and logos are trademarks of Intel Corporation or its subsidiaries in
the U.S. and/or other countries.
24
Developing Software for the Nios II ProcessorExercise Manual
Exercise 5
Interrupts
25
Exercise Manual Nios II Processor: Hardware Abstraction Layer
Use the file, “Lab5_base.c”, provided in the software directory, as the starting point for
this exercise. Fill in the missing sections using commands that you learned in the
previous sections of the presentation or by referring to the interrupt section of the “Nios
II Software Developers Handbook.” (All Programs → Altera → Nios II EDS<ver> →
Documentation)
____ 1. Familiarize yourself with the code, and then modify it to make it interrupt-driven as
described in the synopsis above. There are 6 places in the code you will modify.
Search for the INSERT CODE comments in Lab5.c. Create the ISR based on the
notes in class and the partially completed code provided.
____ 2. In order to check your syntax, recall that you just have to Build the project.
26
Developing Software for the Nios II ProcessorExercise Manual
____ 1. Once the code is complete, run it on the development board and make sure that the
LED positions shift in accordance with the button pressed.
a. Highlight the application project, Lab3 and select Run As → Nios II
Hardware.
b. If the Run Configurations page appears, refresh the target connection, and
select Run.
____ 2. Press the red stop button in the Nios II Console tab to terminate the program.
You may also wish to relocate the exception stack and ISR routine in this software project
to high-speed tightly-coupled memories in order to obtain better system performance.
____ 3. Click Add… inside Linker Section Mappings. Set the new section name to be .tcim
and the corresponding memory region as tightly_coupled_instruction_memory
27
Exercise Manual Nios II Processor: Hardware Abstraction Layer
____ 5. Map the ISR into the tightly coupled instruction memory so that it can run faster:
In the C Code, after the last #define in the code and before the ISR function, simply
add the following function prototype with accompanying section attribute:
static void button_isr(void *context) __attribute__
((section (".tcim")));
____ 7. Check to see that the exception stack was mapped to tightly_coupled_data_memory.
____ 8. Go into the Lab3 application project folder, and open the Lab3.objdump file.
____ 9. Scroll down to the SYMBOL TABLE or search for button_isr. You should see that
the function is mapped to tightly_coupled_instruction memory:
00020000 l F .tcim 0000005c button_isr
____ 10. Download and Run the project again on the development board.
____ 11. Press the buttons on the board to test whether the LED pattern is still changing
properly.
____ 12. Press the red stop button in the Nios II Console tab to terminate the program.
28
Developing Software for the Nios II ProcessorExercise Manual
---------------------------------------------------------------------------------------------
Intel Corporation. All rights reserved.
Intel, the Intel logo, Altera, Arria, Cyclone, Enpirion, MAX, Nios, Quartus and
Stratix words and logos are trademarks of Intel Corporation or its subsidiaries in
the U.S. and/or other countries.
29