event_sample.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (c) 2006-2018, 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/thread-sync/thread-sync/#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. ALIGN(RT_ALIGN_SIZE)
  29. static char thread1_stack[1024]; /* thread stack 1024 Byte*/
  30. static struct rt_thread thread1; /* TCB (Thread Control Block) */
  31. /* thread #1 entry function */
  32. static void thread1_recv_event(void *param)
  33. {
  34. rt_uint32_t e;
  35. /*
  36. first time to receive event(s):
  37. EITHER Event3 OR Event5 happened can resume thread1
  38. and then clear conrresponding event(s)' flag
  39. */
  40. if (rt_event_recv(&event, (EVENT_FLAG3 | EVENT_FLAG5),
  41. RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
  42. RT_WAITING_FOREVER, &e) == RT_EOK)
  43. {
  44. rt_kprintf("thread1: OR recv event 0x%x\n", e);
  45. }
  46. rt_kprintf("thread1: delay 1s to prepare the second event\n");
  47. rt_thread_mdelay(1000);
  48. /*
  49. second time to receive event(s):
  50. BOTH Event3 AND Event5 happened can resume thread1
  51. and then clear conrresponding event(s)' flag
  52. */
  53. if (rt_event_recv(&event, (EVENT_FLAG3 | EVENT_FLAG5),
  54. RT_EVENT_FLAG_AND | RT_EVENT_FLAG_CLEAR,
  55. RT_WAITING_FOREVER, &e) == RT_EOK)
  56. {
  57. rt_kprintf("thread1: AND recv event 0x%x\n", e);
  58. }
  59. rt_kprintf("thread1 leave.\n");
  60. }
  61. ALIGN(RT_ALIGN_SIZE)
  62. static char thread2_stack[1024]; /* thread stack 1024 Byte*/
  63. static struct rt_thread thread2; /* TCB (Thread Control Block) */
  64. /* thread #2 entry function */
  65. static void thread2_send_event(void *param)
  66. {
  67. rt_kprintf("thread2: send event3\n");
  68. rt_event_send(&event, EVENT_FLAG3);
  69. rt_thread_mdelay(200);
  70. rt_kprintf("thread2: send event5\n");
  71. rt_event_send(&event, EVENT_FLAG5);
  72. rt_thread_mdelay(200);
  73. rt_kprintf("thread2: send event3\n");
  74. rt_event_send(&event, EVENT_FLAG3);
  75. rt_kprintf("thread2 leave.\n");
  76. }
  77. int event_sample(void)
  78. {
  79. rt_err_t result;
  80. /* initiate the event (statically) */
  81. result = rt_event_init(&event, "event", RT_IPC_FLAG_FIFO);
  82. if (result != RT_EOK)
  83. {
  84. rt_kprintf("init event failed.\n");
  85. return -1;
  86. }
  87. /* initiate the thread #1 (statically) */
  88. rt_thread_init(&thread1,
  89. "thread1",
  90. thread1_recv_event,
  91. RT_NULL,
  92. &thread1_stack[0],
  93. sizeof(thread1_stack),
  94. THREAD_PRIORITY - 1, THREAD_TIMESLICE);
  95. rt_thread_startup(&thread1); /* start thread #1 */
  96. /* initiate the thread #2 (statically) */
  97. rt_thread_init(&thread2,
  98. "thread2",
  99. thread2_send_event,
  100. RT_NULL,
  101. &thread2_stack[0],
  102. sizeof(thread2_stack),
  103. THREAD_PRIORITY, THREAD_TIMESLICE);
  104. rt_thread_startup(&thread2); /* start thread #2 */
  105. return 0;
  106. }
  107. /* export the msh command */
  108. MSH_CMD_EXPORT(event_sample, event sample);