Exemple #1
0
/*
************************************************************************************************************************
*                                       Activate  the specified timer
*
* Description: This function is called to activate  the specified timer
*
* Arguments  :timer_ptr is the address of this timer object
*                    -----
*                
*				         
*				         
* Returns   RAW_TIMER_STATE_INVALID:  means timer is already active or timer is deleted..
*				   RAW_SUCCESS: means timer activate success
* Note(s)    :RAW_STATE_UNKNOWN wrong task state, probally sysytem error.
*
*             
************************************************************************************************************************
*/
RAW_U16 raw_timer_activate(RAW_TIMER *timer_ptr, RAW_VOID *expiration_input)
{
	RAW_U16 position;
	RAW_U16 mutex_ret;
	
	#if (RAW_TIMER_FUNCTION_CHECK > 0)
	
	if (timer_ptr == 0) {
		return RAW_NULL_OBJECT;
	}

	if (raw_int_nesting) {

		return RAW_NOT_CALLED_BY_ISR;
		
	}

	
	#endif

	if (timer_ptr->object_type != RAW_TIMER_OBJ_TYPE) {

		return RAW_ERROR_OBJECT_TYPE;
	}
	
	/*Timer state TIMER_ACTIVE TIMER_DELETED is not allowed to delete*/
	if (timer_ptr->timer_state == TIMER_ACTIVE) {
		
		return RAW_TIMER_STATE_INVALID;	
	}

	if (timer_ptr->timer_state == TIMER_DELETED) {
		
		return RAW_TIMER_STATE_INVALID;
	}

	timer_ptr->raw_timeout_param = expiration_input;
	
	mutex_ret = raw_mutex_get(&timer_mutex, RAW_WAIT_FOREVER);
	RAW_ASSERT(mutex_ret == RAW_SUCCESS);
		
	timer_ptr->match  = raw_timer_count + timer_ptr->init_count;
	position   = (RAW_U16)(timer_ptr->match & (TIMER_HEAD_NUMBERS - 1) );
	/*Sort by remain time*/
	timer_ptr->remain = timer_ptr->init_count;
	/*Used by timer delete*/
	timer_ptr->to_head = &timer_head[position];
	
	timer_list_priority_insert(&timer_head[position], timer_ptr);
	timer_ptr->timer_state = TIMER_ACTIVE;
	raw_mutex_put(&timer_mutex);

	return RAW_SUCCESS;
}
Exemple #2
0
/*
************************************************************************************************************************
*                                    Timer task 
*
* Description: This function is called to  start a timer task.
*
* Arguments  :pa is the parameters to task.
*                    -----
*                
*				         
*				         
* Returns   
*				   
* Note(s) :This function is called by internal, users shoud not touch this function.
*
*             
************************************************************************************************************************
*/
void timer_task(void *pa) 
{
	LIST 								*timer_head_ptr;
	LIST 								*iter;
	LIST 								*iter_temp;
	RAW_TIMER							*timer_ptr;
	RAW_OS_ERROR                         mutex_ret;
	RAW_U16                              callback_ret;
	
	/*reset the timer_sem count since it may not be 0 at this point, make it start here*/
	raw_semaphore_set(&timer_sem, 0);
	pa = pa;
	
	while (1) {
		
		/*timer task will be blocked after call this function*/
		raw_semaphore_get(&timer_sem, RAW_WAIT_FOREVER);

		mutex_ret = raw_mutex_get(&timer_mutex, RAW_WAIT_FOREVER);
		RAW_ASSERT(mutex_ret == RAW_SUCCESS);

		/*calculate which  timer_head*/
		raw_timer_count++;                                          
	
		timer_head_ptr  = &timer_head;

		iter = timer_head_ptr->next;
		
		while (RAW_TRUE) {

			/*if timer exits*/	
			if (iter != timer_head_ptr) {

				/*Must use iter_temp because iter may be remove later.*/
				iter_temp = iter->next;
				timer_ptr =  raw_list_entry(iter, RAW_TIMER, timer_list);

				/*if timeout*/
				if (raw_timer_count == timer_ptr->match) {  

					/*remove from timer list*/
					timer_list_remove(timer_ptr);

					/*if timer is reschedulable*/			
					if (timer_ptr->reschedule_ticks) {

						/*Sort by remain time*/
						timer_ptr->remain = timer_ptr->reschedule_ticks;

						timer_ptr->match  = raw_timer_count + timer_ptr->remain;
						timer_ptr->to_head = &timer_head;
						timer_list_priority_insert(&timer_head, timer_ptr);
					          
					} 

					else {

						timer_ptr->timer_state = TIMER_DEACTIVE;

					}

					/*Any way both condition need to call registered timer function*/
					/*the registered timer function should not touch any timer related API,otherwise system will be crashed*/
					if (timer_ptr->raw_timeout_function) {

						callback_ret = timer_ptr->raw_timeout_function(timer_ptr->raw_timeout_param);
						if ((callback_ret == TIMER_CALLBACK_STOP) && (timer_ptr->timer_state != TIMER_DEACTIVE)) {
							/*remove from timer list*/
							timer_list_remove(timer_ptr);
							timer_ptr->timer_state = TIMER_DEACTIVE;
						}
					         
					}

					iter  = iter_temp; 

				} 

				else { 

					break;	
				}

			}

			/*exit because timer is not exit*/		
			else {

				break;
			}

		}

		raw_mutex_put(&timer_mutex);

	}


}