RT-Thread 入门学习笔记 - 熟悉邮箱rt_mailbox的使用

发布于 2021-03-30 08:20:59

简介

  • 邮箱是用来传输邮件的,如果没人取件,会暂存下来。
  • RT-Thread的邮箱,传递的是一个4字节(32位)值,可以传值,可以传指针(32位MCU,指针为32位)。
  • 邮箱使用比消息队列更精简,简单的命令的传输,可以使用邮箱,资源占用可以更小。

2021-03-30_075724.png

相关API

  • 在rt-thread内核代码:rtthread.h中,可以找到邮箱的各个API
  • 邮箱API的实现,在:ipc.c,可以认为邮箱属于线程间通信的一种。
  • 邮箱:mailbox相关API如下
rt_mb_init \ rt_mb_detach :静态初始化 \ 脱离,成对使用。
rt_mb_create \ rt_mb_delete : 动态创建 \ 删除,成对使用。
一般邮箱初始化后,就不再删除,可以使用静态初始化的方式。
rt_mb_send : 发送邮件,可以工作在在线程、回调函数里
rt_mb_send_wait :有超时的发送,工作在线程
rt_mb_recv :等待接收邮件,注意是队列式的接收,工作在线程里,循环等待接收
rt_mb_control : 提供一个初始化RESET邮箱的命令,不常用。
  • 开来,邮箱的API,跟消息队列很相似。

测试实例

  • 本例程用于线程间的通信,邮箱的消息,采用结构体指针。结构体的数据,动态申请内存的方式。
  • 注意邮箱接收,需要判断返回值是否 == RT_EOK,如下:
/* 因为反正RT_EOK时为:0, 所以if判断时,需要 == RT_EOK。否则接收正确后,if不成立 */
if (rt_mq_recv(&t3_mq, &buf, sizeof(buf), RT_WAITING_FOREVER) == RT_EOK)
  • 用于验证邮箱传输是否正常,用于验证内存的申请与释放是否成对。
#include <rtthread.h>

/* 定义静态全局的邮箱:非指针 */
static struct rt_mailbox t1_mb;
static struct rt_mailbox t2_mb;
static struct rt_mailbox t3_mb;

/* 邮箱的大小(池子):一般一个消息为4个字节,这里采用数组 */
static rt_uint8_t msg_buf1[128];
static rt_uint8_t msg_buf2[128];
static rt_uint8_t msg_buf3[128];

/* 测试线程 */
#define THREAD1_STACK_SIZE   1024
#define THREAD1_PRIORITY     20
#define THREAD1_TIMESLICE    10

#define THREAD2_STACK_SIZE   1024
#define THREAD2_PRIORITY     20
#define THREAD2_TIMESLICE    10

#define THREAD3_STACK_SIZE   1024
#define THREAD3_PRIORITY     20
#define THREAD3_TIMESLICE    10

static rt_thread_t tid1 = RT_NULL;
static rt_thread_t tid2 = RT_NULL;
static rt_thread_t tid3 = RT_NULL;

extern void list_mem(void);

/* 测试传输的数据 */
struct mb_msg
{
    rt_uint8_t *data_ptr;
    rt_uint32_t data_size;
};

/* 邮箱初始化,使用前需要初始化 */
static void mb_init(void)
{
    rt_err_t result;

    /* 初始化邮箱1 */
    result = rt_mb_init(&t1_mb,
                        "t1mb",
                        &msg_buf1[0],            /* 内存池指向 msg_pool */
                        sizeof(msg_buf1) / 4,    /* 邮箱中的邮件数目,一封邮件占用4字节 */
                        RT_IPC_FLAG_FIFO);       /* 采用FIFO方式进行线程等待 */

    if (result != RT_EOK)
    {
        rt_kprintf("init thread1 mailbox failed.\n");
        return;
    }

    /* 初始化邮箱2 */
    result = rt_mb_init(&t2_mb,
                        "t2mb",
                        &msg_buf2[0],            /* 内存池指向 msg_pool */
                        sizeof(msg_buf2) / 4,    /* 邮箱中的邮件数目,一封邮件占用4字节 */
                        RT_IPC_FLAG_FIFO);       /* 采用FIFO方式进行线程等待 */

    if (result != RT_EOK)
    {
        rt_kprintf("init thread2 mailbox failed.\n");
        return;
    }

    /* 初始化邮箱3 */
    result = rt_mb_init(&t3_mb,
                        "t3mb",
                        &msg_buf3[0],            /* 内存池指向 msg_pool */
                        sizeof(msg_buf3) / 4,    /* 邮箱中的邮件数目,一封邮件占用4字节 */
                        RT_IPC_FLAG_FIFO);       /* 采用FIFO方式进行线程等待 */

    if (result != RT_EOK)
    {
        rt_kprintf("init thread3 mailbox failed.\n");
        return;
    }
    rt_kprintf("mb_init ok.\n");
}

