0% found this document useful (0 votes)
105 views

Nios II Processor: Hardware Abstraction Layer Exercise Manual

Uploaded by

Kadek Arisudama
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
105 views

Nios II Processor: Hardware Abstraction Layer Exercise Manual

Uploaded by

Kadek Arisudama
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Nios® II Processor: Hardware

Abstraction Layer
Exercise Manual

Software Requirements:

Intel® Quartus® Prime Version 17.1.0 Standard Edition

Hardware Requirements:

Intel® Cyclone® 10 LP FPGA Evaluation Kit

Link to this document:


https://www.altera.com/customertraining/OLT/NiosII_HAL/lab.pdf

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

____ 1. Launch the Nios II Software Build Tools for Eclipse.


____ 2. When the Workspace Launcher opens, click OK.

____ 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.

____ 9. When it opens, click Add File…


____ 10. Browse to find the .sof file in the NIOS_II_HAL_training/output_files directory.
Select the .sof file and click Open.
____ 11. Click Start to program the FPGA. The progress indicator will display 100%
when the programming is successful.

____ 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.

Intel warrants performance of its FPGA and semiconductor products to current


specifications in accordance with Intel's standard warranty, but reserves the right
to make changes to any products and services at any time without notice. Intel
assumes no responsibility or liability arising out of the application or use of any
information, product, or service described herein except as expressly agreed to
in writing by Intel. Intel customers are advised to obtain the latest version of
device specifications before relying on any published information and before
placing orders for products or services.

*Other names and brands may be claimed as the property of others.

9
Exercise Manual Nios II Processor: Hardware Abstraction Layer

Exercise 2

Controlling LEDs

10
Developing Software for the Nios II ProcessorExercise Manual

Use HAL Macros to control the LEDs


In this section, your goal is to create a program that controls 3 LEDs to display a binary
counter from 0 - 7

You will use the file from the project from the previous lab and only modify the
“hello_world_small.c” file.

1. Replace the includes of the “hello_world_small.c” from the previous project


with the following code
#include <sys/alt_stdio.h>
#include <stdio.h>
#include "altera_avalon_pio_regs.h"
#include "system.h"
#include <unistd.h>

#define delay 1000000

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()
{

alt_putstr("Hello from Nios II HAL Lab!\n");

/* Event loop never exits. */


while (1){

/*STEP 1: Controlling LEDS, displaying a binary counter 0-15*/


for(int i=0;i<7;i++){
}
}
return 0;
}
3. Now, inside the for loop add the IOWR_ALTERA_AVALON_PIO_DATA function.
This function will allow you to control Input and Output peripherals. It will need
2 parameters. The first one is LED_BASE which is needed to know which
peripheral will be controlled, in our case the LEDs. The second parameter will be
the value that we want to write to the LEDs (output port) which will be the same
variable that we use in the for loop, “i”.
for(int i=0;i<7;i++){
IOWR_ALTERA_AVALON_PIO_DATA(LED_BASE, i);
}

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

For the complete code please refer to the file “lab2_final.c”

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.

Intel warrants performance of its FPGA and semiconductor products to current


specifications in accordance with Intel's standard warranty, but reserves the right
to make changes to any products and services at any time without notice. Intel
assumes no responsibility or liability arising out of the application or use of any
information, product, or service described herein except as expressly agreed to
in writing by Intel. Intel customers are advised to obtain the latest version of
device specifications before relying on any published information and before
placing orders for products or services.

*Other names and brands may be claimed as the property of others.

13
Exercise Manual Nios II Processor: Hardware Abstraction Layer

Exercise 3

Accessing Buttons

14
Developing Software for the Nios II ProcessorExercise Manual

Use HAL Macros to Access the Push Buttons


In this section, your goal is to create a program that responds to user button presses on
the development board. You will modify the code so that you can turn on the LEDs using
the buttons on the board.

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.

1. In order to sample a push button we will use the


IOWR_ALTERA_AVALON_PIO_DATA function to sample the value of the buttons.
You will also need a debounce logic to make sure the button is pressed. Insert the
code below where the INSERT CODE 1 comment is written.
print_cnt = 0;

buttons = IORD_ALTERA_AVALON_PIO_DATA(PB_BASE);

// Run De-Bouncing Algorithm:


