[Serialization] [FPGA black gold Development Board] niosii-timer Experiment (11)

Source: Internet
Author: User
Disclaimer: This article is an original work and copyright belongs to the author of this blog. All. If you need to repost, please indicate the source Http://www.cnblogs.com/kingst/

 

Introduction

In this section, let's talk about timer content. A timer, as its name implies, is a device related to time. It is a hardware peripheral device used to count clock cycles and generate periodic interrupt signals.

People who have used single-chip microcomputer must be familiar with the timer. It is mainly used to handle periodic events, such as setting the sampling frequency of AD and generating a periodic timer through the timer. I have found that in many documents, I have introduced how to implement the system clock, timestamp, or watchdog functions, but I have not really introduced how to actually use the functions of the timer itself, this can easily mislead you. Some people may encounter this problem when learning this part. A soft core can only define one system clock. What if we want to use two timers? No way, this method does not work, this is the drawback of the system clock method. In order to solve this problem, we need to turn aside the system clock and truly understand the functions of the timer itself. It can be operated like the timer of the single chip microcomputer. Let's start.

Hardware development

First, we need to build the timer module in the nioss soft core, as shown in

As shown in, Red Circle 1 is used to preset the timer cycle after hardware generation. That is to say, this is an initial value. We can change the timer cycle through software. The red circle 2 is the size of the timer counter, which is divided into 32-bit and 64-bit. You need to select according to your timer cycle. Here we choose 32-bit. The red circle 3 is the timer preset mode to implement different functions. Here we choose full-featured, which is a full function. With this option selected, we can modify the period and control the stop start bit. Click Finish to complete the settings.

Here we create two timers, And the other method is the same. As shown in figure

We need four LEDs for the timer test, so we need to create a PIO module. I will not discuss it here. I have already discussed it. Next, the address is automatically configured and the configuration is interrupted. It is the same as the previous one. Compile, wait, compile, and wait...

The hardware is okay. Next we start software development.

Software Development

Open the nioss 9.0 IDE and compile it. Press Ctrl + B. After compilation, let's take a look at the system. h file. The following content should appear:

# Define timer1_name "/dev/timer1" # define timerfill type "altera_avalon_timer" # define timerfill base 0x00201000 ...... # Define timer2_name "/dev/timer2" # define timer2_type "altera_avalon_timer" # define timer2_base 0x00201000 ......

When developing the Nios software, you must first check the IMG. first, make sure that the hardware has been correctly loaded, and the name is the same as what you set to avoid problems in this place.

Next, let's take a look at the timer registers. The table is from page 24-6 of n2cpu_embedded peripherals.pdf.

Through this table, we can see that the timer has status registers, control registers, 16-bit high in the timer cycle, 16-bit low, 16-bit high in snap, 16-bit low, we use the first three registers, which have not been obtained yet.

The following experiment uses timer 1 to Control the flickering of four LEDs, while Timer 2 is used to change the timer cycle of timer 1. In this way, we can see that, the flickering frequency of the four LEDs is constantly changing.

Let's take a look.Source code

/** =================================================== =========================================== * Filename: main. C * Description: timer test * version: 1.0.0 * created: 2010.4.16 * revision: none * Compiler: NiO II 9.0 ide * Author: Mary (AVIC) * Email: avic633@gmail.com * =================================================== ======================= * // * -------------------------------------------------------------- * include * Others */# <stdio. h> # include <sys/unistd. h> # include <Io. h> # include <string. h> # include "system. H "# include" altera_avalon_pio_regs.h "# include" altera_avalon_timer_regs.h "# include" alt_types.h "# include" sys/alt_irq.h "# include ".. /INC/systems. H "/* optional * variable * --------------------------------------------------------------- */static void timer_init (void); // initialize interrupt int I = 0, j = 0, flag; alt_u32 timer_prd [4] = {5000000,100 00000, 50000000,100 000000}; // Number of timer clocks
 // The timer time of the timer is calculated as follows: timer clock count/Timer cycle 
