timer_stop_self.c 1.9 KB

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