1 /* 2 * 程序清单:定时器例程 3 * 4 * 这个程序会初始化2个静态定时器,一个是单次定时,一个是周期性的定时 5 */ 6 #include <rtthread.h> 7 #include "tc_comm.h" 8 9 /* 定时器的控制块 */ 10 static struct rt_timer timer1; 11 static struct rt_timer 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_static_init() 26 { 27 /* 初始化定时器 */ 28 rt_timer_init(&timer1, "timer1", /* 定时器名字是 timer1 */ 29 timeout1, /* 超时时回调的处理函数 */ 30 RT_NULL, /* 超时函数的入口参数 */ 31 10, /* 定时长度,以OS Tick为单位,即10个OS Tick */ 32 RT_TIMER_FLAG_PERIODIC); /* 周期性定时器 */ 33 rt_timer_init(&timer2, "timer2", /* 定时器名字是 timer2 */ 34 timeout2, /* 超时时回调的处理函数 */ 35 RT_NULL, /* 超时函数的入口参数 */ 36 30, /* 定时长度为30个OS Tick */ 37 RT_TIMER_FLAG_ONE_SHOT); /* 单次定时器 */ 38 39 /* 启动定时器 */ 40 rt_timer_start(&timer1); 41 rt_timer_start(&timer2); 42 } 43 44 #ifdef RT_USING_TC 45 static void _tc_cleanup() 46 { 47 /* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */ 48 rt_enter_critical(); 49 50 /* 执行定时器脱离 */ 51 rt_timer_detach(&timer1); 52 rt_timer_detach(&timer2); 53 54 /* 调度器解锁 */ 55 rt_exit_critical(); 56 57 /* 设置TestCase状态 */ 58 tc_done(TC_STAT_PASSED); 59 } 60 61 int _tc_timer_static() 62 { 63 /* 设置TestCase清理回调函数 */ 64 tc_cleanup(_tc_cleanup); 65 66 /* 执行定时器例程 */ 67 timer_static_init(); 68 69 /* 返回TestCase运行的最长时间 */ 70 return 100; 71 } 72 /* 输出函数命令到finsh shell中 */ 73 FINSH_FUNCTION_EXPORT(_tc_timer_static, a static timer example); 74 #else 75 /* 用户应用入口 */ 76 int rt_application_init() 77 { 78 timer_static_init(); 79 80 return 0; 81 } 82 #endif 83