[example] 格式化整理
This commit is contained in:
parent
ce86058201
commit
08af426f01
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2006-2020, RT-Thread Development Team
|
* Copyright (c) 2006-2021, RT-Thread Development Team
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2006-2020, RT-Thread Development Team
|
* Copyright (c) 2006-2021, RT-Thread Development Team
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2006-2020, RT-Thread Development Team
|
* Copyright (c) 2006-2021, RT-Thread Development Team
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2006-2020, RT-Thread Development Team
|
* Copyright (c) 2006-2021, RT-Thread Development Team
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2006-2020, RT-Thread Development Team
|
* Copyright (c) 2006-2021, RT-Thread Development Team
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,21 +1,21 @@
|
||||||
/*
|
/*
|
||||||
* 程序清单:消息队列例程
|
* 程序清单:消息队列例程
|
||||||
*
|
*
|
||||||
* 这个程序会创建3个动态线程,一个线程会从消息队列中收取消息;一个线程会定时给消
|
* 这个程序会创建3个动态线程,一个线程会从消息队列中收取消息;一个线程会定时给消
|
||||||
* 息队列发送消息;一个线程会定时给消息队列发送紧急消息。
|
* 息队列发送消息;一个线程会定时给消息队列发送紧急消息。
|
||||||
*/
|
*/
|
||||||
#include <rtthread.h>
|
#include <rtthread.h>
|
||||||
#include "tc_comm.h"
|
#include "tc_comm.h"
|
||||||
|
|
||||||
/* 指向线程控制块的指针 */
|
/* 指向线程控制块的指针 */
|
||||||
static rt_thread_t tid = RT_NULL;
|
static rt_thread_t tid = RT_NULL;
|
||||||
|
|
||||||
/* 消息队列控制块 */
|
/* 消息队列控制块 */
|
||||||
static struct rt_messagequeue mq;
|
static struct rt_messagequeue mq;
|
||||||
/* 消息队列中用到的放置消息的内存池 */
|
/* 消息队列中用到的放置消息的内存池 */
|
||||||
static char msg_pool[2048];
|
static char msg_pool[2048];
|
||||||
|
|
||||||
/* 定时器的控制块 */
|
/* 定时器的控制块 */
|
||||||
static struct rt_timer timer;
|
static struct rt_timer timer;
|
||||||
static rt_uint16_t no = 0;
|
static rt_uint16_t no = 0;
|
||||||
static void timer_timeout(void* parameter)
|
static void timer_timeout(void* parameter)
|
||||||
|
@ -27,24 +27,24 @@ static void timer_timeout(void* parameter)
|
||||||
rt_mq_send(&mq, &buf[0], length);
|
rt_mq_send(&mq, &buf[0], length);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 线程入口函数 */
|
/* 线程入口函数 */
|
||||||
static void thread_entry(void* parameter)
|
static void thread_entry(void* parameter)
|
||||||
{
|
{
|
||||||
char buf[64];
|
char buf[64];
|
||||||
rt_err_t result;
|
rt_err_t result;
|
||||||
|
|
||||||
/* 初始化定时器 */
|
/* 初始化定时器 */
|
||||||
rt_timer_init(&timer, "timer", /* 定时器名字是 timer1 */
|
rt_timer_init(&timer, "timer", /* 定时器名字是 timer1 */
|
||||||
timer_timeout, /* 超时时回调的处理函数 */
|
timer_timeout, /* 超时时回调的处理函数 */
|
||||||
RT_NULL, /* 超时函数的入口参数 */
|
RT_NULL, /* 超时函数的入口参数 */
|
||||||
1, /* 定时长度,以OS Tick为单位,即1个OS Tick */
|
1, /* 定时长度,以OS Tick为单位,即1个OS Tick */
|
||||||
RT_TIMER_FLAG_PERIODIC); /* 周期性定时器 */
|
RT_TIMER_FLAG_PERIODIC); /* 周期性定时器 */
|
||||||
|
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
rt_memset(&buf[0], 0, sizeof(buf));
|
rt_memset(&buf[0], 0, sizeof(buf));
|
||||||
|
|
||||||
/* 从消息队列中接收消息 */
|
/* 从消息队列中接收消息 */
|
||||||
result = rt_mq_recv(&mq, &buf[0], sizeof(buf), 1);
|
result = rt_mq_recv(&mq, &buf[0], sizeof(buf), 1);
|
||||||
if (result == RT_EOK)
|
if (result == RT_EOK)
|
||||||
{
|
{
|
||||||
|
@ -59,16 +59,16 @@ static void thread_entry(void* parameter)
|
||||||
|
|
||||||
int timer_timeout_init()
|
int timer_timeout_init()
|
||||||
{
|
{
|
||||||
/* 初始化消息队列 */
|
/* 初始化消息队列 */
|
||||||
rt_mq_init(&mq, "mqt",
|
rt_mq_init(&mq, "mqt",
|
||||||
&msg_pool[0], /* 内存池指向msg_pool */
|
&msg_pool[0], /* 内存池指向msg_pool */
|
||||||
128 - sizeof(void*), /* 每个消息的大小是 128 - void* */
|
128 - sizeof(void*), /* 每个消息的大小是 128 - void* */
|
||||||
sizeof(msg_pool), /* 内存池的大小是msg_pool的大小 */
|
sizeof(msg_pool), /* 内存池的大小是msg_pool的大小 */
|
||||||
RT_IPC_FLAG_FIFO); /* 如果有多个线程等待,按照先来先得到的方法分配消息 */
|
RT_IPC_FLAG_FIFO); /* 如果有多个线程等待,按照先来先得到的方法分配消息 */
|
||||||
|
|
||||||
/* 创建线程 */
|
/* 创建线程 */
|
||||||
tid = rt_thread_create("t",
|
tid = rt_thread_create("t",
|
||||||
thread_entry, RT_NULL, /* 线程入口是thread_entry, 入口参数是RT_NULL */
|
thread_entry, RT_NULL, /* 线程入口是thread_entry, 入口参数是RT_NULL */
|
||||||
THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
|
THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
|
||||||
if (tid != RT_NULL)
|
if (tid != RT_NULL)
|
||||||
rt_thread_startup(tid);
|
rt_thread_startup(tid);
|
||||||
|
@ -81,38 +81,38 @@ int timer_timeout_init()
|
||||||
#ifdef RT_USING_TC
|
#ifdef RT_USING_TC
|
||||||
static void _tc_cleanup()
|
static void _tc_cleanup()
|
||||||
{
|
{
|
||||||
/* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */
|
/* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */
|
||||||
rt_enter_critical();
|
rt_enter_critical();
|
||||||
|
|
||||||
/* 删除线程 */
|
/* 删除线程 */
|
||||||
if (tid != RT_NULL && tid->stat != RT_THREAD_CLOSE)
|
if (tid != RT_NULL && tid->stat != RT_THREAD_CLOSE)
|
||||||
rt_thread_delete(tid);
|
rt_thread_delete(tid);
|
||||||
|
|
||||||
/* 执行消息队列对象脱离 */
|
/* 执行消息队列对象脱离 */
|
||||||
rt_mq_detach(&mq);
|
rt_mq_detach(&mq);
|
||||||
/* 执行定时器脱离 */
|
/* 执行定时器脱离 */
|
||||||
rt_timer_detach(&timer);
|
rt_timer_detach(&timer);
|
||||||
|
|
||||||
/* 调度器解锁 */
|
/* 调度器解锁 */
|
||||||
rt_exit_critical();
|
rt_exit_critical();
|
||||||
|
|
||||||
/* 设置TestCase状态 */
|
/* 设置TestCase状态 */
|
||||||
tc_done(TC_STAT_PASSED);
|
tc_done(TC_STAT_PASSED);
|
||||||
}
|
}
|
||||||
|
|
||||||
int _tc_timer_timeout()
|
int _tc_timer_timeout()
|
||||||
{
|
{
|
||||||
/* 设置TestCase清理回调函数 */
|
/* 设置TestCase清理回调函数 */
|
||||||
tc_cleanup(_tc_cleanup);
|
tc_cleanup(_tc_cleanup);
|
||||||
timer_timeout_init();
|
timer_timeout_init();
|
||||||
|
|
||||||
/* 返回TestCase运行的最长时间 */
|
/* 返回TestCase运行的最长时间 */
|
||||||
return 100;
|
return 100;
|
||||||
}
|
}
|
||||||
/* 输出函数命令到finsh shell中 */
|
/* 输出函数命令到finsh shell中 */
|
||||||
FINSH_FUNCTION_EXPORT(_tc_timer_timeout, a thread timer testcase);
|
FINSH_FUNCTION_EXPORT(_tc_timer_timeout, a thread timer testcase);
|
||||||
#else
|
#else
|
||||||
/* 用户应用入口 */
|
/* 用户应用入口 */
|
||||||
int rt_application_init()
|
int rt_application_init()
|
||||||
{
|
{
|
||||||
timer_timeout_init();
|
timer_timeout_init();
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2006-2018, RT-Thread Development Team
|
* Copyright (c) 2006-2021, RT-Thread Development Team
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2006-2018, RT-Thread Development Team
|
* Copyright (c) 2006-2021, RT-Thread Development Team
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
*
|
*
|
||||||
|
|
|
@ -35,8 +35,8 @@ int hwtimer(void)
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 时间测量 */
|
/* 时间测量 */
|
||||||
/* 计数时钟设置(默认1Mhz或支持的最小计数频率) */
|
/* 计数时钟设置(默认1Mhz或支持的最小计数频率) */
|
||||||
err = rt_device_control(dev, HWTIMER_CTRL_FREQ_SET, &freq);
|
err = rt_device_control(dev, HWTIMER_CTRL_FREQ_SET, &freq);
|
||||||
if (err != RT_EOK)
|
if (err != RT_EOK)
|
||||||
{
|
{
|
||||||
|
@ -44,13 +44,13 @@ int hwtimer(void)
|
||||||
goto EXIT;
|
goto EXIT;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 周期模式 */
|
/* 周期模式 */
|
||||||
mode = HWTIMER_MODE_PERIOD;
|
mode = HWTIMER_MODE_PERIOD;
|
||||||
err = rt_device_control(dev, HWTIMER_CTRL_MODE_SET, &mode);
|
err = rt_device_control(dev, HWTIMER_CTRL_MODE_SET, &mode);
|
||||||
|
|
||||||
tick = rt_tick_get();
|
tick = rt_tick_get();
|
||||||
rt_kprintf("Start Timer> Tick: %d\n", tick);
|
rt_kprintf("Start Timer> Tick: %d\n", tick);
|
||||||
/* 设置定时器超时值并启动定时器 */
|
/* 设置定时器超时值并启动定时器 */
|
||||||
val.sec = t;
|
val.sec = t;
|
||||||
val.usec = 0;
|
val.usec = 0;
|
||||||
rt_kprintf("SetTime: Sec %d, Usec %d\n", val.sec, val.usec);
|
rt_kprintf("SetTime: Sec %d, Usec %d\n", val.sec, val.usec);
|
||||||
|
@ -62,22 +62,22 @@ int hwtimer(void)
|
||||||
rt_kprintf("Sleep %d sec\n", t);
|
rt_kprintf("Sleep %d sec\n", t);
|
||||||
rt_thread_delay(t*RT_TICK_PER_SECOND);
|
rt_thread_delay(t*RT_TICK_PER_SECOND);
|
||||||
|
|
||||||
/* 停止定时器 */
|
/* 停止定时器 */
|
||||||
err = rt_device_control(dev, HWTIMER_CTRL_STOP, RT_NULL);
|
err = rt_device_control(dev, HWTIMER_CTRL_STOP, RT_NULL);
|
||||||
rt_kprintf("Timer Stoped\n");
|
rt_kprintf("Timer Stoped\n");
|
||||||
/* 读取计数 */
|
/* 读取计数 */
|
||||||
rt_device_read(dev, 0, &val, sizeof(val));
|
rt_device_read(dev, 0, &val, sizeof(val));
|
||||||
rt_kprintf("Read: Sec = %d, Usec = %d\n", val.sec, val.usec);
|
rt_kprintf("Read: Sec = %d, Usec = %d\n", val.sec, val.usec);
|
||||||
|
|
||||||
/* 定时执行回调函数 -- 单次模式 */
|
/* 定时执行回调函数 -- 单次模式 */
|
||||||
/* 设置超时回调函数 */
|
/* 设置超时回调函数 */
|
||||||
rt_device_set_rx_indicate(dev, timer_timeout_cb);
|
rt_device_set_rx_indicate(dev, timer_timeout_cb);
|
||||||
|
|
||||||
/* 单次模式 */
|
/* 单次模式 */
|
||||||
mode = HWTIMER_MODE_PERIOD;
|
mode = HWTIMER_MODE_PERIOD;
|
||||||
err = rt_device_control(dev, HWTIMER_CTRL_MODE_SET, &mode);
|
err = rt_device_control(dev, HWTIMER_CTRL_MODE_SET, &mode);
|
||||||
|
|
||||||
/* 设置定时器超时值并启动定时器 */
|
/* 设置定时器超时值并启动定时器 */
|
||||||
val.sec = t;
|
val.sec = t;
|
||||||
val.usec = 0;
|
val.usec = 0;
|
||||||
rt_kprintf("SetTime: Sec %d, Usec %d\n", val.sec, val.usec);
|
rt_kprintf("SetTime: Sec %d, Usec %d\n", val.sec, val.usec);
|
||||||
|
@ -87,7 +87,7 @@ int hwtimer(void)
|
||||||
goto EXIT;
|
goto EXIT;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 等待回调函数执行 */
|
/* 等待回调函数执行 */
|
||||||
rt_thread_delay((t + 1)*RT_TICK_PER_SECOND);
|
rt_thread_delay((t + 1)*RT_TICK_PER_SECOND);
|
||||||
|
|
||||||
EXIT:
|
EXIT:
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2006-2018, RT-Thread Development Team
|
* Copyright (c) 2006-2021, RT-Thread Development Team
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in New Issue