if (buttons != NONE_PRESSED) { // if button pressed
usleep (DEBOUNCE);
while (buttons != NONE_PRESSED) { // wait for button release
button_val = buttons; // strobe state of SW switch
// INSERT CODE 2. Sample "buttons" again to update with new value ***
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.

For the complete code please refer to the file “lab3_final.c”

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.

Intel warrants performance of its FPGA and semiconductor products to current


specifications in accordance with Intel's standard warranty, but reserves the right
to make changes to any products and services at any time without notice. Intel
assumes no responsibility or liability arising out of the application or use of any
information, product, or service described herein except as expressly agreed to
in writing by Intel. Intel customers are advised to obtain the latest version of
device specifications before relying on any published information and before
placing orders for products or services.

*Other names and brands may be claimed as the property of others.

17
Exercise Manual Nios II Processor: Hardware Abstraction Layer

Exercise 4

Using timers and alarms

18
Developing Software for the Nios II ProcessorExercise Manual

Part A: Use HAL API Functions to Utilize the System Timer


In this section, your objective is to determine how long a “Test Loop” takes to run in the
file called, “Lab4a_base.c”. The point of this exercise is to, once again, call upon the
API functions you learned in the last section of the class to perform the necessary
operations.

Step 1 – Change the BSP


____ 1. Highlight the nios_hal_training_bsp folder, right-click on it, and select Nios II.
Then click BSP Editor. This will open the BSP Editor.
____ 2. Ensure that the timestamp_timer is set to to timer_0

____ 3. Click Generate, and then Exit.

19
Exercise Manual Nios II Processor: Hardware Abstraction Layer

Step 2 – Add timer


____ 1. Use lab4a_base.c as a starting point for this lab.

____ 2. In order to use a timer you need to create a timestamp, paste at INSERT CODE 1 the
following code:

// INSERT CODE 1-Start a timestamp timer


if (alt_timestamp_start() < 0) {
printf("Timer init failed \n");
exit(0);

____ 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();

timer_overhead = time2 - time1;

____ 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();

num_ticks = time2 - time1 - timer_overhead;

printf("Time in ticks for loop = %u \n", (unsigned int)num_ticks);


printf("Number of ticks per second = %u \n", (unsigned int)alt_timestamp_freq());

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

Part B - Add an Alarm for to expire after 3 seconds


In this section, your objective is modify “Lab4b.c” using the API functions you learned in
the last section of the class to add a timer that expires after 3 seconds

Step 1 – Change the BSP


____ 11. Highlight the nios_hal_training_bsp folder, right-click on it, and select Nios II.
Then click BSP Editor. This will open the BSP Editor.
____ 12. Ensure that the sys_clk_timer is set to to timer_0
____ 13.

____ 14. Click Generate, and then Exit.

22
Developing Software for the Nios II ProcessorExercise Manual

Step 2 – Add an alarm


____ 1. Use lab4b_base.c as a starting point for this lab.

____ 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");

____ 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.
____ 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.

____ 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.

Intel warrants performance of its FPGA and semiconductor products to current


specifications in accordance with Intel's standard warranty, but reserves the right
to make changes to any products and services at any time without notice. Intel
assumes no responsibility or liability arising out of the application or use of any
information, product, or service described herein except as expressly agreed to
in writing by Intel. Intel customers are advised to obtain the latest version of
device specifications before relying on any published information and before
placing orders for products or services.

*Other names and brands may be claimed as the property of others.

24
Developing Software for the Nios II ProcessorExercise Manual

Exercise 5

Interrupts

25
Exercise Manual Nios II Processor: Hardware Abstraction Layer

Part A - Create an Interrupt-Driven Program


In this section, you will take the program concept from Part 1 of Lab 2 and employ an
interrupt rather than a polling loop to control the LED positions. When any of the push-
buttons are pressed, an interrupt will be triggered, which will vector the processor to an
interrupt service routine. This ISR will tell the processor what to do with the LEDs
depending up on which button was pressed.

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)

Step 1 – Complete the Code

____ 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.

You can utilize the Software Developers Handbook as an example of how to


implement an ISR. Please also refer to the generic example in the class notes and
modify it to read-from (and reset) the KEYs EDGE_CAP register.

26
Developing Software for the Nios II ProcessorExercise Manual

Step 3 –Run the Code

____ 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.

Step 4 –Relocate the Exception Stack and ISR to Tightly-Coupled Memory

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.

____ 1. To re-target the exception stack and interrupt stack to the


tightly_coupled_data_memory, right-click on the Lab3_bsp project and select Nios
II and then click on BSP Editor. Enable the exception stack on the Main tab, and set
the memory_region_name and stack_size as shown below:

____ 2. Click on Linker Script tab in the BSP Editor


We will now create a linker section named “.tcim”

____ 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

____ 4. Click Add, then Generate and then Exit.

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")));

____ 6. Rebuild the project.

____ 7. Check to see that the exception stack was mapped to tightly_coupled_data_memory.

a. In the Lab3_bsp project in the Project Explorer, double-click on linker.x.


b. Scroll down in linker.x to the MEMORY section located near the top of the
file, and find the tightly_coupled_data_memory, interrupt_stack and
exception_stack. Look at the base addresses - you should see that the stack is
located in the span of the Data TCM.

tightly_coupled_instruction_memory : ORIGIN = 0x20000, LENGTH = 4096


tightly_coupled_data_memory : ORIGIN = 0x21000, LENGTH = 2048
interrupt_stack : ORIGIN = 0x21800, LENGTH = 1024
exception_stack : ORIGIN = 0x21c00, LENGTH = 1024

____ 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.

Intel warrants performance of its FPGA and semiconductor products to current


specifications in accordance with Intel's standard warranty, but reserves the right
to make changes to any products and services at any time without notice. Intel
assumes no responsibility or liability arising out of the application or use of any
information, product, or service described herein except as expressly agreed to
in writing by Intel. Intel customers are advised to obtain the latest version of
device specifications before relying on any published information and before
placing orders for products or services.

*Other names and brands may be claimed as the property of others.

29

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy