timer_stop_self.c 1.8 KB

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