[af85485] | 1 | /* |
---|
| 2 | * Cogent CSB337 Timer driver |
---|
| 3 | * |
---|
| 4 | * This uses timer 0 for timing measurments. |
---|
| 5 | * |
---|
| 6 | * Copyright (c) 2004 by Jay Monkman <jtm@lopingdog.com> |
---|
| 7 | * |
---|
| 8 | * The license and distribution terms for this file may be |
---|
| 9 | * found in the file LICENSE in this distribution or at |
---|
| 10 | * |
---|
| 11 | * http://www.OARcorp.com/rtems/license.html. |
---|
| 12 | * |
---|
| 13 | * Notes: |
---|
| 14 | * This file manages the benchmark timer used by the RTEMS Timing Test |
---|
| 15 | * Suite. Each measured time period is demarcated by calls to |
---|
| 16 | * Timer_initialize() and Read_timer(). Read_timer() usually returns |
---|
| 17 | * the number of microseconds since Timer_initialize() exitted. |
---|
| 18 | * |
---|
| 19 | * It is important that the timer start/stop overhead be determined |
---|
| 20 | * when porting or modifying this code. |
---|
| 21 | * |
---|
| 22 | * $Id$ |
---|
| 23 | */ |
---|
| 24 | |
---|
| 25 | #include <rtems.h> |
---|
| 26 | #include <bsp.h> |
---|
| 27 | #include <at91rm9200.h> |
---|
| 28 | #include <at91rm9200_pmc.h> |
---|
| 29 | |
---|
[15ebe58b] | 30 | uint16_t tstart; |
---|
[af85485] | 31 | rtems_boolean Timer_driver_Find_average_overhead; |
---|
[15ebe58b] | 32 | uint32_t tick_time; |
---|
[af85485] | 33 | /* |
---|
| 34 | * Set up TC0 - |
---|
| 35 | * timer_clock2 (MCK/8) |
---|
| 36 | * capture mode - this shouldn't matter |
---|
| 37 | */ |
---|
| 38 | void Timer_initialize( void ) |
---|
| 39 | { |
---|
[15ebe58b] | 40 | uint32_t tmr_freq; |
---|
[af85485] | 41 | |
---|
| 42 | /* since we are using timer_clock2, divide mck by 8 */ |
---|
| 43 | tmr_freq = at91rm9200_get_mck() / 8; |
---|
| 44 | |
---|
| 45 | TC_TC0_REG(TC_CMR) = TC_CMR_TCCLKS(1); /* timer_clock2 */ |
---|
| 46 | TC_TC0_REG(TC_CCR) = (TC_CCR_CLKEN /* enable the counter */ |
---|
| 47 | | TC_CCR_SWTRG); /* start it up */ |
---|
| 48 | |
---|
| 49 | /* tick time in nanoseconds */ |
---|
| 50 | tick_time = 1000000000/tmr_freq; |
---|
| 51 | |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | /* |
---|
| 55 | * The following controls the behavior of Read_timer(). |
---|
| 56 | * |
---|
| 57 | * AVG_OVEREHAD is the overhead for starting and stopping the timer. It |
---|
| 58 | * is usually deducted from the number returned. |
---|
| 59 | * |
---|
| 60 | * LEAST_VALID is the lowest number this routine should trust. Numbers |
---|
| 61 | * below this are "noise" and zero is returned. |
---|
| 62 | */ |
---|
| 63 | |
---|
| 64 | #define AVG_OVERHEAD 0 /* It typically takes X.X microseconds */ |
---|
| 65 | /* (Y countdowns) to start/stop the timer. */ |
---|
| 66 | /* This value is in microseconds. */ |
---|
| 67 | #define LEAST_VALID 1 /* Don't trust a clicks value lower than this */ |
---|
| 68 | |
---|
| 69 | int Read_timer( void ) |
---|
| 70 | { |
---|
[15ebe58b] | 71 | uint16_t t; |
---|
| 72 | uint32_t total; |
---|
[af85485] | 73 | t = TC_TC0_REG(TC_CV); |
---|
| 74 | |
---|
| 75 | /* |
---|
| 76 | * Total is calculated by taking into account the number of timer overflow |
---|
| 77 | * interrupts since the timer was initialized and clicks since the last |
---|
| 78 | * interrupts. |
---|
| 79 | */ |
---|
| 80 | |
---|
| 81 | total = t * tick_time; |
---|
| 82 | |
---|
| 83 | if ( Timer_driver_Find_average_overhead == 1 ) |
---|
| 84 | return total; /* in nanosecond units */ |
---|
| 85 | else { |
---|
| 86 | if ( total < LEAST_VALID ) |
---|
| 87 | return 0; /* below timer resolution */ |
---|
| 88 | /* |
---|
| 89 | * Somehow convert total into microseconds |
---|
| 90 | */ |
---|
| 91 | return (total - AVG_OVERHEAD); |
---|
| 92 | } |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | /* |
---|
| 96 | * Empty function call used in loops to measure basic cost of looping |
---|
| 97 | * in Timing Test Suite. |
---|
| 98 | */ |
---|
| 99 | |
---|
| 100 | rtems_status_code Empty_function( void ) |
---|
| 101 | { |
---|
| 102 | return RTEMS_SUCCESSFUL; |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | void Set_find_average_overhead( |
---|
| 106 | rtems_boolean find_flag |
---|
| 107 | ) |
---|
| 108 | { |
---|
| 109 | Timer_driver_Find_average_overhead = find_flag; |
---|
| 110 | } |
---|
| 111 | |
---|