定时器机制
RT-Thread 向用户提供了一系列的API接口,在构建硬件定时器-系统滴答定时器的基础上。
RT-Thread 提供的定时器是系统能够提供不受数目限制的定时器服务。
RT-Thread 提供的定时器单位位:系统节拍(OS Tick),是一种基于OS Tick整数倍的能力,
如果设置OS Tick 为10ms ,那么上层软件定时器只能提供10ms,20ms,100ms,等时间单位的定时服务。
RT-Thread 定时器服务还提供了一个timerout回调函数,用户可以将需要定时运行的业务放在该回调函数中。
定时器模式
硬件模式HardTimer
这种模式的定时器超时函数在中断上下文环境执行,此模式在定时器初始化时候被指定,这种模式下的超时函数运行条件和中断服务例程相同,执行时间尽可能的短,执行期间不应该导致上下文被挂起,HardTimer模式事RT-Thread的默认模式。
软件模式 SoftTimer
软件定时模式的超时函数在系统的timer线程上下文中执行,
这种模式需要通过 RT_USING_TIMER_SOFT (rtconfig.h) 来开启软件定时模式。
当启用软件定时器模式后,我们可以在定时器初始化时指定定时器工作在软件定时模式。
定时器控制块
/**
* timer structure
*/
struct rt_timer
{
struct rt_object parent; /**< inherit from rt_object */
rt_list_t row[RT_TIMER_SKIP_LIST_LEVEL];
void (*timeout_func)(void *parameter); /**< timeout function */
void *parameter; /**< timeout function's parameter */
rt_tick_t init_tick; /**< timer timeout tick */
rt_tick_t timeout_tick; /**< timeout tick */
};
typedef struct rt_timer *rt_timer_t;
定时器对象定义
struct rt_messagequeue static_timer; /*静态定时器对象*/
rt_timer_t dynamic_timer; /*动态定时器对象*/
初始化与脱离-同信号量和互斥锁类似,主要针对静态定义
/**
* This function will initialize a timer, normally this function is used to
* initialize a static timer object.
*
* @param timer the static timer object
* @param name the name of timer
* @param timeout the timeout function
* @param parameter the parameter of timeout function
* @param time the tick of timer
* @param flag the flag of timer
*/
void rt_timer_init(rt_timer_t timer,
const char *name,
void (*timeout)(void *parameter),
void *parameter,
rt_tick_t time,
rt_uint8_t flag)
/**
* This function will detach a timer from timer management.
*
* @param timer the static timer object
*
* @return the operation status, RT_EOK on OK; RT_ERROR on error
*/
rt_err_t rt_timer_detach(rt_timer_t timer)
#define RT_TIMER_FLAG_ONE_SHOT 0x0 /**< one shot timer */
#define RT_TIMER_FLAG_PERIODIC 0x2 /**< periodic timer */
#define RT_TIMER_FLAG_HARD_TIMER 0x0 /**< hard timer,the timer's callback function will be called in tick isr. */
#define RT_TIMER_FLAG_SOFT_TIMER 0x4 /**< soft timer,the timer's callback function will be called in timer thread. */
特别说明组合方式:
RT_TIMER_FLAG_ONE_SHOT|RT_TIMER_FLAG_HARD_TIMER 在硬件定时模式下单次运行
RT_TIMER_FLAG_ONE_SHOT|RT_TIMER_FLAG_SOFT_TIMER 在软件定时模式下单次运行
RT_TIMER_FLAG_PERIODIC|RT_TIMER_FLAG_HARD_TIMER 在硬件定时模式下周期性运行
RT_TIMER_FLAG_PERIODIC|RT_TIMER_FLAG_SOFT_TIMER 在软件定时模式下周期性运行
定时器模式和定时器超时函数模式设定只能二选一。因为RT-Thread的默认模式时硬件模式,所以如果不指定定时模式则就时在硬件定时模式运行超时函数。
创建和删除-同信号量和互斥锁类似,针对动态定义
/**
* This function will create a timer
*
* @param name the name of timer
* @param timeout the timeout function
* @param parameter the parameter of timeout function
* @param time the tick of timer
* @param flag the flag of timer
*
* @return the created timer object
*/
rt_timer_t rt_timer_create(const char *name,
void (*timeout)(void *parameter),
void *parameter,
rt_tick_t time,
rt_uint8_t flag)
/**
* This function will delete a timer and release timer memory
*
* @param timer the timer to be deleted
*
* @return the operation status, RT_EOK on OK; RT_ERROR on error
*/
rt_err_t rt_timer_delete(rt_timer_t timer)
启动定时器
/**
* This function will start the timer
*
* @param timer the timer to be started
*
* @return the operation status, RT_EOK on OK, -RT_ERROR on error
*/
rt_err_t rt_timer_start(rt_timer_t timer)
停止定时器
/**
* This function will stop the timer
*
* @param timer the timer to be stopped
*
* @return the operation status, RT_EOK on OK, -RT_ERROR on error
*/
rt_err_t rt_timer_stop(rt_timer_t timer)
参考官网例程
/*
* 这个例程会创建两个动态定时器,一个是单次定时,一个是周期性定时
* 并让周期定时器运行一段时间后停止运行
*/
#include <rtthread.h>
/* 定时器的控制块 */
static rt_timer_t timer1;
static rt_timer_t timer2;
static int cnt = 0;
/* 定时器1超时函数 */
static void timeout1(void *parameter)
{
rt_kprintf("periodic timer is timeout %d\n", cnt);
/* 运行第10次,停止周期定时器 */
if (cnt++ >= 9)
{
rt_timer_stop(timer1);
rt_kprintf("periodic timer was stopped! \n");
}
}
/* 定时器2超时函数 */
static void timeout2(void *parameter)
{
rt_kprintf("one shot timer is timeout\n");
}
int timer_sample(void)
{
/* 创建定时器1 周期定时器 */
timer1 = rt_timer_create("timer1", timeout1,
RT_NULL, 10,
RT_TIMER_FLAG_PERIODIC);
/* 启动定时器1 */
if (timer1 != RT_NULL) rt_timer_start(timer1);
/* 创建定时器2 单次定时器 */
timer2 = rt_timer_create("timer2", timeout2,
RT_NULL, 30,
RT_TIMER_FLAG_ONE_SHOT);
/* 启动定时器2 */
if (timer2 != RT_NULL) rt_timer_start(timer2);
return 0;
}