[Note]. How do I use sys_clk_timer in niosii?

Source: Internet
Author: User

This article briefly describes how to use the sys_clk_timer service to control the switch of an LED every Ms.

Environment: Altera Quartus 9.1 SP1 + niosii 9.1 software build tools for eclipse SP1

Step 1. Sample the interval timer core in the System Builder:

1. The interval timer core is named sys_clk_timer in the example of the system disk builder.

Figure 1 example of interval timer Core

Note: The name of sys_clk_timer must be the same as sys_clk_timer in BSP. You can also name it another name.

Figure 2 related settings in BSP

2. Configure the interval timer Core

Figure 3 configure the interval timer Core

When timerout period is set to 1 ms, each tick is 1 ms. The Tick concept will be mentioned later. The default timer counter size is 32 bits. You can configure hardware options by yourself. Because the sys_clk_timer service is only used here, you can select one of the default options: simple periodic interrupt, that is, simple periodic interruption. The following describes the concept of timerout period.

Figure 4 timerout period concept in the manual

Note: Because the writeable period option is not used, timerout period is fixed.

Step 2 compile the CProgram

1. view the corresponding content in system. h.

 
# Define altera_avalon_timer # define limit 1 # define sys_clk_timer_base 0x1002000 # define limit 32 # define limit 1 # define limit 125000000u # define limit 2 # define limit 0 # define limit 124999ull # limit 0.0010 # define sys_clk_timer_name "/dev/sys_clk_timer" # define limit 1 # define limit "Ms" # define limit 0 # define limit 0 # define sys_clk_timer_span 32 # define limit 1000u # define limit 0 # define sys_clk_timer_type "altera_avalon_timer"

5th rows, fixed timerout period, 1; 13th rows, which is the unit of timerout period.

17th rows. Since each tick is 1 ms, there are 1000 ticks in 1 s.

The input clock frequency of the 6th-line, niosii soft core, which is 125 MHz.

2. Demonstration procedure

# Include "system. H "// The storage ing header file # include" altera_avalon_pio_regs.h "after the configuration of the systems builder // The storage ing header file of the PIO core # include" alt_types.h "// the Data Type of Altera # include" sys /alt_alarm.h "// driver header file of the interval timer core # include" unistd. H "// null // user callback function alt_u32 timer_callbackfunc (void * context); alt_alarm timer_addr; // the pointer to the alt_alarm struct alt_u32 ticks_num = 100; // 100 ticks * 1 MS/tick = 100 msint main () {/** function: Start sys_clk_timer Server Note * function: # include "sys/alt_alarm.h" * entry parameter: timer_addr, pointing to the pointer * ticks_num of the alt_alarm struct, and executing the callback function * timer_callbackfunc every ticks_num, user callback function * context, which is the parameter passed to the user callback function. Here it is null */alt_alarm_start (& timer_addr, ticks_num, timer_callbackfunc, null); While (1); Return 0 ;} /** function: User callback function * function remarks: you can write the * entry parameter: * context, a parameter sent from alt_alarm_start () * outlet parameter: ticks_num, number of cycles of sys_clk_timer Service */alt_u32 timer_callbackfunc (void * con Text) {static alt_u8 temp = 0; // when defining static values, only one iowr_altera_avalon_pio_data (q_led_base, temp) can be assigned. Temp = ~ Temp; // flip temp return ticks_num; // return the ticks_num of the next sys_clk_timer service}

Corresponding to the simple periodic interrupt mentioned above, the user callback function between 34 and 40 rows can be regarded as a timer interrupt service program; the alt_alarm_start () line 23 can be regarded as a registration function of the timer interrupt. What is the use of ticks_num? As mentioned above, one tick is 1 ms (configured in the system-wide Image Processing System Builder). Once the timer runs with alt_alarm_start, the callback function is executed once every ticks_num * 1 mS/tick.

Note: In row 3, the keyword static must be added to the variable declaration in the user callback function to prevent errors caused by Compiler optimization. The ticks_num value of row 23rd is the ticks_num value of the first sys_clk_timer service; The ticks_num value of row 39th is the ticks_num value of the next sys_clk_timer service; the two can be different. You can try replacing the ticks_num Value. In addition, the time of each tick is only related to the configuration in the FPGA builder, and does not have much to do with the input clock of the Nios II soft core.

As to why the callback function is called, we can open sys/alt_alarm.h and view the prototype of the related function. Due to the limited level, no related Parsing is required. Please analyze it by yourself.

/** "Alt_alarm" is a structure type used by applications to register an alarm * callback function. an instance of this type must be passed as an input * argument to alt_alarm_start (). the user is not responsible for initialising * the contents of the instance. this is done by alt_alarm_start (). */typedef struct alt_alarm_s alt_alarm;/** alt_alarm_start () can be called by an application/driver in Order to register * a function for periodic callback at the system clock frequency. be aware that * This callback is likely to occur in interrupt context. */extern int alt_alarm_start (alt_alarm * the_alarm, alt_u32 nticks, alt_u32 (* callback) (void * context), void * context);/** alt_alarm_stop () is used to unregister a callback. alternatively the callback * can return zero to unregister. */extern Void alt_alarm_stop (alt_alarm * the_alarm);/** obtain the system clock rate in ticks/s. */static alt_inline alt_u32 alt_always_inline alt_ticks_per_second (void) {return _ alt_tick_rate;}/** alt_sysclk_init () is intended to be only used by the system clock driver * In order to initialise the value of the clock frequency. */static alt_inline int alt_always_inline alt_sysclk_init (alt_u32 nticks ){ If (! _ Alt_tick_rate) {_ alt_tick_rate = nticks; return 0 ;}else {return-1 ;}/ ** alt_nticks () returns the elapsed number of system clock ticks since reset. */static alt_inline alt_u32 alt_always_inline alt_nticks (void) {return _ alt_nticks ;}
References

1. volume5: Embedded peripherals, quartuⅱ handbook version 9.1

Http://www.altera.com/literature/hb/nios2/n2cpu_nii5v3.pdf

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.