xref: /nrf52832-nimble/rt-thread/examples/kernel/timer_stop_self.c (revision 104654410c56c573564690304ae786df310c91fc)
1 /*
2  * 程序清单:动态定时器例程
3  *
4  * 这个例程会创建1个动态周期型定时器对象
5  */
6 #include <rtthread.h>
7 #include "tc_comm.h"
8 
9 /* 定时器的控制块 */
10 static rt_timer_t timer1;
11 static rt_uint8_t count;
12 
13 /* 定时器超时函数 */
timeout1(void * parameter)14 static void timeout1(void* parameter)
15 {
16     rt_kprintf("periodic timer is timeout\n");
17 
18     count ++;
19     /* 停止定时器自身 */
20     if (count >= 8)
21     {
22         /* 停止定时器 */
23         rt_timer_stop(timer1);
24         count = 0;
25     }
26 }
27 
timer_stop_self_init()28 void timer_stop_self_init()
29 {
30     /* 创建定时器1 */
31     timer1 = rt_timer_create("timer1",  /* 定时器名字是 timer1 */
32         timeout1, /* 超时时回调的处理函数 */
33         RT_NULL, /* 超时函数的入口参数 */
34         10, /* 定时长度,以OS Tick为单位,即10个OS Tick */
35         RT_TIMER_FLAG_PERIODIC); /* 周期性定时器 */
36     /* 启动定时器 */
37     if (timer1 != RT_NULL)
38         rt_timer_start(timer1);
39     else
40         tc_stat(TC_STAT_END | TC_STAT_FAILED);
41 }
42 
43 #ifdef RT_USING_TC
_tc_cleanup()44 static void _tc_cleanup()
45 {
46     /* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */
47     rt_enter_critical();
48 
49     /* 删除定时器对象 */
50     rt_timer_delete(timer1);
51     timer1 = RT_NULL;
52 
53     /* 调度器解锁 */
54     rt_exit_critical();
55 
56     /* 设置TestCase状态 */
57     tc_done(TC_STAT_PASSED);
58 }
59 
_tc_timer_stop_self()60 int _tc_timer_stop_self()
61 {
62     /* 设置TestCase清理回调函数 */
63     tc_cleanup(_tc_cleanup);
64 
65     /* 执行定时器例程 */
66     count = 0;
67     timer_stop_self_init();
68 
69     /* 返回TestCase运行的最长时间 */
70     return 100;
71 }
72 /* 输出函数命令到finsh shell中 */
73 FINSH_FUNCTION_EXPORT(_tc_timer_stop_self, a dynamic timer example);
74 #else
75 /* 用户应用入口 */
rt_application_init()76 int rt_application_init()
77 {
78     timer_stop_self_init();
79 
80     return 0;
81 }
82 #endif
83