// The system clock I used is 100 MHz. Therefore, the above four scheduled times are {0.05, 0.1, 0.5, 1}/** = function ====================================== ===================================* Name: main * description: * ===================================================== =========================================*/INT main (void) {// initialize timer timer_init (); While (1); Return 0 ;} /** = function ================================================== =============================* Name: isr_timer * description: * ===================================================== =======================*/static void isr_timer1 (void * context, alt_u32 ID) {// control the flashing of the flow light. A total of four led LEDs-> DATA = 1 <I; I ++; if (I = 4) I = 0; // clear the timer interrupt mark register iowr_altera_avalon_timer_status (timer1_base, 0x00 );} /** = function ================================================== =============================* Name: isr_timer2 * Description: The Timer 2 is used to change the cycle of timer 1, after the change, you need to restart the timer * ====================================== =================================== */static void isr_timer2 (void * context, alt_u32 ID) {// change the cycle iowr_altera_avalon_timer_periodl of timer 1 (timer1_base, timer_prd [J]); reset (timer1_base, timer_prd [J]> 16 ); // restart the timer iowr_altera_avalon_timer_control (timer1_base, 0x07); // If (j = 0) Flag = 0 when the flicker frequency is increased first, then the image is increased; if (j = 3) Flag = 1; if (flag = 0) {J ++;} else {J --;} // clear the iowr_altera_avalon_timer_status (timer2_base, 0 );} /** = function ================================================== =============================* Name: timer_init * description: timer initialization * =================================================== ============================= */void timer_init (void) // initialize interrupt {// clear timer1 interrupt flag register iowr_altera_avalon_timer_status (timer1_base, 0x00); // set timer1 cycle. Here, the input is the number of clock periods (timer1_base, 100000000); disconnect (timer1_base, 100000000> 16); // allows timer1 to interrupt commit (timer1_base, 0x07); // registers timer1 to interrupt alt_irq_register (timer1_irq, (void *) timer1_base, isr_timer1); // clear timer2 interrupt mark register swap (timer2_base, 0x00); // set timer2 cycle swap (timer2_base, 500000000); Round (timer2_base, 500000000> 16); // enable timer2 to interrupt commit (timer2_base, 0x07); // register timer2 to interrupt alt_irq_register (timer2_irq, (void *) timer2_base, isr_timer2 );}

The above method is implemented through the API provided by Hal. Of course, we can also implement it through the method I provided previously, create a struct, and assign values to the register directly, I prefer this method, which is clear and fully controlled by myself. The following provides a structure for you to implement it by yourself. It is very simple. just replace it with one sentence. The content of the struct is determined based on the following table.

 typedef struct {Union {struct {volatile unsigned long int to: 1; volatile unsigned long int run: 1; volatile unsigned long int NC: 30;} bits; volatile unsigned long int word;} status; Union {struct {volatile unsigned long int Ito: 1; volatile unsigned long int cont: 1; volatile unsigned long int start: 1; volatile unsigned long int stop: 1; volatile unsigned long int NC: 28;} bits; volatile unsigned long int word;} control; volatile unsigned long int periodl; volatile unsigned long int periodh; volatile unsigned long int snapl; volatile unsigned long int snaph;} timer; 

With this struct, You can implement the timer function according to the method I mentioned earlier. At this point, the timer method I mentioned is finished, and the problem of using two timers can also be solved.

Next, I will give you a timer system clock routine as a reference, but I do not recommend you to use it because I have already said it. This function lights up an LED every second, with a total of four LEDs. First, you need to set the software, as shown in. Go to the system database properties and select the timer in the system clock timer option box. After the selection, you need to recompile it.

/** =================================================== ========================================= * Filename: main. C * Description: * version: 1.0.0 * created: 2010.4.16 * revision: none * Compiler: NiO II 9.0 ide * Author: Mary (AVIC) * Email: avic633@gmail.com * =================================================== =======================================* // * optional * include *---------------------------- ----------------------------------- */# Include <stdio. h> # include <stdlib. h> # include "system. H "# include" altera_avalon_pio_regs.h "# include" alt_types.h "# include" sys/alt_irq.h "# include ".. /INC/systems. H "# include" sys/alt_alarm.h "/* ----------------------------------------------------------------- * function prototypes * ----------------------------------------------------------------- */alt_u32 my_alarm_c Allback (void * context);/* optional * variable * --------------------------------------------------------------- */unsigned int I = 0; unsigned int alarm_flag; /* define * ----------------------------------------------------------------- */# define inteval_tick 1 # define debug/** = ========================================================== ============== * Name: main * description: * ===================================================== =========================================*/INT main (void) {// variable for calling the API function alt_alarm alarm; // If (alt_alarm_start (& alarm, inteval_tick, my_alarm_callback, null) for starting the system clock service <0) {# ifdef debug printf ("error: No system clock available \ n"); # endif exit (0);} else {# ifdef debug printf ("success: System clock available \ n "); # endif} while (1) {If (alarm_flag! = 0) {led-> DATA = 1 <I; I ++; if (I = 4) I = 0; alarm_flag = 0 ;}; return 0 ;} /* = function =============================================== ====================== * Name: my_alarm_callback * description: * ===================================================== ======================= */alt_u32 my_alarm_callback (void * context) {alarm_flag = 1; // The value of inteval_tick determines the next time the timer is interrupted. Return inteval_tick ;}
 

The timer content is finished. Due to the rush of time, there may be some errors. You can leave a message for me. I will modify it after I confirm it. Thank you!

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.