Project

General

Profile

Download (14.3 KB) Statistics
| Branch: | Tag: | Revision:
1 32985a29 laforge
/* AT91SAM7 debug function implementation for OpenPCD
2
 * (C) 2006 by Harald Welte <hwelte@hmw-consulting.de>
3
 *
4
 * I based this on existing BSD-style licensed code, please see below.
5
 *
6
 *  This program is free software; you can redistribute it and/or modify
7
 *  it under the terms of the GNU General Public License as published by 
8
 *  the Free Software Foundation; either version 2 of the License, or
9
 *  (at your option) any later version.
10
 *
11
 *  This program is distributed in the hope that it will be useful,
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 *  GNU General Public License for more details.
15
 *
16
 *  You should have received a copy of the GNU General Public License
17
 *  along with this program; if not, write to the Free Software
18
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
 *
20
 */
21
22 caf50003 (no author)
/*----------------------------------------------------------------------------
23
 *         ATMEL Microcontroller Software Support  -  ROUSSET  -
24
 *----------------------------------------------------------------------------
25
 * The software is delivered "AS IS" without warranty or condition of any
26
 * kind, either express, implied or statutory. This includes without
27
 * limitation any warranty or condition with respect to merchantability or
28
 * fitness for any particular purpose, or against the infringements of
29
 * intellectual property rights of others.
30
 *----------------------------------------------------------------------------
31
 * File Name           : Debug.c
32
 * Object              : Debug menu
33
 * Creation            : JPP   14/Sep/2004
34
 * 1.1 29/Aug/05 JPP   : Update AIC definion
35
 *----------------------------------------------------------------------------*/
36 f57b548d (no author)
37
// Include Standard files
38 9164ed93 (no author)
#include <board.h>
39 520784c7 (no author)
#include <os/dbgu.h>
40
#include "../openpcd.h"
41
#include <os/led.h>
42
#include <os/main.h>
43 bfff30bf laforge
#include <os/system_irq.h>
44 28eb4a57 laforge
#include <os/pcd_enumerate.h>
45 28a12f7d (no author)
#include <asm/system.h>
46 833d0d5e laforge
#include <compile.h>
47 9d0d7022 (no author)
48 ca4e8055 Min Xu
/* In case we find that while (); is non interruptible, we may need to
49
 * uncommend this line: */
