event_sample.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-08-24 yangjie the first version
  9. * 2020-10-17 Meco Man translate to English comment
  10. */
  11. /*
  12. * Demo: event(s)
  13. *
  14. * This demo creates two threads and one statical event:
  15. * 1) thread #1: pend and receive events
  16. * 2) thread #2: sent events (Event3 and Event5)
  17. *
  18. * read more:
  19. * https://www.rt-thread.io/document/site/programming-manual/ipc1/ipc1/#event
  20. */
  21. #include <rtthread.h>
  22. #define THREAD_PRIORITY 9
  23. #define THREAD_TIMESLICE 5
  24. #define EVENT_FLAG3 (1 << 3)
  25. #define EVENT_FLAG5 (1 << 5)
  26. /* ECB (Event Control Block) */
  27. static struct rt_event event;
  28. #ifdef rt_align
  29. rt_align(RT_ALIGN_SIZE)
  30. #else
  31. ALIGN(RT_ALIGN_SIZE)
  32. #endif
  33. static char thread1_stack[1024]; /* thread stack 1024 Byte*/
  34. static struct rt_thread thread1; /* TCB (Thread Control Block) */
  35. /* thread #1 entry function */
  36. static void thread1_recv_event(void *param)
  37. {
  38. rt_uint32_t e;
  39. /*
  40. first time to receive event(s):
  41. EITHER Event3 OR Event5 happened can resume thread1
  42. and then clear conrresponding event(s)' flag
  43. */
  44. if (rt_event_recv(&event, (EVENT_FLAG3 | EVENT_FLAG5),
  45. RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
  46. RT_WAITING_FOREVER, &e) == RT_EOK)
  47. {
  48. rt_kprintf("thread1: OR recv event 0x%x\n", e);
  49. }
  50. rt_kprintf("thread1: delay 1s to prepare the second event\n");
  51. rt_thread_mdelay(1000);
  52. /*
  53. second time to receive event(s):
  54. BOTH Event3 AND Event5 happened can resume thread1
  55. and then clear conrresponding event(s)' flag
  56. */
  57. if (rt_event_recv(&event, (EVENT_FLAG3 | EVENT_FLAG5),
  58. RT_EVENT_FLAG_AND | RT_EVENT_FLAG_CLEAR,
  59. RT_WAITING_FOREVER, &e) == RT_EOK)
  60. {
  61. rt_kprintf("thread1: AND recv event 0x%x\n", e);
  62. }
  63. rt_kprintf("thread1 leave.\n");
  64. }
  65. #ifdef rt_align
  66. rt_align(RT_ALIGN_SIZE)
  67. #else
  68. ALIGN(RT_ALIGN_SIZE)
  69. #endif
  70. static char thread2_stack[1024]; /* thread stack 1024 Byte*/
  71. static struct rt_thread thread2; /* TCB (Thread Control Block) */
  72. /* thread #2 entry function */
  73. static void thread2_send_event(void *param)
  74. {
  75. rt_kprintf("thread2: send event3\n");
  76. rt_event_send(&event, EVENT_FLAG3);
  77. rt_thread_mdelay(200);
  78. rt_kprintf("thread2: send event5\n");
  79. rt_event_send(&event, EVENT_FLAG5);
  80. rt_thread_mdelay(200);
  81. rt_kprintf("thread2: send event3\n");
  82. rt_event_send(&event, EVENT_FLAG3);
  83. rt_kprintf("thread2 leave.\n");
  84. }
  85. int event_sample(void)
  86. {
  87. rt_err_t result;
  88. /* initiate the event (statically) */
  89. result = rt_event_init(&event, "event", RT_IPC_FLAG_PRIO);
  90. if (result != RT_EOK)
  91. {
  92. rt_kprintf("init event failed.\n");
  93. return -1;
  94. }
  95. /* initiate the thread #1 (statically) */
  96. rt_thread_init(&thread1,
  97. "thread1",
  98. thread1_recv_event,
  99. RT_NULL,
  100. &thread1_stack[0],
  101. sizeof(thread1_stack),
  102. THREAD_PRIORITY - 1, THREAD_TIMESLICE);
  103. #ifdef RT_USING_SMP
  104. /* Bind threads to the same core to avoid messy log output when multiple cores are enabled */
  105. rt_thread_control(&thread1, RT_THREAD_CTRL_BIND_CPU, (void*)0);
  106. #endif
  107. rt_thread_startup(&thread1); /* start thread #1 */
  108. /* initiate the thread #2 (statically) */
  109. rt_thread_init(&thread2,
  110. "thread2",
  111. thread2_send_event,
  112. RT_NULL,
  113. &thread2_stack[0],
  114. sizeof(thread2_stack),
  115. THREAD_PRIORITY, THREAD_TIMESLICE);
  116. #ifdef RT_USING_SMP
  117. /* Bind threads to the same core to avoid messy log output when multiple cores are enabled */
  118. rt_thread_control(&thread2, RT_THREAD_CTRL_BIND_CPU, (void*)0);
  119. #endif
  120. rt_thread_startup(&thread2); /* start thread #2 */
  121. return 0;
  122. }
  123. /* export the msh command */
  124. MSH_CMD_EXPORT(event_sample, event sample);