msgq_sample.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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: message queue
  13. *
  14. * This demo creates two threads and one message queue:
  15. * 1) thread #1: receive message(s) from message queue
  16. * 2) thread #2: send normal and urgent messages to message queue
  17. *
  18. * read more:
  19. * https://www.rt-thread.io/document/site/programming-manual/ipc2/ipc2/#message-queue
  20. */
  21. #include <rtthread.h>
  22. #define THREAD_PRIORITY 25
  23. #define THREAD_TIMESLICE 5
  24. /* message queue control block */
  25. static struct rt_messagequeue mq;
  26. /* the memory pool used to place messages in the message queue */
  27. static rt_uint8_t msg_pool[2048];
  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];
  34. static struct rt_thread thread1;
  35. /* thread #1 entry function */
  36. static void thread1_entry(void *parameter)
  37. {
  38. char buf = 0;
  39. rt_uint8_t cnt = 0;
  40. while (1)
  41. {
  42. /* pend and receive message(s) from message queue */
  43. #if (RTTHREAD_VERSION >= RT_VERSION_CHECK(5, 0, 1))
  44. if (rt_mq_recv(&mq, &buf, sizeof(buf), RT_WAITING_FOREVER) > 0)
  45. #else
  46. if (rt_mq_recv(&mq, &buf, sizeof(buf), RT_WAITING_FOREVER) == RT_EOK)
  47. #endif
  48. {
  49. rt_kprintf("thread1: recv msg from msg queue, the content:%c\n", buf);
  50. if (cnt == 19)
  51. {
  52. break;
  53. }
  54. }
  55. cnt++;
  56. /* delay for 50ms */
  57. rt_thread_mdelay(50);
  58. }
  59. rt_kprintf("thread1: detach mq \n");
  60. rt_mq_detach(&mq);
  61. }
  62. #ifdef rt_align
  63. rt_align(RT_ALIGN_SIZE)
  64. #else
  65. ALIGN(RT_ALIGN_SIZE)
  66. #endif
  67. static char thread2_stack[1024];
  68. static struct rt_thread thread2;
  69. /* thread #2 entry function */
  70. static void thread2_entry(void *parameter)
  71. {
  72. int result;
  73. char buf = 'A';
  74. rt_uint8_t cnt = 0;
  75. while (1)
  76. {
  77. if (cnt == 8)
  78. {
  79. /* send one URGENT message to the message queue */
  80. result = rt_mq_urgent(&mq, &buf, 1);
  81. if (result != RT_EOK)
  82. {
  83. rt_kprintf("rt_mq_urgent ERR\n");
  84. }
  85. else
  86. {
  87. rt_kprintf("thread2: send urgent message - %c\n", buf);
  88. }
  89. }
  90. else if (cnt >= 20) /* exit */
  91. {
  92. rt_kprintf("message queue stop send, thread2 quit\n");
  93. break;
  94. }
  95. else
  96. {
  97. /* send one message to the message queue */
  98. result = rt_mq_send(&mq, &buf, 1);
  99. if (result != RT_EOK)
  100. {
  101. rt_kprintf("rt_mq_send ERR\n");
  102. }
  103. rt_kprintf("thread2: send message - %c\n", buf);
  104. }
  105. buf++;
  106. cnt++;
  107. /* delay for 5ms */
  108. rt_thread_mdelay(5);
  109. }
  110. }
  111. int msgq_sample(void)
  112. {
  113. rt_err_t result;
  114. /* initiate a message queue */
  115. result = rt_mq_init(&mq,
  116. "mqt",
  117. &msg_pool[0], /* msg_pool's address */
  118. 1, /* the size of each message is 1 byte */
  119. sizeof(msg_pool), /* The size of the memory pool is the size of msg_pool */
  120. RT_IPC_FLAG_PRIO);
  121. if (result != RT_EOK)
  122. {
  123. rt_kprintf("init message queue failed.\n");
  124. return -1;
  125. }
  126. rt_thread_init(&thread1,
  127. "thread1",
  128. thread1_entry,
  129. RT_NULL,
  130. &thread1_stack[0],
  131. sizeof(thread1_stack),
  132. THREAD_PRIORITY, THREAD_TIMESLICE);
  133. #ifdef RT_USING_SMP
  134. /* Bind threads to the same core to avoid messy log output when multiple cores are enabled */
  135. rt_thread_control(&thread1, RT_THREAD_CTRL_BIND_CPU, (void*)0);
  136. #endif
  137. rt_thread_startup(&thread1);
  138. rt_thread_init(&thread2,
  139. "thread2",
  140. thread2_entry,
  141. RT_NULL,
  142. &thread2_stack[0],
  143. sizeof(thread2_stack),
  144. THREAD_PRIORITY, THREAD_TIMESLICE);
  145. #ifdef RT_USING_SMP
  146. /* Bind threads to the same core to avoid messy log output when multiple cores are enabled */
  147. rt_thread_control(&thread2, RT_THREAD_CTRL_BIND_CPU, (void*)0);
  148. #endif
  149. rt_thread_startup(&thread2);
  150. return 0;
  151. }
  152. /* export the msh command */
  153. MSH_CMD_EXPORT(msgq_sample, msgq sample);