timer.c 11.6 KB
Newer Older
1 2 3
/*
 * File      : timer.c
 * This file is part of RT-Thread RTOS
B
bernard.xiong 已提交
4
 * COPYRIGHT (C) 2006 - 2009, 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 19 20 21 22 23 24
 */

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

#include "kservice.h"

25
/* #define RT_TIMER_DEBUG */
26 27

/* hard timer list */
28 29
static rt_list_t rt_timer_list;

30
#ifdef RT_USING_TIMER_SOFT
31 32
/* soft timer list */
static rt_list_t rt_soft_timer_list;
33
#endif
34

35 36 37 38 39 40 41 42 43 44 45 46 47
#ifdef RT_USING_HOOK
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);

/**
 * @addtogroup Hook
 */
/*@{*/

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

/*@}*/
#endif

59 60 61
static void _rt_timer_init(rt_timer_t timer,
						   void (*timeout)(void* parameter), void* parameter,
						   rt_tick_t time, rt_uint8_t flag)
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
{
	/* set flag */
	timer->parent.flag 	= flag;

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

	timer->timeout_func = timeout;
	timer->parameter   	= parameter;

	timer->timeout_tick	= 0;
	timer->init_tick 	= time;

	/* init timer list */
	rt_list_init(&(timer->list));
}

/**
 * @addtogroup Clock
 */
/*@{*/

/**
85
 * This function will init 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 96 97 98
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)
99 100 101 102 103 104 105 106 107 108
{
	/* timer check */
	RT_ASSERT(timer != RT_NULL);

	/* timer object init */
	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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
	/* 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
 */
