Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

This experiment uses the HDMI display and timer counter function of the Plus1 7021 platform to introduce the basic programming method, which involves interrupt use. The Plus1 7021 system provides four general-purpose 16-bit count timers, timer0 to timer3. When the preset value is reached, the corresponding interrupt signal is generated by each timer。

...

 

Control Register

Counting controller

Counting clock preprocessing controller

Counting value controller

 

 

 

 

 

 

 

Timer0

timer0_ctrl

bit[31:16]Reserved

bit[15:14] :Timer clock selection

0: System clock (default)

1: STC 90KHZ clock

bit13 :Operation method

0:Single operation (default)

1:Repeat the operation

bit12 : Reserved

bit11 :Switch control

0:Turn off the counter

1:Start counter

bit[10:0] :Reserved

timer0_reload

bit[31:16]Reserved

bit[15:0]

16-bit count preset value setting

 

 

timer0_cnt

bit[31:16]Reserved

bit[15:0]

16-bit count value

 

 

 

 

 

 

 

 

timer1

timer1_ctrl

bit[31:16]Reserved

bit[15:14] :Timer clock selection

0: System clock (default)

1: STC 90KHZ clock

bit13 :Operation method

0:Single operation (default)

1:重复操作1:Repeat the operation

bit12 : Reserved

bit11 :开关控制

0:关闭计数器

1:启动计数器 :Switch control

0:Turn off the counter

1:Start counter

bit[10:0] :Reserved

timer1_reload

bit[31:16]Reserved

bit[15:0]

16-bit count preset value setting

 

 

timer1_cnt

bit[31:16]Reserved

bit[15:0]

16-bit count value

 

 

 

 

 

 

timer2

timer2_ctrl

bit[31:6]Reserved

bit[5:2] :Timer clock selection

0: System clock (default)

1: STC 90KHZ clock

bit1 :Operation method

0:单次操作0:Single operation (default)

1:重复操作1:Repeat the operation

bit1 : Reserved

bit0 :开关控制

0:关闭计数器

1:启动计数器 :Switch control

0:Turn off the counter

1:Start counter

timer2_reload

bit[31:16]保留Reserved

bit[15:0]

16-bit count preset value setting

 

timer2_pres_val

bit[31:16]保留Reserved

bit[15:0]16位计数时钟预值设置

16-bit count clock preset value setting

 

timer2_cnt

bit[31:16]保留Reserved

bit[15:0]

16-bit count value

 

 

 

 

 

 

timer3

timer3_ctrl

bit[31:6]Reserved

bit[5:2] :Timer clock selection

0: System clock (default)

1: STC 90KHZ clock

bit1 :Operation method

0:单次操作0:Single operation (default)

1:重复操作1:Repeat the operation

bit1 : Reserved

bit0 :开关控制

0:关闭计数器

1:启动计数器 :Switch control

0:Turn off the counter

1:Start counter

timer3_reload

bit[31:16]保留Reserved

bit[15:0]

16-bit count preset value setting

 

timer3_pres_val

bit[31:16]保留Reserved

bit[15:0]

16位计数时钟预值设置

16-bit count clock preset value setting

 

timer3_cnt

bit[31:16]保留Reserved

bit[15:0]

16-bit count value

 

安装目录 The regmap_q628.h under the installation directory \ SP7021 \ workspace \ sp7021 \ include 文件夹下的regmap_q628.h定义了这四个寄存器,如下图红色folder defines these four registers, as shown in red below:

regmap_q628.h

struct stc_regs {

...

    unsigned int stc_config;     // 12.7

    unsignedint timer0_ctrl;    // 12.8

    unsignedint timer0_cnt;     // 12.9

    unsignedint timer1_ctrl;    // 12.10

    unsignedint timer1_cnt;     // 12.11

    unsigned int timerw_ctrl;    // 12.12

...

    unsigned int stc_63_48;      // 12.15

   unsignedint timer2_ctl;     // 12.16

    unsignedint timer2_pres_val;// 12.17

    unsignedint timer2_reload;  // 12.18

    unsignedint timer2_cnt;     // 12.19

    unsignedint timer3_ctl;     // 12.20

    unsignedint timer3_pres_val;// 12.21

    unsignedint timer3_reload;  // 12.22

    unsignedint timer3_cnt;     // 12.23

    unsigned int stcl_0;         // 12.24

...

    unsigned int atc_2;          // 12.29

    unsignedint timer0_reload;  // 12.30

    unsignedint timer1_reload;  // 12.31

};

#define STC_REG     ((volatile struct stc_regs *)RF_GRP(12, 0))

HDMI显示及定时器中断控制实验需要如下3个文件,如下:

1) 安装目录 The HDMI display and timer interrupt control experiment require the following 3 files, as follows:

1) Install Main.c under the directory \ SP7021 \ workspace \ sp7021 \ 文件夹下的main.cfolder

2) 安装目录 Install Timer.c in the directory \ SP7021 \ workspace \ sp7021 \ testapi \ util 文件夹下的timer.cfolder

3) 安装目录 Install Timer.h in the directory \ SP7021 \ workspace \ sp7021 \ include \ util文件夹下的timer.hutil folder

main.c

int main(void)

{

    printf("Build @%s, %s\n", __DATE__, __TIME__);

...

    sp_interrupt_setup(); /* interrupt manager module init */

    while(1);

}

Step1: 首先时系统及硬件初始化:hwFirst, system and hardware initialization: hw_init (); sys_init ();

Step2: HDMI 显示初始化设置display initialization setting disp_hdmi_init (),如下:, as follows:

void disp_hdmi_init()

{

    sp_disp_init();

...

    sp_osd_draw_string("Display and Timer test...",240,50,20,113);

}