/* 邮箱测试线程,按次序收与发 */
static void thread1_entry(void *param)
{
    struct mb_msg *msg_recv_ptr1;
    struct mb_msg *msg_send_ptr1;
    char sbuf[6] = {'T', 'a', 's', 'k', '1', '.'};

    while(1)
    {
        if (rt_mb_recv(&t1_mb, (rt_ubase_t *)&msg_recv_ptr1, RT_WAITING_FOREVER) == RT_EOK)
        {
            rt_kprintf("thread 1:[recv=%s], print 1.\n", msg_recv_ptr1->data_ptr);
            rt_thread_mdelay(10);
            rt_free(msg_recv_ptr1->data_ptr);
            list_mem();
            rt_free(msg_recv_ptr1);
            list_mem();
            rt_thread_mdelay(500);
            msg_send_ptr1 = (struct mb_msg *)rt_malloc(sizeof(struct mb_msg));
            msg_send_ptr1->data_size = sizeof(sbuf);
            msg_send_ptr1->data_ptr = (rt_uint8_t *)rt_malloc(msg_send_ptr1->data_size);
            rt_memcpy(msg_send_ptr1->data_ptr, sbuf, sizeof(sbuf));
            rt_kprintf("thread 1:[send=%s]\n", msg_send_ptr1->data_ptr);
            rt_mb_send(&t2_mb, (rt_uint32_t)msg_send_ptr1);
        }
    }
}

static void thread2_entry(void *param)
{
    struct mb_msg *msg_recv_ptr2;
    struct mb_msg *msg_send_ptr2;

    char sbuf[6] = {'T', 'a', 's', 'k', '2', '.'};

    while(1)
    {
        if (rt_mb_recv(&t2_mb, (rt_ubase_t *)&msg_recv_ptr2, RT_WAITING_FOREVER) == RT_EOK)
        {
            rt_kprintf("thread 2:[recv=%s], print 2.\n", msg_recv_ptr2->data_ptr);
            rt_thread_mdelay(10);
            rt_free(msg_recv_ptr2->data_ptr);
            list_mem();
            rt_free(msg_recv_ptr2);
            list_mem();
            rt_thread_mdelay(500);
            msg_send_ptr2 = (struct mb_msg *)rt_malloc(sizeof(struct mb_msg));
            msg_send_ptr2->data_size = sizeof(sbuf);
            msg_send_ptr2->data_ptr = (rt_uint8_t *)rt_malloc(msg_send_ptr2->data_size);
            rt_memcpy(msg_send_ptr2->data_ptr, sbuf, sizeof(sbuf));
            rt_kprintf("thread 2:[send=%s]\n", msg_send_ptr2->data_ptr);
            rt_mb_send(&t3_mb, (rt_uint32_t)msg_send_ptr2);
        }
    }
}

static void thread3_entry(void *param)
{
    struct mb_msg *msg_recv_ptr3;
    struct mb_msg *msg_send_ptr3;
    char sbuf[6] = {'T', 'a', 's', 'k', '3', '.'};

    while(1)
    {
        if (rt_mb_recv(&t3_mb, (rt_ubase_t *)&msg_recv_ptr3, RT_WAITING_FOREVER) == RT_EOK)
        {
            rt_kprintf("thread 3:[recv=%s], print 3.\n", msg_recv_ptr3->data_ptr);
            rt_thread_mdelay(10);
            rt_free(msg_recv_ptr3->data_ptr);
            list_mem();
            rt_free(msg_recv_ptr3);
            list_mem();
            rt_thread_mdelay(500);
            msg_send_ptr3 = (struct mb_msg *)rt_malloc(sizeof(struct mb_msg));
            msg_send_ptr3->data_size = sizeof(sbuf);
            msg_send_ptr3->data_ptr = (rt_uint8_t *)rt_malloc(msg_send_ptr3->data_size);
            rt_memcpy(msg_send_ptr3->data_ptr, sbuf, sizeof(sbuf));
            rt_kprintf("thread 3:[send=%s]\n", msg_send_ptr3->data_ptr);
            rt_mb_send(&t1_mb, (rt_uint32_t)msg_send_ptr3);
        }
    }
}

