mbox_simple.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. * 这个程序会创建2个动态线程,一个静态的邮箱对象,其中一个线程往邮箱中发送邮件,
  13. * 一个线程往邮箱中收取邮件。
  14. */
  15. #include <rtthread.h>
  16. #include "tc_comm.h"
  17. /* 指向线程控制块的指针 */
  18. static rt_thread_t tid1 = RT_NULL;
  19. static rt_thread_t tid2 = RT_NULL;
  20. /* 邮箱控制块 */
  21. static struct rt_mailbox mb;
  22. /* 用于放邮件的内存池 */
  23. static char mb_pool[128];
  24. static char mb_str1[] = "I'm a mail!";
  25. static char mb_str2[] = "this is another mail!";
  26. /* 线程1入口 */
  27. static void thread1_entry(void* parameter)
  28. {
  29. unsigned char* str;
  30. while (1)
  31. {
  32. rt_kprintf("thread1: try to recv a mail\n");
  33. /* 从邮箱中收取邮件 */
  34. if (rt_mb_recv(&mb, (rt_uint32_t*)&str, RT_WAITING_FOREVER) == RT_EOK)
  35. {
  36. rt_kprintf("thread1: get a mail from mailbox, the content:%s\n", str);
  37. /* 延时10个OS Tick */
  38. rt_thread_delay(10);
  39. }
  40. }
  41. }
  42. /* 线程2入口 */
  43. static void thread2_entry(void* parameter)
  44. {
  45. rt_uint8_t count;
  46. count = 0;
  47. while (1)
  48. {
  49. count ++;
  50. if (count & 0x1)
  51. {
  52. /* 发送mb_str1地址到邮箱中 */
  53. rt_mb_send(&mb, (rt_uint32_t)&mb_str1[0]);
  54. }
  55. else
  56. {
  57. /* 发送mb_str2地址到邮箱中 */
  58. rt_mb_send(&mb, (rt_uint32_t)&mb_str2[0]);
  59. }
  60. /* 延时20个OS Tick */
  61. rt_thread_delay(20);
  62. }
  63. }
  64. int mbox_simple_init()
  65. {
  66. /* 初始化一个mailbox */
  67. rt_mb_init(&mb,
  68. "mbt", /* 名称是mbt */
  69. &mb_pool[0], /* 邮箱用到的内存池是mb_pool */
  70. sizeof(mb_pool)/4, /* 大小是mb_pool大小除以4,因为一封邮件的大小是4字节 */
  71. RT_IPC_FLAG_PRIO); /* 采用PRIO方式进行线程等待 */
  72. /* 创建线程1 */
  73. tid1 = rt_thread_create("t1",
  74. thread1_entry, RT_NULL, /* 线程入口是thread1_entry, 入口参数是RT_NULL */
  75. THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
  76. if (tid1 != RT_NULL)
  77. rt_thread_startup(tid1);
  78. else
  79. tc_stat(TC_STAT_END | TC_STAT_FAILED);
  80. /* 创建线程2 */
  81. tid2 = rt_thread_create("t2",
  82. thread2_entry, RT_NULL, /* 线程入口是thread2_entry, 入口参数是RT_NULL */
  83. THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
  84. if (tid2 != RT_NULL)
  85. rt_thread_startup(tid2);
  86. else
  87. tc_stat(TC_STAT_END | TC_STAT_FAILED);
  88. return 0;
  89. }
  90. #ifdef RT_USING_TC
  91. static void _tc_cleanup()
  92. {
  93. /* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */
  94. rt_enter_critical();
  95. /* 删除线程 */
  96. if (tid1 != RT_NULL && tid1->stat != RT_THREAD_CLOSE)
  97. rt_thread_delete(tid1);
  98. if (tid2 != RT_NULL && tid2->stat != RT_THREAD_CLOSE)
  99. rt_thread_delete(tid2);
  100. /* 执行邮箱对象脱离 */
  101. rt_mb_detach(&mb);
  102. /* 调度器解锁 */
  103. rt_exit_critical();
  104. /* 设置TestCase状态 */
  105. tc_done(TC_STAT_PASSED);
  106. }
  107. int _tc_mbox_simple()
  108. {
  109. /* 设置TestCase清理回调函数 */
  110. tc_cleanup(_tc_cleanup);
  111. mbox_simple_init();
  112. /* 返回TestCase运行的最长时间 */
  113. return 100;
  114. }
  115. /* 输出函数命令到finsh shell中 */
  116. FINSH_FUNCTION_EXPORT(_tc_mbox_simple, a simple mailbox example);
  117. #else
  118. /* 用户应用入口 */
  119. int rt_application_init()
  120. {
  121. mbox_simple_init();
  122. return 0;
  123. }
  124. #endif