summaryrefslogtreecommitdiff
path: root/firmware/src/os/dbgu.c
blob: 113208e634a38a1f4646f619d667eb330b2d114a (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
/* 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>

//#define DEBUG_UNBUFFERED

#define USART_SYS_LEVEL 4
/*---------------------------- Global Variable ------------------------------*/
//*--------------------------1--------------------------------------------------
//* \fn    AT91F_DBGU_Printk
//* \brief This function is used to send a string through the DBGU channel
//*----------------------------------------------------------------------------
void AT91F_DBGU_Ready(void)
{
	while (!(AT91C_BASE_DBGU->DBGU_CSR & AT91C_US_TXEMPTY)) ;
}

//*----------------------------------------------------------------------------
//* 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;
	AT91F_DBGU_Ready();
	// Jump in reset
	pfct();
}

//*----------------------------------------------------------------------------
//* Function Name       : DBGU_irq_handler
//* Object              : C handler interrupt function called by the sysirq
//*                       demultiplexer
//*----------------------------------------------------------------------------
static void DBGU_irq_handler(u_int32_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_Printk("Set Pull up\n\r");
		// Reset Application
		Send_reset();
		break;
	case '2':
		AT91F_DBGU_Printk("Toggling LED 1\n\r");
		led_toggle(1);
		break;
	case '3':
		AT91F_DBGU_Printk("Toggling LED 2\n\r");
		led_toggle(2);
		break;
	case '9':
		AT91F_DBGU_Printk("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_Printk("\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);

	AT91F_DBGU_Printk("\n\r");
	AT91F_DBGU_Printk("(C) 2006-2011 by Harald Welte <hwelte@hmw-consulting.de>\n\r"
			  "This software is FREE SOFTWARE licensed under GNU GPL\n\r");
	AT91F_DBGU_Printk("Version " COMPILE_SVNREV
			  " compiled " COMPILE_DATE
			  " by " COMPILE_BY "\n\r\n\r");
	AT91F_DBGU_Printk("\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", 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_Printk
//* \brief This function is used to send a string through the DBGU channel (Very low level debugging)
//*----------------------------------------------------------------------------
void AT91F_DBGU_Printk(char *buffer)
{
	while (*buffer != '\0') {
		while (!AT91F_US_TxReady((AT91PS_USART) AT91C_BASE_DBGU)) ;
		AT91F_US_PutChar((AT91PS_USART) AT91C_BASE_DBGU, *buffer++);
	}
}

//*----------------------------------------------------------------------------
//* \fn    AT91F_DBGU_Frame
//* \brief This function is used to send a string through the DBGU channel
//*----------------------------------------------------------------------------
void AT91F_DBGU_Frame(char *buffer)
{
	unsigned char len;

	for (len = 0; buffer[len] != '\0'; len++) {
	}

	AT91F_US_SendFrame((AT91PS_USART) AT91C_BASE_DBGU, 
			   (unsigned char *)buffer, len, 0, 0);

}

//*----------------------------------------------------------------------------
//* \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[4096];
	char *next_inbyte;
	char *next_outbyte;
};
static struct dbgu dbgu;

void dbgu_rb_init(void)
{
	memset(dbgu.buf, 0, sizeof(dbgu.buf));
	dbgu.next_inbyte = &dbgu.buf[0];
	dbgu.next_outbyte = &dbgu.buf[0];
}

/* pull one char out of debug ring buffer */
static int dbgu_rb_pull(char *ret)
{
	unsigned long flags;

	local_irq_save(flags);

	if (dbgu.next_outbyte == dbgu.next_inbyte) {
		local_irq_restore(flags);
		return -1;
	}

	*ret = *dbgu.next_outbyte;

	dbgu.next_outbyte++;
	if (dbgu.next_outbyte == &dbgu.buf[0]+sizeof(dbgu.buf)) {
		//AT91F_DBGU_Printk("WRAP DURING PULL\r\n");
		dbgu.next_outbyte = &dbgu.buf[0];
	} else if (dbgu.next_outbyte > &dbgu.buf[0]+sizeof(dbgu.buf)) {
		//AT91F_DBGU_Printk("OUTBYTE > END_OF_BUF!!\r\n");
		dbgu.next_outbyte -= sizeof(dbgu.buf);
	}

	local_irq_restore(flags);

	return 0;
}

static void __rb_flush(void)
{
	char ch;
	while (dbgu_rb_pull(&ch) >= 0) {
		while (!AT91F_US_TxReady((AT91PS_USART) AT91C_BASE_DBGU)) ;
		AT91F_US_PutChar((AT91PS_USART) AT91C_BASE_DBGU, ch);
	}
}

/* flush pending data from debug ring buffer to serial port */
void dbgu_rb_flush(void)
{
	__rb_flush();
}

static void __dbgu_rb_append(char *data, int len)
{
	char *pos = dbgu.next_inbyte;

	dbgu.next_inbyte += len;
	if (dbgu.next_inbyte >= &dbgu.buf[0]+sizeof(dbgu.buf)) {
		AT91F_DBGU_Printk("WRAP DURING APPEND\r\n");
		dbgu.next_inbyte -= sizeof(dbgu.buf);
	}

	memcpy(pos, data, len);
}

void dbgu_rb_append(char *data, int len)
{
	unsigned long flags;
	int bytes_left;
	char *data_cur;
	
	local_irq_save(flags);

	bytes_left = &dbgu.buf[0]+sizeof(dbgu.buf)-dbgu.next_inbyte;
	data_cur = data;

	if (len > bytes_left) {
		AT91F_DBGU_Printk("LEN > BYTES_LEFT\r\n");
		__rb_flush();
		__dbgu_rb_append(data_cur, bytes_left);
		len -= bytes_left;
		data_cur += bytes_left;
	}
	__dbgu_rb_append(data_cur, len);

	local_irq_restore(flags);
}

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

	va_start(ap, format);
	vsnprintf(dbg_buf, sizeof(dbg_buf)-1, 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