File indexing completed on 2025-05-11 08:23:51
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023 #define TIMER_WRAPS_AFTER_1MS 0
0024
0025 #include <rtems.h>
0026 #include <rtems/btimer.h>
0027 #include <rtems/score/cpu.h>
0028 #include <bsp.h>
0029
0030 volatile uint32_t Timer_interrupts;
0031 bool benchmark_timer_find_average_overhead;
0032
0033 #define TIMER_REGS ((altera_avalon_timer_regs*)NIOS2_IO_BASE(TIMER_BASE))
0034
0035 static rtems_isr timerisr(rtems_vector_number vector)
0036 {
0037 TIMER_REGS->status = 0;
0038 Timer_interrupts++;
0039 }
0040
0041 void benchmark_timer_initialize( void )
0042 {
0043
0044
0045 TIMER_REGS->control = ALTERA_AVALON_TIMER_CONTROL_STOP_MSK;
0046
0047 set_vector(timerisr, TIMER_VECTOR, 1);
0048
0049
0050
0051 NIOS2_IENABLE(1 << TIMER_VECTOR);
0052
0053 #if TIMER_WRAPS_AFTER_1MS
0054
0055
0056
0057
0058 TIMER_REGS->period_hi = (TIMER_FREQ/1000)>>16;
0059 TIMER_REGS->period_lo = (TIMER_FREQ/1000)&0xFFFF;
0060 #else
0061
0062
0063
0064 TIMER_REGS->period_hi = 0xFFFF;
0065 TIMER_REGS->period_lo = 0xFFFF;
0066 #endif
0067
0068
0069
0070
0071 TIMER_REGS->control = ALTERA_AVALON_TIMER_CONTROL_ITO_MSK |
0072 ALTERA_AVALON_TIMER_CONTROL_CONT_MSK |
0073 ALTERA_AVALON_TIMER_CONTROL_START_MSK;
0074
0075
0076
0077
0078
0079
0080
0081 Timer_interrupts = 0;
0082 }
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094 #define AVG_OVERHEAD 2
0095
0096
0097 #define LEAST_VALID AVG_OVERHEAD
0098
0099 benchmark_timer_t benchmark_timer_read( void )
0100 {
0101 uint32_t timer_wraps;
0102 uint32_t timer_snap;
0103 uint32_t timer_ticks;
0104 uint32_t total;
0105
0106
0107 TIMER_REGS->control = ALTERA_AVALON_TIMER_CONTROL_STOP_MSK;
0108
0109
0110 TIMER_REGS->snap_lo = 0;
0111
0112 timer_snap = ((TIMER_REGS->snap_hi)<<16) | TIMER_REGS->snap_lo;
0113 timer_wraps = Timer_interrupts;
0114
0115
0116 TIMER_REGS->control = ALTERA_AVALON_TIMER_CONTROL_START_MSK;
0117
0118 #if TIMER_WRAPS_AFTER_1MS
0119 timer_ticks = (TIMER_FREQ / 1000) - 1 - timer_snap;
0120 total = timer_wraps * 1000;
0121 #else
0122 timer_ticks = 0xFFFFFFFF - timer_snap;
0123 total = timer_wraps * 0x80000000 / (TIMER_FREQ / 2000000L);
0124 #endif
0125 total += timer_ticks / (TIMER_FREQ / 1000000L);
0126
0127 if(total < LEAST_VALID) return 0;
0128
0129 if(benchmark_timer_find_average_overhead != TRUE) total-= AVG_OVERHEAD;
0130
0131 return total;
0132 }
0133
0134 void benchmark_timer_disable_subtracting_average_overhead(bool find_flag)
0135 {
0136 benchmark_timer_find_average_overhead = find_flag;
0137 }