ImageVerifierCode 换一换
格式:DOC , 页数:7 ,大小:110.05KB ,
资源ID:3100931      下载积分:20 文钱
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,省得不是一点点
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.wenke99.com/d-3100931.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: QQ登录   微博登录 

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(嵌入式操作系统内核原理和开发(实时系统中的定时器).doc)为本站会员(sk****8)主动上传,文客久久仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知文客久久(发送邮件至hr@wenke99.com或直接QQ联系客服),我们立即给予删除!

嵌入式操作系统内核原理和开发(实时系统中的定时器).doc

1、 软件英才网 软件行业驰名招聘网站有需要请联系我们嵌入式操作系统内核原理和开发(实时系统中的定时器) 关于定时器的内容,其实我们之前也讨论过,也书写过相应的代码,但是表达得比较晦涩,效率也比较低。所以我们这里重新再讲一下定时器的相关代码,看看嵌入式系统中的定时器是怎么实现的。在我们之前讨论线程延时的时候就使用 hash 的方法,将不同的线程归类到不同的延时队列当中,并且按照时间长短先后排列,这样在最短的时间内就可以寻找到最合适的线程了。本质上,线程延时和定时器的基本原理是一样的。唯一的区别就是,线程延时响应的优先级要高一些,而定时器一般由独立线程完成,rawos 也是这么做的。cpp view

