Beispiel #1
0
void rtgui_timer_set_timeout(rtgui_timer_t *timer, rt_int32_t time)
{
    RT_ASSERT(timer != RT_NULL);

    /* start rt-thread timer */
    if (timer->state == RTGUI_TIMER_ST_RUNNING)
    {
        rtgui_timer_stop(timer);
        rt_timer_control(&timer->timer, RT_TIMER_CTRL_SET_TIME, &time);
        rtgui_timer_start(timer);
    }
    else
    {
        rt_timer_control(&timer->timer, RT_TIMER_CTRL_SET_TIME, &time);
    }
}
Beispiel #2
0
/**
 * This function will let current thread sleep for some ticks.
 *
 * @param tick the sleep ticks
 *
 * @return RT_EOK
 *
 */
rt_err_t rt_thread_sleep(rt_tick_t tick)
{
	register rt_base_t temp;
	struct rt_thread *thread;

	/* disable interrupt */
	temp = rt_hw_interrupt_disable();
	/* set to current thread */
	thread = rt_current_thread;
	RT_ASSERT(thread != RT_NULL);

	/* suspend thread */
	rt_thread_suspend(thread);

	/* reset the timeout of thread timer and start it */
	rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &tick);
	rt_timer_start(&(thread->thread_timer));

	/* enable interrupt */
	rt_hw_interrupt_enable(temp);

	rt_schedule();

	/* clear error number of this thread to RT_EOK */
	if (thread->error == -RT_ETIMEOUT)
		thread->error = RT_EOK;

	return RT_EOK;
}
Beispiel #3
0
rt_err_t rt_completion_wait(struct rt_completion *completion, rt_int32_t timeout)
{
    rt_err_t result;
    rt_base_t level;
    rt_thread_t thread;
    RT_ASSERT(completion != RT_NULL);

    result = RT_EOK;
    thread = rt_thread_self();

    level = rt_hw_interrupt_disable();
    if (completion->flag != RT_COMPLETED)
    {
        /* only one thread can suspend on complete */
        RT_ASSERT(rt_list_isempty(&(completion->suspended_list)));

        if (timeout == 0)
        {
            result = -RT_ETIMEOUT;
            goto __exit;
        }
        else
        {
            /* reset thread error number */
            thread->error = RT_EOK;

            /* suspend thread */
            rt_thread_suspend(thread);
            /* add to suspended list */
            rt_list_insert_before(&(completion->suspended_list), &(thread->tlist));

            /* current context checking */
            RT_DEBUG_NOT_IN_INTERRUPT;

            /* start timer */
            if (timeout > 0)
            {
                /* reset the timeout of thread timer and start it */
                rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &timeout);
                rt_timer_start(&(thread->thread_timer));
            }
            /* enable interrupt */
            rt_hw_interrupt_enable(level);

            /* do schedule */
            rt_schedule();

            /* thread is waked up */
            result = thread->error;

            level = rt_hw_interrupt_disable();
            /* clean completed flag */
            completion->flag = RT_UNCOMPLETED;
        }
    }

