RT-Thread RTOS  1.2.0
An open source embedded real-time operating system
Clock and Timer Management
+ Collaboration diagram for Clock and Timer Management:

Data Structures

struct  rt_timer
 

Macros

#define RT_TIMER_FLAG_DEACTIVATED   0x0
 
#define RT_TIMER_FLAG_ACTIVATED   0x1
 
#define RT_TIMER_FLAG_ONE_SHOT   0x0
 
#define RT_TIMER_FLAG_PERIODIC   0x2
 
#define RT_TIMER_FLAG_HARD_TIMER   0x0
 
#define RT_TIMER_FLAG_SOFT_TIMER   0x4
 
#define RT_TIMER_FLAG_THREAD_TIMER    (0x8 | RT_TIMER_FLAG_HARD_TIMER)
 
#define RT_TIMER_CTRL_SET_TIME   0x0
 
#define RT_TIMER_CTRL_GET_TIME   0x1
 
#define RT_TIMER_CTRL_SET_ONESHOT   0x2
 
#define RT_TIMER_CTRL_SET_PERIODIC   0x3
 
#define RT_TIMER_CTRL_GET_STATE   0x4
 
#define RT_TIMER_CTRL_GET_REMAIN_TIME   0x5
 
#define RT_TIMER_CTRL_GET_FUNC   0x6
 
#define RT_TIMER_CTRL_SET_FUNC   0x7
 
#define RT_TIMER_CTRL_GET_PARM   0x8
 
#define RT_TIMER_CTRL_SET_PARM   0x9
 
#define RT_TIMER_SKIP_LIST_MASK   0x3
 

Typedefs

typedef void(* rt_timer_func_t) (void *parameter)
 

Functions

rt_tick_t rt_tick_get (void)
 
void rt_tick_set (rt_tick_t tick)
 
void rt_tick_increase (void)
 
void rt_tick_increase_tick (rt_tick_t tick)
 
rt_tick_t rt_tick_from_millisecond (rt_int32_t ms)
 
rt_weak rt_tick_t rt_tick_get_millisecond (void)
 
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)
 
rt_err_t rt_timer_detach (rt_timer_t timer)
 
rt_timer_t rt_timer_create (const char *name, void(*timeout)(void *parameter), void *parameter, rt_tick_t time, rt_uint8_t flag)
 
rt_err_t rt_timer_delete (rt_timer_t timer)
 
rt_err_t rt_timer_start (rt_timer_t timer)
 
rt_err_t rt_timer_stop (rt_timer_t timer)
 
rt_err_t rt_timer_control (rt_timer_t timer, int cmd, void *arg)
 
void rt_timer_check (void)
 
rt_tick_t rt_timer_next_timeout_tick (void)
 
void rt_system_timer_init (void)
 
void rt_system_timer_thread_init (void)
 

Detailed Description

clock and system timer management

RT-Thread uses clock tick to implement shared time-slice scheduling.

The timing sensitivity of thread is implemented by timers. The timer can be set as one-shot or periodic timeout.

Macro Definition Documentation

◆ RT_TIMER_FLAG_DEACTIVATED

#define RT_TIMER_FLAG_DEACTIVATED   0x0

clock & timer macros timer is deactive

◆ RT_TIMER_FLAG_ACTIVATED

#define RT_TIMER_FLAG_ACTIVATED   0x1

timer is active

◆ RT_TIMER_FLAG_ONE_SHOT

#define RT_TIMER_FLAG_ONE_SHOT   0x0

one shot timer

◆ RT_TIMER_FLAG_PERIODIC

#define RT_TIMER_FLAG_PERIODIC   0x2

periodic timer

◆ RT_TIMER_FLAG_HARD_TIMER

#define RT_TIMER_FLAG_HARD_TIMER   0x0

hard timer,the timer's callback function will be called in tick isr.

◆ RT_TIMER_FLAG_SOFT_TIMER

#define RT_TIMER_FLAG_SOFT_TIMER   0x4

soft timer,the timer's callback function will be called in timer thread.

◆ RT_TIMER_FLAG_THREAD_TIMER

#define RT_TIMER_FLAG_THREAD_TIMER    (0x8 | RT_TIMER_FLAG_HARD_TIMER)

thread timer that cooperates with scheduler directly

◆ RT_TIMER_CTRL_SET_TIME

#define RT_TIMER_CTRL_SET_TIME   0x0

set timer control command

◆ RT_TIMER_CTRL_GET_TIME

#define RT_TIMER_CTRL_GET_TIME   0x1

get timer control command

◆ RT_TIMER_CTRL_SET_ONESHOT

#define RT_TIMER_CTRL_SET_ONESHOT   0x2

change timer to one shot

◆ RT_TIMER_CTRL_SET_PERIODIC

#define RT_TIMER_CTRL_SET_PERIODIC   0x3

change timer to periodic

◆ RT_TIMER_CTRL_GET_STATE

#define RT_TIMER_CTRL_GET_STATE   0x4

get timer run state active or deactive

◆ RT_TIMER_CTRL_GET_REMAIN_TIME

#define RT_TIMER_CTRL_GET_REMAIN_TIME   0x5

get the remaining hang time

◆ RT_TIMER_CTRL_GET_FUNC

#define RT_TIMER_CTRL_GET_FUNC   0x6

get timer timeout func

◆ RT_TIMER_CTRL_SET_FUNC

#define RT_TIMER_CTRL_SET_FUNC   0x7

