Project

General

Profile

Download (3.96 KB) Statistics
| Branch: | Tag: | Revision:
1 9cde1dd0 henryk
/* T/C driver for performance measurements
2
 *  
3
 * Copyright 2008 Henryk Plötz <henryk@ploetzli.ch>
4
 *
5
 *  This program is free software; you can redistribute it and/or modify
6
 *  it under the terms of the GNU General Public License as published by 
7
 *  the Free Software Foundation; either version 2 of the License, or
8
 *  (at your option) any later version.
9
 *
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU General Public License
16
 *  along with this program; if not, write to the Free Software
17
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
 *
19
 */
20
21
#include <FreeRTOS.h>
22
#include <AT91SAM7.h>
23
#include <openpicc.h>
24
25
#include <string.h>
26
27
#include "performance.h"
28
#include "cmd.h"
29
30
static AT91PS_TC tc_perf = AT91C_BASE_TC1;
31
static u_int32_t overruns = 0;
32
33
#define NUMBER_OF_CHECKPOINTS 20
34
static struct performance_checkpoint checkpoints[NUMBER_OF_CHECKPOINTS];
35
static int current_checkpoint;
36 e2e37bea henryk
static int running=0;
37 9cde1dd0 henryk
38
static void __ramfunc tc_perf_irq(void) __attribute__ ((naked));
39
static void __ramfunc tc_perf_irq(void)
40
{
41
	portSAVE_CONTEXT();
42
	u_int32_t sr = tc_perf->TC_SR;
43
	if(sr & AT91C_TC_COVFS) overruns++;
44
	AT91F_AIC_AcknowledgeIt();
45
	portRESTORE_CONTEXT();
46
}
47
48
void performance_init(void)
49
{
50
	AT91F_PMC_EnablePeriphClock(AT91C_BASE_PMC, ((u_int32_t) 1 << AT91C_ID_TC1));
51
	/* clock is MCK/2, TIOA/B unconfigured, reset only on SWTRG, ignore Compare C */
52
	tc_perf->TC_CMR = AT91C_TC_CLKS_TIMER_DIV1_CLOCK;
53
	
54
	tc_perf->TC_CCR = AT91C_TC_CLKDIS;
55
	
56
	AT91F_AIC_ConfigureIt(AT91C_ID_TC1, AT91C_AIC_PRIOR_HIGHEST-1,
57
			      AT91C_AIC_SRCTYPE_INT_HIGH_LEVEL, (THandler)&tc_perf_irq);
58
	tc_perf->TC_IER = AT91C_TC_COVFS;
59
	AT91F_AIC_ClearIt(AT91C_ID_TC1);
60
	AT91F_AIC_EnableIt(AT91C_ID_TC1);
61
	
62
	memset(checkpoints, 0, sizeof(checkpoints));
63
}
64
65
inline void performance_start(void)
66
{
67
	memset(checkpoints, 0, sizeof(checkpoints));
68
	current_checkpoint = 0;
69
	overruns = 0;
70 e2e37bea henryk
	running = 1;
71 9cde1dd0 henryk
	tc_perf->TC_CCR = AT91C_TC_SWTRG | AT91C_TC_CLKEN;
72
}
73
74
inline perf_time_t performance_get(void)
75
{
76
	u_int32_t cv = tc_perf->TC_CV;
77
	perf_time_t result = {
78
			.high = overruns,
79
			.low = cv,
80
	};
81
	return result;
82
}
83
84
inline perf_time_t performance_stop(void)
85
{
86
	perf_time_t result = performance_get();
87 e2e37bea henryk
	running = 0;
88 9cde1dd0 henryk
	tc_perf->TC_CCR = AT91C_TC_CLKDIS;
89
	return result;
90
}
91
92
void performance_print(perf_time_t time)
93
{
94
	DumpUIntToUSB(time.high);
95
	DumpStringToUSB(":");
96
	DumpUIntToUSB(time.low);
97
}
98
99
perf_time_t performance_diff(perf_time_t a, perf_time_t b)
100
{
101
	// assert that a <= b
102
	if( (a.high > b.high) || (a.high == b.high && a.low > b.low) ) return performance_diff(b, a);
103
	perf_time_t result = { b.high - a.high, 0 };
104
	if(b.high == a.high) result.low = b.low - a.low;
105
	
106
	return result;
107
}
108
109
void performance_set_checkpoint(const char * const description)
110
{
111 e2e37bea henryk
	if(!running) return;
112 9cde1dd0 henryk
	if(current_checkpoint < NUMBER_OF_CHECKPOINTS) {
113
		perf_time_t time = performance_get();
114
		checkpoints[current_checkpoint++] = (struct performance_checkpoint){
115
				.time = time,
116
				.description = description,
117
		};
118
	}
119
}
120
121
void performance_stop_report(void)
122
{
123 e2e37bea henryk
	if(!running) return;
124 9cde1dd0 henryk
	perf_time_t _now = performance_stop();
125
	struct performance_checkpoint now = {
126
			.time = _now,
127
			.description = "end of data collection",
128
	};
129
	perf_time_t last = {0,0};
130
	int i;
131
	DumpStringToUSB("Performance report: \n\r");
132
	for(i = 0; i <= current_checkpoint; i++) {
133
		struct performance_checkpoint current = (i<current_checkpoint ? checkpoints[i] : now);
134
		
135
		DumpStringToUSB("\t");
136
		performance_print(current.time);
137
		DumpStringToUSB("   \t");
138
		
139
		if(last.high == 0 && last.low == 0) 
140
			DumpStringToUSB("       ");
141
		else
142
			performance_print(performance_diff(last, current.time));
143
		last = current.time;
144
		DumpStringToUSB("   \t");
145
		
146
		if(current.description)
147
			DumpStringToUSB(current.description);
148
		DumpStringToUSB("\n\r");
149
	}
150
}
Add picture from clipboard (Maximum size: 48.8 MB)