summaryrefslogtreecommitdiff
path: root/firmware/src/os/dbgu.c
blob: 7f1df44c8635d19b367ebd3cdebf6c135a6fc255 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/* AT91SAM7 debug function implementation for OpenPCD
 * (C) 2006 by Harald Welte <hwelte@hmw-consulting.de>
 *
 * I based this on existing BSD-style licensed code, please see below.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by 
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

/*----------------------------------------------------------------------------
 *         ATMEL Microcontroller Software Support  -  ROUSSET  -
 *----------------------------------------------------------------------------
 * The software is delivered "AS IS" without warranty or condition of any
 * kind, either express, implied or statutory. This includes without
 * limitation any warranty or condition with respect to merchantability or
 * fitness for any particular purpose, or against the infringements of
 * intellectual property rights of others.
 *----------------------------------------------------------------------------
 * File Name           : Debug.c
 * Object              : Debug menu
 * Creation            : JPP   14/Sep/2004
 * 1.1 29/Aug/05 JPP   : Update AIC definion
 *----------------------------------------------------------------------------*/

// Include Standard files
#include <board.h>
#include <os/dbgu.h>
#include "../openpcd.h"
#include <os/led.h>
#include <os/main.h>
#include <os/system_irq.h>
#include <os/pcd_enumerate.h>
#include <asm/system.h>
#include <compile.h>

/* In case we find that while (); is non interruptible, we may need to
 * uncommend this line: */
// #define ALLOW_INTERRUPT_LOOP asm("nop;nop;nop;nop;nop;nop;nop;nop;")

#define ALLOW_INTERRUPT_LOOP

/* DEBUG_BUFFER_SIZE MUST BE A POWER OF 2 */
#define DEBUG_BUFFER_SIZE     (1 << 10)
#define DEBUG_BUFFER_MASK     (DEBUG_BUFFER_SIZE - 1)

/*---------------------------- Global Variable ------------------------------*/

//*----------------------------------------------------------------------------
//* Function Name       : Send_reset
//* Object              : Acknoledeg AIC and send reset
//*----------------------------------------------------------------------------
static void Send_reset(void)
{
	void (*pfct) (void) = (void (*)(void))0x00000000;

	// Acknoledge the interrupt
	// Mark the End of Interrupt on the AIC
	AT91C_BASE_AIC->AIC_EOICR = 0;
	while (!(AT91C_BASE_DBGU->DBGU_CSR & AT91C_US_TXEMPTY)) ;
	// Jump in reset
	pfct();
}

//*----------------------------------------------------------------------------
//* Function Name       : DBGU_irq_handler
//* Object              : C handler interrupt function called by the sysirq
//*                       demultiplexer
//*----------------------------------------------------------------------------
static void DBGU_irq_handler(uint32_t sr)
{
	static char value;

	AT91F_DBGU_Get(&value);
	switch (value) {
	case '0':		//* info
		AT91F_DBGU_Frame("Clear Pull up\n\r");
		// Set
		udp_pullup_on();
		break;
	case '1':		//* info
		udp_pullup_off();
		AT91F_DBGU_Frame("Set Pull up\n\r");
		// Reset Application
		Send_reset();
		break;
	case '2':
		AT91F_DBGU_Frame("Toggling LED 1\n\r");
		led_toggle(1);
		break;
	case '3':
		AT91F_DBGU_Frame("Toggling LED 2\n\r");
		led_toggle(2);
		break;
	case '9':
		AT91F_DBGU_Frame("Resetting SAM7\n\r");
		AT91F_RSTSoftReset(AT91C_BASE_RSTC, AT91C_RSTC_PROCRST|
				   AT91C_RSTC_PERRST|AT91C_RSTC_EXTRST);
		break;
	default:
		if (_main_dbgu(value) < 0)
			AT91F_DBGU_Frame("\n\r");
		break;
	}			// end switch
}