__exit:
    rt_hw_interrupt_enable(level);
    return result;
}
Beispiel #4
0
static void timeout1(void* parameter)
{
    rt_tick_t timeout = 3000;
    rt_kprintf("periodic timer is timeout\n");
    
    rt_kprintf("the time set is %d\n", (rt_tick_get()-tick));
    tick = rt_tick_get();
    count ++;
   
    if (count >= 8)
    {
        rt_timer_control(timer1, RT_TIMER_CTRL_SET_ONESHOT, (void *)&timeout);
    
        rt_timer_control(timer1, RT_TIMER_CTRL_SET_TIME, (void *)&timeout);
        count = 0;
    }
}
Beispiel #5
0
void timer_conrol(void)
{
    timeout_value+=10;
    rt_timer_control(timer_test, RT_TIMER_CTRL_SET_TIME, (void *)&timeout_value);
//    rt_kprintf("timer timeout time set to %d !\n", timeout_value);
    if (timeout_value==500)    {
        rt_timer_stop(timer_test); /* 停止定时器 */
        rt_kprintf("timer stoped !\n");
    }
    if (timeout_value>=510) {
        /* 再次启动定时器 */
        rt_timer_start(timer_test);
        timeout_value=10;
        rt_timer_control(timer_test, RT_TIMER_CTRL_SET_TIME, (void *)&timeout_value);
    }

}
void vMBMasterPortTimersRespondTimeoutEnable()
{
    rt_tick_t timer_tick = MB_MASTER_TIMEOUT_MS_RESPOND * RT_TICK_PER_SECOND / 1000;

    /* Set current timer mode, don't change it.*/
    vMBMasterSetCurTimerMode(MB_TMODE_RESPOND_TIMEOUT);

    rt_timer_control(&timer, RT_TIMER_CTRL_SET_TIME, &timer_tick);

    rt_timer_start(&timer);
}
void vMBMasterPortTimersConvertDelayEnable()
{
    rt_tick_t timer_tick = MB_MASTER_DELAY_MS_CONVERT * RT_TICK_PER_SECOND / 1000;

    /* Set current timer mode, don't change it.*/
    vMBMasterSetCurTimerMode(MB_TMODE_CONVERT_DELAY);

    rt_timer_control(&timer, RT_TIMER_CTRL_SET_TIME, &timer_tick);

    rt_timer_start(&timer);
}
void vMBMasterPortTimersT35Enable()
{
    rt_tick_t timer_tick = (50 * usT35TimeOut50us)
            / (1000 * 1000 / RT_TICK_PER_SECOND);

    /* Set current timer mode, don't change it.*/
    vMBMasterSetCurTimerMode(MB_TMODE_T35);

    rt_timer_control(&timer, RT_TIMER_CTRL_SET_TIME, &timer_tick);

    rt_timer_start(&timer);
}
Beispiel #9
0
/// Start or restart timer
osStatus osTimerStart(osTimerId timer_id, uint32_t millisec)
{
    rt_err_t result;
    rt_tick_t ticks;

    ticks = rt_tick_from_millisecond(millisec);
    rt_timer_control(timer_id, RT_TIMER_CTRL_SET_TIME, &ticks);
    result = rt_timer_start(timer_id);
    if (result == RT_EOK)
        return osOK;
    else
        return osErrorOS;
}
Beispiel #10
0
/* 定时器超时函数 */
static void timeout1(void* parameter)
{
	rt_tick_t timeout = 50;
	
	rt_kprintf("periodic timer is timeout\n");

	count ++;
	/* 停止定时器自身 */
	if (count >= 8)
	{
		/* 控制定时器然后更改超时时间长度 */
		rt_timer_control(timer1, RT_TIMER_CTRL_SET_TIME, (void *)&timeout);
		count = 0;
	}
}
Beispiel #11
0
void led_flash(rt_uint32_t led,rt_uint32_t freq)
{

    rt_timer_t set_timer;

    switch(led)
        {
        case 0:
            set_timer = &timer1;
            break;

//        case 1:
//            set_timer = &timer2;
//            break;
// 
//        case 2:
//            set_timer = &timer3;
//            break;
// 
//        case 3:
//            set_timer = &timer4;
//            break;
// 
//        default:
//            rt_timer_stop(&timer1);
//            rt_timer_stop(&timer2);
//            rt_timer_stop(&timer3);
//            rt_timer_stop(&timer4);
//            return;
        }


    if(freq == 0)
    {
        rt_timer_stop(set_timer);
        return;
    }

    freq/=10;
    rt_timer_control(set_timer,RT_TIMER_CTRL_SET_TIME,(void*)&freq);
    rt_timer_start(set_timer);
}
Beispiel #12
0
/**
 * This function will allocate a block from memory pool
 *
 * @param mp the memory pool object
 * @param time the waiting time
 *
 * @return the allocated memory block or RT_NULL on allocated failed
 */