set timer timeout func

◆ RT_TIMER_CTRL_GET_PARM

#define RT_TIMER_CTRL_GET_PARM   0x8

get timer parameter

◆ RT_TIMER_CTRL_SET_PARM

#define RT_TIMER_CTRL_SET_PARM   0x9

get timer parameter

◆ RT_TIMER_SKIP_LIST_MASK

#define RT_TIMER_SKIP_LIST_MASK   0x3

Timer skips the list mask

Typedef Documentation

◆ rt_timer_func_t

typedef void(* rt_timer_func_t) (void *parameter)

timeout handler of rt_timer

Function Documentation

◆ rt_tick_get()

rt_tick_t rt_tick_get ( void  )

This function will return current tick from operating system startup.

Returns
Return current tick.

◆ rt_tick_set()

void rt_tick_set ( rt_tick_t  tick)

This function will set current tick.

Parameters
tickis the value that you will set.

◆ rt_tick_increase()

void rt_tick_increase ( void  )

This function will notify kernel there is one tick passed. Normally, this function is invoked by clock ISR.

◆ rt_tick_increase_tick()

void rt_tick_increase_tick ( rt_tick_t  tick)

This function will notify kernel there is n tick passed. Normally, this function is invoked by clock ISR.

◆ rt_tick_from_millisecond()

rt_tick_t rt_tick_from_millisecond ( rt_int32_t  ms)

This function will calculate the tick from millisecond.

Parameters
msis the specified millisecond.
  • Negative Number wait forever
  • Zero not wait
  • Max 0x7fffffff
Returns
Return the calculated tick.

◆ rt_tick_get_millisecond()

rt_tick_t rt_tick_get_millisecond ( void  )

This function will return the passed millisecond from boot.

Note
if the value of RT_TICK_PER_SECOND is lower than 1000 or is not an integral multiple of 1000, this function will not provide the correct 1ms-based tick.
Returns
Return passed millisecond from boot.

◆ rt_timer_init()

void rt_timer_init ( rt_timer_t  timer,
const char *  name,
void(*)(void *parameter)  timeout,
void *  parameter,
rt_tick_t  time,
rt_uint8_t  flag 
)

This function will initialize a timer normally this function is used to initialize a static timer object.

Parameters
timeris the point of timer
nameis a pointer to the name of the timer
timeoutis the callback of timer
parameteris the param of the callback
timeis timeout ticks of timer
        NOTE: The max timeout tick should be no more than (RT_TICK_MAX/2 - 1).
flagis the flag of timer

◆ rt_timer_detach()

rt_err_t rt_timer_detach ( rt_timer_t  timer)

This function will detach a timer from timer management.

Parameters
timeris the timer to be detached
Returns
the status of detach

◆ rt_timer_create()

rt_timer_t rt_timer_create ( const char *  name,
void(*)(void *parameter)  timeout,
void *  parameter,
rt_tick_t  time,
rt_uint8_t  flag 
)

This function will create a timer.

Parameters
nameis the name of timer
timeoutis the timeout function
parameteris the parameter of timeout function
timeis timeout ticks of the timer
   NOTE: The max timeout tick should be no more than (RT_TICK_MAX/2 - 1).
flagis the flag of timer. Timer will invoke the timeout function according to the selected values of flag, if one or more of the following flags is set.
     RT_TIMER_FLAG_ONE_SHOT          One shot timing
     RT_TIMER_FLAG_PERIODIC          Periodic timing

     RT_TIMER_FLAG_HARD_TIMER        Hardware timer
     RT_TIMER_FLAG_SOFT_TIMER        Software timer
     RT_TIMER_FLAG_THREAD_TIMER      Thread timer

   NOTE:
   You can use multiple values with "|" logical operator.  By default, system will use the RT_TIME_FLAG_HARD_TIMER.
Returns
the created timer object

◆ rt_timer_delete()

rt_err_t rt_timer_delete ( rt_timer_t  timer)

This function will delete a timer and release timer memory.

Parameters
timerthe timer to be deleted
Returns
the operation status, RT_EOK on OK; -RT_ERROR on error

◆ rt_timer_start()

rt_err_t rt_timer_start ( rt_timer_t  timer)

This function will start the timer.

Parameters
timerthe timer to be started
Returns
the operation status, RT_EOK on OK, -RT_ERROR on error

◆ rt_timer_stop()

rt_err_t rt_timer_stop ( rt_timer_t  timer)

This function will stop the timer.

Parameters
timerthe timer to be stopped
Returns
the operation status, RT_EOK on OK, -RT_ERROR on error

◆ rt_timer_control()

rt_err_t rt_timer_control ( rt_timer_t  timer,
int  cmd,
void *  arg 
)

This function will get or set some options of the timer.

Parameters
timerthe timer to be get or set
cmdthe control command
argthe argument
Returns
the statu of control

◆ rt_timer_check()

void rt_timer_check ( void  )

This function will check timer list, if a timeout event happens, the corresponding timeout function will be invoked.

Note
This function shall be invoked in operating system timer interrupt.

◆ rt_timer_next_timeout_tick()

rt_tick_t rt_timer_next_timeout_tick ( void  )

This function will return the next timeout tick in the system.

Returns
the next timeout tick in the system

◆ rt_system_timer_init()

void rt_system_timer_init ( void  )

This function will initialize system timer.

◆ rt_system_timer_thread_init()

void rt_system_timer_thread_init ( void  )

This function will initialize system timer thread.