50
// #define ALLOW_INTERRUPT_LOOP asm("nop;nop;nop;nop;nop;nop;nop;nop;")
51
52
#define ALLOW_INTERRUPT_LOOP
53
54
/* DEBUG_BUFFER_SIZE MUST BE A POWER OF 2 */
55
#define DEBUG_BUFFER_SIZE     (1 << 10)
56
#define DEBUG_BUFFER_MASK     (DEBUG_BUFFER_SIZE - 1)
57 28eb4a57 laforge
58 f57b548d (no author)
/*---------------------------- Global Variable ------------------------------*/
59
60
//*----------------------------------------------------------------------------
61
//* Function Name       : Send_reset
62
//* Object              : Acknoledeg AIC and send reset
63
//*----------------------------------------------------------------------------
64
static void Send_reset(void)
65
{
66
	void (*pfct) (void) = (void (*)(void))0x00000000;
67
68
	// Acknoledge the interrupt
69
	// Mark the End of Interrupt on the AIC
70
	AT91C_BASE_AIC->AIC_EOICR = 0;
71 ca4e8055 Min Xu
	while (!(AT91C_BASE_DBGU->DBGU_CSR & AT91C_US_TXEMPTY)) ;
72 f57b548d (no author)
	// Jump in reset
73
	pfct();
74
}
75
76
//*----------------------------------------------------------------------------
77
//* Function Name       : DBGU_irq_handler
78 bfff30bf laforge
//* Object              : C handler interrupt function called by the sysirq
79
//*                       demultiplexer
80 f57b548d (no author)
//*----------------------------------------------------------------------------
81 373c172a Harald Welte
static void DBGU_irq_handler(uint32_t sr)
82 f57b548d (no author)
{
83 9d0d7022 (no author)
	static char value;
84 f57b548d (no author)
85
	AT91F_DBGU_Get(&value);
86
	switch (value) {
87
	case '0':		//* info
88 84f258ce (no author)
		AT91F_DBGU_Frame("Clear Pull up\n\r");
89 f57b548d (no author)
		// Set
90 d1dd3611 laforge
		udp_pullup_on();
91 f57b548d (no author)
		break;
92
	case '1':		//* info
93 d1dd3611 laforge
		udp_pullup_off();
94 ca4e8055 Min Xu
		AT91F_DBGU_Frame("Set Pull up\n\r");
95 f57b548d (no author)
		// Reset Application
96
		Send_reset();
97 41333333 (no author)
		break;
98
	case '2':
99 ca4e8055 Min Xu
		AT91F_DBGU_Frame("Toggling LED 1\n\r");
100 41333333 (no author)
		led_toggle(1);
101
		break;
102
	case '3':
103 ca4e8055 Min Xu
		AT91F_DBGU_Frame("Toggling LED 2\n\r");
104 41333333 (no author)
		led_toggle(2);
105
		break;
106 568a1b28 laforge
	case '9':
107 ca4e8055 Min Xu
		AT91F_DBGU_Frame("Resetting SAM7\n\r");
108 568a1b28 laforge
		AT91F_RSTSoftReset(AT91C_BASE_RSTC, AT91C_RSTC_PROCRST|
109
				   AT91C_RSTC_PERRST|AT91C_RSTC_EXTRST);
110
		break;
111 f57b548d (no author)
	default:
112 9d0d7022 (no author)
		if (_main_dbgu(value) < 0)
113 ca4e8055 Min Xu
			AT91F_DBGU_Frame("\n\r");
114 f57b548d (no author)
		break;
115
	}			// end switch
116
}
117
118 28a12f7d (no author)
void dbgu_rb_init(void);
119 f57b548d (no author)
//*----------------------------------------------------------------------------
120
//* \fn    AT91F_DBGU_Init
121
//* \brief This function is used to send a string through the DBGU channel (Very low level debugging)
122
//*----------------------------------------------------------------------------
123
void AT91F_DBGU_Init(void)
124
{
125 ea85282e Harald Welte
	unsigned int rst_status = AT91F_RSTGetStatus(AT91C_BASE_RSTC);
126
127 28a12f7d (no author)
	dbgu_rb_init();
128
129 f57b548d (no author)
	//* Open PIO for DBGU
130
	AT91F_DBGU_CfgPIO();
131
	//* Enable Transmitter & receivier
132
	((AT91PS_USART) AT91C_BASE_DBGU)->US_CR =
133
	    AT91C_US_RSTTX | AT91C_US_RSTRX;
134
135
	//* Configure DBGU
136
	AT91F_US_Configure((AT91PS_USART) AT91C_BASE_DBGU,	// DBGU base address
137
			   MCK, AT91C_US_ASYNC_MODE,	// Mode Register to be programmed
138
			   AT91C_DBGU_BAUD,	// Baudrate to be programmed
139
			   0);	// Timeguard to be programmed
140
141
	//* Enable Transmitter & receivier
142
	((AT91PS_USART) AT91C_BASE_DBGU)->US_CR = AT91C_US_RXEN | AT91C_US_TXEN;
143
144
	//* Enable USART IT error and AT91C_US_ENDRX
145
	AT91F_US_EnableIt((AT91PS_USART) AT91C_BASE_DBGU, AT91C_US_RXRDY);
146
147
	//* open interrupt
148 bfff30bf laforge
	sysirq_register(AT91SAM7_SYSIRQ_DBGU, &DBGU_irq_handler);
149 168cbe5f (no author)
150 ca4e8055 Min Xu
	debugp("\n\r");
151
	debugp("(C) 2006-2011 by Harald Welte <hwelte@hmw-consulting.de>\n\r"
152 833d0d5e laforge
			  "This software is FREE SOFTWARE licensed under GNU GPL\n\r");
153 ca4e8055 Min Xu
	debugp("Version " COMPILE_SVNREV
154 833d0d5e laforge
			  " compiled " COMPILE_DATE
155
			  " by " COMPILE_BY "\n\r\n\r");
156 ca4e8055 Min Xu
	debugp("\n\rDEBUG Interface:\n\r"
157 833d0d5e laforge
			  "0) Set Pull-up 1) Clear Pull-up 2) Toggle LED1 3) "
158
			  "Toggle LED2\r\n9) Reset\n\r");
159 ea85282e Harald Welte
160 ca4e8055 Min Xu
	debugp("RSTC_SR=0x%08x\n\r", rst_status);
161 f57b548d (no author)
}
162
163 6c6b1878 laforge
/*
164
 * Disable the PIO assignments for the DBGU
165
 */