void *rt_mp_alloc(rt_mp_t mp, rt_int32_t time)
{
	rt_uint8_t *block_ptr;
	register rt_base_t level;
	struct rt_thread *thread;

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

	if (mp->block_free_count)
	{
		/* memory block is available. decrease the free block counter */
		mp->block_free_count --;

		/* get block from block list */
		block_ptr = mp->block_list;
		mp->block_list = *(rt_uint8_t **)block_ptr;

		/* point to memory pool */
		*(rt_uint8_t **)block_ptr = (rt_uint8_t *)mp;
	}
	else
	{
		/* memory block is unavailable. */
		if (time == 0)
		{
			/* enable interrupt */
			rt_hw_interrupt_enable(level);
			return RT_NULL;
		}
		else
		{
			RT_DEBUG_NOT_IN_INTERRUPT;

			/* get current thread */
			thread = rt_thread_self();

			/* need suspend thread */
			rt_thread_suspend(thread);
			rt_list_insert_after(&(mp->suspend_thread), &(thread->tlist));
			mp->suspend_thread_count ++;

			if (time > 0)
			{
				/* init thread timer and start it */
				rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &time);
				rt_timer_start(&(thread->thread_timer));
			}

			/* enable interrupt */
			rt_hw_interrupt_enable(level);

			/* do a schedule */
			rt_schedule();

			if (thread->error != RT_EOK)
				return RT_NULL;

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

			/* decrease free block */
			mp->block_free_count --;

			/* get block from block list */
			block_ptr = mp->block_list;
			mp->block_list = *(rt_uint8_t **)block_ptr;

			/* point to memory pool */
			*(rt_uint8_t **)block_ptr = (rt_uint8_t *)mp;
		}
	}

	/* enable interrupt */
	rt_hw_interrupt_enable(level);

	RT_OBJECT_HOOK_CALL(rt_mp_alloc_hook, (mp, (rt_uint8_t *)(block_ptr + sizeof(rt_uint8_t *))));

	return (rt_uint8_t *)(block_ptr + sizeof(rt_uint8_t *));
}
Beispiel #13
0
/**
 * This function will take a mutex, if the mutex is unavailable, the
 * thread shall wait for a specified time.
 *
 * @param mutex the mutex object
 * @param time the waiting time
 *
 * @return the error code
 */
rt_err_t rt_mutex_take (rt_mutex_t mutex, rt_int32_t time)
{
	register rt_base_t temp;
	struct rt_thread* thread;

	RT_ASSERT(mutex != RT_NULL);

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

	/* get current thread */
	thread = rt_thread_self();

#ifdef RT_USING_HOOK
	if (rt_object_trytake_hook != RT_NULL) rt_object_trytake_hook(&(mutex->parent.parent));
#endif

#ifdef RT_IPC_DEBUG
	rt_kprintf("mutex_take: current thread %s, mutex value: %d, hold: %d\n", 
		thread->name, mutex->value, mutex->hold);
#endif

	/* reset thread error */
	thread->error = RT_EOK;

	if (mutex->owner == thread)
	{
		/* it's the same thread */
		mutex->hold ++;
	}
	else
	{
		/* in initialization status, the value is 1. Therefore, if the 
		 * value is great than 1, which indicates the mutex is avaible.
		 */
		if (mutex->value > 0)
		{
			/* mutex is available */
			mutex->value --;

			/* set mutex owner and original priority */
			mutex->owner = thread;
			mutex->original_priority = thread->current_priority;
			mutex->hold ++;
		}
		else
		{
			/* no waiting, return with timeout */
			if (time == 0 )
			{
				/* set error as timeout */
				thread->error = -RT_ETIMEOUT;

				/* enable interrupt */
				rt_hw_interrupt_enable(temp);

				return -RT_ETIMEOUT;
			}
			else
			{
				/* mutex is unavailable, push to suspend list */
#ifdef RT_IPC_DEBUG
				rt_kprintf("mutex_take: suspend thread: %s\n", thread->name);
#endif
				/* change the owner thread priority of mutex */
				if (thread->current_priority < mutex->owner->current_priority)
				{
					/* change the owner thread priority */
					rt_thread_control(mutex->owner, RT_THREAD_CTRL_CHANGE_PRIORITY,
						&thread->current_priority);
				}

				/* suspend current thread */
				rt_ipc_object_suspend(&(mutex->parent), thread);

				/* has waiting time, start thread timer */
				if (time > 0)
				{
#ifdef RT_IPC_DEBUG
					rt_kprintf("mutex_take: start the timer of thread:%s\n", thread->name);
#endif
					/* reset the timeout of thread timer and start it */
					rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &time);
					rt_timer_start(&(thread->thread_timer));
				}

				/* enable interrupt */
				rt_hw_interrupt_enable(temp);

				/* do schedule */
				rt_schedule();

				if (thread->error != RT_EOK)
				{
					/* decrease suspended thread count */
					rt_ipc_object_decrease(&(mutex->parent));

					/* return error */
					return thread->error;
				}
				else
				{
					/* the mutex is taken successfully. */
					/* disable interrupt */
					temp = rt_hw_interrupt_disable();
				}
			}
		}
	}

	/* enable interrupt */
	rt_hw_interrupt_enable(temp);

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

	return RT_EOK;
}
Beispiel #14
0
/**
 * This function will take a semaphore, if the semaphore is unavailable, the
 * thread shall wait for a specified time.
 *
 * @param sem the semaphore object
 * @param time the waiting time
 *
 * @return the error code
 */
