timer.c 12.1 KB
Newer Older
1 2 3
/*
 * File      : timer.c
 * This file is part of RT-Thread RTOS
D
dzzxzz 已提交
4
 * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
5 6 7
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
8
 * http://www.rt-thread.org/license/LICENSE
9 10 11 12 13 14 15 16
 *
 * Change Logs:
 * Date           Author       Notes
 * 2006-03-12     Bernard      first version
 * 2006-04-29     Bernard      implement thread timer
 * 2006-06-04     Bernard      implement rt_timer_control
 * 2006-08-10     Bernard      fix the periodic timer bug
 * 2006-09-03     Bernard      implement rt_timer_detach
17
 * 2009-11-11     LiJin        add soft timer
18
 * 2010-05-12     Bernard      fix the timer check bug.
19
 * 2010-11-02     Charlie      re-implement tick overflow issue
20 21 22 23 24
 */

#include <rtthread.h>
#include <rthw.h>

25
/* hard timer list */
26
static rt_list_t rt_timer_list = RT_LIST_OBJECT_INIT(rt_timer_list);
27

28
#ifdef RT_USING_TIMER_SOFT
29
/* soft timer list */
30
static rt_list_t rt_soft_timer_list;
31 32 33
static struct rt_thread timer_thread;
ALIGN(RT_ALIGN_SIZE)
static rt_uint8_t timer_thread_stack[RT_TIMER_THREAD_STACK_SIZE];
34
#endif
35

36
#ifdef RT_USING_HOOK
D
dzzxzz 已提交
37 38 39
extern void (*rt_object_take_hook)(struct rt_object *object);
extern void (*rt_object_put_hook)(struct rt_object *object);
static void (*rt_timer_timeout_hook)(struct rt_timer *timer);
40 41 42 43

/**
 * @addtogroup Hook
 */
D
dzzxzz 已提交
44

45 46 47 48 49
/*@{*/

/**
 * This function will set a hook function, which will be invoked when timer
 * is timeout.
50
 *
51 52
 * @param hook the hook function
 */
D
dzzxzz 已提交
53
void rt_timer_timeout_sethook(void (*hook)(struct rt_timer *timer))
54 55 56 57 58 59 60
{
	rt_timer_timeout_hook = hook;
}

/*@}*/
#endif

61
static void _rt_timer_init(rt_timer_t timer,
D
dzzxzz 已提交
62
						   void (*timeout)(void *parameter), void *parameter,
63
						   rt_tick_t time, rt_uint8_t flag)
64 65
{
	/* set flag */
D
dzzxzz 已提交
66
	timer->parent.flag  = flag;
67 68 69 70 71

	/* set deactivated */
	timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;

	timer->timeout_func = timeout;
D
dzzxzz 已提交
72
	timer->parameter    = parameter;
73

D
dzzxzz 已提交
74 75
	timer->timeout_tick = 0;
	timer->init_tick    = time;
76

77
	/* initialize timer list */
78 79 80
	rt_list_init(&(timer->list));
}

81 82 83 84 85 86 87 88 89
static rt_tick_t rt_timer_list_next_timeout(rt_list_t* timer_list)
{
	struct rt_timer* timer;
	if (rt_list_isempty(timer_list)) return RT_TICK_MAX;
	
	timer = rt_list_entry(timer_list->next, struct rt_timer, list);
	return timer->timeout_tick;
}

90 91 92
/**
 * @addtogroup Clock
 */
D
dzzxzz 已提交
93

94 95 96
/*@{*/

/**
97
 * This function will initialize a timer, normally this function is used to initialize
98 99 100 101 102 103 104 105 106
 * 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
 */
107
void rt_timer_init(rt_timer_t timer,
D
dzzxzz 已提交
108 109
				   const char *name,
				   void (*timeout)(void *parameter), void *parameter,
110
				   rt_tick_t time, rt_uint8_t flag)
111 112 113 114
{
	/* timer check */
	RT_ASSERT(timer != RT_NULL);

115
	/* timer object initialization */
116 117 118 119 120
	rt_object_init((rt_object_t)timer, RT_Object_Class_Timer, name);

	_rt_timer_init(timer, timeout, parameter, time, flag);
}

121
/**
122 123 124
 * This function will detach a timer from timer management.
 *
 * @param timer the static timer object
125
 *
126 127 128 129 130
 * @return the operation status, RT_EOK on OK; RT_ERROR on error
 */
