Nios® II Software Developer Handbook

ID 683525
Date 8/28/2023
Public
Document Table of Contents

7.9.1. System Clock Driver

The HAL system clock driver provides a periodic heartbeat, causing the system clock to increment on each beat. Software can use the system clock facilities to execute functions at specified times, and to obtain timing information. You select a specific hardware timer peripheral as the system clock device by manipulating BSP settings.

For more information about how to control BSP settings, refer to the “HAL BSP Settings” chapter.

The HAL provides implementations of the following standard UNIX functions: gettimeofday(), settimeofday(), and times(). The times returned by these functions are based on the HAL system clock.

The system clock measures time in clock ticks. For embedded engineers who deal with both hardware and software, do not confuse the HAL system clock with the clock signal driving the Nios® II processor hardware. The period of a HAL system clock tick is generally much longer than the hardware system clock. system.h defines the clock tick frequency.

At runtime, you can obtain the current value of the system clock by calling the alt_nticks() function. This function returns the elapsed time in system clock ticks since reset. You can get the system clock rate, in ticks per second, by calling the function alt_ticks_per_second(). The HAL timer driver initializes the tick frequency when it creates the instance of the system clock.

The standard UNIX function gettimeofday() is available to obtain the current time. You must first calibrate the time of day by calling settimeofday(). In addition, you can use the times() function to obtain information about the number of elapsed ticks. The prototypes for these functions appear in times.h.

For more information about the use of these functions, refer to the "HAL API Reference" chapter.