timer_control.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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_tick_t timeout = 50;
  23. rt_kprintf("periodic timer is timeout\n");
  24. count ++;
  25. /* 停止定时器自身 */
  26. if (count >= 8)
  27. {
  28. /* 控制定时器然后更改超时时间长度 */
  29. rt_timer_control(timer1, RT_TIMER_CTRL_SET_TIME, (void *)&timeout);
  30. count = 0;
  31. }
  32. }
  33. void timer_control_init()
  34. {
  35. /* 创建定时器1 */
  36. timer1 = rt_timer_create("timer1", /* 定时器名字是 timer1 */
  37. timeout1, /* 超时时回调的处理函数 */
  38. RT_NULL, /* 超时函数的入口参数 */
  39. 10, /* 定时长度,以OS Tick为单位,即10个OS Tick */
  40. RT_TIMER_FLAG_PERIODIC); /* 周期性定时器 */
  41. /* 启动定时器 */
  42. if (timer1 != RT_NULL)
  43. rt_timer_start(timer1);
  44. else
  45. tc_stat(TC_STAT_END | TC_STAT_FAILED);
  46. }
  47. #ifdef RT_USING_TC
  48. static void _tc_cleanup()
  49. {
  50. /* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */
  51. rt_enter_critical();
  52. /* 删除定时器对象 */
  53. rt_timer_delete(timer1);
  54. timer1 = RT_NULL;
  55. /* 调度器解锁 */
  56. rt_exit_critical();
  57. /* 设置TestCase状态 */
  58. tc_done(TC_STAT_PASSED);
  59. }
  60. int _tc_timer_control()
  61. {
  62. /* 设置TestCase清理回调函数 */
  63. tc_cleanup(_tc_cleanup);
  64. /* 执行定时器例程 */
  65. count = 0;
  66. timer_control_init();
  67. /* 返回TestCase运行的最长时间 */
  68. return 100;
  69. }
  70. /* 输出函数命令到finsh shell中 */
  71. FINSH_FUNCTION_EXPORT(_tc_timer_control, a timer control example);
  72. #else
  73. /* 用户应用入口 */
  74. int rt_application_init()
  75. {
  76. timer_control_init();
  77. return 0;
  78. }
  79. #endif