rt_err_t rt_timer_detach(rt_timer_t timer)
{
	register rt_base_t level;
131

132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
	/* timer check */
	RT_ASSERT(timer != RT_NULL);

	/* disable interrupt */
	level = rt_hw_interrupt_disable();

	/* remove it from timer list */
	rt_list_remove(&(timer->list));

	/* enable interrupt */
	rt_hw_interrupt_enable(level);

	rt_object_detach((rt_object_t)timer);

	return -RT_EOK;
}

#ifdef RT_USING_HEAP
/**
 * 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
 */
D
dzzxzz 已提交
161
rt_timer_t rt_timer_create(const char *name, void (*timeout)(void *parameter), void *parameter, rt_tick_t time, rt_uint8_t flag)
162
{
D
dzzxzz 已提交
163
	struct rt_timer *timer;
164 165

	/* allocate a object */
D
dzzxzz 已提交
166
	timer = (struct rt_timer *)rt_object_allocate(RT_Object_Class_Timer, name);
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
	if (timer == RT_NULL)
	{
		return RT_NULL;
	}

	_rt_timer_init(timer, timeout, parameter, time, flag);

	return timer;
}

/**
 * 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)
{
	register rt_base_t level;
187

188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
	/* timer check */
	RT_ASSERT(timer != RT_NULL);

	/* disable interrupt */
	level = rt_hw_interrupt_disable();

	/* remove it from timer list */
	rt_list_remove(&(timer->list));

	/* enable interrupt */
	rt_hw_interrupt_enable(level);

	rt_object_delete((rt_object_t)timer);

	return -RT_EOK;
}
#endif

/**
 * This function will start the timer
 *
 * @param timer the timer to be started
 *
B
bernard.xiong@gmail.com 已提交
211
 * @return the operation status, RT_EOK on OK, -RT_ERROR on error
212 213 214
 */
rt_err_t rt_timer_start(rt_timer_t timer)
{
D
dzzxzz 已提交
215
	struct rt_timer *t;
216
	register rt_base_t level;
217
	rt_list_t *n, *timer_list;
218 219 220

	/* timer check */
	RT_ASSERT(timer != RT_NULL);
D
dzzxzz 已提交
221 222
	if (timer->parent.flag & RT_TIMER_FLAG_ACTIVATED)
		return -RT_ERROR;
223

224
	RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(timer->parent)));
225

B
bernard.xiong@gmail.com 已提交
226 227
	/* get timeout tick, the max timeout tick shall not great than RT_TICK_MAX/2 */
	RT_ASSERT(timer->init_tick < RT_TICK_MAX/2);
228 229
	timer->timeout_tick = rt_tick_get() + timer->init_tick;

230 231 232
	/* disable interrupt */
	level = rt_hw_interrupt_disable();

233
#ifdef RT_USING_TIMER_SOFT
234
	if (timer->parent.flag & RT_TIMER_FLAG_SOFT_TIMER)
235
	{
236 237
		/* insert timer to soft timer list */
		timer_list = &rt_soft_timer_list;
238 239
	}
	else
240
#endif
241
	{
242
		/* insert timer to system timer list */
243 244
		timer_list = &rt_timer_list;
	}
245

246 247 248 249 250 251 252 253
	for (n = timer_list->next; n != timer_list; n = n->next)
	{
		t = rt_list_entry(n, struct rt_timer, list);
		
		/*
		 * It supposes that the new tick shall less than the half duration of tick max.
		 */
		if ((t->timeout_tick - timer->timeout_tick) < RT_TICK_MAX/2)
254 255
		{
			rt_list_insert_before(n, &(timer->list));
256
			break;
257
		}
258
	}
259 260 261 262 263
	/* no found suitable position in timer list */
	if (n == timer_list)
	{
		rt_list_insert_before(n, &(timer->list));
	}
264

265 266 267 268 269
	timer->parent.flag |= RT_TIMER_FLAG_ACTIVATED;

	/* enable interrupt */
	rt_hw_interrupt_enable(level);

270 271 272 273 274 275 276 277 278 279 280 281 282
#ifdef RT_USING_TIMER_SOFT
	if (timer->parent.flag & RT_TIMER_FLAG_SOFT_TIMER)
	{
		/* check whether timer thread is ready */
		if (timer_thread.stat != RT_THREAD_READY)
		{
			/* resume timer thread to check soft timer */
			rt_thread_resume(&timer_thread);
			rt_schedule();
		}
	}
#endif

283 284 285 286 287 288 289 290
	return -RT_EOK;
}

/**
 * This function will stop the timer
 *
 * @param timer the timer to be stopped
 *
B
bernard.xiong@gmail.com 已提交
291
 * @return the operation status, RT_EOK on OK, -RT_ERROR on error
292 293 294 295 296 297 298
 */