rt_timer_t rt_timer_create(const char* name, void (*timeout)(void* parameter), void* parameter, rt_tick_t time, rt_uint8_t flag)
{
	struct rt_timer* timer;

	/* allocate a object */
	timer = (struct rt_timer*)rt_object_allocate(RT_Object_Class_Timer, name);
	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;
176

177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
	/* 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
 *
 * @return the operation status, RT_EOK on OK; RT_ERROR on error
 *
 */
rt_err_t rt_timer_start(rt_timer_t timer)
{
	rt_list_t *n;
	struct rt_timer* t;
	register rt_base_t level;

	/* timer check */
	RT_ASSERT(timer != RT_NULL);
	if (timer->parent.flag & RT_TIMER_FLAG_ACTIVATED) return -RT_ERROR;

#ifdef RT_USING_HOOK
	if (rt_object_take_hook != RT_NULL) rt_object_take_hook(&(timer->parent));
#endif

	timer->timeout_tick = rt_tick_get() + timer->init_tick;

	/* disable interrupt */
	level = rt_hw_interrupt_disable();
221
#ifdef RT_USING_TIMER_SOFT
222
	if (timer->parent.flag & RT_TIMER_FLAG_SOFT_TIMER)
223 224
	{
        /* insert timer to soft timer list */
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
		for (n = rt_soft_timer_list.next; n != &rt_soft_timer_list; n = n->next)
		{
			t = rt_list_entry(n, struct rt_timer, list);
			if (t->timeout_tick > timer->timeout_tick)
			{
				rt_list_insert_before(n, &(timer->list));
				break;
			}
		}
		/* no found suitable position in timer list */
		if (n == &rt_soft_timer_list)
		{
			rt_list_insert_before(n, &(timer->list));
		}
	}
	else
241
#endif
242
	{
243 244
		/* insert timer to system timer list */
		for (n = rt_timer_list.next; n != &rt_timer_list; n = n->next)
245
		{
246 247 248 249 250 251
			t = rt_list_entry(n, struct rt_timer, list);
			if (t->timeout_tick > timer->timeout_tick)
			{
				rt_list_insert_before(n, &(timer->list));
				break;
			}
252 253
		}

254 255 256 257 258
		/* no found suitable position in timer list */
		if (n == &rt_timer_list)
		{
			rt_list_insert_before(n, &(timer->list));
		}
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
	}

	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
 *
 * @return the operation status, RT_EOK on OK; RT_ERROR on error
 *
 */
rt_err_t rt_timer_stop(rt_timer_t timer)
{
	register rt_base_t level;

	/* timer check */
	RT_ASSERT(timer != RT_NULL);
	if(!(timer->parent.flag & RT_TIMER_FLAG_ACTIVATED)) return -RT_ERROR;

#ifdef RT_USING_HOOK
	if (rt_object_put_hook != RT_NULL) rt_object_put_hook(&(timer->parent));
#endif

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

292 293 294
	/* change stat */
	timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;

295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
	/* 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
 *
 * @return the operation status, RT_EOK on OK; RT_ERROR on error
 *
 */
rt_err_t rt_timer_control(rt_timer_t timer, rt_uint8_t cmd, void* arg)
{
	/* timer check */
	RT_ASSERT(timer != RT_NULL);

	switch (cmd)
	{
	case RT_TIMER_CTRL_GET_TIME:
		*(rt_tick_t*)arg = timer->init_tick;
		break;

	case RT_TIMER_CTRL_SET_TIME:
		timer->init_tick = *(rt_tick_t*)arg;
		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;
}

/**
342
 * This function will check timer list, if a timeout event happens, the
343 344 345
 * corresponding timeout function will be invoked.
 *
 */
346
#ifdef RT_USING_TIMER_SOFT
B
bernard.xiong 已提交
347
void  rt_soft_timer_tick_increase (void);
348
#endif
349
void rt_timer_check(void)
350 351 352 353 354 355
{
	rt_tick_t current_tick;
	rt_list_t *n;
	struct rt_timer *t;
	register rt_base_t level;

356
#ifdef RT_TIMER_DEBUG
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
	rt_kprintf("timer check enter\n");
#endif

	current_tick = rt_tick_get();

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

	for (n = rt_timer_list.next; n != &(rt_timer_list); )
	{
		t = rt_list_entry(n, struct rt_timer, list);
		if (current_tick >= t->timeout_tick)
		{
#ifdef RT_USING_HOOK
			if (rt_timer_timeout_hook != RT_NULL) rt_timer_timeout_hook(t);
#endif
			/* 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);

			/* reget tick */
			current_tick = rt_tick_get();

385
#ifdef RT_TIMER_DEBUG
386 387 388
			rt_kprintf("current tick: %d\n", current_tick);
#endif

389 390
			if ((t->parent.flag & RT_TIMER_FLAG_PERIODIC) &&
					(t->parent.flag & RT_TIMER_FLAG_ACTIVATED))
391 392
			{
				/* start it */
393
				t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
394 395
				rt_timer_start(t);
			}
396 397 398 399 400
			else
			{
				/* stop timer */
				t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
			}
401 402 403 404 405 406 407
		}
		else break;
	}

	/* enable interrupt */
	rt_hw_interrupt_enable(level);

B
bernard.xiong 已提交
408
	/* increase soft timer tick */
409
#ifdef RT_USING_TIMER_SOFT
B
bernard.xiong 已提交
410
	rt_soft_timer_tick_increase ( );
411 412 413
#endif

#ifdef TIMER_DEBUG
414 415 416 417
	rt_kprintf("timer check leave\n");
#endif
}

418 419 420
#ifdef RT_USING_TIMER_SOFT
static struct rt_thread timer_thread;
static rt_uint8_t timer_thread_stack[RT_TIMER_THREAD_STACK_SIZE];
421 422
static struct rt_semaphore timer_sem;

B
bernard.xiong 已提交
423
static rt_uint16_t timer_ex_cnt;
424

B
bernard.xiong 已提交
425
void  rt_soft_timer_tick_increase (void)
426 427
{
	timer_ex_cnt++;
B
bernard.xiong 已提交
428
	if (timer_ex_cnt >= (RT_TICK_PER_SECOND / RT_TIMER_TICK_PER_SECOND))
429 430
	{
		timer_ex_cnt = 0;
B
bernard.xiong 已提交
431
		rt_sem_release(&timer_sem);
432 433 434 435 436 437 438 439 440
	}
}

/**
 * This function will check timer list, if a timeout event happens, the
 * corresponding timeout function will be invoked.
 *
 */
void rt_soft_timer_check()
441 442 443 444
{
	rt_tick_t current_tick;
	rt_list_t *n;
	struct rt_timer *t;
445 446 447

#ifdef RT_TIMER_DEBUG
	rt_kprintf("software timer check enter\n");
448 449
#endif

450 451
	current_tick = rt_tick_get();

452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
	for (n = rt_soft_timer_list.next; n != &(rt_soft_timer_list); )
	{
		t = rt_list_entry(n, struct rt_timer, list);
		if (current_tick >= t->timeout_tick)
		{
#ifdef RT_USING_HOOK
			if (rt_timer_timeout_hook != RT_NULL) rt_timer_timeout_hook(t);
#endif
			/* 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);

			/* reget tick */
			current_tick = rt_tick_get();

472
#ifdef RT_TIMER_DEBUG
473 474 475
			rt_kprintf("current tick: %d\n", current_tick);
#endif

476 477
			if ((t->parent.flag & RT_TIMER_FLAG_PERIODIC) &&
					(t->parent.flag & RT_TIMER_FLAG_ACTIVATED))
478 479 480 481 482 483 484 485 486 487 488 489
			{
				/* start it */
				t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
				rt_timer_start(t);
			}
			else
			{
				/* stop timer */
				t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
			}
		}
		else break;
490 491 492 493
	}

#ifdef RT_TIMER_DEBUG
	rt_kprintf("software timer check leave\n");
494 495 496
#endif
}

497
/* system timer thread entry */
498 499 500 501
static void rt_thread_timer_entry(void* parameter)
{
	while (1)
	{
B
bernard.xiong 已提交
502
		/* take software timer semaphore */
503
		rt_sem_take(&timer_sem,RT_WAITING_FOREVER);
504

505
        /* lock scheduler */
506
		rt_enter_critical();
507 508 509 510 511

        /* check software timer */
		rt_soft_timer_check();

        /* unlock scheduler */
512
		rt_exit_critical();
513
	}
514
}
515
#endif
516

517 518 519 520 521 522 523 524 525 526 527 528 529
/**
 * @ingroup SystemInit
 *
 * This function will init system timer
 *
 */
void rt_system_timer_init()
{
	rt_list_init(&rt_timer_list);

#ifdef RT_USING_TIMER_SOFT
	rt_list_init(&rt_soft_timer_list);
    rt_sem_init(&timer_sem, "timer", 0, RT_IPC_FLAG_FIFO);
B
bernard.xiong 已提交
530 531
#endif
}
532

B
bernard.xiong 已提交
533 534 535 536 537 538 539 540 541
/**
 * @ingroup SystemInit
 *
 * This function will init system timer thread
 *
 */
void rt_system_timer_thread_init()
{
#ifdef RT_USING_TIMER_SOFT
542 543 544 545 546 547
    /* start software timer thread */
	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);
548 549

	/* startup */
550 551
	rt_thread_startup(&timer_thread);
#endif
552
}
553

554
/*@}*/