timer_timeout.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. * 这个程序会创建3个动态线程,一个线程会从消息队列中收取消息;一个线程会定时给消
  13. * 息队列发送消息;一个线程会定时给消息队列发送紧急消息。
  14. */
  15. #include <rtthread.h>
  16. #include "tc_comm.h"
  17. /* 指向线程控制块的指针 */
  18. static rt_thread_t tid = RT_NULL;
  19. /* 消息队列控制块 */
  20. static struct rt_messagequeue mq;
  21. /* 消息队列中用到的放置消息的内存池 */
  22. static char msg_pool[2048];
  23. /* 定时器的控制块 */
  24. static struct rt_timer timer;
  25. static rt_uint16_t no = 0;
  26. static void timer_timeout(void* parameter)
  27. {
  28. char buf[32];
  29. rt_uint32_t length;
  30. length = rt_snprintf(buf, sizeof(buf), "message %d", no++);
  31. rt_mq_send(&mq, &buf[0], length);
  32. }
  33. /* 线程入口函数 */
  34. static void thread_entry(void* parameter)
  35. {
  36. char buf[64];
  37. rt_err_t result;
  38. /* 初始化定时器 */
  39. rt_timer_init(&timer, "timer", /* 定时器名字是 timer1 */
  40. timer_timeout, /* 超时时回调的处理函数 */
  41. RT_NULL, /* 超时函数的入口参数 */
  42. 1, /* 定时长度,以OS Tick为单位,即1个OS Tick */
  43. RT_TIMER_FLAG_PERIODIC); /* 周期性定时器 */
  44. while (1)
  45. {
  46. rt_memset(&buf[0], 0, sizeof(buf));
  47. /* 从消息队列中接收消息 */
  48. result = rt_mq_recv(&mq, &buf[0], sizeof(buf), 1);
  49. if (result == RT_EOK)
  50. {
  51. rt_kprintf("recv msg: %s\n", buf);
  52. }
  53. else if (result == -RT_ETIMEOUT)
  54. {
  55. rt_kprintf("recv msg timeout\n");
  56. }
  57. }
  58. }
  59. int timer_timeout_init()
  60. {
  61. /* 初始化消息队列 */
  62. rt_mq_init(&mq, "mqt",
  63. &msg_pool[0], /* 内存池指向msg_pool */
  64. 128 - sizeof(void*), /* 每个消息的大小是 128 - void* */
  65. sizeof(msg_pool), /* 内存池的大小是msg_pool的大小 */
  66. RT_IPC_FLAG_FIFO); /* 如果有多个线程等待,按照先来先得到的方法分配消息 */
  67. /* 创建线程 */
  68. tid = rt_thread_create("t",
  69. thread_entry, RT_NULL, /* 线程入口是thread_entry, 入口参数是RT_NULL */
  70. THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
  71. if (tid != RT_NULL)
  72. rt_thread_startup(tid);
  73. else
  74. tc_stat(TC_STAT_END | TC_STAT_FAILED);
  75. return 0;
  76. }
  77. #ifdef RT_USING_TC
  78. static void _tc_cleanup()
  79. {
  80. /* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */
  81. rt_enter_critical();
  82. /* 删除线程 */
  83. if (tid != RT_NULL && tid->stat != RT_THREAD_CLOSE)
  84. rt_thread_delete(tid);
  85. /* 执行消息队列对象脱离 */
  86. rt_mq_detach(&mq);
  87. /* 执行定时器脱离 */
  88. rt_timer_detach(&timer);
  89. /* 调度器解锁 */
  90. rt_exit_critical();
  91. /* 设置TestCase状态 */
  92. tc_done(TC_STAT_PASSED);
  93. }
  94. int _tc_timer_timeout()
  95. {
  96. /* 设置TestCase清理回调函数 */
  97. tc_cleanup(_tc_cleanup);
  98. timer_timeout_init();
  99. /* 返回TestCase运行的最长时间 */
  100. return 100;
  101. }
  102. /* 输出函数命令到finsh shell中 */
  103. FINSH_FUNCTION_EXPORT(_tc_timer_timeout, a thread timer testcase);
  104. #else
  105. /* 用户应用入口 */
  106. int rt_application_init()
  107. {
  108. timer_timeout_init();
  109. return 0;
  110. }
  111. #endif