source: UsbWattMeter/trunk/asp_dcre/kernel/time_event.c

Last change on this file was 167, checked in by coas-nagasima, 8 years ago

MIMEにSJISを設定

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-csrc; charset=SHIFT_JIS
File size: 9.4 KB
Line 
1/*
2 * TOPPERS/ASP Kernel
3 * Toyohashi Open Platform for Embedded Real-Time Systems/
4 * Advanced Standard Profile Kernel
5 *
6 * Copyright (C) 2000-2003 by Embedded and Real-Time Systems Laboratory
7 * Toyohashi Univ. of Technology, JAPAN
8 * Copyright (C) 2005-2013 by Embedded and Real-Time Systems Laboratory
9 * Graduate School of Information Science, Nagoya Univ., JAPAN
10 *
11 * 上記著作権者は,以下の(1)〜(4)の条件を満たす場合に限り,本ソフトウェ
12 * ア(本ソフトウェアを改変したものを含む.以下同じ)を使用・複製・改
13 * 変・再配布(以下,利用と呼ぶ)することを無償で許諾する.
14 * (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作
15 * 権表示,この利用条件および下記の無保証規定が,そのままの形でソー
16 * スコード中に含まれていること.
17 * (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使
18 * 用できる形で再配布する場合には,再配布に伴うドキュメント(利用
19 * 者マニュアルなど)に,上記の著作権表示,この利用条件および下記
20 * の無保証規定を掲載すること.
21 * (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使
22 * 用できない形で再配布する場合には,次のいずれかの条件を満たすこ
23 * と.
24 * (a) 再配布に伴うドキュメント(利用者マニュアルなど)に,上記の著
25 * 作権表示,この利用条件および下記の無保証規定を掲載すること.
26 * (b) 再配布の形態を,別に定める方法によって,TOPPERSプロジェクトに
27 * 報告すること.
28 * (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
29 * 害からも,上記著作権者およびTOPPERSプロジェクトを免責すること.
30 * また,本ソフトウェアのユーザまたはエンドユーザからのいかなる理
31 * 由に基づく請求からも,上記著作権者およびTOPPERSプロジェクトを
32 * 免責すること.
33 *
34 * 本ソフトウェアは,無保証で提供されているものである.上記著作権者お
35 * よびTOPPERSプロジェクトは,本ソフトウェアに関して,特定の使用目的
36 * に対する適合性も含めて,いかなる保証も行わない.また,本ソフトウェ
37 * アの利用により直接的または間接的に生じたいかなる損害に関しても,そ
38 * の責任を負わない.
39 *
40 * $Id: time_event.c 167 2016-03-08 11:37:45Z coas-nagasima $
41 */
42
43/*
44 * タイムイベント管理モジュール
45 */
46
47#include "kernel_impl.h"
48#include "time_event.h"
49
50/*
51 * タイムイベントヒープ操作マクロ
52 */
53#define PARENT(index) ((index) >> 1) /* 親ノードを求める */
54#define LCHILD(index) ((index) << 1) /* 左の子ノードを求める */
55#define TMEVT_NODE(index) (tmevt_heap[(index) - 1])
56
57/*
58 * イベント発生時刻比較マクロ
59 *
60 * イベント発生時刻は,min_timeからの相対値で比較する.すなわち,
61 * min_timeを最小値(最も近い時刻),mit_time-1が最大値(最も遠い時刻)
62 * とみなして比較する.
63 */
64#define EVTTIM_LT(t1, t2) (((t1) - min_time) < ((t2) - min_time))
65#define EVTTIM_LE(t1, t2) (((t1) - min_time) <= ((t2) - min_time))
66
67#ifdef TOPPERS_tmeini
68
69/*
70 * 現在のシステム時刻(単位: 1ミリ秒)
71 *
72 * 厳密には,前のタイムティックのシステム時刻.
73 */
74EVTTIM current_time;
75
76/*
77 * タイムイベントヒープ中で有効な最小のシステム時刻(単位: 1ミリ秒)
78 */
79EVTTIM min_time;
80
81/*
82 * 次のタイムティックのシステム時刻(単位: 1ミリ秒)
83 */
84EVTTIM next_time;
85
86/*
87 * システム時刻積算用変数(単位: 1/TIC_DENOミリ秒)
88 */
89#if TIC_DENO != 1U
90uint_t next_subtime;
91#endif /* TIC_DENO != 1U */
92
93/*
94 * タイムイベントヒープの最後の使用領域のインデックス
95 */
96uint_t last_index;
97
98/*
99 * タイマモジュールの初期化
100 */
101void
102initialize_tmevt(void)
103{
104 current_time = 0U;
105 min_time = 0U;
106 next_time = current_time + TIC_NUME / TIC_DENO;
107#if TIC_DENO != 1U
108 next_subtime = TIC_NUME % TIC_DENO;
109#endif /* TIC_DENO != 1U */
110 last_index = 0U;
111}
112
113#endif /* TOPPERS_tmeini */
114
115/*
116 * タイムイベントの挿入位置を上向きに探索
117 *
118 * 時刻timeに発生するタイムイベントを挿入するノードを空けるために,
119 * ヒープの上に向かって空ノードを移動させる.移動前の空ノードの位置を
120 * indexに渡すと,移動後の空ノードの位置(すなわち挿入位置)を返す.
121 */
122#ifdef TOPPERS_tmeup
123
124uint_t
125tmevt_up(uint_t index, EVTTIM time)
126{
127 uint_t parent;
128
129 while (index > 1) {
130 /*
131 * 親ノードのイベント発生時刻の方が早い(または同じ)ならば,
132 * indexが挿入位置なのでループを抜ける.
133 */
134 parent = PARENT(index);
135 if (EVTTIM_LE(TMEVT_NODE(parent).time, time)) {
136 break;
137 }
138
139 /*
140 * 親ノードをindexの位置に移動させる.
141 */
142 TMEVT_NODE(index) = TMEVT_NODE(parent);
143 TMEVT_NODE(index).p_tmevtb->index = index;
144
145 /*
146 * indexを親ノードの位置に更新.
147 */
148 index = parent;
149 }
150 return(index);
151}
152
153#endif /* TOPPERS_tmeup */
154
155/*
156 * タイムイベントの挿入位置を下向きに探索
157 *
158 * 時刻timeに発生するタイムイベントを挿入するノードを空けるために,
159 * ヒープの下に向かって空ノードを移動させる.移動前の空ノードの位置を
160 * indexに渡すと,移動後の空ノードの位置(すなわち挿入位置)を返す.
161 */
162#ifdef TOPPERS_tmedown
163
164uint_t
165tmevt_down(uint_t index, EVTTIM time)
166{
167 uint_t child;
168
169 while ((child = LCHILD(index)) <= last_index) {
170 /*
171 * 左右の子ノードのイベント発生時刻を比較し,早い方の子ノード
172 * の位置をchildに設定する.以下の子ノードは,ここで選ばれた方
173 * の子ノードのこと.
174 */
175 if (child + 1 <= last_index
176 && EVTTIM_LT(TMEVT_NODE(child + 1).time,
177 TMEVT_NODE(child).time)) {
178 child = child + 1;
179 }
180
181 /*
182 * 子ノードのイベント発生時刻の方が遅い(または同じ)ならば,
183 * indexが挿入位置なのでループを抜ける.
184 */
185 if (EVTTIM_LE(time, TMEVT_NODE(child).time)) {
186 break;
187 }
188
189 /*
190 * 子ノードをindexの位置に移動させる.
191 */
192 TMEVT_NODE(index) = TMEVT_NODE(child);
193 TMEVT_NODE(index).p_tmevtb->index = index;
194
195 /*
196 * indexを子ノードの位置に更新.
197 */
198 index = child;
199 }
200 return(index);
201}
202
203#endif /* TOPPERS_tmedown */
204
205/*
206 * タイムイベントヒープへの登録
207 *
208 * p_tmevtbで指定したタイムイベントブロックを,timeで指定した時間が経
209 * 過後にイベントが発生するように,タイムイベントヒープに登録する.
210 */
211#ifdef TOPPERS_tmeins
212
213void
214tmevtb_insert(TMEVTB *p_tmevtb, EVTTIM time)
215{
216 uint_t index;
217
218 /*
219 * last_indexをインクリメントし,そこから上に挿入位置を探す.
220 */
221 index = tmevt_up(++last_index, time);
222
223 /*
224 * タイムイベントをindexの位置に挿入する.
225 */
226 TMEVT_NODE(index).time = time;
227 TMEVT_NODE(index).p_tmevtb = p_tmevtb;
228 p_tmevtb->index = index;
229}
230
231#endif /* TOPPERS_tmeins */
232
233/*
234 * タイムイベントヒープからの削除
235 */
236#ifdef TOPPERS_tmedel
237
238void
239tmevtb_delete(TMEVTB *p_tmevtb)
240{
241 uint_t index = p_tmevtb->index;
242 uint_t parent;
243 EVTTIM event_time = TMEVT_NODE(last_index).time;
244
245 /*
246 * 削除によりタイムイベントヒープが空になる場合は何もしない.
247 */
248 if (--last_index == 0) {
249 return;
250 }
251
252 /*
253 * 削除したノードの位置に最後のノード(last_index+1の位置のノード)
254 * を挿入し,それを適切な位置へ移動させる.実際には,最後のノード
255 * を実際に挿入するのではなく,削除したノードの位置が空ノードにな
256 * るので,最後のノードを挿入すべき位置へ向けて空ノードを移動させ
257 * る.
258 * 最後のノードのイベント発生時刻が,削除したノードの親ノードのイ
259 * ベント発生時刻より前の場合には,上に向かって挿入位置を探す.そ
260 * うでない場合には,下に向かって探す.
261 */
262 if (index > 1 && EVTTIM_LT(event_time,
263 TMEVT_NODE(parent = PARENT(index)).time)) {
264 /*
265 * 親ノードをindexの位置に移動させる.
266 */
267 TMEVT_NODE(index) = TMEVT_NODE(parent);
268 TMEVT_NODE(index).p_tmevtb->index = index;
269
270 /*
271 * 削除したノードの親ノードから上に向かって挿入位置を探す.
272 */
273 index = tmevt_up(parent, event_time);
274 }
275 else {
276 /*
277 * 削除したノードから下に向かって挿入位置を探す.
278 */
279 index = tmevt_down(index, event_time);
280 }
281
282 /*
283 * 最後のノードをindexの位置に挿入する.
284 */
285 TMEVT_NODE(index) = TMEVT_NODE(last_index + 1);
286 TMEVT_NODE(index).p_tmevtb->index = index;
287}
288
289#endif /* TOPPERS_tmedel */
290
291/*
292 * タイムイベントヒープの先頭のノードの削除
293 */
294Inline void
295tmevtb_delete_top(void)
296{
297 uint_t index;
298 EVTTIM event_time = TMEVT_NODE(last_index).time;
299
300 /*
301 * 削除によりタイムイベントヒープが空になる場合は何もしない.
302 */
303 if (--last_index == 0) {
304 return;
305 }
306
307 /*
308 * ルートノードに最後のノード(last_index + 1 の位置のノード)を
309 * 挿入し,それを適切な位置へ移動させる.実際には,最後のノードを
310 * 実際に挿入するのではなく,ルートノードが空ノードになるので,最
311 * 後のノードを挿入すべき位置へ向けて空ノードを移動させる.
312 */
313 index = tmevt_down(1, event_time);
314
315 /*
316 * 最後のノードをindexの位置に挿入する.
317 */
318 TMEVT_NODE(index) = TMEVT_NODE(last_index + 1);
319 TMEVT_NODE(index).p_tmevtb->index = index;
320}
321
322/*
323 * タイムイベントまでの残り時間の計算
324 */
325#ifdef TOPPERS_tmeltim
326
327RELTIM
328tmevt_lefttim(TMEVTB *p_tmevtb)
329{
330 EVTTIM time;
331
332 time = TMEVT_NODE(p_tmevtb->index).time;
333 if (EVTTIM_LE(time, next_time)) {
334 /*
335 * 次のタイムティックで処理される場合には0を返す.
336 */
337 return(0U);
338 }
339 else {
340 return((RELTIM)(time - base_time));
341 }
342}
343
344#endif /* TOPPERS_tmeltim */
345
346/*
347 * タイムティックの供給
348 */
349#ifdef TOPPERS_sigtim
350
351void
352signal_time(void)
353{
354 TMEVTB *p_tmevtb;
355
356 assert(sense_context());
357 assert(!i_sense_lock());
358
359 i_lock_cpu();
360
361 /*
362 * current_timeを更新する.
363 */
364 current_time = next_time;
365
366 /*
367 * next_time,next_subtimeを更新する.
368 */
369#if TIC_DENO == 1U
370 next_time = current_time + TIC_NUME;
371#else /* TIC_DENO == 1U */
372 next_subtime += TIC_NUME % TIC_DENO;
373 next_time = current_time + TIC_NUME / TIC_DENO;
374 if (next_subtime >= TIC_DENO) {
375 next_subtime -= TIC_DENO;
376 next_time += 1U;
377 }
378#endif /* TIC_DENO == 1U */
379
380 /*
381 * current_timeよりイベント発生時刻の早い(または同じ)タイムイベ
382 * ントを,タイムイベントヒープから削除し,コールバック関数を呼び
383 * 出す.
384 */
385 while (last_index > 0 && EVTTIM_LE(TMEVT_NODE(1).time, current_time)) {
386 p_tmevtb = TMEVT_NODE(1).p_tmevtb;
387 tmevtb_delete_top();
388 (*(p_tmevtb->callback))(p_tmevtb->arg);
389 }
390
391 /*
392 * min_timeを更新する.
393 */
394 min_time = current_time;
395
396 i_unlock_cpu();
397}
398
399#endif /* TOPPERS_sigtim */
Note: See TracBrowser for help on using the repository browser.