嵌入式操作系統(tǒng)內(nèi)核原理和開發(fā)(實(shí)時(shí)系統(tǒng)中的定時(shí)器)_第1頁
嵌入式操作系統(tǒng)內(nèi)核原理和開發(fā)(實(shí)時(shí)系統(tǒng)中的定時(shí)器)_第2頁
嵌入式操作系統(tǒng)內(nèi)核原理和開發(fā)(實(shí)時(shí)系統(tǒng)中的定時(shí)器)_第3頁
嵌入式操作系統(tǒng)內(nèi)核原理和開發(fā)(實(shí)時(shí)系統(tǒng)中的定時(shí)器)_第4頁
嵌入式操作系統(tǒng)內(nèi)核原理和開發(fā)(實(shí)時(shí)系統(tǒng)中的定時(shí)器)_第5頁
已閱讀5頁,還剩2頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、嵌入式操作系統(tǒng)內(nèi)核原理和開發(fā)(實(shí)時(shí)系統(tǒng)中的定時(shí)器) 關(guān)于定時(shí)器的內(nèi)容,其實(shí)我們之前也討論過,也書寫過相應(yīng)的代碼,但是表達(dá)得比較晦澀,效率也比較低。所以我們這里重新再講一下定時(shí)器的相關(guān)代碼,看看嵌入式系統(tǒng)中的定時(shí)器是怎么實(shí)現(xiàn)的。在我們之前討論線程延時(shí)的時(shí)候就使用hash的方法,將不同的線程歸類到不同的延時(shí)隊(duì)列當(dāng)中,并且按照時(shí)間長短先后排列,這樣在最短的時(shí)間內(nèi)就可以尋找到最合適的線程了。本質(zhì)上,線程延時(shí)和定時(shí)器的基本原理是一樣的。唯一的區(qū)別就是,線程延時(shí)響應(yīng)的優(yōu)先級要高一些,而定時(shí)器一般由獨(dú)立線程完成,rawos也是這么做的。cpp view plaincopy1 void timer_task(

2、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 sched

3、ule 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; 2

4、5 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 t

5、imer 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_H

6、EAD_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

7、_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 由于基本原理和之前的線程延時(shí)是一樣的,所以這里就不重復(fù)了。定時(shí)器的基本操作其實(shí)也不多,主要包括定時(shí)器創(chuàng)建、啟動(dòng)定時(shí)器、修改定時(shí)器、關(guān)閉定時(shí)器、刪除定時(shí)器共五個(gè)基本函數(shù),大家可以和我一起慢慢看過來。cpp view plaincopy79 R

8、AW_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_OB

9、JECT; 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_tick

10、s = 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 創(chuàng)建定時(shí)器的操作

11、很簡單,主要是把輸入的參數(shù)存入到RAW_TIMER結(jié)構(gòu)里面。相關(guān)的參數(shù)包括名稱、函數(shù)指針、函數(shù)參數(shù)、初始tick、循環(huán)tick、標(biāo)志參數(shù)。當(dāng)然,由于定時(shí)器分為單次定時(shí)和循環(huán)定時(shí)兩種,所以這里會有兩個(gè)參數(shù),如果不是循環(huán)定時(shí)器,循環(huán)tick參數(shù)可以不用配置。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 (tim

12、er_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_INVAL

13、ID; 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_

14、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 啟動(dòng)定時(shí)器,就是把tick加入到定時(shí)器隊(duì)列當(dāng)中,看看timer_list_priority_insert這個(gè)函數(shù)你就明白了。因?yàn)檎麄€(gè)函數(shù)的處理過程涉及到 全局變量timer_head,所以需要在前后面添加中斷的處理

15、動(dòng)作。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_DEACTIV

16、E 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

17、_ticks = reschedule_ticks; 178 RAW_CRITICAL_EXIT(); 179 return RAW_SUCCESS; 180 定時(shí)器修改函數(shù)就是把初始tick和循環(huán)tick修改一下,當(dāng)然如果此時(shí)定時(shí)器還沒有運(yùn)行,初始tick還有用。但是一旦定時(shí)器起作用了,起作用的就只能是循環(huán)tick了,這一點(diǎn)大家要好好注意。cpp view plaincopy181 RAW_U16 raw_timer_deactivate(RAW_TIMER *timer_ptr) 182 183 RAW_SR_ALLOC(); 184 185 #if (RAW_TIMER_FUNCTION

18、_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 停止定時(shí)器,顧名思義就是將定時(shí)器從隊(duì)列中刪除,同時(shí)

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論