rt_err_t rt_timer_stop(rt_timer_t timer)
{
	register rt_base_t level;

	/* timer check */
	RT_ASSERT(timer != RT_NULL);
D
dzzxzz 已提交
299 300
	if (!(timer->parent.flag & RT_TIMER_FLAG_ACTIVATED))
		return -RT_ERROR;
301

302
	RT_OBJECT_HOOK_CALL(rt_object_put_hook, (&(timer->parent)));
303 304 305 306 307 308 309 310 311 312

	/* disable interrupt */
	level = rt_hw_interrupt_disable();

	/* remove it from timer list */
	rt_list_remove(&(timer->list));

	/* enable interrupt */
	rt_hw_interrupt_enable(level);

313 314 315
	/* change stat */
	timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;

316 317 318 319 320 321 322 323 324 325
	return RT_EOK;
}

/**
 * This function will get or set some options of the timer
 *
 * @param timer the timer to be get or set
 * @param cmd the control command
 * @param arg the argument
 *
B
bernard.xiong@gmail.com 已提交
326
 * @return RT_EOK
327
 */
D
dzzxzz 已提交
328
rt_err_t rt_timer_control(rt_timer_t timer, rt_uint8_t cmd, void *arg)
329 330 331 332 333 334 335
{
	/* timer check */
	RT_ASSERT(timer != RT_NULL);

	switch (cmd)
	{
	case RT_TIMER_CTRL_GET_TIME:
D
dzzxzz 已提交
336
		*(rt_tick_t *)arg = timer->init_tick;
337 338 339
		break;

	case RT_TIMER_CTRL_SET_TIME:
D
dzzxzz 已提交
340
		timer->init_tick = *(rt_tick_t *)arg;
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
		break;

	case RT_TIMER_CTRL_SET_ONESHOT:
		timer->parent.flag &= ~(1 << RT_TIMER_FLAG_PERIODIC);
		break;

	case RT_TIMER_CTRL_SET_PERIODIC:
		timer->parent.flag |= (1 << RT_TIMER_FLAG_PERIODIC);
		break;
	}

	return RT_EOK;
}

/**
356
 * This function will check timer list, if a timeout event happens, the
357 358
 * corresponding timeout function will be invoked.
 *
B
bernard.xiong@gmail.com 已提交
359
 * @note this function shall be invoked in operating system timer interrupt.
360
 */
361
void rt_timer_check(void)
362 363
{
	struct rt_timer *t;
364
	rt_tick_t current_tick;
365 366
	register rt_base_t level;

D
dzzxzz 已提交
367
	RT_DEBUG_LOG(RT_DEBUG_TIMER, ("timer check enter\n"));
368 369 370 371 372 373

	current_tick = rt_tick_get();

	/* disable interrupt */
	level = rt_hw_interrupt_disable();

374
	while (!rt_list_isempty(&rt_timer_list))
375
	{
376
		t = rt_list_entry(rt_timer_list.next, struct rt_timer, list);
D
dzzxzz 已提交
377

378 379 380 381
		/*
		 * It supposes that the new tick shall less than the half duration of tick max.
		 */
		if ((current_tick - t->timeout_tick) < RT_TICK_MAX/2)
382
		{
383
			RT_OBJECT_HOOK_CALL(rt_timer_timeout_hook, (t));
384 385 386 387 388 389 390

			/* remove timer from timer list firstly */
			rt_list_remove(&(t->list));

			/* call timeout function */
			t->timeout_func(t->parameter);

391
			/* re-get tick */
392 393
			current_tick = rt_tick_get();

D
dzzxzz 已提交
394
			RT_DEBUG_LOG(RT_DEBUG_TIMER, ("current tick: %d\n", current_tick));
395

396 397
			if ((t->parent.flag & RT_TIMER_FLAG_PERIODIC) &&
					(t->parent.flag & RT_TIMER_FLAG_ACTIVATED))
398 399
			{
				/* start it */
400
				t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
401 402
				rt_timer_start(t);
			}
403 404 405 406 407
			else
			{
				/* stop timer */
				t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
			}
408
		}
D
dzzxzz 已提交
409 410
		else
			break;
411 412 413 414 415
	}

	/* enable interrupt */
	rt_hw_interrupt_enable(level);

D
dzzxzz 已提交
416
	RT_DEBUG_LOG(RT_DEBUG_TIMER, ("timer check leave\n"));
417 418
}

419 420 421 422 423 424
/**
 * This function will return the next timeout tick in the system.
 *
 * @return the next timeout tick in the system
 */
