[171f834] | 1 | /* |
---|
| 2 | * This file contains the PC386 timer package. |
---|
| 3 | * |
---|
| 4 | * Rosimildo daSilva -ConnectTel, Inc - Fixed infinite loop in the Calibration |
---|
| 5 | * routine. I've seen this problems with faster machines ( pentiums ). Sometimes |
---|
| 6 | * RTEMS just hangs at startup. |
---|
| 7 | * |
---|
| 8 | * Joel 9 May 2010: This is now seen sometimes on qemu. |
---|
| 9 | * |
---|
| 10 | * Modifications by: |
---|
| 11 | * (C) Copyright 1997 - |
---|
| 12 | * NavIST Group - Real-Time Distributed Systems and Industrial Automation |
---|
| 13 | * http://pandora.ist.utl.pt |
---|
| 14 | * Instituto Superior Tecnico * Lisboa * PORTUGAL |
---|
| 15 | * |
---|
| 16 | * This file is provided "AS IS" without warranty of any kind, either |
---|
| 17 | * expressed or implied. |
---|
| 18 | * |
---|
| 19 | * Based upon code by |
---|
| 20 | * COPYRIGHT (c) 1989-1999. |
---|
| 21 | * On-Line Applications Research Corporation (OAR). |
---|
| 22 | * |
---|
| 23 | * The license and distribution terms for this file may be |
---|
[ba64905] | 24 | * found in the file LICENSE in this distribution or at |
---|
[171f834] | 25 | * http://www.rtems.com/license/LICENSE. |
---|
| 26 | * |
---|
| 27 | * $Id$ |
---|
| 28 | */ |
---|
[7150f00f] | 29 | |
---|
| 30 | #include <stdlib.h> |
---|
| 31 | #include <bsp.h> |
---|
[d085040] | 32 | #include <rtems/btimer.h> |
---|
[6daada6] | 33 | #include <bsp/irq.h> |
---|
[959f887a] | 34 | #include <libcpu/cpuModel.h> |
---|
[7150f00f] | 35 | |
---|
[171f834] | 36 | /* |
---|
| 37 | * Constants |
---|
| 38 | */ |
---|
| 39 | #define AVG_OVERHEAD 0 /* 0.1 microseconds to start/stop timer. */ |
---|
| 40 | #define LEAST_VALID 1 /* Don't trust a value lower than this. */ |
---|
| 41 | #define SLOW_DOWN_IO 0x80 /* io which does nothing */ |
---|
[7150f00f] | 42 | |
---|
[171f834] | 43 | #define TWO_MS (uint32_t)(2000) /* TWO_MS = 2000us (sic!) */ |
---|
[eff217e] | 44 | |
---|
[171f834] | 45 | #define MSK_NULL_COUNT 0x40 /* bit counter available for reading */ |
---|
[eff217e] | 46 | |
---|
| 47 | #define CMD_READ_BACK_STATUS 0xE2 /* command read back status */ |
---|
[171f834] | 48 | |
---|
| 49 | /* |
---|
| 50 | * Global Variables |
---|
| 51 | */ |
---|
[6fda59f] | 52 | volatile uint32_t Ttimer_val; |
---|
[171f834] | 53 | bool benchmark_timer_find_average_overhead = true; |
---|
[d57c04e] | 54 | volatile unsigned int fastLoop1ms, slowLoop1ms; |
---|
[959f887a] | 55 | |
---|
| 56 | void (*benchmark_timer_initialize_function)(void) = 0; |
---|
[25c62b0] | 57 | uint32_t (*benchmark_timer_read_function)(void) = 0; |
---|
[959f887a] | 58 | void (*Timer_exit_function)(void) = 0; |
---|
[7150f00f] | 59 | |
---|
[171f834] | 60 | /* timer (int 08h) Interrupt Service Routine (defined in 'timerisr.s') */ |
---|
[2d7d605] | 61 | extern void timerisr(void); |
---|
[171f834] | 62 | |
---|
| 63 | void Timer_exit(void); |
---|
[1131dfe] | 64 | |
---|
| 65 | /* |
---|
[171f834] | 66 | * Pentium optimized timer handling. |
---|
[1131dfe] | 67 | */ |
---|
| 68 | |
---|
[171f834] | 69 | /* |
---|
| 70 | * Timer cleanup routine at RTEMS exit. NOTE: This routine is |
---|
| 71 | * not really necessary, since there will be a reset at exit. |
---|
| 72 | */ |
---|
[67a2288] | 73 | |
---|
[171f834] | 74 | void tsc_timer_exit(void) |
---|
[7150f00f] | 75 | { |
---|
[171f834] | 76 | } |
---|
| 77 | |
---|
| 78 | void tsc_timer_initialize(void) |
---|
[7150f00f] | 79 | { |
---|
[78b05d3] | 80 | static bool First = true; |
---|
[7150f00f] | 81 | |
---|
[171f834] | 82 | if (First) { |
---|
[78b05d3] | 83 | First = false; |
---|
[7150f00f] | 84 | |
---|
| 85 | atexit(Timer_exit); /* Try not to hose the system at exit. */ |
---|
| 86 | } |
---|
| 87 | Ttimer_val = rdtsc(); /* read starting time */ |
---|
[171f834] | 88 | } |
---|
| 89 | |
---|
| 90 | /* |
---|
| 91 | * |
---|
| 92 | */ |
---|
| 93 | uint32_t tsc_read_timer(void) |
---|
[7150f00f] | 94 | { |
---|
[171f834] | 95 | register uint32_t total; |
---|
[7150f00f] | 96 | |
---|
[6fda59f] | 97 | total = (uint32_t)(rdtsc() - Ttimer_val); |
---|
[7150f00f] | 98 | |
---|
[25c62b0] | 99 | if (benchmark_timer_find_average_overhead) |
---|
[7150f00f] | 100 | return total; |
---|
| 101 | |
---|
[171f834] | 102 | if (total < LEAST_VALID) |
---|
| 103 | return 0; /* below timer resolution */ |
---|
| 104 | |
---|
| 105 | return (total - AVG_OVERHEAD); |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | /* |
---|
| 109 | * Non-Pentium timer handling. |
---|
| 110 | */ |
---|
[7150f00f] | 111 | #define US_PER_ISR 250 /* Number of micro-seconds per timer interruption */ |
---|
| 112 | |
---|
[171f834] | 113 | /* |
---|
| 114 | * Timer cleanup routine at RTEMS exit. NOTE: This routine is |
---|
| 115 | * not really necessary, since there will be a reset at exit. |
---|
| 116 | */ |
---|
| 117 | static void timerOff(const rtems_raw_irq_connect_data* used) |
---|
[67a2288] | 118 | { |
---|
[171f834] | 119 | /* |
---|
| 120 | * disable interrrupt at i8259 level |
---|
| 121 | */ |
---|
| 122 | BSP_irq_disable_at_i8259s(used->idtIndex - BSP_IRQ_VECTOR_BASE); |
---|
| 123 | /* reset timer mode to standard (DOS) value */ |
---|
| 124 | outport_byte(TIMER_MODE, TIMER_SEL0|TIMER_16BIT|TIMER_RATEGEN); |
---|
| 125 | outport_byte(TIMER_CNTR0, 0); |
---|
| 126 | outport_byte(TIMER_CNTR0, 0); |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | static void timerOn(const rtems_raw_irq_connect_data* used) |
---|
[67a2288] | 130 | { |
---|
[171f834] | 131 | /* load timer for US_PER_ISR microsecond period */ |
---|
| 132 | outport_byte(TIMER_MODE, TIMER_SEL0|TIMER_16BIT|TIMER_RATEGEN); |
---|
| 133 | outport_byte(TIMER_CNTR0, US_TO_TICK(US_PER_ISR) >> 0 & 0xff); |
---|
| 134 | outport_byte(TIMER_CNTR0, US_TO_TICK(US_PER_ISR) >> 8 & 0xff); |
---|
| 135 | |
---|
| 136 | /* |
---|
| 137 | * enable interrrupt at i8259 level |
---|
| 138 | */ |
---|
| 139 | BSP_irq_enable_at_i8259s(used->idtIndex - BSP_IRQ_VECTOR_BASE); |
---|
[67a2288] | 140 | } |
---|
| 141 | |
---|
[171f834] | 142 | static int timerIsOn(const rtems_raw_irq_connect_data *used) |
---|
[2d7d605] | 143 | { |
---|
[171f834] | 144 | return BSP_irq_enabled_at_i8259s(used->idtIndex - BSP_IRQ_VECTOR_BASE); |
---|
[68f4e5f] | 145 | } |
---|
[2d7d605] | 146 | |
---|
[67a2288] | 147 | static rtems_raw_irq_connect_data timer_raw_irq_data = { |
---|
[0ebbf66] | 148 | BSP_PERIODIC_TIMER + BSP_IRQ_VECTOR_BASE, |
---|
[67a2288] | 149 | timerisr, |
---|
| 150 | timerOn, |
---|
| 151 | timerOff, |
---|
[2d7d605] | 152 | timerIsOn |
---|
[67a2288] | 153 | }; |
---|
[7150f00f] | 154 | |
---|
[171f834] | 155 | /* |
---|
| 156 | * Timer cleanup routine at RTEMS exit. NOTE: This routine is |
---|
| 157 | * not really necessary, since there will be a reset at exit. |
---|
| 158 | */ void |
---|
[1131dfe] | 159 | i386_timer_exit(void) |
---|
[7150f00f] | 160 | { |
---|
[67a2288] | 161 | i386_delete_idt_entry (&timer_raw_irq_data); |
---|
[171f834] | 162 | } |
---|
| 163 | |
---|
| 164 | extern void rtems_irq_prologue_0(void); |
---|
| 165 | void i386_timer_initialize(void) |
---|
[7150f00f] | 166 | { |
---|
[0bbd74de] | 167 | static bool First = true; |
---|
[7150f00f] | 168 | |
---|
[171f834] | 169 | if (First) { |
---|
| 170 | rtems_raw_irq_connect_data raw_irq_data = { |
---|
| 171 | BSP_PERIODIC_TIMER + BSP_IRQ_VECTOR_BASE, |
---|
| 172 | rtems_irq_prologue_0, |
---|
| 173 | NULL, |
---|
| 174 | NULL, |
---|
| 175 | NULL |
---|
| 176 | }; |
---|
| 177 | |
---|
[0bbd74de] | 178 | First = false; |
---|
[171f834] | 179 | i386_delete_idt_entry (&raw_irq_data); |
---|
[7150f00f] | 180 | |
---|
[171f834] | 181 | atexit(Timer_exit); /* Try not to hose the system at exit. */ |
---|
[67a2288] | 182 | if (!i386_set_idt_entry (&timer_raw_irq_data)) { |
---|
[171f834] | 183 | printk("raw handler connection failed\n"); |
---|
[67a2288] | 184 | rtems_fatal_error_occurred(1); |
---|
| 185 | } |
---|
[7150f00f] | 186 | } |
---|
| 187 | /* wait for ISR to be called at least once */ |
---|
| 188 | Ttimer_val = 0; |
---|
| 189 | while (Ttimer_val == 0) |
---|
| 190 | continue; |
---|
| 191 | Ttimer_val = 0; |
---|
[171f834] | 192 | } |
---|
| 193 | |
---|
| 194 | /* |
---|
| 195 | * Read hardware timer value. |
---|
| 196 | */ |
---|
| 197 | uint32_t i386_read_timer(void) |
---|
[7150f00f] | 198 | { |
---|
[6fda59f] | 199 | register uint32_t total, clicks; |
---|
| 200 | register uint8_t lsb, msb; |
---|
[7150f00f] | 201 | |
---|
| 202 | outport_byte(TIMER_MODE, TIMER_SEL0|TIMER_LATCH); |
---|
| 203 | inport_byte(TIMER_CNTR0, lsb); |
---|
| 204 | inport_byte(TIMER_CNTR0, msb); |
---|
| 205 | clicks = (msb << 8) | lsb; |
---|
| 206 | total = (Ttimer_val * US_PER_ISR) + (US_PER_ISR - TICK_TO_US(clicks)); |
---|
| 207 | |
---|
[25c62b0] | 208 | if (benchmark_timer_find_average_overhead) |
---|
[7150f00f] | 209 | return total; |
---|
[171f834] | 210 | |
---|
| 211 | if (total < LEAST_VALID) |
---|
| 212 | return 0; /* below timer resolution */ |
---|
| 213 | |
---|
| 214 | return (total - AVG_OVERHEAD); |
---|
[7150f00f] | 215 | } |
---|
| 216 | |
---|
[1131dfe] | 217 | /* |
---|
| 218 | * General timer functions using either TSC-based implementation |
---|
| 219 | * or interrupt-based implementation |
---|
| 220 | */ |
---|
| 221 | |
---|
[171f834] | 222 | void benchmark_timer_initialize(void) |
---|
[1131dfe] | 223 | { |
---|
[171f834] | 224 | static bool First = true; |
---|
[1131dfe] | 225 | |
---|
[171f834] | 226 | if (First) { |
---|
| 227 | if (x86_has_tsc()) { |
---|
[1131dfe] | 228 | #if defined(DEBUG) |
---|
[171f834] | 229 | printk("TSC: timer initialization\n"); |
---|
[3e21ecf] | 230 | #endif /* DEBUG */ |
---|
[171f834] | 231 | benchmark_timer_initialize_function = &tsc_timer_initialize; |
---|
| 232 | benchmark_timer_read_function = &tsc_read_timer; |
---|
| 233 | Timer_exit_function = &tsc_timer_exit; |
---|
| 234 | } else { |
---|
[1131dfe] | 235 | #if defined(DEBUG) |
---|
[171f834] | 236 | printk("ISR: timer initialization\n"); |
---|
[3e21ecf] | 237 | #endif /* DEBUG */ |
---|
[171f834] | 238 | benchmark_timer_initialize_function = &i386_timer_initialize; |
---|
| 239 | benchmark_timer_read_function = &i386_read_timer; |
---|
| 240 | Timer_exit_function = &i386_timer_exit; |
---|
[1131dfe] | 241 | } |
---|
[171f834] | 242 | First = false; |
---|
| 243 | } |
---|
| 244 | (*benchmark_timer_initialize_function)(); |
---|
[1131dfe] | 245 | } |
---|
| 246 | |
---|
[171f834] | 247 | uint32_t benchmark_timer_read(void) |
---|
[1131dfe] | 248 | { |
---|
[171f834] | 249 | return (*benchmark_timer_read_function)(); |
---|
[1131dfe] | 250 | } |
---|
| 251 | |
---|
[171f834] | 252 | void Timer_exit(void) |
---|
[1131dfe] | 253 | { |
---|
[59211c6] | 254 | if ( Timer_exit_function ) |
---|
| 255 | return (*Timer_exit_function)(); |
---|
[1131dfe] | 256 | } |
---|
[7150f00f] | 257 | |
---|
[171f834] | 258 | /* |
---|
| 259 | * Set internal benchmark_timer_find_average_overhead flag value. |
---|
| 260 | */ |
---|
| 261 | void benchmark_timer_disable_subtracting_average_overhead(bool find_flag) |
---|
[7150f00f] | 262 | { |
---|
[25c62b0] | 263 | benchmark_timer_find_average_overhead = find_flag; |
---|
[171f834] | 264 | } |
---|
[eff217e] | 265 | |
---|
[d57c04e] | 266 | static unsigned short lastLoadedValue; |
---|
[23291e99] | 267 | |
---|
[171f834] | 268 | /* |
---|
| 269 | * Loads timer 0 with value passed as arguemnt. |
---|
| 270 | * |
---|
| 271 | * Returns: Nothing. Loaded value must be a number of clock bits... |
---|
| 272 | */ |
---|
[d57c04e] | 273 | void loadTimerValue( unsigned short loadedValue ) |
---|
[23291e99] | 274 | { |
---|
[d57c04e] | 275 | lastLoadedValue = loadedValue; |
---|
| 276 | outport_byte(TIMER_MODE, TIMER_SEL0|TIMER_16BIT|TIMER_SQWAVE); |
---|
| 277 | outport_byte(TIMER_CNTR0, loadedValue & 0xff); |
---|
| 278 | outport_byte(TIMER_CNTR0, (loadedValue >> 8) & 0xff); |
---|
[23291e99] | 279 | } |
---|
| 280 | |
---|
[171f834] | 281 | /* |
---|
| 282 | * Reads the current value of the timer, and converts the |
---|
| 283 | * number of ticks to micro-seconds. |
---|
| 284 | * |
---|
| 285 | * Returns: number of clock bits elapsed since last load. |
---|
| 286 | */ |
---|
[9212cb3] | 287 | unsigned int readTimer0(void) |
---|
[23291e99] | 288 | { |
---|
| 289 | unsigned short lsb, msb; |
---|
[d57c04e] | 290 | unsigned char status; |
---|
[171f834] | 291 | unsigned int count; |
---|
[d57c04e] | 292 | |
---|
[171f834] | 293 | outport_byte( |
---|
| 294 | TIMER_MODE, |
---|
| 295 | (TIMER_RD_BACK | (RB_COUNT_0 & ~(RB_NOT_STATUS | RB_NOT_COUNT))) |
---|
| 296 | ); |
---|
[d57c04e] | 297 | inport_byte(TIMER_CNTR0, status); |
---|
[23291e99] | 298 | inport_byte(TIMER_CNTR0, lsb); |
---|
| 299 | inport_byte(TIMER_CNTR0, msb); |
---|
[d57c04e] | 300 | count = ( msb << 8 ) | lsb ; |
---|
| 301 | if (status & RB_OUTPUT ) |
---|
[171f834] | 302 | count += lastLoadedValue; |
---|
[6128a4a] | 303 | |
---|
[d57c04e] | 304 | return (2*lastLoadedValue - count); |
---|
[23291e99] | 305 | } |
---|
| 306 | |
---|
[9212cb3] | 307 | void Timer0Reset(void) |
---|
[d57c04e] | 308 | { |
---|
| 309 | loadTimerValue(0xffff); |
---|
| 310 | readTimer0(); |
---|
| 311 | } |
---|
[23291e99] | 312 | |
---|
[d57c04e] | 313 | void fastLoop (unsigned int loopCount) |
---|
[23291e99] | 314 | { |
---|
[d57c04e] | 315 | unsigned int i; |
---|
| 316 | for( i=0; i < loopCount; i++ )outport_byte( SLOW_DOWN_IO, 0 ); |
---|
[23291e99] | 317 | } |
---|
| 318 | |
---|
[d57c04e] | 319 | void slowLoop (unsigned int loopCount) |
---|
| 320 | { |
---|
| 321 | unsigned int j; |
---|
| 322 | for (j=0; j <100 ; j++) { |
---|
| 323 | fastLoop (loopCount); |
---|
| 324 | } |
---|
| 325 | } |
---|
[23291e99] | 326 | |
---|
[d57c04e] | 327 | /* |
---|
| 328 | * #define DEBUG_CALIBRATE |
---|
| 329 | */ |
---|
[eff217e] | 330 | void |
---|
[23291e99] | 331 | Calibrate_loop_1ms(void) |
---|
| 332 | { |
---|
[d57c04e] | 333 | unsigned int offset, offsetTmp, emptyCall, emptyCallTmp, res, i, j; |
---|
| 334 | unsigned int targetClockBits, currentClockBits; |
---|
| 335 | unsigned int slowLoopGranularity, fastLoopGranularity; |
---|
[eff217e] | 336 | rtems_interrupt_level level; |
---|
[13e4ab64] | 337 | int retries = 0; |
---|
[6128a4a] | 338 | |
---|
[13e4ab64] | 339 | rtems_interrupt_disable(level); |
---|
| 340 | |
---|
| 341 | retry: |
---|
| 342 | if ( ++retries >= 5 ) { |
---|
| 343 | printk( "Calibrate_loop_1ms: too many attempts. giving up!!\n" ); |
---|
| 344 | while (1); |
---|
| 345 | } |
---|
[d57c04e] | 346 | #ifdef DEBUG_CALIBRATE |
---|
[171f834] | 347 | printk("Calibrate_loop_1ms is starting, please wait (but not too long.)\n"); |
---|
[6128a4a] | 348 | #endif |
---|
[d57c04e] | 349 | targetClockBits = US_TO_TICK(1000); |
---|
| 350 | /* |
---|
| 351 | * Fill up the cache to get a correct offset |
---|
| 352 | */ |
---|
| 353 | Timer0Reset(); |
---|
| 354 | readTimer0(); |
---|
[6128a4a] | 355 | /* |
---|
[d57c04e] | 356 | * Compute the minimal offset to apply due to read counter register. |
---|
| 357 | */ |
---|
| 358 | offset = 0xffffffff; |
---|
| 359 | for (i=0; i <1000; i++) { |
---|
| 360 | Timer0Reset(); |
---|
| 361 | offsetTmp = readTimer0(); |
---|
| 362 | offset += offsetTmp; |
---|
| 363 | } |
---|
| 364 | offset = offset / 1000; /* compute average */ |
---|
| 365 | /* |
---|
| 366 | * calibrate empty call |
---|
| 367 | */ |
---|
| 368 | fastLoop (0); |
---|
| 369 | emptyCall = 0; |
---|
| 370 | j = 0; |
---|
| 371 | for (i=0; i <10; i++) { |
---|
| 372 | Timer0Reset(); |
---|
| 373 | fastLoop (0); |
---|
| 374 | res = readTimer0(); |
---|
| 375 | /* res may be inferior to offset on fast |
---|
| 376 | * machine because we took an average for offset |
---|
| 377 | */ |
---|
| 378 | if (res > offset) { |
---|
| 379 | ++j; |
---|
| 380 | emptyCallTmp = res - offset; |
---|
| 381 | emptyCall += emptyCallTmp; |
---|
| 382 | } |
---|
| 383 | } |
---|
| 384 | if (j == 0) emptyCall = 0; |
---|
| 385 | else emptyCall = emptyCall / j; /* compute average */ |
---|
| 386 | /* |
---|
| 387 | * calibrate fast loop |
---|
| 388 | */ |
---|
| 389 | Timer0Reset(); |
---|
| 390 | fastLoop (10000); |
---|
| 391 | res = readTimer0() - offset; |
---|
| 392 | if (res < emptyCall) { |
---|
[13e4ab64] | 393 | printk( |
---|
| 394 | "Problem #1 in offset computation in Calibrate_loop_1ms " |
---|
| 395 | " in file libbsp/i386/pc386/timer/timer.c\n" |
---|
| 396 | ); |
---|
| 397 | goto retry; |
---|
[d57c04e] | 398 | } |
---|
| 399 | fastLoopGranularity = (res - emptyCall) / 10000; |
---|
| 400 | /* |
---|
| 401 | * calibrate slow loop |
---|
| 402 | */ |
---|
| 403 | Timer0Reset(); |
---|
| 404 | slowLoop(10); |
---|
| 405 | res = readTimer0(); |
---|
| 406 | if (res < offset + emptyCall) { |
---|
[13e4ab64] | 407 | printk( |
---|
| 408 | "Problem #2 in offset computation in Calibrate_loop_1ms " |
---|
| 409 | " in file libbsp/i386/pc386/timer/timer.c\n" |
---|
| 410 | ); |
---|
| 411 | goto retry; |
---|
[d57c04e] | 412 | } |
---|
| 413 | slowLoopGranularity = (res - offset - emptyCall)/ 10; |
---|
[6128a4a] | 414 | |
---|
[d57c04e] | 415 | if (slowLoopGranularity == 0) { |
---|
[13e4ab64] | 416 | printk( |
---|
| 417 | "Problem #3 in offset computation in Calibrate_loop_1ms " |
---|
| 418 | " in file libbsp/i386/pc386/timer/timer.c\n" |
---|
| 419 | ); |
---|
| 420 | goto retry; |
---|
[d57c04e] | 421 | } |
---|
[23291e99] | 422 | |
---|
[d57c04e] | 423 | targetClockBits += offset; |
---|
[6128a4a] | 424 | #ifdef DEBUG_CALIBRATE |
---|
[d57c04e] | 425 | printk("offset = %u, emptyCall = %u, targetClockBits = %u\n", |
---|
[171f834] | 426 | offset, emptyCall, targetClockBits); |
---|
[d57c04e] | 427 | printk("slowLoopGranularity = %u fastLoopGranularity = %u\n", |
---|
[171f834] | 428 | slowLoopGranularity, fastLoopGranularity); |
---|
[6128a4a] | 429 | #endif |
---|
[d57c04e] | 430 | slowLoop1ms = (targetClockBits - emptyCall) / slowLoopGranularity; |
---|
| 431 | if (slowLoop1ms != 0) { |
---|
| 432 | fastLoop1ms = targetClockBits % slowLoopGranularity; |
---|
| 433 | if (fastLoop1ms > emptyCall) fastLoop1ms -= emptyCall; |
---|
[6128a4a] | 434 | } |
---|
[d57c04e] | 435 | else |
---|
| 436 | fastLoop1ms = targetClockBits - emptyCall / fastLoopGranularity; |
---|
[23291e99] | 437 | |
---|
[d57c04e] | 438 | if (slowLoop1ms != 0) { |
---|
| 439 | /* |
---|
| 440 | * calibrate slow loop |
---|
| 441 | */ |
---|
[6128a4a] | 442 | |
---|
[d57c04e] | 443 | while(1) |
---|
| 444 | { |
---|
[171f834] | 445 | int previousSign = 0; /* 0 = unset, 1 = incrementing, 2 = decrementing */ |
---|
| 446 | Timer0Reset(); |
---|
| 447 | slowLoop(slowLoop1ms); |
---|
| 448 | currentClockBits = readTimer0(); |
---|
| 449 | if (currentClockBits > targetClockBits) { |
---|
| 450 | if ((currentClockBits - targetClockBits) < slowLoopGranularity) { |
---|
| 451 | /* decrement loop counter anyway to be sure slowLoop(slowLoop1ms) < targetClockBits */ |
---|
| 452 | --slowLoop1ms; |
---|
| 453 | break; |
---|
| 454 | } |
---|
| 455 | else { |
---|
| 456 | --slowLoop1ms; |
---|
| 457 | if (slowLoop1ms == 0) break; |
---|
| 458 | if (previousSign == 0) previousSign = 2; |
---|
| 459 | if (previousSign == 1) break; |
---|
| 460 | } |
---|
| 461 | } |
---|
| 462 | else { |
---|
| 463 | if ((targetClockBits - currentClockBits) < slowLoopGranularity) { |
---|
| 464 | break; |
---|
| 465 | } |
---|
| 466 | else { |
---|
| 467 | ++slowLoop1ms; |
---|
| 468 | if (previousSign == 0) previousSign = 1; |
---|
| 469 | if (previousSign == 2) break; |
---|
| 470 | } |
---|
| 471 | } |
---|
[d57c04e] | 472 | } |
---|
| 473 | } |
---|
[eff217e] | 474 | /* |
---|
[d57c04e] | 475 | * calibrate fast loop |
---|
[eff217e] | 476 | */ |
---|
[6128a4a] | 477 | |
---|
[d57c04e] | 478 | if (fastLoopGranularity != 0 ) { |
---|
| 479 | while(1) { |
---|
| 480 | int previousSign = 0; /* 0 = unset, 1 = incrementing, 2 = decrementing */ |
---|
| 481 | Timer0Reset(); |
---|
| 482 | if (slowLoop1ms != 0) slowLoop(slowLoop1ms); |
---|
| 483 | fastLoop(fastLoop1ms); |
---|
| 484 | currentClockBits = readTimer0(); |
---|
| 485 | if (currentClockBits > targetClockBits) { |
---|
[171f834] | 486 | if ((currentClockBits - targetClockBits) < fastLoopGranularity) |
---|
| 487 | break; |
---|
| 488 | else { |
---|
| 489 | --fastLoop1ms; |
---|
| 490 | if (previousSign == 0) previousSign = 2; |
---|
| 491 | if (previousSign == 1) break; |
---|
| 492 | } |
---|
[d57c04e] | 493 | } |
---|
| 494 | else { |
---|
[171f834] | 495 | if ((targetClockBits - currentClockBits) < fastLoopGranularity) |
---|
| 496 | break; |
---|
| 497 | else { |
---|
| 498 | ++fastLoop1ms; |
---|
| 499 | if (previousSign == 0) previousSign = 1; |
---|
| 500 | if (previousSign == 2) break; |
---|
| 501 | } |
---|
[d57c04e] | 502 | } |
---|
| 503 | } |
---|
[eff217e] | 504 | } |
---|
[6128a4a] | 505 | #ifdef DEBUG_CALIBRATE |
---|
[d57c04e] | 506 | printk("slowLoop1ms = %u, fastLoop1ms = %u\n", slowLoop1ms, fastLoop1ms); |
---|
[6128a4a] | 507 | #endif |
---|
[eff217e] | 508 | rtems_interrupt_enable(level); |
---|
[6128a4a] | 509 | |
---|
[eff217e] | 510 | } |
---|
| 511 | |
---|
[171f834] | 512 | /* |
---|
| 513 | * loop which waits at least timeToWait ms |
---|
| 514 | */ |
---|
| 515 | void Wait_X_ms( unsigned int timeToWait) |
---|
| 516 | { |
---|
[d57c04e] | 517 | unsigned int j; |
---|
| 518 | |
---|
| 519 | for (j=0; j<timeToWait ; j++) { |
---|
| 520 | if (slowLoop1ms != 0) slowLoop(slowLoop1ms); |
---|
| 521 | fastLoop(fastLoop1ms); |
---|
| 522 | } |
---|
[eff217e] | 523 | } |
---|