void dbgu_rb_init(void);
//*----------------------------------------------------------------------------
//* \fn    AT91F_DBGU_Init
//* \brief This function is used to send a string through the DBGU channel (Very low level debugging)
//*----------------------------------------------------------------------------
void AT91F_DBGU_Init(void)
{
	unsigned int rst_status = AT91F_RSTGetStatus(AT91C_BASE_RSTC);

	dbgu_rb_init();

	//* Open PIO for DBGU
	AT91F_DBGU_CfgPIO();
	//* Enable Transmitter & receivier
	((AT91PS_USART) AT91C_BASE_DBGU)->US_CR =
	    AT91C_US_RSTTX | AT91C_US_RSTRX;

	//* Configure DBGU
	AT91F_US_Configure((AT91PS_USART) AT91C_BASE_DBGU,	// DBGU base address
			   MCK, AT91C_US_ASYNC_MODE,	// Mode Register to be programmed
			   AT91C_DBGU_BAUD,	// Baudrate to be programmed
			   0);	// Timeguard to be programmed

	//* Enable Transmitter & receivier
	((AT91PS_USART) AT91C_BASE_DBGU)->US_CR = AT91C_US_RXEN | AT91C_US_TXEN;

	//* Enable USART IT error and AT91C_US_ENDRX
	AT91F_US_EnableIt((AT91PS_USART) AT91C_BASE_DBGU, AT91C_US_RXRDY);

	//* open interrupt
	sysirq_register(AT91SAM7_SYSIRQ_DBGU, &DBGU_irq_handler);

	debugp("\n\r");
	debugp("(C) 2006-2011 by Harald Welte <hwelte@hmw-consulting.de>\n\r"
			  "This software is FREE SOFTWARE licensed under GNU GPL\n\r");
	debugp("Version " COMPILE_SVNREV
			  " compiled " COMPILE_DATE
			  " by " COMPILE_BY "\n\r\n\r");
	debugp("\n\rDEBUG Interface:\n\r"
			  "0) Set Pull-up 1) Clear Pull-up 2) Toggle LED1 3) "
			  "Toggle LED2\r\n9) Reset\n\r");

	debugp("RSTC_SR=0x%08x\n\r", rst_status);
}

/*
 * Disable the PIO assignments for the DBGU
 */
void AT91F_DBGU_Fini(void)
{
	((AT91PS_USART) AT91C_BASE_DBGU)->US_CR = AT91C_US_RXDIS | AT91C_US_TXDIS;
	AT91F_PIO_CfgOutput(AT91C_BASE_PIOA, AT91C_PA10_DTXD);
	AT91F_PIO_CfgInput(AT91C_BASE_PIOA, AT91C_PA9_DRXD);
	// Maybe FIXME, do more? -- Henryk Plötz <henryk@ploetzli.ch>
}

//*----------------------------------------------------------------------------
//* \fn    AT91F_DBGU_Frame
//* \brief This function is used to send a string through the DBGU channel (Very low level debugging)
//*----------------------------------------------------------------------------
void AT91F_DBGU_Frame(char *buffer)
{
	unsigned long intcFlags;
	unsigned int len = 0;
	while (buffer[len++]) ALLOW_INTERRUPT_LOOP;
	len--;
	local_irq_save(intcFlags);
	AT91C_BASE_DBGU->DBGU_PTCR = 1 << 9; // Disable transfer
	if (AT91C_BASE_DBGU->DBGU_TNCR) {
		AT91C_BASE_DBGU->DBGU_PTCR = 1 << 8;  // Resume transfer
		local_irq_restore(intcFlags);
		while ((AT91C_BASE_DBGU->DBGU_CSR & AT91C_US_TXBUFE) == 0) ALLOW_INTERRUPT_LOOP;
		local_irq_save(intcFlags);
		AT91C_BASE_DBGU->DBGU_PTCR = 1 << 9; // Disable transfer
		AT91C_BASE_DBGU->DBGU_TPR = (unsigned)buffer;
		AT91C_BASE_DBGU->DBGU_TCR = len;
	} else if (AT91C_BASE_DBGU->DBGU_TCR) {
		AT91C_BASE_DBGU->DBGU_TNPR = (unsigned)buffer;
		AT91C_BASE_DBGU->DBGU_TNCR = len;
	} else {
		AT91C_BASE_DBGU->DBGU_TPR = (unsigned)buffer;
		AT91C_BASE_DBGU->DBGU_TCR = len;
	}
	AT91C_BASE_DBGU->DBGU_PTCR = 1 << 8;  // Resume transfer
	local_irq_restore(intcFlags);
	/* Return ONLY after we complete Transfer */
	while ((AT91C_BASE_DBGU->DBGU_CSR & AT91C_US_TXBUFE) == 0) ALLOW_INTERRUPT_LOOP;
}

//*----------------------------------------------------------------------------
//* \fn    AT91F_US_Get
//* \brief Get a Char to USART
//*----------------------------------------------------------------------------
int AT91F_DBGU_Get(char *val)
{
	if ((AT91F_US_RxReady((AT91PS_USART) AT91C_BASE_DBGU)) == 0)
		return (0);
	else {
		*val = AT91F_US_GetChar((AT91PS_USART) AT91C_BASE_DBGU);
		return (-1);
	}
}

// mthomas: function not used in this application. avoid
//  linking huge newlib code for sscanf.

#ifdef DEBUG
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
const char *
hexdump(const void *data, unsigned int len)
{
	static char string[256];
	unsigned char *d = (unsigned char *) data;
	unsigned int i, left;

	string[0] = '\0';
	left = sizeof(string);
	for (i = 0; len--; i += 3) {
		if (i >= sizeof(string) -4)
			break;
		snprintf(string+i, 4, " %02x", *d++);
	}
	return string;
}