rt_tick_t rt_timer_next_timeout_tick(void)
425
{
426
	return rt_timer_list_next_timeout(&rt_timer_list);
427 428
}

429
#ifdef RT_USING_TIMER_SOFT
430 431 432 433
/**
 * This function will check timer list, if a timeout event happens, the
 * corresponding timeout function will be invoked.
 */
D
dzzxzz 已提交
434
void rt_soft_timer_check(void)
435 436 437 438
{
	rt_tick_t current_tick;
	rt_list_t *n;
	struct rt_timer *t;
439

D
dzzxzz 已提交
440
	RT_DEBUG_LOG(RT_DEBUG_TIMER, ("software timer check enter\n"));
441

442 443
	current_tick = rt_tick_get();

D
dzzxzz 已提交
444
	for (n = rt_soft_timer_list.next; n != &(rt_soft_timer_list);)
445 446
	{
		t = rt_list_entry(n, struct rt_timer, list);
D
dzzxzz 已提交
447

448 449 450 451
		/*
		 * It supposes that the new tick shall less than the half duration of tick max.
		 */
		if ((current_tick - t->timeout_tick) < RT_TICK_MAX/2)
452
		{
453 454
			RT_OBJECT_HOOK_CALL(rt_timer_timeout_hook, (t));

455 456 457 458 459 460 461 462 463
			/* move node to the next */
			n = n->next;

			/* remove timer from timer list firstly */
			rt_list_remove(&(t->list));

			/* call timeout function */
			t->timeout_func(t->parameter);

464
			/* re-get tick */
465 466
			current_tick = rt_tick_get();

D
dzzxzz 已提交
467
			RT_DEBUG_LOG(RT_DEBUG_TIMER, ("current tick: %d\n", current_tick));
468

469 470
			if ((t->parent.flag & RT_TIMER_FLAG_PERIODIC) &&
					(t->parent.flag & RT_TIMER_FLAG_ACTIVATED))
471 472 473 474 475 476 477 478 479 480 481
			{
				/* start it */
				t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
				rt_timer_start(t);
			}
			else
			{
				/* stop timer */
				t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
			}
		}
482
		else break; /* not check anymore */
483 484
	}

D
dzzxzz 已提交
485
	RT_DEBUG_LOG(RT_DEBUG_TIMER, ("software timer check leave\n"));
486 487
}

488
/* system timer thread entry */
D
dzzxzz 已提交
489
static void rt_thread_timer_entry(void *parameter)
490
{
491 492
	rt_tick_t next_timeout;
	
493 494
	while (1)
	{
495 496 497 498 499 500 501 502 503 504
		next_timeout = rt_timer_list_next_timeout(&rt_soft_timer_list);
		if (next_timeout == RT_TICK_MAX)
		{
			rt_thread_suspend(rt_thread_self());
			rt_schedule();
		}
		else
		{
			rt_thread_delay(next_timeout);
		}
505

D
dzzxzz 已提交
506
		/* lock scheduler */
507
		rt_enter_critical();
D
dzzxzz 已提交
508
		/* check software timer */
509
		rt_soft_timer_check();
D
dzzxzz 已提交
510
		/* unlock scheduler */
511
		rt_exit_critical();
512
	}
513
}
514
#endif
515

516 517 518
/**
 * @ingroup SystemInit
 *
519
 * This function will initialize system timer
520 521 522
 *
 * @deprecated since 1.1.0, this function does not need to be invoked
 * in the system initialization.
523
 */
D
dzzxzz 已提交
524
void rt_system_timer_init(void)
525
{
B
bernard.xiong 已提交
526
}
527

B
bernard.xiong 已提交
528 529 530
/**
 * @ingroup SystemInit
 *
531
 * This function will initialize system timer thread
B
bernard.xiong 已提交
532
 */
D
dzzxzz 已提交
533
void rt_system_timer_thread_init(void)
B
bernard.xiong 已提交
534 535
{
#ifdef RT_USING_TIMER_SOFT
536 537
	rt_list_init(&rt_soft_timer_list);

D
dzzxzz 已提交
538
	/* start software timer thread */
539 540 541 542 543
	rt_thread_init(&timer_thread,
				   "timer",
				   rt_thread_timer_entry, RT_NULL,
				   &timer_thread_stack[0], sizeof(timer_thread_stack),
				   RT_TIMER_THREAD_PRIO, 10);
544 545

	/* startup */
546 547
	rt_thread_startup(&timer_thread);
#endif
548
}
549

550
/*@}*/