2、 plaincopy void timer_task(void *pa) RAW_U16 position; LIST *timer_head_ptr; LIST *iter; LIST *iter_temp; RAW_TIMER *timer_ptr; timer_sem.count = 0; while (1) /*timer task will be blocked after call this function*/ raw_semaphore_get( /*Disable the system schedule we do not need disable interrupt sin

3、ce nothing to do with interrupt*/ raw_disable_sche(); /*calculate which timer_head*/ raw_timer_count+; position = (RAW_U16)(raw_timer_count timer_head_ptr = 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

4、.*/ iter_temp = iter-next; 软件英才网 软件行业驰名招聘网站有需要请联系我们 timer_ptr = list_entry(iter, RAW_TIMER, timer_list); /*if timeout*/ if (raw_timer_count = timer_ptr-match) /*remove form timer list*/ timer_list_remove(timer_ptr); /*if timer is reschedulable*/ if (timer_ptr-reschedule_ticks) /*Sort by remain time*

5、/ timer_ptr-remain = timer_ptr-reschedule_ticks; timer_ptr-match = raw_timer_count + timer_ptr-remain; position = (RAW_U16)(timer_ptr-match timer_ptr-to_head = timer_list_priority_insert( /*Any way both condition need to call registered timer function*/ if (timer_ptr-raw_timeout_function) timer_ptr-

6、raw_timeout_function(timer_ptr-raw_timeout_param); iter = iter_temp; else break; /*exit because timer is not exit*/ else 软件英才网 软件行业驰名招聘网站有需要请联系我们 break; raw_enable_sche(); 由于基本原理和之前的线程延时是一样的,所以这里就不重复了。定时器的基本操作其实也不多,主要包括定时器创建、启动定时器、修改定时器、关闭定时器、删除定时器共五个基本函数,大家可以和我一起慢慢看过来。cpp view plaincopy RAW_U16 raw

7、_timer_create(RAW_TIMER *timer_ptr, RAW_U8 *name_ptr, RAW_VOID (*expiration_function)(RAW_U32), RAW_U32 expiration_input, RAW_U32 initial_ticks, RAW_U32 reschedule_ticks, RAW_U8 auto_activate) #if (RAW_TIMER_FUNCTION_CHECK 0) if (timer_ptr = 0) return RAW_NULL_OBJECT; if (expiration_function = 0) re

8、turn RAW_NULL_POINTER; #endif timer_ptr-name = name_ptr; timer_ptr-raw_timeout_function = expiration_function; timer_ptr-raw_timeout_param = expiration_input; timer_ptr-init_count = initial_ticks; timer_ptr-reschedule_ticks = reschedule_ticks; timer_ptr-remain = 0; timer_ptr-match = 0; timer_ptr-tim

9、er_state = TIMER_DEACTIVE; 软件英才网 软件行业驰名招聘网站有需要请联系我们 timer_ptr-to_head = 0; list_init( if (auto_activate) raw_timer_activate(timer_ptr); return RAW_SUCCESS; 创建定时器的操作很简单,主要是把输入的参数存入到 RAW_TIMER 结构里面。相关的参数包括名称、函数指针、函数参数、初始 tick、循环 tick、标志参数。当然,由于定时器分为单次定时和循环定时两种,所以这里会有两个参数,如果不是循环定时器,循环tick 参数可以不用配置。cpp

10、view plaincopy RAW_U16 raw_timer_activate(RAW_TIMER *timer_ptr) RAW_U16 position; RAW_SR_ALLOC(); #if (RAW_TIMER_FUNCTION_CHECK 0) if (timer_ptr = 0) return RAW_NULL_OBJECT; #endif /*Timer state TIMER_ACTIVE TIMER_DELETED is not allowed to delete*/ if (timer_ptr-timer_state = TIMER_ACTIVE) return RA

11、W_TIMER_STATE_INVALID; if (timer_ptr-timer_state = TIMER_DELETED) return RAW_TIMER_STATE_INVALID; RAW_CRITICAL_ENTER(); timer_ptr-match = raw_timer_count + timer_ptr-init_count; position = (RAW_U16)(timer_ptr-match /*Sort by remain time*/ timer_ptr-remain = timer_ptr-init_count; 软件英才网 软件行业驰名招聘网站有需要请

12、联系我们 /*Used by timer delete*/ timer_ptr-to_head = timer_list_priority_insert( timer_ptr-timer_state = TIMER_ACTIVE; RAW_CRITICAL_EXIT(); return RAW_SUCCESS; 启动定时器,就是把 tick 加入到定时器队列当中,看看 timer_list_priority_insert 这个函数你就明白了。因为整个函数的处理过程涉及到 全局变量 timer_head,所以需要在前后面添加中断的处理动作。cpp view plaincopy RAW_U16 r

13、aw_timer_change(RAW_TIMER *timer_ptr, RAW_U32 initial_ticks, RAW_U32 reschedule_ticks) RAW_SR_ALLOC(); #if (RAW_TIMER_FUNCTION_CHECK 0) if (timer_ptr = 0) return RAW_NULL_OBJECT; #endif /*Only timer state TIMER_DEACTIVE is not allowed here*/ if (timer_ptr-timer_state != TIMER_DEACTIVE) return RAW_TI

14、MER_STATE_INVALID; if (timer_ptr-timer_state = TIMER_DELETED) return RAW_TIMER_STATE_INVALID; RAW_CRITICAL_ENTER(); timer_ptr-init_count = initial_ticks; timer_ptr-reschedule_ticks = reschedule_ticks; RAW_CRITICAL_EXIT(); 软件英才网 软件行业驰名招聘网站有需要请联系我们 return RAW_SUCCESS; 定时器修改函数就是把初始 tick 和循环 tick 修改一下,当

15、然如果此时定时器还没有运行,初始 tick 还有用。但是一旦定时器起作用了,起作用的就只能是循环 tick 了,这一点大家要好好注意。cpp view plaincopy RAW_U16 raw_timer_deactivate(RAW_TIMER *timer_ptr) RAW_SR_ALLOC(); #if (RAW_TIMER_FUNCTION_CHECK 0) if (timer_ptr = 0) return RAW_NULL_OBJECT; #endif /*Timer state TIMER_DEACTIVE TIMER_DELETED is not allowed to del

16、ete*/ if (timer_ptr-timer_state = TIMER_DEACTIVE) return RAW_TIMER_STATE_INVALID; if (timer_ptr-timer_state = TIMER_DELETED) return RAW_TIMER_STATE_INVALID; RAW_CRITICAL_ENTER(); timer_list_remove(timer_ptr); timer_ptr-timer_state = TIMER_DEACTIVE; RAW_CRITICAL_EXIT(); return RAW_SUCCESS; 停止定时器,顾名思义

17、就是将定时器从队列中删除,同时设置状态为TIMER_DEACTIVE。当然在进行操作之前需要判断一下当前定时器的属性,如果定时器本身已经删除或者停止,那什么也不用做了。软件英才网 软件行业驰名招聘网站有需要请联系我们cpp view plaincopy RAW_U16 raw_timer_delete(RAW_TIMER *timer_ptr) RAW_SR_ALLOC(); #if (RAW_TIMER_FUNCTION_CHECK 0) if (timer_ptr = 0) return RAW_NULL_OBJECT; #endif if (timer_ptr-timer_state = TIMER_DELETED) return RAW_TIMER_STATE_INVALID; RAW_CRITICAL_ENTER(); timer_list_remove(timer_ptr); timer_ptr-timer_state = TIMER_DELETED; RAW_CRITICAL_EXIT(); return RAW_SUCCESS; 定时器的删除函数和定时器的停止函数是一样的,主要是判断逻辑不一样的。删除定时器,只要定时器的状态不是已经删除就可以了,其他的操作都是一样的。

Copyright © 2018-2021 Wenke99.com All rights reserved

工信部备案号浙ICP备20026746号-2  

公安局备案号:浙公网安备33038302330469号

本站为C2C交文档易平台,即用户上传的文档直接卖给下载用户,本站只是网络服务中间平台,所有原创文档下载所得归上传人所有,若您发现上传作品侵犯了您的权利,请立刻联系网站客服并提供证据,平台将在3个工作日内予以改正。