rt_err_t rt_sem_take (rt_sem_t sem, rt_int32_t time)
{
	register rt_base_t temp;
	struct rt_thread* thread;

	RT_ASSERT(sem != RT_NULL);

#ifdef RT_USING_HOOK
	if (rt_object_trytake_hook != RT_NULL) rt_object_trytake_hook(&(sem->parent.parent));
#endif

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

#ifdef RT_IPC_DEBUG
	rt_kprintf("thread %s take sem:%s, which value is: %d\n", rt_thread_self()->name, 
		((struct rt_object*)sem)->name, sem->value);
#endif
	if (sem->value > 0)
	{
		/* semaphore is available */
		sem->value --;

		/* enable interrupt */
		rt_hw_interrupt_enable(temp);
	}
	else
	{
		/* no waiting, return with timeout */
		if (time == 0 )
		{
			rt_hw_interrupt_enable(temp);
			return -RT_ETIMEOUT;
		}
		else
		{
			/* semaphore is unavailable, push to suspend list */
			/* get current thread */
			thread = rt_thread_self();

			/* reset thread error number */
			thread->error = RT_EOK;

#ifdef RT_IPC_DEBUG
			rt_kprintf("sem take: suspend thread - %s\n", thread->name);
#endif

			/* suspend thread */
			rt_ipc_object_suspend(&(sem->parent), thread);

			/* has waiting time, start thread timer */
			if (time > 0)
			{
#ifdef RT_IPC_DEBUG
				rt_kprintf("set thread:%s to timer list\n", thread->name);
#endif
				/* reset the timeout of thread timer and start it */
				rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &time);
				rt_timer_start(&(thread->thread_timer));
			}

			/* enable interrupt */
			rt_hw_interrupt_enable(temp);

			/* do schedule */
			rt_schedule();

			if (thread->error != RT_EOK)
			{
				/* decrease suspended thread count */
				rt_ipc_object_decrease(&(sem->parent));
				return thread->error;
			}
		}
	}

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

	return RT_EOK;
}
rt_err_t rt_data_queue_push(struct rt_data_queue *queue,
                            const void *data_ptr,
                            rt_size_t data_size,
                            rt_int32_t timeout)
{
    rt_uint16_t mask;
    rt_ubase_t  level;
    rt_thread_t thread;
    rt_err_t    result;

    RT_ASSERT(queue != RT_NULL);

    result = RT_EOK;
    thread = rt_thread_self();
    mask = queue->size - 1;

    level = rt_hw_interrupt_disable();
    while (queue->put_index - queue->get_index == queue->size)
    {
        queue->waiting_lwm = RT_TRUE;

        /* queue is full */
        if (timeout == 0)
        {
            result = -RT_ETIMEOUT;

            goto __exit;
        }

        /* current context checking */
        RT_DEBUG_NOT_IN_INTERRUPT;

        /* reset thread error number */
        thread->error = RT_EOK;

        /* suspend thread on the push list */
        rt_thread_suspend(thread);
        rt_list_insert_before(&(queue->suspended_push_list), &(thread->tlist));
        /* start timer */
        if (timeout > 0)
        {
            /* reset the timeout of thread timer and start it */
            rt_timer_control(&(thread->thread_timer),
                             RT_TIMER_CTRL_SET_TIME,
                             &timeout);
            rt_timer_start(&(thread->thread_timer));
        }

        /* enable interrupt */
        rt_hw_interrupt_enable(level);

        /* do schedule */
        rt_schedule();

        /* thread is waked up */
        result = thread->error;
        level = rt_hw_interrupt_disable();
        if (result != RT_EOK) goto __exit;
    }

    queue->queue[queue->put_index & mask].data_ptr  = data_ptr;
    queue->queue[queue->put_index & mask].data_size = data_size;
    queue->put_index += 1;

    if (!rt_list_isempty(&(queue->suspended_pop_list)))
    {
        /* there is at least one thread in suspended list */

        /* get thread entry */
        thread = rt_list_entry(queue->suspended_pop_list.next,
                               struct rt_thread,
                               tlist);

        /* resume it */
        rt_thread_resume(thread);
        rt_hw_interrupt_enable(level);

        /* perform a schedule */
        rt_schedule();

        return result;
    }
Beispiel #16
0
/**
 * This function will take a semaphore, if the semaphore is unavailable, the
 * thread shall wait for a specified time.
 *
 * @param sem the semaphore object
 * @param time the waiting time
 *
 * @return the error code
 */
rt_err_t rt_sem_take(rt_sem_t sem, rt_int32_t time)
{
	register rt_base_t temp;
	struct rt_thread *thread;

	RT_ASSERT(sem != RT_NULL);

	RT_OBJECT_HOOK_CALL(rt_object_trytake_hook, (&(sem->parent.parent)));

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

	RT_DEBUG_LOG(RT_DEBUG_IPC,
		("thread %s take sem:%s, which value is: %d\n", rt_thread_self()->name,
		((struct rt_object *)sem)->name, sem->value));

	if (sem->value > 0)
	{
		/* semaphore is available */
		sem->value --;

		/* enable interrupt */
		rt_hw_interrupt_enable(temp);
	}
	else
	{
		/* no waiting, return with timeout */
		if (time == 0)
		{
			rt_hw_interrupt_enable(temp);
			return -RT_ETIMEOUT;
		}
		else
		{
			/* current context checking */
			RT_DEBUG_NOT_IN_INTERRUPT;

			/* semaphore is unavailable, push to suspend list */
			/* get current thread */
			thread = rt_thread_self();

			/* reset thread error number */
			thread->error = RT_EOK;

			RT_DEBUG_LOG(RT_DEBUG_IPC, ("sem take: suspend thread - %s\n", thread->name));

			/* suspend thread */
			rt_ipc_list_suspend(&(sem->parent.suspend_thread),
				thread, sem->parent.parent.flag);

			/* has waiting time, start thread timer */
			if (time > 0)
			{
				RT_DEBUG_LOG(RT_DEBUG_IPC, ("set thread:%s to timer list\n", thread->name));

				/* reset the timeout of thread timer and start it */
				rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &time);
				rt_timer_start(&(thread->thread_timer));
			}

			/* enable interrupt */
			rt_hw_interrupt_enable(temp);

			/* do schedule */
			rt_schedule();

			if (thread->error != RT_EOK)
			{
				return thread->error;
			}
		}
	}

	RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(sem->parent.parent)));

	return RT_EOK;
}