struct dbgu {
	char buf[DEBUG_BUFFER_SIZE];
	/* Since debugp appears to require to be re-entrant, we need a
	 * bit more state variables
	 * in_head	Position where incoming *append* characters have
	 * 		finished copy to buffer
	 * in_tail	Position where NEW incoming *append* characters
	 * 		should COPY to
	 * out_head	Position where the LAST *possibly incomplete*
	 * 		dbgu_rb_flush started from
	 * out_tail	Position where the NEXT dbug_rb_flush should
	 * 		start from
	 * The position in the RING should be in this order:
	 * --> out_tail --> in_head --> in_tail --> out_head -->
	 */
	volatile unsigned int in_head, in_tail, out_head, out_tail;

	/* flush_stack is to indicate rb_flush is being executed, NOT to
	 * execute again */
	volatile unsigned int flush_stack;

	/* append_stack is to indicate the re-entrack stack order of the
	 * current dbgu_append call */
	volatile unsigned int append_stack;
};

static struct dbgu dbgu;

void dbgu_rb_init(void)
{
	dbgu.in_head = dbgu.in_tail = dbgu.out_head = dbgu.out_tail = 0;
	dbgu.flush_stack = dbgu.append_stack = 0;
}

/* flush pending data from debug ring buffer to serial port */

static void dbgu_rb_flush(void)
{
	unsigned long intcFlags;
	unsigned int flush_stack, start, len;
	if (dbgu.in_head == dbgu.out_tail)
		return;

	/* Transmit can ONLY be disabled when Interrupt is disabled.  We
	 * don't want to be interrupted while Transmit is disabled. */
	local_irq_save(intcFlags);
	flush_stack = dbgu.flush_stack;
	dbgu.flush_stack = 1;
	if (flush_stack) {
		local_irq_restore(intcFlags);
		return;
	}
	AT91C_BASE_DBGU->DBGU_PTCR = 1 << 9; // Disable transfer
	start = (unsigned)dbgu.buf + dbgu.out_tail;
	len = (dbgu.in_head - dbgu.out_tail) & DEBUG_BUFFER_MASK;
	if (dbgu.in_head > dbgu.out_tail || dbgu.in_head == 0) { // Just 1 fragmentf
		if (AT91C_BASE_DBGU->DBGU_TNCR) {
			if (AT91C_BASE_DBGU->DBGU_TNPR + AT91C_BASE_DBGU->DBGU_TNCR == start) {
				AT91C_BASE_DBGU->DBGU_TNCR += len;
			} else {
				AT91C_BASE_DBGU->DBGU_PTCR = 1 << 8;  // Resume transfer
				local_irq_restore(intcFlags);
				while ((AT91C_BASE_DBGU->DBGU_CSR & AT91C_US_TXBUFE) == 0) ALLOW_INTERRUPT_LOOP;
				dbgu.out_head = dbgu.out_tail; // in case we are interrupted, they may need more space
				/* Since the ONLY place where Transmit is started is dbgu_rb_flush and AT91F_DBGU_Frame and:
				 * 1) AT91F_DBGU_Frame always leave the dbgu in a TX completed state
				 * 2) dbgu_rb is non-reentrant by safeguard at the beginning of this routing
				 * We can assume that after this INTERRUPTIBLE while loop, there are NO data to be transmitted
				 */
				local_irq_save(intcFlags);
				AT91C_BASE_DBGU->DBGU_PTCR = 1 << 9; // Disable transfer
				AT91C_BASE_DBGU->DBGU_TPR = start;
				AT91C_BASE_DBGU->DBGU_TCR = len;
			}
		} else if (AT91C_BASE_DBGU->DBGU_TCR) {
		  if (AT91C_BASE_DBGU->DBGU_TPR + AT91C_BASE_DBGU->DBGU_TCR == start) {
		    dbgu.out_head = AT91C_BASE_DBGU->DBGU_TPR - (unsigned)dbgu.buf;
		    AT91C_BASE_DBGU->DBGU_TCR += len;
		  } else {
		    AT91C_BASE_DBGU->DBGU_TNPR = start;
		    AT91C_BASE_DBGU->DBGU_TNCR = len;
		  }
		} else {
		  AT91C_BASE_DBGU->DBGU_TPR = start;
		  AT91C_BASE_DBGU->DBGU_TCR = len;
		  dbgu.out_head = dbgu.out_tail;
		}
	} else { // 2 fragments
		if ((AT91C_BASE_DBGU->DBGU_CSR & AT91C_US_TXBUFE) == 0) {
			AT91C_BASE_DBGU->DBGU_PTCR = 1 << 8;  // Resume transfer
			local_irq_restore(intcFlags);
			while ((AT91C_BASE_DBGU->DBGU_CSR & AT91C_US_TXBUFE) == 0) ALLOW_INTERRUPT_LOOP;
			dbgu.out_head = dbgu.out_tail; // in case we are interrupted, they may need more space
			local_irq_save(intcFlags);
			AT91C_BASE_DBGU->DBGU_PTCR = 1 << 9; // Disable transfer
		}
		AT91C_BASE_DBGU->DBGU_TPR = start;
		AT91C_BASE_DBGU->DBGU_TCR = DEBUG_BUFFER_SIZE - dbgu.out_tail;
		AT91C_BASE_DBGU->DBGU_TNPR = (unsigned)dbgu.buf;
		AT91C_BASE_DBGU->DBGU_TNCR = dbgu.in_head;
		dbgu.out_head = dbgu.out_tail;
	}
	AT91C_BASE_DBGU->DBGU_PTCR = 1 << 8;  // Resume transfer
	dbgu.out_tail = dbgu.in_head;
	dbgu.flush_stack = 0;
	local_irq_restore(intcFlags);
}

