luat_msgbus_rtt.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // 导入rt-thread头文件, 用它的消息队列来实现
  2. #include <rtthread.h>
  3. #include "luat_base.h"
  4. #include "luat_malloc.h"
  5. #include "luat_msgbus.h"
  6. #define DBG_TAG "luat.msgbus"
  7. #define DBG_LVL DBG_INFO
  8. #include <rtdbg.h>
  9. ALIGN(RT_ALIGN_SIZE)
  10. static rt_uint8_t msg_pool[4*1024];
  11. static struct rt_messagequeue mq = {0};
  12. // static void *msgdata = {0};
  13. void luat_msgbus_init(void) {
  14. rt_err_t result;
  15. /* 初始化消息队列 */
  16. result = rt_mq_init(&mq,
  17. "mqt",
  18. &msg_pool[0], /* 内存池指向 msg_pool */
  19. sizeof(rtos_msg_t), /* 每个消息的大小是 8 字节 */
  20. 4*1024, /* 内存池的大小是 msg_pool 的大小 */
  21. RT_IPC_FLAG_FIFO); /* 如果有多个线程等待,按照先来先得到的方法分配消息 */
  22. if (result) {
  23. // pass
  24. }
  25. }
  26. // void* luat_msgbus_data() {
  27. // return msgdata;
  28. // }
  29. uint32_t luat_msgbus_put(rtos_msg_t* msg, size_t timeout) {
  30. if (!mq.msg_size) // 未初始化
  31. return 0;
  32. LOG_D(">>luat_msgbus_put msg->ptr= %08X", msg->ptr);
  33. int re = rt_mq_send(&mq, msg, sizeof(rtos_msg_t));
  34. if (re) {
  35. LOG_W("msgbus is FULL!!!!");
  36. }
  37. return re;
  38. }
  39. uint32_t luat_msgbus_get(rtos_msg_t* msg, size_t timeout) {
  40. rt_err_t result;
  41. result = rt_mq_recv(&mq, msg, sizeof(rtos_msg_t), timeout);
  42. if (result == RT_EOK) {
  43. // msgdata = msg->ptr;
  44. LOG_D("luat_msgbus_get msg->ptr= %08X", msg->ptr);
  45. return 0;
  46. }
  47. else {
  48. // msgdata = NULL;
  49. return result;
  50. }
  51. }
  52. uint32_t luat_msgbus_freesize(void) {
  53. return 1;
  54. }