Lab 5-1 Develop Software Nios Timer
Lab 5-1 Develop Software Nios Timer
OBJECTIVE
Students study Lab 4-1 after doing all steps in Lab 4. The objective of this Lab is to
provide for students the knowledge of programming with Nios II processor using C
language. After study Lab 4-1, students can understand how to program some basic
projects with timer using C language.
SUMMARY OF LAB 4
Lab 4 can be summarized into these steps:
- Use DE10-Standard Sytem Builder Tool to create a project with necessary peripherals.
- Use Qsys Tool to configure a system with Nios II processor and timer.
- Create HDL code with Qsys Tool and connect the system with peripherals using
VHDL or Verilog on Quartus.
- Use Quartus 18.1 programmer to download .sof file to the development board (DE10-
standard) via JTAG interface.
- Use Eclipse Software to program and run c code.
Department of Electronics
Microcomputer Laboratory
1
Lab 4-1: Develop Software for Nios II Processor with Timer
RESULTS OF LAB 4
Doing all steps in LAB 4, we have a project with two folders: one is the project folder
(the first folder) and two is the BSP (Board Support Package). The BSP is created
automatically by Eclipse and Quartus Tool. This folder contains all nescessary libraries
used for programming.
Department of Electronics
Microcomputer Laboratory
2
Lab 4-1: Develop Software for Nios II Processor with Timer
A specific hardware timer peripheral is defined as the system clock device within
the BSP settings.
Remember in lab4, we add two timer. In this project, we configure one of them become
system clock timer and the other is used as timestamp timer. To do that, we need to
define them in BSP settings following these steps.
- Right click on bsp, choose Nios II >> BSP Editor…
- In BSP Editor Window, choose one timer for sys_clk_timer and one timer for
time_stamp_timer.
Department of Electronics
Microcomputer Laboratory
3
Lab 4-1: Develop Software for Nios II Processor with Timer
Next, in BSP, we considered file system.h. As disscussed in lab 2-1, this file contains
all important parameters of the system: The frequency of CPU, name and address of
each peripheral register, parameters relating to interrupt configuration, …. After adding
timer to our system, the parameters of timers are also found here.
Department of Electronics
Microcomputer Laboratory
4
Lab 4-1: Develop Software for Nios II Processor with Timer
- alt_timestamp_start()
- alt_timestamp()
- alt_timestamp_freq()
The timestamp driver functions are defined (prototyped) in the alt_timestamp.h
header file.
Calling alt_timestamp_start() starts the counter running
Subsequent calls to alt_timestamp() return the current value of the
timestamp counter
Calling alt_timestamp_start() again resets the counter to zero
The behavior of the timestamp driver is undefined when the counter
reaches the value (232 - 1)
Calling alt_timestamp_freq() return the rate at which the timestamp
counter increments. This rate is typically the hardware frequency of the
NIOS II processor system – Usually millions of cycles per second.
Example 1: This code is used to measure the time of a function processing and display
this value to console.
#include <stdio.h>
#include <sys/alt_timestamp.h>
void func() {
int a,b,c;
c = a + b;
}
int main()
{
if (alt_timestamp_start() < 0) {
printf("Error !");
}
int time1;
int time2;
int time_base;
time1 = alt_timestamp();
time2 = alt_timestamp();
time_base = time2 - time1;
time1 = alt_timestamp();
func();
time2 = alt_timestamp();
return 0;
}
Department of Electronics
Microcomputer Laboratory
5
Lab 4-1: Develop Software for Nios II Processor with Timer
In the program, time1 and time2 is used to save the current value of the timestamp
counter. Because alt_timestamp() is also a function, it needs time to process which is
saved by time_base.
To measure the time process func(), we need to count the value of timestamp counter
before and after func() excute. This results is time2-time1-time_base. It is the value
measured by counter. To convert it to time, we need to devide this value by frequency
of the timestamp counter.
Example 2: In lab 2-1, we know that there is a function call usleep() used to create
delay time in microcomputer. In this example, we remesure the accruracy of this
function by using the concept in example 1.
#include <stdio.h>
#include <unistd.h>
#include <sys/alt_timestamp.h>
int main()
{
if (alt_timestamp_start() < 0) {
printf("Error !");
}
int time1;
int time2;
int time_base;
time1 = alt_timestamp();
time2 = alt_timestamp();
time_base = time2 - time1;
time1 = alt_timestamp();
usleep(500000);
time2 = alt_timestamp();
return 0;
}
The result shows that the actual processing time of usleep() function is longer
approximately 40% the time we need.
Department of Electronics
Microcomputer Laboratory
6
Lab 4-1: Develop Software for Nios II Processor with Timer
return alt_ticks_per_second();
}
int main()
{
static alt_alarm alarm;
if (alt_alarm_start(&alarm, alt_ticks_per_second(),
my_alarm_callback, NULL) < 0)
{
Department of Electronics
Microcomputer Laboratory
7
Lab 4-1: Develop Software for Nios II Processor with Timer
while (1){
}
return 0;
}
In main function we have while (1) loop which means the program will stuck here
forever. The result shows that the sentence “Alarm called” is display every 1s.
Therefore, the function my_alarm_callback(void* context) is called every 1s. To make
it clear, the microcomputer execute while(1) loop if there is no signal from alarm. When
the alarm counts enough ticks, it creates a signal to microcomputer. It makes the system
changes to execute function my_alarm_callback(). Alarms can be considered part of the
exception processing system.
Department of Electronics
Microcomputer Laboratory
8
Lab 4-1: Develop Software for Nios II Processor with Timer
HOMEWORK
1. Build a Nios II system with timer. Write a C program to do this task by using alarm
Each led from LED1 to 10 turns on in 500ms and then turn off.
3. Build a Nios II with timers. Write a C program to do these tasks by using alarm:
- When switch 1 is set, each led at even position from LED1 to 10 turns on in 500ms
and then turn off while each led at odd position from LED1 to 10 turns on in 1s and
then turn off.
- When switch 2 is set, each led from LED1 to 5 turns on in 500ms and then turn off.
Each led from led 6 to 10 turn on in 1s and turn off.
- When two switch are set, turn on all leds.
- When two switch are not set, turn off all leds.
Department of Electronics
Microcomputer Laboratory
9