嵌入式操作系统内核原理和开发(实时系统中的定时器).doc_第1页
嵌入式操作系统内核原理和开发(实时系统中的定时器).doc_第2页
嵌入式操作系统内核原理和开发(实时系统中的定时器).doc_第3页
嵌入式操作系统内核原理和开发(实时系统中的定时器).doc_第4页
嵌入式操作系统内核原理和开发(实时系统中的定时器).doc_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

软件英才网 软件行业驰名招聘网站嵌入式操作系统内核原理和开发(实时系统中的定时器) 关于定时器的内容,其实我们之前也讨论过,也书写过相应的代码,但是表达得比较晦涩,效率也比较低。所以我们这里重新再讲一下定时器的相关代码,看看嵌入式系统中的定时器是怎么实现的。在我们之前讨论线程延时的时候就使用hash的方法,将不同的线程归类到不同的延时队列当中,并且按照时间长短先后排列,这样在最短的时间内就可以寻找到最合适的线程了。本质上,线程延时和定时器的基本原理是一样的。唯一的区别就是,线程延时响应的优先级要高一些,而定时器一般由独立线程完成,rawos也是这么做的。cpp view plaincopy1 void timer_task(void *pa) 2 3 RAW_U16 position; 4 LIST *timer_head_ptr; 5 LIST *iter; 6 LIST *iter_temp; 7 RAW_TIMER *timer_ptr; 8 9 timer_sem.count = 0; 10 11 while (1) 12 13 /*timer task will be blocked after call this function*/ 14 raw_semaphore_get(&timer_sem, RAW_WAIT_FOREVER); 15 16 /*Disable the system schedule we do not need disable interrupt since nothing to do with interrupt*/ 17 raw_disable_sche(); 18 19 /*calculate which timer_head*/ 20 raw_timer_count+; 21 position = (RAW_U16)(raw_timer_count & (TIMER_HEAD_NUMBERS - 1) ); 22 timer_head_ptr = &timer_headposition; 23 24 iter =timer_head_ptr-next; 25 26 while (RAW_TRUE) 27 /*if timer exits*/ 28 if (iter !=timer_head_ptr) 29 /*Must use iter_temp because iter may be remove later.*/ 30 iter_temp = iter-next; 31 timer_ptr = list_entry(iter, RAW_TIMER, timer_list); 32 33 /*if timeout*/ 34 if (raw_timer_count = timer_ptr-match) 35 36 /*remove form timer list*/ 37 timer_list_remove(timer_ptr); 38 /*if timer is reschedulable*/ 39 if (timer_ptr-reschedule_ticks) 40 /*Sort by remain time*/ 41 timer_ptr-remain = timer_ptr-reschedule_ticks; 42 43 timer_ptr-match = raw_timer_count + timer_ptr-remain; 44 position = (RAW_U16)(timer_ptr-match & (TIMER_HEAD_NUMBERS - 1); 45 timer_ptr-to_head = &timer_headposition; 46 timer_list_priority_insert(&timer_headposition, timer_ptr); 47 48 49 50 /*Any way both condition need to call registered timer function*/ 51 52 if (timer_ptr-raw_timeout_function) 53 timer_ptr-raw_timeout_function(timer_ptr-raw_timeout_param); 54 55 56 57 iter = iter_temp; 58 59 60 else 61 62 break; 63 64 65 66 67 /*exit because timer is not exit*/ 68 else 69 70 break; 71 72 73 74 75 raw_enable_sche(); 76 77 78 由于基本原理和之前的线程延时是一样的,所以这里就不重复了。定时器的基本操作其实也不多,主要包括定时器创建、启动定时器、修改定时器、关闭定时器、删除定时器共五个基本函数,大家可以和我一起慢慢看过来。cpp view plaincopy79 RAW_U16 raw_timer_create(RAW_TIMER *timer_ptr, RAW_U8 *name_ptr, 80 RAW_VOID (*expiration_function)(RAW_U32), RAW_U32 expiration_input, 81 RAW_U32 initial_ticks, RAW_U32 reschedule_ticks, RAW_U8 auto_activate) 82 83 84 85 #if (RAW_TIMER_FUNCTION_CHECK 0) 86 87 if (timer_ptr = 0) 88 return RAW_NULL_OBJECT; 89 90 91 if (expiration_function = 0) 92 return RAW_NULL_POINTER; 93 94 95 #endif 96 97 timer_ptr-name = name_ptr; 98 timer_ptr-raw_timeout_function = expiration_function; 99 timer_ptr-raw_timeout_param = expiration_input; 100 timer_ptr-init_count = initial_ticks; 101 timer_ptr-reschedule_ticks = reschedule_ticks; 102 timer_ptr-remain = 0; 103 timer_ptr-match = 0; 104 timer_ptr-timer_state = TIMER_DEACTIVE; 105 timer_ptr-to_head = 0; 106 107 list_init(&timer_ptr-timer_list); 108 109 if (auto_activate) 110 111 raw_timer_activate(timer_ptr); 112 113 114 return RAW_SUCCESS; 115 116 创建定时器的操作很简单,主要是把输入的参数存入到RAW_TIMER结构里面。相关的参数包括名称、函数指针、函数参数、初始tick、循环tick、标志参数。当然,由于定时器分为单次定时和循环定时两种,所以这里会有两个参数,如果不是循环定时器,循环tick参数可以不用配置。cpp view plaincopy117 RAW_U16 raw_timer_activate(RAW_TIMER *timer_ptr) 118 119 RAW_U16 position; 120 RAW_SR_ALLOC(); 121 122 #if (RAW_TIMER_FUNCTION_CHECK 0) 123 124 if (timer_ptr = 0) 125 return RAW_NULL_OBJECT; 126 127 128 #endif 129 130 /*Timer state TIMER_ACTIVE TIMER_DELETED is not allowed to delete*/ 131 if (timer_ptr-timer_state = TIMER_ACTIVE) 132 return RAW_TIMER_STATE_INVALID; 133 134 if (timer_ptr-timer_state = TIMER_DELETED) 135 return RAW_TIMER_STATE_INVALID; 136 137 RAW_CRITICAL_ENTER(); 138 timer_ptr-match = raw_timer_count + timer_ptr-init_count; 139 position = (RAW_U16)(timer_ptr-match & (TIMER_HEAD_NUMBERS - 1) ); 140 /*Sort by remain time*/ 141 timer_ptr-remain = timer_ptr-init_count; 142 /*Used by timer delete*/ 143 timer_ptr-to_head = &timer_headposition; 144 145 timer_list_priority_insert(&timer_headposition, timer_ptr); 146 timer_ptr-timer_state = TIMER_ACTIVE; 147 148 RAW_CRITICAL_EXIT(); 149 150 151 return RAW_SUCCESS; 152 153 启动定时器,就是把tick加入到定时器队列当中,看看timer_list_priority_insert这个函数你就明白了。因为整个函数的处理过程涉及到 全局变量timer_head,所以需要在前后面添加中断的处理动作。cpp view plaincopy154 RAW_U16 raw_timer_change(RAW_TIMER *timer_ptr, RAW_U32 initial_ticks, RAW_U32 reschedule_ticks) 155 156 RAW_SR_ALLOC(); 157 158 #if (RAW_TIMER_FUNCTION_CHECK 0) 159 160 if (timer_ptr = 0) 161 return RAW_NULL_OBJECT; 162 163 164 #endif 165 166 /*Only timer state TIMER_DEACTIVE is not allowed here*/ 167 if (timer_ptr-timer_state != TIMER_DEACTIVE) 168 return RAW_TIMER_STATE_INVALID; 169 170 171 if (timer_ptr-timer_state = TIMER_DELETED) 172 return RAW_TIMER_STATE_INVALID; 173 174 175 RAW_CRITICAL_ENTER(); 176 timer_ptr-init_count = initial_ticks; 177 timer_ptr-reschedule_ticks = reschedule_ticks; 178 RAW_CRITICAL_EXIT(); 179 return RAW_SUCCESS; 180 定时器修改函数就是把初始tick和循环tick修改一下,当然如果此时定时器还没有运行,初始tick还有用。但是一旦定时器起作用了,起作用的就只能是循环tick了,这一点大家要好好注意。cpp view plaincopy181 RAW_U16 raw_timer_deactivate(RAW_TIMER *timer_ptr) 182 183 RAW_SR_ALLOC(); 184 185 #if (RAW_TIMER_FUNCTION_CHECK 0) 186 187 if (timer_ptr = 0) 188 return RAW_NULL_OBJECT; 189 190 191 #endif 192 193 /*Timer state TIMER_DEACTIVE TIMER_DELETED is not allowed to delete*/ 194 if (timer_ptr-timer_state = TIMER_DEACTIVE) 195 return RAW_TIMER_STATE_INVALID; 196 197 198 if (timer_ptr-timer_state = TIMER_DELETED) 199 return RAW_TIMER_STATE_INVALID; 200 201 202 203 RAW_CRITICAL_ENTER(); 204 timer_list_remove(timer_ptr); 205 timer_ptr-timer_state = TIMER_DEACTIVE; 206 RAW_CRITICAL_EXIT(); 207 208 return RAW_SUCCESS; 209 210 211 停止定时器,顾名思义就是将定时器从队列中删除,同时设置状态为T

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论