[302] | 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 | */
|
---|
| 81 | static HRTCNT hrtcnt_current; /* é«å解è½ã¿ã¤ãã®ç¾æç¹ã®å¤ */
|
---|
| 82 | static HRTCNT hrtcnt_left; /* é«å解è½ã¿ã¤ãå²è¾¼ã¿ã®æ®ãã«ã¦ã³ã */
|
---|
| 83 | static bool_t hrtcnt_pending; /* é«å解è½ã¿ã¤ãå²è¾¼ã¿ãè¦æ±ä¸ */
|
---|
| 84 |
|
---|
| 85 | /*
|
---|
| 86 | * ãªã¼ãã©ã³ã¿ã¤ãã®ç¶æ
|
---|
| 87 |
|
---|
| 88 | */
|
---|
| 89 | #ifdef TOPPERS_SUPPORT_OVRHDR
|
---|
| 90 | static PRCTIM ovrtimer_left; /* ãªã¼ãã©ã³ã¿ã¤ãå²è¾¼ã¿ã®æ®ãæé */
|
---|
| 91 | static bool_t ovrtimer_pending; /* ãªã¼ãã©ã³ã¿ã¤ãå²è¾¼ã¿ãè¦æ±ä¸ */
|
---|
| 92 | #endif /* TOPPERS_SUPPORT_OVRHDR */
|
---|
| 93 |
|
---|
| 94 | /*
|
---|
| 95 | * ã¤ã³ã¿ã¼ãã«ã¿ã¤ãã®è¨å®ç¶æ
|
---|
| 96 |
|
---|
| 97 | */
|
---|
| 98 | static ulong_t itimer_value; /* æå¾ã«ã¤ã³ã¿ã¼ãã«ã¿ã¤ãã«è¨å®ããå¤ */
|
---|
| 99 |
|
---|
| 100 | /*
|
---|
| 101 | * ã¤ã³ã¿ã¼ãã«ã¿ã¤ããåæ¢ãããããã®å¤æ°
|
---|
| 102 | */
|
---|
| 103 | static const struct itimerval itimerval_stop = {{ 0, 0 }, { 0, 0 }};
|
---|
| 104 |
|
---|
| 105 | /*
|
---|
| 106 | * ã¤ã³ã¿ã¼ãã«ã¿ã¤ãã®çµéæéã®ç®åº
|
---|
| 107 | */
|
---|
| 108 | Inline ulong_t
|
---|
| 109 | itimer_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 | */
|
---|
| 118 | static void
|
---|
| 119 | pause_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 | */
|
---|
| 172 | static void
|
---|
| 173 | start_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 | */
|
---|
| 200 | void
|
---|
| 201 | target_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 | */
|
---|
| 216 | void
|
---|
| 217 | target_timer_terminate(intptr_t exinf)
|
---|
| 218 | {
|
---|
| 219 | /*
|
---|
| 220 | * ã¤ã³ã¿ã¼ãã«ã¿ã¤ãã®åä½ãåæ¢ããï¼
|
---|
| 221 | */
|
---|
| 222 | setitimer(ITIMER_REAL, &itimerval_stop, NULL);
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 | /*
|
---|
| 226 | * é«å解è½ã¿ã¤ãã®ç¾å¨ã®ã«ã¦ã³ãå¤ã®èªåºã
|
---|
| 227 | */
|
---|
| 228 | HRTCNT
|
---|
| 229 | target_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 | */
|
---|
| 240 | void
|
---|
| 241 | target_hrt_set_event(HRTCNT hrtcnt)
|
---|
| 242 | {
|
---|
| 243 | pause_itimer();
|
---|
| 244 | hrtcnt_left = hrtcnt;
|
---|
| 245 | start_itimer();
|
---|
| 246 | }
|
---|
| 247 |
|
---|
| 248 | /*
|
---|
| 249 | * é«å解è½ã¿ã¤ãå²è¾¼ã¿ã®è¦æ±
|
---|
| 250 | */
|
---|
| 251 | void
|
---|
| 252 | target_hrt_raise_event(void)
|
---|
| 253 | {
|
---|
| 254 | hrtcnt_pending = true;
|
---|
| 255 | raise(SIGALRM);
|
---|
| 256 | }
|
---|
| 257 |
|
---|
| 258 | #ifdef TOPPERS_SUPPORT_OVRHDR
|
---|
| 259 |
|
---|
| 260 | /*
|
---|
| 261 | * ãªã¼ãã©ã³ã¿ã¤ãã®åä½éå§
|
---|
| 262 | */
|
---|
| 263 | void
|
---|
| 264 | target_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 | */
|
---|
| 281 | PRCTIM
|
---|
| 282 | target_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 | */
|
---|
| 297 | PRCTIM
|
---|
| 298 | target_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 | */
|
---|
| 312 | void
|
---|
| 313 | target_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 | }
|
---|