The function of sp_enable_log_to_osd () 的功能实现将串口调试信息printfrealizes sending serial debugging information printf () 送到HDMI显示接口to HDMI display interface;

The function of sp_osd_draw_string()的功能实现在HDMI显示画面的指定坐标位置显示ASCII字符

Step3: 然后时调用timer.c中定义的计数定时器初始化函数timer_test_init();

Step4: 最后调用系统中断管理函数spstring () realizes the display of ASCII characters at the specified coordinate position of the HDMI display screen

Step3: Then call the count timer initialization function timer_test_init () defined in timer.c;

Step4: Finally call the system interrupt management function sp_interrupt_setup ();

上面的4个函数只有timerOf the above four functions, only timer_test_init () 是由用户根据应用需求编写,其它都是系统提供。is written by the user according to the application requirements, and the others are provided by the system.

The function body of timer_test_init () 的函数体实现在timer.c中,负责计数定时器timer3进行初始化工作,包含计数时钟的选择,定时值设置,定时器中断配置及对应的中断处理函数操作。is implemented in timer.c, which is responsible for the initialization of counting timer timer3, including the selection of counting clock, setting of timer value, timer interrupt configuration and corresponding interrupt processing function operation.

 timer.c

#include "common_all.h"

...

void timer3_callback(void)

void timer_test_init()

timer.c提供实现定时器控制实验的4个方法函数及宏定义,分别讲解如下:timer.c provides 4 method functions and macro definitions for implementing timer control experiments, which are explained as follows:

 void timer3_interrupt_control_mask(int enable)

...

        hal_interrupt_mask(TIMER3_INT);

    }

}用来控制计数定时器3中断的开与关;enable为0时关闭中断,为1时开启中断

Used to control the opening and closing of the count timer 3 interrupt; when the enable is 0, the interrupt is turned off, and when it is 1, the interrupt is turned on

static void timer3_isr_cfg() 

...

    STC_REG->timer3_ctl |= TIMER3_RUN;

}实现计数定时器每1秒产生1次中断,相关配置工作如下:

Realize that the count timer generates an interrupt every 1 second. The related configuration work is as follows:

1)    STC_REG->timer3_ctl = TIMER3_CONFIG_STC | TIMER3_RELOAD;计时钟选择STC 90KHZ 时钟;操作方式为重复操作;

Select clock as STC 90KHZ clock; operation mode is repeated operation

2)    STC_REG->timer3_pres_val = 999;

   计数时钟预处理设置为999,表示每收到1000个STC 90KHZ时钟,timer3 才计数1次;Counting clock preprocessing is set to 999, which means that timer3 only counts once every 1000 STC 90KHZ clocks are received;

3)    STC_REG->timer3_reload = TIMER3_TICKS;

计数预值设置为89, 表示timer3 计数90次后,重新开始计数The pre-count value is set to 89, which means that timer3 starts counting again after 90 times

4)    STC_REG->timer3_cnt = TIMER3_TICKS;计数初值设置为89,因为计数器是 UP to DOWN 的方式,即从初值计数到0完成1次操作;_TICKS;

The initial count value is set to 89, because the counter is UP to DOWN, that is, from the initial value count to 0 to complete 1 operation;

5)    hal_interrupt_configure(TIMER3_INT, 0, 1);这是系统提供的中断设置函数,共有3个参数需要设置,如下:

This is the interrupt setting function provided by the system. There are 3 parameters to be set, as follows:

     void hal_interrupt_configure(int vector, int level, int up)

     int vector :中断号,系统分配指定,每个定时器都有自己专门的中断号:

time0的中断号是151

time1的中断号是152

time2的中断号是153

time3的中断号是154

     int level :中断信号触发方式

0:边沿触发

1:电平触发:Interrupt number, assigned by the system, each timer has its own dedicated interrupt number:

The interrupt number of time0 is 151

The interrupt number of time1 is 152

The interrupt number of time2 is 153

The interrupt number of time3 is 154

     int level :Interrupt signal trigger mode

0:Edge trigger

1:Level trigger

     int  up    :中断信号极性方式

0:负极性(负边沿或负电平)

1:正极性(正边沿或正电平:Interrupt signal polarity

0:Negative polarity (negative edge or negative level)

1:Positive polarity (positive edge or positive level)

6)    timer3_interrupt_control_mask(1);    开启计数定时器3中断

Turn on count timer 3 interrupt

7)    STC_REG->timer3_ctl |= TIMER3_RUN;

    启动计数定时器3,计数开始    Start counting timer 3, counting starts

void timer3_callback(void)

...

    printf("@Timer3[%d]\n", ++g_repeat_cnt);

}是对应的中断处理函数操作,将中断的次数显示在IDE 环境的Terminal窗口,同时也送到HDMI终端显示。

It is the corresponding interrupt processing function operation, which displays the number of interrupts in the Terminal window of the IDE environment, and also sends them to the HDMI terminal for display.

 

void timer_test_init()

{

    static interrupt_operation timer3_opt;

...

    timer3_opt.interrupt_handler = timer3_callback;

    interrupt_register(&timer3_opt);

}

是中断初始化操作,完成中断函数的注册,包含上面定义的3个函数的调用

(&timer3_opt);

}

It is an interrupt initialization operation to complete the registration of the interrupt function, including the call of the three functions defined above

 

timer.h

#ifndef __TIMER_H__

...

void timer_test_init();

#endif // __TIMER_H__

When a function in timer.c 中的函数被其它文件中的函数调用时,需要在timer.h中申明在Plus1 IDE环境中compile后,下载到平台运行,在terminal窗口看到如下信息is called by a function in another file, it needs to be declared in timer.h

After compile in the Plus1 IDE environment, download to the platform to run, and see the following information in the terminal window

...