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