/* 初始化并启动邮箱的测试线程 */
static void task_init(void)
{
    struct mb_msg *msg_send_ptr;
    char sbuf[6] = {'T', 'a', 's', 'k', '1', '.'};
    rt_kprintf("%s: init start!\n", __func__);
    list_mem();
    rt_kprintf("%s: init end!\n", __func__);

    tid1 = rt_thread_create("task1",
                            thread1_entry,
                            RT_NULL,
                            THREAD1_STACK_SIZE,
                            THREAD1_PRIORITY,
                            THREAD1_TIMESLICE);

    
    tid2 = rt_thread_create("task2",
                            thread2_entry,
                            RT_NULL,
                            THREAD2_STACK_SIZE,
                            THREAD2_PRIORITY,
                            THREAD2_TIMESLICE);

    
    tid3 = rt_thread_create("task3",
                            thread3_entry,
                            RT_NULL,
                            THREAD3_STACK_SIZE,
                            THREAD3_PRIORITY,
                            THREAD3_TIMESLICE);

    if (tid1 != RT_NULL)
        rt_thread_startup(tid1);

    if (tid2 != RT_NULL)
        rt_thread_startup(tid2);

    if (tid3 != RT_NULL)
        rt_thread_startup(tid3);

    rt_thread_mdelay(500);
    msg_send_ptr = (struct mb_msg *)rt_malloc(sizeof(struct mb_msg));
    rt_kprintf("%s: rt_malloc 01\n", __func__);
    list_mem();
    rt_kprintf("%s: rt_malloc 01 end\n", __func__);
    msg_send_ptr->data_size = sizeof(sbuf);
    msg_send_ptr->data_ptr = (rt_uint8_t *)rt_malloc(msg_send_ptr->data_size);
    rt_kprintf("%s: rt_malloc 02", __func__);
    list_mem();
    rt_kprintf("%s: rt_malloc 02 end\n", __func__);
    rt_memcpy(msg_send_ptr->data_ptr, sbuf, sizeof(sbuf));
    
    rt_mb_send(&t1_mb, (rt_uint32_t)msg_send_ptr);
    rt_kprintf("task_init ok.\n");
}

/* MSH命令:启动邮箱测试例程 */
int mb_test(void)
{
    mb_init();
    task_init();
    return 1;
}

MSH_CMD_EXPORT(mb_test, mb test);

测试方法

  • 串口输入:mb_test 测试命令,启动邮箱的测试
msh >mb_test
mb_init ok.
task_init: init start!
total memory: 89568
used memory : 10656
maximum allocated memory: 10656
task_init: init end!
task_init: rt_malloc 01
total memory: 89568
used memory : 14208
maximum allocated memory: 14208
task_init: rt_malloc 01 end
task_init: rt_malloc 02total memory: 89568
used memory : 14232
maximum allocated memory: 14232
task_init: rt_malloc 02 end
task_init ok.
msh >thread 1:[recv=Task1.###### ], print 1.
total memory: 89568
used memory : 14208
maximum allocated memory: 14232
total memory: 89568
used memory : 14184
maximum allocated memory: 14232
thread 1:[send=Task1.###### ]
thread 2:[recv=Task1.###### ], print 2.
total memory: 89568
used memory : 14208
maximum allocated memory: 14232
total memory: 89568
used memory : 14184
maximum allocated memory: 14232
thread 2:[send=Task2.###### ]
thread 3:[recv=Task2.###### ], print 3.
total memory: 89568
used memory : 14208
maximum allocated memory: 14232
total memory: 89568
used memory : 14184
maximum allocated memory: 14232
thread 3:[send=Task3.###### ]
thread 1:[recv=Task3.###### ], print 1.
total memory: 89568
used memory : 14208
maximum allocated memory: 14232
total memory: 89568
used memory : 14184
maximum allocated memory: 14232
thread 1:[send=Task1.###### ]
thread 2:[recv=Task1.###### ], print 2.
total memory: 89568
used memory : 14208
maximum allocated memory: 14232
total memory: 89568
used memory : 14184
  • 说明邮箱已经正常的工作了。

总结

  • 邮箱于消息队列,都可以用于线程间的通信,相比消息队列,邮箱本身更精简。
  • 邮箱的接收,类似于消息队列,要放在线程,没收到回suspend线程,因此可以使用:RT_WAITING_FOREVER
  • 熟练使用邮箱等线程间的通信,需要熟悉下内核代码的实现,做的心中有数。
0 条评论

发布
问题