管理员
最后登录1970-1-1
在线时间 小时
注册时间2018-4-4
|
发表于 2019-5-23 09:20:27
|
显示全部楼层
首先,你要明白火哥这样子写的用意是什么,为了什么目的,其次你要对比在实际应用上的源码是怎么样的,
第一,这是软件仿真的程序,火哥为什么这样子写,是因为阻塞延时用到了这个 thread->remaining_tick ;[mw_shl_code=c,true]void rt_thread_delay(rt_tick_t tick)
{
struct rt_thread *thread;
/* 获取当前线程的线程控制块 */
thread = rt_current_thread;
/* 设置延时时间 */
thread->remaining_tick = tick;
/* 进行系统调度 */
rt_schedule();
}[/mw_shl_code]
是为了方便延时,所以在更新时基的时候对所有的任务控制块进行 -- 操作,没做某个控制块判断也是问题所在,但是这个工程本身就是为了给大家参考思路的,无法下载到芯片运行。。
在rtt源码上实现的方式如下:
阻塞延时:
[mw_shl_code=c,true]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;
}[/mw_shl_code]
更新时基:
[mw_shl_code=c,true]void rt_tick_increase(void)
{
struct rt_thread *thread;
/* increase the global tick */
++ rt_tick;
/* check time slice */
thread = rt_thread_self();
-- thread->remaining_tick;
if (thread->remaining_tick == 0)
{
/* change to initialized tick */
thread->remaining_tick = thread->init_tick;
/* yield */
rt_thread_yield();
}
/* check timer */
rt_timer_check();
}[/mw_shl_code]
|
|