timer.c 11.7 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
#endif
32

33
#ifdef RT_USING_HOOK
D
dzzxzz 已提交
34 35 36
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);
37 38 39 40

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

42 43 44 45 46
/*@{*/

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

/*@}*/
#endif

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

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

	timer->timeout_func = timeout;
D
dzzxzz 已提交
69
	timer->parameter    = parameter;
70

D
dzzxzz 已提交
71 72
	timer->timeout_tick = 0;
	timer->init_tick    = time;
73

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

/**
 * @addtogroup Clock
 */
D
dzzxzz 已提交
81

82 83 84
/*@{*/

/**
85
 * This function will initialize a timer, normally this function is used to initialize
86 87 88 89 90 91 92 93 94
 * 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
 */
95
void rt_timer_init(rt_timer_t timer,
D
dzzxzz 已提交
96 97
				   const char *name,
				   void (*timeout)(void *parameter), void *parameter,
98
				   rt_tick_t time, rt_uint8_t flag)
99 100 101 102
{
	/* timer check */
	RT_ASSERT(timer != RT_NULL);

103
	/* timer object initialization */
104 105 106 107 108
	rt_object_init((rt_object_t)timer, RT_Object_Class_Timer, name);

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

109
/**
110 111 112
 * This function will detach a timer from timer management.
 *
 * @param timer the static timer object
113
 *
114 115 116 117 118
 * @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;
119

120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
	/* 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 已提交
149
rt_timer_t rt_timer_create(const char *name, void (*timeout)(void *parameter), void *parameter, rt_tick_t time, rt_uint8_t flag)
150
{
D
dzzxzz 已提交
151
	struct rt_timer *timer;
152 153

	/* allocate a object */
D
dzzxzz 已提交
154
	timer = (struct rt_timer *)rt_object_allocate(RT_Object_Class_Timer, name);
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
	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;
175

176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
	/* 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 已提交
199
 * @return the operation status, RT_EOK on OK, -RT_ERROR on error
200 201 202
 */
rt_err_t rt_timer_start(rt_timer_t timer)
{
D
dzzxzz 已提交
203
	struct rt_timer *t;
204
	register rt_base_t level;
205
	rt_list_t *n, *timer_list;
206 207 208

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

212
	RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(timer->parent)));
213 214 215

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

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

221
#ifdef RT_USING_TIMER_SOFT
222
	if (timer->parent.flag & RT_TIMER_FLAG_SOFT_TIMER)
223
	{
224 225
		/* insert timer to soft timer list */
		timer_list = &rt_soft_timer_list;
226 227
	}
	else
228
#endif
229
	{
230
		/* insert timer to system timer list */
231 232
		timer_list = &rt_timer_list;
	}
233

234 235 236 237 238 239 240 241
	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)
242 243
		{
			rt_list_insert_before(n, &(timer->list));
244
			break;
245
		}
246
	}
247 248 249 250 251 252
	/* no found suitable position in timer list */
	if (n == timer_list)
	{
		rt_list_insert_before(n, &(timer->list));
	}
		
253 254 255 256 257 258 259 260 261 262 263 264 265
	timer->parent.flag |= RT_TIMER_FLAG_ACTIVATED;

	/* enable interrupt */
	rt_hw_interrupt_enable(level);

	return -RT_EOK;
}

/**
 * This function will stop the timer
 *
 * @param timer the timer to be stopped
 *
B
bernard.xiong@gmail.com 已提交
266
 * @return the operation status, RT_EOK on OK, -RT_ERROR on error
267 268 269 270 271 272 273
 */
