source: asp3_wo_tecs/trunk/target/macosx_xcode/target_timer.c@ 302

Last change on this file since 302 was 302, checked in by ertl-honda, 7 years ago

TECSレスのASP3の開発のため以下のtrunkからコピー
http://dev.toppers.jp/svn/asp3/branches/WO_TECS-3.C.0

File size: 8.9 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) 2005-2015 by Embedded and Real-Time Systems Laboratory
7 * Graduate School of Information Science, Nagoya Univ., JAPAN
8 *
9 * 上記著作権者
10は,以下の(1)〜(4)の条件を満たす場合に限り,本ソフトウェ
11 * ア(本ソフトウェアを改変したものを含む.以下同じ)を使用・複製・改
12 * 変・再é…
13å¸ƒï¼ˆä»¥ä¸‹ï¼Œåˆ©ç”¨ã¨å‘¼ã¶ï¼‰ã™ã‚‹ã“とを無償で許諾する.
14 * (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作
15 * 権表示,この利用条件および下記の無保証規定が,そのままの形でソー
16 * スコード中に含まれていること.
17 * (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使
18 * 用できる形で再é…
19å¸ƒã™ã‚‹å ´åˆã«ã¯ï¼Œå†é…
20å¸ƒã«ä¼´ã†ãƒ‰ã‚­ãƒ¥ãƒ¡ãƒ³ãƒˆï¼ˆåˆ©ç”¨
21 * 者
22マニュアルなど)に,上記の著作権表示,この利用条件および下記
23 * の無保証規定を掲載すること.
24 * (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使
25 * 用できない形で再é…
26å¸ƒã™ã‚‹å ´åˆã«ã¯ï¼Œæ¬¡ã®ã„ずれかの条件を満たすこ
27 * と.
28 * (a) 再é…
29å¸ƒã«ä¼´ã†ãƒ‰ã‚­ãƒ¥ãƒ¡ãƒ³ãƒˆï¼ˆåˆ©ç”¨è€…
30マニュアルなど)に,上記の著
31 * 作権表示,この利用条件および下記の無保証規定を掲載すること.
32 * (b) 再é…
33å¸ƒã®å½¢æ…
34‹ã‚’,別に定める方法によって,TOPPERSプロジェクトに
35 * 報告すること.
36 * (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
37 * 害からも,上記著作権者
38およびTOPPERSプロジェクトをå…
39è²¬ã™ã‚‹ã“と.
40 * また,本ソフトウェアのユーザまたはエンドユーザからのいかなる理
41 * 由に基づく請求からも,上記著作権者
42およびTOPPERSプロジェクトを
43 * å…
44è²¬ã™ã‚‹ã“と.
45 *
46 * 本ソフトウェアは,無保証で提供されているものである.上記著作権者
47お
48 * よびTOPPERSプロジェクトは,本ソフトウェアに関して,特定の使用目的
49 * に対する適合性も含めて,いかなる保証も行わない.また,本ソフトウェ
50 * アの利用により直接的または間接的に生じたいかなる損害に関しても,そ
51 * の責任を負わない.
52 *
53 * $Id: target_timer.c 458 2015-08-21 14:59:09Z ertl-hiro $
54 */
55
56/*
57 * タイマドライバ(Mac OS X用)
58 *
59 * Mac OS X上で使用できるマイクロ秒精度のタイマが1つしかないため,1つ
60 * のインターバルタイマでシステム時刻を進めるための高分解能タイマを実
61 * 現している.この方法は,時間のずれが生じるために推奨できないが,シ
62 * ミュレーション環境で時間のずれは大きい問題ではないため,この方法を
63 * 採用している.
64 *
65 * さらに,オーバランハンドラ機能のためのタイマ(オーバランタイマ)も,
66 * 同じインターバルタイマを多重化して実現している.
67 */
68
69#include "kernel_impl.h"
70#include "time_event.h"
71#ifdef TOPPERS_SUPPORT_OVRHDR
72#include "overrun.h"
73#endif /* TOPPERS_SUPPORT_OVRHDR */
74#include "target_timer.h"
75#include <sys/time.h>
76
77/*
78 * 高分解能タイマの状æ…
79‹
80 */
81static HRTCNT hrtcnt_current; /* 高分解能タイマの現時点の値 */
82static HRTCNT hrtcnt_left; /* 高分解能タイマ割込みの残りカウント */
83static bool_t hrtcnt_pending; /* 高分解能タイマ割込みが要求中 */
84
85/*
86 * オーバランタイマの状æ…
87‹
88 */
89#ifdef TOPPERS_SUPPORT_OVRHDR
90static PRCTIM ovrtimer_left; /* オーバランタイマ割込みの残り時間 */
91static bool_t ovrtimer_pending; /* オーバランタイマ割込みが要求中 */
92#endif /* TOPPERS_SUPPORT_OVRHDR */
93
94/*
95 * インターバルタイマの設定状æ…
96‹
97 */
98static ulong_t itimer_value; /* 最後にインターバルタイマに設定した値 */
99
100/*
101 * インターバルタイマを停止させるための変数
102 */
103static const struct itimerval itimerval_stop = {{ 0, 0 }, { 0, 0 }};
104
105/*
106 * インターバルタイマの経過時間の算出
107 */
108Inline ulong_t
109itimer_progress(struct itimerval *p_val)
110{
111 return(itimer_value - ((ulong_t)(p_val->it_value.tv_sec) * 1000000U
112 + (ulong_t)(p_val->it_value.tv_usec)));
113}
114
115/*
116 * インターバルタイマの一時停止処理
117 */
118static void
119pause_itimer(void)
120{
121 ulong_t progtim;
122 struct itimerval val;
123
124 /*
125 * インターバルタイマを停止し,経過した時間を求める.
126 */
127 setitimer(ITIMER_REAL, &itimerval_stop, &val);
128 progtim = itimer_progress(&val);
129
130 /*
131 * 高分解能タイマの状æ…
132‹ã‚’更新する.
133 */
134 hrtcnt_current += progtim;
135 if (hrtcnt_left > 0U) {
136 if (hrtcnt_left > progtim) {
137 hrtcnt_left -= progtim;
138 }
139 else {
140 hrtcnt_left = 0U;
141 hrtcnt_pending = true;
142 /*
143 * ここでSIGALRMを要求しないと,タイマ割込み要求が抜けてし
144 * まう場合がある(サンプルプログラムで,'o'を連打すると再
145 * 現する).スプリアス割込みがかかるため,避けたいところ
146 * だが,抜ける原因が不明であるため,修正できていない.
147 */
148 raise(SIGALRM);
149 }
150 }
151
152 /*
153 * オーバランタイマの状æ…
154‹ã‚’更新する.
155 */
156#ifdef TOPPERS_SUPPORT_OVRHDR
157 if (ovrtimer_left > 0U) {
158 if (ovrtimer_left > progtim) {
159 ovrtimer_left -= progtim;
160 }
161 else {
162 ovrtimer_left = 0U;
163 ovrtimer_pending = true;
164 }
165 }
166#endif /* TOPPERS_SUPPORT_OVRHDR */
167}
168
169/*
170 * インターバルタイマの動作開始処理
171 */
172static void
173start_itimer(void)
174{
175 struct itimerval val;
176
177 /*
178 * インターバルタイマに設定する時間を求める.
179 */
180 itimer_value = (hrtcnt_left > 0U) ? hrtcnt_left : ULONG_MAX;
181#ifdef TOPPERS_SUPPORT_OVRHDR
182 if (ovrtimer_left > 0U && ovrtimer_left < itimer_value) {
183 itimer_value = ovrtimer_left;
184 }
185#endif /* TOPPERS_SUPPORT_OVRHDR */
186
187 /*
188 * インターバルタイマの動作を開始する.
189 */
190 val.it_interval.tv_sec = 0;
191 val.it_interval.tv_usec = 0;
192 val.it_value.tv_sec = itimer_value / 1000000U;
193 val.it_value.tv_usec = itimer_value % 1000000U;
194 setitimer(ITIMER_REAL, &val, NULL);
195}
196
197/*
198 * タイマの初期化処理
199 */
200void
201target_timer_initialize(intptr_t exinf)
202{
203 hrtcnt_current = 0U;
204 hrtcnt_left = 0U;
205 hrtcnt_pending = false;
206#ifdef TOPPERS_SUPPORT_OVRHDR
207 ovrtimer_left = 0U;
208 ovrtimer_pending = false;
209#endif /* TOPPERS_SUPPORT_OVRHDR */
210 start_itimer();
211}
212
213/*
214 * タイマの終了処理
215 */
216void
217target_timer_terminate(intptr_t exinf)
218{
219 /*
220 * インターバルタイマの動作を停止する.
221 */
222 setitimer(ITIMER_REAL, &itimerval_stop, NULL);
223}
224
225/*
226 * 高分解能タイマの現在のカウント値の読出し
227 */
228HRTCNT
229target_hrt_get_current(void)
230{
231 struct itimerval val;
232
233 getitimer(ITIMER_REAL, &val);
234 return(hrtcnt_current + itimer_progress(&val));
235}
236
237/*
238 * 高分解能タイマへの割込みタイミングの設定
239 */
240void
241target_hrt_set_event(HRTCNT hrtcnt)
242{
243 pause_itimer();
244 hrtcnt_left = hrtcnt;
245 start_itimer();
246}
247
248/*
249 * 高分解能タイマ割込みの要求
250 */
251void
252target_hrt_raise_event(void)
253{
254 hrtcnt_pending = true;
255 raise(SIGALRM);
256}
257
258#ifdef TOPPERS_SUPPORT_OVRHDR
259
260/*
261 * オーバランタイマの動作開始
262 */
263void
264target_ovrtimer_start(PRCTIM ovrtim)
265{
266 assert(ovrtimer_left == 0U);
267 if (ovrtim > 0U) {
268 pause_itimer();
269 ovrtimer_left = ovrtim;
270 start_itimer();
271 }
272 else {
273 ovrtimer_pending = true;
274 raise(SIGALRM);
275 }
276}
277
278/*
279 * オーバランタイマの停止
280 */
281PRCTIM
282target_ovrtimer_stop(void)
283{
284 PRCTIM lefttim;
285
286 assert(ovrtimer_left > 0U);
287 pause_itimer();
288 lefttim = ovrtimer_left;
289 ovrtimer_left = 0U;
290 start_itimer();
291 return(lefttim);
292}
293
294/*
295 * オーバランタイマの現在値の読出し
296 */
297PRCTIM
298target_ovrtimer_get_current(void)
299{
300 struct itimerval val;
301
302 assert(ovrtimer_left > 0U);
303 getitimer(ITIMER_REAL, &val);
304 return(ovrtimer_left - itimer_progress(&val));
305}
306
307#endif /* TOPPERS_SUPPORT_OVRHDR */
308
309/*
310 * タイマ割込みハンドラ
311 */
312void
313target_timer_handler(void)
314{
315 lock_cpu();
316 pause_itimer();
317
318#ifdef TOPPERS_SUPPORT_OVRHDR
319 /*
320 * オーバランタイマにより割込みが発生した場合
321 */
322 if (ovrtimer_pending) {
323 ovrtimer_pending = false;
324 start_itimer();
325 unlock_cpu();
326
327 call_ovrhdr(); /* オーバランハンドラの起動処理 */
328
329 if (!sense_lock()) {
330 lock_cpu();
331 }
332 pause_itimer();
333 }
334#endif /* TOPPERS_SUPPORT_OVRHDR */
335
336 /*
337 * 高分解能タイマにより割込みが発生した場合
338 */
339 if (hrtcnt_pending) {
340 hrtcnt_pending = false;
341 start_itimer();
342 unlock_cpu();
343
344 signal_time(); /* 高分解能タイマ割込みの処理 */
345 }
346 else {
347 start_itimer();
348 unlock_cpu();
349 }
350}
Note: See TracBrowser for help on using the repository browser.