166
void AT91F_DBGU_Fini(void)
167
{
168
	((AT91PS_USART) AT91C_BASE_DBGU)->US_CR = AT91C_US_RXDIS | AT91C_US_TXDIS;
169
	AT91F_PIO_CfgOutput(AT91C_BASE_PIOA, AT91C_PA10_DTXD);
170
	AT91F_PIO_CfgInput(AT91C_BASE_PIOA, AT91C_PA9_DRXD);
171
	// Maybe FIXME, do more? -- Henryk Plötz <henryk@ploetzli.ch>
172
}
173
174 f57b548d (no author)
//*----------------------------------------------------------------------------
175
//* \fn    AT91F_DBGU_Frame
176 ca4e8055 Min Xu
//* \brief This function is used to send a string through the DBGU channel (Very low level debugging)
177 f57b548d (no author)
//*----------------------------------------------------------------------------
178
void AT91F_DBGU_Frame(char *buffer)
179
{
180 ca4e8055 Min Xu
	unsigned long intcFlags;
181
	unsigned int len = 0;
182
	while (buffer[len++]) ALLOW_INTERRUPT_LOOP;
183
	len--;
184
	local_irq_save(intcFlags);
185
	AT91C_BASE_DBGU->DBGU_PTCR = 1 << 9; // Disable transfer
186
	if (AT91C_BASE_DBGU->DBGU_TNCR) {
187
		AT91C_BASE_DBGU->DBGU_PTCR = 1 << 8;  // Resume transfer
188
		local_irq_restore(intcFlags);
189
		while ((AT91C_BASE_DBGU->DBGU_CSR & AT91C_US_TXBUFE) == 0) ALLOW_INTERRUPT_LOOP;
190
		local_irq_save(intcFlags);
191
		AT91C_BASE_DBGU->DBGU_PTCR = 1 << 9; // Disable transfer
192
		AT91C_BASE_DBGU->DBGU_TPR = (unsigned)buffer;
193
		AT91C_BASE_DBGU->DBGU_TCR = len;
194
	} else if (AT91C_BASE_DBGU->DBGU_TCR) {
195
		AT91C_BASE_DBGU->DBGU_TNPR = (unsigned)buffer;
196
		AT91C_BASE_DBGU->DBGU_TNCR = len;
197
	} else {
198
		AT91C_BASE_DBGU->DBGU_TPR = (unsigned)buffer;
199
		AT91C_BASE_DBGU->DBGU_TCR = len;
200 f57b548d (no author)
	}
201 ca4e8055 Min Xu
	AT91C_BASE_DBGU->DBGU_PTCR = 1 << 8;  // Resume transfer
202
	local_irq_restore(intcFlags);
203
	/* Return ONLY after we complete Transfer */
204
	while ((AT91C_BASE_DBGU->DBGU_CSR & AT91C_US_TXBUFE) == 0) ALLOW_INTERRUPT_LOOP;
205 f57b548d (no author)
}
206
207
//*----------------------------------------------------------------------------
208
//* \fn    AT91F_US_Get
209
//* \brief Get a Char to USART
210
//*----------------------------------------------------------------------------
211
int AT91F_DBGU_Get(char *val)
212
{
213
	if ((AT91F_US_RxReady((AT91PS_USART) AT91C_BASE_DBGU)) == 0)
214 9164ed93 (no author)
		return (0);
215 f57b548d (no author)
	else {
216
		*val = AT91F_US_GetChar((AT91PS_USART) AT91C_BASE_DBGU);
217 9164ed93 (no author)
		return (-1);
218 f57b548d (no author)
	}
219
}
220
221
// mthomas: function not used in this application. avoid
222
//  linking huge newlib code for sscanf.
223
224 41333333 (no author)
#ifdef DEBUG
225
#include <stdio.h>
226
#include <stdarg.h>
227
#include <string.h>
228 60d3bef2 (no author)
const char *
229
hexdump(const void *data, unsigned int len)
230
{
231
	static char string[256];
232
	unsigned char *d = (unsigned char *) data;
233
	unsigned int i, left;
234
235
	string[0] = '\0';
236
	left = sizeof(string);
237
	for (i = 0; len--; i += 3) {
238
		if (i >= sizeof(string) -4)
239
			break;
240
		snprintf(string+i, 4, " %02x", *d++);
241
	}
242
	return string;
243
}
244
245 28a12f7d (no author)
struct dbgu {
246 ca4e8055 Min Xu
	char buf[DEBUG_BUFFER_SIZE];
247
	/* Since debugp appears to require to be re-entrant, we need a
248
	 * bit more state variables
249
	 * in_head	Position where incoming *append* characters have
250
	 * 		finished copy to buffer
251
	 * in_tail	Position where NEW incoming *append* characters
252
	 * 		should COPY to
253
	 * out_head	Position where the LAST *possibly incomplete*
254
	 * 		dbgu_rb_flush started from
255
	 * out_tail	Position where the NEXT dbug_rb_flush should
256
	 * 		start from
257
	 * The position in the RING should be in this order:
258
	 * --> out_tail --> in_head --> in_tail --> out_head -->
259
	 */
260
	volatile unsigned int in_head, in_tail, out_head, out_tail;
261
262
	/* flush_stack is to indicate rb_flush is being executed, NOT to
263
	 * execute again */
264
	volatile unsigned int flush_stack;
265
266
	/* append_stack is to indicate the re-entrack stack order of the
267
	 * current dbgu_append call */
268
	volatile unsigned int append_stack;
269 28a12f7d (no author)
};
270 ca4e8055 Min Xu
271 28a12f7d (no author)
static struct dbgu dbgu;
272
273
void dbgu_rb_init(void)
274
{
275 ca4e8055 Min Xu
	dbgu.in_head = dbgu.in_tail = dbgu.out_head = dbgu.out_tail = 0;
276
	dbgu.flush_stack = dbgu.append_stack = 0;
277 28a12f7d (no author)
}
278
279
/* flush pending data from debug ring buffer to serial port */
280
281 ca4e8055 Min Xu
static void dbgu_rb_flush(void)
282 28a12f7d (no author)
{
283 ca4e8055 Min Xu
	unsigned long intcFlags;
284
	unsigned int flush_stack, start, len;
285
	if (dbgu.in_head == dbgu.out_tail)
286
		return;
287
288
	/* Transmit can ONLY be disabled when Interrupt is disabled.  We
289
	 * don't want to be interrupted while Transmit is disabled. */
290
	local_irq_save(intcFlags);
291
	flush_stack = dbgu.flush_stack;
292
	dbgu.flush_stack = 1;
293
	if (flush_stack) {
294
		local_irq_restore(intcFlags);
295
		return;
296 28a12f7d (no author)
	}
297 ca4e8055 Min Xu
	AT91C_BASE_DBGU->DBGU_PTCR = 1 << 9; // Disable transfer
298
	start = (unsigned)dbgu.buf + dbgu.out_tail;
299
	len = (dbgu.in_head - dbgu.out_tail) & DEBUG_BUFFER_MASK;
300
	if (dbgu.in_head > dbgu.out_tail || dbgu.in_head == 0) { // Just 1 fragmentf
301
		if (AT91C_BASE_DBGU->DBGU_TNCR) {
302
			if (AT91C_BASE_DBGU->DBGU_TNPR + AT91C_BASE_DBGU->DBGU_TNCR == start) {
303
				AT91C_BASE_DBGU->DBGU_TNCR += len;
304
			} else {
305
				AT91C_BASE_DBGU->DBGU_PTCR = 1 << 8;  // Resume transfer
306
				local_irq_restore(intcFlags);
307
				while ((AT91C_BASE_DBGU->DBGU_CSR & AT91C_US_TXBUFE) == 0) ALLOW_INTERRUPT_LOOP;
308
				dbgu.out_head = dbgu.out_tail; // in case we are interrupted, they may need more space
309
				/* Since the ONLY place where Transmit is started is dbgu_rb_flush and AT91F_DBGU_Frame and:
310
				 * 1) AT91F_DBGU_Frame always leave the dbgu in a TX completed state
311
				 * 2) dbgu_rb is non-reentrant by safeguard at the beginning of this routing
312
				 * We can assume that after this INTERRUPTIBLE while loop, there are NO data to be transmitted
313
				 */
314
				local_irq_save(intcFlags);
315
				AT91C_BASE_DBGU->DBGU_PTCR = 1 << 9; // Disable transfer
316
				AT91C_BASE_DBGU->DBGU_TPR = start;
317
				AT91C_BASE_DBGU->DBGU_TCR = len;
318
			}
319
		} else if (AT91C_BASE_DBGU->DBGU_TCR) {
320
		  if (AT91C_BASE_DBGU->DBGU_TPR + AT91C_BASE_DBGU->DBGU_TCR == start) {
321
		    dbgu.out_head = AT91C_BASE_DBGU->DBGU_TPR - (unsigned)dbgu.buf;
322
		    AT91C_BASE_DBGU->DBGU_TCR += len;
323
		  } else {
324
		    AT91C_BASE_DBGU->DBGU_TNPR = start;
325
		    AT91C_BASE_DBGU->DBGU_TNCR = len;
326
		  }
327
		} else {
328
		  AT91C_BASE_DBGU->DBGU_TPR = start;
329
		  AT91C_BASE_DBGU->DBGU_TCR = len;
330
		  dbgu.out_head = dbgu.out_tail;
331
		}
332
	} else { // 2 fragments
333
		if ((AT91C_BASE_DBGU->DBGU_CSR & AT91C_US_TXBUFE) == 0) {
334
			AT91C_BASE_DBGU->DBGU_PTCR = 1 << 8;  // Resume transfer
335
			local_irq_restore(intcFlags);
336
			while ((AT91C_BASE_DBGU->DBGU_CSR & AT91C_US_TXBUFE) == 0) ALLOW_INTERRUPT_LOOP;
337
			dbgu.out_head = dbgu.out_tail; // in case we are interrupted, they may need more space
338
			local_irq_save(intcFlags);
339
			AT91C_BASE_DBGU->DBGU_PTCR = 1 << 9; // Disable transfer
340
		}
341
		AT91C_BASE_DBGU->DBGU_TPR = start;
342
		AT91C_BASE_DBGU->DBGU_TCR = DEBUG_BUFFER_SIZE - dbgu.out_tail;
343
		AT91C_BASE_DBGU->DBGU_TNPR = (unsigned)dbgu.buf;
344
		AT91C_BASE_DBGU->DBGU_TNCR = dbgu.in_head;
345
		dbgu.out_head = dbgu.out_tail;
346
	}
347
	AT91C_BASE_DBGU->DBGU_PTCR = 1 << 8;  // Resume transfer
348
	dbgu.out_tail = dbgu.in_head;
349
	dbgu.flush_stack = 0;
350
	local_irq_restore(intcFlags);
351 28a12f7d (no author)
}
352
353
void dbgu_rb_append(char *data, int len)
354
{
355 ca4e8055 Min Xu
	/* Rules:
356
	 * 1) ONLY the LOWEST order of dbgu_rb_append CAN update in_head;
357
	 * 2) WHEN updateing in_head, always set it to the current in_tail (since all higher order dbgu_rb_append have completed
358
	 * 3) ONLY the LOWEST order of dbgu_rb_append MAY call dbgu_rb_flush
359
	 */
360
	unsigned long intcFlags;
361
	unsigned int append_stack, avail, local_head;
362
	if (len <= 0)
363
		return;
364 28a12f7d (no author)
	
365 ca4e8055 Min Xu
	local_irq_save(intcFlags);
366
	append_stack = dbgu.append_stack++;
367
	if (AT91C_BASE_DBGU->DBGU_CSR & AT91C_US_TXBUFE)
368
		dbgu.out_head = dbgu.out_tail;
369
	avail = (dbgu.out_head - 1 - dbgu.in_tail) & DEBUG_BUFFER_MASK;
370
	local_head = (unsigned)len;
371
	if (local_head > avail) {
372 478c2332 Min Xu
		local_irq_restore(intcFlags);
373
		while ((AT91C_BASE_DBGU->DBGU_CSR & AT91C_US_TXBUFE) == 0);
374
		local_irq_save(intcFlags);
375 ca4e8055 Min Xu
		while ((AT91C_BASE_DBGU->DBGU_CSR & AT91C_US_TXBUFE) == 0);
376
		dbgu.out_head = dbgu.out_tail;
377
		avail = (dbgu.out_head - 1 - dbgu.in_tail) & DEBUG_BUFFER_MASK;
378
		if (local_head > avail) {
379
			local_head -= avail;
380
			AT91C_BASE_DBGU->DBGU_TPR = (unsigned)data;
381
			AT91C_BASE_DBGU->DBGU_TCR = local_head;
382
			data += local_head;
383
			len = avail;
384
		}
385 28a12f7d (no author)
	}
386 ca4e8055 Min Xu
	local_head = dbgu.in_tail;
387
	dbgu.in_tail += len;
388
	dbgu.in_tail &= DEBUG_BUFFER_MASK;
389
	local_irq_restore(intcFlags);
390
	if (dbgu.out_head <= local_head) {
391
		// We may have to wrap around: out_head won't change because NO call to flush will be made YET
392
		avail = DEBUG_BUFFER_SIZE - local_head;
393
		if (avail >= (unsigned)len) {
394
			memcpy(dbgu.buf + local_head, data, (size_t)len);
395
		} else {
396
			memcpy(dbgu.buf + local_head, data, (size_t)avail);
397
			memcpy(dbgu.buf, data + avail, (size_t)(len - avail));
398
		}
399
	} else {
400
		memcpy(dbgu.buf + local_head, data, len);
401
	}
402
	local_irq_save(intcFlags);
403
	dbgu.append_stack--;
404
	if (!append_stack)
405
		dbgu.in_head = dbgu.in_tail;
406
	local_irq_restore(intcFlags);
407
	if (!append_stack)
408
		dbgu_rb_flush();
409 28a12f7d (no author)
}
410
411 41333333 (no author)
static char dbg_buf[256];
412 ca4e8055 Min Xu
static int line_num = 0;
413 41333333 (no author)
void debugp(const char *format, ...)
414
{
415
	va_list ap;
416
417
	va_start(ap, format);
418 ca4e8055 Min Xu
	sprintf(dbg_buf, "[%06X] ", line_num++);
419
	vsnprintf(dbg_buf + 9, sizeof(dbg_buf)-10, format, ap);
420 41333333 (no author)
	va_end(ap);
421
422 caf50003 (no author)
	dbg_buf[sizeof(dbg_buf)-1] = '\0';			
423
	//AT91F_DBGU_Frame(dbg_buf);
424 28eb4a57 laforge
#ifdef DEBUG_UNBUFFERED
425
	AT91F_DBGU_Printk(dbg_buf);
426
#else
427 28a12f7d (no author)
	dbgu_rb_append(dbg_buf, strlen(dbg_buf));
428 28eb4a57 laforge
#endif
429 41333333 (no author)
}
430 767a06ec (no author)
#else
431
void dbgu_rb_flush(void) {}
432
void dbgu_rb_init(void) {}
433 28a12f7d (no author)
#endif
Add picture from clipboard (Maximum size: 48.8 MB)