rt_err_t rt_timer_stop(rt_timer_t timer)
{
	register rt_base_t level;

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

277
	RT_OBJECT_HOOK_CALL(rt_object_put_hook, (&(timer->parent)));
278 279 280 281

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

282 283 284
	/* change stat */
	timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;

285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
	/* remove it from timer list */
	rt_list_remove(&(timer->list));

	/* enable interrupt */
	rt_hw_interrupt_enable(level);

	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 已提交
301
 * @return RT_EOK
302
 */
D
dzzxzz 已提交
303
rt_err_t rt_timer_control(rt_timer_t timer, rt_uint8_t cmd, void *arg)
304 305 306 307 308 309 310
{
	/* timer check */
	RT_ASSERT(timer != RT_NULL);

	switch (cmd)
	{
	case RT_TIMER_CTRL_GET_TIME:
D
dzzxzz 已提交
311
		*(rt_tick_t *)arg = timer->init_tick;
312 313 314
		break;

	case RT_TIMER_CTRL_SET_TIME:
D
dzzxzz 已提交
315
		timer->init_tick = *(rt_tick_t *)arg;
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
		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;
}

/**
331
 * This function will check timer list, if a timeout event happens, the
332 333
 * corresponding timeout function will be invoked.
 *
B
bernard.xiong@gmail.com 已提交
334
 * @note this function shall be invoked in operating system timer interrupt.
335
 */
336
#ifdef RT_USING_TIMER_SOFT
D
dzzxzz 已提交
337
void rt_soft_timer_tick_increase(void);
338
#endif
339
void rt_timer_check(void)
340 341
{
	struct rt_timer *t;
342
	rt_tick_t current_tick;
343 344
	register rt_base_t level;

D
dzzxzz 已提交
345
	RT_DEBUG_LOG(RT_DEBUG_TIMER, ("timer check enter\n"));
346 347 348 349 350 351

	current_tick = rt_tick_get();

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

352
	while (!rt_list_isempty(&rt_timer_list))
353
	{
354
		t = rt_list_entry(rt_timer_list.next, struct rt_timer, list);
D
dzzxzz 已提交
355

356 357 358 359
		/*
		 * 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)
360
		{
361
			RT_OBJECT_HOOK_CALL(rt_timer_timeout_hook, (t));
362 363 364 365 366 367 368

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

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

369
			/* re-get tick */
370 371
			current_tick = rt_tick_get();

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

374 375
			if ((t->parent.flag & RT_TIMER_FLAG_PERIODIC) &&
					(t->parent.flag & RT_TIMER_FLAG_ACTIVATED))
376 377
			{
				/* start it */
378
				t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
379 380
				rt_timer_start(t);
			}
381 382 383 384 385
			else
			{
				/* stop timer */
				t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
			}
386
		}
D
dzzxzz 已提交
387 388
		else
			break;
389 390 391 392 393
	}

	/* enable interrupt */
	rt_hw_interrupt_enable(level);

B
bernard.xiong 已提交
394
	/* increase soft timer tick */
395
#ifdef RT_USING_TIMER_SOFT
D
dzzxzz 已提交
396
	rt_soft_timer_tick_increase();
397 398
#endif

D
dzzxzz 已提交
399
	RT_DEBUG_LOG(RT_DEBUG_TIMER, ("timer check leave\n"));
400 401
}

402 403
#ifdef RT_USING_TIMER_SOFT
static struct rt_thread timer_thread;
404
ALIGN(RT_ALIGN_SIZE)
405
static rt_uint8_t timer_thread_stack[RT_TIMER_THREAD_STACK_SIZE];
406 407
static struct rt_semaphore timer_sem;

B
bernard.xiong 已提交
408
static rt_uint16_t timer_ex_cnt;
409

D
dzzxzz 已提交
410
void rt_soft_timer_tick_increase(void)
411
{
D
dzzxzz 已提交
412
	timer_ex_cnt ++;
B
bernard.xiong 已提交
413
	if (timer_ex_cnt >= (RT_TICK_PER_SECOND / RT_TIMER_TICK_PER_SECOND))
414 415
	{
		timer_ex_cnt = 0;
B
bernard.xiong 已提交
416
		rt_sem_release(&timer_sem);
417 418 419 420 421 422 423
	}
}

/**
 * This function will check timer list, if a timeout event happens, the
 * corresponding timeout function will be invoked.
 */
D
dzzxzz 已提交
424
void rt_soft_timer_check(void)
425 426 427 428
{
	rt_tick_t current_tick;
	rt_list_t *n;
	struct rt_timer *t;
429

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

432 433
	current_tick = rt_tick_get();

D
dzzxzz 已提交
434
	for (n = rt_soft_timer_list.next; n != &(rt_soft_timer_list);)
435 436
	{
		t = rt_list_entry(n, struct rt_timer, list);
D
dzzxzz 已提交
437

438 439 440 441
		/*
		 * 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)
442
		{
443 444
			RT_OBJECT_HOOK_CALL(rt_timer_timeout_hook, (t));

445 446 447 448 449 450 451 452 453
			/* 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);

454
			/* re-get tick */
455 456
			current_tick = rt_tick_get();

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

459 460
			if ((t->parent.flag & RT_TIMER_FLAG_PERIODIC) &&
					(t->parent.flag & RT_TIMER_FLAG_ACTIVATED))
461 462 463 464 465 466 467 468 469 470 471
			{
				/* start it */
				t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
				rt_timer_start(t);
			}
			else
			{
				/* stop timer */
				t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
			}
		}
D
dzzxzz 已提交
472 473
		else
			break; /* not check anymore */
474 475
	}

D
dzzxzz 已提交
476
	RT_DEBUG_LOG(RT_DEBUG_TIMER, ("software timer check leave\n"));
477 478
}

479
/* system timer thread entry */
D
dzzxzz 已提交
480
static void rt_thread_timer_entry(void *parameter)
481 482 483
{
	while (1)
	{
B
bernard.xiong 已提交
484
		/* take software timer semaphore */
D
dzzxzz 已提交
485
		rt_sem_take(&timer_sem, RT_WAITING_FOREVER);
486

D
dzzxzz 已提交
487
		/* lock scheduler */
488
		rt_enter_critical();
489

D
dzzxzz 已提交
490
		/* check software timer */
491 492
		rt_soft_timer_check();

D
dzzxzz 已提交
493
		/* unlock scheduler */
494
		rt_exit_critical();
495
	}
496
}
497
#endif
498

499 500 501
/**
 * @ingroup SystemInit
 *
502
 * This function will initialize system timer
503 504 505
 *
 * @deprecated since 1.1.0, this function does not need to be invoked
 * in the system initialization.
506
 */
D
dzzxzz 已提交
507
void rt_system_timer_init(void)
508
{
B
bernard.xiong 已提交
509
}
510

B
bernard.xiong 已提交
511 512 513
/**
 * @ingroup SystemInit
 *
514
 * This function will initialize system timer thread
B
bernard.xiong 已提交
515
 */
D
dzzxzz 已提交
516
void rt_system_timer_thread_init(void)
B
bernard.xiong 已提交
517 518
{
#ifdef RT_USING_TIMER_SOFT
519 520 521
	rt_list_init(&rt_soft_timer_list);
	rt_sem_init(&timer_sem, "timer", 0, RT_IPC_FLAG_FIFO);

D
dzzxzz 已提交
522
	/* start software timer thread */
523 524 525 526 527
	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);
528 529

	/* startup */
530 531
	rt_thread_startup(&timer_thread);
#endif
532
}
533

534
/*@}*/