timer_timeout.c 3.3 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 *
 */

B
Bernard Xiong 已提交
10
/*
mysterywolf's avatar
mysterywolf 已提交
11
 * 程序清单:消息队列例程
B
Bernard Xiong 已提交
12
 *
mysterywolf's avatar
mysterywolf 已提交
13 14
 * 这个程序会创建3个动态线程,一个线程会从消息队列中收取消息;一个线程会定时给消
 * 息队列发送消息;一个线程会定时给消息队列发送紧急消息。
B
Bernard Xiong 已提交
15 16 17 18
 */
#include <rtthread.h>
#include "tc_comm.h"

mysterywolf's avatar
mysterywolf 已提交
19
/* 指向线程控制块的指针 */
B
Bernard Xiong 已提交
20 21
static rt_thread_t tid = RT_NULL;

mysterywolf's avatar
mysterywolf 已提交
22
/* 消息队列控制块 */
B
Bernard Xiong 已提交
23
static struct rt_messagequeue mq;
mysterywolf's avatar
mysterywolf 已提交
24
/* 消息队列中用到的放置消息的内存池 */
B
Bernard Xiong 已提交
25 26
static char msg_pool[2048];

mysterywolf's avatar
mysterywolf 已提交
27
/* 定时器的控制块 */
B
Bernard Xiong 已提交
28 29 30 31
static struct rt_timer timer;
static rt_uint16_t no = 0;
static void timer_timeout(void* parameter)
{
G
Grissiom 已提交
32 33
    char buf[32];
    rt_uint32_t length;
B
Bernard Xiong 已提交
34

G
Grissiom 已提交
35 36
    length = rt_snprintf(buf, sizeof(buf), "message %d", no++);
    rt_mq_send(&mq, &buf[0], length);
B
Bernard Xiong 已提交
37 38
}

mysterywolf's avatar
mysterywolf 已提交
39
/* 线程入口函数 */
B
Bernard Xiong 已提交
40 41
static void thread_entry(void* parameter)
{
G
Grissiom 已提交
42 43 44
    char buf[64];
    rt_err_t result;

mysterywolf's avatar
mysterywolf 已提交
45 46 47 48 49 50
    /* 初始化定时器 */
    rt_timer_init(&timer, "timer",  /* 定时器名字是 timer1 */
        timer_timeout, /* 超时时回调的处理函数 */
        RT_NULL, /* 超时函数的入口参数 */
        1, /* 定时长度,以OS Tick为单位,即1个OS Tick */
        RT_TIMER_FLAG_PERIODIC); /* 周期性定时器 */
G
Grissiom 已提交
51 52 53 54 55

    while (1)
    {
        rt_memset(&buf[0], 0, sizeof(buf));

mysterywolf's avatar
mysterywolf 已提交
56
        /* 从消息队列中接收消息 */
G
Grissiom 已提交
57 58 59 60 61 62 63 64 65 66
        result = rt_mq_recv(&mq, &buf[0], sizeof(buf), 1);
        if (result == RT_EOK)
        {
            rt_kprintf("recv msg: %s\n", buf);
        }
        else if (result == -RT_ETIMEOUT)
        {
            rt_kprintf("recv msg timeout\n");
        }
    }
B
Bernard Xiong 已提交
67 68 69 70
}

int timer_timeout_init()
{
mysterywolf's avatar
mysterywolf 已提交
71 72 73 74 75
    /* 初始化消息队列 */
    rt_mq_init(&mq, "mqt",
        &msg_pool[0], /* 内存池指向msg_pool */
        128 - sizeof(void*), /* 每个消息的大小是 128 - void* */
        sizeof(msg_pool), /* 内存池的大小是msg_pool的大小 */
76
        RT_IPC_FLAG_PRIO); /* 如果有多个线程等待,按照优先级由高到低的方法分配消息 */
mysterywolf's avatar
mysterywolf 已提交
77 78

    /* 创建线程 */
G
Grissiom 已提交
79
    tid = rt_thread_create("t",
mysterywolf's avatar
mysterywolf 已提交
80
        thread_entry, RT_NULL, /* 线程入口是thread_entry, 入口参数是RT_NULL */
G
Grissiom 已提交
81 82 83 84 85 86 87
        THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
    if (tid != RT_NULL)
        rt_thread_startup(tid);
    else
        tc_stat(TC_STAT_END | TC_STAT_FAILED);

    return 0;
B
Bernard Xiong 已提交
88 89 90 91 92
}

#ifdef RT_USING_TC
static void _tc_cleanup()
{
mysterywolf's avatar
mysterywolf 已提交
93
    /* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */
G
Grissiom 已提交
94
    rt_enter_critical();
B
Bernard Xiong 已提交
95

mysterywolf's avatar
mysterywolf 已提交
96
    /* 删除线程 */
G
Grissiom 已提交
97 98
    if (tid != RT_NULL && tid->stat != RT_THREAD_CLOSE)
        rt_thread_delete(tid);
B
Bernard Xiong 已提交
99

mysterywolf's avatar
mysterywolf 已提交
100
    /* 执行消息队列对象脱离 */
G
Grissiom 已提交
101
    rt_mq_detach(&mq);
mysterywolf's avatar
mysterywolf 已提交
102
    /* 执行定时器脱离 */
G
Grissiom 已提交
103
    rt_timer_detach(&timer);
B
Bernard Xiong 已提交
104

mysterywolf's avatar
mysterywolf 已提交
105
    /* 调度器解锁 */
G
Grissiom 已提交
106
    rt_exit_critical();
B
Bernard Xiong 已提交
107

mysterywolf's avatar
mysterywolf 已提交
108
    /* 设置TestCase状态 */
G
Grissiom 已提交
109
    tc_done(TC_STAT_PASSED);
B
Bernard Xiong 已提交
110 111 112 113
}

int _tc_timer_timeout()
{
mysterywolf's avatar
mysterywolf 已提交
114
    /* 设置TestCase清理回调函数 */
G
Grissiom 已提交
115 116
    tc_cleanup(_tc_cleanup);
    timer_timeout_init();
B
Bernard Xiong 已提交
117

mysterywolf's avatar
mysterywolf 已提交
118
    /* 返回TestCase运行的最长时间 */
G
Grissiom 已提交
119
    return 100;
B
Bernard Xiong 已提交
120
}
mysterywolf's avatar
mysterywolf 已提交
121
/* 输出函数命令到finsh shell中 */
B
Bernard Xiong 已提交
122 123
FINSH_FUNCTION_EXPORT(_tc_timer_timeout, a thread timer testcase);
#else
mysterywolf's avatar
mysterywolf 已提交
124
/* 用户应用入口 */
B
Bernard Xiong 已提交
125 126
int rt_application_init()
{
G
Grissiom 已提交
127
    timer_timeout_init();
B
Bernard Xiong 已提交
128

G
Grissiom 已提交
129
    return 0;
B
Bernard Xiong 已提交
130 131
}
#endif