void dbgu_rb_append(char *data, int len)
{
	/* Rules:
	 * 1) ONLY the LOWEST order of dbgu_rb_append CAN update in_head;
	 * 2) WHEN updateing in_head, always set it to the current in_tail (since all higher order dbgu_rb_append have completed
	 * 3) ONLY the LOWEST order of dbgu_rb_append MAY call dbgu_rb_flush
	 */
	unsigned long intcFlags;
	unsigned int append_stack, avail, local_head;
	if (len <= 0)
		return;
	
	local_irq_save(intcFlags);
	append_stack = dbgu.append_stack++;
	if (AT91C_BASE_DBGU->DBGU_CSR & AT91C_US_TXBUFE)
		dbgu.out_head = dbgu.out_tail;
	avail = (dbgu.out_head - 1 - dbgu.in_tail) & DEBUG_BUFFER_MASK;
	local_head = (unsigned)len;
	if (local_head > avail) {
		local_irq_restore(intcFlags);
		while ((AT91C_BASE_DBGU->DBGU_CSR & AT91C_US_TXBUFE) == 0);
		local_irq_save(intcFlags);
		while ((AT91C_BASE_DBGU->DBGU_CSR & AT91C_US_TXBUFE) == 0);
		dbgu.out_head = dbgu.out_tail;
		avail = (dbgu.out_head - 1 - dbgu.in_tail) & DEBUG_BUFFER_MASK;
		if (local_head > avail) {
			local_head -= avail;
			AT91C_BASE_DBGU->DBGU_TPR = (unsigned)data;
			AT91C_BASE_DBGU->DBGU_TCR = local_head;
			data += local_head;
			len = avail;
		}
	}
	local_head = dbgu.in_tail;
	dbgu.in_tail += len;
	dbgu.in_tail &= DEBUG_BUFFER_MASK;
	local_irq_restore(intcFlags);
	if (dbgu.out_head <= local_head) {
		// We may have to wrap around: out_head won't change because NO call to flush will be made YET
		avail = DEBUG_BUFFER_SIZE - local_head;
		if (avail >= (unsigned)len) {
			memcpy(dbgu.buf + local_head, data, (size_t)len);
		} else {
			memcpy(dbgu.buf + local_head, data, (size_t)avail);
			memcpy(dbgu.buf, data + avail, (size_t)(len - avail));
		}
	} else {
		memcpy(dbgu.buf + local_head, data, len);
	}
	local_irq_save(intcFlags);
	dbgu.append_stack--;
	if (!append_stack)
		dbgu.in_head = dbgu.in_tail;
	local_irq_restore(intcFlags);
	if (!append_stack)
		dbgu_rb_flush();
}

static char dbg_buf[256];
static int line_num = 0;
void debugp(const char *format, ...)
{
	va_list ap;

	va_start(ap, format);
	sprintf(dbg_buf, "[%06X] ", line_num++);
	vsnprintf(dbg_buf + 9, sizeof(dbg_buf)-10, format, ap);
	va_end(ap);

	dbg_buf[sizeof(dbg_buf)-1] = '\0';			
	//AT91F_DBGU_Frame(dbg_buf);
#ifdef DEBUG_UNBUFFERED
	AT91F_DBGU_Printk(dbg_buf);
#else
	dbgu_rb_append(dbg_buf, strlen(dbg_buf));
#endif
}
#else
void dbgu_rb_flush(void) {}
void dbgu_rb_init(void) {}
#endif
personal git repositories of Harald Welte. Your mileage may vary