event_simple.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. * 一个线程定时发送事件 (事件3)
  15. * 一个线程定时发送事件 (事件5)
  16. */
  17. #include <rtthread.h>
  18. #include <time.h>
  19. #include "tc_comm.h"
  20. /* 指向线程控制块的指针 */
  21. static rt_thread_t tid1 = RT_NULL;
  22. static rt_thread_t tid2 = RT_NULL;
  23. static rt_thread_t tid3 = RT_NULL;
  24. /* 事件控制块 */
  25. static struct rt_event event;
  26. /* 线程1入口函数 */
  27. static void thread1_entry(void *param)
  28. {
  29. rt_uint32_t e;
  30. while (1)
  31. {
  32. /* receive first event */
  33. if (rt_event_recv(&event, ((1 << 3) | (1 << 5)),
  34. RT_EVENT_FLAG_AND | RT_EVENT_FLAG_CLEAR,
  35. RT_WAITING_FOREVER, &e) == RT_EOK)
  36. {
  37. rt_kprintf("thread1: AND recv event 0x%x\n", e);
  38. }
  39. rt_kprintf("thread1: delay 1s to prepare second event\n");
  40. rt_thread_delay(10);
  41. /* receive second event */
  42. if (rt_event_recv(&event, ((1 << 3) | (1 << 5)),
  43. RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
  44. RT_WAITING_FOREVER, &e) == RT_EOK)
  45. {
  46. rt_kprintf("thread1: OR recv event 0x%x\n", e);
  47. }
  48. rt_thread_delay(5);
  49. }
  50. }
  51. /* 线程2入口函数 */
  52. static void thread2_entry(void *param)
  53. {
  54. while (1)
  55. {
  56. rt_kprintf("thread2: send event1\n");
  57. rt_event_send(&event, (1 << 3));
  58. rt_thread_delay(10);
  59. }
  60. }
  61. /* 线程3入口函数 */
  62. static void thread3_entry(void *param)
  63. {
  64. while (1)
  65. {
  66. rt_kprintf("thread3: send event2\n");
  67. rt_event_send(&event, (1 << 5));
  68. rt_thread_delay(20);
  69. }
  70. }
  71. int event_simple_init()
  72. {
  73. /* 初始化事件对象 */
  74. rt_event_init(&event, "event", RT_IPC_FLAG_PRIO);
  75. /* 创建线程1 */
  76. tid1 = rt_thread_create("t1",
  77. thread1_entry, RT_NULL, /* 线程入口是thread1_entry, 入口参数是RT_NULL */
  78. THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
  79. if (tid1 != RT_NULL)
  80. rt_thread_startup(tid1);
  81. else
  82. tc_stat(TC_STAT_END | TC_STAT_FAILED);
  83. /* 创建线程2 */
  84. tid2 = rt_thread_create("t2",
  85. thread2_entry, RT_NULL, /* 线程入口是thread2_entry, 入口参数是RT_NULL */
  86. THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
  87. if (tid2 != RT_NULL)
  88. rt_thread_startup(tid2);
  89. else
  90. tc_stat(TC_STAT_END | TC_STAT_FAILED);
  91. /* 创建线程3 */
  92. tid3 = rt_thread_create("t3",
  93. thread3_entry, RT_NULL, /* 线程入口是thread3_entry, 入口参数是RT_NULL */
  94. THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
  95. if (tid3 != RT_NULL)
  96. rt_thread_startup(tid3);
  97. else
  98. tc_stat(TC_STAT_END | TC_STAT_FAILED);
  99. return 0;
  100. }
  101. #ifdef RT_USING_TC
  102. static void _tc_cleanup()
  103. {
  104. /* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */
  105. rt_enter_critical();
  106. /* 删除线程 */
  107. if (tid1 != RT_NULL && tid1->stat != RT_THREAD_CLOSE)
  108. rt_thread_delete(tid1);
  109. if (tid2 != RT_NULL && tid2->stat != RT_THREAD_CLOSE)
  110. rt_thread_delete(tid2);
  111. if (tid3 != RT_NULL && tid3->stat != RT_THREAD_CLOSE)
  112. rt_thread_delete(tid3);
  113. /* 执行事件对象脱离 */
  114. rt_event_detach(&event);
  115. /* 调度器解锁 */
  116. rt_exit_critical();
  117. /* 设置TestCase状态 */
  118. tc_done(TC_STAT_PASSED);
  119. }
  120. int _tc_event_simple()
  121. {
  122. /* 设置TestCase清理回调函数 */
  123. tc_cleanup(_tc_cleanup);
  124. event_simple_init();
  125. /* 返回TestCase运行的最长时间 */
  126. return 100;
  127. }
  128. /* 输出函数命令到finsh shell中 */
  129. FINSH_FUNCTION_EXPORT(_tc_event_simple, a simple event example);
  130. #else
  131. /* 用户应用入口 */
  132. int rt_application_init()
  133. {
  134. event_simple_init();
  135. return 0;
  136. }
  137. #endif