首页 OSAL_Timers.c其余函数解读

OSAL_Timers.c其余函数解读

举报
开通vip

OSAL_Timers.c其余函数解读CC2430学习之九__ OSAL_Timers.c其余函数解读 2010.4.29 void osalTimerInit( void ) { // Initialize the rollover modulo // tmr_count:Amount of time per tick - in micro-sec // 即每个时钟周期的时间 // #define TIMER_DECR_TIME 1 【OnBoard.h】 // 每次时钟滴答的时间(毫秒) // #define TICK_TIME 1000 【OnB...

OSAL_Timers.c其余函数解读
CC2430学习之九__ OSAL_Timers.c其余函数解读 2010.4.29 void osalTimerInit( void ) { // Initialize the rollover modulo // tmr_count:Amount of time per tick - in micro-sec // 即每个时钟周期的时间 // #define TIMER_DECR_TIME 1 【OnBoard.h】 // 每次时钟滴答的时间(毫秒) // #define TICK_TIME 1000 【OnBoard.h】 tmr_count = TICK_TIME; tmr_decr_time = TIMER_DECR_TIME; // Initialize the system timer osal_timer_activate( false ); timerActive = false; osal_systemClock = 0; } osalTimerRec_t *osalFindTimer( byte task_id, uint16 event_flag ) { osalTimerRec_t *srchTimer; // Head of the timer list // 先找出定时器列 关于同志近三年现实表现材料材料类招标技术评分表图表与交易pdf视力表打印pdf用图表说话 pdf 头 srchTimer = timerHead; // Stop when found or at the end // 找到所需定时器或者到达表尾时停止 while ( srchTimer ) { if ( srchTimer->event_flag == event_flag && srchTimer->task_id == task_id ) break; // Not this one, check another // 不是,寻找下一个 srchTimer = srchTimer->next; } //找到后返回定时器表地址 return ( srchTimer ); } void osalDeleteTimer( osalTimerRec_t *rmTimer ) { osalTimerRec_t *srchTimer; // Does the timer list really exist if ( (timerHead != NULL) && rmTimer ) { // Add it to the end of the timer list srchTimer = timerHead; // First element? // 如果是第一个元素的话 if ( srchTimer == rmTimer ) { //首地址移到第一个元素的尾地址 timerHead = rmTimer->next; //释放这个定时器的内存 osal_mem_free( rmTimer ); } else { // Stop when found or at the end // 停止寻找,如果找到了,或者到表尾了 while ( srchTimer->next && srchTimer->next != rmTimer ) //否则,移到下一个 srchTimer = srchTimer->next; // Found? if ( srchTimer->next == rmTimer ) { // Fix pointers固定节点 srchTimer->next = rmTimer->next; // Deallocate the timer struct memory // 解除定时器内存 osal_mem_free( rmTimer ); } } } } byte osal_start_timerEx( byte taskID, UINT16 event_id, UINT16 timeout_value ) { halIntState_t intState; osalTimerRec_t *newTimer; //临界区,先保存EA然后禁止所有中断 HAL_ENTER_CRITICAL_SECTION( intState ); // Hold off interrupts. // Add timer // 加入一个定时器到定时器列表 newTimer = osalAddTimer( taskID, event_id, timeout_value ); if ( newTimer ) { #ifdef POWER_SAVING // Update timer registers // 更新定时器注册信息 osal_retune_timers(); (void)timerActive; #endif // Does the timer need to be started? // 是否这个定时器需要被启动 if ( timerActive == FALSE ) { //false - turn off, true - turn on osal_timer_activate( TRUE ); } } //临界区,恢复EA信息 HAL_EXIT_CRITICAL_SECTION( intState ); // Re-enable interrupts. //成功,返回ZSUCCESS,不成功,返回NO_TIMER_AVAIL return ( (newTimer != NULL) ? ZSUCCESS : NO_TIMER_AVAIL ); } 最后通过条件选择语句,来确定返回值 byte osal_stop_timerEx( byte task_id, UINT16 event_id ) { halIntState_t intState; osalTimerRec_t *foundTimer; //临界区,先保存EA然后禁止所有中断 HAL_ENTER_CRITICAL_SECTION( intState ); // Hold off interrupts. // Find the timer to stop // 先找到定时器 foundTimer = osalFindTimer( task_id, event_id ); // 如果上一步成功 if ( foundTimer ) { //删除该定时器 osalDeleteTimer( foundTimer ); #ifdef POWER_SAVING osal_retune_timers(); #endif } //临界区,恢复EA信息 HAL_EXIT_CRITICAL_SECTION( intState ); // Re-enable interrupts. return ( (foundTimer != NULL) ? ZSUCCESS : INVALID_EVENT_ID ); } 从这可以看出, osalFindTimer()/osalDeleteTimer()使用在这里面的。而且真正有用的函数一般都会返回一个ZSUCCESS(成功的话),或者一个不成功的标志。 通过这样的标志来来确认操作是否成功。 //得到时钟滴答次数 UINT16 osal_get_timeoutEx( byte task_id, UINT16 event_id ) { halIntState_t intState; uint16 rtrn = 0; osalTimerRec_t *tmr; HAL_ENTER_CRITICAL_SECTION( intState ); // Hold off interrupts. tmr = osalFindTimer( task_id, event_id ); if ( tmr ) { rtrn = tmr->timeout; } HAL_EXIT_CRITICAL_SECTION( intState ); // Re-enable interrupts. return rtrn; } //打开或关闭硬件时钟 void osal_timer_activate( byte turn_on ) { osal_timer_hw_setup( turn_on ); timerActive = turn_on; } //统计活动着的定时器 byte osal_timer_num_active( void ) { halIntState_t intState; byte num_timers = 0; osalTimerRec_t *srchTimer; HAL_ENTER_CRITICAL_SECTION( intState ); // Hold off interrupts. // Head of the timer list srchTimer = timerHead; // Count timers in the list // 一个一个的找 while ( srchTimer != NULL ) { num_timers++; srchTimer = srchTimer->next; } HAL_EXIT_CRITICAL_SECTION( intState ); // Re-enable interrupts. //返回活动着的定时器数目 return num_timers; } //配置系统定时器 void osal_timer_hw_setup( byte turn_on ) { if (turn_on) { //开启定时器 HalTimerStart (OSAL_TIMER, tmr_count ); } else { //停止定时器 HalTimerStop (OSAL_TIMER); } } //如果定义了省电模式 #if defined( POWER_SAVING ) //睡眠模式 void osal_sleep_timers( void ) { #ifndef TIMER_INT //如果有活动着的定时器 if ( osal_timer_num_active() ) //打开中断 osal_set_timer_interrupt( TRUE ); #endif } //不睡眠 void osal_unsleep_timers( void ) { #ifndef TIMER_INT //关闭中断 osal_set_timer_interrupt( FALSE ); #endif } #endif void osal_set_timer_interrupt( byte turn_on ) { // Enable or disable timer interrupts //打开或关闭中断 HalTimerInterruptEnable ( OSAL_TIMER, HAL_TIMER_CH_MODE_OUTPUT_COMPARE, turn_on); } //更新定时器结构,每隔1ms调用一次 static void osalTimerUpdate( uint16 updateTime ) { halIntState_t intState; osalTimerRec_t *srchTimer; osalTimerRec_t *prevTimer; osalTimerRec_t *saveTimer; HAL_ENTER_CRITICAL_SECTION( intState ); // Hold off interrupts. // Update the system time // osal_systemClock是32位int型的, // 可以计时2^32ms=49.7天 osal_systemClock += updateTime; // Look for open timer slot // 寻找开启的定时器时隙 if ( timerHead != NULL ) { // Add it to the end of the timer list srchTimer = timerHead; prevTimer = (void *)NULL; // Look for open timer slot while ( srchTimer ) { // Decrease the correct amount of time // 执行一次,srchTimer->timeout - updateTime if (srchTimer->timeout <= updateTime) srchTimer->timeout = 0; else srchTimer->timeout = srchTimer->timeout - updateTime; // When timeout, execute the task // 溢出的时候执行任务 if ( srchTimer->timeout == 0 ) { //为一个任务设置事件标志 osal_set_event( srchTimer->task_id, srchTimer->event_flag ); // Take out of list // 移出列表 if ( prevTimer == NULL ) timerHead = srchTimer->next; else prevTimer->next = srchTimer->next; // Next操作下一个 saveTimer = srchTimer->next; // Free memory osal_mem_free( srchTimer ); srchTimer = saveTimer; } else { // Get next prevTimer = srchTimer; srchTimer = srchTimer->next; } } #ifdef POWER_SAVING osal_retune_timers(); #endif } HAL_EXIT_CRITICAL_SECTION( intState ); // Re-enable interrupts. } //读出本地系统时钟 uint32 osal_GetSystemClock( void ) { return ( osal_systemClock ); } //timeout是进入绑定模式的持续时间(s) //找出最低的进入绑定模式的持续时间,返回时间 uint16 osal_next_timeout( void ) { uint16 nextTimeout; osalTimerRec_t *srchTimer; if ( timerHead != NULL ) { // Head of the timer list srchTimer = timerHead; nextTimeout = OSAL_TIMERS_MAX_TIMEOUT; // Look for the next timeout timer while ( srchTimer != NULL ) { if (srchTimer->timeout < nextTimeout) { nextTimeout = srchTimer->timeout; } // Check next timer srchTimer = srchTimer->next; } } else { // No timers nextTimeout = 0; } return ( nextTimeout ); } #endif // POWER_SAVING /osal_retune_timers调整CPU睡眠时间到最低的超时值, //如果超时值大于RETUNE_THRESHOLD, //则睡眠时间将被设置为RETUNE_THRESHOLD void osal_retune_timers( void ) { halIntState_t intState; uint16 nextTimeout; HAL_ENTER_CRITICAL_SECTION( intState ); // Hold off interrupts. // Next occuring timeout nextTimeout = osal_next_timeout(); // Make sure timer counter can handle it if ( !nextTimeout || (nextTimeout > RETUNE_THRESHOLD) ) nextTimeout = RETUNE_THRESHOLD; if (nextTimeout != tmr_decr_time) { // Stop the clock osal_timer_activate( FALSE ); // Alter the rolling time tmr_decr_time = nextTimeout; tmr_count = (uint32)nextTimeout * TICK_TIME; // Restart the clock osal_timer_activate( TRUE ); } HAL_EXIT_CRITICAL_SECTION( intState ); // Re-enable interrupts. } //启动或者停止系统时钟,参数为FALSE或者TRUE void osal_adjust_timers( void ) { uint16 eTime; if ( timerHead != NULL ) { // Compute elapsed time (msec) eTime = TimerElapsed() / TICK_COUNT; if ( eTime ) osalTimerUpdate( eTime ); } }
本文档为【OSAL_Timers.c其余函数解读】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
is_473877
暂无简介~
格式:doc
大小:59KB
软件:Word
页数:10
分类:互联网
上传时间:2011-06-26
浏览量:60